Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/simple/SimpleStatistics.java @ 11255

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

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

File size: 5.9 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.simple;
9import ec.*;
10import ec.steadystate.*;
11import java.io.IOException;
12import ec.util.*;
13import java.io.File;
14
15/*
16 * SimpleStatistics.java
17 *
18 * Created: Tue Aug 10 21:10:48 1999
19 * By: Sean Luke
20 */
21
22/**
23 * A basic Statistics class suitable for simple problem applications.
24 *
25 * SimpleStatistics prints out the best individual, per subpopulation,
26 * each generation.  At the end of a run, it also prints out the best
27 * individual of the run.  SimpleStatistics outputs this data to a log
28 * which may either be a provided file or stdout.  Compressed files will
29 * be overridden on restart from checkpoint; uncompressed files will be
30 * appended on restart.
31 *
32 * <p>SimpleStatistics implements a simple version of steady-state statistics:
33 * if it quits before a generation boundary,
34 * it will include the best individual discovered, even if the individual was discovered
35 * after the last boundary.  This is done by using individualsEvaluatedStatistics(...)
36 * to update best-individual-of-generation in addition to doing it in
37 * postEvaluationStatistics(...).
38
39 <p><b>Parameters</b><br>
40 <table>
41 <tr><td valign=top><i>base.</i><tt>gzip</tt><br>
42 <font size=-1>boolean</font></td>
43 <td valign=top>(whether or not to compress the file (.gz suffix added)</td></tr>
44 <tr><td valign=top><i>base.</i><tt>file</tt><br>
45 <font size=-1>String (a filename), or nonexistant (signifies stdout)</font></td>
46 <td valign=top>(the log for statistics)</td></tr>
47 </table>
48
49 *
50 * @author Sean Luke
51 * @version 1.0
52 */
53
54public class SimpleStatistics extends Statistics implements SteadyStateStatisticsForm //, ec.eval.ProvidesBestSoFar
55    {
56    public Individual[] getBestSoFar() { return best_of_run; }
57
58    /** log file parameter */
59    public static final String P_STATISTICS_FILE = "file";
60   
61    /** compress? */
62    public static final String P_COMPRESS = "gzip";
63
64    /** The Statistics' log */
65    public int statisticslog;
66
67    /** The best individual we've found so far */
68    public Individual[] best_of_run;
69       
70    /** Should we compress the file? */
71    public boolean compress;
72
73
74    public SimpleStatistics() { best_of_run = null; statisticslog = 0; /* stdout */ }
75
76    public void setup(final EvolutionState state, final Parameter base)
77        {
78        super.setup(state,base);
79       
80        compress = state.parameters.getBoolean(base.push(P_COMPRESS),null,false);
81               
82        File statisticsFile = state.parameters.getFile(
83            base.push(P_STATISTICS_FILE),null);
84
85        if (statisticsFile!=null)
86            try
87                {
88                statisticslog = state.output.addLog(statisticsFile, !compress, compress);
89                }
90            catch (IOException i)
91                {
92                state.output.fatal("An IOException occurred while trying to create the log " + statisticsFile + ":\n" + i);
93                }
94        }
95
96    public void postInitializationStatistics(final EvolutionState state)
97        {
98        super.postInitializationStatistics(state);
99       
100        // set up our best_of_run array -- can't do this in setup, because
101        // we don't know if the number of subpopulations has been determined yet
102        best_of_run = new Individual[state.population.subpops.length];
103        }
104
105    /** Logs the best individual of the generation. */
106    public void postEvaluationStatistics(final EvolutionState state)
107        {
108        super.postEvaluationStatistics(state);
109       
110        // for now we just print the best fitness per subpopulation.
111        Individual[] best_i = new Individual[state.population.subpops.length];  // quiets compiler complaints
112        for(int x=0;x<state.population.subpops.length;x++)
113            {
114            best_i[x] = state.population.subpops[x].individuals[0];
115            for(int y=1;y<state.population.subpops[x].individuals.length;y++)
116                if (state.population.subpops[x].individuals[y].fitness.betterThan(best_i[x].fitness))
117                    best_i[x] = state.population.subpops[x].individuals[y];
118       
119            // now test to see if it's the new best_of_run
120            if (best_of_run[x]==null || best_i[x].fitness.betterThan(best_of_run[x].fitness))
121                best_of_run[x] = (Individual)(best_i[x].clone());
122            }
123       
124        // print the best-of-generation individual
125        state.output.println("\nGeneration: " + state.generation,statisticslog);
126        state.output.println("Best Individual:",statisticslog);
127        for(int x=0;x<state.population.subpops.length;x++)
128            {
129            state.output.println("Subpopulation " + x + ":",statisticslog);
130            best_i[x].printIndividualForHumans(state,statisticslog);
131            state.output.message("Subpop " + x + " best fitness of generation: " + best_i[x].fitness.fitnessToStringForHumans());
132            }
133        }
134
135    /** Logs the best individual of the run. */
136    public void finalStatistics(final EvolutionState state, final int result)
137        {
138        super.finalStatistics(state,result);
139       
140        // for now we just print the best fitness
141       
142        state.output.println("\nBest Individual of Run:",statisticslog);
143        for(int x=0;x<state.population.subpops.length;x++ )
144            {
145            state.output.println("Subpopulation " + x + ":",statisticslog);
146            best_of_run[x].printIndividualForHumans(state,statisticslog);
147            state.output.message("Subpop " + x + " best fitness of run: " + best_of_run[x].fitness.fitnessToStringForHumans());
148
149            // finally describe the winner if there is a description
150            if (state.evaluator.p_problem instanceof SimpleProblemForm)
151                ((SimpleProblemForm)(state.evaluator.p_problem.clone())).describe(state, best_of_run[x], x, 0, statisticslog);     
152            }
153        }
154    }
Note: See TracBrowser for help on using the repository browser.