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 
40 class StructuredOptimalControlProblem : public OptimalControlProblemInterface
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;
101  }
102 
104  {
105  _ocp_modified = true;
106  _functions.stage_inequalities = stage_ineq;
107  }
108 
109  void setFinalStageConstraint(FinalStageConstraint::Ptr final_stage_constraint)
110  {
111  _ocp_modified = true;
112  _functions.final_stage_constraints = final_stage_constraint;
113  }
114 
115  void setStagePreprocessor(StagePreprocessor::Ptr stage_preprocessor)
116  {
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 
145  void setStatisticsObject(OptimalControlProblemStatistics::Ptr statistics) { _statistics = statistics; }
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_
corbo::SignalTargetInterface
Interface class for signal targets.
Definition: signal_target_interface.h:84
corbo::OptimizationEdgeSet::Ptr
std::shared_ptr< OptimizationEdgeSet > Ptr
Definition: edge_set.h:99
corbo::NlpFunctions::stage_inequalities
StageInequalityConstraint::Ptr stage_inequalities
Definition: nlp_functions.h:64
corbo::StructuredOptimalControlProblem::getInstance
OptimalControlProblemInterface::Ptr getInstance() const override
Definition: structured_optimal_control_problem.h:98
corbo::StagePreprocessor::Ptr
std::shared_ptr< StagePreprocessor > Ptr
Definition: stage_preprocessor.h:66
corbo::StructuredOptimalControlProblem::~StructuredOptimalControlProblem
virtual ~StructuredOptimalControlProblem()
Definition: structured_optimal_control_problem.h:96
corbo::OptimalControlProblemInterface::ControlVector
Eigen::VectorXd ControlVector
Definition: optimal_control_problem_interface.h:94
system_dynamics_interface.h
StructuredOptimalControlProblem
Definition: test_structured_optimal_control_problem.cpp:44
corbo::StructuredOptimalControlProblem::setControlBounds
void setControlBounds(const Eigen::VectorXd &u_lb, const Eigen::VectorXd &u_ub)
Definition: structured_optimal_control_problem.cpp:204
corbo::ReferenceTrajectoryInterface
Interface class for reference trajectories.
Definition: reference_trajectory.h:82
corbo::OptimalControlProblemStatistics::Ptr
std::shared_ptr< OptimalControlProblemStatistics > Ptr
Definition: optimal_control/include/corbo-optimal-control/statistics.h:81
corbo::StructuredOptimalControlProblem::providesFutureStates
bool providesFutureStates() const override
Definition: structured_optimal_control_problem.cpp:74
corbo::StructuredOptimalControlProblem::setSystemDynamics
void setSystemDynamics(SystemDynamicsInterface::Ptr dynamics)
Definition: structured_optimal_control_problem.h:173
corbo::StructuredOptimalControlProblem::setPreviousControlInput
void setPreviousControlInput(const Eigen::Ref< const ControlVector > &u_prev, double dt) override
Definition: structured_optimal_control_problem.h:117
corbo
Definition: communication/include/corbo-communication/utilities.h:37
corbo::StructuredOptimalControlProblem::getNlpFunctions
const NlpFunctions & getNlpFunctions() const
Definition: structured_optimal_control_problem.h:196
corbo::StructuredOptimalControlProblem::StateVector
Eigen::VectorXd StateVector
Definition: structured_optimal_control_problem.h:89
corbo::NlpFunctions
Definition: nlp_functions.h:58
corbo::StructuredOptimalControlProblem::initialize
bool initialize() override
Definition: structured_optimal_control_problem.cpp:76
corbo::StructuredOptimalControlProblem
Definition: structured_optimal_control_problem.h:62
corbo::StructuredOptimalControlProblem::getCurrentObjectiveValue
double getCurrentObjectiveValue() override
Definition: structured_optimal_control_problem.h:194
corbo::StructuredOptimalControlProblem::_solver
NlpSolverInterface::Ptr _solver
Definition: structured_optimal_control_problem.h:211
corbo::StageInequalityConstraint::Ptr
std::shared_ptr< StageInequalityConstraint > Ptr
Definition: stage_functions.h:319
corbo::StructuredOptimalControlProblem::_grid
DiscretizationGridInterface::Ptr _grid
Definition: structured_optimal_control_problem.h:207
corbo::NlpFunctions::stage_cost
StageCost::Ptr stage_cost
Definition: nlp_functions.h:61
corbo::StructuredOptimalControlProblem::getDiscretizationGrid
DiscretizationGridInterface::Ptr getDiscretizationGrid()
Definition: structured_optimal_control_problem.h:171
discretization_grid_interface.h
corbo::OptimalControlProblemInterface::StateVector
Eigen::VectorXd StateVector
Definition: optimal_control_problem_interface.h:93
corbo::CORBO_INF_DBL
constexpr const double CORBO_INF_DBL
Representation for infinity (double version)
Definition: core/include/corbo-core/types.h:75
nlp_solver_interface.h
corbo::NlpSolverInterface::Ptr
std::shared_ptr< NlpSolverInterface > Ptr
Definition: nlp_solver_interface.h:114
corbo::StructuredOptimalControlProblem::_u_prev
ControlVector _u_prev
Definition: structured_optimal_control_problem.h:216
corbo::StructuredOptimalControlProblem::UPtr
std::unique_ptr< StructuredOptimalControlProblem > UPtr
Definition: structured_optimal_control_problem.h:88
corbo::NlpFunctions::final_stage_constraints
FinalStageConstraint::Ptr final_stage_constraints
Definition: nlp_functions.h:65
corbo::StageCost::Ptr
std::shared_ptr< StageCost > Ptr
Definition: stage_functions.h:155
corbo::StructuredOptimalControlProblem::setOptimizedTimeSeriesTf
void setOptimizedTimeSeriesTf(double tf)
Definition: structured_optimal_control_problem.h:187
corbo::StructuredOptimalControlProblem::_objective_value
double _objective_value
Definition: structured_optimal_control_problem.h:214
corbo::StructuredOptimalControlProblem::setSolver
void setSolver(NlpSolverInterface::Ptr solver)
Definition: structured_optimal_control_problem.h:180
corbo::StructuredOptimalControlProblem::_ts_dt_cache
double _ts_dt_cache
Definition: structured_optimal_control_problem.h:221
corbo::OptimalControlProblemInterface::Ptr
std::shared_ptr< OptimalControlProblemInterface > Ptr
Definition: optimal_control_problem_interface.h:91
corbo::StructuredOptimalControlProblem::getTimeSeries
void getTimeSeries(TimeSeries::Ptr x_sequence, TimeSeries::Ptr u_sequence, double t_max=CORBO_INF_DBL) override
Definition: structured_optimal_control_problem.cpp:211
corbo::DiscretizationGridInterface::Ptr
std::shared_ptr< DiscretizationGridInterface > Ptr
Definition: discretization_grid_interface.h:107
corbo::StructuredOptimalControlProblem::setOptimizedTimeSeriesDt
void setOptimizedTimeSeriesDt(double dt)
Definition: structured_optimal_control_problem.h:186
nlp_functions.h
corbo::StructuredOptimalControlProblem::ControlVector
Eigen::VectorXd ControlVector
Definition: structured_optimal_control_problem.h:90
corbo::StructuredOptimalControlProblem::getFirstDt
double getFirstDt() const override
Definition: structured_optimal_control_problem.h:103
corbo::NlpFunctions::stage_equalities
StageEqualityConstraint::Ptr stage_equalities
Definition: nlp_functions.h:63
corbo::StructuredOptimalControlProblem::_dynamics
SystemDynamicsInterface::Ptr _dynamics
Definition: structured_optimal_control_problem.h:210
x
Scalar * x
Definition: level1_cplx_impl.h:89
corbo::StructuredOptimalControlProblem::getFirstControlInput
bool getFirstControlInput(ControlVector &u0) const override
Definition: structured_optimal_control_problem.cpp:178
corbo::StructuredOptimalControlProblem::setStatisticsObject
void setStatisticsObject(OptimalControlProblemStatistics::Ptr statistics)
Definition: structured_optimal_control_problem.h:189
FACTORY_REGISTER_OCP
#define FACTORY_REGISTER_OCP(type)
Definition: optimal_control_problem_interface.h:117
corbo::FinalStageConstraint::Ptr
std::shared_ptr< FinalStageConstraint > Ptr
Definition: stage_functions.h:227
corbo::StructuredOptimalControlProblem::setFinalStageConstraint
void setFinalStageConstraint(FinalStageConstraint::Ptr final_stage_constraint)
Definition: structured_optimal_control_problem.h:153
corbo::StructuredOptimalControlProblem::_u_prev_dt
double _u_prev_dt
Definition: structured_optimal_control_problem.h:217
corbo::StructuredOptimalControlProblem::setDiscretizationGrid
void setDiscretizationGrid(DiscretizationGridInterface::Ptr grid)
Definition: structured_optimal_control_problem.h:165
corbo::NlpFunctions::stage_preprocessor
StagePreprocessor::Ptr stage_preprocessor
Definition: nlp_functions.h:66
corbo::SystemDynamicsInterface::Ptr
std::shared_ptr< SystemDynamicsInterface > Ptr
Definition: system_dynamics_interface.h:91
corbo::StructuredOptimalControlProblem::setStageInequalityConstraint
void setStageInequalityConstraint(StageInequalityConstraint::Ptr stage_ineq)
Definition: structured_optimal_control_problem.h:147
corbo::StructuredOptimalControlProblem::StructuredOptimalControlProblem
StructuredOptimalControlProblem()
Definition: structured_optimal_control_problem.cpp:63
corbo::StructuredOptimalControlProblem::isConstantControlAction
bool isConstantControlAction() const override
Definition: structured_optimal_control_problem.h:104
corbo::StructuredOptimalControlProblem::setBounds
void setBounds(const Eigen::VectorXd &x_lb, const Eigen::VectorXd &x_ub, const Eigen::VectorXd &u_lb, const Eigen::VectorXd &u_ub)
Definition: structured_optimal_control_problem.cpp:187
corbo::StructuredOptimalControlProblem::getN
int getN() const override
Definition: structured_optimal_control_problem.h:102
corbo::StructuredOptimalControlProblem::_optim_prob
BaseHyperGraphOptimizationProblem::Ptr _optim_prob
Definition: structured_optimal_control_problem.h:209
corbo::StructuredOptimalControlProblem::setPreviousControlInputDt
void setPreviousControlInputDt(double dt) override
Definition: structured_optimal_control_problem.h:123
corbo::BaseHyperGraphOptimizationProblem::Ptr
std::shared_ptr< BaseHyperGraphOptimizationProblem > Ptr
Definition: hyper_graph_optimization_problem_base.h:117
corbo::StructuredOptimalControlProblem::_resample_tf
double _resample_tf
Definition: structured_optimal_control_problem.h:226
corbo::StructuredOptimalControlProblem::getControlInputDimension
int getControlInputDimension() const override
Definition: structured_optimal_control_problem.h:100
corbo::StructuredOptimalControlProblem::_ts_x_cache
TimeSeries::Ptr _ts_x_cache
Definition: structured_optimal_control_problem.h:220
corbo::StructuredOptimalControlProblem::setStageCost
void setStageCost(StageCost::Ptr stage_cost)
Definition: structured_optimal_control_problem.h:129
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
optimal_control_problem_interface.h
corbo::StructuredOptimalControlProblem::setStageEqualityConstraint
void setStageEqualityConstraint(StageEqualityConstraint::Ptr stage_eq)
Definition: structured_optimal_control_problem.h:141
corbo::StructuredOptimalControlProblem::compute
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
Definition: structured_optimal_control_problem.cpp:99
corbo::StructuredOptimalControlProblem::_resample_dt_hint
double _resample_dt_hint
Definition: structured_optimal_control_problem.h:225
corbo::StructuredOptimalControlProblem::providesFutureControls
bool providesFutureControls() const override
Definition: structured_optimal_control_problem.cpp:72
corbo::StructuredOptimalControlProblem::setStagePreprocessor
void setStagePreprocessor(StagePreprocessor::Ptr stage_preprocessor)
Definition: structured_optimal_control_problem.h:159
corbo::StructuredOptimalControlProblem::_ts_u_cache
TimeSeries::Ptr _ts_u_cache
Definition: structured_optimal_control_problem.h:219
corbo::StructuredOptimalControlProblem::reset
void reset() override
Definition: structured_optimal_control_problem.cpp:221
corbo::Time
Representation of time stamps.
Definition: time.h:273
corbo::FinalStageCost::Ptr
std::shared_ptr< FinalStageCost > Ptr
Definition: stage_functions.h:175
corbo::StructuredOptimalControlProblem::_edges
OptimizationEdgeSet::Ptr _edges
Definition: structured_optimal_control_problem.h:208
corbo::StructuredOptimalControlProblem::getStateDimension
int getStateDimension() const override
Definition: structured_optimal_control_problem.h:101
corbo::StructuredOptimalControlProblem::setStateBounds
void setStateBounds(const Eigen::VectorXd &x_lb, const Eigen::VectorXd &x_ub)
Definition: structured_optimal_control_problem.cpp:197
corbo::TimeSeries::Ptr
std::shared_ptr< TimeSeries > Ptr
Definition: time_series.h:108
corbo::StructuredOptimalControlProblem::getStatistics
OptimalControlProblemStatistics::Ptr getStatistics() const override
Definition: structured_optimal_control_problem.h:190
hyper_graph_optimization_problem_base.h
corbo::NlpFunctions::final_stage_cost
FinalStageCost::Ptr final_stage_cost
Definition: nlp_functions.h:62
corbo::StructuredOptimalControlProblem::setFinalStageCost
void setFinalStageCost(FinalStageCost::Ptr final_stage_cost)
Definition: structured_optimal_control_problem.h:135
corbo::StructuredOptimalControlProblem::_ocp_modified
bool _ocp_modified
Definition: structured_optimal_control_problem.h:223
corbo::StructuredOptimalControlProblem::setHyperGraphOptimizationProblem
void setHyperGraphOptimizationProblem(BaseHyperGraphOptimizationProblem::Ptr optim_prob)
Definition: structured_optimal_control_problem.h:174
corbo::StructuredOptimalControlProblem::_increase_n_if_infeas
bool _increase_n_if_infeas
Definition: structured_optimal_control_problem.h:228
corbo::StageEqualityConstraint::Ptr
std::shared_ptr< StageEqualityConstraint > Ptr
Definition: stage_functions.h:301
corbo::StructuredOptimalControlProblem::_functions
NlpFunctions _functions
Definition: structured_optimal_control_problem.h:206
corbo::StructuredOptimalControlProblem::_statistics
OptimalControlProblemStatistics::Ptr _statistics
Definition: structured_optimal_control_problem.h:212
corbo::StructuredOptimalControlProblem::Ptr
std::shared_ptr< StructuredOptimalControlProblem > Ptr
Definition: structured_optimal_control_problem.h:87


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:06:28