forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov_chain.cpp
46 lines (39 loc) · 1005 Bytes
/
markov_chain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "stdafx.h"
#include "markov_chain.h"
#include "debug.h"
#include "enums.h"
#include "util.h"
template<class T>
MarkovChain<T>::MarkovChain(T s, map<T, vector<pair<T, double>>> t) : state(s), transitions(t) {
for (auto elem : transitions) {
double sum = 0;
for (auto trans : elem.second) {
sum += trans.second;
CHECK(trans.first != elem.first);
}
CHECK(sum <= 1);
if (sum < 1)
transitions[elem.first].emplace_back(elem.first, 1 - sum);
}
}
template<class T>
T MarkovChain<T>::getState() const {
return state;
}
template<class T>
void MarkovChain<T>::setState(T s) {
state = s;
}
template<class T>
void MarkovChain<T>::update() {
state = chooseRandom(transitions.at(state));
}
template<class T>
bool MarkovChain<T>::updateToNext() {
const vector<pair<T, double>> t = transitions.at(state);
if (t.size() == 1)
return false;
state = chooseRandom(getPrefix(t, 0, t.size() - 1));
return true;
}
template class MarkovChain<MinionTask>;