#include #include #include #include "Expression.hpp" #include "Operator.hpp" #include "EquationSystem.hpp" using namespace std; struct Variable : public Operator { Variable(const std::string& x) : _value(x) { } const float eval(const vector >& v, const map& m) const { //assert(v.size() == 0); return m.find(_value)->second; } private: std::string _value; }; struct Const : public Operator { Const(const float& x) : _value(x) { } const float eval(const vector >& v, const map& m) const { //assert(v.size() == 0); return _value; } private: float _value; }; struct Plus : public Operator { Plus(const float& x) : _value(x) { } const float eval(const vector >& v, const map& m) const { //assert(v.size() == 1); return _value + v[0].eval(m); } private: float _value; }; struct Mult : public Operator { Mult(const float& x) : _value(x) { } const float eval(const vector >& v, const map& m) const { //assert(v.size() == 1); return _value * v[0].eval(m); } private: float _value; }; Expression constant(float x) { return Expression(new Const(x), vector >()); } Expression variable(string x) { return Expression(new Variable(x), vector >()); } Expression add(float a, Expression b) { vector > args; args.push_back(b); return Expression(new Plus(a), args); } Expression Max(Expression a, Expression b) { vector > args; args.push_back(a); args.push_back(b); return Expression(new Maximum(), args); } Expression Min(Expression a, Expression b) { vector > args; args.push_back(a); args.push_back(b); return Expression(new Minimum(), args); } int main () { typedef Expression E; typedef vector Args; Args empty; EquationSystem 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 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(), xArgs)); Args yArgs; yArgs.push_back(E(new Variable("x"), empty)); test.add("y", E(new Mult(10), yArgs)); map m = test.solveBF(-INFINITY); cout << m["x"] << endl; cout << m["y"] << endl; map assignment; assignment["x"] = -INFINITY; assignment["y"] = -INFINITY; map result = test.maxStrategy(assignment).solveBF(INFINITY); cout << result["x"] << endl; cout << result["y"] << endl; assignment["x"] = 12; cout << E(new Maximum(), xArgs).maxStrategyEager(assignment).eval(assignment) << endl;*/ return 0; }