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