edge_interface.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_INTERFACE_H_
26 #define SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_EDGE_INTERFACE_H_
27 
28 #include <corbo-core/types.h>
30 
31 #include <memory>
32 
33 namespace corbo {
34 
35 class VertexInterface; // Forward declaration, include in subclass!
36 class VertexSetInterface; // Forward declaration, include in subclass!
37 class EdgeSetInterface; // Forward declaration for friend access
38 
55 {
56  friend class EdgeSetInterface;
57 
58  public:
59  using Ptr = std::shared_ptr<EdgeInterface>;
60  using UPtr = std::unique_ptr<EdgeInterface>;
61 
63  virtual ~EdgeInterface() {}
64 
66  virtual int getDimension() const = 0;
67 
78  virtual void computeValues(Eigen::Ref<Eigen::VectorXd> values) = 0;
79 
80  virtual double computeSumOfValues()
81  {
82  Eigen::VectorXd values(getDimension());
83  computeValues(values);
84  return values.sum();
85  }
86 
87  virtual double computeSquaredNormOfValues()
88  {
89  Eigen::VectorXd values(getDimension());
90  computeValues(values);
91  return values.squaredNorm();
92  }
93 
95  virtual int getNumVertices() const = 0;
97  virtual int verticesDimension() const = 0;
98 
100  virtual VertexInterface* getVertexRaw(int idx) = 0;
101  virtual const VertexInterface* getVertex(int idx) const = 0;
102 
105 };
106 
107 class BaseEdge : public EdgeInterface
108 {
109  friend class EdgeSetInterface;
110 
111  public:
112  using Ptr = std::shared_ptr<BaseEdge>;
113  using UPtr = std::unique_ptr<BaseEdge>;
114 
116  int getDimension() const override = 0;
117 
134  virtual bool isLeastSquaresForm() const { return false; }
135 
137  virtual bool isLinear() const { return false; }
139  virtual bool providesJacobian() const { return false; }
141  virtual bool providesHessian() const { return false; }
142 
143  void computeValues(Eigen::Ref<Eigen::VectorXd> values) override = 0;
144 
157  virtual void computeJacobian(int vtx_idx, Eigen::Ref<Eigen::MatrixXd> block_jacobian, const double* multipliers = nullptr);
158 
177  virtual void computeHessianInc(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
178  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
179 
180  virtual void computeHessian(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
181  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
182 
183  virtual void computeHessianInc(int vtx_idx_i, int vtx_idx_j, Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr,
184  double weight = 1.0);
185 
188 
189  void reserveCacheMemory(int num_value_vectors, int num_jacobians)
190  {
191  reserveValuesCacheMemory(num_value_vectors);
192  reserveJacobiansCacheMemory(num_jacobians);
193  }
194  void reserveValuesCacheMemory(int num_value_vectors) { _cache.reserveMemoryValues(num_value_vectors); }
195  void reserveJacobiansCacheMemory(int num_jacobians) { _cache.reserveMemoryJacobians(num_jacobians); }
196 
198  void computeValuesCached() { computeValues(_cache.pushValues(getDimension())); }
199 
201  void computeSquaredNormOfValuesCached() { _cache.pushValues(1)[0] = computeSquaredNormOfValues(); }
202 
204  EdgeCache& getCache() { return _cache; }
205  const EdgeCache& getCache() const { return _cache; }
206 
208 
210  int getEdgeIdx() const { return _edge_idx; }
211 
212  protected:
213  int _edge_idx = 0;
215 };
216 
218 {
219  friend class EdgeSetInterface;
220 
221  public:
222  using Ptr = std::shared_ptr<BaseMixedEdge>;
223  using UPtr = std::unique_ptr<BaseMixedEdge>;
224 
225  int getDimension() const override { return getObjectiveDimension() + getEqualityDimension() + getInequalityDimension(); }
226 
227  virtual int getObjectiveDimension() const = 0;
228  virtual int getEqualityDimension() const = 0;
229  virtual int getInequalityDimension() const = 0;
230 
231  virtual bool isObjectiveLeastSquaresForm() const = 0;
232 
234  virtual bool isObjectiveLinear() const { return false; }
235  virtual bool isEqualityLinear() const { return false; }
236  virtual bool isInequalityLinear() const { return false; }
237 
238  virtual void precompute() = 0;
239  virtual void computeObjectiveValues(Eigen::Ref<Eigen::VectorXd> obj_values) = 0;
240  virtual void computeEqualityValues(Eigen::Ref<Eigen::VectorXd> eq_values) = 0;
241  virtual void computeInequalityValues(Eigen::Ref<Eigen::VectorXd> ineq_values) = 0;
242 
244  {
245  precompute();
246  computeObjectiveValues(values);
247  computeEqualityValues(values.segment(getObjectiveDimension(), getEqualityDimension()));
248  computeInequalityValues(values.tail(getInequalityDimension()));
249  }
250 
252  {
253  Eigen::VectorXd values(getObjectiveDimension());
254  computeObjectiveValues(values);
255  return values.sum();
256  }
257 
259  {
260  Eigen::VectorXd values(getObjectiveDimension());
261  computeObjectiveValues(values);
262  return values.squaredNorm();
263  }
264 
265  virtual void computeObjectiveJacobian(int vtx_idx, Eigen::Ref<Eigen::MatrixXd> block_jacobian, const double* multipliers = nullptr);
266  virtual void computeEqualityJacobian(int vtx_idx, Eigen::Ref<Eigen::MatrixXd> block_jacobian, const double* multipliers = nullptr);
267  virtual void computeInequalityJacobian(int vtx_idx, Eigen::Ref<Eigen::MatrixXd> block_jacobian, const double* multipliers = nullptr);
268  virtual void computeConstraintJacobians(int vtx_idx, Eigen::Ref<Eigen::MatrixXd> eq_jacobian, Eigen::Ref<Eigen::MatrixXd> ineq_jacobian,
269  const double* eq_multipliers = nullptr, const double* ineq_multipliers = nullptr);
270  virtual void computeJacobians(int vtx_idx, Eigen::Ref<Eigen::MatrixXd> obj_jacobian, Eigen::Ref<Eigen::MatrixXd> eq_jacobian,
271  Eigen::Ref<Eigen::MatrixXd> ineq_jacobian, const double* obj_multipliers = nullptr,
272  const double* eq_multipliers = nullptr, const double* ineq_multipliers = nullptr);
273 
274  virtual void computeObjectiveHessian(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
275  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
276  virtual void computeEqualityHessian(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
277  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
278  virtual void computeInequalityHessian(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
279  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
280  virtual void computeHessians(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& obj_jacobian_i,
281  const Eigen::Ref<const Eigen::MatrixXd>& eq_jacobian_i, const Eigen::Ref<const Eigen::MatrixXd>& ineq_jacobian_i,
282  Eigen::Ref<Eigen::MatrixXd> obj_hessian_ij, Eigen::Ref<Eigen::MatrixXd> eq_hessian_ij,
283  Eigen::Ref<Eigen::MatrixXd> ineq_hessian_ij, const double* multipliers_eq = nullptr,
284  const double* multipliers_ineq = nullptr, double weight_obj = 1.0);
285  virtual void computeConstraintHessians(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& eq_jacobian_i,
286  const Eigen::Ref<const Eigen::MatrixXd>& ineq_jacobian_i, Eigen::Ref<Eigen::MatrixXd> eq_hessian_ij,
287  Eigen::Ref<Eigen::MatrixXd> ineq_hessian_ij, const double* multipliers_eq = nullptr,
288  const double* multipliers_ineq = nullptr);
289 
290  virtual void computeObjectiveHessianInc(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
291  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
292  virtual void computeEqualityHessianInc(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
293  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
294  virtual void computeInequalityHessianInc(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& block_jacobian_i,
295  Eigen::Ref<Eigen::MatrixXd> block_hessian_ij, const double* multipliers = nullptr, double weight = 1.0);
296  virtual void computeHessiansInc(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& obj_jacobian_i,
297  const Eigen::Ref<const Eigen::MatrixXd>& eq_jacobian_i, const Eigen::Ref<const Eigen::MatrixXd>& ineq_jacobian_i,
298  Eigen::Ref<Eigen::MatrixXd> obj_hessian_ij, Eigen::Ref<Eigen::MatrixXd> eq_hessian_ij,
299  Eigen::Ref<Eigen::MatrixXd> ineq_hessian_ij, const double* multipliers_eq = nullptr,
300  const double* multipliers_ineq = nullptr, double weight_obj = 1.0);
301  virtual void computeConstraintHessiansInc(int vtx_idx_i, int vtx_idx_j, const Eigen::Ref<const Eigen::MatrixXd>& eq_jacobian_i,
302  const Eigen::Ref<const Eigen::MatrixXd>& ineq_jacobian_i, Eigen::Ref<Eigen::MatrixXd> eq_hessian_ij,
303  Eigen::Ref<Eigen::MatrixXd> ineq_hessian_ij, const double* multipliers_eq = nullptr,
304  const double* multipliers_ineq = nullptr);
305 
308 
309  void reserveCacheMemory(int num_value_vectors, int num_jacobians)
310  {
311  reserveValuesCacheMemory(num_value_vectors, num_value_vectors, num_value_vectors);
312  reserveJacobiansCacheMemory(num_jacobians, num_jacobians, num_jacobians);
313  }
314  void reserveValuesCacheMemory(int num_obj_values, int num_eq_values, int num_ineq_values)
315  {
316  _objective_cache.reserveMemoryValues(num_obj_values);
317  _equality_cache.reserveMemoryValues(num_eq_values);
318  _inequality_cache.reserveMemoryValues(num_ineq_values);
319  }
320  void reserveJacobiansCacheMemory(int num_obj_jacobians, int num_eq_jacobians, int num_ineq_jacobians)
321  {
322  _objective_cache.reserveMemoryJacobians(num_obj_jacobians);
323  _equality_cache.reserveMemoryJacobians(num_eq_jacobians);
324  _inequality_cache.reserveMemoryJacobians(num_ineq_jacobians);
325  }
326 
328  void computeObjectiveValuesCached() { computeObjectiveValues(_objective_cache.pushValues(getObjectiveDimension())); }
330  void computeSquaredNormOfObjectiveValuesCached() { _objective_cache.pushValues(1)[0] = computeSquaredNormOfObjectiveValues(); }
332  void computeEqualityValuesCached() { computeEqualityValues(_equality_cache.pushValues(getEqualityDimension())); }
334  void computeInequalityValuesCached() { computeInequalityValues(_inequality_cache.pushValues(getInequalityDimension())); }
335 
336  EdgeCache& getObjectiveCache() { return _objective_cache; }
337  const EdgeCache& getObjectiveCache() const { return _objective_cache; }
338 
339  EdgeCache& getEqualityCache() { return _equality_cache; }
340  const EdgeCache& getEqualityCache() const { return _equality_cache; }
341 
342  EdgeCache& getInequalityCache() { return _inequality_cache; }
343  const EdgeCache& getInequalityCache() const { return _inequality_cache; }
344 
346 
348  int getEdgeObjectiveIdx() const { return _edge_idx_obj; }
349  int getEdgeEqualityIdx() const { return _edge_idx_eq; }
350  int getEdgeInequalityIdx() const { return _edge_idx_ineq; }
351 
352  protected:
353  int _edge_idx_obj = 0;
354  int _edge_idx_eq = 0;
355  int _edge_idx_ineq = 0;
356 
360 };
361 
362 } // namespace corbo
363 
364 #endif // SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_EDGE_INTERFACE_H_
EdgeCache & getObjectiveCache()
virtual bool providesJacobian() const
Return true if a custom Jacobian is provided (e.g. computeJacobian() is overwritten) ...
virtual int getDimension() const =0
Get dimension of the edge (dimension of the cost-function/constraint value vector) ...
std::unique_ptr< EdgeInterface > UPtr
virtual bool providesHessian() const
Return true if a custom Hessian is provided (e.g. computeHessian() is overwritten) ...
const EdgeCache & getCache() const
int getEdgeIdx() const
Retrieve current edge index (warning, this value is determined within the related HyperGraph) ...
virtual int getNumVertices() const =0
Return number of attached vertices.
const EdgeCache & getInequalityCache() const
void computeValuesCached()
Call computeValues() and store result to previously allocated internal cache (call allocateInteralVal...
virtual bool isInequalityLinear() const
int getEdgeObjectiveIdx() const
Retrieve current edge index (warning, this value is determined within the related HyperGraph) ...
void computeSquaredNormOfValuesCached()
compute the specialied squared-norm method for computing the values (note only the first element in t...
Generic interface class for vertices.
std::shared_ptr< EdgeInterface > Ptr
const EdgeCache & getEqualityCache() const
virtual bool isLeastSquaresForm() const
Defines if the edge is formulated as Least-Squares form.
void computeValues(Eigen::Ref< Eigen::VectorXd > values) final
Compute function values.
void computeSquaredNormOfObjectiveValuesCached()
compute the specialied squared-norm method for computing the values (note only the first element in t...
void reserveCacheMemory(int num_value_vectors, int num_jacobians)
Abstract class representing a set of edges.
Definition: edge_set.h:46
void reserveValuesCacheMemory(int num_value_vectors)
void computeInequalityValuesCached()
Call computeInequalityValues() and store result to the internal cache.
virtual ~EdgeInterface()
Virtual destructor.
int getEdgeEqualityIdx() const
void reserveValuesCacheMemory(int num_obj_values, int num_eq_values, int num_ineq_values)
int getDimension() const override
Get dimension of the edge (dimension of the cost-function/constraint value vector) ...
virtual void computeValues(Eigen::Ref< Eigen::VectorXd > values)=0
Compute function values.
virtual double computeSumOfObjectiveValues()
void reserveJacobiansCacheMemory(int num_obj_jacobians, int num_eq_jacobians, int num_ineq_jacobians)
virtual int verticesDimension() const =0
Return the combined dimension of all attached vertices (excluding fixed vertex components) ...
virtual bool isLinear() const
Return true if the edge is linear (and hence its Hessian is always zero)
void computeObjectiveValuesCached()
Call computeObjectiveValues() and store result to the internal cache.
virtual VertexInterface * getVertexRaw(int idx)=0
Get access to vertex with index idx (0 <= idx < numVertices)
EdgeCache & getInequalityCache()
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
int getNumFiniteVerticesUpperBounds() const
virtual bool isEqualityLinear() const
EdgeCache & getCache()
Retreive values computed previously via computeValuesCached()
virtual double computeSquaredNormOfValues()
virtual double computeSquaredNormOfObjectiveValues()
EdgeCache & getEqualityCache()
void reserveJacobiansCacheMemory(int num_jacobians)
int getNumFiniteVerticesLowerBounds() const
int getEdgeInequalityIdx() const
void reserveCacheMemory(int num_value_vectors, int num_jacobians)
Generic interface class for edges.
virtual const VertexInterface * getVertex(int idx) const =0
virtual bool isObjectiveLinear() const
Return true if the edge is linear (and hence its Hessian is always zero)
void computeEqualityValuesCached()
Call computeEqualityValues() and store result to the internal cache.
virtual double computeSumOfValues()
const EdgeCache & getObjectiveCache() const


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:06:51