Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/BreedingPipeline.java @ 7611

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

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

File size: 11.0 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 ec.steadystate.*;
11
12/*
13 * BreedingPipeline.java
14 *
15 * Created: Tue Aug 17 21:37:10 1999
16 * By: Sean Luke
17 */
18
19/**
20 * A BreedingPipeline is a BreedingSource which provides "fresh" individuals which
21 * can be used to fill a new population.  BreedingPipelines might include
22 * Crossover pipelines, various Mutation pipelines, etc.  This abstract class
23 * provides some default versions of various methods to simplify matters for you.
24 * It also contains an array of breeding sources for your convenience.  You don't
25 * have to use them of course, but this means you have to customize the
26 * default methods below to make sure they get distributed to your special
27 * sources.  Note that these sources may contain references to the same
28 * object -- they're not necessarily distinct.  This is to provide both
29 * some simple DAG features and also to conserve space.
30 *
31 *
32 * <p>A BreedingPipeline implements SteadyStateBSourceForm, meaning that
33 * it receives the individualReplaced(...) and sourcesAreProperForm(...) messages.
34 * however by default it doesn't do anything with these except distribute them
35 * to its sources.  You might override these to do something more interesting.
36
37 <p><b>Parameters</b><br>
38 <table>
39 <tr><td valign=top><i>base</i>.<tt>num-sources</tt><br>
40 <font size=-1>int &gt;= 1</font></td>
41 <td valign=top>(User-specified number of sources to the pipeline. 
42 Some pipelines have hard-coded numbers of sources; others indicate
43 (with the java constant DYNAMIC_SOURCES) that the number of sources is determined by this
44 user parameter instead.)</td></tr>
45
46 <tr><td valign=top><i>base</i>.<tt>source.</tt><i>n</i><br>
47 <font size=-1>classname, inherits and != BreedingSource, or the value <tt>same</tt><br>
48 <td valign=top>(Source <i>n</i> for this BreedingPipeline.
49 If the value is set to <tt>same</tt>, then this source is the
50 exact same source object as <i>base</i>.<tt>source.</tt><i>n-1</i>, and
51 further parameters for this object will be ignored and treated as the same
52 as those for <i>n-1</i>.  <tt>same<tt> is not valid for
53 <i>base</i>.<tt>source.0</tt>)</td></tr>
54 </table>
55
56 <p><b>Parameter bases</b><br>
57 <table>
58
59 <tr><td valign=top><i>base</i>.<tt>source.</tt><i>n</i><br>
60 <td>Source <i>n</i></td></tr>
61 </table>
62
63 * @author Sean Luke
64 * @version 1.0
65 */
66
67public abstract class BreedingPipeline extends BreedingSource implements SteadyStateBSourceForm
68    {
69    /** Indicates that a source is the exact same source as the previous source. */
70    public static final String V_SAME = "same";
71
72    /** Indicates the probability that the Breeding Pipeline will perform its mutative action instead of just doing reproduction. */
73    public static final String P_LIKELIHOOD = "likelihood";
74
75    /** Indicates that the number of sources is variable and determined by the
76        user in the parameter file. */
77
78    public static final int DYNAMIC_SOURCES = 0;
79
80    /** Standard parameter for number of sources (only used if numSources
81        returns DYNAMIC_SOURCES */
82
83    public static final String P_NUMSOURCES = "num-sources";
84
85    /** Standard parameter for individual-selectors associated with a BreedingPipeline */
86    public static final String P_SOURCE = "source";
87
88    /** My parameter base -- I keep it around so I can print some messages that
89        are useful with it (not deep cloned) */
90       
91    public Parameter mybase;
92
93    public float likelihood;
94
95    /** Array of sources feeding the pipeline */
96    public BreedingSource[] sources;
97
98    /** Returns the number of sources to this pipeline.  Called during
99        BreedingPipeline's setup.  Be sure to return a value > 0, or
100        DYNAMIC_SOURCES which indicates that setup should check the parameter
101        file for the parameter "num-sources" to make its determination. */
102
103    public abstract int numSources();
104           
105    /** Returns the minimum among the typicalIndsProduced() for any children --
106        a function that's useful internally, not very useful for you to call externally. */
107    public int minChildProduction()
108        {
109        if (sources.length==0) return 0;
110        int min = sources[0].typicalIndsProduced();
111        for(int x=1;x<sources.length;x++)
112            {
113            int cur = sources[x].typicalIndsProduced();
114            if (min > cur) min = cur;
115            }
116        return min;
117        }
118
119    /** Returns the maximum among the typicalIndsProduced() for any children --
120        a function that's useful internally, not very useful for you to call externally. */
121    public int maxChildProduction()
122        {
123        if (sources.length==0) return 0;
124        int max = sources[0].typicalIndsProduced();
125        for(int x=1;x<sources.length;x++)
126            {
127            int cur = sources[x].typicalIndsProduced();
128            if (max < cur) max = cur;
129            }
130        return max;
131        }
132
133
134    /** Returns the "typical" number of individuals produced -- by default
135        this is the minimum typical number of individuals produced by any
136        children sources of the pipeline.  If you'd prefer something different,
137        override this method. */
138    public int typicalIndsProduced()
139        {
140        return minChildProduction();
141        }
142
143    public void setup(final EvolutionState state, final Parameter base)
144        {
145        super.setup(state,base);
146        mybase = base;
147       
148        Parameter def = defaultBase();
149
150        likelihood = state.parameters.getFloatWithDefault(base.push(P_LIKELIHOOD), def.push(P_LIKELIHOOD), 1.0f);
151        if (likelihood < 0.0f || likelihood > 1.0f)
152            state.output.fatal("Breeding Pipeline likelihood must be a value between 0.0 and 1.0 inclusive",
153                base.push(P_LIKELIHOOD),
154                def.push(P_LIKELIHOOD));
155
156
157        int numsources = numSources();
158        if (numsources <= DYNAMIC_SOURCES)
159            {
160            // figure it from the file
161            numsources = state.parameters.getInt(
162                base.push(P_NUMSOURCES), def.push(P_NUMSOURCES),1);
163            if (numsources==0)
164                state.output.fatal("Breeding pipeline num-sources value must be > 0",
165                    base.push(P_NUMSOURCES),
166                    def.push(P_NUMSOURCES));
167            }
168
169        sources = new BreedingSource[numsources];
170
171        for(int x=0;x<sources.length;x++)
172            {
173            Parameter p = base.push(P_SOURCE).push(""+x);
174            Parameter d = def.push(P_SOURCE).push(""+x);
175
176            String s = state.parameters.getString(p,d);
177            if (s!=null && s.equals(V_SAME))
178                {
179                if (x==0)  // oops
180                    state.output.fatal(
181                        "Source #0 cannot be declared with the value \"same\".",
182                        p,d);
183               
184                // else the source is the same source as before
185                sources[x] = sources[x-1];
186                }
187            else
188                {
189                sources[x] = (BreedingSource)
190                    (state.parameters.getInstanceForParameter(
191                        p,d,BreedingSource.class));
192                sources[x].setup(state,p);
193                }
194            }
195        state.output.exitIfErrors();
196        }
197
198
199    public Object clone()
200        {
201        BreedingPipeline c = (BreedingPipeline)(super.clone());
202       
203        // make a new array
204        c.sources = new BreedingSource[sources.length];
205
206        // clone the sources -- we won't go through the hassle of
207        // determining if we have a DAG or not -- we'll just clone
208        // it out to a tree.  I doubt it's worth it.
209
210        for(int x=0;x<sources.length;x++)
211            {
212            if (x==0 || sources[x]!=sources[x-1])
213                c.sources[x] = (BreedingSource)(sources[x].clone());
214            else
215                c.sources[x] = c.sources[x-1];
216            }
217
218        return c;
219        }
220
221    /** Performs direct cloning of n individuals.  if produceChildrenFromSource is true, then */
222    public int reproduce(final int n,
223        final int start,
224        final int subpopulation,
225        final Individual[] inds,
226        final EvolutionState state,
227        final int thread,
228        boolean produceChildrenFromSource)
229        {
230        if (produceChildrenFromSource)
231            sources[0].produce(n,n,start,subpopulation,inds,state,thread);
232        if (sources[0] instanceof SelectionMethod)
233            for(int q=start; q < n+start; q++)
234                inds[q] = (Individual)(inds[q].clone());
235        return n;
236        }
237
238
239    public boolean produces(final EvolutionState state,
240        final Population newpop,
241        final int subpopulation,
242        int thread)
243        {
244        for(int x=0;x<sources.length;x++)
245            if (x==0 || sources[x]!=sources[x-1])
246                if (!sources[x].produces(state,newpop,subpopulation,thread))
247                    return false;
248        return true;
249        }
250
251    public void prepareToProduce(final EvolutionState state,
252        final int subpopulation,
253        final int thread)
254        {
255        for(int x=0;x<sources.length;x++)
256            if (x==0 || sources[x]!=sources[x-1])
257                sources[x].prepareToProduce(state,subpopulation,thread);
258        }
259
260    public void finishProducing(final EvolutionState state,
261        final int subpopulation,
262        final int thread)
263        {
264        for(int x=0;x<sources.length;x++)
265            if (x==0 || sources[x]!=sources[x-1])
266                sources[x].finishProducing(state,subpopulation,thread);
267        }
268
269    public void preparePipeline(Object hook)
270        {
271        // the default form calls this on all the sources.
272        // note that it follows all the source paths even if they're
273        // duplicates
274        for(int x=0; x<sources.length;x++)
275            sources[x].preparePipeline(hook);
276        }
277       
278    public void individualReplaced(final SteadyStateEvolutionState state,
279        final int subpopulation,
280        final int thread,
281        final int individual)
282        {
283        for(int x=0; x<sources.length;x++)
284            ((SteadyStateBSourceForm)(sources[x])).individualReplaced(state,subpopulation,thread,individual);
285        }
286
287    public void sourcesAreProperForm(final SteadyStateEvolutionState state)
288        {
289        for(int x=0; x<sources.length;x++)
290            if (! (sources[x] instanceof SteadyStateBSourceForm))
291                {
292                state.output.error("The following breeding source is not of SteadyStateBSourceForm.",
293                    mybase.push(P_SOURCE).push(""+x), defaultBase().push(P_SOURCE).push(""+x));
294                }
295            else
296                ((SteadyStateBSourceForm)(sources[x])).sourcesAreProperForm(state);
297        }
298
299    }
300
301
Note: See TracBrowser for help on using the repository browser.