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 | * FitProportionateSelection.java |
---|
14 | * |
---|
15 | * Created: Thu Feb 10 16:31:24 2000 |
---|
16 | * By: Sean Luke |
---|
17 | */ |
---|
18 | |
---|
19 | /** |
---|
20 | * Picks individuals in a population in direct proportion to their |
---|
21 | * fitnesses as returned by their fitness() methods. This is expensive to |
---|
22 | * set up and bring down, so it's not appropriate for steady-state evolution. |
---|
23 | * If you're not familiar with the relative advantages of |
---|
24 | * selection methods and just want a good one, |
---|
25 | * use TournamentSelection instead. Not appropriate for |
---|
26 | * multiobjective fitnesses. |
---|
27 | * |
---|
28 | * <p><b><font color=red> |
---|
29 | * Note: Fitnesses must be non-negative. 0 is assumed to be the worst fitness. |
---|
30 | * </font></b> |
---|
31 | |
---|
32 | <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br> |
---|
33 | Always 1. |
---|
34 | |
---|
35 | <p><b>Default Base</b><br> |
---|
36 | select.fitness-proportionate |
---|
37 | |
---|
38 | * |
---|
39 | * @author Sean Luke |
---|
40 | * @version 1.0 |
---|
41 | */ |
---|
42 | |
---|
43 | public class FitProportionateSelection extends SelectionMethod |
---|
44 | { |
---|
45 | /** Default base */ |
---|
46 | public static final String P_FITNESSPROPORTIONATE = "fitness-proportionate"; |
---|
47 | /** Normalized, totalized fitnesses for the population */ |
---|
48 | public float[] fitnesses; |
---|
49 | |
---|
50 | public Parameter defaultBase() |
---|
51 | { |
---|
52 | return SelectDefaults.base().push(P_FITNESSPROPORTIONATE); |
---|
53 | } |
---|
54 | |
---|
55 | // don't need clone etc. |
---|
56 | |
---|
57 | public void prepareToProduce(final EvolutionState s, |
---|
58 | final int subpopulation, |
---|
59 | final int thread) |
---|
60 | { |
---|
61 | // load fitnesses |
---|
62 | fitnesses = new float[s.population.subpops[subpopulation].individuals.length]; |
---|
63 | for(int x=0;x<fitnesses.length;x++) |
---|
64 | { |
---|
65 | fitnesses[x] = ((Individual)(s.population.subpops[subpopulation].individuals[x])).fitness.fitness(); |
---|
66 | if (fitnesses[x] < 0) // uh oh |
---|
67 | s.output.fatal("Discovered a negative fitness value. FitProportionateSelection requires that all fitness values be non-negative(offending subpopulation #" + subpopulation + ")"); |
---|
68 | } |
---|
69 | |
---|
70 | // organize the distribution. All zeros in fitness is fine |
---|
71 | RandomChoice.organizeDistribution(fitnesses, true); |
---|
72 | } |
---|
73 | |
---|
74 | public int produce(final int subpopulation, |
---|
75 | final EvolutionState state, |
---|
76 | final int thread) |
---|
77 | { |
---|
78 | // Pick and return an individual from the population |
---|
79 | return RandomChoice.pickFromDistribution( |
---|
80 | fitnesses,state.random[thread].nextFloat()); |
---|
81 | } |
---|
82 | |
---|
83 | public void finishProducing(final EvolutionState s, |
---|
84 | final int subpopulation, |
---|
85 | final int thread) |
---|
86 | { |
---|
87 | // release the distributions so we can quickly |
---|
88 | // garbage-collect them if necessary |
---|
89 | fitnesses = null; |
---|
90 | } |
---|
91 | } |
---|