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.tutorial2;
|
---|
9 | import ec.*;
|
---|
10 | import ec.simple.*;
|
---|
11 | import ec.vector.*;
|
---|
12 |
|
---|
13 | public class AddSubtract extends Problem implements SimpleProblemForm
|
---|
14 | {
|
---|
15 | public void evaluate(final EvolutionState state,
|
---|
16 | final Individual ind,
|
---|
17 | final int subpopulation,
|
---|
18 | final int threadnum)
|
---|
19 | {
|
---|
20 | if (ind.evaluated) return;
|
---|
21 |
|
---|
22 | if (!(ind instanceof IntegerVectorIndividual))
|
---|
23 | state.output.fatal("Whoa! It's not a IntegerVectorIndividual!!!",null);
|
---|
24 |
|
---|
25 | IntegerVectorIndividual ind2 = (IntegerVectorIndividual)ind;
|
---|
26 |
|
---|
27 | int rawfitness = 0;
|
---|
28 | for(int x=0; x<ind2.genome.length; x++)
|
---|
29 | if (x % 2 == 0) rawfitness += ind2.genome[x];
|
---|
30 | else rawfitness -= ind2.genome[x];
|
---|
31 |
|
---|
32 | // We finish by taking the ABS of rawfitness. By the way,
|
---|
33 | // in SimpleFitness, fitness values must be set up so that 0 is <= the worst
|
---|
34 | // fitness and +infinity is >= the ideal possible fitness. Our raw fitness
|
---|
35 | // value here satisfies this.
|
---|
36 | if (rawfitness < 0) rawfitness = -rawfitness;
|
---|
37 | if (!(ind2.fitness instanceof SimpleFitness))
|
---|
38 | state.output.fatal("Whoa! It's not a SimpleFitness!!!",null);
|
---|
39 | ((SimpleFitness)ind2.fitness).setFitness(state,
|
---|
40 | // what the heck, lets normalize the fitness for genome length
|
---|
41 | // so it's within float range
|
---|
42 | (float)(((double)rawfitness)/ind2.genome.length),
|
---|
43 | ///... is the individual ideal? Indicate here...
|
---|
44 | false);
|
---|
45 | ind2.evaluated = true;
|
---|
46 | }
|
---|
47 | }
|
---|