[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 | /* |
---|
| 13 | * SigmaScalingSelection.java |
---|
| 14 | * |
---|
| 15 | * Created: Fri Jun 5 2009 |
---|
| 16 | * By: Jack Compton |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * Similar to FitProportionateSelection, but with adjustments to scale up/exaggerate differences in fitness for selection when true fitness values are very close to |
---|
| 21 | * eachother across the population. This addreses a common problem with FitProportionateSelection wherein selection approaches random selection during |
---|
| 22 | * late runs when fitness values do not differ by much. |
---|
| 23 | * |
---|
| 24 | * <p> |
---|
| 25 | * Like FitProportionateSelection this is not appropriate for steady-state evolution. |
---|
| 26 | * If you're not familiar with the relative advantages of |
---|
| 27 | * selection methods and just want a good one, |
---|
| 28 | * use TournamentSelection instead. Not appropriate for |
---|
| 29 | * multiobjective fitnesses. |
---|
| 30 | * |
---|
| 31 | * <p><b><font color=red> |
---|
| 32 | * Note: Fitnesses must be non-negative. 0 is assumed to be the worst fitness. |
---|
| 33 | * </font></b> |
---|
| 34 | |
---|
| 35 | <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br> |
---|
| 36 | Always 1. |
---|
| 37 | |
---|
| 38 | <p><b>Parameters</b><br> |
---|
| 39 | <table> |
---|
| 40 | <tr><td valign=top><i>base.</i><tt>scaled-fitness-floor</tt><br> |
---|
| 41 | <font size=-1>double = some small number (defaults to 0.1)</font></td> |
---|
| 42 | <td valign=top>(The sigma scaling formula sometimes returns negative values. This is unacceptable for fitness proportionate style selection so we must substitute |
---|
| 43 | the fitnessFloor (some value >= 0) for the sigma scaled fitness when that sigma scaled fitness <= fitnessFloor.)</td></tr> |
---|
| 44 | </table> |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | <p><b>Default Base</b><br> |
---|
| 48 | select.sigma-scaling |
---|
| 49 | |
---|
| 50 | * |
---|
| 51 | * @author Jack Compton |
---|
| 52 | * @version 1.0 |
---|
| 53 | */ |
---|
| 54 | |
---|
| 55 | public class SigmaScalingSelection extends FitProportionateSelection |
---|
| 56 | { |
---|
| 57 | /** Default base */ |
---|
| 58 | public static final String P_SIGMA_SCALING = "sigma-scaling"; |
---|
| 59 | |
---|
| 60 | /** Scaled fitness floor */ |
---|
| 61 | // Used as a cut-off point when negative valued scaled fitnesses are encountered (negative fitness values are not compatible with fitness proportionate style selection methods) |
---|
| 62 | public static final String P_SCALED_FITNESS_FLOOR = "scaled-fitness-floor"; |
---|
| 63 | |
---|
| 64 | /** Floor for sigma scaled fitnesses **/ |
---|
| 65 | private float fitnessFloor; |
---|
| 66 | |
---|
| 67 | public Parameter defaultBase() |
---|
| 68 | { |
---|
| 69 | return SelectDefaults.base().push(P_SIGMA_SCALING); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public void setup(final EvolutionState state, final Parameter base) |
---|
| 73 | { |
---|
| 74 | super.setup(state,base); |
---|
| 75 | |
---|
| 76 | Parameter def = defaultBase(); |
---|
| 77 | |
---|
| 78 | fitnessFloor = state.parameters.getFloatWithDefault(base.push(P_SCALED_FITNESS_FLOOR),def.push(P_SCALED_FITNESS_FLOOR),0.1); // default scaled fitness floor of 0.1 according to Tanese (1989) |
---|
| 79 | |
---|
| 80 | if (fitnessFloor < 0) |
---|
| 81 | { |
---|
| 82 | //Hey! you gotta cool! Set your cooling rate to a positive value! |
---|
| 83 | state.output.fatal("The scaled-fitness-floor must be a non-negative value.",base.push(P_SCALED_FITNESS_FLOOR),def.push(P_SCALED_FITNESS_FLOOR)); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | // completely override FitProportionateSelection.prepareToProduce |
---|
| 88 | public void prepareToProduce(final EvolutionState s, |
---|
| 89 | final int subpopulation, |
---|
| 90 | final int thread) |
---|
| 91 | { |
---|
| 92 | // load fitnesses |
---|
| 93 | fitnesses = new float[s.population.subpops[subpopulation].individuals.length]; |
---|
| 94 | |
---|
| 95 | double sigma; |
---|
| 96 | double meanFitness; |
---|
| 97 | double meanSum = 0; |
---|
| 98 | double squaredDeviationsSum = 0; |
---|
| 99 | |
---|
| 100 | for(int x=0;x<fitnesses.length;x++) |
---|
| 101 | { |
---|
| 102 | fitnesses[x] = ((Individual)(s.population.subpops[subpopulation].individuals[x])).fitness.fitness(); |
---|
| 103 | if (fitnesses[x] < 0) // uh oh |
---|
| 104 | s.output.fatal("Discovered a negative fitness value. SigmaScalingSelection requires that all fitness values be non-negative(offending subpopulation #" + subpopulation + ")"); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // Calculate meanFitness |
---|
| 108 | for(int x=0;x<fitnesses.length;x++) |
---|
| 109 | { |
---|
| 110 | meanSum = meanSum + fitnesses[x]; |
---|
| 111 | } |
---|
| 112 | meanFitness = meanSum/fitnesses.length; |
---|
| 113 | |
---|
| 114 | // Calculate sum of squared deviations |
---|
| 115 | for(int x=0;x<fitnesses.length;x++) |
---|
| 116 | { |
---|
| 117 | squaredDeviationsSum = squaredDeviationsSum + Math.pow(fitnesses[x]-meanFitness,2); |
---|
| 118 | } |
---|
| 119 | sigma = Math.sqrt(squaredDeviationsSum/(fitnesses.length-1)); |
---|
| 120 | |
---|
| 121 | // Fill fitnesses[] with sigma scaled fitness values |
---|
| 122 | for(int x=0;x<fitnesses.length;x++) |
---|
| 123 | { |
---|
| 124 | fitnesses[x] = (float)sigmaScaledValue(fitnesses[x], meanFitness, sigma, s); // adjust the fitness proportion according to sigma scaling. |
---|
| 125 | |
---|
| 126 | // Sigma scaling formula can return negative values, this is unacceptable for fitness proportionate style selection... |
---|
| 127 | // so we must substitute the fitnessFloor (some value >= 0) when a sigma scaled fitness <= fitnessFloor is encountered. |
---|
| 128 | if (fitnesses[x] < fitnessFloor) |
---|
| 129 | fitnesses[x] = fitnessFloor; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // organize the distribution. All zeros in fitness is fine |
---|
| 133 | RandomChoice.organizeDistribution(fitnesses, true); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | private double sigmaScaledValue(double fitness, double meanFitness, double sigma, final EvolutionState s) |
---|
| 137 | { |
---|
| 138 | if (sigma != 0) |
---|
| 139 | return 1+(fitness-meanFitness)/(2*sigma); |
---|
| 140 | return 1.0; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | } |
---|