Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/Species.java @ 8279

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

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

File size: 8.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;
9import ec.util.*;
10import java.io.*;
11
12/*
13 * Species.java
14 *
15 * Created: Tue Aug 10 20:31:50 1999
16 * By: Sean Luke
17 */
18
19/**
20 * Species is a prototype which defines the features for a set of individuals
21 * in the population.  Typically, individuals may breed if they belong to the
22 * same species (but it's not a hard-and-fast rule).  Each Subpopulation has
23 * one Species object which defines the species for individuals in that
24 * Subpopulation.
25 *
26 * <p>Species are generally responsible for creating individuals, through
27 * their newIndividual(...) method.  This method usually clones its prototypical
28 * individual and makes some additional modifications to the clone, then returns it.
29 * Note that the prototypical individual does <b>not need to be a complete individual</b> --
30 * for example, GPSpecies holds a GPIndividual which doesn't have any trees (the tree
31 * roots are null).
32 *
33 * <p>Species also holds a prototypical breeding pipeline meant to breed
34 * this individual.  To breed individuals of this species, clone the pipeline
35 * and use the clone.
36
37 <p><b>Parameters</b><br>
38 <table>
39 <tr><td valign=top><i>base</i>.<tt>ind</tt><br>
40 <font size=-1>classname, inherits and != ec.Individual</font></td>
41 <td valign=top>(the class for the prototypical individual for the species)</td></tr>
42
43 <tr><td valign=top><i>base</i>.<tt>fitness</tt><br>
44 <font size=-1>classname, inherits and != ec.Fitness</font></td>
45 <td valign=top>(the class for the prototypical fitness for the species)</td></tr>
46
47 <tr><td valign=top><i>base</i>.<tt>numpipes</tt><br>
48 <font size=-1>int &gt;= 1</font></td>
49 <td valign=top>(total number of breeding pipelines for the species)</td></tr>
50
51 <tr><td valign=top><i>base</i>.<tt>pipe</tt><br>
52 <font size=-1>classname, inherits and != ec.BreedingPipeline</font></td>
53 <td valign=top>(the class for the prototypical Breeding Pipeline)</td></tr>
54
55 </table>
56
57
58 <p><b>Parameter bases</b><br>
59 <table>
60 <tr><td valign=top><i>base</i>.<tt>ind</tt></td>
61 <td>i_prototype (the prototypical individual)</td></tr>
62
63 <tr><td valign=top><i>base</i>.<tt>pipe</tt></td>
64 <td>pipe_prototype (breeding pipeline prototype)</td></tr>
65
66 <tr><td valign=top><i>base</i>.<tt>fitness</tt></td>
67 <td>f_prototype (the prototypical fitness)</td></tr>
68
69 </table>
70
71
72
73 * @author Sean Luke
74 * @version 1.0
75 */
76
77public abstract class Species implements Prototype
78    {
79    public static final String P_INDIVIDUAL = "ind";
80    public static final String P_PIPE = "pipe";
81    public static final String P_FITNESS = "fitness";
82
83    /** The prototypical individual for this species. */
84    public Individual i_prototype;
85   
86    /** The prototypical breeding pipeline for this species. */
87    public BreedingPipeline pipe_prototype;
88
89    /** The prototypical fitness for individuals of this species. */
90    public Fitness f_prototype;   
91
92    public Object clone()
93        {
94        try
95            {
96            Species myobj = (Species) (super.clone());
97            myobj.i_prototype = (Individual) i_prototype.clone();
98            myobj.f_prototype = (Fitness) f_prototype.clone();
99            myobj.pipe_prototype = (BreedingPipeline) pipe_prototype.clone();
100            return myobj;
101            }
102        catch (CloneNotSupportedException e)
103            { throw new InternalError(); } // never happens
104        }
105
106
107
108    // deprecate the old ones
109    final private Individual newIndividual(final EvolutionState state,
110        final Subpopulation _population,
111        final Fitness _fitness) throws IOException { return null; }
112    final private Individual newIndividual(final EvolutionState state,
113        final Subpopulation _population,
114        final Fitness _fitness,
115        final LineNumberReader reader) throws IOException  { return null; }
116    final private Individual newIndividual(final EvolutionState state,
117        final Subpopulation _population,
118        final Fitness _fitness,
119        final DataInput input) throws IOException  { return null; }
120   
121    /** Provides a brand-new individual to fill in a population.  The default form
122        simply calls clone(), creates a fitness, sets evaluated to false, and sets
123        the species.  If you need to make a more custom genotype (as is the case
124        for GPSpecies, which requires a light rather than deep clone),
125        you will need to override this method as you see fit.
126    */
127   
128    public Individual newIndividual(final EvolutionState state, int thread)
129        {
130        Individual newind = (Individual)(i_prototype.clone());
131
132        // Set the fitness
133        newind.fitness = (Fitness)(f_prototype.clone());
134        newind.evaluated = false;
135
136        // Set the species to me
137        newind.species = this;
138
139        // ...and we're ready!
140        return newind;
141        }
142   
143    /**
144       Provides an individual read from a stream, including
145       the fitness; the individual will
146       appear as it was written by printIndividual(...).  Doesn't
147       close the stream.  Sets evaluated to false and sets the species.
148       If you need to make a more custom mechanism (as is the case
149       for GPSpecies, which requires a light rather than deep clone),
150       you will need to override this method as you see fit.
151    */
152
153    public Individual newIndividual(final EvolutionState state,
154        final LineNumberReader reader)
155        throws IOException
156        {
157        Individual newind = (Individual)(i_prototype.clone());
158               
159        // Set the fitness
160        newind.fitness = (Fitness)(f_prototype.clone());
161        newind.evaluated = false; // for sanity's sake, though it's a useless line
162
163        // load that sucker
164        newind.readIndividual(state,reader);
165
166        // Set the species to me
167        newind.species = this;
168
169        // and we're ready!
170        return newind; 
171        }
172
173    /**
174       Provides an individual read from a DataInput source, including
175       the fitness.  Doesn't
176       close the DataInput.  Sets evaluated to false and sets the species.
177       If you need to make a more custom mechanism (as is the case
178       for GPSpecies, which requires a light rather than deep clone),
179       you will need to override this method as you see fit.
180    */
181
182    public Individual newIndividual(final EvolutionState state,
183        final DataInput dataInput)
184        throws IOException
185        {
186        Individual newind = (Individual)(i_prototype.clone());
187       
188        // Set the fitness
189        newind.fitness = (Fitness)(f_prototype.clone());
190        newind.evaluated = false; // for sanity's sake, though it's a useless line
191
192        // Set the species to me
193        newind.species = this;
194
195        // load that sucker
196        newind.readIndividual(state,dataInput);
197
198        // and we're ready!
199        return newind; 
200        }
201
202
203    /** The default version of setup(...) loads requested pipelines and calls setup(...) on them and normalizes their probabilities. 
204        If your individual prototype might need to know special things about the species (like parameters stored in it),
205        then when you override this setup method, you'll need to set those parameters BEFORE you call super.setup(...),
206        because the setup(...) code in Species sets up the prototype.
207        @see Prototype#setup(EvolutionState,Parameter)
208    */
209 
210    public void setup(final EvolutionState state, final Parameter base)
211        {
212        Parameter def = defaultBase();
213
214        // load the breeding pipeline
215        pipe_prototype = (BreedingPipeline)(
216            state.parameters.getInstanceForParameter(
217                base.push(P_PIPE),def.push(P_PIPE),BreedingPipeline.class));
218        pipe_prototype.setup(state,base.push(P_PIPE));
219
220        // I promised over in BreedingSource.java that this method would get called.
221        state.output.exitIfErrors();
222
223        // load our individual prototype
224        i_prototype = (Individual)(state.parameters.getInstanceForParameter(
225                base.push(P_INDIVIDUAL),def.push(P_INDIVIDUAL),
226                Individual. class));
227        // set the species to me before setting up the individual, so they know who I am
228        i_prototype.species = this;
229        i_prototype.setup(state,base.push(P_INDIVIDUAL));
230       
231        // load our fitness
232        f_prototype = (Fitness) state.parameters.getInstanceForParameter(
233            base.push(P_FITNESS),def.push(P_FITNESS),
234            Fitness.class);
235        f_prototype.setup(state,base.push(P_FITNESS));
236        }
237    }
238
239
Note: See TracBrowser for help on using the repository browser.