OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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/property_tree/ptree.hpp>
59
60#include "Util/Trace/Trace.h"
61
62
63template<
64 template <class> class CrossoverOperator
65 , template <class> class MutationOperator
66>
67class FixedPisaNsga2 : public Optimizer {
68
69public:
70
85 Expressions::Named_t constraints,
86 DVarContainer_t dvars, size_t dim, Comm::Bundle_t comms,
87 CmdArguments_t args,
88 std::vector<double> hypervolRef,
89 int nrWorkerGroups);
90
92
94 virtual void initialize();
95
97 typedef std::vector< Individual > SolutionState_t;
104 using individual = std::shared_ptr<typename FixedPisaNsga2::Individual_t>;
105
106protected:
107
110
112 virtual bool onMessage(MPI_Status status, size_t length);
113 virtual void postPoll();
114
115 virtual void setupPoll() {}
116 virtual void prePoll() {
117 // std::ostringstream debug;
118 // debug << "IN PRE POLL: ";
119 // debug << getStateString(curState_m) << std::endl;
120 // progress_->log(debug);
121 }
122 virtual void onStop() {}
126
127
128private:
129
135 , Select = 3
136 , Stop = 4
139 /* , SelectorStopped = 7 */
140 /* , Reset = 8 */
141 /* , ReadyForReset = 9 */
142 /* , ReadyForResetS = 10 */
143 /* , Restart = 11 */
144 };
145
146 std::string getStateString(PisaState_t) const;
147
148 // selector parameters
149 int seed; /* seed for random number generator */
150 const int tournament_m = 1; /* number of opponents for mating selection */
151
154
156 const std::unique_ptr<Statistics<size_t> > statistics_;
157
158 std::unique_ptr<Variator_t> variator_m;
159
160 std::vector<unsigned int> pp_all;
161 std::vector<unsigned int> parent_queue_;
162 // std::set<unsigned int> archive_;
163 std::set<unsigned int> to_selector_;
164
165 // to compute the front
166 std::vector<int> copies;
167 std::vector<double> dist;
168 std::vector< std::vector<int> > front;
169 std::map<size_t, double> fitness_;
170
173
175 std::deque<unsigned int> finishedBuffer_m;
176
178 std::map<size_t, individual > jobmapping_m;
179
181 std::shared_ptr<Population_t> paretoFront_m;
182
185
194
197
199 size_t alpha_m;
205 std::string file_start_m;
206
208 //size_t mu_m;
210 size_t lambda_m;
212 size_t dim_m;
214 size_t act_gen = 1;
219
221 std::string resultFile_m;
222 std::string resultDir_m;
223
224
225 // dump frequency
232 double hvol_eps_;
237
239 std::vector<double> hvol_ref_m;
240
242 std::string file_param_descr_;
243
244 std::chrono::system_clock::time_point run_clock_start_;
245 std::chrono::system_clock::time_point last_clock_;
246
247 // DEBUG output helpers
248 std::unique_ptr<Trace> job_trace_;
249 std::unique_ptr<Trace> progress_;
250
251
252 // entry point for starting the selector side of the PISA state machine
253 void startSelector(std::string filename_base);
254
257
260
263
266
267 // Selector methods
268 void selection();
274 int dominates(individual ind_a, individual ind_b);
275
277 bool checkParetoFront(unsigned int id);
280 void dumpPopulation(std::shared_ptr<Population_t>);
281 void dumpPopulationToFile(std::shared_ptr<Population_t>, std::ostringstream& filename, bool dump_offspring);
282 void dumpPopulationToJSON(std::shared_ptr<Population_t>, std::ostringstream& filename, bool dump_offspring);
284 individual& ind,
285 std::ofstream& file,
286 const size_t numDigits);
288 individual& ind,
289 boost::property_tree::ptree& tree);
290
296 int irand(int range) {
297 return (int) ((double) range * (double) rand() / (RAND_MAX + 1.0));
298 }
299};
300
301#include "Optimizer/EA/FixedPisaNsga2.tcc"
302
303#endif
std::map< std::string, DVar_t > DVarContainer_t
Definition Types.h:92
std::shared_ptr< CmdArguments > CmdArguments_t
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition Expression.h:74
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 std::unique_ptr< Statistics< size_t > > statistics_
collect some statistics of rejected and accepted individuals
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
std::unique_ptr< Trace > progress_
DVarContainer_t dvars_m
design variables
void toSelectorAndCommit()
passes finished individuals to the selector
virtual bool onMessage(MPI_Status status, size_t length)
implementing poller hooks
void calcDistances()
std::shared_ptr< Population_t > paretoFront_m
population of pareto-front (for final output)
void dumpPopulationToJSON(std::shared_ptr< Population_t >, std::ostringstream &filename, bool dump_offspring)
virtual void postPoll()
executed after handling (if any) new request
size_t alpha_m
size of initial population
std::unique_ptr< Trace > job_trace_
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)
Population< Individual_t > Population_t
void runStateMachine()
executes one loop of the PISA state machine
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
void dumpPopulationToFile(std::shared_ptr< Population_t >, std::ostringstream &filename, bool dump_offspring)
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_
std::unique_ptr< Variator_t > variator_m
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
std::vector< double > hvol_ref_m
hypervolume reference point
int irand(int range)
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)
std::shared_ptr< typename FixedPisaNsga2::Individual_t > individual
alias for usage in template
void dumpPopulation(std::shared_ptr< Population_t >)
void writeVariatorCfg()
Write the variator config file.
std::vector< unsigned int > pp_all
IDs of population.
bool birthControl_m
enforce strict population size
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
std::chrono::system_clock::time_point run_clock_start_
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
std::chrono::system_clock::time_point last_clock_
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
Optimizer(MPI_Comm comm)
Definition Optimizer.h:35