Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/app/tutorial2/AddSubtract.java @ 6152

Last change on this file since 6152 was 6152, checked in by bfarka, 13 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 1.7 KB
Line 
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
8package ec.app.tutorial2;
9import ec.*;
10import ec.simple.*;
11import ec.vector.*;
12
13public 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    }
Note: See TracBrowser for help on using the repository browser.