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 java.io.*; |
---|
11 | import ec.util.*; |
---|
12 | import ec.eval.*; |
---|
13 | |
---|
14 | /* |
---|
15 | * SimpleShortStatistics.java |
---|
16 | * |
---|
17 | * Created: Tue Jun 19 15:08:29 EDT 2001 |
---|
18 | * By: Sean Luke |
---|
19 | */ |
---|
20 | |
---|
21 | /** |
---|
22 | * A Simple-style statistics generator, intended to be easily parseable with |
---|
23 | * awk or other Unix tools. Prints fitness information, |
---|
24 | * one generation (or pseudo-generation) per line. |
---|
25 | * If gather-full is true, then timing information, number of nodes |
---|
26 | * and depths of trees, etc. are also given. No final statistics information |
---|
27 | * is given. |
---|
28 | * |
---|
29 | * <p> Each line represents a single generation. |
---|
30 | * The first items on a line are always: |
---|
31 | <ul> |
---|
32 | <li> The generation number |
---|
33 | <li> (if gather-full) how long initialization took in milliseconds, or how long the previous generation took to breed to form this generation |
---|
34 | <li> (if gather-full) how many bytes initialization took, or how how many bytes the previous generation took to breed to form this generation. This utilization is an approximation only, made by the Java system, and does not take into consideration the possibility of garbage collection (which might make the number negative). |
---|
35 | <li> (if gather-full) How long evaluation took in milliseconds this generation |
---|
36 | <li> (if gather-full) how many bytes evaluation took this generation. This utilization is an approximation only, made by the Java system, and does not take into consideration the possibility of garbage collection (which might make the number negative). |
---|
37 | </ul> |
---|
38 | |
---|
39 | <p>Then the following items appear, per subpopulation: |
---|
40 | <ul> |
---|
41 | <li> (if gather-full) The average size of an individual this generation |
---|
42 | <li> (if gather-full) The average size of an individual so far in the run |
---|
43 | <li> The mean fitness of the subpopulation this generation |
---|
44 | <li> The best fitness of the subpopulation this generation |
---|
45 | <li> The best fitness of the subpopulation so far in the run |
---|
46 | <li> (if gather-full) The size of the best individual this generation |
---|
47 | <li> (if gather-full) The size of the best individual so far in the run |
---|
48 | </ul> |
---|
49 | |
---|
50 | Compressed files will be overridden on restart from checkpoint; uncompressed files will be |
---|
51 | appended on restart. |
---|
52 | |
---|
53 | <p><b>Parameters</b><br> |
---|
54 | <table> |
---|
55 | <tr><td valign=top><i>base.</i><tt>gzip</tt><br> |
---|
56 | <font size=-1>boolean</font></td> |
---|
57 | <td valign=top>(whether or not to compress the file (.gz suffix added)</td></tr> |
---|
58 | <tr><td valign=top><i>base.</i><tt>file</tt><br> |
---|
59 | <font size=-1>String (a filename), or nonexistant (signifies stdout)</font></td> |
---|
60 | <td valign=top>(the log for statistics)</td></tr> |
---|
61 | <tr><td valign=top><i>base</i>.<tt>gather-full</tt><br> |
---|
62 | <font size=-1>bool = <tt>true</tt> or <tt>false</tt> (default)</font></td> |
---|
63 | <td valign=top>(should we full statistics on individuals (will run slower, though the slowness is due to off-line processing that won't mess up timings)</td></tr> |
---|
64 | </table> |
---|
65 | * @author Sean Luke |
---|
66 | * @version 1.0 |
---|
67 | */ |
---|
68 | |
---|
69 | public class SimpleShortStatistics extends Statistics // implements ProvidesBestSoFar |
---|
70 | { |
---|
71 | public Individual[] getBestSoFar() { return best_of_run; } |
---|
72 | |
---|
73 | /** log file parameter */ |
---|
74 | public static final String P_STATISTICS_FILE = "file"; |
---|
75 | |
---|
76 | /** The Statistics' log */ |
---|
77 | public int statisticslog; |
---|
78 | |
---|
79 | /** compress? */ |
---|
80 | public static final String P_COMPRESS = "gzip"; |
---|
81 | |
---|
82 | public static final String P_FULL = "gather-full"; |
---|
83 | |
---|
84 | public boolean doFull; |
---|
85 | |
---|
86 | public Individual[] best_of_run; |
---|
87 | public long lengths[]; |
---|
88 | |
---|
89 | // timings |
---|
90 | public long lastTime; |
---|
91 | |
---|
92 | // usage |
---|
93 | public long lastUsage; |
---|
94 | |
---|
95 | public SimpleShortStatistics() { /*best_of_run = null;*/ statisticslog = 0; /* stdout */ } |
---|
96 | |
---|
97 | public void setup(final EvolutionState state, final Parameter base) |
---|
98 | { |
---|
99 | super.setup(state,base); |
---|
100 | File statisticsFile = state.parameters.getFile( |
---|
101 | base.push(P_STATISTICS_FILE),null); |
---|
102 | |
---|
103 | if (statisticsFile!=null) try |
---|
104 | { |
---|
105 | statisticslog = state.output.addLog(statisticsFile, |
---|
106 | !state.parameters.getBoolean(base.push(P_COMPRESS),null,false), |
---|
107 | state.parameters.getBoolean(base.push(P_COMPRESS),null,false)); |
---|
108 | } |
---|
109 | catch (IOException i) |
---|
110 | { |
---|
111 | state.output.fatal("An IOException occurred while trying to create the log " + statisticsFile + ":\n" + i); |
---|
112 | } |
---|
113 | doFull = state.parameters.getBoolean(base.push(P_FULL),null,false); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | public void preInitializationStatistics(final EvolutionState state) |
---|
118 | { |
---|
119 | super.preInitializationStatistics(state); |
---|
120 | |
---|
121 | if (doFull) |
---|
122 | { |
---|
123 | Runtime r = Runtime.getRuntime(); |
---|
124 | lastTime = System.currentTimeMillis(); |
---|
125 | lastUsage = r.totalMemory() - r.freeMemory(); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public void postInitializationStatistics(final EvolutionState state) |
---|
130 | { |
---|
131 | super.postInitializationStatistics(state); |
---|
132 | |
---|
133 | // set up our best_of_run array -- can't do this in setup, because |
---|
134 | // we don't know if the number of subpopulations has been determined yet |
---|
135 | best_of_run = new Individual[state.population.subpops.length]; |
---|
136 | |
---|
137 | // print out our generation number |
---|
138 | state.output.print("0 ", statisticslog); |
---|
139 | |
---|
140 | // gather timings |
---|
141 | if (doFull) |
---|
142 | { |
---|
143 | lengths = new long[state.population.subpops.length]; |
---|
144 | for(int x=0;x<lengths.length;x++) lengths[x] = 0; |
---|
145 | Runtime r = Runtime.getRuntime(); |
---|
146 | long curU = r.totalMemory() - r.freeMemory(); |
---|
147 | state.output.print("" + (System.currentTimeMillis()-lastTime) + " ", statisticslog); |
---|
148 | state.output.print("" + (curU-lastUsage) + " ", statisticslog); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | public void preBreedingStatistics(final EvolutionState state) |
---|
153 | { |
---|
154 | super.preBreedingStatistics(state); |
---|
155 | if (doFull) |
---|
156 | { |
---|
157 | Runtime r = Runtime.getRuntime(); |
---|
158 | lastTime = System.currentTimeMillis(); |
---|
159 | lastUsage = r.totalMemory() - r.freeMemory(); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | public void postBreedingStatistics(final EvolutionState state) |
---|
164 | { |
---|
165 | super.postBreedingStatistics(state); |
---|
166 | state.output.print("" + (state.generation + 1) + " ", statisticslog); // 1 because we're putting the breeding info on the same line as the generation it *produces*, and the generation number is increased *after* breeding occurs, and statistics for it |
---|
167 | |
---|
168 | // gather timings |
---|
169 | if (doFull) |
---|
170 | { |
---|
171 | Runtime r = Runtime.getRuntime(); |
---|
172 | long curU = r.totalMemory() - r.freeMemory(); |
---|
173 | state.output.print("" + (System.currentTimeMillis()-lastTime) + " ", statisticslog); |
---|
174 | state.output.print("" + (curU-lastUsage) + " ", statisticslog); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | public void preEvaluationStatistics(final EvolutionState state) |
---|
179 | { |
---|
180 | super.preEvaluationStatistics(state); |
---|
181 | if (doFull) |
---|
182 | { |
---|
183 | Runtime r = Runtime.getRuntime(); |
---|
184 | lastTime = System.currentTimeMillis(); |
---|
185 | lastUsage = r.totalMemory() - r.freeMemory(); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | /** Prints out the statistics, but does not end with a println -- |
---|
190 | this lets overriding methods print additional statistics on the same line */ |
---|
191 | protected void _postEvaluationStatistics(final EvolutionState state) |
---|
192 | { |
---|
193 | // gather timings |
---|
194 | if (doFull) |
---|
195 | { |
---|
196 | Runtime r = Runtime.getRuntime(); |
---|
197 | long curU = r.totalMemory() - r.freeMemory(); |
---|
198 | state.output.print("" + (System.currentTimeMillis()-lastTime) + " ", statisticslog); |
---|
199 | state.output.print("" + (curU-lastUsage) + " ", statisticslog); |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | long lengthPerGen = 0; |
---|
204 | Individual[] best_i = new Individual[state.population.subpops.length]; |
---|
205 | for(int x=0;x<state.population.subpops.length;x++) |
---|
206 | { |
---|
207 | if (doFull) |
---|
208 | { |
---|
209 | lengthPerGen = 0; |
---|
210 | for(int y=0;y<state.population.subpops[x].individuals.length;y++) |
---|
211 | { |
---|
212 | long size = state.population.subpops[x].individuals[y].size(); |
---|
213 | lengthPerGen += size; |
---|
214 | lengths[x] += size; |
---|
215 | } |
---|
216 | |
---|
217 | state.output.print("" + ((double)lengthPerGen)/state.population.subpops[x].individuals.length + " ", statisticslog); |
---|
218 | |
---|
219 | state.output.print("" + ((double)lengths[x])/(state.population.subpops[x].individuals.length * (state.generation + 1)) + " ", statisticslog); |
---|
220 | } |
---|
221 | |
---|
222 | // fitness information |
---|
223 | double meanFitness = 0.0; |
---|
224 | |
---|
225 | for(int y=0;y<state.population.subpops[x].individuals.length;y++) |
---|
226 | { |
---|
227 | // best individual |
---|
228 | if (best_i[x]==null || |
---|
229 | state.population.subpops[x].individuals[y].fitness.betterThan(best_i[x].fitness)) |
---|
230 | best_i[x] = state.population.subpops[x].individuals[y]; |
---|
231 | |
---|
232 | // mean fitness for population |
---|
233 | meanFitness += state.population.subpops[x].individuals[y].fitness.fitness(); |
---|
234 | } |
---|
235 | |
---|
236 | // compute fitness stats |
---|
237 | meanFitness /= state.population.subpops[x].individuals.length; |
---|
238 | state.output.print("" + meanFitness + " " + best_i[x].fitness.fitness() + " ", |
---|
239 | statisticslog); |
---|
240 | |
---|
241 | // now test to see if it's the new best_of_run[x] |
---|
242 | if (best_of_run[x]==null || best_i[x].fitness.betterThan(best_of_run[x].fitness)) |
---|
243 | best_of_run[x] = (Individual)(best_i[x].clone()); |
---|
244 | |
---|
245 | state.output.print("" + best_of_run[x].fitness.fitness() + " ", |
---|
246 | statisticslog); |
---|
247 | |
---|
248 | if( doFull ) |
---|
249 | { |
---|
250 | state.output.print("" + (double)(best_i[x].size()) + " " + |
---|
251 | (double)(best_of_run[x].size()) + " ", |
---|
252 | statisticslog); |
---|
253 | } |
---|
254 | } |
---|
255 | // we're done! |
---|
256 | } |
---|
257 | |
---|
258 | public void postEvaluationStatistics(final EvolutionState state) |
---|
259 | { |
---|
260 | super.postEvaluationStatistics(state); |
---|
261 | _postEvaluationStatistics(state); |
---|
262 | state.output.println("", statisticslog); |
---|
263 | } |
---|
264 | |
---|
265 | } |
---|