[6152] | 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 | |
---|
| 8 | package ec.select; |
---|
| 9 | import ec.util.*; |
---|
| 10 | import ec.*; |
---|
| 11 | /* |
---|
| 12 | * BestSelection.java |
---|
| 13 | * |
---|
| 14 | * Created: Thu Feb 10 18:52:09 2000 |
---|
| 15 | * By: Sean Luke |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * Picks among the best <i>n</i> individuals in a population in |
---|
| 20 | * direct proportion to their absolute |
---|
| 21 | * fitnesses as returned by their fitness() methods relative to the |
---|
| 22 | * fitnesses of the other "best" individuals in that <i>n</i>. This is expensive to |
---|
| 23 | * set up and bring down, so it's not appropriate for steady-state evolution. |
---|
| 24 | * If you're not familiar with the relative advantages of |
---|
| 25 | * selection methods and just want a good one, |
---|
| 26 | * use TournamentSelection instead. Not appropriate for |
---|
| 27 | * multiobjective fitnesses. |
---|
| 28 | * |
---|
| 29 | * <p><b><font color=red> |
---|
| 30 | * Note: Fitnesses must be non-negative. 0 is assumed to be the worst fitness. |
---|
| 31 | * </font></b> |
---|
| 32 | * |
---|
| 33 | <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br> |
---|
| 34 | Always 1. |
---|
| 35 | |
---|
| 36 | <p><b>Parameters</b><br> |
---|
| 37 | <table> |
---|
| 38 | <tr><td valign=top><i>base.</i><tt>pick-worst</tt><br> |
---|
| 39 | <font size=-1> bool = <tt>true</tt> or <tt>false</tt> (default)</font></td> |
---|
| 40 | <td valign=top>(should we pick from among the <i>worst n</i> individuals in the tournament instead of the <i>best n</i>?)</td></tr> |
---|
| 41 | <tr><td valign=top><i>base.</i><tt>n</tt><br> |
---|
| 42 | <font size=-1> int > 0 (default is 1)</font></td> |
---|
| 43 | <td valign=top>(the number of best-individuals to select from)</td></tr> |
---|
| 44 | </table> |
---|
| 45 | |
---|
| 46 | <p><b>Default Base</b><br> |
---|
| 47 | select.best |
---|
| 48 | |
---|
| 49 | * @author Sean Luke |
---|
| 50 | * @version 1.0 |
---|
| 51 | */ |
---|
| 52 | |
---|
| 53 | public class BestSelection extends SelectionMethod |
---|
| 54 | { |
---|
| 55 | /** Default base */ |
---|
| 56 | public static final String P_BEST = "best"; |
---|
| 57 | public static final String P_N = "n"; |
---|
| 58 | public static final String P_PICKWORST = "pick-worst"; |
---|
| 59 | /** Sorted, normalized, totalized fitnesses for the population */ |
---|
| 60 | public float[] sortedFit; |
---|
| 61 | /** Sorted population -- since I *have* to use an int-sized |
---|
| 62 | individual (short gives me only 16K), |
---|
| 63 | I might as well just have pointers to the |
---|
| 64 | population itself. :-( */ |
---|
| 65 | public int[] sortedPop; |
---|
| 66 | |
---|
| 67 | /** Do we pick the worst instead of the best? */ |
---|
| 68 | public boolean pickWorst; |
---|
| 69 | |
---|
| 70 | public int bestn; |
---|
| 71 | |
---|
| 72 | public Parameter defaultBase() |
---|
| 73 | { |
---|
| 74 | return SelectDefaults.base().push(P_BEST); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // don't need clone etc. |
---|
| 78 | |
---|
| 79 | public void setup(final EvolutionState state, final Parameter base) |
---|
| 80 | { |
---|
| 81 | super.setup(state,base); |
---|
| 82 | |
---|
| 83 | Parameter def = defaultBase(); |
---|
| 84 | |
---|
| 85 | bestn = |
---|
| 86 | state.parameters.getInt(base.push(P_N),def.push(P_N),1); |
---|
| 87 | if (bestn == 0 ) |
---|
| 88 | state.output.fatal("n must be an integer greater than 0", base.push(P_N),def.push(P_N)); |
---|
| 89 | |
---|
| 90 | pickWorst = state.parameters.getBoolean(base.push(P_PICKWORST),def.push(P_PICKWORST),false); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | public void prepareToProduce(final EvolutionState s, |
---|
| 94 | final int subpopulation, |
---|
| 95 | final int thread) |
---|
| 96 | { |
---|
| 97 | // load sortedPop integers |
---|
| 98 | final Individual[] i = s.population.subpops[subpopulation].individuals; |
---|
| 99 | |
---|
| 100 | sortedPop = new int[i.length]; |
---|
| 101 | for(int x=0;x<sortedPop.length;x++) sortedPop[x] = x; |
---|
| 102 | |
---|
| 103 | // sort sortedPop in increasing fitness order |
---|
| 104 | QuickSort.qsort(sortedPop, |
---|
| 105 | new SortComparatorL() |
---|
| 106 | { |
---|
| 107 | public boolean lt(long a, long b) |
---|
| 108 | { |
---|
| 109 | return ((Individual)(i[(int)b])).fitness.betterThan( |
---|
| 110 | ((Individual)(i[(int)a])).fitness); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public boolean gt(long a, long b) |
---|
| 114 | { |
---|
| 115 | return ((Individual)(i[(int)a])).fitness.betterThan( |
---|
| 116 | ((Individual)(i[(int)b])).fitness); |
---|
| 117 | } |
---|
| 118 | }); |
---|
| 119 | |
---|
| 120 | // load sortedFit |
---|
| 121 | sortedFit = new float[Math.min(sortedPop.length,bestn)]; |
---|
| 122 | if (pickWorst) |
---|
| 123 | for(int x=0;x<sortedFit.length;x++) |
---|
| 124 | sortedFit[x] = ((Individual)(i[sortedPop[x]])).fitness.fitness(); |
---|
| 125 | else |
---|
| 126 | for(int x=0;x<sortedFit.length;x++) |
---|
| 127 | sortedFit[x] = ((Individual)(i[sortedPop[sortedPop.length-x-1]])).fitness.fitness(); |
---|
| 128 | |
---|
| 129 | for(int x=0;x<sortedFit.length;x++) |
---|
| 130 | { |
---|
| 131 | if (sortedFit[x] < 0) // uh oh |
---|
| 132 | s.output.fatal("Discovered a negative fitness value. BestSelection requires that all fitness values be non-negative(offending subpopulation #" + subpopulation + ")"); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | // organize the distributions. All zeros in fitness is fine |
---|
| 137 | RandomChoice.organizeDistribution(sortedFit, true); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | public int produce(final int subpopulation, |
---|
| 141 | final EvolutionState state, |
---|
| 142 | final int thread) |
---|
| 143 | { |
---|
| 144 | // Pick and return an individual from the population |
---|
| 145 | if (pickWorst) |
---|
| 146 | return sortedPop[RandomChoice.pickFromDistribution( |
---|
| 147 | sortedFit,state.random[thread].nextFloat())]; |
---|
| 148 | else |
---|
| 149 | return sortedPop[sortedPop.length - RandomChoice.pickFromDistribution( |
---|
| 150 | sortedFit,state.random[thread].nextFloat()) - 1]; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | public void finishProducing(final EvolutionState s, |
---|
| 154 | final int subpopulation, |
---|
| 155 | final int thread) |
---|
| 156 | { |
---|
| 157 | // release the distributions so we can quickly |
---|
| 158 | // garbage-collect them if necessary |
---|
| 159 | sortedFit = null; |
---|
| 160 | sortedPop = null; |
---|
| 161 | } |
---|
| 162 | } |
---|