Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/BreedingSource.java @ 9449

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

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

File size: 9.5 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.*;
10
11/*
12 * BreedingSource.java
13 *
14 * Created: Thu Nov 18 17:40:26 1999
15 * By: Sean Luke
16 */
17
18/**
19 * A BreedingSource is a Prototype which
20 * provides Individuals to populate new populations based on
21 * old ones.  The BreedingSource/BreedingPipeline/SelectionMethod mechanism
22 * is inherently designed to work within single subpopulations, which is
23 * by far the most common case.  If for some
24 * reason you need to breed among different subpopulations to produce new ones
25 * in a manner that can't be handled with exchanges, you will probably have to
26 * write your own custom Breeder; you'd have to write your own custom breeding
27 * pipelines anyway of course, though you can probably get away with reusing
28 * the SelectionMethods.
29 *
30 * <p>A BreedingSource may have parent sources which feed it as well.
31 * Some BreedingSources, <i>SelectionMethods</i>,
32 * are meant solely to plug into other BreedingSources, <i>BreedingPipelines</i>.
33 * BreedingPipelines can plug into other BreedingPipelines, and can also be
34 * used to provide the final Individual meant to populate a new generation.
35 *
36 * <p>Think of BreedingSources as Streams of Individuals; at one end of the
37 * stream is the provider, a SelectionMethod, which picks individuals from
38 * the old population.  At the other end of the stream is a BreedingPipeline
39 * which hands you the finished product, a small set of new Individuals
40 * for you to use in populating your new population.
41
42 <p><b>Parameters</b><br>
43 <table>
44 <tr><td valign=top><i>base</i><tt>.prob</tt><br>
45 <font size=-1>0.0 &lt;= float &lt;= 1.0, or undefined</font></td>
46 <td valign=top>(probability this BreedingSource gets chosen.  Undefined is only valid if the caller of this BreedingSource doesn't need a probability)</td></tr>
47 </table>
48 * @author Sean Luke
49 * @version 1.0
50 */
51
52public abstract class BreedingSource implements Prototype, RandomChoiceChooser
53    {
54    public static final String P_PROB = "prob";
55    public static final float NO_PROBABILITY = -1.0f;
56   
57    /** The probability that this BreedingSource will be chosen
58        to breed over other BreedingSources.  This may or may
59        not be used, depending on what the caller to this BreedingSource is.
60        It also might be modified by external sources owning this object,
61        for their own purposes.  A BreedingSource should not use it for
62        any purpose of its own, nor modify it except when setting it up.
63
64        <p>The most common modification is to normalize it with some other
65        set of probabilities, then set all of them up in increasing summation;
66        this allows the use of the fast static BreedingSource-picking utility
67        method, BreedingSource.pickRandom(...).  In order to use this method,
68        for example, if four
69        breeding source probabilities are {0.3, 0.2, 0.1, 0.4}, then
70        they should get normalized and summed by the outside owners
71        as: {0.3, 0.5, 0.6, 1.0}.
72    */
73
74    public float probability;
75
76    /** Sets up the BreedingPipeline.  You can use state.output.error here
77        because the top-level caller promises to call exitIfErrors() after calling
78        setup.  Note that probability might get modified again by
79        an external source if it doesn't normalize right.
80
81        <p>The most common modification is to normalize it with some other
82        set of probabilities, then set all of them up in increasing summation;
83        this allows the use of the fast static BreedingSource-picking utility
84        method, BreedingSource.pickRandom(...).  In order to use this method,
85        for example, if four
86        breeding source probabilities are {0.3, 0.2, 0.1, 0.4}, then
87        they should get normalized and summed by the outside owners
88        as: {0.3, 0.5, 0.6, 1.0}.
89
90
91        @see Prototype#setup(EvolutionState,Parameter)
92    */
93    public void setup(final EvolutionState state, final Parameter base)
94        {
95        Parameter def = defaultBase();
96
97        if (!state.parameters.exists(base.push(P_PROB),def.push(P_PROB)))
98            probability = NO_PROBABILITY;
99        else
100            {
101            probability = state.parameters.getFloat(base.push(P_PROB),def.push(P_PROB),0.0);
102            if (probability<0.0) state.output.error("Breeding Source's probability must be a floating point value >= 0.0, or empty, which represents NO_PROBABILITY.",base.push(P_PROB),def.push(P_PROB));
103            }
104        }
105
106    public final float getProbability(final Object obj)
107        {
108        return ((BreedingSource)obj).probability;
109        }
110
111    public final void setProbability(final Object obj, final float prob)
112        {
113        ((BreedingSource)obj).probability = prob;
114        }
115
116
117    /** Picks a random source from an array of sources, with their
118        probabilities normalized and summed as follows:  For example,
119        if four
120        breeding source probabilities are {0.3, 0.2, 0.1, 0.4}, then
121        they should get normalized and summed by the outside owners
122        as: {0.3, 0.5, 0.6, 1.0}. */
123
124    public static int pickRandom(final BreedingSource[] sources,final float prob)
125        {
126        return RandomChoice.pickFromDistribution(sources,sources[0], prob);
127        }
128
129    /** Normalizes and arranges the probabilities in sources so that they
130        are usable by pickRandom(...).  If the sources have all zero probabilities,
131        then a uniform selection is used.  Negative probabilities will
132        generate an ArithmeticException, as will an empty source array. */
133    public static void setupProbabilities(final BreedingSource[] sources)
134        {
135        RandomChoice.organizeDistribution(sources,sources[0],true);
136        }
137
138
139    /** Returns the "typical" number of individuals
140        generated with one call of produce(...). */
141    public abstract int typicalIndsProduced();
142
143    /** Returns true if this BreedingSource, when attached to the given
144        subpopulation, will produce individuals of the subpopulation's species.
145        SelectionMethods should additionally make sure that their Fitnesses are
146        of a valid type, if necessary. newpop *may* be the same as state.population
147    */
148
149    public abstract boolean produces(final EvolutionState state,
150        final Population newpop,
151        final int subpopulation,
152        int thread);
153
154    /** Called before produce(...), usually once a generation, or maybe only
155        once if you're doing steady-state evolution, to let the breeding source
156        "warm up" prior to producing.  Individuals should be produced from
157        old individuals in positions [start...start+length] in the subpopulation
158        only.  May be called again to reset the BreedingSource for a whole
159        'nuther subpopulation. */
160
161    public abstract void prepareToProduce(final EvolutionState state,
162        final int subpopulation,
163        final int thread);
164
165    /** Called after produce(...), usually once a generation, or maybe only
166        once if you're doing steady-state evolution (at the end of the run). */
167       
168    public abstract void finishProducing(final EvolutionState s,
169        final int subpopulation,
170        final int thread);
171
172    /** Produces <i>n</i> individuals from the given subpopulation
173        and puts them into inds[start...start+n-1],
174        where n = Min(Max(q,min),max), where <i>q</i> is the "typical" number of
175        individuals the BreedingSource produces in one shot, and returns
176        <i>n</i>.  max must be >= min, and min must be >= 1. For example, crossover
177        might typically produce two individuals, tournament selection might typically
178        produce a single individual, etc. */
179    public abstract int produce(final int min,
180        final int max,
181        final int start,
182        final int subpopulation,
183        final Individual[] inds,
184        final EvolutionState state,
185        final int thread) ;       
186
187    public Object clone()
188        {
189        try { return super.clone(); }
190        catch (CloneNotSupportedException e)
191            { throw new InternalError(); } // never happens
192        }
193
194
195    /** A hook which should be passed to all your subsidiary breeding
196        sources.  The default does this for you already, so ordinarily
197        you don't need to change anything.  If you are a BreedingPipeline and you
198        implement your sources in a way different
199        than using the sources[] array, be sure to override this method
200        so that it calls preparePipeline(hook) on all of your sources.
201       
202        <p>ECJ at present does not custom-implement or call this method:
203        it's available for you. Becuase it has custom functionality,
204        this method might get called more than once, and by various objects
205        as needed.  If you use it, you should determine somehow how to use
206        it to send information under the assumption that it might be sent
207        by nested items in the pipeline; you don't want to scribble over
208        each other's calls! Note that this method should travel *all*
209        breeding source paths regardless of whether or not it's redundant to
210        do so. */
211    public void preparePipeline(final Object hook)
212        {
213        // the default method does nothing, though BreedingPipelines override this
214        // to guarantee that it's called on all their sources as well.
215        }
216    }
Note: See TracBrowser for help on using the repository browser.