Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/app/coevolve2/CoevolutionaryECSuite.java @ 11049

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

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

File size: 3.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.coevolve2;
9
10import ec.*;
11import ec.app.ecsuite.*;
12import ec.coevolve.*;
13import ec.vector.DoubleVectorIndividual;
14import ec.simple.SimpleFitness;
15
16public 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    }
Note: See TracBrowser for help on using the repository browser.