full_discretization_grid_base.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_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_STRUCTURED_OCP_DISCRETIZATION_GRIDS_FULL_DISCRETIZATION_GRID_BASE_H_
26 #define SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_STRUCTURED_OCP_DISCRETIZATION_GRIDS_FULL_DISCRETIZATION_GRID_BASE_H_
27 
29 
32 
34 
35 #include <memory>
36 
37 namespace corbo {
38 
40 {
41  public:
42  using Ptr = std::shared_ptr<FullDiscretizationGridBase>;
43  using UPtr = std::unique_ptr<FullDiscretizationGridBase>;
44 
46 
47  FullDiscretizationGridBase() = default;
48  virtual ~FullDiscretizationGridBase() = default;
49 
51  DiscretizationGridInterface::Ptr getInstance() const override = 0;
52 
53  GridUpdateResult update(const Eigen::VectorXd& x0, ReferenceTrajectoryInterface& xref, ReferenceTrajectoryInterface& uref, NlpFunctions& nlp_fun,
54  OptimizationEdgeSet& edges, SystemDynamicsInterface::Ptr dynamics, bool new_run, const Time& t,
55  ReferenceTrajectoryInterface* sref = nullptr, const Eigen::VectorXd* prev_u = nullptr, double prev_u_dt = 0,
56  ReferenceTrajectoryInterface* xinit = nullptr, ReferenceTrajectoryInterface* uinit = nullptr) override;
57 
58  double getFirstDt() const override { return getDt(); }
59  double getFinalTime() const override { return double(getN() - 1) * getDt(); }
60 
61  bool hasConstantControls() const override { return true; }
62  bool hasSingleDt() const override { return true; }
63  bool isTimeVariableGrid() const override { return !isDtFixedIntended(); }
64  bool isUniformGrid() const override { return true; }
65  bool providesStateTrajectory() const override { return true; }
66 
67  bool getFirstControlInput(Eigen::VectorXd& u0) override;
68 
69  void getStateAndControlTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max = CORBO_INF_DBL) const override;
70 
71  void clear() override;
72 
73  bool isEmpty() const override { return _x_seq.empty() || _u_seq.empty(); }
74  virtual bool isValid() const { return (_x_seq.size() == _u_seq.size()); }
75 
76  void setN(int n, bool try_resample = true) override;
77  void setInitialDt(double dt) override { setDtRef(dt); }
78  double getInitialDt() const override { return getDtRef(); }
79  int getInitialN() const override { return _n_ref; }
80 
81  int getNRef() const { return _n_ref; }
82  int getN() const override { return _x_seq.size() + 1; }
83  void setNRef(int n);
84  void setDtRef(double dt) { _dt_ref = dt; }
85  double getDtRef() const { return _dt_ref; }
86  double getDt() const { return _dt.value(); }
87  void setWarmStart(bool active) { _warm_start = active; }
88 
89  void setXfFixed(const Eigen::Matrix<bool, -1, 1>& xf_fixed)
90  {
91  _xf_fixed = xf_fixed;
92  setModified(true);
93  }
94 
96  void setCostIntegrationRule(CostIntegrationRule integration) { _cost_integration = integration; }
97 
98  const Eigen::VectorXd& getState(int k) const
99  {
100  assert(k <= getN());
101  if (k == _x_seq.size()) return _xf.values();
102  return _x_seq[k].values();
103  }
104 
105  // VertexSet methods
106  std::vector<VertexInterface*>& getActiveVertices() override { return _active_vertices; }
107  void getVertices(std::vector<VertexInterface*>& vertices) override; // TODO(roesmann) add dt, just in case
108 
109 #ifdef MESSAGE_SUPPORT
110  void fromMessage(const messages::DiscretizationGrid& message, std::stringstream* issues) override {}
111  void toMessage(messages::DiscretizationGrid& message) const override {}
112 #endif
113 
114  protected:
115  virtual void initializeSequences(const Eigen::VectorXd& x0, const Eigen::VectorXd& xf, ReferenceTrajectoryInterface& uref, NlpFunctions& nlp_fun);
116  virtual void initializeSequences(const Eigen::VectorXd& x0, const Eigen::VectorXd& xf, ReferenceTrajectoryInterface& xref,
118  virtual void warmStartShifting(const Eigen::VectorXd& x0);
119 
120  virtual bool adaptGrid(bool new_run, NlpFunctions& nlp_fun) { return false; } // A subclass might add a grid adaptation, returns true if adapted
121 
122  int findNearestState(const Eigen::VectorXd& x0);
123 
124  void updateBounds(const NlpFunctions& nlp_fun);
125 
126  virtual void resampleTrajectory(int n_new);
127 
128  bool checkAndInitializeXfFixedFlags(int dim_x);
129 
130  virtual void createEdges(NlpFunctions& nlp_fun, OptimizationEdgeSet& edges, SystemDynamicsInterface::Ptr dynamics) = 0;
131 
132  virtual bool isDtFixedIntended() const { return true; }
133  virtual bool isMovingHorizonWarmStartActive() const { return _warm_start; }
134  virtual bool isGridAdaptActive() const { return false; }
135 
136  void computeActiveVertices() override;
137 
138  FiniteDifferencesCollocationInterface::Ptr _fd_eval = std::make_shared<CrankNicolsonDiffCollocation>();
139 
140  std::vector<VectorVertex> _x_seq;
141  std::vector<VectorVertex> _u_seq;
143  std::vector<VertexInterface*> _active_vertices;
144 
145  const NlpFunctions* _nlp_fun = nullptr; // cache -> for bounds
146 
147  int _n_ref = 11;
148  int _n_adapt = 0; // if adaption is on and warmstart off, we might use this n instead of n_ref (only if n_adapt > 0)
149  double _dt_ref = 0.1;
150  ScalarVertex _dt; // we need a ScalarVertex to use the helper methods in stage_functions.cpp
151  bool _warm_start = false;
152  bool _first_run = true;
153 
154  // might be required if the last dt should be fixed or if dt is not fixed
155  Eigen::Matrix<bool, -1, 1> _xf_fixed;
156  double _dt_lb = 0;
158 
160 };
161 
162 } // namespace corbo
163 
164 #endif // SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_STRUCTURED_OCP_DISCRETIZATION_GRIDS_FULL_DISCRETIZATION_GRID_BASE_H_
void setXfFixed(const Eigen::Matrix< bool, -1, 1 > &xf_fixed)
int findNearestState(const Eigen::VectorXd &x0)
std::unique_ptr< DiscretizationGridInterface > UPtr
std::vector< VertexInterface * > _active_vertices
virtual ~FullDiscretizationGridBase()=default
Generic interface class for discretization grids.
Vertex implementation for scalar values.
Definition: scalar_vertex.h:50
virtual void createEdges(NlpFunctions &nlp_fun, OptimizationEdgeSet &edges, SystemDynamicsInterface::Ptr dynamics)=0
Representation of time stamps.
Definition: time.h:251
void setFiniteDifferencesCollocationMethod(FiniteDifferencesCollocationInterface::Ptr fd_eval)
Vector based vertex with support for partially fixed components.
virtual bool adaptGrid(bool new_run, NlpFunctions &nlp_fun)
void setCostIntegrationRule(CostIntegrationRule integration)
void setN(int n, bool try_resample=true) override
Return dimension of the control input dimension in the grid.
FiniteDifferencesCollocationInterface::Ptr _fd_eval
virtual void initializeSequences(const Eigen::VectorXd &x0, const Eigen::VectorXd &xf, ReferenceTrajectoryInterface &uref, NlpFunctions &nlp_fun)
void getVertices(std::vector< VertexInterface *> &vertices) override
void setModified(bool modified)
Definition: vertex_set.h:82
constexpr const double CORBO_INF_DBL
Representation for infinity (double version)
const double & value() const
Get underlying value.
const Eigen::VectorXd & getState(int k) const
GridUpdateResult update(const Eigen::VectorXd &x0, ReferenceTrajectoryInterface &xref, ReferenceTrajectoryInterface &uref, NlpFunctions &nlp_fun, OptimizationEdgeSet &edges, SystemDynamicsInterface::Ptr dynamics, bool new_run, const Time &t, ReferenceTrajectoryInterface *sref=nullptr, const Eigen::VectorXd *prev_u=nullptr, double prev_u_dt=0, ReferenceTrajectoryInterface *xinit=nullptr, ReferenceTrajectoryInterface *uinit=nullptr) override
bool getFirstControlInput(Eigen::VectorXd &u0) override
virtual void warmStartShifting(const Eigen::VectorXd &x0)
Interface class for reference trajectories.
std::shared_ptr< DiscretizationGridInterface > Ptr
std::vector< VertexInterface * > & getActiveVertices() override
DiscretizationGridInterface::Ptr getInstance() const override=0
Return a newly created shared instance of the implemented class.
const Eigen::VectorXd & values() const
Read-access to the underlying value vector.
void updateBounds(const NlpFunctions &nlp_fun)
std::shared_ptr< TimeSeries > Ptr
Definition: time_series.h:64
void getStateAndControlTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max=CORBO_INF_DBL) const override
Return state and control trajectory as time series object (shared instance)
PlainMatrixType mat * n
Definition: eigenvalues.cpp:41
std::shared_ptr< FiniteDifferencesCollocationInterface > Ptr
std::shared_ptr< SystemDynamicsInterface > Ptr


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