summaryrefslogtreecommitdiff
path: root/impl/EquationSystem.hpp
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@carlo-laptop>2012-04-20 01:19:54 +1000
committerCarlo Zancanaro <carlo@carlo-laptop>2012-04-20 01:19:54 +1000
commita9de4273bf8377aeab7d3f76a6d8bc0c89b42f79 (patch)
treeb49e84610e194047b77620afb28f864639b3176b /impl/EquationSystem.hpp
parent2ee29b3426d2d79872fba35adbeb8d768983ffec (diff)
Start on the max-strategy stuff. Also more BF.
Diffstat (limited to 'impl/EquationSystem.hpp')
-rw-r--r--impl/EquationSystem.hpp64
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