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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#ifndef EQUATION_SYSTEM_H
#define EQUATION_SYSTEM_H
#include <map>
#include <string>
#include "Expression.hpp"
template<typename T>
struct EquationSystem {
void add(std::string k, Expression<T> v) {
equations.insert(std::pair<std::string, Expression<T> >(k, v));
}
/**
init is the value where it STARTS, it's working the other way
if there's no stability then it ends with -init
*/
std::map<std::string, T> solveBF(const T& init) const {
std::map<std::string, T> solutions;
for (typename std::map<std::string, Expression<T> >::const_iterator it = equations.begin();
it != equations.end();
++it){
solutions[it->first] = init;
}
int num = equations.size();
for (int i = 0; i < num; ++i) {
solutions = eval(solutions, false, -init);
}
solutions = eval(solutions, true, -init);
for (int i = 0; i < num-1; ++i) {
solutions = eval(solutions, false, -init);
}
return solutions;
}
EquationSystem maxStrategy(const std::map<std::string, T>& assignment) {
EquationSystem result;
for (typename std::map<std::string, Expression<T> >::const_iterator it = equations.begin();
it != equations.end();
++it) {
result.add(it->first, it->second.maxStrategyEager(assignment));
}
return result;
}
private:
std::map<std::string, T> eval(const std::map<std::string, T>& assignment, bool infinity, const T& infValue) const {
std::map<std::string, T> result;
for (typename std::map<std::string, Expression<T> >::const_iterator it = equations.begin();
it != equations.end();
++it) {
T val = it->second.eval(assignment);
if (infinity) {
typename std::map<std::string, T>::const_iterator oldEntry = assignment.find(it->first);
result[it->first] = (oldEntry->second == val ? val : infValue);
} else {
result[it->first] = val;
}
}
return result;
}
std::map<std::string, Expression<T> > equations;
};
#endif
|