edge_set.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_EDGE_SET_H_
26 #define SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_EDGE_SET_H_
27 
29 
30 #include <corbo-core/console.h>
31 
32 #include <memory>
33 #include <vector>
34 
35 namespace corbo {
36 
46 class EdgeSetInterface
47 {
48  public:
49  using Ptr = std::shared_ptr<EdgeSetInterface>;
50 
52  virtual ~EdgeSetInterface() {}
53 
55  virtual void computeEdgeIndices() = 0;
56  virtual void reserveEdgeCacheMemory(int est_value_cache_size, int est_jacobians_cache_size) = 0;
57 
58  virtual void clear() = 0;
59 
60  void setModified(bool modified) { _modified = modified; }
61  bool isModified() const { return _modified; }
62 
63  protected:
64  void setEdgeIdx(BaseEdge& edge, int idx) { edge._edge_idx = idx; }
65  void setEdgeIdx(BaseMixedEdge& edge, int obj_idx, int eq_idx, int ineq_idx)
66  {
67  edge._edge_idx_obj = obj_idx;
68  edge._edge_idx_eq = eq_idx;
69  edge._edge_idx_ineq = ineq_idx;
70  }
71  bool _modified = true;
72 };
73 
74 class OptimizationEdgeSet : public EdgeSetInterface
75 {
76  public:
77  using Ptr = std::shared_ptr<OptimizationEdgeSet>;
78 
79  void clear() override;
80 
81  void computeEdgeIndices() override;
82  void registerEdgesAtVertices(VertexSetInterface& vertices);
84 
85  void getDimensions(int& non_lsq_obj_dim, int& lsq_obj_dim, int& eq_dim, int& ineq_dim);
86 
87  void addEdges(std::initializer_list<BaseEdge::Ptr> objective_edges, std::initializer_list<BaseEdge::Ptr> lsq_objective_edges,
88  std::initializer_list<BaseEdge::Ptr> equality_edges, std::initializer_list<BaseEdge::Ptr> inequality_edges,
89  std::initializer_list<BaseMixedEdge::Ptr> mixed_edges)
90  {
91  setModified(true);
92  if (objective_edges.size() > 0) _objectives.insert(_objectives.end(), objective_edges.begin(), objective_edges.end());
93  if (lsq_objective_edges.size() > 0) _lsq_objectives.insert(_lsq_objectives.end(), lsq_objective_edges.begin(), lsq_objective_edges.end());
94  if (equality_edges.size() > 0) _equalities.insert(_equalities.end(), equality_edges.begin(), equality_edges.end());
95  if (inequality_edges.size() > 0) _inequalities.insert(_inequalities.end(), inequality_edges.begin(), inequality_edges.end());
96  if (mixed_edges.size() > 0) _mixed.insert(_mixed.end(), mixed_edges.begin(), mixed_edges.end());
97  }
98 
99  void addEdges(std::initializer_list<BaseEdge::Ptr> objective_edges, std::initializer_list<BaseEdge::Ptr> equality_edges,
100  std::initializer_list<BaseEdge::Ptr> inequality_edges, std::initializer_list<BaseMixedEdge::Ptr> mixed_edges)
101  {
102  setModified(true);
103  if (objective_edges.size() > 0)
104  {
105  for (auto& edge : objective_edges)
106  {
107  if (edge->isLeastSquaresForm())
108  _lsq_objectives.push_back(edge);
109  else
110  _objectives.push_back(edge);
111  }
112  }
113  if (equality_edges.size() > 0) _equalities.insert(_equalities.end(), equality_edges.begin(), equality_edges.end());
114  if (inequality_edges.size() > 0) _inequalities.insert(_inequalities.end(), inequality_edges.begin(), inequality_edges.end());
115  if (mixed_edges.size() > 0) _mixed.insert(_mixed.end(), mixed_edges.begin(), mixed_edges.end());
116  }
117 
119  {
120  if (edge->isLeastSquaresForm())
122  else
123  {
124  getObjectiveEdgesRef().push_back(edge);
125  }
126 
127  // PRINT_ERROR_COND(edge->isLeastSquaresForm(),
128  // "OptimizationEdgeSet::addLsqObjectiveEdge(): The added edge does return isLeastSquaresForm() == true. You should add it
129  // "
130  // "using addLsqObjectiveEdge.");
131  }
133  {
134  PRINT_ERROR_COND(!edge->isLeastSquaresForm(),
135  "OptimizationEdgeSet::addLsqObjectiveEdge(): The added edge does not return isLeastSquaresForm() == true.");
136  getLsqObjectiveEdgesRef().push_back(edge);
137  }
138  void addEqualityEdge(BaseEdge::Ptr edge) { getEqualityEdgesRef().push_back(edge); }
139  void addInequalityEdge(BaseEdge::Ptr edge) { getInequalityEdgesRef().push_back(edge); }
140  void addMixedEdge(BaseMixedEdge::Ptr edge) { getMixedEdgesRef().push_back(edge); }
141 
142  std::vector<BaseEdge::Ptr>& getObjectiveEdgesRef()
143  {
144  setModified(true);
145  return _objectives;
146  }
147  const std::vector<BaseEdge::Ptr>& getObjectiveEdges() const { return _objectives; }
148  std::vector<BaseEdge::Ptr>& getLsqObjectiveEdgesRef()
149  {
150  setModified(true);
151  return _lsq_objectives;
152  }
153  const std::vector<BaseEdge::Ptr>& getLsqObjectiveEdges() const { return _lsq_objectives; }
154  std::vector<BaseEdge::Ptr>& getEqualityEdgesRef()
155  {
156  setModified(true);
157  return _equalities;
158  }
159  const std::vector<BaseEdge::Ptr>& getEqualityEdges() const { return _equalities; }
160  std::vector<BaseEdge::Ptr>& getInequalityEdgesRef()
161  {
162  setModified(true);
163  return _inequalities;
164  }
165  const std::vector<BaseEdge::Ptr>& getInequalityEdges() const { return _inequalities; }
166  std::vector<BaseMixedEdge::Ptr>& getMixedEdgesRef()
167  {
168  setModified(true);
169  return _mixed;
170  }
171  const std::vector<BaseMixedEdge::Ptr>& getMixedEdges() const { return _mixed; }
172 
173  bool hasOnlyLeastSquaresObjectives() const { return _objectives.empty(); }
174 
175  void reserveEdgeCacheMemory(int est_value_cache_size, int est_jacobians_cache_size) override;
176 
177  bool isEdgeCacheEmpty();
178  void clearEdgeCache();
179 
180  protected:
182  void computeObjectiveEdgeIndices(std::vector<BaseEdge::Ptr>& edges, int& idx, bool lsq_edges);
183  void computeEdgeIndices(std::vector<BaseEdge::Ptr>& edges, int& idx);
184  void computeEdgeIndices(std::vector<BaseMixedEdge::Ptr>& edges, int& idx_obj, int& idx_lsq_obj, int& idx_eq, int& idx_ineq);
185 
186  private:
187  std::vector<BaseEdge::Ptr> _objectives;
188  std::vector<BaseEdge::Ptr> _lsq_objectives;
189  std::vector<BaseEdge::Ptr> _equalities;
190  std::vector<BaseEdge::Ptr> _inequalities;
191  std::vector<BaseMixedEdge::Ptr> _mixed;
192 };
193 
194 } // namespace corbo
195 
196 #endif // SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_EDGE_SET_H_
corbo::OptimizationEdgeSet::computeEdgeIndices
void computeEdgeIndices() override
Precompute edge indices in the hyper-graph (e.g. for the Jacobian structure)
Definition: edge_set.cpp:53
corbo::OptimizationEdgeSet::getObjectiveEdgesRef
std::vector< BaseEdge::Ptr > & getObjectiveEdgesRef()
Definition: edge_set.h:164
corbo::OptimizationEdgeSet::Ptr
std::shared_ptr< OptimizationEdgeSet > Ptr
Definition: edge_set.h:99
corbo::EdgeSetInterface::_modified
bool _modified
Definition: edge_set.h:115
corbo::OptimizationEdgeSet::getLsqObjectiveEdges
const std::vector< BaseEdge::Ptr > & getLsqObjectiveEdges() const
Definition: edge_set.h:175
corbo::OptimizationEdgeSet::getInequalityEdges
const std::vector< BaseEdge::Ptr > & getInequalityEdges() const
Definition: edge_set.h:187
corbo::OptimizationEdgeSet::getMixedEdgesRef
std::vector< BaseMixedEdge::Ptr > & getMixedEdgesRef()
Definition: edge_set.h:188
PRINT_ERROR_COND
#define PRINT_ERROR_COND(cond, msg)
Print msg-stream only if cond == true.
Definition: console.h:186
corbo
Definition: communication/include/corbo-communication/utilities.h:37
corbo::OptimizationEdgeSet::addEqualityEdge
void addEqualityEdge(BaseEdge::Ptr edge)
Definition: edge_set.h:160
corbo::OptimizationEdgeSet::addObjectiveEdge
void addObjectiveEdge(BaseEdge::Ptr edge)
Definition: edge_set.h:140
corbo::EdgeSetInterface::reserveEdgeCacheMemory
virtual void reserveEdgeCacheMemory(int est_value_cache_size, int est_jacobians_cache_size)=0
corbo::EdgeSetInterface::~EdgeSetInterface
virtual ~EdgeSetInterface()
Virtual destructor.
Definition: edge_set.h:96
corbo::OptimizationEdgeSet::_objectives
std::vector< BaseEdge::Ptr > _objectives
Definition: edge_set.h:209
console.h
corbo::OptimizationEdgeSet::computeObjectiveEdgeIndices
void computeObjectiveEdgeIndices(std::vector< BaseEdge::Ptr > &edges, int &idx, bool lsq_edges)
Precompute overall edge indices in the hyper-graph (e.g. for the Jacobian structure)
Definition: edge_set.cpp:123
corbo::OptimizationEdgeSet::_inequalities
std::vector< BaseEdge::Ptr > _inequalities
Definition: edge_set.h:212
corbo::OptimizationEdgeSet::getMixedEdges
const std::vector< BaseMixedEdge::Ptr > & getMixedEdges() const
Definition: edge_set.h:193
corbo::OptimizationEdgeSet::clearEdgeCache
void clearEdgeCache()
Definition: edge_set.cpp:109
corbo::BaseMixedEdge::Ptr
std::shared_ptr< BaseMixedEdge > Ptr
Definition: edge_interface.h:244
corbo::OptimizationEdgeSet::reserveEdgeCacheMemory
void reserveEdgeCacheMemory(int est_value_cache_size, int est_jacobians_cache_size) override
Definition: edge_set.cpp:66
corbo::OptimizationEdgeSet::_mixed
std::vector< BaseMixedEdge::Ptr > _mixed
Definition: edge_set.h:213
corbo::OptimizationEdgeSet::_lsq_objectives
std::vector< BaseEdge::Ptr > _lsq_objectives
Definition: edge_set.h:210
corbo::OptimizationEdgeSet::addLsqObjectiveEdge
void addLsqObjectiveEdge(BaseEdge::Ptr edge)
Definition: edge_set.h:154
corbo::OptimizationEdgeSet::isEdgeCacheEmpty
bool isEdgeCacheEmpty()
Definition: edge_set.cpp:75
corbo::BaseEdge::Ptr
std::shared_ptr< BaseEdge > Ptr
Definition: edge_interface.h:134
corbo::EdgeSetInterface::Ptr
std::shared_ptr< EdgeSetInterface > Ptr
Definition: edge_set.h:93
edge_interface.h
corbo::OptimizationEdgeSet::hasOnlyLeastSquaresObjectives
bool hasOnlyLeastSquaresObjectives() const
Definition: edge_set.h:195
corbo::OptimizationEdgeSet::addInequalityEdge
void addInequalityEdge(BaseEdge::Ptr edge)
Definition: edge_set.h:161
corbo::OptimizationEdgeSet::getInequalityEdgesRef
std::vector< BaseEdge::Ptr > & getInequalityEdgesRef()
Definition: edge_set.h:182
corbo::OptimizationEdgeSet::_equalities
std::vector< BaseEdge::Ptr > _equalities
Definition: edge_set.h:211
corbo::OptimizationEdgeSet::getEqualityEdgesRef
std::vector< BaseEdge::Ptr > & getEqualityEdgesRef()
Definition: edge_set.h:176
corbo::EdgeSetInterface::setEdgeIdx
void setEdgeIdx(BaseEdge &edge, int idx)
Definition: edge_set.h:108
corbo::OptimizationEdgeSet::registerEdgesAtVertices
void registerEdgesAtVertices()
Definition: edge_set.cpp:196
corbo::OptimizationEdgeSet::addMixedEdge
void addMixedEdge(BaseMixedEdge::Ptr edge)
Definition: edge_set.h:162
corbo::EdgeSetInterface::computeEdgeIndices
virtual void computeEdgeIndices()=0
Precompute edge indices in the hyper-graph (e.g. for the Jacobian structure)
corbo::OptimizationEdgeSet::getEqualityEdges
const std::vector< BaseEdge::Ptr > & getEqualityEdges() const
Definition: edge_set.h:181
corbo::EdgeSetInterface::clear
virtual void clear()=0
corbo::OptimizationEdgeSet::getLsqObjectiveEdgesRef
std::vector< BaseEdge::Ptr > & getLsqObjectiveEdgesRef()
Definition: edge_set.h:170
corbo::OptimizationEdgeSet::getObjectiveEdges
const std::vector< BaseEdge::Ptr > & getObjectiveEdges() const
Definition: edge_set.h:169
corbo::EdgeSetInterface::isModified
bool isModified() const
Definition: edge_set.h:105
corbo::EdgeSetInterface::setModified
void setModified(bool modified)
Definition: edge_set.h:104
corbo::OptimizationEdgeSet::clear
void clear() override
Definition: edge_set.cpp:260
corbo::OptimizationEdgeSet::getDimensions
void getDimensions(int &non_lsq_obj_dim, int &lsq_obj_dim, int &eq_dim, int &ineq_dim)
Definition: edge_set.cpp:220
corbo::OptimizationEdgeSet::addEdges
void addEdges(std::initializer_list< BaseEdge::Ptr > objective_edges, std::initializer_list< BaseEdge::Ptr > lsq_objective_edges, std::initializer_list< BaseEdge::Ptr > equality_edges, std::initializer_list< BaseEdge::Ptr > inequality_edges, std::initializer_list< BaseMixedEdge::Ptr > mixed_edges)
Definition: edge_set.h:109


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:05:45