Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/select/TournamentSelection.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: 5.9 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.*;
11import ec.steadystate.*;
12
13/*
14 * TournamentSelection.java
15 *
16 * Created: Mon Aug 30 19:27:15 1999
17 * By: Sean Luke
18 */
19
20/**
21 * Does a simple tournament selection, limited to the subpopulation it's
22 * working in at the time.
23 *
24 * <p>Tournament selection works like this: first, <i>size</i> individuals
25 * are chosen at random from the population.  Then of those individuals,
26 * the one with the best fitness is selected. 
27 *
28 * <p><i>size</i> can also be a floating-point value between 1.0 and 2.0,
29 * exclusive of them. In this situation, two individuals are chosen at random, and
30 * the better one is selected with a probability of <i>size/2</i>
31 *
32 * <p>Common sizes for <i>size</i> include: 2, popular in Genetic Algorithms
33 * circles, and 7, popularized in Genetic Programming by John Koza.
34 * If the size is 1, then individuals are picked entirely at random.
35 *
36 * <p>Tournament selection is so simple that it doesn't need to maintain
37 * a cache of any form, so many of the SelectionMethod methods just
38 * don't do anything at all.
39 *
40
41 <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br>
42 Always 1.
43
44 <p><b>Parameters</b><br>
45 <table>
46 <tr><td valign=top><i>base.</i><tt>size</tt><br>
47 <font size=-1>float &gt;= 1</font></td>
48 <td valign=top>(the tournament size)</td></tr>
49
50 <tr><td valign=top><i>base.</i><tt>pick-worst</tt><br>
51 <font size=-1> bool = <tt>true</tt> or <tt>false</tt> (default)</font></td>
52 <td valign=top>(should we pick the <i>worst</i> individual in the tournament instead of the <i>best</i>?)</td></tr>
53
54 </table>
55
56 <p><b>Default Base</b><br>
57 select.tournament
58
59 *
60 * @author Sean Luke
61 * @version 1.0
62 */
63
64public class TournamentSelection extends SelectionMethod implements SteadyStateBSourceForm
65    {
66    /** default base */
67    public static final String P_TOURNAMENT = "tournament";
68
69    public static final String P_PICKWORST = "pick-worst";
70
71    /** size parameter */
72    public static final String P_SIZE = "size";
73
74    /* Default size */
75    public static final int DEFAULT_SIZE = 7;
76
77    /** Base size of the tournament; this may change.  */
78    int size;
79
80    /** Probablity of picking the size plus one */
81    public double probabilityOfPickingSizePlusOne;
82   
83    /** Do we pick the worst instead of the best? */
84    public boolean pickWorst;
85
86    public Parameter defaultBase()
87        {
88        return SelectDefaults.base().push(P_TOURNAMENT);
89        }
90   
91    public void setup(final EvolutionState state, final Parameter base)
92        {
93        super.setup(state,base);
94       
95        Parameter def = defaultBase();
96
97        double val = state.parameters.getDouble(base.push(P_SIZE),def.push(P_SIZE),1.0);
98        if (val < 1.0)
99            state.output.fatal("Tournament size must be >= 1.",base.push(P_SIZE),def.push(P_SIZE));
100        else if (val == (int) val)  // easy, it's just an integer
101            {
102            size = (int) val;
103            probabilityOfPickingSizePlusOne = 0.0;
104            }
105        else
106            {
107            size = (int) Math.floor(val);
108            probabilityOfPickingSizePlusOne = val - size;  // for example, if we have 5.4, then the probability of picking *6* is 0.4
109            }
110
111        pickWorst = state.parameters.getBoolean(base.push(P_PICKWORST),def.push(P_PICKWORST),false);
112        }
113
114    /** Returns a tournament size to use, at random, based on base size and probability of picking the size plus one. */
115    public int getTournamentSizeToUse(MersenneTwisterFast random)
116        {
117        double p = probabilityOfPickingSizePlusOne;   // pulls us to under 35 bytes
118        if (p == 0.0) return size;
119        return size + (random.nextBoolean(p) ? 1 : 0);
120        }
121
122
123    /** Produces the index of a (typically uniformly distributed) randomly chosen individual
124        to fill the tournament.  <i>number</> is the position of the individual in the tournament.  */
125    public int getRandomIndividual(int number, int subpopulation, EvolutionState state, int thread)
126        {
127        Individual[] oldinds = state.population.subpops[subpopulation].individuals;
128        return state.random[thread].nextInt(oldinds.length);
129        }
130
131    /** Returns true if *first* is a better (fitter, whatever) individual than *second*. */
132    public boolean betterThan(Individual first, Individual second, int subpopulation, EvolutionState state, int thread)
133        {
134        return first.fitness.betterThan(second.fitness);
135        }
136               
137    public int produce(final int subpopulation,
138        final EvolutionState state,
139        final int thread)
140        {
141        // pick size random individuals, then pick the best.
142        Individual[] oldinds = state.population.subpops[subpopulation].individuals;
143        int best = getRandomIndividual(0, subpopulation, state, thread);
144       
145        int s = getTournamentSizeToUse(state.random[thread]);
146               
147        if (pickWorst)
148            for (int x=1;x<s;x++)
149                {
150                int j = getRandomIndividual(x, subpopulation, state, thread);
151                if (!betterThan(oldinds[j], oldinds[best], subpopulation, state, thread))  // j is at least as bad as best
152                    best = j;
153                }
154        else
155            for (int x=1;x<s;x++)
156                {
157                int j = getRandomIndividual(x, subpopulation, state, thread);
158                if (betterThan(oldinds[j], oldinds[best], subpopulation, state, thread))  // j is better than best
159                    best = j;
160                }
161           
162        return best;
163        }
164
165    // included for SteadyState
166    public void individualReplaced(final SteadyStateEvolutionState state,
167        final int subpopulation,
168        final int thread,
169        final int individual)
170        { return; }
171   
172    public void sourcesAreProperForm(final SteadyStateEvolutionState state)
173        { return; }
174   
175    }
Note: See TracBrowser for help on using the repository browser.