Qontrol
ConstraintSet.hpp
1 // This file is part of Qontrol, a quadratic optimization library to
2 // control robot.
3 //
4 // Copyright (C) 2023 Lucas Joseph <lucas.joseph@inria.fr>
5 //
6 // Qontrol is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 3 of
14 // the License, or (at your option) any later version.
15 //
16 // Qontrol is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Qontrol. If not, see <http://www.gnu.org/licenses/>.
24 
25 #pragma once
26 
27 #include <Qontrol/Constraints/GenericConstraint.hpp>
28 #include <Qontrol/Model/GenericModel.hpp>
29 #include <Qontrol/Problem/ControlOutput.hpp>
30 
31 #include <algorithm>
32 #include <memory>
33 #include <vector>
34 
35 namespace Qontrol {
36 namespace Constraint {
42 template <Qontrol::ControlOutput output> class ConstraintSet {
43 public:
44  ConstraintSet(std::shared_ptr<Model::GenericModel> model_ptr)
45  : model_ptr_{model_ptr} {
46  }
47 
55  template <template <Qontrol::ControlOutput output2> class ControlInput>
56  std::shared_ptr<ControlInput<output>> add(std::string constraint_name) {
57  if (!exist(constraint_name)) {
58  auto constraint_ptr = std::shared_ptr<ControlInput<output>>(
59  new ControlInput<output>(constraint_name, model_ptr_));
60  // Implicit conversion to GenericConstraint when pushing
61  constraints_ptrs_.push_back(constraint_ptr);
62  constraints_names_.push_back(constraint_name);
63  return constraint_ptr;
64  } else {
65  throw std::logic_error("Constraint with name " + constraint_name +
66  " already exist.");
67  }
68  }
69 
77  std::shared_ptr<Constraint::GenericConstraint> add(std::string constraint_name, int constraint_size) {
78  if (!exist(constraint_name)) {
79  auto base_constraint_ptr = std::make_shared<Constraint::GenericConstraint>(
80  constraint_name,constraint_size, model_ptr_->getNrOfDegreesOfFreedom());
81  constraints_ptrs_.push_back(base_constraint_ptr);
82  constraints_names_.push_back(constraint_name);
83  return base_constraint_ptr;
84  } else {
85  throw std::logic_error("Constraint with name " + constraint_name +
86  " already exist.");
87  }
88  }
89 
96  std::vector<std::string>::iterator
97  findConstraintIterator(std::string constraint_name) {
98  return std::find(constraints_names_.begin(), constraints_names_.end(),
99  constraint_name);
100  }
101 
107  void remove(std::shared_ptr<Constraint::GenericConstraint> constraint_ptr) {
108  remove(constraint_ptr->getName());
109  }
110 
116  void remove(std::string constraint_name) {
117  if (exist(constraint_name)) {
118  auto index = getConstraintIndex(constraint_name);
119  constraints_ptrs_.erase(constraints_ptrs_.begin() + index);
120  constraints_names_.erase(constraints_names_.begin() + index);
121  }
122  }
123 
129  bool exist(std::string constraint_name) {
130  return findConstraintIterator(constraint_name) != constraints_names_.end();
131  }
132 
139  int getConstraintIndex(std::string constraint_name) {
140  if (exist(constraint_name)) {
141  auto itr = findConstraintIterator(constraint_name);
142  return std::distance(constraints_names_.begin(), itr);
143  } else {
144  PLOGI << "Constraint " << constraint_name
145  << " doesn't exist. Cannot get it's index.";
146  return -1;
147  }
148  }
149 
150 
155  void update(double dt) {
156  for (auto constraint : constraints_ptrs_) {
157  constraint->update(dt);
158  }
159  }
160 
161  Constraint::GenericConstraint getConstraint(std::string constraint_name) {
162  if (exist(constraint_name)) {
163  auto index = getConstraintIndex(constraint_name);
164  return *constraints_ptrs_[index];
165  } else {
166  throw std::logic_error("Constraint with name " + constraint_name +
167  " doesn't exist.");
168  }
169  }
170 
176  std::vector<std::shared_ptr<Constraint::GenericConstraint>> getConstraints() {
177  return constraints_ptrs_;
178  }
179 
180 private:
181  std::shared_ptr<Model::GenericModel> model_ptr_;
182  std::vector<std::string> constraints_names_;
183  std::vector<std::shared_ptr<Constraint::GenericConstraint>> constraints_ptrs_;
184 };
185 } // namespace Constraint
186 } // namespace Qontrol
Qontrol::Constraint::ConstraintSet::findConstraintIterator
std::vector< std::string >::iterator findConstraintIterator(std::string constraint_name)
Find the iterator of a constraint in the constraint set given its name.
Definition: ConstraintSet.hpp:97
Qontrol::Constraint::GenericConstraint
Represents a generic constraint.
Definition: GenericConstraint.hpp:47
Qontrol::Constraint::ConstraintSet::exist
bool exist(std::string constraint_name)
Check if a constraint existe in the constraint set.
Definition: ConstraintSet.hpp:129
Qontrol::Constraint::ConstraintSet::getConstraints
std::vector< std::shared_ptr< Constraint::GenericConstraint > > getConstraints()
Get the constraint set.
Definition: ConstraintSet.hpp:176
Qontrol::Constraint::ConstraintSet::update
void update(double dt)
Call the update function of all the constraints in the constraint set.
Definition: ConstraintSet.hpp:155
Qontrol::Constraint::ConstraintSet::remove
void remove(std::shared_ptr< Constraint::GenericConstraint > constraint_ptr)
Remove a constraint from the constraint set.
Definition: ConstraintSet.hpp:107
Qontrol::Constraint::ConstraintSet::getConstraintIndex
int getConstraintIndex(std::string constraint_name)
Get the index of a constraint in the constraint set given its name.
Definition: ConstraintSet.hpp:139
Qontrol::Constraint::ConstraintSet
Handle all the constraints added to the problem.
Definition: ConstraintSet.hpp:42
Qontrol::Constraint::ConstraintSet::add
std::shared_ptr< ControlInput< output > > add(std::string constraint_name)
Add a custom constraint to the constraint set.
Definition: ConstraintSet.hpp:56
Qontrol::Constraint::ConstraintSet::remove
void remove(std::string constraint_name)
Remove a constraint from the constraint set given its name.
Definition: ConstraintSet.hpp:116
Qontrol::Constraint::ConstraintSet::add
std::shared_ptr< Constraint::GenericConstraint > add(std::string constraint_name, int constraint_size)
Add an implemented constaint in the constraint set.
Definition: ConstraintSet.hpp:77