Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/steadystate/SteadyStateBreeder.java @ 6703

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

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

File size: 7.4 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.steadystate;
9import ec.simple.*;
10import ec.*;
11import ec.util.*;
12import java.util.*;
13
14/*
15 * SteadyStateBreeder.java
16 *
17 */
18
19/**
20 * This subclass of Breeder performs the evaluation portion of Steady-State Evolution and (in distributed form)
21 * Asynchronous Evolution. The procedure is as follows.  We begin with an empty Population and one by
22 * one create new Indivdiuals and send them off to be evaluated.  In basic Steady-State Evolution the
23 * individuals are immediately evaluated and we wait for them; but in Asynchronous Evolution the individuals are evaluated
24 * for however long it takes and we don't wait for them to finish.  When individuals return they are
25 * added to the Population until it is full.  No duplicate individuals are allowed.
26 *
27 * <p>At this point the system switches to its "steady state": individuals are bred from the population
28 * one by one, and sent off to be evaluated.  Once again, in basic Steady-State Evolution the
29 * individuals are immediately evaluated and we wait for them; but in Asynchronous Evolution the individuals are evaluated
30 * for however long it takes and we don't wait for them to finish.  When an individual returns, we
31 * mark an individual in the Population for death, then replace it with the new returning individual.
32 * Note that during the steady-state, Asynchronous Evolution could be still sending back some "new" individuals
33 * created during the initialization phase, not "bred" individuals.
34 *
35 * <p>The determination of how an individual is marked for death is done by the SteadyStateBreeder.  This is
36 * a SelectionMethod.  Note that this SelectionMethod probably should <i>not</i> be selecting for the "fittest"
37 * individuals, but rather for either random individuals (the standard approach) or for "bad" individuals.
38 *
39 
40 <p><b>Parameters</b><br>
41 <table>
42 <tr><td valign=top><tt>deselector</tt><br>
43 <font size=-1>classname, inherits and != ec.SelectionMethod</font></td>
44 <td valign=top>(The SelectionMethod used to pick individuals for death)</td></tr>
45 </table>
46
47 * @author Sean Luke
48 * @version 1.0
49 */
50
51public class SteadyStateBreeder extends SimpleBreeder
52    {
53    /** If st.firstTimeAround, this acts exactly like SimpleBreeder.
54        Else, it only breeds one new individual per subpopulation, to
55        place in position 0 of the subpopulation. 
56    */
57    BreedingPipeline[] bp;
58   
59    public static final String P_DESELECTOR = "deselector";
60    // public static final String P_RETRIES = "duplicate-retries";
61       
62    /** Loaded during the first iteration of breedPopulation */
63    SelectionMethod deselectors[];
64       
65    /** Do we allow duplicates? */
66    // public int numDuplicateRetries;
67   
68    public SteadyStateBreeder() { bp = null; deselectors = null; }
69       
70    public void setup(final EvolutionState state, final Parameter base)
71        {
72        super.setup(state,base);
73               
74        Parameter p = new Parameter(Initializer.P_POP).push(Population.P_SIZE);
75        int size = state.parameters.getInt(p,null,1); 
76       
77        // if size is wrong, we'll let Population complain about it  -- for us, we'll just make 0-sized arrays and drop out.
78        if (size > 0)
79            deselectors = new SelectionMethod[size];
80       
81        // load the deselectors
82        for(int x=0;x<deselectors.length;x++)
83            {
84            deselectors[x] = (SelectionMethod)(
85                state.parameters.getInstanceForParameter(
86                    SteadyStateDefaults.base().push(P_DESELECTOR).push(""+x),null,SelectionMethod.class));
87            if (!(deselectors[x] instanceof SteadyStateBSourceForm))
88                state.output.error("Deselector for subpopulation " + x + " is not of SteadyStateBSourceForm.");
89            deselectors[x].setup(state,SteadyStateDefaults.base().push(P_DESELECTOR).push(""+x));
90            }
91        state.output.exitIfErrors();
92       
93        // How often do we retry if we find a duplicate?
94        /*
95          numDuplicateRetries = state.parameters.getInt(
96          SteadyStateDefaults.base().push(P_RETRIES),null,0);
97          if (numDuplicateRetries < 0) state.output.fatal(
98          "The number of retries for duplicates must be an integer >= 0.\n",
99          base.push(P_RETRIES),null);
100        */
101        }
102       
103    /** Called to check to see if the breeding sources are correct -- if you
104        use this method, you must call state.output.exitIfErrors() immediately
105        afterwards. */
106    public void sourcesAreProperForm(final SteadyStateEvolutionState state,
107        final BreedingPipeline[] breedingPipelines)
108        {
109        for(int x=0;x<breedingPipelines.length;x++)
110            {
111            if (!(breedingPipelines[x] instanceof SteadyStateBSourceForm))
112                state.output.error("Breeding Pipeline of subpopulation " + x + " is not of SteadyStateBSourceForm");
113            ((SteadyStateBSourceForm)(breedingPipelines[x])).sourcesAreProperForm(state);
114            }
115        }
116   
117    /** Called whenever individuals have been replaced by new
118        individuals in the population. */
119    public void individualReplaced(final SteadyStateEvolutionState state,
120        final int subpopulation,
121        final int thread,
122        final int individual)
123        {
124        for(int x=0;x<bp.length;x++)
125            ((SteadyStateBSourceForm)bp[x]).
126                individualReplaced(state,subpopulation,thread,individual);
127        // let the deselector know
128        ((SteadyStateBSourceForm)deselectors[subpopulation]).individualReplaced(state,subpopulation,thread,individual);
129        }
130       
131    public void finishPipelines(EvolutionState state)
132        {
133        for(int x = 0 ; x < deselectors.length; x++)
134            {
135            bp[x].finishProducing(state,x,0);
136            deselectors[x].finishProducing(state,x,0);
137            }
138        }
139       
140    public void prepareToBreed(EvolutionState state, int thread)
141        {
142        final SteadyStateEvolutionState st = (SteadyStateEvolutionState) state;
143        // set up the breeding pipelines
144        bp = new BreedingPipeline[st.population.subpops.length];
145        for(int pop=0;pop<bp.length;pop++)
146            {
147            bp[pop] = (BreedingPipeline)st.population.subpops[pop].species.pipe_prototype.clone();
148            if (!bp[pop].produces(st,st.population,pop,0))
149                st.output.error("The Breeding Pipeline of subpopulation " + pop + " does not produce individuals of the expected species " + st.population.subpops[pop].species.getClass().getName() + " and with the expected Fitness class " + st.population.subpops[pop].species.f_prototype.getClass().getName());
150            }
151        // are they of the proper form?
152        sourcesAreProperForm(st,bp);
153        // because I promised when calling sourcesAreProperForm
154        st.output.exitIfErrors();
155               
156        // warm them up
157        for(int pop=0;pop<bp.length;pop++)
158            {
159            bp[pop].prepareToProduce(state,pop,0);
160            deselectors[pop].prepareToProduce(state,pop,0);
161            }
162        }
163       
164    public Individual breedIndividual(final EvolutionState state, int subpop, int thread)
165        {
166        final SteadyStateEvolutionState st = (SteadyStateEvolutionState) state;
167        Individual[] newind = new Individual[1];
168               
169        // breed a single individual
170        bp[subpop].produce(1,1,0,subpop,newind,state,thread);
171        return newind[0];
172        }
173    }
Note: See TracBrowser for help on using the repository browser.