OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Sampler.h
Go to the documentation of this file.
1 //
2 // Class Sampler
3 // This class creates, dispatches and dumps new individuals.
4 //
5 // Copyright (c) 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // Yves Ineichen, ETH Zürich
7 // All rights reserved
8 //
9 // Implemented as part of the PhD thesis
10 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
11 //
12 // This file is part of OPAL.
13 //
14 // OPAL is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
21 //
22 #ifndef __OPAL_SAMPLER_H__
23 #define __OPAL_SAMPLER_H__
24 
25 #include <cstdio>
26 #include <cstdlib>
27 #include <ctime>
28 #include <string>
29 #include <sstream>
30 #include <vector>
31 #include <map>
32 #include <memory>
33 #include <queue>
34 #include <utility>
35 #include <fstream>
36 #include <list>
37 #include <memory>
38 #include <queue>
39 
40 #include "Comm/types.h"
41 #include "Util/Types.h"
42 #include "Util/CmdArguments.h"
43 
44 #include "Optimizer/Optimizer.h"
46 #include "Sample/SamplingMethod.h"
47 
48 #include <boost/smart_ptr.hpp>
49 
50 class Sampler : public Optimizer {
51 
52 public:
53 
59  Sampler(Expressions::Named_t objectives,
60  Expressions::Named_t constraints,
61  DVarContainer_t dvars,
62  size_t dim, Comm::Bundle_t comms,
63  CmdArguments_t args,
64  std::vector<double> hypervolRef,
65  int nrWorkerGroups);
66 
67 
78  Sampler(const std::map< std::string,
79  std::shared_ptr<SamplingMethod>
80  >& sampleMethods,
81  Expressions::Named_t objectives,
82  DVarContainer_t dvars,
83  Comm::Bundle_t comms,
84  CmdArguments_t args);
85 
87  virtual void initialize();
88 
90  typedef std::vector< SampleIndividual > SolutionState_t;
91 
92 protected:
93 
94  // implementing poller hooks
95  bool onMessage(MPI_Status status, size_t length);
96  void postPoll();
97 
98  void setupPoll() {}
99  void prePoll() {}
100  void onStop() {}
101 
102  // helper sending evaluation requests to the pilot
104 
105 private:
106 
107  std::map<std::string,
108  std::shared_ptr<SamplingMethod>
110 
111  // global index (for job id)
112  int gid;
113 
115 
117 
120 
122  std::map<size_t, boost::shared_ptr<Individual_t> > jobmapping_m;
123 
124  std::queue<boost::shared_ptr<Individual_t> > individuals_m;
125 
128 
131 
134 
136 
137 
140 
143 
145 
146  enum State {
149  TERMINATE
150  };
151 
153 
155  std::size_t jsonDumpFreq_m;
156  std::string jsonFname_m;
157  void writeJsonHeader();
158  std::list<Individual_t> individualsToDump_m;
159 
160  void dumpIndividualsToJSON();
161  void addIndividualToJSON(const boost::shared_ptr<Individual_t>& ind);
162 
163  void runStateMachine();
164 
165  void createNewIndividual();
166 };
167 
168 #endif
boost::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:176
std::map< std::string, DVar_t > DVarContainer_t
Definition: Types.h:92
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Definition: Expression.h:74
SampleIndividual Individual_t
Definition: Sampler.h:116
std::string jsonFname_m
Definition: Sampler.h:156
std::map< size_t, boost::shared_ptr< Individual_t > > jobmapping_m
mapping from unique job ID to individual
Definition: Sampler.h:122
int nSamples_m
Definition: Sampler.h:135
bool onMessage(MPI_Status status, size_t length)
Definition: Sampler.cpp:132
std::vector< SampleIndividual > SolutionState_t
type used in solution state exchange with other optimizers
Definition: Sampler.h:90
int done_sample_m
Definition: Sampler.h:144
void postPoll()
executed after handling (if any) new request
Definition: Sampler.cpp:183
int my_local_pid_
Definition: Sampler.h:114
State curState_m
Definition: Sampler.h:152
void setupPoll()
executed before starting polling loop
Definition: Sampler.h:98
void dispatch_forward_solves()
Definition: Sampler.cpp:340
std::list< Individual_t > individualsToDump_m
Definition: Sampler.h:158
Expressions::Named_t objectives_m
objectives
Definition: Sampler.h:133
virtual void initialize()
Initialization and start algorithm.
Definition: Sampler.cpp:95
std::size_t jsonDumpFreq_m
Dumps id, design variables and bound.
Definition: Sampler.h:155
Comm::Bundle_t comms_
communicator bundle for the optimizer
Definition: Sampler.h:119
std::map< std::string, std::shared_ptr< SamplingMethod > > sampleMethods_m
Definition: Sampler.h:109
int gid
Definition: Sampler.h:112
DVarContainer_t dvars_m
design variables
Definition: Sampler.h:130
std::queue< boost::shared_ptr< Individual_t > > individuals_m
Definition: Sampler.h:124
void dumpIndividualsToJSON()
Definition: Sampler.cpp:242
CmdArguments_t args_
command line arguments specified by the user
Definition: Sampler.h:139
void runStateMachine()
Definition: Sampler.cpp:304
void writeJsonHeader()
Definition: Sampler.cpp:218
int act_sample_m
current generation
Definition: Sampler.h:142
void onStop()
enable implementation to react to STOP tag
Definition: Sampler.h:100
void addIndividualToJSON(const boost::shared_ptr< Individual_t > &ind)
Definition: Sampler.cpp:295
@ STOP
Definition: Sampler.h:148
@ SUBMIT
Definition: Sampler.h:147
@ TERMINATE
Definition: Sampler.h:149
Sampler(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)
Definition: Sampler.cpp:39
void createNewIndividual()
Definition: Sampler.cpp:193
void prePoll()
executed before checking for new request
Definition: Sampler.h:99
bounds_t dVarBounds_m
bounds on each specified gene
Definition: Sampler.h:127
bundles all communicators for a specific role/pid
Definition: types.h:32
std::vector< std::pair< double, double > > bounds_t
type of bounds for design variables
Definition: Optimizer.h:39