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.app.ecsuite.*;
|
---|
12 | import ec.coevolve.*;
|
---|
13 | import ec.vector.DoubleVectorIndividual;
|
---|
14 | import ec.simple.SimpleFitness;
|
---|
15 |
|
---|
16 | public class CoevolutionaryECSuite extends ECSuite implements GroupedProblemForm
|
---|
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 | ((SimpleFitness)(pop.subpops[i].individuals[j].fitness)).setFitness( state, Integer.MIN_VALUE, false );
|
---|
23 | }
|
---|
24 |
|
---|
25 | public void postprocessPopulation(final EvolutionState state, Population pop, boolean countVictoriesOnly)
|
---|
26 | {
|
---|
27 | for( int i = 0 ; i < pop.subpops.length ; i++ )
|
---|
28 | for( int j = 0 ; j < pop.subpops[i].individuals.length ; j++ )
|
---|
29 | pop.subpops[i].individuals[j].evaluated = true;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public void evaluate(final EvolutionState state,
|
---|
33 | final Individual[] ind, // the individuals to evaluate together
|
---|
34 | final boolean[] updateFitness, // should this individuals' fitness be updated?
|
---|
35 | final boolean countVictoriesOnly, // can be neglected in cooperative coevolution
|
---|
36 | int[] subpops,
|
---|
37 | final int threadnum)
|
---|
38 | {
|
---|
39 | if (ind.length == 0)
|
---|
40 | state.output.fatal("Number of individuals provided to CoevolutionaryECSuite is 0!");
|
---|
41 | if (ind.length == 1)
|
---|
42 | state.output.warnOnce("Coevolution used, but number of individuals provided to CoevolutionaryECSuite is 1.");
|
---|
43 |
|
---|
44 | int size = 0;
|
---|
45 | for(int i = 0 ; i < ind.length; i++)
|
---|
46 | if ( ! ( ind[i] instanceof CoevolutionaryDoubleVectorIndividual ) )
|
---|
47 | state.output.error( "Individual " + i + "in coevolution is not a CoevolutionaryDoubleVectorIndividual." );
|
---|
48 | else
|
---|
49 | {
|
---|
50 | CoevolutionaryDoubleVectorIndividual coind = (CoevolutionaryDoubleVectorIndividual)(ind[i]);
|
---|
51 | size += coind.genome.length;
|
---|
52 | }
|
---|
53 | state.output.exitIfErrors();
|
---|
54 |
|
---|
55 | // concatenate all the arrays
|
---|
56 | double[] vals = new double[size];
|
---|
57 | int pos = 0;
|
---|
58 | for(int i = 0 ; i < ind.length; i++)
|
---|
59 | {
|
---|
60 | System.err.println("-->" + i);
|
---|
61 | CoevolutionaryDoubleVectorIndividual coind = (CoevolutionaryDoubleVectorIndividual)(ind[i]);
|
---|
62 | System.err.println(coind.genome.length);
|
---|
63 | System.arraycopy(coind.genome, 0, vals, pos, coind.genome.length);
|
---|
64 | pos += coind.genome.length;
|
---|
65 | }
|
---|
66 |
|
---|
67 | double fit = (function(state, problemType, vals, threadnum));
|
---|
68 | boolean isOptimal = isOptimal(problemType, fit);
|
---|
69 |
|
---|
70 | for(int i = 0 ; i < ind.length; i++)
|
---|
71 | {
|
---|
72 | CoevolutionaryDoubleVectorIndividual coind = (CoevolutionaryDoubleVectorIndividual)(ind[i]);
|
---|
73 | if (updateFitness[i])
|
---|
74 | {
|
---|
75 | if ( fit > coind.fitness.fitness() )
|
---|
76 | {
|
---|
77 | ((SimpleFitness)(coind.fitness)).setFitness( state, (float) fit, isOptimal );
|
---|
78 | coind.context = new CoevolutionaryDoubleVectorIndividual[ind.length];
|
---|
79 | for(int j = 0; j < ind.length; j++)
|
---|
80 | {
|
---|
81 | if (i != j)
|
---|
82 | coind.context[j] = (CoevolutionaryDoubleVectorIndividual)(ind[j]);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|