OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
OpalSample.cpp
Go to the documentation of this file.
1 #include "Sample/OpalSample.h"
2 
6 #include "Utilities/Util.h"
7 
8 #include "Sample/Uniform.h"
9 #include "Sample/Normal.h"
10 #include "Sample/SampleSequence.h"
12 #include "Sample/FromFile.h"
13 #include "Sample/LatinHyperCube.h"
15 
16 
17 // Class OpalSample
18 // ------------------------------------------------------------------------
19 
20 // The attributes of class OpalSample.
21 namespace {
22  enum {
23  TYPE, // The type of sampling
24  VARIABLE, // name of design variable
25  SEED, // for random sample methods
26  FNAME, // file to read from sampling points
27  N,
28  RANDOM,
29  STEP,
30  SIZE
31  };
32 }
33 
35  Definition(SIZE, "SAMPLING",
36  "The \"SAMPLING\" statement defines methods used for the optimizer in sample mode.")
37  , size_m(1)
38 {
40  ("TYPE", "UNIFORM_INT, UNIFORM, GAUSSIAN, FROMFILE, LATIN_HYPERCUBE");
41 
43  ("VARIABLE", "Name of design variable");
44 
46  ("SEED", "seed for random sampling");
47 
49  ("FNAME", "File to read from the sampling points");
50 
52  ("N", "Number of sampling points", 1);
53 
55  ("RANDOM", "Whether sequence should be sampled randomly (default: false)", false);
56 
58  ("STEP", "Increment for randomized sequences (default: 1)", 1.0);
59 
61 }
62 
63 
64 OpalSample::OpalSample(const std::string &name, OpalSample *parent):
65  Definition(name, parent)
66 {}
67 
68 
69 OpalSample *OpalSample::clone(const std::string &name) {
70  return new OpalSample(name, this);
71 }
72 
73 
75 
76 }
77 
78 
79 OpalSample *OpalSample::find(const std::string &name) {
80  OpalSample *sampling = dynamic_cast<OpalSample *>(OpalData::getInstance()->find(name));
81 
82  if (sampling == nullptr) {
83  throw OpalException("OpalSample::find()",
84  "OpalSample \"" + name + "\" not found.");
85  }
86  return sampling;
87 }
88 
89 
90 void OpalSample::initialize(const std::string &dvarName,
91  double lower,
92  double upper,
93  size_t modulo,
94  bool sequence) {
95 
96  if ( lower >= upper )
97  throw OpalException("OpalSample::initialize()",
98  "Lower bound >= upper bound.");
99 
101 
102  int seed = Attributes::getReal(itsAttr[SEED]);
104  double step = Attributes::getReal(itsAttr[STEP]);
105 
106  bool random = Attributes::getBool(itsAttr[RANDOM]);
107 
108  if (!random) {
109  if (type == "UNIFORM_INT") {
110  sampleMethod_m.reset( new SampleSequence<int>(lower, upper, modulo, size_m) );
111  } else if (type == "UNIFORM") {
112  sampleMethod_m.reset( new SampleSequence<double>(lower, upper, modulo, size_m) );
113  } else if (type == "GAUSSIAN") {
114  sampleMethod_m.reset( new SampleGaussianSequence(lower, upper, modulo, size_m) );
115  } else if (type == "FROMFILE") {
116  std::string fname = Attributes::getString(itsAttr[FNAME]);
117  sampleMethod_m.reset( new FromFile(fname, dvarName, modulo) );
118  size_m = static_cast<FromFile*>(sampleMethod_m.get())->getSize();
119  } else {
120  throw OpalException("OpalSample::initialize()",
121  "Unknown sampling method: '" + type + "'.");
122  }
123  } else {
124  if (type == "UNIFORM_INT") {
125  if (Attributes::getReal(itsAttr[SEED])) {
126  sampleMethod_m.reset( new Uniform<int>(lower, upper, seed) );
127  } else {
128  sampleMethod_m.reset( new Uniform<int>(lower, upper) );
129  }
130  } else if (type == "UNIFORM") {
131  if (Attributes::getReal(itsAttr[SEED])) {
132  sampleMethod_m.reset( new Uniform<double>(lower, upper, seed) );
133  } else {
134  sampleMethod_m.reset( new Uniform<double>(lower, upper) );
135  }
136  } else if (type == "GAUSSIAN") {
137  if (Attributes::getReal(itsAttr[SEED])) {
138  sampleMethod_m.reset( new Normal(lower, upper, seed) );
139  } else {
140  sampleMethod_m.reset( new Normal(lower, upper) );
141  }
142  } else if (type == "FROMFILE") {
143  std::string fname = Attributes::getString(itsAttr[FNAME]);
144  sampleMethod_m.reset( new FromFile(fname, dvarName, modulo) );
145  size_m = static_cast<FromFile*>(sampleMethod_m.get())->getSize();
146  } else if (type == "LATIN_HYPERCUBE") {
147  if (Attributes::getReal(itsAttr[SEED])) {
148  sampleMethod_m.reset( new LatinHyperCube(lower, upper, seed) );
149  } else {
150  sampleMethod_m.reset( new LatinHyperCube(lower, upper) );
151  }
152  } else if (type == "RANDOM_SEQUENCE_UNIFORM_INT") {
153  if (Attributes::getReal(itsAttr[SEED])) {
154  sampleMethod_m.reset(
155  new SampleRandomizedSequence<int>(lower, upper, step, seed)
156  );
157  } else {
158  sampleMethod_m.reset(
159  new SampleRandomizedSequence<int>(lower, upper, step)
160  );
161  }
162  } else if (type == "RANDOM_SEQUENCE_UNIFORM") {
163  if (Attributes::getReal(itsAttr[SEED])) {
164  sampleMethod_m.reset(
165  new SampleRandomizedSequence<double>(lower, upper, step, seed)
166  );
167  } else {
168  sampleMethod_m.reset(
169  new SampleRandomizedSequence<double>(lower, upper, step)
170  );
171  }
172  } else {
173  throw OpalException("OpalSample::initialize()",
174  "Unknown sampling method: '" + type + "'.");
175  }
176  }
177 }
178 
179 
180 std::string OpalSample::getVariable() const {
181  return Attributes::getString(itsAttr[VARIABLE]);
182 }
int seed
The current random seed.
Definition: Options.cpp:41
virtual void execute()
Check the OpalSample data.
Definition: OpalSample.cpp:74
The base class for all OPAL definitions.
Definition: Definition.h:30
The base class for all OPAL exceptions.
Definition: OpalException.h:28
std::string toUpper(const std::string &str)
Definition: Util.cpp:130
Definition: Normal.h:9
unsigned int getSize() const
Definition: OpalSample.h:58
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:66
static OpalSample * find(const std::string &name)
Find sampling method.
Definition: OpalSample.cpp:79
static OpalData * getInstance()
Definition: OpalData.cpp:209
The SAMPLING definition.
Definition: OpalSample.h:15
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
std::string getVariable() const
Definition: OpalSample.cpp:180
std::shared_ptr< SamplingMethod > sampleMethod_m
Definition: OpalSample.h:43
void initialize(const std::string &dvarName, double lower, double upper, size_t modulo=1, bool sequence=false)
Definition: OpalSample.cpp:90
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:618
unsigned int size_m
Definition: OpalSample.h:54
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
const std::string name
virtual OpalSample * clone(const std::string &name)
Make clone.
Definition: OpalSample.cpp:69
double FromFile(std::string file, const std::vector< double > &referencePoint)
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:217
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:296
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:205
OpalSample()
Exemplar constructor.
Definition: OpalSample.cpp:34
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:307