diff options
Diffstat (limited to 'impl/EquationSystem.hpp')
-rw-r--r-- | impl/EquationSystem.hpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/impl/EquationSystem.hpp b/impl/EquationSystem.hpp new file mode 100644 index 0000000..efd34b3 --- /dev/null +++ b/impl/EquationSystem.hpp @@ -0,0 +1,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 |