Skip to content

Commit

Permalink
Added consts to root and POMDP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Svalorzen committed Nov 20, 2016
1 parent c740e4e commit 722826b
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 164 deletions.
4 changes: 2 additions & 2 deletions include/AIToolbox/MDP/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ namespace AIToolbox {

template <typename M, typename std::enable_if<is_model<M>::value, int>::type>
Model::Model(const M& model) :
S(model.getS()), A(model.getA()), transitions_(A, Matrix2D(S, S)),
rewards_(A, Matrix2D(S, S)), rand_(Impl::Seeder::getSeed())
S(model.getS()), A(model.getA()), transitions_(A, Matrix2D(S, S)),
rewards_(A, Matrix2D(S, S)), rand_(Impl::Seeder::getSeed())
{
setDiscount(model.getDiscount());
for ( size_t a = 0; a < A; ++a )
Expand Down
42 changes: 21 additions & 21 deletions include/AIToolbox/POMDP/Algorithms/AMDP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,30 @@ namespace AIToolbox {

template <typename M, typename>
std::tuple<MDP::Model, AMDP::Discretizer> AMDP::discretizeDense(const M& model) {
size_t S = model.getS(), A = model.getA(), O = model.getO();
size_t S1 = S * buckets_;
const size_t S = model.getS(), A = model.getA(), O = model.getO();
const size_t S1 = S * buckets_;

BeliefGenerator<M> bGen(model);
auto beliefs = bGen(beliefSize_);
const auto beliefs = bGen(beliefSize_);

auto T = MDP::Model::TransitionTable (A, Matrix2D::Zero(S1, S1));
auto R = MDP::Model::RewardTable (A, Matrix2D::Zero(S1, S1));

auto discretizer = makeDiscretizer(S);
const auto discretizer = makeDiscretizer(S);

Belief b1(S);
for ( auto & b : beliefs ) {
size_t s = discretizer(b);
for ( const auto & b : beliefs ) {
const size_t s = discretizer(b);

for ( size_t a = 0; a < A; ++a ) {
double r = beliefExpectedReward(model, b, a);
const double r = beliefExpectedReward(model, b, a);

for ( size_t o = 0; o < O; ++o ) {
updateBeliefUnnormalized(model, b, a, o, &b1);
auto p = b1.sum();
const auto p = b1.sum();
if (checkDifferentSmall(0.0, p)) {
b1 /= p;
size_t s1 = discretizer(b1);
const size_t s1 = discretizer(b1);

T[a](s, s1) += p;
R[a](s, s1) += p * r;
Expand All @@ -146,40 +146,40 @@ namespace AIToolbox {
if ( checkDifferentSmall(0.0, T[a](s, s1)) )
R[a](s, s1) /= T[a](s, s1);
}
double sum = T[a].row(s).sum();
const double sum = T[a].row(s).sum();
if ( checkEqualSmall(sum, 0.0) ) T[a](s, s) = 1.0;
else T[a].row(s) /= sum;
}

return std::make_tuple(MDP::Model::makeFromTrustedData(S1, A, std::move(T), std::move(R), model.getDiscount()), discretizer);
return std::make_tuple(MDP::Model::makeFromTrustedData(S1, A, std::move(T), std::move(R), model.getDiscount()), std::move(discretizer));
}

template <typename M, typename>
std::tuple<MDP::SparseModel, AMDP::Discretizer> AMDP::discretizeSparse(const M& model) {
size_t S = model.getS(), A = model.getA(), O = model.getO();
size_t S1 = S * buckets_;
const size_t S = model.getS(), A = model.getA(), O = model.getO();
const size_t S1 = S * buckets_;

BeliefGenerator<M> bGen(model);
auto beliefs = bGen(beliefSize_);
const auto beliefs = bGen(beliefSize_);

auto T = MDP::SparseModel::TransitionTable (A, SparseMatrix2D(S1, S1));
auto R = MDP::SparseModel::RewardTable (A, SparseMatrix2D(S1, S1));

auto discretizer = makeDiscretizer(S);

Belief b1(S);
for ( auto & b : beliefs ) {
size_t s = discretizer(b);
for ( const auto & b : beliefs ) {
const size_t s = discretizer(b);

for ( size_t a = 0; a < A; ++a ) {
double r = beliefExpectedReward(model, b, a);
const double r = beliefExpectedReward(model, b, a);

for ( size_t o = 0; o < O; ++o ) {
updateBeliefUnnormalized(model, b, a, o, &b1);
auto p = b1.sum();
const auto p = b1.sum();
if (checkDifferentSmall(0.0, p)) {
b1 /= p;
size_t s1 = discretizer(b1);
const size_t s1 = discretizer(b1);

T[a].coeffRef(s, s1) += p;
R[a].coeffRef(s, s1) += p * r;
Expand All @@ -194,12 +194,12 @@ namespace AIToolbox {
if ( checkDifferentSmall(0.0, T[a].coeff(s, s1)) )
R[a].coeffRef(s, s1) /= T[a].coeff(s, s1);
}
double sum = T[a].row(s).sum();
const double sum = T[a].row(s).sum();
if ( checkEqualSmall(sum, 0.0) ) T[a].coeffRef(s, s) = 1.0;
else T[a].row(s) /= sum;
}

return std::make_tuple(MDP::SparseModel::makeFromTrustedData(S1, A, std::move(T), std::move(R), model.getDiscount()), discretizer);
return std::make_tuple(MDP::SparseModel::makeFromTrustedData(S1, A, std::move(T), std::move(R), model.getDiscount()), std::move(discretizer));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions include/AIToolbox/POMDP/Algorithms/IncrementalPruning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ namespace AIToolbox {
Pruner<WitnessLP_lpsolve> prune(S);
Projecter<M> projecter(model);

bool useEpsilon = checkDifferentSmall(epsilon_, 0.0);
const bool useEpsilon = checkDifferentSmall(epsilon_, 0.0);
double variation = epsilon_ * 2; // Make it bigger
while ( timestep < horizon_ && ( !useEpsilon || variation > epsilon_ ) ) {
++timestep;
Expand Down Expand Up @@ -203,9 +203,9 @@ namespace AIToolbox {
--elements;
}

bool oddNew = elements % 2;
const bool oddNew = elements % 2;

int tmp = back;
const int tmp = back;
back = front - ( oddNew ? 0 : stepsize );
front = tmp - ( oddOld ? 0 : stepsize );
stepsize *= -2;
Expand Down
8 changes: 4 additions & 4 deletions include/AIToolbox/POMDP/Algorithms/PBVI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ namespace AIToolbox {
// can be called multiple times to increase the size of the belief
// vector.
BeliefGenerator<M> bGen(model);
auto beliefs = bGen(beliefSize_);
const auto beliefs = bGen(beliefSize_);

ValueFunction v(1, VList(1, makeVEntry(S)));

Expand All @@ -186,7 +186,7 @@ namespace AIToolbox {
Projecter<M> projecter(model);

// And off we go
bool useEpsilon = checkDifferentSmall(epsilon_, 0.0);
const bool useEpsilon = checkDifferentSmall(epsilon_, 0.0);
double variation = epsilon_ * 2; // Make it bigger
while ( timestep < horizon_ && ( !useEpsilon || variation > epsilon_ ) ) {
++timestep;
Expand All @@ -213,7 +213,7 @@ namespace AIToolbox {
std::move(std::begin(projs[a][0]), std::end(projs[a][0]), std::back_inserter(w));

auto begin = std::begin(w), bound = begin, end = std::end(w);
for ( auto & belief : beliefs )
for ( const auto & belief : beliefs )
bound = extractWorstAtBelief(belief, begin, bound, end);

w.erase(bound, end);
Expand All @@ -235,7 +235,7 @@ namespace AIToolbox {
}

template <typename ProjectionsRow>
VList PBVI::crossSum(const ProjectionsRow & projs, size_t a, const std::vector<Belief> & bl) {
VList PBVI::crossSum(const ProjectionsRow & projs, const size_t a, const std::vector<Belief> & bl) {
VList result;
result.reserve(bl.size());

Expand Down
10 changes: 5 additions & 5 deletions include/AIToolbox/POMDP/Algorithms/PERSEUS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace AIToolbox {
};

template <typename M, typename std::enable_if<is_model<M>::value, int>::type>
std::tuple<bool, ValueFunction> PERSEUS::operator()(const M & model, double minReward) {
std::tuple<bool, ValueFunction> PERSEUS::operator()(const M & model, const double minReward) {
if ( model.getDiscount() == 1 ) throw std::invalid_argument("The model cannot have a discount of 1 in PERSEUS!");
// Initialize "global" variables
S = model.getS();
Expand All @@ -169,7 +169,7 @@ namespace AIToolbox {
// can be called multiple times to increase the size of the belief
// vector.
BeliefGenerator<M> bGen(model);
auto beliefs = bGen(beliefSize_);
const auto beliefs = bGen(beliefSize_);

// We initialize the ValueFunction to the "worst" case scenario.
ValueFunction v(1, VList(1, std::make_tuple(MDP::Values(S), 0, VObs(0))));
Expand All @@ -180,15 +180,15 @@ namespace AIToolbox {
Projecter<M> projecter(model);

// And off we go
bool useEpsilon = checkDifferentSmall(epsilon_, 0.0);
const bool useEpsilon = checkDifferentSmall(epsilon_, 0.0);
double variation = epsilon_ * 2; // Make it bigger
while ( timestep < horizon_ && ( !useEpsilon || variation > epsilon_ ) ) {
++timestep;
// Compute all possible outcomes, from our previous results.
// This means that for each action-observation pair, we are going
// to obtain the same number of possible outcomes as the number
// of entries in our initial vector w.
auto projs = projecter(v[timestep-1]);
const auto projs = projecter(v[timestep-1]);
// Here we find the minimum number of VEntries that we need to improve
// v on all beliefs from v[timestep-1].
v.emplace_back( crossSum( projs, beliefs, v[timestep-1] ) );
Expand All @@ -210,7 +210,7 @@ namespace AIToolbox {
bool start = true;
double currentValue, oldValue;

for ( auto & b : bl ) {
for ( const auto & b : bl ) {
if ( !start ) {
// If we have already improved this belief, skip it
findBestAtBelief( b, std::begin(result), std::end(result), &currentValue );
Expand Down
29 changes: 15 additions & 14 deletions include/AIToolbox/POMDP/Algorithms/POMCP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,12 @@ namespace AIToolbox {
};

template <typename M>
POMCP<M>::POMCP(const M& m, size_t beliefSize, unsigned iter, double exp) : model_(m), S(model_.getS()), A(model_.getA()), beliefSize_(beliefSize), iterations_(iter),
exploration_(exp), graph_(), rand_(Impl::Seeder::getSeed()) {}
POMCP<M>::POMCP(const M& m, const size_t beliefSize, const unsigned iter, const double exp) :
model_(m), S(model_.getS()), A(model_.getA()), beliefSize_(beliefSize),
iterations_(iter), exploration_(exp), graph_(), rand_(Impl::Seeder::getSeed()) {}

template <typename M>
size_t POMCP<M>::sampleAction(const Belief& b, unsigned horizon) {
size_t POMCP<M>::sampleAction(const Belief& b, const unsigned horizon) {
// Reset graph
graph_ = BeliefNode(A);
graph_.children.resize(A);
Expand All @@ -333,8 +334,8 @@ namespace AIToolbox {
}

template <typename M>
size_t POMCP<M>::sampleAction(size_t a, size_t o, unsigned horizon) {
auto & obs = graph_.children[a].children;
size_t POMCP<M>::sampleAction(const size_t a, const size_t o, const unsigned horizon) {
const auto & obs = graph_.children[a].children;

auto it = obs.find(o);
if ( it == obs.end() ) {
Expand Down Expand Up @@ -365,7 +366,7 @@ namespace AIToolbox {
}

template <typename M>
size_t POMCP<M>::runSimulation(unsigned horizon) {
size_t POMCP<M>::runSimulation(const unsigned horizon) {
if ( !horizon ) return 0;

maxDepth_ = horizon;
Expand All @@ -379,11 +380,11 @@ namespace AIToolbox {
}

template <typename M>
double POMCP<M>::simulate(BeliefNode & b, size_t s, unsigned depth) {
double POMCP<M>::simulate(BeliefNode & b, const size_t s, const unsigned depth) {
b.N++;

auto begin = std::begin(b.children);
size_t a = std::distance(begin, findBestBonusA(begin, std::end(b.children), b.N));
const size_t a = std::distance(begin, findBestBonusA(begin, std::end(b.children), b.N));

size_t s1, o; double rew;
std::tie(s1, o, rew) = model_.sampleSOR(s, a);
Expand Down Expand Up @@ -448,10 +449,10 @@ namespace AIToolbox {

template <typename M>
template <typename Iterator>
Iterator POMCP<M>::findBestBonusA(Iterator begin, Iterator end, unsigned count) {
Iterator POMCP<M>::findBestBonusA(Iterator begin, Iterator end, const unsigned count) {
// Count here can be as low as 1.
// Since log(1) = 0, and 0/0 = error, we add 1.0.
double logCount = std::log(count + 1.0);
const double logCount = std::log(count + 1.0);
// We use this function to produce a score for each action. This can be easily
// substituted with something else to produce different POMCP variants.
auto evaluationFunction = [this, logCount](const ActionNode & an){
Expand All @@ -462,7 +463,7 @@ namespace AIToolbox {
double bestValue = evaluationFunction(*bestIterator);

for ( ; begin < end; ++begin ) {
double actionValue = evaluationFunction(*begin);
const double actionValue = evaluationFunction(*begin);
if ( actionValue > bestValue ) {
bestValue = actionValue;
bestIterator = begin;
Expand All @@ -484,17 +485,17 @@ namespace AIToolbox {
}

template <typename M>
void POMCP<M>::setBeliefSize(size_t beliefSize) {
void POMCP<M>::setBeliefSize(const size_t beliefSize) {
beliefSize_ = beliefSize;
}

template <typename M>
void POMCP<M>::setIterations(unsigned iter) {
void POMCP<M>::setIterations(const unsigned iter) {
iterations_ = iter;
}

template <typename M>
void POMCP<M>::setExploration(double exp) {
void POMCP<M>::setExploration(const double exp) {
exploration_ = exp;
}

Expand Down
15 changes: 8 additions & 7 deletions include/AIToolbox/POMDP/Algorithms/QMDP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ namespace AIToolbox {
};

template <typename M>
QMDP<M>::QMDP(unsigned horizon, double epsilon) : solver_(horizon, epsilon) {}
QMDP<M>::QMDP(const unsigned horizon, const double epsilon) :
solver_(horizon, epsilon) {}

template <typename M>
std::tuple<bool, ValueFunction, MDP::ValueFunction> QMDP<M>::operator()(const M & m) {
auto solution = solver_(m);
auto & mdpValueFunction = std::get<1>(solution);
auto & mdpValues = std::get<MDP::VALUES >(mdpValueFunction);
auto & mdpActions = std::get<MDP::ACTIONS>(mdpValueFunction);
const auto & mdpValues = std::get<MDP::VALUES >(mdpValueFunction);
const auto & mdpActions = std::get<MDP::ACTIONS>(mdpValueFunction);

size_t S = m.getS();
const size_t S = m.getS();

VList w;
w.reserve(S);
Expand All @@ -149,16 +150,16 @@ namespace AIToolbox {
ValueFunction vf(1, VList(1, makeVEntry(S)));
vf.emplace_back(std::move(w));

return std::make_tuple(std::get<0>(solution), vf, mdpValueFunction);
return std::make_tuple(std::get<0>(solution), vf, std::move(mdpValueFunction));
}

template <typename M>
void QMDP<M>::setEpsilon(double e) {
void QMDP<M>::setEpsilon(const double e) {
solver_.setEpsilon(e);
}

template <typename M>
void QMDP<M>::setHorizon(unsigned h) {
void QMDP<M>::setHorizon(const unsigned h) {
solver_.setHorizon(h);
}

Expand Down
14 changes: 8 additions & 6 deletions include/AIToolbox/POMDP/Algorithms/RTBSS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ namespace AIToolbox {
};

template <typename M>
RTBSS<M>::RTBSS(const M& m, double maxR) : model_(m), S(model_.getS()), A(model_.getA()), O(model_.getO()), maxR_(maxR) {}
RTBSS<M>::RTBSS(const M& m, const double maxR) :
model_(m), S(model_.getS()), A(model_.getA()),
O(model_.getO()), maxR_(maxR) {}

template <typename M>
std::tuple<size_t, double> RTBSS<M>::sampleAction(const Belief& b, unsigned horizon) {
std::tuple<size_t, double> RTBSS<M>::sampleAction(const Belief& b, const unsigned horizon) {
maxA_ = 0; maxDepth_ = horizon;

double value = simulate(b, horizon);
Expand All @@ -124,7 +126,7 @@ namespace AIToolbox {
}

template <typename M>
double RTBSS<M>::simulate(const Belief & b, unsigned horizon) {
double RTBSS<M>::simulate(const Belief & b, const unsigned horizon) {
if ( horizon == 0 ) return 0;

std::vector<size_t> actionList(A);
Expand All @@ -138,10 +140,10 @@ namespace AIToolbox {
for ( auto a : actionList ) {
double rew = beliefExpectedReward(model_, b, a);

double uBound = rew + upperBound(b, a, horizon - 1);
const double uBound = rew + upperBound(b, a, horizon - 1);
if ( uBound > max ) {
for ( size_t o = 0; o < O; ++o ) {
double p = beliefObservationProbability(model_, b, a, o);
const double p = beliefObservationProbability(model_, b, a, o);
// Only work if it makes sense
if ( checkDifferentSmall(p, 0.0) ) rew += model_.getDiscount() * p * simulate(updateBelief(model_, b, a, o), horizon - 1);
}
Expand All @@ -155,7 +157,7 @@ namespace AIToolbox {
}

template <typename M>
double RTBSS<M>::upperBound(const Belief &, size_t, unsigned horizon) const {
double RTBSS<M>::upperBound(const Belief &, const size_t, const unsigned horizon) const {
return model_.getDiscount() * maxR_ * horizon;
}

Expand Down
Loading

0 comments on commit 722826b

Please sign in to comment.