- 3.0.2 optimal control module.
OptVector.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 
7 #pragma once
8 
9 namespace ct {
10 namespace optcon {
11 namespace tpl {
12 
13 
20 template <typename SCALAR>
21 class OptVector
22 {
23 public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25  using VectorXs = Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>;
26  using VectorXi = Eigen::Matrix<int, Eigen::Dynamic, 1>;
27  using MapVecXs = Eigen::Map<VectorXs>;
28  using MapVecXi = Eigen::Map<VectorXi>;
29  using MapConstVecXs = Eigen::Map<const VectorXs>;
30 
31  OptVector() = delete;
32 
39  OptVector(const size_t n) : updateCount_(0)
40  {
41  x_.resize(n);
42  x_.setZero();
43  xInit_.resize(n);
44  xLb_.resize(n);
45  xUb_.resize(n);
46  xLb_.setConstant(std::numeric_limits<SCALAR>::lowest());
47  xUb_.setConstant(std::numeric_limits<SCALAR>::max());
48  xMul_.resize(n);
49  xMul_.setZero();
50  xState_.resize(n);
51  xState_.setZero();
52  zUpper_.resize(n);
53  zUpper_.setZero();
54  zLow_.resize(n);
55  zLow_.setZero();
56  }
57 
64  void resizeConstraintVars(size_t m)
65  {
66  zMul_.resize(m + 1);
67  zMul_.setZero();
68  zState_.resize(m + 1);
69  zState_.setZero();
70  }
71 
72  void reset() { updateCount_ = 0; }
76  virtual ~OptVector() = default;
82  void resizeOptimizationVars(const size_t size)
83  {
84  x_.resize(size);
85  xInit_.resize(size);
86  xLb_.resize(size);
87  xUb_.resize(size);
88  lambda_.resize(size);
89  zUpper_.resize(size);
90  zLow_.resize(size);
91  }
92 
93 
97  void setZero()
98  {
99  x_.setZero();
100  xInit_.setZero();
101  lambda_.setZero();
102  zUpper_.setZero();
103  zLow_.setZero();
104  }
105 
107  {
108  x_.setRandom();
109  xInit_.setRandom();
110  }
111 
112  void setInitialGuess(const VectorXs& xinit)
113  {
114  x_ = xinit;
115  xInit_ = xinit;
116  }
117 
125  bool checkOptimizationVarDimension(const unsigned int n)
126  {
127  bool xDim = x_.size() == n ? true : false;
128  bool xLDim = xLb_.size() == n ? true : false;
129  bool xUDim = xUb_.size() == n ? true : false;
130  return xDim && xLDim && xUDim;
131  }
132 
139  void setBounds(const VectorXs& xLb, const VectorXs& xUb)
140  {
141  xLb_ = xLb;
142  xUb_ = xUb;
143  }
144 
145 
151  void getLowerBounds(MapVecXs& x) const { x = xLb_; }
157  void getUpperBounds(MapVecXs& x) const { x = xUb_; }
166  void getOptimizationMultState(const size_t n, MapVecXs& xMul, MapVecXi& xState) const
167  {
168  assert(n == xMul_.size());
169  assert(n == xState_.size());
170  xMul = xMul_;
171  xState = xState_;
172  }
173 
182  void getConstraintsMultState(const size_t m, MapVecXs& zMul, MapVecXi& zState) const
183  {
184  assert(m == zMul_.size());
185  assert(m == zState_.size());
186  zMul = zMul_;
187  zState = zState_;
188  }
189 
195  size_t size() const { return x_.size(); }
203  void getBoundMultipliers(size_t n, MapVecXs& low, MapVecXs& up) const
204  {
205  assert(n == static_cast<size_t>(zLow_.size()));
206  low = zLow_;
207  up = zUpper_;
208  }
209 
216  void getLambdaVars(size_t m, MapVecXs& x) const
217  {
218  assert(m == static_cast<size_t>(lambda_.size()));
219  x = lambda_;
220  }
221 
228  void getOptimizationVars(size_t n, MapVecXs& x) const
229  {
230  assert(n == x_.size());
231  x = x_;
232  }
233 
234  const VectorXs& getOptimizationVars() const { return x_; }
235  void getInitialGuess(size_t n, MapVecXs& x) const
236  {
237  assert(n == static_cast<size_t>(xInit_.size()));
238  x = xInit_;
239  }
240 
251  const MapConstVecXs& zL,
252  const MapConstVecXs& zU,
253  const MapConstVecXs& lambda)
254  {
255  x_ = x;
256  zLow_ = zL;
257  zUpper_ = zU;
258  lambda_ = lambda;
259  }
260 
272  const MapVecXs& xMul,
273  const MapVecXi& xState,
274  const MapVecXs& fMul,
275  const MapVecXi& fState)
276  {
277  x_ = x;
278  xMul_ = xMul;
279  xState_ = xState;
280  zMul_ = fMul;
281  zState_ = fState;
282  }
283 
291  {
292  x_ = x;
293  updateCount_++;
294  }
295 
297  {
298  x_ = x;
299  updateCount_++;
300  }
301 
302 
308  size_t getUpdateCount() const { return updateCount_; }
309 protected:
319  // Snopt variables
325  size_t updateCount_;
326 };
327 }
328 
330 }
331 }
VectorXs zLow_
Definition: OptVector.h:316
size_t updateCount_
Definition: OptVector.h:325
VectorXs xInit_
Definition: OptVector.h:311
VectorXs xUb_
Definition: OptVector.h:313
void getOptimizationVars(size_t n, MapVecXs &x) const
Gets the optimization variables.
Definition: OptVector.h:228
void getUpperBounds(MapVecXs &x) const
Gets the upper bounds of the optimization variables.
Definition: OptVector.h:157
void setZero()
Resets the optimization variables.
Definition: OptVector.h:97
bool checkOptimizationVarDimension(const unsigned int n)
Checks if the optimization variables have to correct size.
Definition: OptVector.h:125
void getConstraintsMultState(const size_t m, MapVecXs &zMul, MapVecXi &zState) const
Gets the constraint multiplier and state, used in the NLP solver SNOPT.
Definition: OptVector.h:182
VectorXs lambda_
Definition: OptVector.h:317
void setBounds(const VectorXs &xLb, const VectorXs &xUb)
Sets the optimization variable bounds.
Definition: OptVector.h:139
Eigen::Map< const VectorXs > MapConstVecXs
Definition: OptVector.h:29
virtual ~OptVector()=default
Destructor.
void setNewIpoptSolution(const MapConstVecXs &x, const MapConstVecXs &zL, const MapConstVecXs &zU, const MapConstVecXs &lambda)
Extracts the solution from ipopt and stores them into class variables.
Definition: OptVector.h:250
void getOptimizationMultState(const size_t n, MapVecXs &xMul, MapVecXi &xState) const
Return the state and the multiplier of the optimization variables, used in the NLP solver SNOPT...
Definition: OptVector.h:166
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: OptVector.h:25
void setInitialGuess(const VectorXs &xinit)
Definition: OptVector.h:112
void getLowerBounds(MapVecXs &x) const
Gets the lower bounds of the optimization variables.
Definition: OptVector.h:151
Eigen::Matrix< int, Eigen::Dynamic, 1 > VectorXi
Definition: OptVector.h:26
VectorXs xMul_
Definition: OptVector.h:320
VectorXs zMul_
Definition: OptVector.h:322
void resizeOptimizationVars(const size_t size)
Resizes the vectors of the optimization variables.
Definition: OptVector.h:82
void setOptimizationVars(const VectorXs &x)
Definition: OptVector.h:296
VectorXs x_
Definition: OptVector.h:310
Eigen::Map< VectorXi > MapVecXi
Definition: OptVector.h:28
VectorXs xLb_
Definition: OptVector.h:312
void getInitialGuess(size_t n, MapVecXs &x) const
Definition: OptVector.h:235
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
VectorXi zState_
Definition: OptVector.h:323
void reset()
Definition: OptVector.h:72
VectorXi xState_
Definition: OptVector.h:321
size_t getUpdateCount() const
Returns the update counter.
Definition: OptVector.h:308
void getBoundMultipliers(size_t n, MapVecXs &low, MapVecXs &up) const
Gets the bound multipliers used in the NLP solver IPOPT.
Definition: OptVector.h:203
void resizeConstraintVars(size_t m)
Resizes the vectors of the constraint variables to the correct size.
Definition: OptVector.h:64
Class containing and managing all the optimization variables used for in the NLP solver IPOPT and SNO...
Definition: OptVector.h:21
Eigen::Map< VectorXs > MapVecXs
Definition: OptVector.h:27
VectorXs zUpper_
Definition: OptVector.h:315
const VectorXs & getOptimizationVars() const
Definition: OptVector.h:234
void setOptimizationVars(const MapConstVecXs &x)
Sets the updates optimization variables from the NLP solver and updates the counter.
Definition: OptVector.h:290
OptVector(const size_t n)
{ Constructor resizing the vectors of the optimization variables to the correct size } ...
Definition: OptVector.h:39
void setRandomInitialGuess()
Definition: OptVector.h:106
void setNewSnoptSolution(const MapVecXs &x, const MapVecXs &xMul, const MapVecXi &xState, const MapVecXs &fMul, const MapVecXi &fState)
Extracts the solution from snopt and stores it into class variables.
Definition: OptVector.h:271
size_t size() const
Returns the number of optimization variables.
Definition: OptVector.h:195
void getLambdaVars(size_t m, MapVecXs &x) const
Gets the values of the constraint multipliers.
Definition: OptVector.h:216