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.simple; |
---|
9 | import ec.*; |
---|
10 | import ec.util.Checkpoint; |
---|
11 | |
---|
12 | /* |
---|
13 | * SimpleEvolutionState.java |
---|
14 | * |
---|
15 | * Created: Tue Aug 10 22:14:46 1999 |
---|
16 | * By: Sean Luke |
---|
17 | */ |
---|
18 | |
---|
19 | /** |
---|
20 | * A SimpleEvolutionState is an EvolutionState which implements a simple form |
---|
21 | * of generational evolution. |
---|
22 | * |
---|
23 | * <p>First, all the individuals in the population are created. |
---|
24 | * <b>(A)</b>Then all individuals in the population are evaluated. |
---|
25 | * Then the population is replaced in its entirety with a new population |
---|
26 | * of individuals bred from the old population. Goto <b>(A)</b>. |
---|
27 | * |
---|
28 | * <p>Evolution stops when an ideal individual is found (if quitOnRunComplete |
---|
29 | * is set to true), or when the number of generations (loops of <b>(A)</b>) |
---|
30 | * exceeds the parameter value numGenerations. Each generation the system |
---|
31 | * will perform garbage collection and checkpointing, if the appropriate |
---|
32 | * parameters were set. |
---|
33 | * |
---|
34 | * <p>This approach can be readily used for |
---|
35 | * most applications of Genetic Algorithms and Genetic Programming. |
---|
36 | * |
---|
37 | * @author Sean Luke |
---|
38 | * @version 1.0 |
---|
39 | */ |
---|
40 | |
---|
41 | public class SimpleEvolutionState extends EvolutionState |
---|
42 | { |
---|
43 | |
---|
44 | /** |
---|
45 | * |
---|
46 | */ |
---|
47 | public void startFresh() |
---|
48 | { |
---|
49 | output.message("Setting up"); |
---|
50 | setup(this,null); // a garbage Parameter |
---|
51 | |
---|
52 | // POPULATION INITIALIZATION |
---|
53 | output.message("Initializing Generation 0"); |
---|
54 | statistics.preInitializationStatistics(this); |
---|
55 | population = initializer.initialPopulation(this, 0); // unthreaded |
---|
56 | statistics.postInitializationStatistics(this); |
---|
57 | |
---|
58 | // INITIALIZE CONTACTS -- done after initialization to allow |
---|
59 | // a hook for the user to do things in Initializer before |
---|
60 | // an attempt is made to connect to island models etc. |
---|
61 | exchanger.initializeContacts(this); |
---|
62 | evaluator.initializeContacts(this); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * @return |
---|
67 | * @throws InternalError |
---|
68 | */ |
---|
69 | public int evolve() |
---|
70 | { |
---|
71 | if (generation > 0) |
---|
72 | output.message("Generation " + generation); |
---|
73 | |
---|
74 | // EVALUATION |
---|
75 | statistics.preEvaluationStatistics(this); |
---|
76 | evaluator.evaluatePopulation(this); |
---|
77 | statistics.postEvaluationStatistics(this); |
---|
78 | |
---|
79 | // SHOULD WE QUIT? |
---|
80 | if (evaluator.runComplete(this) && quitOnRunComplete) |
---|
81 | { |
---|
82 | output.message("Found Ideal Individual"); |
---|
83 | return R_SUCCESS; |
---|
84 | } |
---|
85 | |
---|
86 | // SHOULD WE QUIT? |
---|
87 | if (generation == numGenerations-1) |
---|
88 | { |
---|
89 | return R_FAILURE; |
---|
90 | } |
---|
91 | |
---|
92 | // PRE-BREEDING EXCHANGING |
---|
93 | statistics.prePreBreedingExchangeStatistics(this); |
---|
94 | population = exchanger.preBreedingExchangePopulation(this); |
---|
95 | statistics.postPreBreedingExchangeStatistics(this); |
---|
96 | |
---|
97 | String exchangerWantsToShutdown = exchanger.runComplete(this); |
---|
98 | if (exchangerWantsToShutdown!=null) |
---|
99 | { |
---|
100 | output.message(exchangerWantsToShutdown); |
---|
101 | /* |
---|
102 | * Don't really know what to return here. The only place I could |
---|
103 | * find where runComplete ever returns non-null is |
---|
104 | * IslandExchange. However, that can return non-null whether or |
---|
105 | * not the ideal individual was found (for example, if there was |
---|
106 | * a communication error with the server). |
---|
107 | * |
---|
108 | * Since the original version of this code didn't care, and the |
---|
109 | * result was initialized to R_SUCCESS before the while loop, I'm |
---|
110 | * just going to return R_SUCCESS here. |
---|
111 | */ |
---|
112 | |
---|
113 | return R_SUCCESS; |
---|
114 | } |
---|
115 | |
---|
116 | // BREEDING |
---|
117 | statistics.preBreedingStatistics(this); |
---|
118 | |
---|
119 | population = breeder.breedPopulation(this); |
---|
120 | |
---|
121 | // POST-BREEDING EXCHANGING |
---|
122 | statistics.postBreedingStatistics(this); |
---|
123 | |
---|
124 | // POST-BREEDING EXCHANGING |
---|
125 | statistics.prePostBreedingExchangeStatistics(this); |
---|
126 | population = exchanger.postBreedingExchangePopulation(this); |
---|
127 | statistics.postPostBreedingExchangeStatistics(this); |
---|
128 | |
---|
129 | // INCREMENT GENERATION AND CHECKPOINT |
---|
130 | generation++; |
---|
131 | if (checkpoint && generation%checkpointModulo == 0) |
---|
132 | { |
---|
133 | output.message("Checkpointing"); |
---|
134 | statistics.preCheckpointStatistics(this); |
---|
135 | Checkpoint.setCheckpoint(this); |
---|
136 | statistics.postCheckpointStatistics(this); |
---|
137 | } |
---|
138 | |
---|
139 | return R_NOTDONE; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * @param result |
---|
144 | */ |
---|
145 | public void finish(int result) |
---|
146 | { |
---|
147 | //Output.message("Finishing"); |
---|
148 | /* finish up -- we completed. */ |
---|
149 | statistics.finalStatistics(this,result); |
---|
150 | finisher.finishPopulation(this,result); |
---|
151 | exchanger.closeContacts(this,result); |
---|
152 | evaluator.closeContacts(this,result); |
---|
153 | } |
---|
154 | |
---|
155 | } |
---|