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.coevolve2;
|
---|
9 |
|
---|
10 | import ec.*;
|
---|
11 | import ec.coevolve.*;
|
---|
12 | import ec.vector.DoubleVectorIndividual;
|
---|
13 | import ec.simple.SimpleFitness;
|
---|
14 |
|
---|
15 | public class CoevolutionaryRosenbrock extends Problem implements GroupedProblemForm
|
---|
16 | {
|
---|
17 | double rosenbrock( double i, double j )
|
---|
18 | {
|
---|
19 | return - ( 100.0d*(i-j*j)*(i-j*j) + (1.0d-j)*(1.0d-j) );
|
---|
20 | }
|
---|
21 |
|
---|
22 | public void preprocessPopulation(final EvolutionState state, Population pop, boolean countVictoriesOnly)
|
---|
23 | {
|
---|
24 | for( int i = 0 ; i < pop.subpops.length ; i++ )
|
---|
25 | for( int j = 0 ; j < pop.subpops[i].individuals.length ; j++ )
|
---|
26 | ((SimpleFitness)(pop.subpops[i].individuals[j].fitness)).setFitness( state, Integer.MIN_VALUE, false );
|
---|
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 | pop.subpops[i].individuals[j].evaluated = true;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void evaluate(final EvolutionState state,
|
---|
37 | final Individual[] ind, // the individuals to evaluate together
|
---|
38 | final boolean[] updateFitness, // should this individuals' fitness be updated?
|
---|
39 | final boolean countVictoriesOnly, // can be neglected in cooperative coevolution
|
---|
40 | int[] subpops,
|
---|
41 | final int threadnum)
|
---|
42 | {
|
---|
43 | if( ind.length != 2 ||
|
---|
44 | ( ! ( ind[0] instanceof CoevolutionaryDoubleVectorIndividual ) ) ||
|
---|
45 | ( ! ( ind[1] instanceof CoevolutionaryDoubleVectorIndividual ) ) )
|
---|
46 | {
|
---|
47 | state.output.error( "There should be two subpopulations, both with CoevolutionaryDoubleVectorIndividual." );
|
---|
48 | }
|
---|
49 |
|
---|
50 | CoevolutionaryDoubleVectorIndividual ind1 = (CoevolutionaryDoubleVectorIndividual)(ind[0]);
|
---|
51 | CoevolutionaryDoubleVectorIndividual ind2 = (CoevolutionaryDoubleVectorIndividual)(ind[1]);
|
---|
52 |
|
---|
53 | double i = ind1.genome[0];
|
---|
54 | double j = ind2.genome[0];
|
---|
55 | double functionValue = rosenbrock( i, j );
|
---|
56 |
|
---|
57 | if( updateFitness[0] )
|
---|
58 | {
|
---|
59 | if( functionValue > ind1.fitness.fitness() )
|
---|
60 | {
|
---|
61 | ((SimpleFitness)(ind1.fitness)).setFitness( state, (float)functionValue, false );
|
---|
62 | ind1.context = new CoevolutionaryDoubleVectorIndividual[2];
|
---|
63 | ind1.context[1] = ind2;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | if( updateFitness[1] )
|
---|
67 | {
|
---|
68 | if( functionValue > ind2.fitness.fitness() )
|
---|
69 | {
|
---|
70 | ((SimpleFitness)(ind2.fitness)).setFitness( state, (float)functionValue, false );
|
---|
71 | ind2.context = new CoevolutionaryDoubleVectorIndividual[2];
|
---|
72 | ind2.context[0] = ind1;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|