Drake
binding.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <utility>
5 
8 
9 namespace drake {
10 namespace solvers {
11 /**
12  * A binding on constraint type C is a mapping of the decision
13  * variables onto the inputs of C. This allows the constraint to operate
14  * on a vector made up of different elements of the decision variables.
15  */
16 template <typename C>
17 class Binding {
18  public:
20 
21  Binding(const std::shared_ptr<C>& c,
22  const Eigen::Ref<const VectorXDecisionVariable>& v)
23  : constraint_(c), vars_(v) {
24  DRAKE_DEMAND(c->num_vars() == v.rows() || c->num_vars() == Eigen::Dynamic);
25  }
26 
27  /**
28  * Concatenates each VectorDecisionVariable object in @p v into a single
29  * column vector, binds this column vector of decision variables with
30  * the constraint @p c.
31  */
32  Binding(const std::shared_ptr<C>& c, const VariableRefList& v)
33  : constraint_(c) {
34  vars_ = ConcatenateVariableRefList(v);
35  DRAKE_DEMAND(c->num_vars() == vars_.rows() ||
36  c->num_vars() == Eigen::Dynamic);
37  }
38 
39  template <typename U>
40  Binding(const Binding<U>& b,
41  typename std::enable_if<std::is_convertible<
42  std::shared_ptr<U>, std::shared_ptr<C>>::value>::type* = nullptr)
43  : Binding(b.constraint(), b.variables()) {}
44 
45  // TODO(eric.cousineau): Rename `constraint` to `evaluator` to incorporate
46  // `Cost` (and `EvaluatorBase`) as well.
47  const std::shared_ptr<C>& constraint() const { return constraint_; }
48 
49  const VectorXDecisionVariable& variables() const { return vars_; }
50 
51  /**
52  * Returns true iff the given @p var is included in this Binding.*/
53  bool ContainsVariable(const symbolic::Variable& var) const {
54  for (int i = 0; i < vars_.rows(); ++i) {
55  if (vars_(i).equal_to(var)) {
56  return true;
57  }
58  }
59  return false;
60  }
61 
62  size_t GetNumElements() const {
63  // TODO(ggould-tri) assumes that no index appears more than once in the
64  // view, which is nowhere asserted (but seems assumed elsewhere).
65  return vars_.size();
66  }
67 
68  private:
69  std::shared_ptr<C> constraint_;
71 };
72 
73 namespace internal {
74 
75 /*
76  * Create binding, inferring the type from the provided pointer.
77  * @tparam C Cost or Constraint type to be bound.
78  * @note Since this forwards arguments, this will not be usable with
79  * `std::intializer_list`.
80  */
81 template <typename C, typename... Args>
82 Binding<C> CreateBinding(const std::shared_ptr<C>& c, Args&&... args) {
83  return Binding<C>(c, std::forward<Args>(args)...);
84 }
85 
86 } // namespace internal
87 
88 } // namespace solvers
89 } // namespace drake
Binding< C > CreateBinding(const std::shared_ptr< C > &c, Args &&...args)
Definition: binding.h:82
Binding(const std::shared_ptr< C > &c, const VariableRefList &v)
Concatenates each VectorDecisionVariable object in v into a single column vector, binds this column v...
Definition: binding.h:32
const std::shared_ptr< C > & constraint() const
Definition: binding.h:47
Represents a symbolic variable.
Definition: symbolic_variable.h:24
Definition: automotive_demo.cc:88
bool ContainsVariable(const symbolic::Variable &var) const
Returns true iff the given var is included in this Binding.
Definition: binding.h:53
STL namespace.
Definition: autodiff_overloads.h:34
A binding on constraint type C is a mapping of the decision variables onto the inputs of C...
Definition: binding.h:17
#define DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(Classname)
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN defaults the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:57
int value
Definition: copyable_unique_ptr_test.cc:61
#define DRAKE_DEMAND(condition)
Evaluates condition and iff the value is false will trigger an assertion failure with a message showi...
Definition: drake_assert.h:45
size_t GetNumElements() const
Definition: binding.h:62
Binding(const Binding< U > &b, typename std::enable_if< std::is_convertible< std::shared_ptr< U >, std::shared_ptr< C >>::value >::type *=nullptr)
Definition: binding.h:40
const VectorXDecisionVariable & variables() const
Definition: binding.h:49
VectorDecisionVariable< Eigen::Dynamic > VectorXDecisionVariable
Definition: decision_variable.h:17
std::list< Eigen::Ref< const VectorXDecisionVariable >> VariableRefList
Definition: decision_variable.h:19
VectorXDecisionVariable ConcatenateVariableRefList(const VariableRefList &var_list)
Concatenates each element in var_list into a single Eigen vector of decision variables, returns this concatenated vector.
Definition: decision_variable.cc:5
Provides careful macros to selectively enable or disable the special member functions for copy-constr...