summaryrefslogtreecommitdiff
path: root/impl/main.cpp
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@carlo-laptop>2012-04-26 22:32:35 +1000
committerCarlo Zancanaro <carlo@carlo-laptop>2012-04-26 22:32:35 +1000
commit76a4f0fcf3a9bf54ef910cdb2c0bebea37182391 (patch)
tree86ebaa6f86b53557d92e09ea1a74d2812da01df4 /impl/main.cpp
parent63a10756032fa5c677787fba209706b8bf1e4bef (diff)
A new attempt. Better strategies implementation.
Still lacking: - Factories (to set the ids) - Solver - Systems of equations
Diffstat (limited to 'impl/main.cpp')
-rw-r--r--impl/main.cpp188
1 files changed, 28 insertions, 160 deletions
diff --git a/impl/main.cpp b/impl/main.cpp
index 5b73068..589a1e7 100644
--- a/impl/main.cpp
+++ b/impl/main.cpp
@@ -1,172 +1,40 @@
#include <iostream>
#include <vector>
#include <math.h>
-#include "Expression.hpp"
+
#include "Operator.hpp"
-#include "EquationSystem.hpp"
-#include "ExpressionFactory.hpp"
+#include "Expression.hpp"
+#include "MaxStrategy.hpp"
using namespace std;
-struct Variable : public Operator<float> {
- Variable(const std::string& x)
- : _value(x) { }
- const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const {
- //assert(v.size() == 0);
- return m.find(_value)->second;
- }
- virtual std::string output() const {
- return "var";
- }
- private:
- std::string _value;
-};
-
-struct Const : public Operator<float> {
- Const(const float& x)
- : _value(x) { }
- const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const {
- //assert(v.size() == 0);
- return _value;
- }
- virtual std::string output() const {
- return "const";
- }
- private:
- float _value;
-};
-
-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].eval(m);
- }
- virtual std::string output() const {
- return "+()";
- }
- private:
- float _value;
-};
-
-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);
- return _value * v[0].eval(m);
- }
- virtual std::string output() const {
- return "*()";
- }
+/*template<typename T>
+struct Complete {
+ Complete(const T& value)
+ : _value(value) { }
private:
- float _value;
-};
-
-ExpressionFactory<float> f;
-
-Expression<float> constant(float x) {
- return f.createExpression(new Const(x), vector<Expression<float> >());
-}
-Expression<float> variable(string x) {
- 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 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 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 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;
-
-}
+ bool _infinity;
+ T _value;
+};*/
+std::vector< Expression<float>* > empty;
int main () {
- tests();
- /*typedef Expression<float> E;
- typedef vector<E> Args;
- Args empty;
-
- 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;*/
+ Variable<float> x;
+ x.id(0);
+ Variable<float> y;
+ y.id(1);
+ VariableAssignment<float> rho(2);
+ rho[x] = 12;
+ rho[y] = 10;
+ Expression<float> expr(&x, empty);
+
+ std::vector<Expression<float>*> args;
+ args.push_back(new Expression<float>(&x, empty));
+ args.push_back(new Expression<float>(&y, empty));
+ MaxExpression<float> maxExpr(args);
+ maxExpr.id(0);
+ MaxStrategy<float> strat(1);
+ strat[maxExpr] = 0;
+ cout << strat(maxExpr,rho) << endl;
return 0;
}