Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/Evaluator.java @ 9286

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

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

File size: 5.7 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;
9import ec.util.ParamClassLoadException;
10import ec.util.Parameter;
11import ec.eval.MasterProblem;
12
13/*
14 * Evaluator.java
15 *
16 * Created: Tue Aug 10 20:53:30 1999
17 * By: Sean Luke
18 */
19
20/**
21 * An Evaluator is a singleton object which is responsible for the
22 * evaluation process during the course of an evolutionary run.  Only
23 * one Evaluator is created in a run, and is stored in the EvolutionState
24 * object.
25 *
26 * <p>Evaluators typically do their work by applying an instance of some
27 * subclass of Problem to individuals in the population.  Evaluators come
28 * with a Problem prototype which they may clone as necessary to create
29 * new Problem spaces to evaluate individuals in (Problems may be reused
30 * to prevent constant cloning).
31 *
32 * <p>Evaluators may be multithreaded, with one Problem instance per thread
33 * usually.  The number of threads they may spawn (excepting a parent
34 * "gathering" thread) is governed by the EvolutionState's evalthreads value.
35 *
36 * <p>Be careful about spawning threads -- this system has no few synchronized
37 * methods for efficiency's sake, so you must either divvy up evaluation in
38 * a thread-safe fashion, or
39 * otherwise you must obtain the appropriate locks on individuals in the population
40 * and other objects as necessary.
41 *
42 <p><b>Parameters</b><br>
43 <table>
44 <tr><td valign=top><i>base</i><tt>.problem</tt><br>
45 <font size=-1>classname, inherits and != ec.Problem</font></td>
46 <td valign=top>(the class for the Problem prototype p_problem)</td></tr>
47 <tr><td valign=top><i>base</i><tt>.masterproblem</tt><br>
48 <font size=-1>classname, inherits</font></td>
49 <td valign=top>(the class for the MasterProblem prototype masterproblem)</td></tr>
50 </table>
51 * @author Sean Luke
52 * @version 1.0
53 */
54
55public abstract class Evaluator implements Singleton
56    {
57    public static final String P_PROBLEM = "problem";
58
59    public Problem p_problem;
60
61    public static final String P_MASTERPROBLEM = "masterproblem";
62    public static final String P_IAMSLAVE = "i-am-slave";
63   
64    /** Evaluates the fitness of an entire population.  You will
65        have to determine how to handle multiple threads on your own,
66        as this is a very domain-specific thing. */
67    public abstract void evaluatePopulation(final EvolutionState state);
68
69    /** Returns true if an ideal individual has been found or some
70        other run result has shortcircuited the run so that it should
71        end prematurely right now. */
72    public abstract boolean runComplete(final EvolutionState state);
73
74    public void setup(final EvolutionState state, final Parameter base)
75        {
76        // Load my problem
77        p_problem = (Problem)(state.parameters.getInstanceForParameter(
78                base.push(P_PROBLEM),null,Problem.class));
79        p_problem.setup(state,base.push(P_PROBLEM));
80       
81        /*
82        // Check to see if this process is configured to be a master problem server
83        // If so, don't bother creating and setting up the master problem.
84        String masterServerName = state.parameters.getString(new Parameter(P_EVALSLAVENAME));
85
86        if (masterServerName == null)
87        {
88        */
89        // Am I a master problem and NOT a slave.  Note that the "eval.i-am-slave" parameter
90        // is not set by the user but rather programmatically by the Slave.java class
91        if( state.parameters.exists(base.push(P_MASTERPROBLEM),null) &&  // I am a master (or possibly a slave -- same params)
92            !state.parameters.getBoolean(base.push(P_IAMSLAVE),null,false))  // I am NOT a slave
93            {
94            try {
95                Problem masterproblem = (Problem)(state.parameters.getInstanceForParameter(
96                        base.push(P_MASTERPROBLEM),null,Problem.class));
97                masterproblem.setup(state,base.push(P_MASTERPROBLEM));
98                 
99                /*
100                 * If a MasterProblem was specified, interpose it between the
101                 * evaluator and the real problem.  This allows seamless use
102                 * of the master problem.
103                 */
104                ((MasterProblem)masterproblem).problem = p_problem;
105                p_problem = masterproblem;
106                }
107            catch(ParamClassLoadException e)
108                {
109                state.output.fatal("Parameter has an invalid value: "+base.push(P_MASTERPROBLEM));
110                }
111            }
112        /*
113          }
114        */       
115        }
116   
117    /** Called to set up remote evaluation network contacts when the run is started.  Mostly used for client/server evaluation (see MasterProblem).  By default calls p_problem.initializeContacts(state) */
118    public void initializeContacts(EvolutionState state)
119        {
120        p_problem.initializeContacts(state);
121        }
122   
123    /**  Called to reinitialize remote evaluation network contacts when the run is restarted from checkpoint.  Mostly used for client/server evaluation (see MasterProblem).  By default calls p_problem.reinitializeContacts(state) */
124    public void reinitializeContacts(EvolutionState state)
125        {
126        p_problem.reinitializeContacts(state);
127        }
128       
129    /**  Called to shut down remote evaluation network contacts when the run is completed.  Mostly used for client/server evaluation (see MasterProblem).  By default calls p_problem.closeContacts(state,result) */
130    public void closeContacts(EvolutionState state, int result)
131        {
132        p_problem.closeContacts(state,result);
133        }
134    }
Note: See TracBrowser for help on using the repository browser.