edge_cache.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_CACHE_H_
26 #define SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_EDGE_CACHE_H_
27 
28 #include <corbo-core/types.h>
29 
30 #include <Eigen/Core>
31 
32 #include <corbo-core/console.h>
33 
34 #include <vector>
35 
36 namespace corbo {
37 
38 // TODO(roesmann): we might change the cache to default construct the eigen vectors to avoid memory allocation during caching.
39 // we could think of using a container with id access rather than a stack as underlying datatype
40 class EdgeCache
41 {
42  public:
43  enum Recent : int { Current = 0, Previous = 1 };
44 
45  void reserveMemoryValues(int num_value_vectors) { _values.reserve(num_value_vectors); }
46  void reserveMemoryJacobians(int num_jacobians) { _jacobians.reserve(num_jacobians); }
47 
48  Eigen::VectorXd& pushValues(int value_dim)
49  {
50  PRINT_DEBUG_COND_ONCE(_values.size() >= _values.capacity(),
51  "EdgeCache::pushValues(): cache capacity reached; you might better reserve more space in advance.");
52 #if __cplusplus > 201402L
53  return _values.emplace_back();
54 #else
55  _values.emplace_back(value_dim);
56  return _values.back();
57 #endif
58  }
59 
60  void popValues() { _values.pop_back(); }
61  Eigen::VectorXd& topValues()
62  {
63  assert(!_values.empty());
64  return _values.back();
65  }
66  Eigen::VectorXd& recentValues(int reverse_idx)
67  {
68  assert(reverse_idx < _values.size());
69  return *(&_values.back() - reverse_idx);
70  }
71  int sizeValues() { return (int)_values.size(); }
72  void clearValues() { _values.clear(); }
73  Eigen::MatrixXd& pushJacobian(int value_dim, int param_dim)
74  {
75  PRINT_DEBUG_COND_ONCE(_jacobians.size() >= _values.capacity(),
76  "EdgeCache::pushJacobian(): cache capacity reached; you might better reserve more space in advance.");
77 #if __cplusplus > 201402L
78  return _values.emplace_back();
79 #else
80  _jacobians.emplace_back(value_dim, param_dim);
81  return _jacobians.back();
82 #endif
83  }
84 
85  void popJacobians() { _jacobians.pop_back(); }
86  Eigen::MatrixXd& topJacobians()
87  {
88  assert(!_jacobians.empty());
89  return _jacobians.back();
90  }
91  Eigen::MatrixXd& recentJacobians(int reverse_idx)
92  {
93  assert(reverse_idx < _jacobians.size());
94  return *(&_jacobians.back() - reverse_idx);
95  }
96  int sizeJacobians() { return (int)_jacobians.size(); }
97  void clearJacobians() { _jacobians.clear(); }
98 
99  void clear()
100  {
101  clearValues();
102  clearJacobians();
103  }
104 
105  bool getCustomFlag() const { return _custom_flag; }
106  void setCustomFlag(bool flag) { _custom_flag = flag; }
107 
108  protected:
109  std::vector<Eigen::VectorXd> _values;
110  std::vector<Eigen::MatrixXd> _jacobians;
111 
112  bool _custom_flag = false;
113 
114  public:
116 };
117 
118 } // namespace corbo
119 
120 #endif // SRC_OPTIMIZATION_INCLUDE_CORBO_OPTIMIZATION_HYPER_GRAPH_EDGE_CACHE_H_
#define PRINT_DEBUG_COND_ONCE(cond, msg)
Print msg-stream only if cond == true, only once and only if project is compiled in Debug-mode...
Definition: console.h:77
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:690
bool getCustomFlag() const
Definition: edge_cache.h:105
void setCustomFlag(bool flag)
Definition: edge_cache.h:106
void clearValues()
Definition: edge_cache.h:72
void popJacobians()
Definition: edge_cache.h:85
void reserveMemoryValues(int num_value_vectors)
Definition: edge_cache.h:45
Eigen::MatrixXd & pushJacobian(int value_dim, int param_dim)
Definition: edge_cache.h:73
std::vector< Eigen::MatrixXd > _jacobians
Definition: edge_cache.h:110
void clearJacobians()
Definition: edge_cache.h:97
int sizeJacobians()
Definition: edge_cache.h:96
std::vector< Eigen::VectorXd > _values
Definition: edge_cache.h:109
Eigen::VectorXd & topValues()
Definition: edge_cache.h:61
void reserveMemoryJacobians(int num_jacobians)
Definition: edge_cache.h:46
Eigen::MatrixXd & topJacobians()
Definition: edge_cache.h:86
void popValues()
Definition: edge_cache.h:60
Eigen::MatrixXd & recentJacobians(int reverse_idx)
Definition: edge_cache.h:91
Eigen::VectorXd & recentValues(int reverse_idx)
Definition: edge_cache.h:66
Eigen::VectorXd & pushValues(int value_dim)
Definition: edge_cache.h:48


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