diff options
Diffstat (limited to 'impl/main.cpp')
-rw-r--r-- | impl/main.cpp | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/impl/main.cpp b/impl/main.cpp index c048c5b..5b73068 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -4,6 +4,7 @@ #include "Expression.hpp" #include "Operator.hpp" #include "EquationSystem.hpp" +#include "ExpressionFactory.hpp" using namespace std; @@ -14,6 +15,9 @@ struct Variable : public Operator<float> { //assert(v.size() == 0); return m.find(_value)->second; } + virtual std::string output() const { + return "var"; + } private: std::string _value; }; @@ -25,6 +29,9 @@ struct Const : public Operator<float> { //assert(v.size() == 0); return _value; } + virtual std::string output() const { + return "const"; + } private: float _value; }; @@ -36,6 +43,9 @@ struct Plus : public Operator<float> { //assert(v.size() == 1); return _value + v[0].eval(m); } + virtual std::string output() const { + return "+()"; + } private: float _value; }; @@ -47,36 +57,64 @@ struct Mult : public Operator<float> { //assert(v.size() == 1); return _value * v[0].eval(m); } + virtual std::string output() const { + return "*()"; + } private: float _value; }; +ExpressionFactory<float> f; + Expression<float> constant(float x) { - return Expression<float>(new Const(x), vector<Expression<float> >()); + return f.createExpression(new Const(x), vector<Expression<float> >()); } Expression<float> variable(string x) { - return Expression<float>(new Variable(x), vector<Expression<float> >()); + return f.createExpression(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); + return f.createExpression(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); + return f.createExpression(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); + return f.createExpression(new Minimum<float>(), args); +} + + +void tests() { + map<string,float> assignment; + assignment["x"] = -INFINITY; + + EquationSystem<float> test; + test.add("x", Max(constant(0), + Min(add(1, variable("x")), + constant(100)))); + + test = test.maxStrategy(assignment); + cout << test.output(); + assignment = test.solveBF(-INFINITY); + cout << assignment["x"] << endl; + + test = test.maxStrategy(assignment); + cout << test.output(); + assignment = test.solveBF(-INFINITY); + cout << assignment["x"] << endl; + } int main () { - typedef Expression<float> E; + tests(); + /*typedef Expression<float> E; typedef vector<E> Args; Args empty; @@ -104,7 +142,7 @@ int main () { m = test.maxStrategy(m).solveBF(-INFINITY); cout << m["x1"] << endl; cout << m["x2"] << endl; - cout << m["x3"] << endl; + cout << m["x3"] << endl;*/ /*Args xArgs; |