OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FixedPisaNsga2.h
Go to the documentation of this file.
1//
2// Class FixedPisaNsga2
3// Implementing the Variator for the PISA state machine.
4//
5// @see http://www.tik.ee.ethz.ch/pisa/
6//
7// The convergence behavior of the optimizer can be steered in 3 ways,
8// corresponding command line arguments are given in brackets:
9// - limit the number of generations (maxGenerations),
10// - specify a target hypervolume (expected-hypervol) and tolerance
11// (epsilon)
12// - specify a minimal hypervolume progress (conv-hvol-prog), relative to
13// the last generation, ((prev - new)/prev) that has to be attained to
14// continue optimizing.
15//
16// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
17// All rights reserved
18//
19// Implemented as part of the PhD thesis
20// "Toward massively parallel multi-objective optimization with application to
21// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
22//
23// This file is part of OPAL.
24//
25// OPAL is free software: you can redistribute it and/or modify
26// it under the terms of the GNU General Public License as published by
27// the Free Software Foundation, either version 3 of the License, or
28// (at your option) any later version.
29//
30// You should have received a copy of the GNU General Public License
31// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
32//
33#ifndef __FIXED_PISA_NSGA2_H__
34#define __FIXED_PISA_NSGA2_H__
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <time.h>
39
40#include <deque>
41#include <fstream>
42#include <map>
43#include <string>
44#include <sstream>
45#include <utility>
46#include <vector>
47
48#include "Comm/types.h"
49#include "Util/Types.h"
50#include "Util/CmdArguments.h"
51#include "Util/Statistics.h"
52
53#include "Optimizer/Optimizer.h"
57
58#include <boost/smart_ptr.hpp>
59#include <boost/chrono.hpp>
60#include <boost/property_tree/ptree.hpp>
61
62#include "Util/Trace/Trace.h"
63
64
65template<
66 template <class> class CrossoverOperator
67 , template <class> class MutationOperator
68>
69class FixedPisaNsga2 : public Optimizer {
70
71public:
72
87 Expressions::Named_t constraints,
88 DVarContainer_t dvars, size_t dim, Comm::Bundle_t comms,
89 CmdArguments_t args,
90 std::vector<double> hypervolRef,
91 int nrWorkerGroups);
92
94
96 virtual void initialize();
97
99 typedef std::vector< Individual > SolutionState_t;
106 using individual = boost::shared_ptr<typename FixedPisaNsga2::Individual_t>;
107
108protected:
109
112
114 virtual bool onMessage(MPI_Status status, size_t length);
115 virtual void postPoll();
116
117 virtual void setupPoll() {}
118 virtual void prePoll() {
119 // std::ostringstream debug;
120 // debug << "IN PRE POLL: ";
121 // debug << getStateString(curState_m) << std::endl;
122 // progress_->log(debug);
123 }
124 virtual void onStop() {}
128
129
130private:
131
134 Initialize = 0
137 , Select = 3
138 , Stop = 4
141 /* , SelectorStopped = 7 */
142 /* , Reset = 8 */
143 /* , ReadyForReset = 9 */
144 /* , ReadyForResetS = 10 */
145 /* , Restart = 11 */
146 };
147
148 std::string getStateString(PisaState_t) const;
149
150 // selector parameters
151 int seed; /* seed for random number generator */
152 const int tournament_m = 1; /* number of opponents for mating selection */
153
156
158 boost::scoped_ptr<Statistics<size_t> > statistics_;
159
160 boost::scoped_ptr<Variator_t> variator_m;
161
162 std::vector<unsigned int> pp_all;
163 std::vector<unsigned int> parent_queue_;
164 // std::set<unsigned int> archive_;
165 std::set<unsigned int> to_selector_;
166
167 // to compute the front
168 std::vector<int> copies;
169 std::vector<double> dist;
170 std::vector< std::vector<int> > front;
171 std::map<size_t, double> fitness_;
172
175
177 std::deque<unsigned int> finishedBuffer_m;
178
180 std::map<size_t, individual > jobmapping_m;
181
183 boost::shared_ptr<Population_t> paretoFront_m;
184
187
196
199
201 size_t alpha_m;
207 std::string file_start_m;
208
210 //size_t mu_m;
212 size_t lambda_m;
214 size_t dim_m;
216 size_t act_gen = 1;
221
223 std::string resultFile_m;
224 std::string resultDir_m;
225
226
227 // dump frequency
234 double hvol_eps_;
239
241 std::vector<double> hvol_ref_m;
242
244 std::string file_param_descr_;
245
246 boost::chrono::system_clock::time_point run_clock_start_;
247 boost::chrono::system_clock::time_point last_clock_;
248
249 // DEBUG output helpers
250 boost::scoped_ptr<Trace> job_trace_;
251 boost::scoped_ptr<Trace> progress_;
252
253
254 // entry point for starting the selector side of the PISA state machine
255 void startSelector(std::string filename_base);
256
259
262
265
268
269 // Selector methods
270 void selection();
276 int dominates(individual ind_a, individual ind_b);
277
279 bool checkParetoFront(unsigned int id);
282 void dumpPopulation(boost::shared_ptr<Population_t>);
283 void dumpPopulationToFile(boost::shared_ptr<Population_t>, std::ostringstream& filename, bool dump_offspring);
284 void dumpPopulationToJSON(boost::shared_ptr<Population_t>, std::ostringstream& filename, bool dump_offspring);
286 individual& ind,
287 std::ofstream& file,
288 const size_t numDigits);
290 individual& ind,
291 boost::property_tree::ptree& tree);
292
298 int irand(int range) {
299 return (int) ((double) range * (double) rand() / (RAND_MAX + 1.0));
300 }
301};
302
303#include "Optimizer/EA/FixedPisaNsga2.tcc"
304
305#endif
std::map< std::string, DVar_t > DVarContainer_t
Definition: Types.h:92
boost::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:176
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition: Expression.h:74
Definition: Select.h:26
Definition: Stop.h:24
bundles all communicators for a specific role/pid
Definition: types.h:32
void dispatch_forward_solves()
std::vector< Individual > SolutionState_t
type used in solution state exchange with other optimizers
std::vector< int > copies
number of individuals in the n-th front
void mergeOffspring()
const int tournament_m
std::string file_param_descr_
file header for result files contains this parameter description
size_t lambda_m
number of parents the selector chooses
Expressions::Named_t objectives_m
objectives
bounds_t dVarBounds_m
bounds on each specified gene
DVarContainer_t dvars_m
design variables
void toSelectorAndCommit()
passes finished individuals to the selector
boost::scoped_ptr< Trace > job_trace_
virtual bool onMessage(MPI_Status status, size_t length)
implementing poller hooks
void calcDistances()
virtual void postPoll()
executed after handling (if any) new request
size_t alpha_m
size of initial population
std::string resultFile_m
result file name
Variator< Individual_t, CrossoverOperator, MutationOperator > Variator_t
Expressions::Named_t constraints_m
constraints
int num_workergroups_m
number of individuals running
CmdArguments_t args_m
command line arguments specified by the user
size_t maxGenerations_m
maximal generation (stopping criterion)
void dumpPopulation(boost::shared_ptr< Population_t >)
Population< Individual_t > Population_t
void runStateMachine()
executes one loop of the PISA state machine
boost::shared_ptr< Population_t > paretoFront_m
population of pareto-front (for final output)
int dominates(individual ind_a, individual ind_b)
virtual void setupPoll()
executed before starting polling loop
std::vector< double > dist
void startSelector(std::string filename_base)
Individual Individual_t
type of our variator
std::vector< unsigned int > parent_queue_
IDs that will make new offspring.
std::map< size_t, double > fitness_
map between id and fitness (sum of front number and dist)
std::map< size_t, individual > jobmapping_m
mapping from unique job ID to individual
void matingSelection()
virtual void onStop()
enable implementation to react to STOP tag
boost::chrono::system_clock::time_point last_clock_
std::string file_start_m
population file to be started from
size_t exchangeSolStateFreq_m
how often do we exchange solutions with other optimizers
bool dump_offspring_m
dump offspring / parents flag
void exchangeSolutionStates()
if necessary exchange solution state with other optimizers
double conv_hvol_progress_
boost::chrono::system_clock::time_point run_clock_start_
std::string resultDir_m
size_t dim_m
number of objectives
virtual void initialize()
Starting selection algorithm and variator PISA state machine.
std::vector< std::vector< int > > front
individuals in each front
boost::scoped_ptr< Statistics< size_t > > statistics_
collect some statistics of rejected and accepted individuals
std::vector< double > hvol_ref_m
hypervolume reference point
int irand(int range)
void dumpPopulationToJSON(boost::shared_ptr< Population_t >, std::ostringstream &filename, bool dump_offspring)
bool initialOptimization_m
initial population optimization flag (increases initial population)
double hvol_eps_
convergence accuracy if maxGenerations not set
PisaState_t curState_m
the current state of the state machine
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)
void writeVariatorCfg()
Write the variator config file.
std::vector< unsigned int > pp_all
IDs of population.
bool birthControl_m
enforce strict population size
boost::scoped_ptr< Variator_t > variator_m
boost::scoped_ptr< Trace > progress_
boost::shared_ptr< typename FixedPisaNsga2::Individual_t > individual
alias for usage in template
std::string getStateString(PisaState_t) const
void dumpIndividualToJSON(int id, individual &ind, boost::property_tree::ptree &tree)
void calcFitnesses()
bool dump_dat_m
dump old data format
Comm::Bundle_t comms_
communicator bundle for the optimizer
void environmentalSelection()
bool checkParetoFront(unsigned int id)
check if individual in pareto front and add if not
std::deque< unsigned int > finishedBuffer_m
buffer holding all finished job id's
virtual void prePoll()
executed before checking for new request
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)
PisaState_t
all PISA states
size_t act_gen
current generation
std::set< unsigned int > to_selector_
Successfully run IDs to go into population.
bool initialized_m
indicating if initial population has been created
std::vector< std::pair< double, double > > bounds_t
type of bounds for design variables
Definition: Optimizer.h:39