Page 169 - 49A Field Guide to Genetic Programming
P. 169
B.3 Source Code 155
The code reads command line arguments using the standard args array.
Generally the code is quite standard and should be self-explanatory for
anyone who can program in Java, whether or not they have implemented a
GP system before. Therefore very few comments have been provided in the
source code.
The source is provided below.
/∗
1
2 ∗ Program: tiny gp . java
∗
3
4 ∗ Author: Riccardo Poli (email : rpoli@essex .ac.uk)
∗
5
∗/
6
7
8 import java . u t i l . ∗ ;
9 import java . io . ∗ ;
10 import java . text . DecimalFormat ;
11
12 public class tiny gp {
13 double [ ] f i t n e s s ;
14 char [ ] [ ] pop ;
15 static Random rd = new Random() ;
16 static final int
17 ADD = 110 ,
18 SUB = 111 ,
19 MUL = 112 ,
20 DIV = 113 ,
21 FSET START = ADD,
22 FSET END = DIV ;
23 static double [ ] x = new double [FSET START ] ;
24 static double minrandom , maxrandom ;
25 static char [ ] program ;
26 static int PC;
27 static int varnumber , f i t n e s s c a s e s , randomnumber ;
28 static double fbestpop = 0 . 0 , favgpop = 0 . 0 ;
29 static long seed ;
30 static double avg len ;
31 static final int
32 MAX LEN = 10000 ,
33 POPSIZE = 100000 ,
34 DEPTH = 5 ,
35 GENERATIONS = 100 ,
36 TSIZE = 2 ;
37 public static final double
38 PMUT PER NODE = 0.05 ,
39 CROSSOVER PROB = 0 . 9 ;
40 static double [ ] [ ] t a r g e t s ;
41
42 double run () { /∗ Interpreter ∗/
43 char p r i m i t i v e = program [PC++];
44 i f ( p r i m i t i v e < FSET START )
45 return ( x [ p r i m i t i v e ] ) ;
46 switch ( p r i m i t i v e ) {