1 | /* |
---|
2 | Copyright 2006 by Sean Luke |
---|
3 | Licensed under the Academic Free License version 3.0 |
---|
4 | See the file "LICENSE" for more information |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | package ec.app.twobox; |
---|
9 | import ec.util.*; |
---|
10 | import ec.*; |
---|
11 | import ec.gp.*; |
---|
12 | import ec.gp.koza.*; |
---|
13 | import ec.simple.*; |
---|
14 | |
---|
15 | /* |
---|
16 | * TwoBox.java |
---|
17 | * |
---|
18 | * Created: Mon Nov 1 15:46:19 1999 |
---|
19 | * By: Sean Luke |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * TwoBox implements the TwoBox problem, with or without ADFs, |
---|
24 | * as discussed in Koza-II. |
---|
25 | * |
---|
26 | <p><b>Parameters</b><br> |
---|
27 | <table> |
---|
28 | <tr><td valign=top><i>base</i>.<tt>data</tt><br> |
---|
29 | <font size=-1>classname, inherits or == ec.app.twobox.TwoBoxData</font></td> |
---|
30 | <td valign=top>(the class for the prototypical GPData object for the TwoBox problem)</td></tr> |
---|
31 | <tr><td valign=top><i>base</i>.<tt>size</tt><br> |
---|
32 | <font size=-1>int >= 1</font></td> |
---|
33 | <td valign=top>(the size of the training set)</td></tr> |
---|
34 | <tr><td valign=top><i>base</i>.<tt>range</tt><br> |
---|
35 | <font size=-1>int >= 1</font></td> |
---|
36 | <td valign=top>(the range of dimensional values in the training set -- they'll be integers 1...range inclusive)</td></tr> |
---|
37 | </table> |
---|
38 | |
---|
39 | <p><b>Parameter bases</b><br> |
---|
40 | <table> |
---|
41 | <tr><td valign=top><i>base</i>.<tt>data</tt></td> |
---|
42 | <td>species (the GPData object)</td></tr> |
---|
43 | </table> |
---|
44 | * |
---|
45 | * @author Sean Luke |
---|
46 | * @version 1.0 |
---|
47 | */ |
---|
48 | |
---|
49 | public class TwoBox extends GPProblem implements SimpleProblemForm |
---|
50 | { |
---|
51 | public static final String P_SIZE = "size"; |
---|
52 | public static final String P_RANGE= "range"; |
---|
53 | |
---|
54 | public int currentIndex; |
---|
55 | public int trainingSetSize; |
---|
56 | public int range; |
---|
57 | |
---|
58 | // these are read-only during evaluation-time, so |
---|
59 | // they can be just light-cloned and not deep cloned. |
---|
60 | // cool, huh? |
---|
61 | |
---|
62 | public double inputsl0[]; |
---|
63 | public double inputsw0[]; |
---|
64 | public double inputsh0[]; |
---|
65 | public double inputsl1[]; |
---|
66 | public double inputsw1[]; |
---|
67 | public double inputsh1[]; |
---|
68 | public double outputs[]; |
---|
69 | |
---|
70 | // we'll need to deep clone this one though. |
---|
71 | public TwoBoxData input; |
---|
72 | |
---|
73 | public final double func(final double l0, final double w0, |
---|
74 | final double h0, final double l1, |
---|
75 | final double w1, final double h1) |
---|
76 | { return l0*w0*h0-l1*w1*h1; } |
---|
77 | |
---|
78 | public Object clone() |
---|
79 | { |
---|
80 | // don't bother copying the inputs and outputs; they're read-only :-) |
---|
81 | // don't bother copying the currentIndex; it's transitory |
---|
82 | // but we need to copy our twobox data |
---|
83 | TwoBox myobj = (TwoBox) (super.clone()); |
---|
84 | |
---|
85 | myobj.input = (TwoBoxData)(input.clone()); |
---|
86 | return myobj; |
---|
87 | } |
---|
88 | |
---|
89 | public void setup(final EvolutionState state, |
---|
90 | final Parameter base) |
---|
91 | { |
---|
92 | // very important, remember this |
---|
93 | super.setup(state,base); |
---|
94 | |
---|
95 | trainingSetSize = state.parameters.getInt(base.push(P_SIZE),null,1); |
---|
96 | if (trainingSetSize<1) state.output.fatal("Training Set Size must be an integer greater than 0"); |
---|
97 | |
---|
98 | range = state.parameters.getInt(base.push(P_RANGE),null,1); |
---|
99 | if (trainingSetSize<1) state.output.fatal("Range must be an integer greater than 0"); |
---|
100 | |
---|
101 | // Compute our inputs so they |
---|
102 | // can be copied with clone later |
---|
103 | |
---|
104 | inputsl0 = new double[trainingSetSize]; |
---|
105 | inputsw0 = new double[trainingSetSize]; |
---|
106 | inputsh0 = new double[trainingSetSize]; |
---|
107 | inputsl1 = new double[trainingSetSize]; |
---|
108 | inputsw1 = new double[trainingSetSize]; |
---|
109 | inputsh1 = new double[trainingSetSize]; |
---|
110 | outputs = new double[trainingSetSize]; |
---|
111 | |
---|
112 | for(int x=0;x<trainingSetSize;x++) |
---|
113 | { |
---|
114 | inputsl0[x] = state.random[0].nextInt(range)+1; |
---|
115 | inputsw0[x] = state.random[0].nextInt(range)+1; |
---|
116 | inputsh0[x] = state.random[0].nextInt(range)+1; |
---|
117 | inputsl1[x] = state.random[0].nextInt(range)+1; |
---|
118 | inputsw1[x] = state.random[0].nextInt(range)+1; |
---|
119 | inputsh1[x] = state.random[0].nextInt(range)+1; |
---|
120 | outputs[x] = func(inputsl0[x],inputsw0[x],inputsh0[x], |
---|
121 | inputsl1[x],inputsw1[x],inputsh1[x]); |
---|
122 | state.output.println("{" + inputsl0[x]+ "," + inputsw0[x]+ "," + |
---|
123 | inputsh0[x]+ "," + inputsl1[x]+ "," + |
---|
124 | inputsw1[x]+ "," + inputsh1[x]+ "," + |
---|
125 | outputs[x] + "},",0); |
---|
126 | } |
---|
127 | |
---|
128 | // set up our input -- don't want to use the default base, it's unsafe |
---|
129 | input = (TwoBoxData) state.parameters.getInstanceForParameterEq( |
---|
130 | base.push(P_DATA), null, TwoBoxData.class); |
---|
131 | input.setup(state,base.push(P_DATA)); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | public void evaluate(final EvolutionState state, |
---|
136 | final Individual ind, |
---|
137 | final int subpopulation, |
---|
138 | final int threadnum) |
---|
139 | { |
---|
140 | if (!ind.evaluated) // don't bother reevaluating |
---|
141 | { |
---|
142 | int hits = 0; |
---|
143 | double sum = 0.0; |
---|
144 | double result; |
---|
145 | for (int y=0;y<trainingSetSize;y++) |
---|
146 | { |
---|
147 | currentIndex = y; |
---|
148 | ((GPIndividual)ind).trees[0].child.eval( |
---|
149 | state,threadnum,input,stack,((GPIndividual)ind),this); |
---|
150 | |
---|
151 | final double HIT_LEVEL = 0.01; |
---|
152 | final double PROBABLY_ZERO = 1.11E-15; |
---|
153 | final double BIG_NUMBER = 1.0e15; // the same as lilgp uses |
---|
154 | |
---|
155 | result = Math.abs(outputs[y] - input.x); |
---|
156 | |
---|
157 | // very slight math errors can creep in when evaluating |
---|
158 | // two equivalent by differently-ordered functions, like |
---|
159 | // x * (x*x*x + x*x) vs. x*x*x*x + x*x |
---|
160 | |
---|
161 | if (result<PROBABLY_ZERO) // slightly off |
---|
162 | result = 0.0; |
---|
163 | |
---|
164 | if (result > BIG_NUMBER) |
---|
165 | result = BIG_NUMBER; |
---|
166 | |
---|
167 | if (result <= HIT_LEVEL) hits++; // whatever! |
---|
168 | |
---|
169 | sum += result; } |
---|
170 | |
---|
171 | // the fitness better be KozaFitness! |
---|
172 | KozaFitness f = ((KozaFitness)ind.fitness); |
---|
173 | f.setStandardizedFitness(state,(float)sum); |
---|
174 | f.hits = hits; |
---|
175 | ind.evaluated = true; |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|