forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolic_formula_cell.cc
896 lines (729 loc) · 26.3 KB
/
symbolic_formula_cell.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
#define DRAKE_COMMON_SYMBOLIC_DETAIL_HEADER
#include "drake/common/symbolic_formula_cell.h"
#undef DRAKE_COMMON_SYMBOLIC_DETAIL_HEADER
#include <algorithm>
#include <iostream>
#include <memory>
#include <set>
#include <sstream>
#include <stdexcept>
#include "drake/common/drake_assert.h"
#include "drake/common/hash.h"
#include "drake/common/symbolic.h"
namespace drake {
namespace symbolic {
using std::equal;
using std::lexicographical_compare;
using std::ostream;
using std::ostringstream;
using std::runtime_error;
using std::set;
using std::shared_ptr;
using std::static_pointer_cast;
using std::string;
FormulaCell::FormulaCell(const FormulaKind k) : kind_{k} {}
RelationalFormulaCell::RelationalFormulaCell(const FormulaKind k,
const Expression& lhs,
const Expression& rhs)
: FormulaCell{k}, e_lhs_{lhs}, e_rhs_{rhs} {}
void RelationalFormulaCell::HashAppendDetail(DelegatingHasher* hasher) const {
DRAKE_ASSERT(hasher);
using drake::hash_append;
hash_append(*hasher, e_lhs_);
hash_append(*hasher, e_rhs_);
}
Variables RelationalFormulaCell::GetFreeVariables() const {
Variables ret{e_lhs_.GetVariables()};
ret.insert(e_rhs_.GetVariables());
return ret;
}
bool RelationalFormulaCell::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const auto& rel_f = static_cast<const RelationalFormulaCell&>(f);
return e_lhs_.EqualTo(rel_f.e_lhs_) && e_rhs_.EqualTo(rel_f.e_rhs_);
}
bool RelationalFormulaCell::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const auto& rel_f = static_cast<const RelationalFormulaCell&>(f);
if (e_lhs_.Less(rel_f.e_lhs_)) {
return true;
}
if (rel_f.e_lhs_.Less(e_lhs_)) {
return false;
}
return e_rhs_.Less(rel_f.e_rhs_);
}
NaryFormulaCell::NaryFormulaCell(const FormulaKind k,
const set<Formula>& formulas)
: FormulaCell{k}, formulas_{formulas} {}
void NaryFormulaCell::HashAppendDetail(DelegatingHasher* hasher) const {
DRAKE_ASSERT(hasher);
using drake::hash_append;
hash_append(*hasher, formulas_);
}
Variables NaryFormulaCell::GetFreeVariables() const {
Variables ret{};
for (const auto& f : formulas_) {
ret.insert(f.GetFreeVariables());
}
return ret;
}
bool NaryFormulaCell::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const auto& nary_f = static_cast<const NaryFormulaCell&>(f);
return equal(
formulas_.cbegin(), formulas_.cend(), nary_f.formulas_.cbegin(),
nary_f.formulas_.cend(),
[](const Formula& f1, const Formula& f2) { return f1.EqualTo(f2); });
}
bool NaryFormulaCell::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const auto& nary_f = static_cast<const NaryFormulaCell&>(f);
return lexicographical_compare(
formulas_.cbegin(), formulas_.cend(), nary_f.formulas_.cbegin(),
nary_f.formulas_.cend(),
[](const Formula& f1, const Formula& f2) { return f1.Less(f2); });
}
ostream& NaryFormulaCell::DisplayWithOp(ostream& os, const string& op) const {
const set<Formula>& formulas{get_operands()};
auto it(formulas.cbegin());
DRAKE_ASSERT(formulas.size() > 1u);
os << "(";
os << *it;
++it;
while (it != formulas.cend()) {
os << " " << op << " " << *it;
++it;
}
os << ")";
return os;
}
FormulaTrue::FormulaTrue() : FormulaCell{FormulaKind::True} {}
void FormulaTrue::HashAppendDetail(DelegatingHasher*) const {}
Variables FormulaTrue::GetFreeVariables() const { return Variables{}; }
bool FormulaTrue::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
return true; // There is only one instance of this kind.
}
bool FormulaTrue::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
// True < True ==> false
return false;
}
bool FormulaTrue::Evaluate(const Environment&) const { return true; }
Formula FormulaTrue::Substitute(const Substitution&) const {
return Formula::True();
}
ostream& FormulaTrue::Display(ostream& os) const { return os << "True"; }
FormulaFalse::FormulaFalse() : FormulaCell{FormulaKind::False} {}
void FormulaFalse::HashAppendDetail(DelegatingHasher*) const {}
Variables FormulaFalse::GetFreeVariables() const { return Variables{}; }
bool FormulaFalse::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
return true; // There is only one instance of this kind.
}
bool FormulaFalse::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
// False < False ==> false
return false;
}
bool FormulaFalse::Evaluate(const Environment&) const { return false; }
Formula FormulaFalse::Substitute(const Substitution&) const {
return Formula::False();
}
ostream& FormulaFalse::Display(ostream& os) const { return os << "False"; }
FormulaVar::FormulaVar(const Variable& v)
: FormulaCell{FormulaKind::Var}, var_{v} {
// Dummy symbolic variable (ID = 0) should not be used in constructing
// symbolic formulas.
DRAKE_DEMAND(!var_.is_dummy());
DRAKE_DEMAND(var_.get_type() == Variable::Type::BOOLEAN);
}
void FormulaVar::HashAppendDetail(DelegatingHasher* hasher) const {
DRAKE_ASSERT(hasher);
using drake::hash_append;
hash_append(*hasher, var_);
}
Variables FormulaVar::GetFreeVariables() const { return Variables{var_}; }
bool FormulaVar::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaVar& f_var{static_cast<const FormulaVar&>(f)};
return var_.equal_to(f_var.var_);
}
bool FormulaVar::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaVar& f_var{static_cast<const FormulaVar&>(f)};
return var_.less(f_var.var_);
}
bool FormulaVar::Evaluate(const Environment& env) const {
const Environment::const_iterator it{env.find(var_)};
if (it != env.cend()) {
return static_cast<bool>(it->second);
} else {
ostringstream oss;
oss << "The following environment does not have an entry for the "
"variable "
<< var_ << "\n";
oss << env << "\n";
throw runtime_error(oss.str());
}
}
Formula FormulaVar::Substitute(const Substitution&) const {
// TODO(soonho-tri): Add a substitution (Variable -> Formula) and use it
// here. For now, `Substitute` does nothing for Boolean variables.
return Formula{var_};
}
ostream& FormulaVar::Display(ostream& os) const { return os << var_; }
const Variable& FormulaVar::get_variable() const { return var_; }
FormulaEq::FormulaEq(const Expression& e1, const Expression& e2)
: RelationalFormulaCell{FormulaKind::Eq, e1, e2} {}
bool FormulaEq::Evaluate(const Environment& env) const {
return get_lhs_expression().Evaluate(env) ==
get_rhs_expression().Evaluate(env);
}
Formula FormulaEq::Substitute(const Substitution& s) const {
return get_lhs_expression().Substitute(s) ==
get_rhs_expression().Substitute(s);
}
ostream& FormulaEq::Display(ostream& os) const {
return os << "(" << get_lhs_expression() << " == " << get_rhs_expression()
<< ")";
}
FormulaNeq::FormulaNeq(const Expression& e1, const Expression& e2)
: RelationalFormulaCell{FormulaKind::Neq, e1, e2} {}
bool FormulaNeq::Evaluate(const Environment& env) const {
return get_lhs_expression().Evaluate(env) !=
get_rhs_expression().Evaluate(env);
}
Formula FormulaNeq::Substitute(const Substitution& s) const {
return get_lhs_expression().Substitute(s) !=
get_rhs_expression().Substitute(s);
}
ostream& FormulaNeq::Display(ostream& os) const {
return os << "(" << get_lhs_expression() << " != " << get_rhs_expression()
<< ")";
}
FormulaGt::FormulaGt(const Expression& e1, const Expression& e2)
: RelationalFormulaCell{FormulaKind::Gt, e1, e2} {}
bool FormulaGt::Evaluate(const Environment& env) const {
return get_lhs_expression().Evaluate(env) >
get_rhs_expression().Evaluate(env);
}
Formula FormulaGt::Substitute(const Substitution& s) const {
return get_lhs_expression().Substitute(s) >
get_rhs_expression().Substitute(s);
}
ostream& FormulaGt::Display(ostream& os) const {
return os << "(" << get_lhs_expression() << " > " << get_rhs_expression()
<< ")";
}
FormulaGeq::FormulaGeq(const Expression& e1, const Expression& e2)
: RelationalFormulaCell{FormulaKind::Geq, e1, e2} {}
bool FormulaGeq::Evaluate(const Environment& env) const {
return get_lhs_expression().Evaluate(env) >=
get_rhs_expression().Evaluate(env);
}
Formula FormulaGeq::Substitute(const Substitution& s) const {
return get_lhs_expression().Substitute(s) >=
get_rhs_expression().Substitute(s);
}
ostream& FormulaGeq::Display(ostream& os) const {
return os << "(" << get_lhs_expression() << " >= " << get_rhs_expression()
<< ")";
}
FormulaLt::FormulaLt(const Expression& e1, const Expression& e2)
: RelationalFormulaCell{FormulaKind::Lt, e1, e2} {}
bool FormulaLt::Evaluate(const Environment& env) const {
return get_lhs_expression().Evaluate(env) <
get_rhs_expression().Evaluate(env);
}
Formula FormulaLt::Substitute(const Substitution& s) const {
return get_lhs_expression().Substitute(s) <
get_rhs_expression().Substitute(s);
}
ostream& FormulaLt::Display(ostream& os) const {
return os << "(" << get_lhs_expression() << " < " << get_rhs_expression()
<< ")";
}
FormulaLeq::FormulaLeq(const Expression& e1, const Expression& e2)
: RelationalFormulaCell{FormulaKind::Leq, e1, e2} {}
bool FormulaLeq::Evaluate(const Environment& env) const {
return get_lhs_expression().Evaluate(env) <=
get_rhs_expression().Evaluate(env);
}
Formula FormulaLeq::Substitute(const Substitution& s) const {
return get_lhs_expression().Substitute(s) <=
get_rhs_expression().Substitute(s);
}
ostream& FormulaLeq::Display(ostream& os) const {
return os << "(" << get_lhs_expression() << " <= " << get_rhs_expression()
<< ")";
}
FormulaAnd::FormulaAnd(const set<Formula>& formulas)
: NaryFormulaCell{FormulaKind::And, formulas} {
DRAKE_ASSERT(get_operands().size() > 1u);
}
FormulaAnd::FormulaAnd(const Formula& f1, const Formula& f2)
: NaryFormulaCell{FormulaKind::And, set<Formula>{f1, f2}} {}
bool FormulaAnd::Evaluate(const Environment& env) const {
for (const auto& f : get_operands()) {
if (!f.Evaluate(env)) {
return false;
}
}
return true;
}
Formula FormulaAnd::Substitute(const Substitution& s) const {
Formula ret{Formula::True()};
for (const auto& f : get_operands()) {
ret = ret && f.Substitute(s);
// short-circuiting
if (is_false(ret)) {
return ret;
}
}
return ret;
}
ostream& FormulaAnd::Display(ostream& os) const {
return DisplayWithOp(os, "and");
}
FormulaOr::FormulaOr(const set<Formula>& formulas)
: NaryFormulaCell{FormulaKind::Or, formulas} {
DRAKE_ASSERT(get_operands().size() > 1u);
}
FormulaOr::FormulaOr(const Formula& f1, const Formula& f2)
: NaryFormulaCell{FormulaKind::Or, set<Formula>{f1, f2}} {}
bool FormulaOr::Evaluate(const Environment& env) const {
for (const auto& f : get_operands()) {
if (f.Evaluate(env)) {
return true;
}
}
return false;
}
Formula FormulaOr::Substitute(const Substitution& s) const {
Formula ret{Formula::False()};
for (const auto& f : get_operands()) {
ret = ret || f.Substitute(s);
// short-circuiting
if (is_true(ret)) {
return ret;
}
}
return ret;
}
ostream& FormulaOr::Display(ostream& os) const {
return DisplayWithOp(os, "or");
}
FormulaNot::FormulaNot(const Formula& f)
: FormulaCell{FormulaKind::Not}, f_{f} {}
void FormulaNot::HashAppendDetail(DelegatingHasher* hasher) const {
DRAKE_ASSERT(hasher);
using drake::hash_append;
hash_append(*hasher, f_);
}
Variables FormulaNot::GetFreeVariables() const { return f_.GetFreeVariables(); }
bool FormulaNot::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaNot& f_not{static_cast<const FormulaNot&>(f)};
return f_.EqualTo(f_not.f_);
}
bool FormulaNot::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaNot& not_f{static_cast<const FormulaNot&>(f)};
return f_.Less(not_f.f_);
}
bool FormulaNot::Evaluate(const Environment& env) const {
return !f_.Evaluate(env);
}
Formula FormulaNot::Substitute(const Substitution& s) const {
return !f_.Substitute(s);
}
ostream& FormulaNot::Display(ostream& os) const {
return os << "!(" << f_ << ")";
}
FormulaForall::FormulaForall(const Variables& vars, const Formula& f)
: FormulaCell{FormulaKind::Forall}, vars_{vars}, f_{f} {}
void FormulaForall::HashAppendDetail(DelegatingHasher* hasher) const {
DRAKE_ASSERT(hasher);
using drake::hash_append;
hash_append(*hasher, vars_);
hash_append(*hasher, f_);
}
Variables FormulaForall::GetFreeVariables() const {
return f_.GetFreeVariables() - vars_;
}
bool FormulaForall::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaForall& f_forall{static_cast<const FormulaForall&>(f)};
return vars_ == f_forall.vars_ && f_.EqualTo(f_forall.f_);
}
bool FormulaForall::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaForall& forall_f{static_cast<const FormulaForall&>(f)};
if (vars_ < forall_f.vars_) {
return true;
}
if (forall_f.vars_ < vars_) {
return false;
}
return this->f_.Less(forall_f.f_);
}
bool FormulaForall::Evaluate(const Environment&) const {
// Given ∀ x1, ..., xn. F, check if there is a counterexample satisfying
// ¬F. If exists, it returns false. Otherwise, return true.
// That is, it returns !check(∃ x1, ..., xn. ¬F)
throw runtime_error("not implemented yet");
}
Formula FormulaForall::Substitute(const Substitution& s) const {
// Quantified variables are already bound and should not be substituted by s.
// We construct a new substitution new_s from s by removing the entries of
// bound variables.
Substitution new_s{s};
for (const Variable& var : vars_) {
new_s.erase(var);
}
return forall(vars_, f_.Substitute(new_s));
}
ostream& FormulaForall::Display(ostream& os) const {
return os << "forall(" << vars_ << ". " << f_ << ")";
}
FormulaIsnan::FormulaIsnan(const Expression& e)
: FormulaCell{FormulaKind::Isnan}, e_{e} {}
void FormulaIsnan::HashAppendDetail(DelegatingHasher* hasher) const {
DRAKE_ASSERT(hasher);
using drake::hash_append;
hash_append(*hasher, e_);
}
Variables FormulaIsnan::GetFreeVariables() const { return e_.GetVariables(); }
bool FormulaIsnan::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaIsnan& f_isnan{static_cast<const FormulaIsnan&>(f)};
return e_.EqualTo(f_isnan.e_);
}
bool FormulaIsnan::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaIsnan& f_isnan{static_cast<const FormulaIsnan&>(f)};
return e_.Less(f_isnan.e_);
}
bool FormulaIsnan::Evaluate(const Environment& env) const {
// Note that it throws std::runtime_error if it detects NaN during evaluation.
return std::isnan(e_.Evaluate(env));
}
Formula FormulaIsnan::Substitute(const Substitution& s) const {
return isnan(e_.Substitute(s));
}
ostream& FormulaIsnan::Display(ostream& os) const {
return os << "isnan(" << e_ << ")";
}
namespace {
bool IsSymmetric(const Eigen::Ref<const MatrixX<Expression>>& m) {
const int dim = m.rows();
if (m.cols() != dim) {
return false;
}
for (int i = 0; i < dim; ++i) {
for (int j = i + 1; j < dim; ++j) {
if (!m(i, j).EqualTo(m(j, i))) {
return false;
}
}
}
return true;
}
} // namespace
FormulaPositiveSemidefinite::FormulaPositiveSemidefinite(
const Eigen::Ref<const MatrixX<Expression>>& m)
: FormulaCell{FormulaKind::PositiveSemidefinite}, m_{m} {
if (!IsSymmetric(m)) {
ostringstream oss;
oss << "The following matrix is not symmetric and cannot be used to "
"construct drake::symbolic::FormulaPositiveSemidefinite:\n"
<< m;
throw std::runtime_error(oss.str());
}
}
namespace {
// Helper Eigen-visitor class that we use to implement
// FormulaPositiveSemidefinite::GetFreeVariables().
struct VariablesCollector {
using Index = Eigen::Index;
// Called for the first coefficient.
void init(const Expression& e, Index i, Index j) {
DRAKE_ASSERT(vars_.empty());
return operator()(e, i, j);
}
// Called for all other coefficients.
void operator()(const Expression& e, Index /* i */, Index /* j */) {
vars_ += e.GetVariables();
}
Variables vars_;
};
} // namespace
void FormulaPositiveSemidefinite::HashAppendDetail(
DelegatingHasher* hasher) const {
DRAKE_ASSERT(hasher);
using drake::hash_append;
// Computes a hash of a matrix only using its lower-triangular part.
for (int i = 0; i < m_.rows(); ++i) {
for (int j = 0; j <= i; ++j) {
hash_append(*hasher, m_(i, j));
}
}
hash_append(*hasher, m_.size());
}
Variables FormulaPositiveSemidefinite::GetFreeVariables() const {
VariablesCollector vc;
m_.visit(vc);
return vc.vars_;
}
bool FormulaPositiveSemidefinite::EqualTo(const FormulaCell& f) const {
// Formula::EqualTo guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaPositiveSemidefinite& f_psd{
static_cast<const FormulaPositiveSemidefinite&>(f)};
return (m_.rows() == f_psd.m_.rows()) && (m_.cols() == f_psd.m_.cols()) &&
CheckStructuralEquality(m_, f_psd.m_);
}
bool FormulaPositiveSemidefinite::Less(const FormulaCell& f) const {
// Formula::Less guarantees the following assertion.
DRAKE_ASSERT(get_kind() == f.get_kind());
const FormulaPositiveSemidefinite& f_psd{
static_cast<const FormulaPositiveSemidefinite&>(f)};
// Compare rows.
if (m_.rows() < f_psd.m_.rows()) {
return true;
}
if (f_psd.m_.rows() < m_.rows()) {
return false;
}
// No need to compare cols since m_ and f_psd.m_ are square matrices.
DRAKE_ASSERT(m_.rows() == f_psd.m_.rows() && m_.cols() == f_psd.m_.cols());
// Element-wise comparison.
const int num_of_elements = m_.rows() * m_.cols();
// clang-format off
return lexicographical_compare(
m_.data(), m_.data() + num_of_elements,
f_psd.m_.data(), f_psd.m_.data() + num_of_elements,
[](const Expression& e1, const Expression& e2) { return e1.Less(e2); });
// clang-format on
}
bool FormulaPositiveSemidefinite::Evaluate(const Environment&) const {
// Need to check if xᵀ m x ≥ * 0 for all vector x ∈ ℝⁿ.
// TODO(Soonho): implement this when we have SMT/delta-SMT support.
throw runtime_error(
"Checking positive_semidefinite(M) is not yet implemented.");
}
Formula FormulaPositiveSemidefinite::Substitute(const Substitution& s) const {
return positive_semidefinite(
m_.unaryExpr([&s](const Expression& e) { return e.Substitute(s); }));
}
ostream& FormulaPositiveSemidefinite::Display(ostream& os) const {
return os << "positive_semidefinite(" << m_ << ")";
}
bool is_false(const FormulaCell& f) {
return f.get_kind() == FormulaKind::False;
}
bool is_true(const FormulaCell& f) { return f.get_kind() == FormulaKind::True; }
bool is_variable(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Var;
}
bool is_equal_to(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Eq;
}
bool is_not_equal_to(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Neq;
}
bool is_greater_than(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Gt;
}
bool is_greater_than_or_equal_to(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Geq;
}
bool is_less_than(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Lt;
}
bool is_less_than_or_equal_to(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Leq;
}
bool is_relational(const FormulaCell& 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 FormulaCell& f) {
return f.get_kind() == FormulaKind::And;
}
bool is_disjunction(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Or;
}
bool is_nary(const FormulaCell& f) {
return is_conjunction(f) || is_disjunction(f);
}
bool is_negation(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Not;
}
bool is_forall(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Forall;
}
bool is_isnan(const FormulaCell& f) {
return f.get_kind() == FormulaKind::Isnan;
}
bool is_positive_semidefinite(const FormulaCell& f) {
return f.get_kind() == FormulaKind::PositiveSemidefinite;
}
shared_ptr<const FormulaFalse> to_false(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_false(*f_ptr));
return static_pointer_cast<const FormulaFalse>(f_ptr);
}
shared_ptr<const FormulaFalse> to_false(const Formula& f) {
return to_false(f.ptr_);
}
shared_ptr<const FormulaTrue> to_true(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_true(*f_ptr));
return static_pointer_cast<const FormulaTrue>(f_ptr);
}
shared_ptr<const FormulaTrue> to_true(const Formula& f) {
return to_true(f.ptr_);
}
shared_ptr<const FormulaVar> to_variable(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_variable(*f_ptr));
return static_pointer_cast<const FormulaVar>(f_ptr);
}
shared_ptr<const FormulaVar> to_variable(const Formula& f) {
return to_variable(f.ptr_);
}
shared_ptr<const RelationalFormulaCell> to_relational(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_relational(*f_ptr));
return static_pointer_cast<const RelationalFormulaCell>(f_ptr);
}
shared_ptr<const RelationalFormulaCell> to_relational(const Formula& f) {
return to_relational(f.ptr_);
}
shared_ptr<const FormulaEq> to_equal_to(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_equal_to(*f_ptr));
return static_pointer_cast<const FormulaEq>(f_ptr);
}
shared_ptr<const FormulaEq> to_equal_to(const Formula& f) {
return to_equal_to(f.ptr_);
}
shared_ptr<const FormulaNeq> to_not_equal_to(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_not_equal_to(*f_ptr));
return static_pointer_cast<const FormulaNeq>(f_ptr);
}
shared_ptr<const FormulaNeq> to_not_equal_to(const Formula& f) {
return to_not_equal_to(f.ptr_);
}
shared_ptr<const FormulaGt> to_greater_than(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_greater_than(*f_ptr));
return static_pointer_cast<const FormulaGt>(f_ptr);
}
shared_ptr<const FormulaGt> to_greater_than(const Formula& f) {
return to_greater_than(f.ptr_);
}
shared_ptr<const FormulaGeq> to_greater_than_or_equal_to(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_greater_than_or_equal_to(*f_ptr));
return static_pointer_cast<const FormulaGeq>(f_ptr);
}
shared_ptr<const FormulaGeq> to_greater_than_or_equal_to(const Formula& f) {
return to_greater_than_or_equal_to(f.ptr_);
}
shared_ptr<const FormulaLt> to_less_than(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_less_than(*f_ptr));
return static_pointer_cast<const FormulaLt>(f_ptr);
}
shared_ptr<const FormulaLt> to_less_than(const Formula& f) {
return to_less_than(f.ptr_);
}
shared_ptr<const FormulaLeq> to_less_than_or_equal_to(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_less_than_or_equal_to(*f_ptr));
return static_pointer_cast<const FormulaLeq>(f_ptr);
}
shared_ptr<const FormulaLeq> to_less_than_or_equal_to(const Formula& f) {
return to_less_than_or_equal_to(f.ptr_);
}
shared_ptr<const NaryFormulaCell> to_nary(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_nary(*f_ptr));
return static_pointer_cast<const NaryFormulaCell>(f_ptr);
}
shared_ptr<const NaryFormulaCell> to_nary(const Formula& f) {
return to_nary(f.ptr_);
}
shared_ptr<const FormulaAnd> to_conjunction(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_conjunction(*f_ptr));
return static_pointer_cast<const FormulaAnd>(f_ptr);
}
shared_ptr<const FormulaAnd> to_conjunction(const Formula& f) {
return to_conjunction(f.ptr_);
}
shared_ptr<const FormulaOr> to_disjunction(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_disjunction(*f_ptr));
return static_pointer_cast<const FormulaOr>(f_ptr);
}
shared_ptr<const FormulaOr> to_disjunction(const Formula& f) {
return to_disjunction(f.ptr_);
}
shared_ptr<const FormulaNot> to_negation(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_negation(*f_ptr));
return static_pointer_cast<const FormulaNot>(f_ptr);
}
shared_ptr<const FormulaNot> to_negation(const Formula& f) {
return to_negation(f.ptr_);
}
shared_ptr<const FormulaForall> to_forall(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_forall(*f_ptr));
return static_pointer_cast<const FormulaForall>(f_ptr);
}
shared_ptr<const FormulaForall> to_forall(const Formula& f) {
return to_forall(f.ptr_);
}
shared_ptr<const FormulaIsnan> to_isnan(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_isnan(*f_ptr));
return static_pointer_cast<const FormulaIsnan>(f_ptr);
}
shared_ptr<const FormulaIsnan> to_isnan(const Formula& f) {
return to_isnan(f.ptr_);
}
shared_ptr<const FormulaPositiveSemidefinite> to_positive_semidefinite(
const shared_ptr<const FormulaCell>& f_ptr) {
DRAKE_ASSERT(is_positive_semidefinite(*f_ptr));
return static_pointer_cast<const FormulaPositiveSemidefinite>(f_ptr);
}
shared_ptr<const FormulaPositiveSemidefinite> to_positive_semidefinite(
const Formula& f) {
return to_positive_semidefinite(f.ptr_);
}
} // namespace symbolic
} // namespace drake