Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/app/coevolve1/CompetitiveMaxOne.java @ 9449

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

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

File size: 4.1 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.coevolve1;
9
10import ec.simple.SimpleFitness;
11import ec.coevolve.*;
12import ec.*;
13import ec.vector.*;
14
15public class CompetitiveMaxOne extends Problem implements GroupedProblemForm
16    {
17
18    public void preprocessPopulation( final EvolutionState state, Population pop, boolean countVictoriesOnly )
19        {
20        for( int i = 0 ; i < pop.subpops.length ; i++ )
21            for( int j = 0 ; j < pop.subpops[i].individuals.length ; j++ )
22                {
23                SimpleFitness sf = ((SimpleFitness)(pop.subpops[i].individuals[j].fitness));
24                sf.trials = 0;
25                sf.setFitness( state, 0, false );
26                }
27        }
28
29    public void postprocessPopulation( final EvolutionState state, Population pop, boolean countVictoriesOnly )
30        {
31        for( int i = 0 ; i < pop.subpops.length ; i++ )
32            for( int j = 0 ; j < pop.subpops[i].individuals.length ; j++ )
33                {
34                if (!countVictoriesOnly)   // gotta average by number of trials
35                    {
36                    SimpleFitness sf = ((SimpleFitness)(pop.subpops[i].individuals[j].fitness));
37                    sf.setFitness( state, sf.fitness() / sf.trials, false );
38                    }
39                pop.subpops[i].individuals[j].evaluated = true;
40                }
41        }
42
43    public void evaluate(final EvolutionState state,
44        final Individual[] ind,  // the individuals to evaluate together
45        final boolean[] updateFitness,  // should this individuals' fitness be updated?
46        final boolean countVictoriesOnly,
47        int[] subpops,
48        final int threadnum)
49        {
50        if( ind.length != 2 || updateFitness.length != 2 )
51            state.output.fatal( "The InternalSumProblem evaluates only two individuals at a time." );
52
53        if( ! ( ind[0] instanceof BitVectorIndividual ) )
54            state.output.fatal( "The individuals in the InternalSumProblem should be FloatVectorIndividuals." );
55
56        if( ! ( ind[1] instanceof BitVectorIndividual ) )
57            state.output.fatal( "The individuals in the InternalSumProblem should be FloatVectorIndividuals." );
58       
59        int value1=0;
60        int value2=0;
61
62        BitVectorIndividual temp;
63
64        // calculate the function value for the first individual
65        temp = (BitVectorIndividual)ind[0];
66        for( int i = 0 ; i < temp.genome.length ; i++ )
67            if( temp.genome[i] ) value1++;
68
69        // calculate the function value for the second individual
70        temp = (BitVectorIndividual)ind[1];
71        for( int i = 0 ; i < temp.genome.length ; i++ )
72            if( temp.genome[i] ) value2 ++;
73
74        boolean firstWinsIfDraw = false;
75        if( value1 == value2 )
76            firstWinsIfDraw = state.random[threadnum].nextBoolean( 0.5 );
77
78        if( updateFitness[0] )
79            {
80            SimpleFitness fit = ((SimpleFitness)(ind[0].fitness));
81            fit.trials++;
82            if( countVictoriesOnly )
83                {
84                if( ( value1 > value2 ) ||
85                    ( value1 == value2 && firstWinsIfDraw ) )
86                    {
87                    fit.setFitness( state, (float)(fit.fitness() + 1), false );
88                    }
89                }
90            else
91                fit.setFitness( state, (float)(fit.fitness() + value1 - value2), false );
92            }
93
94        if( updateFitness[1] )
95            {
96            SimpleFitness fit = ((SimpleFitness)(ind[1].fitness));
97            fit.trials++;
98
99            if( countVictoriesOnly )
100                {
101                if( ( value2 > value1 ) ||
102                    ( value2 == value1 && !firstWinsIfDraw ) )
103                    {
104                    fit.setFitness( state, (float)(fit.fitness() + 1), false );
105                    }
106                }
107            else
108                fit.setFitness( state, (float)(fit.fitness() + value2 - value1), false );
109            }
110
111        }
112
113    }
114
115
116
117
118
Note: See TracBrowser for help on using the repository browser.