shooting_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_SHOOTING_GRID_BASE_H_
26 #define SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_STRUCTURED_OCP_DISCRETIZATION_GRIDS_SHOOTING_GRID_BASE_H_
27 
29 
32 
34 
35 #include <memory>
36 
37 namespace corbo {
38 
40 {
41  public:
42  using Ptr = std::shared_ptr<ShootingGridBase>;
43  using UPtr = std::unique_ptr<ShootingGridBase>;
44 
46  {
48  std::vector<VectorVertex> u_seq;
50  };
51 
52  ShootingGridBase() = default;
53  virtual ~ShootingGridBase() = default;
54 
56  DiscretizationGridInterface::Ptr getInstance() const override = 0;
57 
58  GridUpdateResult update(const Eigen::VectorXd& x0, ReferenceTrajectoryInterface& xref, ReferenceTrajectoryInterface& uref, NlpFunctions& nlp_fun,
59  OptimizationEdgeSet& edges, SystemDynamicsInterface::Ptr dynamics, bool new_run, const Time& t,
60  ReferenceTrajectoryInterface* sref = nullptr, const Eigen::VectorXd* prev_u = nullptr, double prev_u_dt = 0,
61  ReferenceTrajectoryInterface* xinit = nullptr, ReferenceTrajectoryInterface* uinit = nullptr) override;
62 
63  double getFirstDt() const override { return getDt(); }
64  double getFinalTime() const override { return double(getN() - 1) * getDt(); }
65 
66  bool hasConstantControls() const override { return true; }
67  bool hasSingleDt() const override { return true; }
68  bool isTimeVariableGrid() const override { return !isDtFixedIntended(); }
69  bool isUniformGrid() const override { return true; }
70  bool providesStateTrajectory() const override { return true; } // TODO(roesmann): clarify
71 
72  bool getFirstControlInput(Eigen::VectorXd& u0) override;
73 
74  void getStateAndControlTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max = CORBO_INF_DBL) const override;
75 
76  void clear() override;
77 
78  bool isEmpty() const override { return _intervals.empty(); }
79  bool isValid() const { return !_intervals.empty() && !_intervals.front().u_seq.empty(); }
80 
81  void setN(int n, bool try_resample = true) override
82  {
83  clear(); // resampling not yet implemented
84  setNRef(n);
85  }
86  void setInitialDt(double dt) override { setDtRef(dt); }
87  double getInitialDt() const override { return getDtRef(); }
88  int getInitialN() const override { return getNRef(); }
89 
90  int getNRef() const { return _n_ref; }
91  int getN() const override;
92  void setNumControlsPerShootingInterval(int num_u_per_interv) { _num_u_per_interv_ref = num_u_per_interv; }
93  void setNumControlsPerShootingInterval(int num_u_per_interv, bool intermediate_x_constraints)
94  {
95  setNumControlsPerShootingInterval(num_u_per_interv);
96  _intermediate_x_constraints = intermediate_x_constraints;
97  }
98  void setNRef(int n);
99  void setDtRef(double dt) { _dt_ref = dt; }
100  double getDtRef() const { return _dt_ref; }
101  double getDt() const { return _dt.value(); }
102  void setWarmStart(bool active) { _warm_start = active; }
103 
105 
107 
109  {
110  _xf_fixed = xf_fixed;
111  setModified(true);
112  }
113 
114  // VertexSet methods
115  std::vector<VertexInterface*>& getActiveVertices() override { return _active_vertices; }
116  void getVertices(std::vector<VertexInterface*>& vertices) override;
117 
118 #ifdef MESSAGE_SUPPORT
119  void fromMessage(const messages::DiscretizationGrid& message, std::stringstream* issues) override {}
120  void toMessage(messages::DiscretizationGrid& message) const override {}
121 #endif
122 
123  protected:
124  void initializeSequences(const Eigen::VectorXd& x0, const Eigen::VectorXd& xf, ReferenceTrajectoryInterface& uref, NlpFunctions& nlp_fun);
125  void initializeSequences(const Eigen::VectorXd& x0, const Eigen::VectorXd& xf, ReferenceTrajectoryInterface& xref,
127  void warmStartShifting(const Eigen::VectorXd& x0);
128 
129  virtual bool adaptGrid(bool new_run, NlpFunctions& nlp_fun) { return false; } // A subclass might add a grid adaptation, returns true if adapted
130 
131  int findNearestShootingInterval(const Eigen::VectorXd& x0);
132 
133  void updateBounds(const NlpFunctions& nlp_fun);
134 
135  void resampleTrajectory(int n_new, NlpFunctions& nlp_fun);
136 
137  bool checkAndInitializeXfFixedFlags(int dim_x);
138 
139  virtual void createEdges(NlpFunctions& nlp_fun, OptimizationEdgeSet& edges, SystemDynamicsInterface::Ptr dynamics) = 0;
140 
141  virtual bool isDtFixedIntended() const { return true; }
142  virtual bool isMovingHorizonWarmStartActive() const { return _warm_start; }
143  virtual bool isGridAdaptActive() const { return false; }
144 
145  void computeActiveVertices() override;
146 
147  protected:
148  virtual bool isXfShootingNode() const { return true; }
149 
151 
152  std::vector<ShootingInterval> _intervals;
155  std::vector<VertexInterface*> _active_vertices;
156 
157  int _num_u_per_interv_ref = 1; // 1 -> full discretization
158  int _n_ref = 11;
159  int _n_adapt = 0; // if adaption is on and warmstart of, we might use this n instead of n_ref (only if n_adapt > 0)
160  double _dt_ref = 0.1;
161 
162  bool _warm_start = false;
163  bool _first_run = true;
164 
165  // might be required if the last dt should be fixed or if dt is not fixed
166  Eigen::Matrix<bool, -1, 1> _xf_fixed;
167  double _dt_lb = 0;
169 
170  bool _full_discretization = true;
172 };
173 
174 } // namespace corbo
175 
176 #endif // SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_STRUCTURED_OCP_DISCRETIZATION_GRIDS_SHOOTING_GRID_BASE_H_
void setNumControlsPerShootingInterval(int num_u_per_interv)
virtual bool isDtFixedIntended() const
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)
std::unique_ptr< DiscretizationGridInterface > UPtr
double getFirstDt() const override
void computeActiveVertices() override
void setInitialDt(double dt) override
Generic interface class for discretization grids.
bool isTimeVariableGrid() const override
void setXfFixed(const Eigen::Matrix< bool, -1, 1 > &xf_fixed)
bool isEmpty() const override
virtual bool adaptGrid(bool new_run, NlpFunctions &nlp_fun)
void setNumControlsPerShootingInterval(int num_u_per_interv, bool intermediate_x_constraints)
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
Definition: Memory.h:690
bool hasSingleDt() const override
Vertex implementation for scalar values.
Definition: scalar_vertex.h:50
NumericalIntegratorExplicitInterface::Ptr _integrator
void updateBounds(const NlpFunctions &nlp_fun)
Representation of time stamps.
Definition: time.h:251
void resampleTrajectory(int n_new, NlpFunctions &nlp_fun)
double getFinalTime() const override
Vector based vertex with support for partially fixed components.
Eigen::Matrix< bool, -1, 1 > _xf_fixed
std::vector< VertexInterface * > _active_vertices
DiscretizationGridInterface::Ptr getInstance() const override=0
Return a newly created shared instance of the implemented class.
bool hasConstantControls() const override
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
std::vector< ShootingInterval > _intervals
void setModified(bool modified)
Definition: vertex_set.h:82
void getVertices(std::vector< VertexInterface *> &vertices) override
constexpr const double CORBO_INF_DBL
Representation for infinity (double version)
int getInitialN() const override
void setWarmStart(bool active)
int getN() const override
void setConsiderIntermediateStateConstraints(bool active)
double getInitialDt() const override
int findNearestShootingInterval(const Eigen::VectorXd &x0)
const double & value() const
Get underlying value.
std::shared_ptr< NumericalIntegratorExplicitInterface > Ptr
virtual ~ShootingGridBase()=default
bool checkAndInitializeXfFixedFlags(int dim_x)
virtual void createEdges(NlpFunctions &nlp_fun, OptimizationEdgeSet &edges, SystemDynamicsInterface::Ptr dynamics)=0
Interface class for reference trajectories.
std::shared_ptr< DiscretizationGridInterface > Ptr
virtual bool isGridAdaptActive() const
void initializeSequences(const Eigen::VectorXd &x0, const Eigen::VectorXd &xf, ReferenceTrajectoryInterface &uref, NlpFunctions &nlp_fun)
virtual bool isXfShootingNode() const
Vertex implementation that stores an Eigen::VectorXd (dynamic dimension)
Definition: vector_vertex.h:51
std::shared_ptr< TimeSeries > Ptr
Definition: time_series.h:64
bool providesStateTrajectory() const override
bool getFirstControlInput(Eigen::VectorXd &u0) override
PartiallyFixedVectorVertex _xf
void warmStartShifting(const Eigen::VectorXd &x0)
PlainMatrixType mat * n
Definition: eigenvalues.cpp:41
std::vector< VertexInterface * > & getActiveVertices() override
std::shared_ptr< SystemDynamicsInterface > Ptr
virtual bool isMovingHorizonWarmStartActive() const
bool isUniformGrid() const override
void setN(int n, bool try_resample=true) override
Return dimension of the control input dimension in the grid.
void setNumericalIntegrator(NumericalIntegratorExplicitInterface::Ptr integrator)


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