Code indexing in gitaly is broken and leads to code not being visible to the user. We work on the issue with highest priority.

Skip to content
Snippets Groups Projects
Commit e6ff3e7a authored by frey_m's avatar frey_m
Browse files

Sampler: Add randomized sequence + add new lines at end of files

parent 0228766f
No related branches found
No related tags found
1 merge request!59Master
......@@ -22,6 +22,7 @@ set (HDRS
SampleIndividual.h
SamplePilot.h
Sampler.h
SampleRandomizedSequence.h
SampleSequence.h
SampleWorker.h
SamplingMethod.h
......
......@@ -11,6 +11,7 @@
#include "Sample/SampleGaussianSequence.h"
#include "Sample/FromFile.h"
#include "Sample/LatinHyperCube.h"
#include "Sample/SampleRandomizedSequence.h"
// Class OpalSample
......@@ -25,6 +26,7 @@ namespace {
FNAME, // file to read from sampling points
N,
RANDOM,
STEP,
SIZE
};
}
......@@ -52,6 +54,9 @@ OpalSample::OpalSample():
itsAttr[RANDOM] = Attributes::makeBool
("RANDOM", "Whether sequence should be sampled randomly (default: false)", false);
itsAttr[STEP] = Attributes::makeReal
("STEP", "Increment for randomized sequences (default: 1)", 1.0);
registerOwnership(AttributeHandler::STATEMENT);
}
......@@ -96,6 +101,7 @@ void OpalSample::initialize(const std::string &dvarName,
int seed = Attributes::getReal(itsAttr[SEED]);
size_m = Attributes::getReal(itsAttr[N]);
double step = Attributes::getReal(itsAttr[STEP]);
bool random = Attributes::getBool(itsAttr[RANDOM]);
......@@ -143,6 +149,26 @@ void OpalSample::initialize(const std::string &dvarName,
} else {
sampleMethod_m.reset( new LatinHyperCube(lower, upper) );
}
} else if (type == "RANDOM_SEQUENCE_UNIFORM_INT") {
if (Attributes::getReal(itsAttr[SEED])) {
sampleMethod_m.reset(
new SampleRandomizedSequence<int>(lower, upper, step, seed)
);
} else {
sampleMethod_m.reset(
new SampleRandomizedSequence<int>(lower, upper, step)
);
}
} else if (type == "RANDOM_SEQUENCE_UNIFORM") {
if (Attributes::getReal(itsAttr[SEED])) {
sampleMethod_m.reset(
new SampleRandomizedSequence<double>(lower, upper, step, seed)
);
} else {
sampleMethod_m.reset(
new SampleRandomizedSequence<double>(lower, upper, step)
);
}
} else {
throw OpalException("OpalSample::initialize()",
"Unknown sampling method: '" + type + "'.");
......@@ -153,4 +179,4 @@ void OpalSample::initialize(const std::string &dvarName,
std::string OpalSample::getVariable() const {
return Attributes::getString(itsAttr[VARIABLE]);
}
\ No newline at end of file
}
#ifndef OPAL_SAMPLE_WEIGHTED_SEQUENCE_H
#define OPAL_SAMPLE_WEIGHTED_SEQUENCE_H
#include "Sample/Uniform.h"
template <typename T>
class SampleRandomizedSequence : public SamplingMethod
{
public:
SampleRandomizedSequence(T lower, T upper, double step)
: unif_m(0, size_t((upper - lower) / step))
, lower_m(lower)
, step_m(step)
{ }
SampleRandomizedSequence(T lower, T upper, double step, size_t seed)
: unif_m(0, size_t((upper - lower) / step), seed)
, lower_m(lower)
, step_m(step)
{ }
void create(boost::shared_ptr<SampleIndividual>& ind, size_t i) {
size_t idx = unif_m.getNext();
ind->genes[i] = static_cast<T>(lower_m + idx * step_m);
}
void allocate(const CmdArguments_t& args, const Comm::Bundle_t& comm) {
unif_m.allocate(args, comm);
}
private:
std::vector<T> points_m;
Uniform<size_t> unif_m;
T lower_m;
double step_m;
};
#endif
......@@ -37,4 +37,4 @@ private:
size_t volumeLowerDimensions_m; // the "volume" of the sampling space of the lower "dimensions"
};
#endif
\ No newline at end of file
#endif
......@@ -43,6 +43,10 @@ public:
RNGInstance_m = RNGStream::getInstance(seed_m + comm.island_id);
}
T getNext() {
return RNGInstance_m->getNext(dist_m);
}
private:
dist_t dist_m;
......@@ -51,4 +55,4 @@ private:
std::size_t seed_m;
};
#endif
\ No newline at end of file
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment