added interface for walkers
some new helper methods added interpolater for paths
This commit is contained in:
52
math/Interpolator.h
Normal file
52
math/Interpolator.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef INTERPOLATOR_H
|
||||
#define INTERPOLATOR_H
|
||||
|
||||
#include "../Assertions.h"
|
||||
|
||||
/**
|
||||
* interpolate between two adjacent values based on their key
|
||||
*/
|
||||
template <typename Key, typename Value> class Interpolator {
|
||||
|
||||
public:
|
||||
|
||||
/** value at key */
|
||||
struct Entry {
|
||||
Key key;
|
||||
Value value;
|
||||
Entry(const Key key, const Value& pos) : key(key), value(value) {;}
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
/** all added entries, SORTED by their key */
|
||||
std::vector<Entry> entries;
|
||||
|
||||
public:
|
||||
|
||||
/** add a new value with its key. entries must be added in sorted order! */
|
||||
void add(const Key key, const Value& value) {
|
||||
Assert::isTrue (entries.empty() || entries.back().key < key, "entries must be ordered!");
|
||||
entries.push_back(Entry(key, value));
|
||||
}
|
||||
|
||||
/** get the interpolated value for the given key */
|
||||
Value get(const Key key) const {
|
||||
const int idx1 = getIdx(key);
|
||||
const int idx2 = idx1+1;
|
||||
const float percent = (key - entries[idx1].key) / (float) (entries[idx2].key - entries[idx1].key);
|
||||
return entries[idx1].value + (entries[idx2].value - entries[idx1].value) * percent;
|
||||
}
|
||||
|
||||
/** get the nearest index for the given key */
|
||||
int getIdx(const Key key) const {
|
||||
// TODO: use O(log(n)) search here!
|
||||
for (size_t i = 0; i < entries.size(); ++i) {
|
||||
if (entries[i].key > key) {return i-1;}
|
||||
}
|
||||
throw "should not happen";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // INTERPOLATOR_H
|
||||
@@ -1,54 +0,0 @@
|
||||
#ifndef EXPONENTIAL_H
|
||||
#define EXPONENTIAL_H
|
||||
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
|
||||
namespace Distribution {
|
||||
|
||||
template <typename T> class Exponential {
|
||||
|
||||
private:
|
||||
|
||||
T lambda;
|
||||
|
||||
std::minstd_rand gen;
|
||||
std::exponential_distribution<T> dist;
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
Exponential(const T lambda) : lambda(lambda), gen(1234), dist(lambda) {
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
/** get probability for the given value */
|
||||
T getProbability(const T val) const {
|
||||
return lambda * std::exp(-lambda * val);
|
||||
}
|
||||
|
||||
/** get a normally distributed random number */
|
||||
T draw() {
|
||||
return dist(gen);
|
||||
}
|
||||
|
||||
/** set the seed to use */
|
||||
void setSeed(const long seed) {
|
||||
gen.seed(seed);
|
||||
}
|
||||
|
||||
|
||||
/** get the probability for the given value */
|
||||
static double getProbability(const double lambda, const double val) {
|
||||
return lambda * std::exp(-lambda * val);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // EXPONENTIAL_H
|
||||
Reference in New Issue
Block a user