Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/gp/koza/KozaShortStatistics.java @ 6152

Last change on this file since 6152 was 6152, checked in by bfarka, 13 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 14.5 KB
Line 
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
8package ec.gp.koza;
9import ec.*;
10import ec.gp.*;
11import java.io.*;
12import ec.util.*;
13
14/*
15 * KozaShortStatistics.java
16 *
17 * Created: Fri Nov  5 16:03:44 1999
18 * By: Sean Luke
19 */
20
21/**
22 * A Koza-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 number of nodes used per individual this generation
42 <li> (if gather-full) [a|b|c...], representing the average number of nodes used in tree <i>a</i>, <i>b</i>, etc. of individuals this generation
43 <li> (if gather-full) The average number of nodes used per individual so far in the run
44 <li> (if gather-full) The average depth of any tree per individual this generation
45 <li> (if gather-full) [a|b|c...], representing the average depth of tree <i>a</i>, <i>b</i>, etc. of individuals this generation
46 <li> (if gather-full) The average depth of any tree per individual so far in the run
47 <li> The mean standardized fitness of the subpopulation this generation
48 <li> The mean adjusted fitness of the subpopulation this generation
49 <li> The mean hits of the subpopulation this generation
50 <li> The best standardized fitness of the subpopulation this generation
51 <li> The best adjusted fitness of the subpopulation this generation
52 <li> The best hits of the subpopulation this generation
53 <li> The best standardized fitness of the subpopulation so far in the run
54 <li> The best adjusted fitness of the subpopulation so far in the run
55 <li> The best hits of the subpopulation so far in the run
56 </ul>
57
58 Compressed files will be overridden on restart from checkpoint; uncompressed files will be
59 appended on restart.
60
61 <p><b>Parameters</b><br>
62 <table>
63 <tr><td valign=top><i>base.</i><tt>gzip</tt><br>
64 <font size=-1>boolean</font></td>
65 <td valign=top>(whether or not to compress the file (.gz suffix added)</td></tr>
66 <tr><td valign=top><i>base.</i><tt>file</tt><br>
67 <font size=-1>String (a filename), or nonexistant (signifies stdout)</font></td>
68 <td valign=top>(the log for statistics)</td></tr>
69 <tr><td valign=top><i>base</i>.<tt>gather-full</tt><br>
70 <font size=-1>bool = <tt>true</tt> or <tt>false</tt> (default)</font></td>
71 <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>
72 </table>
73 * @author Sean Luke
74 * @version 1.0
75 */
76
77public class KozaShortStatistics extends Statistics
78    {
79    public Individual[] getBestSoFar() { return best_of_run; }
80
81    /** compress? */
82    public static final String P_COMPRESS = "gzip";
83
84    public static final String P_FULL = "gather-full";
85
86    public boolean doFull;
87
88    public Individual[] best_of_run;
89    public long totalNodes[];
90    public long totalDepths[];
91
92    // timings
93    public long lastTime;
94   
95    // usage
96    public long lastUsage;
97   
98    /** log file parameter */
99    public static final String P_STATISTICS_FILE = "file";
100
101    /** The Statistics' log */
102    public int statisticslog;
103
104    public KozaShortStatistics() { /*best_of_run = null;*/ statisticslog = 0; /* stdout */ }
105
106
107    public void setup(final EvolutionState state, final Parameter base)
108        {
109        super.setup(state,base);
110        File statisticsFile = state.parameters.getFile(
111            base.push(P_STATISTICS_FILE),null);
112
113        if (statisticsFile!=null) try
114                                      {
115                                      statisticslog = state.output.addLog(statisticsFile,
116                                          !state.parameters.getBoolean(base.push(P_COMPRESS),null,false),
117                                          state.parameters.getBoolean(base.push(P_COMPRESS),null,false));
118                                      }
119            catch (IOException i)
120                {
121                state.output.fatal("An IOException occurred while trying to create the log " + statisticsFile + ":\n" + i);
122                }
123        doFull = state.parameters.getBoolean(base.push(P_FULL),null,false);
124        }
125
126
127    public void preInitializationStatistics(final EvolutionState state)
128        {
129        super.preInitializationStatistics(state);
130
131        if (doFull)
132            {
133            Runtime r = Runtime.getRuntime();
134            lastTime = System.currentTimeMillis();
135            lastUsage = r.totalMemory() - r.freeMemory();
136            }
137        }
138   
139    public void postInitializationStatistics(final EvolutionState state)
140        {
141        super.postInitializationStatistics(state);
142        // set up our best_of_run array -- can't do this in setup, because
143        // we don't know if the number of subpopulations has been determined yet
144        best_of_run = new Individual[state.population.subpops.length];
145       
146        // print out our generation number
147        state.output.print("0 ", statisticslog);
148
149        // gather timings       
150        if (doFull)
151            {
152            totalNodes = new long[state.population.subpops.length];
153            for(int x=0;x<totalNodes.length;x++) totalNodes[x] = 0;
154            totalDepths = new long[state.population.subpops.length];
155            for(int x=0;x<totalDepths.length;x++) totalDepths[x] = 0;
156            Runtime r = Runtime.getRuntime();
157            long curU =  r.totalMemory() - r.freeMemory();         
158            state.output.print("" + (System.currentTimeMillis()-lastTime) + " ",  statisticslog);
159            state.output.print("" + (curU-lastUsage) + " ",  statisticslog);           
160            }
161        }
162
163    public void preBreedingStatistics(final EvolutionState state)
164        {
165        super.preBreedingStatistics(state);
166        if (doFull)
167            {
168            Runtime r = Runtime.getRuntime();
169            lastTime = System.currentTimeMillis();
170            lastUsage = r.totalMemory() - r.freeMemory();
171            }
172        }
173
174    public void postBreedingStatistics(final EvolutionState state)
175        {
176        super.postBreedingStatistics(state);
177        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
178
179        // gather timings
180        if (doFull)
181            {
182            Runtime r = Runtime.getRuntime();
183            long curU =  r.totalMemory() - r.freeMemory();         
184            state.output.print("" + (System.currentTimeMillis()-lastTime) + " ",  statisticslog);
185            state.output.print("" + (curU-lastUsage) + " ",  statisticslog);           
186            }
187        }
188
189    public void preEvaluationStatistics(final EvolutionState state)
190        {
191        super.preEvaluationStatistics(state);
192        if (doFull)
193            {
194            Runtime r = Runtime.getRuntime();
195            lastTime = System.currentTimeMillis();
196            lastUsage = r.totalMemory() - r.freeMemory();
197            }
198        }
199
200    /** Prints out the statistics, but does not end with a println --
201        this lets overriding methods print additional statistics on the same line */
202    protected void _postEvaluationStatistics(final EvolutionState state)
203        {
204        // gather timings
205        if (doFull)
206            {
207            Runtime r = Runtime.getRuntime();
208            long curU =  r.totalMemory() - r.freeMemory();         
209            state.output.print("" + (System.currentTimeMillis()-lastTime) + " ",  statisticslog);
210            state.output.print("" + (curU-lastUsage) + " ",  statisticslog);
211            }
212
213
214        Individual[] best_i = new Individual[state.population.subpops.length];  // quiets compiler complaints
215               
216        for(int x=0;x<state.population.subpops.length;x++)
217            {
218            if (doFull)
219                {
220                long totNodesPerGen = 0;
221                long totDepthPerGen = 0;
222
223                // check to make sure they're the right class
224                if ( !(state.population.subpops[x].species instanceof GPSpecies ))
225                    state.output.fatal("Subpopulation " + x +
226                        " is not of the species form GPSpecies." +
227                        "  Cannot do timing statistics with KozaShortStatistics.");
228               
229                long[] numNodes = new long[((GPIndividual)(state.population.subpops[x].species.i_prototype)).trees.length];
230                long[] numDepth = new long[((GPIndividual)(state.population.subpops[x].species.i_prototype)).trees.length];
231               
232                for(int y=0;y<state.population.subpops[x].individuals.length;y++)
233                    {
234                    GPIndividual i =
235                        (GPIndividual)(state.population.subpops[x].individuals[y]);
236                    for(int z=0;z<i.trees.length;z++)
237                        {
238                        numNodes[z] += i.trees[z].child.numNodes(GPNode.NODESEARCH_ALL);
239                        numDepth[z] += i.trees[z].child.depth();
240                        }
241                    }
242               
243                for(int tr=0;tr<numNodes.length;tr++) totNodesPerGen += numNodes[tr];
244               
245                totalNodes[x] += totNodesPerGen;
246
247
248                state.output.print("" + ((double)totNodesPerGen)/state.population.subpops[x].individuals.length + " [",  statisticslog);
249
250                for(int tr=0;tr<numNodes.length;tr++)
251                    {
252                    if (tr>0) state.output.print("|", statisticslog);
253                    state.output.print(""+((double)numNodes[tr])/state.population.subpops[x].individuals.length, statisticslog);
254                    }
255                state.output.print("] ", statisticslog);
256
257                state.output.print("" + ((double)totalNodes[x])/(state.population.subpops[x].individuals.length * (state.generation + 1)) + " ",
258                    statisticslog);
259
260                for(int tr=0;tr<numDepth.length;tr++) totDepthPerGen += numDepth[tr];
261
262                totalDepths[x] += totDepthPerGen;
263
264                state.output.print("" + ((double)totDepthPerGen)/
265                        (state.population.subpops[x].individuals.length *
266                        numDepth.length)
267                    + " [",  statisticslog);
268
269
270                for(int tr=0;tr<numDepth.length;tr++)
271                    {
272                    if (tr>0) state.output.print("|", statisticslog);
273                    state.output.print(""+((double)numDepth[tr])/state.population.subpops[x].individuals.length, statisticslog);
274                    }
275                state.output.print("] ", statisticslog);
276
277                state.output.print("" + ((double)totalDepths[x])/(state.population.subpops[x].individuals.length * (state.generation + 1)) + " ",
278                    statisticslog);
279                }
280           
281
282           
283            // fitness information
284            float meanStandardized = 0.0f;
285            float meanAdjusted = 0.0f;
286            long hits = 0;
287           
288            if (!(state.population.subpops[x].species.f_prototype instanceof KozaFitness))
289                state.output.fatal("Subpopulation " + x +
290                    " is not of the fitness KozaFitness.  Cannot do timing statistics with KozaStatistics.");
291
292            best_i[x] = null;
293            for(int y=0;y<state.population.subpops[x].individuals.length;y++)
294                {
295                // best individual
296                if (best_i[x]==null ||
297                    state.population.subpops[x].individuals[y].fitness.betterThan(best_i[x].fitness))
298                    best_i[x] = state.population.subpops[x].individuals[y];
299
300                // mean for population
301                meanStandardized += ((KozaFitness)(state.population.subpops[x].individuals[y].fitness)).standardizedFitness();
302                meanAdjusted += ((KozaFitness)(state.population.subpops[x].individuals[y].fitness)).adjustedFitness();
303                hits += ((KozaFitness)(state.population.subpops[x].individuals[y].fitness)).hits;
304                }
305           
306            // compute fitness stats
307            meanStandardized /= state.population.subpops[x].individuals.length;
308            meanAdjusted /= state.population.subpops[x].individuals.length;
309            state.output.print("" + meanStandardized + " " + meanAdjusted + " " +
310                ((double)hits)/state.population.subpops[x].individuals.length + " ",
311                statisticslog);
312            state.output.print("" + ((KozaFitness)(best_i[x].fitness)).standardizedFitness() +
313                " " + ((KozaFitness)(best_i[x].fitness)).adjustedFitness() +
314                " " + ((KozaFitness)(best_i[x].fitness)).hits + " ",
315                statisticslog);
316
317            // now test to see if it's the new best_of_run[x]
318            if (best_of_run[x]==null || best_i[x].fitness.betterThan(best_of_run[x].fitness))
319                best_of_run[x] = best_i[x];
320           
321            state.output.print("" + ((KozaFitness)(best_of_run[x].fitness)).standardizedFitness() +
322                " " + ((KozaFitness)(best_of_run[x].fitness)).adjustedFitness() +
323                " " + ((KozaFitness)(best_of_run[x].fitness)).hits + " ",
324                statisticslog);
325            }
326        // we're done!
327        }
328
329    public void postEvaluationStatistics(final EvolutionState state)
330        {
331        super.postEvaluationStatistics(state);
332        _postEvaluationStatistics(state);
333        state.output.println("", statisticslog);
334        }
335
336    }
Note: See TracBrowser for help on using the repository browser.