ECF 1.5
State.h
1#ifndef State_h
2#define State_h
3
4class Algorithm;
5typedef boost::shared_ptr<Algorithm> AlgorithmP;
6class State;
7typedef boost::shared_ptr<State> StateP;
8
9#include "Population.h"
10#include "Randomizer.h"
11#include "Logger.h"
12#include "EvaluateOp.h"
13#include "HallOfFame.h"
14#include "StatCalc.h"
15#include "Operator.h"
16#include "Communicator.h"
17#include "Migration.h"
18#include "Context.h"
19
20#include <map>
21#include "boost/enable_shared_from_this.hpp"
22
23// XML configuration nodes
24#define NODE_REGISTRY "Registry"
25#define NODE_ALGORITHM "Algorithm"
26#define NODE_GENOTYPE "Genotype"
27#define NODE_MILESTONE "Milestone"
28#define NODE_POPULATION "Population"
29
30
38class State : public boost::enable_shared_from_this<State>
39{
40private:
41 // system components
42 RandomizerP randomizer_;
43 RegistryP registry_;
44 LoggerP logger_;
45 CommunicatorP comm_;
46 MigrationP migration_;
47
48 // evolutionary components
49 PopulationP population_;
50 AlgorithmP algorithm_;
51 CrossoverP crossover_;
52 MutationP mutation_;
53 EvolutionContextP context_;
54 EvaluateOpP evalOp_;
55 FitnessP fitness_;
56 IndividualP individual_;
57 std::vector<GenotypeP> genotype_;
58
59 std::map<std::string, AlgorithmP> mAlgorithms_;
60 typedef std::map<std::string, AlgorithmP>::iterator alg_iter;
61 std::map<std::string, GenotypeP> mGenotypes_;
62 typedef std::map<std::string, GenotypeP>::iterator gen_iter;
63 std::vector<OperatorP> allTerminationOps_;
64 std::vector<OperatorP> activeTerminationOps_;
65 std::vector<OperatorP> allUserOps_;
66 std::vector<OperatorP> activeUserOps_;
67 XMLNode xConfig_;
68
69 // system state
70 bool bInitialized_;
71 bool bCommandLine_;
72 bool bSaveMilestone_;
73 bool bLoadMilestone_;
74 bool bAlgorithmSet_;
75 bool bGenotypeSet_;
76 bool bEvaluatorSet_;
77 bool bBatchMode_;
78 bool bBatchStart_;
79 bool bBatchSingleMilestone_;
80 bool bBatchWriteStats_;
81 uint batchRepeats_;
82 uint batchRemaining_;
83 std::string batchStatsFile_;
84 std::string batchLogFile_;
85 std::string milestoneFilename_;
86 uint milestoneInterval_;
87 uint milestoneGeneration_;
88 time_t milestoneElapsedTime_;
89 time_t startTime_, currentTime_, elapsedTime_;
90 int argc_;
91 char **argv_;
92
93 StateP state_; // pointer to itself (to avoid http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/sp_techniques.html#from_this)
94 virtual StateP getState()
95 { return shared_from_this(); }
96
97 bool initializeComponents(int, char**);
98 bool runBatch();
99 bool parseCommandLine(int, char**);
100 bool parseConfig(std::string filename);
101 bool parseAlgorithmNode(XMLNode node);
102 bool parseGenotypeNode(XMLNode node);
103 void registerParameters(void);
104 void readParameters(void);
105 void saveMilestone(void);
106 void loadMilestone(void);
107 void write(XMLNode&);
108 void dumpParameters(std::string fileName, bool addClear = true);
109
110
111public:
112 State();
113 virtual ~State()
114 { }
115 bool initialize(int, char**);
116 bool run();
117 FitnessP getFitnessObject();
118
120 RandomizerP getRandomizer()
121 { return randomizer_; }
122
124 void setRandomizer(RandomizerP randomizer)
125 { randomizer_ = randomizer; }
126
128 RegistryP getRegistry()
129 { return registry_; }
130
132 LoggerP getLogger()
133 { return logger_; }
134
136 StatCalcP getStats()
137 { return population_->getStats(); }
138
140 HallOfFameP getHoF()
141 { return population_->getHof(); }
142
144 CommunicatorP getCommunicator()
145 { return comm_; }
146
149 { return context_->generationNo_; }
150
153 { return (uint) elapsedTime_; }
154
157 { return bBatchMode_; }
158
161 { context_->bTerminate_ = true; }
162
164 uint increaseEvaluations(uint nEval = 1)
165 { return population_->at(0)->stats_->increaseEvaluations(nEval); }
166
169 { return population_->getStats()->getEvaluations(); }
170
173 { return context_->bTerminate_; }
174
176 EvolutionContextP getContext()
177 { return context_; }
178
181 { return individual_; }
182
184 std::vector<GenotypeP> getGenotypes()
185 { return genotype_; }
186
188 PopulationP getPopulation()
189 { return population_; }
190
192 AlgorithmP getAlgorithm()
193 { return algorithm_; }
194
196 EvaluateOpP getEvalOp()
197 { return evalOp_; }
198
199 bool isImplicitParallel();
200 bool isAlgorithmParallel();
201
202
203// TODO: dynamic insertion of framework components
204 //addMutationOp // genotype
205 //addCrossoverOp // genotype
206
207// setting the components (the ones to be used)
208 uint setGenotype(GenotypeP);
209 void setAlgorithm(AlgorithmP);
210 void setEvalOp(EvaluateOpP);
211 void setEvalOp(EvaluateOp*);
212
213// adding the components (that _may_ be used or configured)
214 bool addGenotype(GenotypeP gen);
215 bool addAlgorithm(AlgorithmP alg);
216 bool addOperator(OperatorP op);
217
218};
219typedef boost::shared_ptr<State> StateP;
220
221#endif // State_h
222
Algorithm base class.
Definition: Algorithm.h:20
Evaluation base class.
Definition: EvaluateOp.h:17
State class - backbone of the framework.
Definition: State.h:39
PopulationP getPopulation()
get Population
Definition: State.h:188
EvolutionContextP getContext()
get evolutionary context
Definition: State.h:176
IndividualP getIndividualObject()
get one initial Individual object
Definition: State.h:180
void setAlgorithm(AlgorithmP)
Set the desired algorithm (overrides the current choice)
Definition: State.cpp:957
FitnessP getFitnessObject()
get one initial Fitness object (create on demand)
Definition: State.cpp:569
bool addGenotype(GenotypeP gen)
Add user-defined or user customized genotype. (The genotype can then be specified and used in config ...
Definition: State.cpp:1000
void setEvalOp(EvaluateOpP)
Set user defined evaluation operator.
Definition: State.cpp:969
bool addAlgorithm(AlgorithmP alg)
Add user-defined or user customized algorithm. (It can then be specified and used in config file....
Definition: State.cpp:1015
AlgorithmP getAlgorithm()
access current active Algorithm
Definition: State.h:192
bool run()
Driver of the evolution process - serial version.
Definition: State.cpp:1047
bool isImplicitParallel()
Is the algorithm executed implicitly parallel.
Definition: State.cpp:838
CommunicatorP getCommunicator()
access current process communicator mechanism
Definition: State.h:144
bool initialize(int, char **)
Initialize the whole system.
Definition: State.cpp:707
RandomizerP getRandomizer()
get current Randomizer
Definition: State.h:120
uint setGenotype(GenotypeP)
Set a genotype to be used in individuals.
Definition: State.cpp:930
RegistryP getRegistry()
get access to parameter repository
Definition: State.h:128
HallOfFameP getHoF()
get population hall of fame
Definition: State.h:140
EvaluateOpP getEvalOp()
access evaluation operator
Definition: State.h:196
bool getBatchMode()
get batch mode info (yes or no)
Definition: State.h:156
uint getEvaluations()
get number of evaluations (global population)
Definition: State.h:168
bool isAlgorithmParallel()
Is current algorithm parallel.
Definition: State.cpp:845
LoggerP getLogger()
get Logger
Definition: State.h:132
void setTerminateCond()
set termination condition (evolution ends with current generation)
Definition: State.h:160
StatCalcP getStats()
get population statistics
Definition: State.h:136
State()
Construct the one and only State object.
Definition: State.cpp:12
uint increaseEvaluations(uint nEval=1)
increase and return number of evaluations (local deme)
Definition: State.h:164
bool addOperator(OperatorP op)
Add user-defined operator. (Its parameters can now be specified and used in config file....
Definition: State.cpp:1028
bool getTerminateCond()
is termination condition set
Definition: State.h:172
std::vector< GenotypeP > getGenotypes()
access all active (currently used) genotypes
Definition: State.h:184
uint getGenerationNo()
get current generation number
Definition: State.h:148
void setRandomizer(RandomizerP randomizer)
set Randomizer to be used
Definition: State.h:124
uint getElapsedTime()
get elapsed time (in seconds)
Definition: State.h:152
Definition: nodes.h:92