summaryrefslogtreecommitdiff
path: root/impl/main.cpp
blob: c048c5bd1b04ad91b93fdc93bd57bd56f175100c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <vector>
#include <math.h>
#include "Expression.hpp"
#include "Operator.hpp"
#include "EquationSystem.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;
  }
  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;
  }
  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);
  }
  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);
  }
  private:
  float _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;
  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;*/
  return 0;
}