forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolic_formula.cc
371 lines (329 loc) · 10.9 KB
/
symbolic_formula.cc
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// NOLINTNEXTLINE(build/include): Its header file is included in symbolic.h.
#include <cstddef>
#include <iostream>
#include <memory>
#include <set>
#include <sstream>
#include "drake/common/drake_assert.h"
#include "drake/common/symbolic.h"
#define DRAKE_COMMON_SYMBOLIC_DETAIL_HEADER
#include "drake/common/symbolic_formula_cell.h"
#undef DRAKE_COMMON_SYMBOLIC_DETAIL_HEADER
namespace drake {
namespace symbolic {
using std::make_shared;
using std::ostream;
using std::ostringstream;
using std::set;
using std::shared_ptr;
using std::string;
bool operator<(FormulaKind k1, FormulaKind k2) {
return static_cast<int>(k1) < static_cast<int>(k2);
}
Formula::Formula(shared_ptr<FormulaCell> ptr) : ptr_{std::move(ptr)} {}
Formula::Formula(const Variable& var) : ptr_{make_shared<FormulaVar>(var)} {}
FormulaKind Formula::get_kind() const {
DRAKE_ASSERT(ptr_ != nullptr);
return ptr_->get_kind();
}
void Formula::HashAppend(DelegatingHasher* hasher) const {
using drake::hash_append;
hash_append(*hasher, get_kind());
ptr_->HashAppendDetail(hasher);
}
Variables Formula::GetFreeVariables() const {
DRAKE_ASSERT(ptr_ != nullptr);
return ptr_->GetFreeVariables();
}
bool Formula::EqualTo(const Formula& f) const {
DRAKE_ASSERT(ptr_ != nullptr);
DRAKE_ASSERT(f.ptr_ != nullptr);
if (ptr_ == f.ptr_) {
// pointer equality
return true;
}
if (get_kind() != f.get_kind()) {
return false;
}
// Same kind/hash, but it could be the result of hash collision,
// check structural equality.
return ptr_->EqualTo(*(f.ptr_));
}
bool Formula::Less(const Formula& f) const {
const FormulaKind k1{get_kind()};
const FormulaKind k2{f.get_kind()};
if (k1 < k2) {
return true;
}
if (k2 < k1) {
return false;
}
return ptr_->Less(*(f.ptr_));
}
bool Formula::Evaluate(const Environment& env) const {
DRAKE_ASSERT(ptr_ != nullptr);
return ptr_->Evaluate(env);
}
Formula Formula::Substitute(const Variable& var, const Expression& e) const {
DRAKE_ASSERT(ptr_ != nullptr);
return Formula{ptr_->Substitute({{var, e}})};
}
Formula Formula::Substitute(const Substitution& s) const {
DRAKE_ASSERT(ptr_ != nullptr);
if (!s.empty()) {
return Formula{ptr_->Substitute(s)};
}
return *this;
}
string Formula::to_string() const {
ostringstream oss;
oss << *this;
return oss.str();
}
Formula Formula::True() {
static Formula tt{make_shared<FormulaTrue>()};
return tt;
}
Formula Formula::False() {
static Formula ff{make_shared<FormulaFalse>()};
return ff;
}
Formula forall(const Variables& vars, const Formula& f) {
return Formula{make_shared<FormulaForall>(vars, f)};
}
Formula make_conjunction(const set<Formula>& formulas) {
set<Formula> operands;
for (const Formula& f : formulas) {
if (is_false(f)) {
// Short-circuits to False.
// f₁ ∧ ... ∧ False ∧ ... ∧ fₙ => False
return Formula::False();
}
if (is_true(f)) {
// Drop redundant True.
// f₁ ∧ ... ∧ True ∧ ... ∧ fₙ => f₁ ∧ ... ∧ fₙ
continue;
}
if (is_conjunction(f)) {
// Flattening.
// f₁ ∧ ... ∧ (fᵢ₁ ∧ ... ∧ fᵢₘ) ∧ ... ∧ fₙ
// => f₁ ∧ ... ∧ fᵢ₁ ∧ ... ∧ fᵢₘ ∧ ... ∧ fₙ
const auto& operands_in_f = get_operands(f);
operands.insert(operands_in_f.cbegin(), operands_in_f.cend());
} else {
operands.insert(f);
}
}
if (operands.empty()) {
// ⋀{} = True
return Formula::True();
}
if (operands.size() == 1) {
return *(operands.begin());
}
// TODO(soonho-tri): Returns False if both f and ¬f appear in operands.
return Formula{make_shared<FormulaAnd>(operands)};
}
Formula operator&&(const Formula& f1, const Formula& f2) {
return make_conjunction({f1, f2});
}
Formula operator&&(const Variable& v, const Formula& f) {
return Formula(v) && f;
}
Formula operator&&(const Formula& f, const Variable& v) {
return f && Formula(v);
}
Formula operator&&(const Variable& v1, const Variable& v2) {
return Formula(v1) && Formula(v2);
}
Formula make_disjunction(const set<Formula>& formulas) {
set<Formula> operands;
for (const Formula& f : formulas) {
if (is_true(f)) {
// Short-circuits to True.
// f₁ ∨ ... ∨ True ∨ ... ∨ fₙ => True
return Formula::True();
}
if (is_false(f)) {
// Drop redundant False.
// f₁ ∨ ... ∨ False ∨ ... ∨ fₙ => f₁ ∨ ... ∨ fₙ
continue;
}
if (is_disjunction(f)) {
// Flattening.
// f₁ ∨ ... ∨ (fᵢ₁ ∨ ... ∨ fᵢₘ) ∨ ... ∨ fₙ
// => f₁ ∨ ... ∨ fᵢ₁ ∨ ... ∨ fᵢₘ ∨ ... ∨ fₙ
const auto& operands_in_f = get_operands(f);
operands.insert(operands_in_f.cbegin(), operands_in_f.cend());
} else {
operands.insert(f);
}
}
if (operands.empty()) {
// ⋁{} = False
return Formula::False();
}
if (operands.size() == 1) {
return *(operands.begin());
}
// TODO(soonho-tri): Returns True if both f and ¬f appear in operands.
return Formula{make_shared<FormulaOr>(operands)};
}
Formula operator||(const Formula& f1, const Formula& f2) {
return make_disjunction({f1, f2});
}
Formula operator||(const Variable& v, const Formula& f) {
return Formula(v) || f;
}
Formula operator||(const Formula& f, const Variable& v) {
return f || Formula(v);
}
Formula operator||(const Variable& v1, const Variable& v2) {
return Formula(v1) || Formula(v2);
}
Formula operator!(const Formula& f) {
if (f.EqualTo(Formula::True())) {
return Formula::False();
}
if (f.EqualTo(Formula::False())) {
return Formula::True();
}
// Simplification: ¬(¬f₁) => f₁
if (is_negation(f)) {
return get_operand(f);
}
return Formula{make_shared<FormulaNot>(f)};
}
Formula operator!(const Variable& v) { return !Formula(v); }
ostream& operator<<(ostream& os, const Formula& f) {
DRAKE_ASSERT(f.ptr_ != nullptr);
return f.ptr_->Display(os);
}
Formula operator==(const Expression& e1, const Expression& e2) {
// Simplification: E1 - E2 == 0 => True
const Expression diff{e1 - e2};
if (diff.get_kind() == ExpressionKind::Constant) {
return diff.Evaluate() == 0.0 ? Formula::True() : Formula::False();
}
return Formula{make_shared<FormulaEq>(e1, e2)};
}
Formula operator!=(const Expression& e1, const Expression& e2) {
// Simplification: E1 - E2 != 0 => True
const Expression diff{e1 - e2};
if (diff.get_kind() == ExpressionKind::Constant) {
return diff.Evaluate() != 0.0 ? Formula::True() : Formula::False();
}
return Formula{make_shared<FormulaNeq>(e1, e2)};
}
Formula operator<(const Expression& e1, const Expression& e2) {
// Simplification: E1 - E2 < 0 => True
const Expression diff{e1 - e2};
if (diff.get_kind() == ExpressionKind::Constant) {
return diff.Evaluate() < 0 ? Formula::True() : Formula::False();
}
return Formula{make_shared<FormulaLt>(e1, e2)};
}
Formula operator<=(const Expression& e1, const Expression& e2) {
// Simplification: E1 - E2 <= 0 => True
const Expression diff{e1 - e2};
if (diff.get_kind() == ExpressionKind::Constant) {
return diff.Evaluate() <= 0 ? Formula::True() : Formula::False();
}
return Formula{make_shared<FormulaLeq>(e1, e2)};
}
Formula operator>(const Expression& e1, const Expression& e2) {
// Simplification: E1 - E2 > 0 => True
const Expression diff{e1 - e2};
if (diff.get_kind() == ExpressionKind::Constant) {
return diff.Evaluate() > 0 ? Formula::True() : Formula::False();
}
return Formula{make_shared<FormulaGt>(e1, e2)};
}
Formula operator>=(const Expression& e1, const Expression& e2) {
// Simplification: E1 - E2 >= 0 => True
const Expression diff{e1 - e2};
if (diff.get_kind() == ExpressionKind::Constant) {
return diff.Evaluate() >= 0 ? Formula::True() : Formula::False();
}
return Formula{make_shared<FormulaGeq>(e1, e2)};
}
Formula isnan(const Expression& e) {
return Formula{make_shared<FormulaIsnan>(e)};
}
Formula positive_semidefinite(const Eigen::Ref<const MatrixX<Expression>>& m) {
return Formula{make_shared<FormulaPositiveSemidefinite>(m)};
}
Formula positive_semidefinite(const MatrixX<Expression>& m,
const Eigen::UpLoType mode) {
switch (mode) {
case Eigen::Lower:
return Formula{make_shared<FormulaPositiveSemidefinite>(
m.triangularView<Eigen::Lower>())};
case Eigen::Upper:
return Formula{make_shared<FormulaPositiveSemidefinite>(
m.triangularView<Eigen::Upper>())};
default:
throw std::runtime_error(
"positive_semidefinite is called with a mode which is neither "
"Eigen::Lower nor Eigen::Upper.");
}
}
bool is_false(const Formula& f) { return is_false(*f.ptr_); }
bool is_true(const Formula& f) { return is_true(*f.ptr_); }
bool is_variable(const Formula& f) { return is_variable(*f.ptr_); }
bool is_equal_to(const Formula& f) { return is_equal_to(*f.ptr_); }
bool is_not_equal_to(const Formula& f) { return is_not_equal_to(*f.ptr_); }
bool is_greater_than(const Formula& f) { return is_greater_than(*f.ptr_); }
bool is_greater_than_or_equal_to(const Formula& f) {
return is_greater_than_or_equal_to(*f.ptr_);
}
bool is_less_than(const Formula& f) { return is_less_than(*f.ptr_); }
bool is_less_than_or_equal_to(const Formula& f) {
return is_less_than_or_equal_to(*f.ptr_);
}
bool is_relational(const Formula& f) {
return is_equal_to(f) || is_not_equal_to(f) || is_greater_than(f) ||
is_greater_than_or_equal_to(f) || is_less_than(f) ||
is_less_than_or_equal_to(f);
}
bool is_conjunction(const Formula& f) { return is_conjunction(*f.ptr_); }
bool is_disjunction(const Formula& f) { return is_disjunction(*f.ptr_); }
bool is_nary(const Formula& f) {
return is_conjunction(f) || is_disjunction(f);
}
bool is_negation(const Formula& f) { return is_negation(*f.ptr_); }
bool is_forall(const Formula& f) { return is_forall(*f.ptr_); }
bool is_isnan(const Formula& f) { return is_isnan(*f.ptr_); }
bool is_positive_semidefinite(const Formula& f) {
return is_positive_semidefinite(*f.ptr_);
}
const Variable& get_variable(const Formula& f) {
DRAKE_ASSERT(is_variable(f));
return to_variable(f)->get_variable();
}
const Expression& get_lhs_expression(const Formula& f) {
DRAKE_ASSERT(is_relational(f));
return to_relational(f)->get_lhs_expression();
}
const Expression& get_rhs_expression(const Formula& f) {
DRAKE_ASSERT(is_relational(f));
return to_relational(f)->get_rhs_expression();
}
const set<Formula>& get_operands(const Formula& f) {
return to_nary(f)->get_operands();
}
const Formula& get_operand(const Formula& f) {
return to_negation(f)->get_operand();
}
const Variables& get_quantified_variables(const Formula& f) {
return to_forall(f)->get_quantified_variables();
}
const Formula& get_quantified_formula(const Formula& f) {
return to_forall(f)->get_quantified_formula();
}
const MatrixX<Expression>& get_matrix_in_positive_semidefinite(
const Formula& f) {
return to_positive_semidefinite(f)->get_matrix();
}
} // namespace symbolic
} // namespace drake