-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolic.cpp
More file actions
263 lines (213 loc) · 6.83 KB
/
Copy pathSymbolic.cpp
File metadata and controls
263 lines (213 loc) · 6.83 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#ifndef _LIB_SYMBOLIC
#define _LIB_SYMBOLIC
#include <bits/stdc++.h>
namespace lib {
using namespace std;
static int g_VAR_PTR = 0;
enum Operation { variable, literal, sum };
template <typename T> struct Variable;
template <typename T> struct BasicExp {
using node = shared_ptr<BasicExp<T>>;
using variable = Variable<T>;
T coef = 1;
Operation op;
vector<node> children;
variable var;
BasicExp(Operation n_op, const vector<node> &n_children, T n_coef = 1);
BasicExp(const T &v);
BasicExp(const Variable<T> &v);
bool has_children() const {
return op != Operation::variable && op != Operation::literal;
}
Variable<T> get_variable() const { return var; }
};
template <typename T> using Expression = shared_ptr<BasicExp<T>>;
template <typename T, typename... Args>
Expression<T> make_exp(Args &&... args) {
return make_shared<BasicExp<T>, Args...>(std::forward<Args>(args)...);
}
template <typename T> struct Variable {
int id;
static Variable<T> get_variable() { return {g_VAR_PTR++}; }
static vector<Variable<T>> get_variables(int n) {
vector<Variable<T>> vars(n);
for (int i = 0; i < n; i++)
vars[i] = get_variable();
return vars;
}
static Expression<T> get_exp_variable() {
return Variable<T>::get_variable().as_exp();
}
static vector<Expression<T>> get_exp_variables(int n) {
vector<Expression<T>> vs(n);
int i = 0;
for (const auto &v : Variable<T>::get_variables(n)) {
vs[i++] = v.as_exp();
}
return vs;
}
operator Expression<T>() const { return make_exp<T>(*this); }
Expression<T> as_exp() const { return Expression<T>(*this); }
bool operator<(const Variable<T> &rhs) const { return id < rhs.id; }
};
template <typename T>
BasicExp<T>::BasicExp(Operation n_op, const vector<node> &n_children, T n_coef)
: op(n_op), children(n_children), coef(n_coef) {}
template <typename T> BasicExp<T>::BasicExp(const T &v) {
op = Operation::literal;
coef = v;
}
template <typename T> BasicExp<T>::BasicExp(const Variable<T> &v) {
op = Operation::variable;
var = v;
}
template <typename T> Expression<T> &operator*=(Expression<T> &e, const T &x) {
e->coef *= x;
return e;
}
template <typename T>
Expression<T> operator*(const Expression<T> &e, const T &x) {
auto res = make_exp<T>(*e);
return res *= x;
}
template <typename T>
Expression<T> &operator+=(Expression<T> &e, const Expression<T> &rhs) {
if (e->op == Operation::sum) {
e->children.push_back(rhs);
} else {
e = make_exp<T>(Operation::sum, vector<Expression<T>>{e, rhs});
}
return e;
}
template <typename T>
Expression<T> &operator+=(Expression<T> &e, const Variable<T> &rhs) {
return e += make_exp<T>(rhs);
}
template <typename T> Expression<T> &operator+=(Expression<T> &e, const T &x) {
return e += make_exp<T>(x);
}
template <typename T>
Expression<T> operator+(const Expression<T> &e, const Expression<T> &rhs) {
auto res = e->op == Operation::sum ? make_exp<T>(*e) : e;
return res += rhs;
}
template <typename T>
Expression<T> operator+(const Expression<T> &e, const Variable<T> &rhs) {
return e + make_exp<T>(rhs);
}
template <typename T>
Expression<T> operator+(const Expression<T> &e, const T &x) {
return e + make_exp<T>(x);
}
template <typename T>
Expression<T> operator+(const Variable<T> &v, const Expression<T> &rhs) {
return make_exp<T>(v) + rhs;
}
template <typename T>
Expression<T> operator+(const Variable<T> &v, const Variable<T> &rhs) {
return make_exp<T>(v) + make_exp<T>(rhs);
}
template <typename T>
Expression<T> operator+(const Variable<T> &v, const T &x) {
return make_exp<T>(v) + make_exp<T>(x);
}
template <typename T>
Expression<T> operator*(const Variable<T> &v, const T &x) {
return make_exp<T>(v) * x;
}
template <typename T> struct ExpressionVisitor {
void visit(const Expression<T> &e) {
if (e->op == Operation::sum)
this->visit_sum(e);
else if (e->op == Operation::variable)
this->visit_variable(e);
else if (e->op == Operation::literal)
this->visit_literal(e);
}
virtual void visit_children(const Expression<T> &e) {
if (e->has_children()) {
for (const Expression<T> &child : e->children)
this->visit(child);
}
}
virtual void visit_sum(const Expression<T> &e) { this->visit_children(e); }
virtual void visit_variable(const Expression<T> &e) {}
virtual void visit_literal(const Expression<T> &e) {}
};
template <typename T> struct VariableVisitor : ExpressionVisitor<T> {
set<Variable<T>> seen;
virtual void visit_variable(const Expression<T> &e) { seen.insert(e->var); }
};
template <typename T, typename S = T>
struct StackVisitor : ExpressionVisitor<T> {
vector<S> sta;
virtual void visit_children(const Expression<T> &e) override {
sta.push_back(sta.empty() ? e->coef : sta.back() * e->coef);
ExpressionVisitor<T>::visit_children(e);
if (!sta.empty())
sta.pop_back();
}
S top() const { return sta.empty() ? S(1) : sta.back(); }
};
template <typename T> struct EvalVisitor : StackVisitor<T> {
map<Variable<T>, T> values;
T result;
T eval(const Expression<T> &e, const map<Variable<T>, T> &values) {
result = T();
this->values = values;
this->visit(e);
return result;
}
virtual void visit_variable(const Expression<T> &e) override {
result += this->top() * e->coef * values[e->var];
}
virtual void visit_literal(const Expression<T> &e) override {
result += this->top() * e->coef;
}
};
enum ConstraintOperation {
equals,
different,
greater,
less,
greater_eq,
less_eq
};
template <typename T> struct Constraint {
Expression<T> lhs, rhs;
ConstraintOperation op;
Constraint(const Expression<T> &a, const Expression<T> &b,
ConstraintOperation op)
: lhs(a), rhs(b), op(op) {}
};
template <typename T>
Constraint<T> operator==(const Expression<T> &a, const Expression<T> &b) {
return Constraint<T>(a, b, ConstraintOperation::equals);
}
template <typename T>
Constraint<T> operator!=(const Expression<T> &a, const Expression<T> &b) {
return Constraint<T>(a, b, ConstraintOperation::different);
}
template <typename T>
Constraint<T> operator>=(const Expression<T> &a, const Expression<T> &b) {
return Constraint<T>(a, b, ConstraintOperation::greater_eq);
}
template <typename T>
Constraint<T> operator<=(const Expression<T> &a, const Expression<T> &b) {
return Constraint<T>(a, b, ConstraintOperation::less_eq);
}
template <typename T>
Constraint<T> operator>(const Expression<T> &a, const Expression<T> &b) {
return Constraint<T>(a, b, ConstraintOperation::greater);
}
template <typename T>
Constraint<T> operator<(const Expression<T> &a, const Expression<T> &b) {
return Constraint<T>(a, b, ConstraintOperation::less);
}
template <typename T>
T eval(const Expression<T> &e, const map<Variable<T>, T> &values) {
auto visitor = std::make_unique<EvalVisitor<T>>();
return visitor->eval(e, values);
}
} // namespace lib
#endif