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.tutorial4; |
---|
9 | import ec.util.*; |
---|
10 | import ec.*; |
---|
11 | import ec.gp.*; |
---|
12 | import ec.gp.koza.*; |
---|
13 | import ec.simple.*; |
---|
14 | |
---|
15 | public class MultiValuedRegression extends GPProblem implements SimpleProblemForm |
---|
16 | { |
---|
17 | public double currentX; |
---|
18 | public double currentY; |
---|
19 | |
---|
20 | public DoubleData input; |
---|
21 | |
---|
22 | public Object clone() |
---|
23 | { |
---|
24 | MultiValuedRegression newobj = (MultiValuedRegression) (super.clone()); |
---|
25 | newobj.input = (DoubleData)(input.clone()); |
---|
26 | return newobj; |
---|
27 | } |
---|
28 | |
---|
29 | public void setup(final EvolutionState state, |
---|
30 | final Parameter base) |
---|
31 | { |
---|
32 | // very important, remember this |
---|
33 | super.setup(state,base); |
---|
34 | |
---|
35 | // set up our input -- don't want to use the default base, it's unsafe here |
---|
36 | input = (DoubleData) state.parameters.getInstanceForParameterEq( |
---|
37 | base.push(P_DATA), null, DoubleData.class); |
---|
38 | input.setup(state,base.push(P_DATA)); |
---|
39 | } |
---|
40 | |
---|
41 | public void evaluate(final EvolutionState state, |
---|
42 | final Individual ind, |
---|
43 | final int subpopulation, |
---|
44 | final int threadnum) |
---|
45 | { |
---|
46 | if (!ind.evaluated) // don't bother reevaluating |
---|
47 | { |
---|
48 | int hits = 0; |
---|
49 | double sum = 0.0; |
---|
50 | double expectedResult; |
---|
51 | double result; |
---|
52 | for (int y=0;y<10;y++) |
---|
53 | { |
---|
54 | currentX = state.random[threadnum].nextDouble(); |
---|
55 | currentY = state.random[threadnum].nextDouble(); |
---|
56 | expectedResult = currentX*currentX*currentY + currentX*currentY + currentY; |
---|
57 | ((GPIndividual)ind).trees[0].child.eval( |
---|
58 | state,threadnum,input,stack,((GPIndividual)ind),this); |
---|
59 | |
---|
60 | result = Math.abs(expectedResult - input.x); |
---|
61 | if (result <= 0.01) hits++; |
---|
62 | sum += result; |
---|
63 | } |
---|
64 | |
---|
65 | // the fitness better be KozaFitness! |
---|
66 | KozaFitness f = ((KozaFitness)ind.fitness); |
---|
67 | f.setStandardizedFitness(state,(float)sum); |
---|
68 | f.hits = hits; |
---|
69 | ind.evaluated = true; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|