diff options
Diffstat (limited to 'impl/main.cpp')
-rw-r--r-- | impl/main.cpp | 133 |
1 files changed, 86 insertions, 47 deletions
diff --git a/impl/main.cpp b/impl/main.cpp index dc6cf45..c048c5b 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -1,8 +1,9 @@ #include <iostream> #include <vector> #include <math.h> -#include "Expression.h" -#include "Operator.h" +#include "Expression.hpp" +#include "Operator.hpp" +#include "EquationSystem.hpp" using namespace std; @@ -11,8 +12,7 @@ struct Variable : public Operator<float> { : _value(x) { } const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { //assert(v.size() == 0); - map<string, float>::const_iterator it = m.find(_value); - return (it == m.end() ? -INFINITY : it->second); + return m.find(_value)->second; } private: std::string _value; @@ -29,67 +29,106 @@ struct Const : public Operator<float> { float _value; }; -struct Mult : public Operator<float> { - Mult(const float& x) +struct Plus : public Operator<float> { + Plus(const float& x) : _value(x) { } const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { //assert(v.size() == 1); - return _value * v[0].minvalue(m); + return _value + v[0].eval(m); } private: float _value; }; -struct Maximum : public Operator<float> { +struct Mult : public Operator<float> { + Mult(const float& x) + : _value(x) { } const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { //assert(v.size() == 1); - float value = -INFINITY; - for (vector<Expression<float> >::const_iterator it = v.begin(); - it != v.end(); - ++it) { - const float result = it->maxvalue(m); - value = (value < result ? result : value); - } - return value; + return _value * v[0].eval(m); } + private: + float _value; }; -struct Minimum : public Operator<float> { - const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { - //assert(v.size() == 1); - float value = INFINITY; - for (vector<Expression<float> >::const_iterator it = v.begin(); - it != v.end(); - ++it) { - const float result = it->maxvalue(m); - value = (value < result ? value : result); - } - return value; - } -}; +Expression<float> constant(float x) { + return Expression<float>(new Const(x), vector<Expression<float> >()); +} +Expression<float> variable(string x) { + return Expression<float>(new Variable(x), vector<Expression<float> >()); +} +Expression<float> add(float a, Expression<float> b) { + vector<Expression<float> > args; + args.push_back(b); + return Expression<float>(new Plus(a), args); +} +Expression<float> Max(Expression<float> a, Expression<float> b) { + vector<Expression<float> > args; + args.push_back(a); + args.push_back(b); + return Expression<float>(new Maximum<float>(), args); +} +Expression<float> Min(Expression<float> a, Expression<float> b) { + vector<Expression<float> > args; + args.push_back(a); + args.push_back(b); + return Expression<float>(new Minimum<float>(), args); +} int main () { typedef Expression<float> E; typedef vector<E> Args; - map<string, float> m; - //m["x"] = -10; Args empty; - { - Args a; - { - Args b; - b.push_back(E(new Const(10), empty)); - b.push_back(E(new Const(20), empty)); - b.push_back(E(new Variable("x"), empty)); - a.push_back(E(new Minimum(), b)); - cout << E(new Minimum(), b).minvalue(m) << endl; - } - } - /*map<string, float> m; - vector<Expression<float> > empty; - vector<Expression<float> > args; - args.push_back(Expression<float>(new Const(10), empty)); - cout << Expression<float>(new Mult(3), args).minvalue(m) << endl;*/ + EquationSystem<float> test; + + // problem: + // x = max(y, 10) + // y = 10 * x + + test.add("x1", Max(constant(0), + Min(add(-1, variable("x1")), + variable("x2")))); + test.add("x2", Max(constant(0), + Max(add(5, variable("x1")), + variable("x1")))); + test.add("x3", Max(constant(0), + Max(add(1, variable("x3")), + add(0, variable("x1"))))); + + map<string, float> m = test.solveBF(-INFINITY); + cout << m["x1"] << endl; + cout << m["x2"] << endl; + cout << m["x3"] << endl; + + m = test.maxStrategy(m).solveBF(-INFINITY); + cout << m["x1"] << endl; + cout << m["x2"] << endl; + cout << m["x3"] << endl; + + + /*Args xArgs; + xArgs.push_back(E(new Variable("x"), empty)); + xArgs.push_back(E(new Const(10), empty)); + test.add("x", E(new Maximum<float>(), xArgs)); + + Args yArgs; + yArgs.push_back(E(new Variable("x"), empty)); + test.add("y", E(new Mult(10), yArgs)); + + map<string, float> m = test.solveBF(-INFINITY); + cout << m["x"] << endl; + cout << m["y"] << endl; + + map<string,float> assignment; + assignment["x"] = -INFINITY; + assignment["y"] = -INFINITY; + map<string, float> result = test.maxStrategy(assignment).solveBF(INFINITY); + cout << result["x"] << endl; + cout << result["y"] << endl; + + + assignment["x"] = 12; + cout << E(new Maximum<float>(), xArgs).maxStrategyEager(assignment).eval(assignment) << endl;*/ return 0; } |