started removing KLib related code:

- assertions
- distributions
new helper methods
worked on stairs
worked on grid-walkers
worked on navigation
This commit is contained in:
2016-01-27 20:03:58 +01:00
parent e6329e1db4
commit 0e05f4bef8
26 changed files with 408 additions and 109 deletions

8
math/Distributions.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef DISTRIBUTIONS_H
#define DISTRIBUTIONS_H
#include "distribution/Normal.h"
#include "distribution/Exponential.h"
#include "distribution/Logistic.h"
#endif // DISTRIBUTIONS_H

View File

@@ -4,7 +4,7 @@
#include <vector>
#include <random>
#include <KLib/Assertions.h>
#include "../Assertions.h"
/**
* add elements of a certain probability
@@ -72,7 +72,7 @@ public:
const auto tmp = std::lower_bound(elements.begin(), elements.end(), rndVal);
// sanity check
_assertFalse(tmp == elements.end(), "draw() did not find a valid element");
Assert::isFalse(tmp == elements.end(), "draw() did not find a valid element");
// done
return (*tmp).element;

View File

@@ -0,0 +1,4 @@
#ifndef EXPONENTIAL_H
#define EXPONENTIAL_H
#endif // EXPONENTIAL_H

View File

@@ -0,0 +1,54 @@
#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

View File

@@ -0,0 +1,22 @@
#ifndef LOGISTIC_H
#define LOGISTIC_H
namespace Distribution {
// https://de.wikipedia.org/wiki/Logistische_Verteilung
template <typename T> class Logistic {
public:
/** alpha = move the center, beta = slope */
static T getCDF(const T x, const T alpha, const T beta) {
return 1 / (1 + std::exp( -((x-alpha)/beta) ) );
}
};
}
#endif // LOGISTIC_H

View File

@@ -0,0 +1,57 @@
#ifndef NORMAL_H
#define NORMAL_H
#include <cmath>
#include <random>
namespace Distribution {
/** normal distribution */
template <typename T> class Normal {
private:
const T mu;
const T sigma;
const T _a;
std::minstd_rand gen;
std::normal_distribution<T> dist;
public:
/** ctor */
Normal(const T mu, const T sigma) :
mu(mu), sigma(sigma), _a(1.0 / (sigma * std::sqrt(2.0 * M_PI))), gen(1234), dist(mu,sigma) {
}
/** get probability for the given value */
T getProbability(const T val) const {
const T b = -0.5 * ((val-mu)/sigma) * ((val-mu)/sigma);
return _a * std::exp(b);
}
/** 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 T getProbability(const T mu, const T sigma, const T val) {
const T a = 1.0 / (sigma * std::sqrt(2.0 * M_PI));
const T b = -0.5 * ((val-mu)/sigma) * ((val-mu)/sigma);
return a * std::exp(b);
}
};
}
#endif // NORMAL_H