dReal4
symbolic.h
Go to the documentation of this file.
1 /// @file symbolic.h
2 ///
3 /// This is the header file that we consolidate Drake's symbolic
4 /// classes and expose them inside of dreal namespace.
5 ///
6 /// Other files in dreal should include this file and should NOT
7 /// include files in drake/common directory. Similarly, BUILD files
8 /// should only have a dependency "//dreal/symbolic:symbolic", not
9 /// "@drake_symbolic//:symbolic".
10 ///
11 /// Test cases which need predicates defined in
12 /// "drake/common/test/symbolic_test_util.h" file should include
13 /// "dreal/symbolic/symbolic_test_util.h" file and include
14 /// "//dreal/symbolic:symbolic_test_util" in BUILD file.
15 ///
16 #pragma once
17 
18 #include <functional>
19 #include <ostream>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "dreal/symbolic/symbolic_environment.h"
25 #include "dreal/symbolic/symbolic_expression.h"
26 #include "dreal/symbolic/symbolic_expression_visitor.h"
27 #include "dreal/symbolic/symbolic_formula.h"
28 #include "dreal/symbolic/symbolic_formula_visitor.h"
29 #include "dreal/symbolic/symbolic_variable.h"
30 #include "dreal/symbolic/symbolic_variables.h"
31 
32 namespace dreal {
33 
34 using drake::hash_value; // NOLINT
35 
36 // NOLINTNEXTLINE(build/namespaces)
37 using namespace drake::symbolic;
38 
39 /// Returns a formula @p f1 ⇒ @p f2.
40 Formula imply(const Formula& f1, const Formula& f2);
41 /// Returns a formula @p v ⇒ @p f.
42 Formula imply(const Variable& v, const Formula& f);
43 /// Returns a formula @p f ⇒ @p v.
44 Formula imply(const Formula& f, const Variable& v);
45 /// Returns a formula @p v1 ⇒ @p v2.
46 Formula imply(const Variable& v1, const Variable& v2);
47 
48 /// Returns a formula @p f1 ⇔ @p f2.
49 Formula iff(const Formula& f1, const Formula& f2);
50 
51 /// Returns a formula @p v ⇔ @p f.
52 Formula iff(const Variable& v, const Formula& f);
53 
54 /// Returns a formula @p f ⇔ @p v.
55 Formula iff(const Formula& f, const Variable& v);
56 
57 /// Returns a formula @p v1 ⇔ @p v2.
58 Formula iff(const Variable& v1, const Variable& v2);
59 
60 // Given @p formulas = {f₁, ..., fₙ} and a @p func : Formula →
61 // Formula, `map(formulas, func)` returns a set `{func(f₁),
62 // ... func(fₙ)}`.
63 std::set<Formula> map(const std::set<Formula>& formulas,
64  const std::function<Formula(const Formula&)>& func);
65 
66 /// Checks if @p f is atomic.
67 bool is_atomic(const Formula& f);
68 
69 /// Checks if @p f is a clause.
70 bool is_clause(const Formula& f);
71 
72 /// Returns the set of clauses in f.
73 ///
74 /// @pre @p f is in CNF. That is, @p f is either a single clause or a
75 /// conjunction of clauses.
76 std::set<Formula> get_clauses(const Formula& f);
77 
78 /// Checks if @p is in CNF form.
79 bool is_cnf(const Formula& f);
80 
81 /// @return true if @p variables1 and @p variables2 is an non-empty
82 /// intersection.
83 bool HaveIntersection(const Variables& variables1, const Variables& variables2);
84 
85 /// Strengthen the input formula @p f by @p delta.
86 /// @pre delta > 0
87 Formula DeltaStrengthen(const Formula& f, double delta);
88 
89 /// Weaken the input formula @p f by @p delta.
90 /// @pre delta > 0
91 Formula DeltaWeaken(const Formula& f, double delta);
92 
93 /// Returns true if the formula @p f is symbolic-differentiable.
94 bool IsDifferentiable(const Formula& f);
95 
96 /// Returns true if the expression @e f is symbolic-differentiable.
97 bool IsDifferentiable(const Expression& e);
98 
99 /// Make conjunction of @p formulas.
100 ///
101 /// @note This is different from the one in Drake's symbolic
102 /// library. It takes `std::vector<Formula>` while Drake's version
103 /// takes `std::set<Formula>`.
104 Formula make_conjunction(const std::vector<Formula>& formulas);
105 
106 /// @note This is different from the one in Drake's symbolic
107 /// library. It takes `std::vector<Formula>` while Drake's version
108 /// takes `std::set<Formula>`.
109 Formula make_disjunction(const std::vector<Formula>& formulas);
110 
111 /// Creates a vector of variables of @p type whose size is @p
112 /// size. The variables are numbered with @p prefix. For example,
113 /// `CreateVector("x", 5)` returns `[x0, x1, x2, x3, x4]`.
114 ///
115 /// @pre @p prefix must not be an empty string.
116 /// @pre @p size >= 1.
117 std::vector<Variable> CreateVector(
118  const std::string& prefix, int size,
120 
121 /// Represents relational operators.
122 enum class RelationalOperator {
123  EQ, ///< =
124  NEQ, ///< !=
125  GT, ///< >
126  GEQ, ///< >=
127  LT, ///< <
128  LEQ, ///< <=
129 };
130 
131 /// Negates @p op.
133 
134 /// Outputs @p op to @p os.
135 std::ostream& operator<<(std::ostream& os, RelationalOperator op);
136 
137 } // namespace dreal
RelationalOperator
Represents relational operators.
Definition: symbolic.h:122
Formula make_conjunction(const vector< Formula > &formulas)
Make conjunction of formulas.
Definition: symbolic.cc:523
Formula make_disjunction(const vector< Formula > &formulas)
Definition: symbolic.cc:531
bool is_atomic(const Formula &f)
Checks if f is atomic.
Definition: symbolic.cc:55
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
Formula iff(const Formula &f1, const Formula &f2)
Returns a formula f1 ⇔ f2.
Definition: symbolic.cc:35
bool HaveIntersection(const Variables &variables1, const Variables &variables2)
Definition: symbolic.cc:130
Type
Supported types of symbolic variables.
Definition: symbolic_variable.h:22
Formula imply(const Formula &f1, const Formula &f2)
Returns a formula f1 ⇒ f2.
Definition: symbolic.cc:24
bool is_clause(const Formula &f)
Checks if f is a clause.
Definition: symbolic.cc:79
RelationalOperator operator!(const RelationalOperator op)
Negates op.
Definition: symbolic.cc:551
set< Formula > get_clauses(const Formula &f)
Returns the set of clauses in f.
Definition: symbolic.cc:99
Formula DeltaWeaken(const Formula &f, const double delta)
Weaken the input formula $p f by delta.
Definition: symbolic.cc:510
vector< Variable > CreateVector(const string &prefix, const int size, const Variable::Type type)
Creates a vector of variables of type whose size is size.
Definition: symbolic.cc:539
bool IsDifferentiable(const Formula &f)
Returns true if the formula f is symbolic-differentiable.
Definition: symbolic.cc:515
A CONTINUOUS variable takes a double value.
Formula DeltaStrengthen(const Formula &f, const double delta)
Strengthen the input formula $p f by delta.
Definition: symbolic.cc:504
bool is_cnf(const Formula &f)
Checks if is in CNF form.
Definition: symbolic.cc:113