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.breed; |
---|
9 | import ec.*; |
---|
10 | import ec.util.*; |
---|
11 | |
---|
12 | /* |
---|
13 | * GenerationSwitchPipeline.java |
---|
14 | * |
---|
15 | * Created: Sat Nov 10 14:10:35 EST 2001 |
---|
16 | * By: Sean Luke |
---|
17 | */ |
---|
18 | |
---|
19 | /** |
---|
20 | * GenerationSwitchPipeline is a simple BreedingPipeline which switches its source depending |
---|
21 | * on the generation. If the generation number is < n, then GenerationSwitchPipeline uses |
---|
22 | * source.0. If the generation number if >= n, then GenerationSwitchPipeline uses source.1. |
---|
23 | |
---|
24 | * <p><b>Important Note:</b> Because GenerationSwitchPipeline gets the generation number |
---|
25 | * from the EvolutionState, and this number is not protected by a mutex, if you create |
---|
26 | * an EvolutionState or Breeder which uses multiple threads that can update the generation |
---|
27 | * number as they like, you could cause a race condition. This doesn't occur with the |
---|
28 | * present EvolutionState objects provided with ECJ, but you should be aware of the |
---|
29 | * possibility. |
---|
30 | |
---|
31 | <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br> |
---|
32 | Defined as the max of its children's responses. |
---|
33 | |
---|
34 | <p><b>Number of Sources</b><br> |
---|
35 | 2 |
---|
36 | |
---|
37 | <p><b>Parameters</b><br> |
---|
38 | <table> |
---|
39 | <tr><td valign=top><i>base</i>.<tt>generate-max</tt><br> |
---|
40 | <font size=-1> bool = <tt>true</tt> (default) or <tt>false</tt></font></td> |
---|
41 | <td valign=top>(Each time produce(...) is called, should the GenerationSwitchPipeline |
---|
42 | force all its sources to produce exactly the same number of individuals as the largest |
---|
43 | typical number of individuals produced by any source in the group?)</td></tr> |
---|
44 | <tr><td valign=top><i>base</i>.<tt>switch-at</tt><br> |
---|
45 | <font size=-1> int >= 0</tt></font></td> |
---|
46 | <td valign=top>(The generation we will switch at)</td></tr> |
---|
47 | </table> |
---|
48 | |
---|
49 | <p><b>Default Base</b><br> |
---|
50 | breed.generation-switch |
---|
51 | |
---|
52 | * |
---|
53 | * @author Sean Luke |
---|
54 | * @version 1.0 |
---|
55 | */ |
---|
56 | |
---|
57 | public class GenerationSwitchPipeline extends BreedingPipeline |
---|
58 | { |
---|
59 | public static final String P_SWITCHAT = "switch-at"; |
---|
60 | public static final String P_MULTIBREED = "generation-switch"; |
---|
61 | public static final String P_GEN_MAX = "generate-max"; |
---|
62 | public static final int NUM_SOURCES = 2; |
---|
63 | |
---|
64 | public int maxGeneratable; |
---|
65 | public boolean generateMax; |
---|
66 | public int generationSwitch; |
---|
67 | |
---|
68 | public Parameter defaultBase() |
---|
69 | { |
---|
70 | return BreedDefaults.base().push(P_MULTIBREED); |
---|
71 | } |
---|
72 | |
---|
73 | public int numSources() { return NUM_SOURCES; } |
---|
74 | |
---|
75 | public void setup(final EvolutionState state, final Parameter base) |
---|
76 | { |
---|
77 | super.setup(state,base); |
---|
78 | |
---|
79 | Parameter def = defaultBase(); |
---|
80 | |
---|
81 | state.output.exitIfErrors(); |
---|
82 | |
---|
83 | generationSwitch = state.parameters.getInt(base.push(P_SWITCHAT),def.push(P_SWITCHAT),0); |
---|
84 | if (generationSwitch < 0) |
---|
85 | state.output.fatal("GenerationSwitchPipeline must have a switch-at >= 0", |
---|
86 | base.push(P_SWITCHAT),def.push(P_SWITCHAT)); |
---|
87 | |
---|
88 | generateMax = state.parameters.getBoolean(base.push(P_GEN_MAX),def.push(P_GEN_MAX),true); |
---|
89 | maxGeneratable=0; // indicates that I don't know what it is yet. |
---|
90 | |
---|
91 | // declare that likelihood isn't used |
---|
92 | if (likelihood < 1.0f) |
---|
93 | state.output.warning("GenerationSwitchPipeline does not respond to the 'likelihood' parameter.", |
---|
94 | base.push(P_LIKELIHOOD), def.push(P_LIKELIHOOD)); |
---|
95 | } |
---|
96 | |
---|
97 | /** Returns the max of typicalIndsProduced() of all its children */ |
---|
98 | public int typicalIndsProduced() |
---|
99 | { |
---|
100 | if (maxGeneratable==0) // not determined yet |
---|
101 | maxGeneratable = maxChildProduction(); |
---|
102 | return maxGeneratable; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | public int produce(final int min, |
---|
107 | final int max, |
---|
108 | final int start, |
---|
109 | final int subpopulation, |
---|
110 | final Individual[] inds, |
---|
111 | final EvolutionState state, |
---|
112 | final int thread) |
---|
113 | |
---|
114 | { |
---|
115 | BreedingSource s = (state.generation < generationSwitch ? |
---|
116 | sources[0] : sources[1] ); |
---|
117 | int total; |
---|
118 | |
---|
119 | if (generateMax) |
---|
120 | { |
---|
121 | if (maxGeneratable==0) |
---|
122 | maxGeneratable = maxChildProduction(); |
---|
123 | int n = maxGeneratable; |
---|
124 | if (n < min) n = min; |
---|
125 | if (n > max) n = max; |
---|
126 | |
---|
127 | total = s.produce( |
---|
128 | n,n,start,subpopulation,inds,state,thread); |
---|
129 | } |
---|
130 | else |
---|
131 | { |
---|
132 | total = s.produce( |
---|
133 | min,max,start,subpopulation,inds,state,thread); |
---|
134 | } |
---|
135 | |
---|
136 | // clone if necessary |
---|
137 | if (s instanceof SelectionMethod) |
---|
138 | for(int q=start; q < total+start; q++) |
---|
139 | inds[q] = (Individual)(inds[q].clone()); |
---|
140 | |
---|
141 | return total; |
---|
142 | } |
---|
143 | } |
---|