Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/Statistics.java @ 6934

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

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

File size: 9.3 KB
RevLine 
[6152]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;
9import ec.steadystate.*;
10import ec.util.*;
11
12/*
13 * Statistics.java
14 *
15 * Created: Tue Aug 10 21:10:48 1999
16 * By: Sean Luke
17 */
18
19/**
20 * Statistics and its subclasses are Cliques which generate statistics
21 * during the run.  Statistics are arranged in a tree hierarchy --
22 * The root statistics object may have "children", and when the root is
23 * called, it calls its children with the same message.  You can override
24 * this behavior however you see fit.
25 
26 * <p>There are lots of places where statistics might be nice to print out.
27 * These places are implemented as hooks in the Statistics object which you
28 * can override if you like; otherwise they call the default behavior.  If
29 * you plan on allowing your Statistics subclass to contain children, you
30 * should remember to call the appropriate super.foo() if you
31 * override any foo() method.
32 *
33 * <p>While there are lots of hooks, various EvolutionState objects only
34 * implement a subset of them.   You'll need to look at the EvolutionState
35 * code to see which ones are implemented to make sure that your statistics
36 * hooks are called appropriately!
37 *
38 * <p>Statistics objects should set up their statistics logs etc. during
39 * <tt>setup(...)</tt>.  Remember to make the log restartable in
40 * case of restarting from a checkpoint.
41 *
42 * <p><b>Steady-State Statistics</b>.  For convenience, Statistics contains
43 * default implementations of the SteadyStateStatisticsForm methods but
44 * does not implement SteadyStateStatisticsForm.  This mostly is intended
45 * to keep SteadyStateStatistcsForm implementations from having to call
46 * functions on all their children: instead they can just call foo.super();
47 *
48 <p><b>Parameters</b><br>
49 <table>
50 <tr><td valign=top><i>base</i>.<tt>num-children</tt><br>
51 <font size=-1>int &gt;= 0</font></td>
52 <td valign=top>(number of child statistics objects)</td></tr>
53
54 <tr><td valign=top><i>base</i>.<tt>child</tt>.<i>n</i><br>
55 <font size=-1>classname, inherits or equals ec.Statistics</font></td>
56 <td valign=top>(the class of child statistics object number <i>n</i>)</td></tr>
57 </table>
58 
59 <p><b>Parameter bases</b><br>
60 <table>
61 <tr><td valign=top><i>base</i>.<tt>child</tt>.<i>n</i></td>
62 <td>species (child statistics object number <i>n</i>)</td></tr>
63 </table>
64 
65
66
67 * @author Sean Luke
68 * @version 2.0
69 */
70
71public class Statistics implements Singleton
72    {
73    public static final String P_NUMCHILDREN = "num-children";
74    public static final String P_CHILD = "child";
75    public Statistics[] children;
76   
77    public void setup(final EvolutionState state, final Parameter base)
78        {
79        int t = state.parameters.getIntWithDefault(base.push(P_NUMCHILDREN),null,0);
80        if (t < 0)
81            state.output.fatal("A Statistics object cannot have negative number of children",
82                base.push(P_NUMCHILDREN));
83
84        // load the trees
85        children = new Statistics[t];
86
87        for (int x=0;x<t;x++)
88            {
89            Parameter p = base.push(P_CHILD).push(""+x);
90            children[x] = (Statistics)(state.parameters.getInstanceForParameterEq(p,null,Statistics.class));
91            ((Statistics)children[x]).setup(state,p);
92            }
93        }
94   
95    /** Called immediately before population initialization occurs. */
96    public void preInitializationStatistics(final EvolutionState state)
97        {
98        for(int x=0;x<children.length;x++)
99            children[x].preInitializationStatistics(state);
100        }
101       
102    /** GENERATIONAL: Called immediately after population initialization occurs. */
103    public void postInitializationStatistics(final EvolutionState state)
104        {
105        for(int x=0;x<children.length;x++)
106            children[x].postInitializationStatistics(state);
107        }
108
109    /** Called immediately before checkpointing occurs. */
110    public void preCheckpointStatistics(final EvolutionState state)
111        {
112        for(int x=0;x<children.length;x++)
113            children[x].preCheckpointStatistics(state);
114        }
115
116    /** Called immediately after checkpointing occurs. */
117    public void postCheckpointStatistics(final EvolutionState state)
118        {
119        for(int x=0;x<children.length;x++)
120            children[x].postCheckpointStatistics(state);
121        }
122
123    /** GENERATIONAL: Called immediately before evaluation occurs. */
124    public void preEvaluationStatistics(final EvolutionState state)
125        {
126        for(int x=0;x<children.length;x++)
127            children[x].preEvaluationStatistics(state);
128        }
129
130    /** GENERATIONAL: Called immediately after evaluation occurs. */
131    public void postEvaluationStatistics(final EvolutionState state)
132        {
133        for(int x=0;x<children.length;x++)
134            children[x].postEvaluationStatistics(state);
135        }
136
137    /** Called immediately before the pre-breeding exchange occurs. */
138    public void prePreBreedingExchangeStatistics(final EvolutionState state)
139        {
140        for(int x=0;x<children.length;x++)
141            children[x].prePreBreedingExchangeStatistics(state);
142        }
143       
144    /** Called immediately after the pre-breeding exchange occurs. */
145    public void postPreBreedingExchangeStatistics(final EvolutionState state)
146        {
147        for(int x=0;x<children.length;x++)
148            children[x].postPreBreedingExchangeStatistics(state);
149        }
150
151    /** GENERATIONAL: Called immediately before breeding occurs. */
152    public void preBreedingStatistics(final EvolutionState state)
153        {
154        for(int x=0;x<children.length;x++)
155            children[x].preBreedingStatistics(state);
156        }
157
158    /** GENERATIONAL: Called immediately after breeding occurs. */
159    public void postBreedingStatistics(final EvolutionState state)
160        {
161        for(int x=0;x<children.length;x++)
162            children[x].postBreedingStatistics(state);
163        }
164
165    /** Called immediately before the post-breeding exchange occurs. */
166    public void prePostBreedingExchangeStatistics(final EvolutionState state)
167        {
168        for(int x=0;x<children.length;x++)
169            children[x].prePostBreedingExchangeStatistics(state);
170        }
171
172    /** Called immediately after the post-breeding exchange occurs. */
173    public void postPostBreedingExchangeStatistics(final EvolutionState state)
174        {
175        for(int x=0;x<children.length;x++)
176            children[x].postPostBreedingExchangeStatistics(state);
177        }
178
179    /** Called immediately after the run has completed.  <i>result</i>
180        is either <tt>state.R_FAILURE</tt>, indicating that an ideal individual
181        was not found, or <tt>state.R_SUCCESS</tt>, indicating that an ideal
182        individual <i>was</i> found. */
183    public void finalStatistics(final EvolutionState state, final int result)
184        {
185        for(int x=0;x<children.length;x++)
186            children[x].finalStatistics(state, result);
187        }
188   
189    /** STEADY-STATE: called when we created an empty initial Population. */
190    public void enteringInitialPopulationStatistics(final SteadyStateEvolutionState state)
191        {
192        for(int x=0;x<children.length;x++)
193            if (children[x] instanceof SteadyStateStatisticsForm)
194                ((SteadyStateStatisticsForm)children[x]).enteringInitialPopulationStatistics(state);
195        }
196       
197    /** STEADY-STATE: called when a given Subpopulation is entering the Steady-State. */
198    public void enteringSteadyStateStatistics(int subpop, final SteadyStateEvolutionState state)
199        {
200        for(int x=0;x<children.length;x++)
201            if (children[x] instanceof SteadyStateStatisticsForm)
202                ((SteadyStateStatisticsForm)children[x]).enteringSteadyStateStatistics(subpop, state);
203        }
204       
205    /** STEADY-STATE: called each time new individuals are bred during the steady-state
206        process. */
207    public void individualsBredStatistics(SteadyStateEvolutionState state, Individual[] individuals)
208        {
209        for(int x=0;x<children.length;x++)
210            if (children[x] instanceof SteadyStateStatisticsForm)
211                ((SteadyStateStatisticsForm)children[x]).individualsBredStatistics(state, individuals);
212        }
213   
214    /** STEADY-STATE: called each time new individuals are evaluated during the steady-state
215        process.  You can look up the individuals in state.newIndividuals[] */
216    public void individualsEvaluatedStatistics(SteadyStateEvolutionState state, Individual[] newIndividuals,
217        Individual[] oldIndividuals, int[] subpopulations, int[] indices)
218        {
219        for(int x=0;x<children.length;x++)
220            if (children[x] instanceof SteadyStateStatisticsForm)
221                ((SteadyStateStatisticsForm)children[x]).individualsEvaluatedStatistics(state, newIndividuals, oldIndividuals, subpopulations, indices);
222        }
223       
224    /** STEADY-STATE: called each time the generation count increments */
225    public void generationBoundaryStatistics(final EvolutionState state)
226        {
227        for (int x=0; x < children.length; x++)
228            if (children[x] instanceof SteadyStateStatisticsForm)
229                ((SteadyStateStatisticsForm)children[x]).generationBoundaryStatistics(state);
230        }
231    }
Note: See TracBrowser for help on using the repository browser.