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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
#include <iostream>
#include <vector>
#include <math.h>
#include "Expression.hpp"
#include "Operator.hpp"
#include "EquationSystem.hpp"
#include "ExpressionFactory.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 "*()";
}
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;
}
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;*/
return 0;
}
|