structured_optimal_control_problem.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_STRUCTURED_OPTIMAL_CONTROL_PROBLEM_H_
26 #define SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_STRUCTURED_OCP_STRUCTURED_OPTIMAL_CONTROL_PROBLEM_H_
27 
29 
35 
36 #include <memory>
37 
38 namespace corbo {
39 
41 {
42  public:
43  using Ptr = std::shared_ptr<StructuredOptimalControlProblem>;
44  using UPtr = std::unique_ptr<StructuredOptimalControlProblem>;
45  using StateVector = Eigen::VectorXd;
46  using ControlVector = Eigen::VectorXd;
47 
51 
53 
54  OptimalControlProblemInterface::Ptr getInstance() const override { return std::make_shared<StructuredOptimalControlProblem>(); }
55 
56  int getControlInputDimension() const override { return _dynamics ? _dynamics->getInputDimension() : 0; }
57  int getStateDimension() const override { return _dynamics ? _dynamics->getStateDimension() : 0; }
58  int getN() const override { return _grid ? _grid->getN() : 0; }
59  double getFirstDt() const override { return _grid ? _grid->getFirstDt() : 0.0; }
60  bool isConstantControlAction() const override { return _grid ? _grid->hasConstantControls() : true; }
61 
62  bool providesFutureControls() const override;
63  bool providesFutureStates() const override;
64 
65  bool initialize() override;
66 
68  const Time& t, bool new_run, SignalTargetInterface* signal_target = nullptr, ReferenceTrajectoryInterface* xinit = nullptr,
69  ReferenceTrajectoryInterface* uinit = nullptr, const std::string& ns = "") override;
70 
71  bool getFirstControlInput(ControlVector& u0) const override;
72 
73  void setPreviousControlInput(const Eigen::Ref<const ControlVector>& u_prev, double dt) override
74  {
75  _u_prev = u_prev;
77  }
78 
79  void setPreviousControlInputDt(double dt) override { _u_prev_dt = dt; }
80 
81  void setBounds(const Eigen::VectorXd& x_lb, const Eigen::VectorXd& x_ub, const Eigen::VectorXd& u_lb, const Eigen::VectorXd& u_ub);
82  void setStateBounds(const Eigen::VectorXd& x_lb, const Eigen::VectorXd& x_ub);
83  void setControlBounds(const Eigen::VectorXd& u_lb, const Eigen::VectorXd& u_ub);
84 
85  void setStageCost(StageCost::Ptr stage_cost)
86  {
87  _ocp_modified = true;
88  _functions.stage_cost = stage_cost;
89  }
90 
91  void setFinalStageCost(FinalStageCost::Ptr final_stage_cost)
92  {
93  _ocp_modified = true;
94  _functions.final_stage_cost = final_stage_cost;
95  }
96 
98  {
99  _ocp_modified = true;
100  _functions.stage_equalities = stage_eq;
101  }
102 
104  {
105  _ocp_modified = true;
106  _functions.stage_inequalities = stage_ineq;
107  }
108 
110  {
111  _ocp_modified = true;
112  _functions.final_stage_constraints = final_stage_constraint;
113  }
114 
116  {
117  _ocp_modified = true;
118  _functions.stage_preprocessor = stage_preprocessor;
119  }
120 
122  {
123  reset();
124  _grid = grid;
125  if (_optim_prob) _optim_prob->setGraph(_edges, _grid);
126  }
128 
131  {
132  reset();
133  _optim_prob = optim_prob;
134  if (_edges && _grid) _optim_prob->setGraph(_edges, _grid);
135  }
137  {
138  reset();
139  _solver = solver;
140  }
141 
142  void setOptimizedTimeSeriesDt(double dt) { _resample_dt_hint = dt; } // TODO(roesmann): check if needed
143  void setOptimizedTimeSeriesTf(double tf) { _resample_tf = tf; } // TODO(roesmann): check if needed
144 
147 
148  void getTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max = CORBO_INF_DBL) override;
149 
150  double getCurrentObjectiveValue() override { return _objective_value; }
151 
152  const NlpFunctions& getNlpFunctions() const { return _functions; }
153 
154 #ifdef MESSAGE_SUPPORT
155  void toMessage(corbo::messages::OptimalControlProblem& message) const override;
156  void fromMessage(const corbo::messages::OptimalControlProblem& message, std::stringstream* issues = nullptr) override;
157 #endif
158 
159  void reset() override;
160 
161  protected:
164  OptimizationEdgeSet::Ptr _edges = std::make_shared<OptimizationEdgeSet>();
169 
170  double _objective_value = -1;
171 
172  ControlVector _u_prev; // we cache the last commanded control
173  double _u_prev_dt = 0; // time stamp of _u_prev
174 
175  TimeSeries::Ptr _ts_u_cache; // TODO(roesmann): check if needed
176  TimeSeries::Ptr _ts_x_cache; // TODO(roesmann): check if needed
177  double _ts_dt_cache = 0; // TODO(roesmann): check if needed
178 
179  bool _ocp_modified = true; // TODO(roesmann): are we really using this right now? Do we need this?
180 
181  double _resample_dt_hint = -1; // TODO(roesmann): check if needed
182  double _resample_tf = -1; // TODO(roesmann): check if needed
183 
184  bool _increase_n_if_infeas = false;
185 };
186 
188 
189 } // namespace corbo
190 
191 #endif // SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_STRUCTURED_OCP_STRUCTURED_OPTIMAL_CONTROL_PROBLEM_H_
OptimalControlProblemInterface::Ptr getInstance() const override
Scalar * x
void setSystemDynamics(SystemDynamicsInterface::Ptr dynamics)
std::shared_ptr< BaseHyperGraphOptimizationProblem > Ptr
#define FACTORY_REGISTER_OCP(type)
FinalStageConstraint::Ptr final_stage_constraints
Definition: nlp_functions.h:43
void setHyperGraphOptimizationProblem(BaseHyperGraphOptimizationProblem::Ptr optim_prob)
StageCost::Ptr stage_cost
Definition: nlp_functions.h:39
std::shared_ptr< OptimalControlProblemInterface > Ptr
Interface class for signal targets.
Representation of time stamps.
Definition: time.h:251
void setStageInequalityConstraint(StageInequalityConstraint::Ptr stage_ineq)
std::shared_ptr< StageEqualityConstraint > Ptr
std::shared_ptr< StageInequalityConstraint > Ptr
void getTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max=CORBO_INF_DBL) override
std::shared_ptr< FinalStageConstraint > Ptr
void setFinalStageConstraint(FinalStageConstraint::Ptr final_stage_constraint)
std::shared_ptr< OptimizationEdgeSet > Ptr
Definition: edge_set.h:77
void setStatisticsObject(OptimalControlProblemStatistics::Ptr statistics)
constexpr const double CORBO_INF_DBL
Representation for infinity (double version)
void setBounds(const Eigen::VectorXd &x_lb, const Eigen::VectorXd &x_ub, const Eigen::VectorXd &u_lb, const Eigen::VectorXd &u_ub)
bool getFirstControlInput(ControlVector &u0) const override
FinalStageCost::Ptr final_stage_cost
Definition: nlp_functions.h:40
void setDiscretizationGrid(DiscretizationGridInterface::Ptr grid)
std::unique_ptr< OptimalControlProblemInterface > UPtr
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
bool compute(const StateVector &x, ReferenceTrajectoryInterface &xref, ReferenceTrajectoryInterface &uref, ReferenceTrajectoryInterface *sref, const Time &t, bool new_run, SignalTargetInterface *signal_target=nullptr, ReferenceTrajectoryInterface *xinit=nullptr, ReferenceTrajectoryInterface *uinit=nullptr, const std::string &ns="") override
void setStageEqualityConstraint(StageEqualityConstraint::Ptr stage_eq)
BaseHyperGraphOptimizationProblem::Ptr _optim_prob
void setStateBounds(const Eigen::VectorXd &x_lb, const Eigen::VectorXd &x_ub)
Interface class for reference trajectories.
std::shared_ptr< DiscretizationGridInterface > Ptr
void setStagePreprocessor(StagePreprocessor::Ptr stage_preprocessor)
std::shared_ptr< NlpSolverInterface > Ptr
std::shared_ptr< StagePreprocessor > Ptr
std::shared_ptr< StageCost > Ptr
StageInequalityConstraint::Ptr stage_inequalities
Definition: nlp_functions.h:42
std::shared_ptr< TimeSeries > Ptr
Definition: time_series.h:64
std::shared_ptr< FinalStageCost > Ptr
void setPreviousControlInput(const Eigen::Ref< const ControlVector > &u_prev, double dt) override
StageEqualityConstraint::Ptr stage_equalities
Definition: nlp_functions.h:41
StagePreprocessor::Ptr stage_preprocessor
Definition: nlp_functions.h:44
void setControlBounds(const Eigen::VectorXd &u_lb, const Eigen::VectorXd &u_ub)
std::shared_ptr< SystemDynamicsInterface > Ptr
void setFinalStageCost(FinalStageCost::Ptr final_stage_cost)
OptimalControlProblemStatistics::Ptr getStatistics() const override
DiscretizationGridInterface::Ptr getDiscretizationGrid()


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