Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/gp/ge/GEProblem.java @ 12912

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

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

File size: 7.9 KB
Line 
1/*
2  Copyright 20010 by Sean Luke and George Mason University
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6package ec.gp.ge;
7
8import ec.*;
9import ec.gp.*;
10import ec.gp.koza.*;
11import ec.simple.*;
12import ec.coevolve.*;
13import ec.util.*;
14
15/*
16 * GEProblem.java
17 *
18 * Created: Sat Oct 16 23:21:01 EDT 2010
19 * By: Joseph Zelibor III, Eric Kangas, and Sean Luke
20 */
21
22/**
23    GEProblem is a special replacement for Problem which performs GE mapping.  You do not subclass
24    from GEProblem.  Rather, create a GPProblem subclass and set it to be the 'problem' parameter of the GEProblem.
25    The GEProblem will convert the GEIndividual into a GPIndividual, then pass this GPIndividual to the GPProblem
26    to be evaluated.
27       
28    <p>The procedure is as follows.  Let's say your GPProblem is the Artificial Ant problem.  Instead of saying...
29       
30    <p><tt>eval.problem = ec.app.ant.Ant<br>
31    eval.problem = ec.app.ant.Ant<br>
32    eval.problem.data = ec.app.ant.AntData<br>
33    eval.problem.moves = 400<br>
34    eval.problem.file = santafe.trl
35    </tt>
36
37    <p>... you instead make your problem a GEProblem like this:
38
39    <p><tt>eval.problem = ec.gp.ge.GEProblem</tt>
40       
41    <p>... and then you hang the Ant problem, and all its subsidiary data, as the 'problem' parameter from the GEProblem like so:
42
43    <p><tt>eval.problem.problem = ec.app.ant.Ant<br>
44    eval.problem.problem.data = ec.app.ant.AntData<br>
45    eval.problem.problem.moves = 400<br>
46    eval.problem.problem.file = santafe.trl
47    </tt>
48
49    <p>Everything else should be handled for you.  GEProblem is also compatible with the MasterProblem procedure
50    for distributed evaluation, and is also both a SimpleProblemForm and a GroupedProblemForm.  We've got you covered.
51
52    <p><b>Parameters</b><br>
53    <table>
54    <tr><td valign=top><i>base</i>.<tt>problem</tt><br>
55    <font size=-1>classname, inherits from GPProblem</font></td>
56    <td valign=top>(The GPProblem which actually performs the evaluation of the mapped GPIndividual)</td></tr>
57    </table>
58*/
59
60public class GEProblem extends Problem implements SimpleProblemForm, GroupedProblemForm
61    {
62    public final static String P_PROBLEM = "problem";
63    public GPProblem problem;
64       
65    public void setup(EvolutionState state, Parameter base)
66        {
67        problem = (GPProblem)state.parameters.getInstanceForParameter(base.push(P_PROBLEM), null, GPProblem.class);
68        problem.setup(state, base.push(P_PROBLEM));
69        }
70       
71    public Object clone()
72        {
73        GEProblem other = (GEProblem)(super.clone());
74        other.problem = (GPProblem)(problem.clone());
75        return other;
76        }
77       
78    public void prepareToEvaluate(final EvolutionState state, final int threadnum)
79        {
80        problem.prepareToEvaluate(state, threadnum);
81        }
82
83    public void finishEvaluating(final EvolutionState state, final int threadnum)
84        {
85        problem.finishEvaluating(state, threadnum);
86        }
87
88    public void initializeContacts( EvolutionState state )
89        {
90        problem.initializeContacts(state);
91        }
92
93    public void reinitializeContacts( EvolutionState state )
94        {
95        problem.reinitializeContacts(state);
96        }
97   
98    public void closeContacts(EvolutionState state, int result)
99        {
100        problem.closeContacts(state, result);
101        }
102       
103    public boolean canEvaluate()
104        {
105        return problem.canEvaluate();
106        }
107
108    public void preprocessPopulation(final EvolutionState state, Population pop, final boolean countVictoriesOnly)
109        {
110        if (!(problem instanceof GroupedProblemForm))
111            state.output.fatal("GEProblem's underlying Problem is not a GroupedProblemForm");
112        ((GroupedProblemForm)problem).preprocessPopulation(state, pop, countVictoriesOnly);
113        }
114
115    public void postprocessPopulation(final EvolutionState state, Population pop, final boolean countVictoriesOnly)
116        {
117        ((GroupedProblemForm)problem).preprocessPopulation(state, pop, countVictoriesOnly);
118        }
119       
120    /** Default version assumes that every individual is a GEIndividual.
121        The underlying problem.evaluate() must be prepared for the possibility that some
122        GPIndividuals handed it are in fact null, meaning that they couldn't be extracted
123        from the GEIndividual string.  You should assign them bad fitness in some appropriate way.
124    */
125    public void evaluate(final EvolutionState state,
126        final Individual[] ind,  // the individuals to evaluate together
127        final boolean[] updateFitness,  // should this individuals' fitness be updated?
128        final boolean countVictoriesOnly, // don't bother updating Fitness with socres, just victories
129        final int[] subpops,
130        final int threadnum)
131        {
132        // the default version assumes that every subpopulation is a GE Individual
133        GPIndividual[] gpi = new GPIndividual[ind.length];
134        for(int i = 0; i < gpi.length; i++)
135            {
136            GEIndividual indiv = (GEIndividual) ind[i];
137            GESpecies species = (GESpecies) (ind[i].species);
138               
139            // warning: gpi[i] may be null
140            gpi[i] = species.map(state, indiv, threadnum);
141            }
142                       
143        ((GroupedProblemForm)problem).evaluate(state, gpi, updateFitness, countVictoriesOnly, subpops, threadnum);
144                       
145        for(int i = 0; i < gpi.length; i++)
146            {
147            // Now we need to move the evaluated flag from the GPIndividual
148            // to the GEIndividual, and also for good measure, let's copy over
149            // the GPIndividual's fitness because even though the mapping function
150            // set the two Individuals to share the same fitness, it's possible
151            // that the evaluation function may have replaced the fitness.
152            ind[i].fitness = gpi[i].fitness;
153            ind[i].evaluated = gpi[i].evaluated;
154            }
155        }
156
157    public void evaluate(final EvolutionState state,
158        final Individual ind,
159        final int subpopulation,
160        final int threadnum)
161        {
162        if (!(problem instanceof SimpleProblemForm))
163            state.output.fatal("GEProblem's underlying Problem is not a SimpleProblemForm");
164
165        GEIndividual indiv = (GEIndividual) ind;
166        GESpecies species = (GESpecies) (ind.species);
167        GPIndividual gpi = species.map(state, indiv, threadnum);
168        if (gpi == null)
169            {
170            KozaFitness fitness = (KozaFitness) (ind.fitness);
171            fitness.setStandardizedFitness(state, Float.MAX_VALUE);
172            }
173        else
174            {
175            ((SimpleProblemForm)problem).evaluate(state, gpi, subpopulation, threadnum);
176            // Now we need to move the evaluated flag from the GPIndividual
177            // to the GEIndividual, and also for good measure, let's copy over
178            // the GPIndividual's fitness because even though the mapping function
179            // set the two Individuals to share the same fitness, it's possible
180            // that the evaluation function may have replaced the fitness.
181            ind.fitness = gpi.fitness;
182            ind.evaluated = gpi.evaluated;
183            }
184        }
185
186    public void describe(final EvolutionState state,
187        final Individual ind,
188        final int subpopulation,
189        final int threadnum,
190        final int log)
191        {
192        GEIndividual indiv = (GEIndividual) ind;
193        GESpecies species = (GESpecies) (ind.species);
194        GPIndividual gpi = species.map(state, indiv, threadnum);
195        if (gpi != null)
196            {
197            problem.describe(state, gpi, subpopulation, threadnum, log);
198
199            // though this is probably not necessary for describe(...),
200            // for good measure we're doing the same rigamarole that we
201            // did for evaluate(...) above.
202            ind.fitness = gpi.fitness;
203            ind.evaluated = gpi.evaluated;
204            }
205        }
206    }
Note: See TracBrowser for help on using the repository browser.