Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/select/MultiSelection.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: 5.8 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.select;
9import ec.*;
10import ec.util.*;
11
12/*
13 * MultiSelection.java
14 *
15 * Created: Wed Dec 29 21:44:50 1999
16 * By: Sean Luke
17 */
18
19/**
20 * MultiSelection is a SelectionMethod which stores some <i>n</i> subordinate
21 * SelectionMethods.  Each time it must produce an individual,
22 * it picks one of these SelectionMethods at random and has it do the production
23 * instead.
24 
25 <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br>
26 Always 1.
27
28 <p><b>Parameters</b><br>
29 <table>
30 <tr><td valign=top><i>base</i>.<tt>num-selects</tt><br>
31 <font size=-1>int &gt;= 1</font></td>
32 <td valign=top>(The number of subordinate SelectionMethods)</td></tr>
33
34 <tr><td valign=top><i>base</i>.<tt>select.</tt><i>n</i><br>
35 <font size=-1>classname, inherits and != SelectionMethod</tt><br>
36 <td valign=top>(Subordinate SelectionMethod <i>n</i>)</td></tr>
37 </table>
38
39 <p><b>Default Base</b><br>
40 select.multiselect
41
42 <p><b>Parameter bases</b><br>
43 <table>
44
45 <tr><td valign=top><i>base</i>.<tt>select.</tt><i>n</i><br>
46 <td>Subordinate SelectionMethod <i>n</i></td></tr>
47 </table>
48
49 * @author Sean Luke
50 * @version 1.0
51 */
52
53public class MultiSelection extends SelectionMethod
54    {
55    public static final String P_NUMSELECTS = "num-selects";
56    public static final String P_SELECT = "select";
57    public static final String P_MULTISELECT = "multiselect";
58
59    /** The MultiSelection's individuals */
60    public SelectionMethod selects[];
61
62    public Parameter defaultBase()
63        {
64        return SelectDefaults.base().push(P_MULTISELECT);
65        }
66
67    public Object clone()
68        {
69        MultiSelection c = (MultiSelection)(super.clone());
70       
71        // make a new array
72        c.selects = new SelectionMethod[selects.length];
73
74        // clone the selects -- we won't go through the hassle of
75        // determining if we have a DAG or not -- we'll just clone
76        // it out to a tree.  I doubt it's worth it.
77
78        for(int x=0;x<selects.length;x++)
79            c.selects[x] = (SelectionMethod)(selects[x].clone());
80
81        return c;
82        }
83
84    public void setup(final EvolutionState state, final Parameter base)
85        {
86        super.setup(state,base);
87
88        Parameter def = defaultBase();
89
90        int numSelects = state.parameters.getInt(
91            base.push(P_NUMSELECTS),def.push(P_NUMSELECTS),1);
92        if (numSelects==0)
93            state.output.fatal("The number of MultiSelection sub-selection methods must be >= 1).",
94                base.push(P_NUMSELECTS),def.push(P_NUMSELECTS));
95
96        // make our arrays
97        selects = new SelectionMethod[numSelects];
98
99        float total = 0.0f;
100
101        for(int x=0;x<numSelects;x++)
102            {
103            Parameter p = base.push(P_SELECT).push(""+x);
104            Parameter d = def.push(P_SELECT).push(""+x);
105
106            selects[x] = (SelectionMethod)
107                (state.parameters.getInstanceForParameter(
108                    p,d, SelectionMethod.class));       
109            selects[x].setup(state,p);
110           
111            // now check probability
112            if (selects[x].probability<0.0)
113                state.output.error(
114                    "MultiSelection select #" + x +
115                    " must have a probability >= 0.0",
116                    p.push(P_PROB),d.push(P_PROB));
117            else total += selects[x].probability;
118            }
119
120        state.output.exitIfErrors();
121
122        // Now check for valid probability
123        if (total <= 0.0)
124            state.output.fatal("MultiSelection selects do not sum to a positive probability",base);
125
126        if (total != 1.0)
127            {
128            state.output.message("Must normalize probabilities for " + base);
129            for(int x=0;x<numSelects;x++) selects[x].probability /= total;
130            }
131
132        // totalize
133        float tmp = 0.0f;
134        for(int x=0;x<numSelects-1;x++) // yes, it's off by one
135            {
136            tmp += selects[x].probability;
137            selects[x].probability = tmp;
138            }
139        selects[numSelects-1].probability = 1.0f;
140        }
141
142    public boolean produces(final EvolutionState state,
143        final Population newpop,
144        final int subpopulation,
145        final int thread)
146        {
147        if (!super.produces(state,newpop,subpopulation,thread))
148            return false;
149
150        for(int x=0;x<selects.length;x++)
151            if (!selects[x].produces(state,newpop,subpopulation,thread))
152                return false;
153        return true;
154        }
155
156
157    public void prepareToProduce(final EvolutionState s,
158        final int subpopulation,
159        final int thread)
160        {
161        for(int x=0;x<selects.length;x++)
162            selects[x].prepareToProduce(s,subpopulation,thread);
163        }
164
165
166    public int produce(final int subpopulation,
167        final EvolutionState state,
168        final int thread)
169        {
170        return selects[BreedingSource.pickRandom(
171                selects,state.random[thread].nextFloat())].produce(
172                    subpopulation,state,thread);
173        }
174
175    public int produce(final int min,
176        final int max,
177        final int start,
178        final int subpopulation,
179        final Individual[] inds,
180        final EvolutionState state,
181        final int thread)
182
183        {
184        return selects[BreedingSource.pickRandom(
185                selects,state.random[thread].nextFloat())].produce(
186                    min,max,start,subpopulation,inds,state,thread);
187        }
188
189    public void preparePipeline(Object hook)
190        {
191        // the default form calls this on all the selects.
192        // note that it follows all the source paths even if they're
193        // duplicates
194        for(int x=0; x<selects.length;x++)
195            selects[x].preparePipeline(hook);
196        }
197       
198    }
Note: See TracBrowser for help on using the repository browser.