/*** * @file Algorithm.hpp * * The general algorithm. */ #include using boost::property_tree::ptree; #include #include using namespace std; #ifndef ALGORITHM_HPP_ #define ALGORITHM_HPP_ /** * This is the main algorithm base class; actual algorithms should be * derived. This base class offers the general framework for setup and * iterative running, but knows nothing about individual * representation or operators actually used. This also handles the * "dirty and mundane work" of logging outputs etc. Formatting can, of * course, be done only by derived classes that know about the actual * representation. * * Boost property_tree is used for setup, because of its nice parser * for structured configuration. */ class Algorithm { protected: /** Number of generations to carry out. */ size_t _maxiter; /** Output directory in which all the results and traces will be saved. */ string _outputdirname; /** Next ID to give to a modified individual (unique running index). */ size_t _nextID = 1; // Tracing of information that gives insight to the algorithmic progression. // TODO: In final application use, we'd probably want to turn off the tracing. /** The IDs of parents; for tracing the algorithm progression. */ vector< vector > _parentsOf; // needs to be vector of vectors.. /** Traced measures; for tracing the algorithm progression. */ vector< vector > _tracedMeasures; protected: /** Derived classes must know how to setup their additional data * structures. */ virtual void impl_setup(const ptree & all) = 0; /** Give the next available ID (running index) */ virtual size_t nextID(); /** Remember a parenting relationship, for tracing the runs.*/ void markAsParents(size_t childID, const vector & parentIDs); /** Trace some measures of an individual, possibly more than just objectives. */ void traceMeasures(size_t childID, const vector & measures); /** Write the traced parenting history to a stream.*/ void writeParentHistory(ostream & os) const; /** Write the traced measures to a stream. */ void writeTracedMeasures(ostream & os) const; public: /** Default constructor; doesn't initialize much, so setup() must be called.*/ Algorithm(); /** Setup case-specific settings. */ virtual void setup(const ptree & all, const string & outputdirname); /** Run the algorithm as the setup tells us to. */ virtual void run(); }; #endif