OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
FixedPisaNsga2.h
Go to the documentation of this file.
1 #ifndef __FIXED_PISA_NSGA2_H__
2 #define __FIXED_PISA_NSGA2_H__
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <time.h>
7 
8 #include <deque>
9 #include <fstream>
10 #include <map>
11 #include <string>
12 #include <sstream>
13 #include <utility>
14 #include <vector>
15 
16 #include "Comm/types.h"
17 #include "Util/Types.h"
18 #include "Util/CmdArguments.h"
19 #include "Util/Statistics.h"
20 
21 #include "Optimizer/Optimizer.h"
24 #include "Optimizer/EA/Variator.h"
25 
26 #include <boost/smart_ptr.hpp>
27 #include <boost/chrono.hpp>
28 #include <boost/property_tree/ptree.hpp>
29 
30 #include "Util/Trace/Trace.h"
31 
47 template<
48  template <class> class CrossoverOperator
49  , template <class> class MutationOperator
50 >
51 class FixedPisaNsga2 : public Optimizer {
52 
53 public:
54 
69  Expressions::Named_t constraints,
70  DVarContainer_t dvars, size_t dim, Comm::Bundle_t comms,
71  CmdArguments_t args,
72  std::vector<double> hypervolRef,
73  int nrWorkerGroups);
74 
76 
78  virtual void initialize();
79 
81  typedef std::vector< Individual > SolutionState_t;
88  using individual = boost::shared_ptr<typename FixedPisaNsga2::Individual_t>;
89 
90 protected:
91 
93  void writeVariatorCfg();
94 
96  virtual bool onMessage(MPI_Status status, size_t length);
97  virtual void postPoll();
98 
99  virtual void setupPoll() {}
100  virtual void prePoll() {
101  // std::ostringstream debug;
102  // debug << "IN PRE POLL: ";
103  // debug << getStateString(curState_m) << std::endl;
104  // progress_->log(debug);
105  }
106  virtual void onStop() {}
110 
111 
112 private:
113 
115  enum PisaState_t {
118  , Variate = 2
119  , Select = 3
120  , Stop = 4
123  /* , SelectorStopped = 7 */
124  /* , Reset = 8 */
125  /* , ReadyForReset = 9 */
126  /* , ReadyForResetS = 10 */
127  /* , Restart = 11 */
128  };
129 
130  std::string getStateString(PisaState_t) const;
131 
132  // selector parameters
133  int seed; /* seed for random number generator */
134  const int tournament_m = 1; /* number of opponents for mating selection */
135 
138 
140  boost::scoped_ptr<Statistics<size_t> > statistics_;
141 
142  boost::scoped_ptr<Variator_t> variator_m;
143 
144  std::vector<unsigned int> pp_all;
145  std::vector<unsigned int> parent_queue_;
146  // std::set<unsigned int> archive_;
147  std::set<unsigned int> to_selector_;
148 
149  // to compute the front
150  std::vector<int> copies;
151  std::vector<double> dist;
152  std::vector< std::vector<int> > front;
153  std::map<size_t, double> fitness_;
154 
157 
159  std::deque<unsigned int> finishedBuffer_m;
160 
162  std::map<size_t, individual > jobmapping_m;
163 
165  boost::shared_ptr<Population_t> paretoFront_m;
166 
169 
178 
181 
183  size_t alpha_m;
189  std::string file_start_m;
190 
192  //size_t mu_m;
194  size_t lambda_m;
196  size_t dim_m;
198  size_t act_gen = 1;
203 
205  std::string resultFile_m;
206  std::string resultDir_m;
207 
208 
209  // dump frequency
216  double hvol_eps_;
221 
223  std::vector<double> hvol_ref_m;
224 
226  std::string file_param_descr_;
227 
228  boost::chrono::system_clock::time_point run_clock_start_;
229  boost::chrono::system_clock::time_point last_clock_;
230 
231  // DEBUG output helpers
232  boost::scoped_ptr<Trace> job_trace_;
233  boost::scoped_ptr<Trace> progress_;
234 
235 
236  // entry point for starting the selector side of the PISA state machine
237  void startSelector(std::string filename_base);
238 
240  void runStateMachine();
241 
243  void toSelectorAndCommit();
244 
247 
249  void exchangeSolutionStates();
250 
251  // Selector methods
252  void selection();
253  void mergeOffspring();
254  void calcFitnesses();
255  void calcDistances();
256  void environmentalSelection();
257  void matingSelection();
258  int dominates(individual ind_a, individual ind_b);
259 
261  bool checkParetoFront(unsigned int id);
264  void dumpPopulation(boost::shared_ptr<Population_t>);
265  void dumpPopulationToFile(boost::shared_ptr<Population_t>, std::ostringstream& filename, bool dump_offspring);
266  void dumpPopulationToJSON(boost::shared_ptr<Population_t>, std::ostringstream& filename, bool dump_offspring);
267  void dumpIndividualToFile(int id,
268  individual& ind,
269  std::ofstream& file,
270  const size_t numDigits);
271  void dumpIndividualToJSON(int id,
272  individual& ind,
273  boost::property_tree::ptree& tree);
274 
280  int irand(int range) {
281  return (int) ((double) range * (double) rand() / (RAND_MAX + 1.0));
282  }
283 };
284 
285 #include "Optimizer/EA/FixedPisaNsga2.tcc"
286 
287 #endif
size_t lambda_m
number of parents the selector chooses
The STOP command.
Definition: Stop.h:28
std::vector< double > hvol_ref_m
hypervolume reference point
void calcDistances()
bool birthControl_m
enforce strict population size
int dominates(individual ind_a, individual ind_b)
std::deque< unsigned int > finishedBuffer_m
buffer holding all finished job id&#39;s
std::vector< int > copies
number of individuals in the n-th front
std::vector< std::pair< double, double > > bounds_t
type of bounds for design variables
Definition: Optimizer.h:22
bool dump_offspring_m
dump offspring / parents flag
std::set< unsigned int > to_selector_
Successfully run IDs to go into population.
virtual bool onMessage(MPI_Status status, size_t length)
implementing poller hooks
std::vector< double > dist
Expressions::Named_t constraints_m
constraints
virtual void initialize()
Starting selection algorithm and variator PISA state machine.
boost::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:169
boost::scoped_ptr< Trace > progress_
std::vector< unsigned int > parent_queue_
IDs that will make new offspring.
Variator< Individual_t, CrossoverOperator, MutationOperator > Variator_t
bool initialized_m
indicating if initial population has been created
std::map< size_t, individual > jobmapping_m
mapping from unique job ID to individual
void environmentalSelection()
std::string getStateString(PisaState_t) const
bool dump_dat_m
dump old data format
double conv_hvol_progress_
Expressions::Named_t objectives_m
objectives
boost::shared_ptr< Population_t > paretoFront_m
population of pareto-front (for final output)
Comm::Bundle_t comms_
communicator bundle for the optimizer
Implementing the Variator for the PISA state machine.
size_t exchangeSolStateFreq_m
how often do we exchange solutions with other optimizers
bounds_t dVarBounds_m
bounds on each specified gene
std::string resultDir_m
void runStateMachine()
executes one loop of the PISA state machine
void startSelector(std::string filename_base)
double hvol_eps_
convergence accuracy if maxGenerations not set
size_t maxGenerations_m
maximal generation (stopping criterion)
boost::chrono::system_clock::time_point run_clock_start_
const int tournament_m
Population< Individual_t > Population_t
void dumpPopulationToJSON(boost::shared_ptr< Population_t >, std::ostringstream &filename, bool dump_offspring)
An abstract class defining the interface for all optimizer components.
Definition: Optimizer.h:14
size_t act_gen
current generation
virtual void onStop()
enable implementation to react to STOP tag
std::map< size_t, double > fitness_
map between id and fitness (sum of front number and dist)
boost::scoped_ptr< Statistics< size_t > > statistics_
collect some statistics of rejected and accepted individuals
bool initialOptimization_m
initial population optimization flag (increases initial population)
PisaState_t
all PISA states
std::vector< unsigned int > pp_all
IDs of population.
void calcFitnesses()
boost::chrono::system_clock::time_point last_clock_
int irand(int range)
CmdArguments_t args_m
command line arguments specified by the user
void dumpIndividualToJSON(int id, individual &ind, boost::property_tree::ptree &tree)
std::map< std::string, DVar_t > DVarContainer_t
Definition: Types.h:77
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition: Expression.h:45
void matingSelection()
FixedPisaNsga2(Expressions::Named_t objectives, Expressions::Named_t constraints, DVarContainer_t dvars, size_t dim, Comm::Bundle_t comms, CmdArguments_t args, std::vector< double > hypervolRef, int nrWorkerGroups)
void dispatch_forward_solves()
virtual void postPoll()
executed after handling (if any) new request
The SELECT command.
Definition: Select.h:30
size_t dim_m
number of objectives
std::vector< std::vector< int > > front
individuals in each front
std::vector< Individual > SolutionState_t
type used in solution state exchange with other optimizers
boost::scoped_ptr< Variator_t > variator_m
boost::shared_ptr< typename FixedPisaNsga2::Individual_t > individual
alias for usage in template
void mergeOffspring()
virtual void prePoll()
executed before checking for new request
std::string file_param_descr_
file header for result files contains this parameter description
int num_workergroups_m
number of individuals running
Individual Individual_t
type of our variator
DVarContainer_t dvars_m
design variables
void dumpPopulation(boost::shared_ptr< Population_t >)
void writeVariatorCfg()
Write the variator config file.
boost::scoped_ptr< Trace > job_trace_
size_t alpha_m
size of initial population
std::string resultFile_m
result file name
bool checkParetoFront(unsigned int id)
check if individual in pareto front and add if not
PisaState_t curState_m
the current state of the state machine
bundles all communicators for a specific role/pid
Definition: types.h:14
std::string file_start_m
population file to be started from
virtual void setupPoll()
executed before starting polling loop
void exchangeSolutionStates()
if necessary exchange solution state with other optimizers
void toSelectorAndCommit()
passes finished individuals to the selector
void dumpIndividualToFile(int id, individual &ind, std::ofstream &file, const size_t numDigits)
void dumpPopulationToFile(boost::shared_ptr< Population_t >, std::ostringstream &filename, bool dump_offspring)