00001 #ifndef State_h
00002 #define State_h
00003
00004 class Algorithm;
00005 typedef boost::shared_ptr<Algorithm> AlgorithmP;
00006 class State;
00007 typedef boost::shared_ptr<State> StateP;
00008
00009 #include "Population.h"
00010 #include "Randomizer.h"
00011 #include "Logger.h"
00012 #include "EvaluateOp.h"
00013 #include "HallOfFame.h"
00014 #include "StatCalc.h"
00015 #include "Operator.h"
00016 #include "Communicator.h"
00017 #include "Migration.h"
00018 #include "Context.h"
00019
00020 #include <map>
00021 #include "boost/enable_shared_from_this.hpp"
00022
00023
00024 #define NODE_REGISTRY "Registry"
00025 #define NODE_ALGORITHM "Algorithm"
00026 #define NODE_GENOTYPE "Genotype"
00027 #define NODE_MILESTONE "Milestone"
00028 #define NODE_POPULATION "Population"
00029
00030
00038 class State : public boost::enable_shared_from_this<State>
00039 {
00040 private:
00041
00042 RandomizerP randomizer_;
00043 RegistryP registry_;
00044 LoggerP logger_;
00045 CommunicatorP comm_;
00046 MigrationP migration_;
00047
00048
00049 PopulationP population_;
00050 AlgorithmP algorithm_;
00051 CrossoverP crossover_;
00052 MutationP mutation_;
00053 EvolutionContextP context_;
00054 EvaluateOpP evalOp_;
00055 FitnessP fitness_;
00056 IndividualP individual_;
00057 std::vector<GenotypeP> genotype_;
00058
00059 std::map<std::string, AlgorithmP> mAlgorithms_;
00060 typedef std::map<std::string, AlgorithmP>::iterator alg_iter;
00061 std::map<std::string, GenotypeP> mGenotypes_;
00062 typedef std::map<std::string, GenotypeP>::iterator gen_iter;
00063 std::vector<OperatorP> allTerminationOps_;
00064 std::vector<OperatorP> activeTerminationOps_;
00065 std::vector<OperatorP> allUserOps_;
00066 std::vector<OperatorP> activeUserOps_;
00067 XMLNode xConfig_;
00068
00069
00070 bool bInitialized_;
00071 bool bCommandLine_;
00072 bool bSaveMilestone_;
00073 bool bLoadMilestone_;
00074 bool bAlgorithmSet_;
00075 bool bGenotypeSet_;
00076 bool bEvaluatorSet_;
00077 bool bBatchMode_;
00078 bool bBatchStart_;
00079 bool bBatchSingleMilestone_;
00080 bool bBatchWriteStats_;
00081 uint batchRepeats_;
00082 uint batchRemaining_;
00083 std::string batchStatsFile_;
00084 std::string batchLogFile_;
00085 std::string milestoneFilename_;
00086 uint milestoneInterval_;
00087 uint milestoneGeneration_;
00088 time_t milestoneElapsedTime_;
00089 time_t startTime_, currentTime_, elapsedTime_;
00090 int argc_;
00091 char **argv_;
00092
00093 StateP state_;
00094 virtual StateP getState()
00095 { return shared_from_this(); }
00096
00097 bool initializeComponents(int, char**);
00098 bool runBatch();
00099 bool parseCommandLine(int, char**);
00100 bool parseConfig(std::string filename);
00101 bool parseAlgorithmNode(XMLNode node);
00102 bool parseGenotypeNode(XMLNode node);
00103 void registerParameters(void);
00104 void readParameters(void);
00105 void saveMilestone(void);
00106 void loadMilestone(void);
00107 void write(XMLNode&);
00108 void dumpParameters(std::string fileName, bool addClear = true);
00109
00110
00111 public:
00112 State();
00113 virtual ~State()
00114 { }
00115 bool initialize(int, char**);
00116 bool run();
00117 FitnessP getFitnessObject();
00118
00120 RandomizerP getRandomizer()
00121 { return randomizer_; }
00122
00124 void setRandomizer(RandomizerP randomizer)
00125 { randomizer_ = randomizer; }
00126
00128 RegistryP getRegistry()
00129 { return registry_; }
00130
00132 LoggerP getLogger()
00133 { return logger_; }
00134
00136 StatCalcP getStats()
00137 { return population_->getStats(); }
00138
00140 HallOfFameP getHoF()
00141 { return population_->getHof(); }
00142
00144 CommunicatorP getCommunicator()
00145 { return comm_; }
00146
00148 uint getGenerationNo()
00149 { return context_->generationNo_; }
00150
00152 uint getElapsedTime()
00153 { return (uint) elapsedTime_; }
00154
00156 bool getBatchMode()
00157 { return bBatchMode_; }
00158
00160 void setTerminateCond()
00161 { context_->bTerminate_ = true; }
00162
00164 uint increaseEvaluations(uint nEval = 1)
00165 { return population_->at(0)->stats_->increaseEvaluations(nEval); }
00166
00168 uint getEvaluations()
00169 { return population_->getStats()->getEvaluations(); }
00170
00172 bool getTerminateCond()
00173 { return context_->bTerminate_; }
00174
00176 EvolutionContextP getContext()
00177 { return context_; }
00178
00180 IndividualP getIndividualObject()
00181 { return individual_; }
00182
00184 std::vector<GenotypeP> getGenotypes()
00185 { return genotype_; }
00186
00188 PopulationP getPopulation()
00189 { return population_; }
00190
00192 AlgorithmP getAlgorithm()
00193 { return algorithm_; }
00194
00196 EvaluateOpP getEvalOp()
00197 { return evalOp_; }
00198
00199 bool isImplicitParallel();
00200 bool isAlgorithmParallel();
00201
00202
00203
00204
00205
00206
00207
00208 uint setGenotype(GenotypeP);
00209 void setAlgorithm(AlgorithmP);
00210 void setEvalOp(EvaluateOpP);
00211 void setEvalOp(EvaluateOp*);
00212
00213
00214 bool addGenotype(GenotypeP gen);
00215 bool addAlgorithm(AlgorithmP alg);
00216 bool addOperator(OperatorP op);
00217
00218 };
00219 typedef boost::shared_ptr<State> StateP;
00220
00221 #endif // State_h
00222