Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/parsimony/DoubleTournamentSelection.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.8 KB
Line 
1/*
2  Copyright 2006 by Sean Luke and George Mason University
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6
7
8package ec.parsimony;
9import ec.select.*;
10import ec.*;
11import ec.util.*;
12import ec.steadystate.*;
13
14/**
15 *
16 * DoubleTournamentSelection.java
17 *
18 * There are 2 tournaments for each selection of an individual. In the first
19 * ("qualifying") tournament, <i>size</i> individuals
20 * are selected and the <i>best</i> one (based on individuals' length if <i>do-length-first</i>
21 * is true, or based on individual's fitness otherwise). This process repeat <i>size2</i> times,
22 * so we end up with <i>size2</i> winners on one criteria. Then, there is second "champion" tournament
23 * on the other criteria (fitness if <i>do-length-first</i> is true, size otherwise) among the
24 * <i>size2</i> individuals, and the best one is the one returned by this selection method.
25 *
26 <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br>
27 Always 1.
28
29 <p><b>Parameters</b><br>
30 <table>
31 <tr><td valign=top><i>base.</i><tt>size</tt><br>
32 <font size=-1>int &gt;= 1 (default 7)</font></td>
33 <td valign=top>(the tournament size for the initial ("qualifying") tournament)</td></tr>
34
35 <tr><td valign=top><i>base.</i><tt>size2</tt><br>
36 <font size=-1>int &gt;= 1 (default 7)</font></td>
37 <td valign=top>(the tournament size for the final ("champion") tournament)</td></tr>
38
39 <tr><td valign=top><i>base.</i><tt>pick-worst</tt><br>
40 <font size=-1> bool = <tt>true</tt> or <tt>false</tt> (default)</font></td>
41 <td valign=top>(should we pick the <i>worst</i> individual in the initial ("qualifying") tournament instead of the <i>best</i>?)</td></tr>
42
43 <tr><td valign=top><i>base.</i><tt>pick-worst2</tt><br>
44 <font size=-1> bool = <tt>true</tt> or <tt>false</tt> (default)</font></td>
45 <td valign=top>(should we pick the <i>worst</i> individual in the final ("champion") tournament instead of the <i>best</i>?)</td></tr>
46
47 <tr><td valign=top><i>base.</i><tt>do-length-first</tt><br>
48 <font size=-1> bool = <tt>true</tt> (default) or <tt>false</tt></font></td>
49 <td valign=top>(should the initial ("qualifying") tournament be based on the length of the individual or (if false) the fitness of the individual?  The final ("champion") tournament will be based on the alternative option)</td></tr>
50 </table>
51
52 <p><b>Default Base</b><br>
53 select.double-tournament
54
55 *
56 */
57
58/**
59 *
60 *
61 * @author Sean Luke & Liviu Panait
62 * @version 1.0
63 *
64 */
65
66public class DoubleTournamentSelection extends SelectionMethod implements SteadyStateBSourceForm
67    {
68    /** default base */
69    public static final String P_TOURNAMENT = "double-tournament";
70
71    public static final String P_PICKWORST = "pick-worst";
72    public static final String P_PICKWORST2 = "pick-worst2";
73
74    public static final String P_DOLENGTHFIRST = "do-length-first";
75   
76    /** size parameter */
77    public static final String P_SIZE = "size";
78    public static final String P_SIZE2 = "size2";
79
80    /* Default size */
81    public static final int DEFAULT_SIZE = 7;
82
83    /** Size of the tournament*/
84    public int size;
85    public int size2;
86
87    /** What's our probability of selection? If 1.0, we always pick the "good" individual. */
88    public double probabilityOfSelection;
89    public double probabilityOfSelection2;
90
91    /** Do we pick the worst instead of the best? */
92    public boolean pickWorst;
93    public boolean pickWorst2;
94    public boolean doLengthFirst;
95
96    public Parameter defaultBase()
97        {
98        return SelectDefaults.base().push(P_TOURNAMENT);
99        }
100   
101    public void setup(final EvolutionState state, final Parameter base)
102        {
103        super.setup(state,base);
104       
105        Parameter def = defaultBase();
106
107        double val = state.parameters.getDouble(base.push(P_SIZE),def.push(P_SIZE),1.0);
108        if (val < 1.0)
109            state.output.fatal("Tournament size must be >= 1.",base.push(P_SIZE),def.push(P_SIZE));
110        else if (val > 1 && val < 2) // pick with probability
111            {
112            size = 2;
113            probabilityOfSelection = (val/2);
114            }
115        else if (val != (int)val)  // it's not an integer
116            state.output.fatal("If >= 2, Tournament size must be an integer.", base.push(P_SIZE), def.push(P_SIZE));
117        else
118            {
119            size = (int)val;
120            probabilityOfSelection = 1.0;
121            }
122
123        val = state.parameters.getDouble(base.push(P_SIZE2),def.push(P_SIZE2),1.0);
124        if (val < 1.0)
125            state.output.fatal("Tournament size2 must be >= 1.",base.push(P_SIZE2),def.push(P_SIZE2));
126        else if (val > 1 && val < 2) // pick with probability
127            {
128            size2 = 2;
129            probabilityOfSelection2 = (val/2);
130            }
131        else if (val != (int)val)  // it's not an integer
132            state.output.fatal("If >= 2, Tournament size2 must be an integer.", base.push(P_SIZE2), def.push(P_SIZE2));
133        else
134            {
135            size2 = (int)val;
136            probabilityOfSelection2 = 1.0;
137            }
138
139        doLengthFirst = state.parameters.getBoolean(base.push(P_DOLENGTHFIRST),def.push(P_DOLENGTHFIRST),true);
140        pickWorst = state.parameters.getBoolean(base.push(P_PICKWORST),def.push(P_PICKWORST),false);
141        pickWorst2 = state.parameters.getBoolean(base.push(P_PICKWORST2),def.push(P_PICKWORST2),false);
142        }
143
144    /**
145       Produces the index of a person selected from among several by a tournament.
146       The tournament's criteria is fitness of individuals if doLengthFirst is true,
147       otherwise the size of the individuals.
148    */
149    public int produce(final int subpopulation,
150        final EvolutionState state,
151        final int thread)
152        {
153        int[] inds = new int[size2];
154        for(int x=0;x<size2;x++) inds[x] = make(subpopulation,state,thread);
155
156        if (!doLengthFirst)
157            {
158            // pick size random individuals, then pick the best.
159            Individual[] oldinds = state.population.subpops[subpopulation].individuals;
160            int i = inds[0];
161            int bad = i;
162           
163            for (int x=1;x<size2;x++)
164                {
165                int j = inds[x];
166                if (pickWorst2)
167                    { if (oldinds[j].size() > oldinds[i].size()) { bad = i; i = j; } else bad = j; }
168                else
169                    { if (oldinds[j].size() < oldinds[i].size()) { bad = i; i = j;} else bad = j; }
170                }
171           
172            if (probabilityOfSelection2 != 1.0 && !state.random[thread].nextBoolean(probabilityOfSelection2))
173                i = bad;
174            return i;
175            }
176        else
177            {
178            // pick size random individuals, then pick the best.
179            Individual[] oldinds = state.population.subpops[subpopulation].individuals;
180            int i = inds[0];
181            int bad = i;
182           
183            for (int x=1;x<size2;x++)
184                {
185                int j = inds[x];
186                if (pickWorst2)
187                    { if (!(oldinds[j].fitness.betterThan(oldinds[i].fitness))) { bad = i; i = j; } else bad = j; }
188                else
189                    { if (oldinds[j].fitness.betterThan(oldinds[i].fitness)) { bad = i; i = j;} else bad = j; }
190                }
191           
192            if (probabilityOfSelection2 != 1.0 && !state.random[thread].nextBoolean(probabilityOfSelection2))
193                i = bad;
194            return i;
195            }
196        }
197
198    /**
199       Produces the index of a person selected from among several by a tournament.
200       The tournament's criteria is size of individuals if doLengthFirst is true,
201       otherwise the fitness of the individuals.
202    */
203    public int make(final int subpopulation,
204        final EvolutionState state,
205        final int thread)
206        {
207        if (doLengthFirst) // if length first, the first tournament is based on size
208            {
209            // pick size random individuals, then pick the best.
210            Individual[] oldinds = state.population.subpops[subpopulation].individuals;
211            int i = state.random[thread].nextInt(oldinds.length) ;
212            int bad = i;
213           
214            for (int x=1;x<size;x++)
215                {
216                int j = state.random[thread].nextInt(oldinds.length);
217                if (pickWorst)
218                    { if (oldinds[j].size() > oldinds[i].size()) { bad = i; i = j; } else bad = j; }
219                else
220                    { if (oldinds[j].size() < oldinds[i].size()) { bad = i; i = j;} else bad = j; }
221                }
222           
223            if (probabilityOfSelection != 1.0 && !state.random[thread].nextBoolean(probabilityOfSelection))
224                i = bad;
225            return i;
226            }
227        else
228            {
229            // pick size random individuals, then pick the best.
230            Individual[] oldinds = state.population.subpops[subpopulation].individuals;
231            int i = state.random[thread].nextInt(oldinds.length) ;
232            int bad = i;
233           
234            for (int x=1;x<size;x++)
235                {
236                int j = state.random[thread].nextInt(oldinds.length);
237                if (pickWorst)
238                    { if (!(oldinds[j].fitness.betterThan(oldinds[i].fitness))) { bad = i; i = j; } else bad = j; }
239                else
240                    { if (oldinds[j].fitness.betterThan(oldinds[i].fitness)) { bad = i; i = j;} else bad = j; }
241                }
242           
243            if (probabilityOfSelection != 1.0 && !state.random[thread].nextBoolean(probabilityOfSelection))
244                i = bad;
245            return i;
246            }
247        }
248
249
250    public void individualReplaced(final SteadyStateEvolutionState state,
251        final int subpopulation,
252        final int thread,
253        final int individual)
254        { return; }
255   
256    public void sourcesAreProperForm(final SteadyStateEvolutionState state)
257        { return; }
258   
259    }
Note: See TracBrowser for help on using the repository browser.