1 | package ec.evolve; |
---|
2 | |
---|
3 | import ec.*; |
---|
4 | import ec.steadystate.*; |
---|
5 | import ec.util.*; |
---|
6 | import java.io.*; |
---|
7 | |
---|
8 | /** |
---|
9 | * A special Statistics class which performs random restarts on the population, |
---|
10 | * effectively reininitializing the population and starting over again. |
---|
11 | * RandomRestarts has two ways of determining when to perform a restart. If |
---|
12 | * the restart type is "fixed", then the restart will occur precisely when |
---|
13 | * the generation is a multiple of restart-upper-bound, minus one. (That's |
---|
14 | * hardly random, of course). If the restart type is "random", then at the |
---|
15 | * beginning of the run, and after every restart, a new restart is chosen |
---|
16 | * randomly from one to restart-upper-bound. |
---|
17 | * |
---|
18 | * <p>This class is compatible with populations which load from files -- it |
---|
19 | * temporarily disables the load-from-file feature when telling the population |
---|
20 | * to populate itself again, forcing the population to do so by creating random |
---|
21 | * individuals. |
---|
22 | * |
---|
23 | * @author James O'Beirne |
---|
24 | |
---|
25 | <p><b>Parameters</b><br> |
---|
26 | <table> |
---|
27 | <tr><td valign=top><i>base</i>.<tt>restart-type</tt><br> |
---|
28 | <font size=-1>random or fixed</font></td> |
---|
29 | <td valign=top>Either initiates clock at a random value or a fixed one.</td></tr> |
---|
30 | <tr><td valign=top><i>base</i>.<tt>restart-upper-bound</tt><br> |
---|
31 | <font size=-1>1 < int < \inf</font></td> |
---|
32 | <td valign=top>Maximum time clock can initiate with.</td></tr> |
---|
33 | </table> |
---|
34 | */ |
---|
35 | |
---|
36 | public class RandomRestarts extends Statistics implements SteadyStateStatisticsForm |
---|
37 | { |
---|
38 | /** Two options available here: "fixed" and "random"; "fixed" |
---|
39 | * will initate the restart timer at the value specified for |
---|
40 | * <i>restart-upper-bound</i>, "random" will initiate the restart |
---|
41 | * timer somewhere below the value specified for |
---|
42 | * <i>restart-upper-bound</i> */ |
---|
43 | public static final String P_RESTART_TYPE = "restart-type"; |
---|
44 | |
---|
45 | /** This is the highest value at which the "ticking" |
---|
46 | * restart clock can initiate at. */ |
---|
47 | public static final String P_RESTART_UPPERBOUND = "restart-upper-bound"; |
---|
48 | |
---|
49 | public int countdown; // what we'll use for the "ticking" clock |
---|
50 | public int upperbound; // highest possible value on the clock |
---|
51 | |
---|
52 | String restartType; // are we doing random or fixed? |
---|
53 | |
---|
54 | /** Gets the clock ticking. */ |
---|
55 | public void setup( final EvolutionState state, final Parameter base ) |
---|
56 | { |
---|
57 | super.setup( state, base ); |
---|
58 | |
---|
59 | restartType = state.parameters.getString(base.push(P_RESTART_TYPE), null); |
---|
60 | upperbound = state.parameters.getInt( base.push(P_RESTART_UPPERBOUND), null, 1); |
---|
61 | |
---|
62 | if( upperbound < 1 ) |
---|
63 | state.output.fatal("Parameter either not found or invalid (<1).", base.push(P_RESTART_UPPERBOUND)); |
---|
64 | |
---|
65 | if( !restartType.equals( "random" ) && !restartType.equals( "fixed" ) ) |
---|
66 | state.output.fatal("Parameter must be either 'fixed' or 'random'.", base.push(P_RESTART_TYPE)); |
---|
67 | |
---|
68 | // start counting down |
---|
69 | this.resetClock( state ); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Checks the clock; if it's time to restart, we repopulate the population. |
---|
74 | * Afterwards, we reset the clock. |
---|
75 | * |
---|
76 | * If it's not time yet, the clock goes tick. |
---|
77 | */ |
---|
78 | public void preEvaluationStatistics( final EvolutionState state ) |
---|
79 | { |
---|
80 | super.preEvaluationStatistics(state); |
---|
81 | possiblyRestart(state); |
---|
82 | } |
---|
83 | |
---|
84 | public void generationBoundaryStatistics(final EvolutionState state) |
---|
85 | { |
---|
86 | super.generationBoundaryStatistics(state); |
---|
87 | possiblyRestart(state); |
---|
88 | } |
---|
89 | |
---|
90 | void possiblyRestart(EvolutionState state) |
---|
91 | { |
---|
92 | |
---|
93 | Subpopulation currentSubp; |
---|
94 | File tempFile; |
---|
95 | // time to restart! |
---|
96 | if( countdown == 0 ) |
---|
97 | { |
---|
98 | System.out.println( "Restarting the population!" ); |
---|
99 | // for each subpopulation |
---|
100 | for( int subp = 0; subp < state.population.subpops.length; subp++ ) |
---|
101 | { |
---|
102 | currentSubp = state.population.subpops[subp]; |
---|
103 | tempFile = currentSubp.loadInds; |
---|
104 | // disable loadInds so we generate candidates randomly |
---|
105 | currentSubp.loadInds = null; |
---|
106 | currentSubp.populate( state, 0 ); |
---|
107 | currentSubp.loadInds = tempFile; |
---|
108 | } |
---|
109 | this.resetClock( state ); |
---|
110 | } |
---|
111 | else |
---|
112 | countdown--; |
---|
113 | } |
---|
114 | |
---|
115 | void resetClock( final EvolutionState state ) |
---|
116 | { |
---|
117 | if(restartType.equals( "fixed" )) |
---|
118 | countdown = upperbound; |
---|
119 | else |
---|
120 | // might need to fix random index to support multithreading |
---|
121 | countdown = state.random[0].nextInt( upperbound + 1 ); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|