45 : _grid(grid), _optim_prob(optim_prob), _dynamics(dynamics), _solver(solver)
47 _optim_prob->setGraph(_edges, _grid);
50 bool StructuredOptimalControlProblem::providesFutureControls()
const {
return true; }
52 bool StructuredOptimalControlProblem::providesFutureStates()
const {
return _grid ? _grid->providesStateTrajectory() :
false; }
54 bool StructuredOptimalControlProblem::initialize()
58 PRINT_ERROR(
"StructuredOptimalControlProblem::initialize(): no hyper-graph optimization problem strategy specified.");
61 if (!_solver || !_solver->initialize(_optim_prob.get()))
63 PRINT_ERROR(
"StructuredOptimalControlProblem::initialize(): no solver specified or solver initialization failed.");
67 if (_u_prev.size() == 0)
70 _u_prev.setZero(_dynamics->getInputDimension());
71 _u_prev_dt = _grid->getInitialDt();
83 PRINT_ERROR(
"StructuredOptimalControlProblem::compute(): no discretization grid specified.");
89 PRINT_ERROR(
"StructuredOptimalControlProblem::compute(): no system dynamics model specified.");
94 PRINT_ERROR(
"StructuredOptimalControlProblem::compute(): no hyper-graph optimization strategy specified.");
99 PRINT_ERROR(
"StructuredOptimalControlProblem::compute(): no solver specified.");
102 if (!_functions.stage_cost && !_functions.final_stage_cost)
104 PRINT_WARNING(
"StructuredOptimalControlProblem::compute(): no cost function specified.");
107 bool success =
false;
114 if (_statistics) _statistics->clear();
116 Time t1 = Time::now();
118 GridUpdateResult grid_udpate_result =
119 _grid->update(
x, xref, uref, _functions, *_edges, _dynamics, new_run, t, sref, &_u_prev, _u_prev_dt, xinit, uinit);
121 if (grid_udpate_result.vertices_updated)
123 _optim_prob->precomputeVertexQuantities();
125 if (grid_udpate_result.updated())
127 _optim_prob->precomputeEdgeQuantities();
130 assert(_optim_prob->getGraph().checkGraphConsistency());
132 Time t2 = Time::now();
134 SolverStatus status = _solver->solve(*_optim_prob, grid_udpate_result.updated(), new_run, &_objective_value);
135 if (status == SolverStatus::Converged || status == SolverStatus::EarlyTerminated)
137 else if (_increase_n_if_infeas)
139 PRINT_WARNING(
"infeasible solution found. Increasing n for next ocp iteration.");
140 _grid->setN(_grid->getN() + 1);
143 Time t3 = Time::now();
147 _statistics->preparation_time = t2 - t1;
148 _statistics->solving_time = t3 - t2;
156 bool StructuredOptimalControlProblem::getFirstControlInput(ControlVector& u0)
const
158 if (!_grid)
return false;
159 if (_grid->getFirstControlInput(u0))
165 void StructuredOptimalControlProblem::setBounds(
const Eigen::VectorXd& x_lb,
const Eigen::VectorXd& x_ub,
const Eigen::VectorXd& u_lb,
166 const Eigen::VectorXd& u_ub)
168 _functions.x_lb = x_lb;
169 _functions.x_ub = x_ub;
170 _functions.u_lb = u_lb;
171 _functions.u_ub = u_ub;
175 void StructuredOptimalControlProblem::setStateBounds(
const Eigen::VectorXd& x_lb,
const Eigen::VectorXd& x_ub)
177 _functions.x_lb = x_lb;
178 _functions.x_ub = x_ub;
182 void StructuredOptimalControlProblem::setControlBounds(
const Eigen::VectorXd& u_lb,
const Eigen::VectorXd& u_ub)
184 _functions.u_lb = u_lb;
185 _functions.u_ub = u_ub;
196 _grid->getStateAndControlTimeSeries(x_sequence, u_sequence);
199 void StructuredOptimalControlProblem::reset()
201 if (_grid) _grid->clear();
202 if (_optim_prob) _optim_prob->clear();
203 if (_dynamics) _dynamics->reset();
204 if (_solver) _solver->clear();
205 if (_statistics) _statistics->clear();
209 _ocp_modified =
true;
210 _objective_value = -1;
213 #ifdef MESSAGE_SUPPORT
214 void StructuredOptimalControlProblem::toMessage(corbo::messages::OptimalControlProblem& message)
const
216 messages::StructuredOptimalControlProblem* msg = message.mutable_structured_optimal_control_problem();
218 if (_dynamics) _dynamics->toMessage(*msg->mutable_system_model());
219 if (_optim_prob) _optim_prob->toMessage(*msg->mutable_hyper_graph_strategy());
220 if (_solver) _solver->toMessage(*msg->mutable_nlp_solver());
221 if (_grid) _grid->toMessage(*msg->mutable_discretization_grid());
222 if (_functions.stage_cost) _functions.stage_cost->toMessage(*msg->mutable_stage_cost());
223 if (_functions.final_stage_cost) _functions.final_stage_cost->toMessage(*msg->mutable_final_stage_cost());
224 if (_functions.stage_equalities) _functions.stage_equalities->toMessage(*msg->mutable_stage_equalities());
225 if (_functions.stage_inequalities) _functions.stage_inequalities->toMessage(*msg->mutable_stage_inequalities());
226 if (_functions.final_stage_constraints) _functions.final_stage_constraints->toMessage(*msg->mutable_final_stage_constraints());
227 if (_functions.stage_preprocessor) _functions.stage_preprocessor->toMessage(*msg->mutable_stage_preprocessors());
229 msg->set_increase_n_if_infeas(_increase_n_if_infeas);
232 msg->mutable_x_min()->Resize(_functions.x_lb.size(), 0);
234 msg->mutable_x_max()->Resize(_functions.x_ub.size(), 0);
236 msg->mutable_u_min()->Resize(_functions.u_lb.size(), 0);
238 msg->mutable_u_max()->Resize(_functions.u_ub.size(), 0);
242 void StructuredOptimalControlProblem::fromMessage(
const corbo::messages::OptimalControlProblem& message, std::stringstream* issues)
252 const messages::StructuredOptimalControlProblem& msg = message.structured_optimal_control_problem();
255 if (!msg.has_system_model())
257 if (issues) *issues <<
"StructuredOptimalControlProblem: no system model specified.\n";
262 util::get_oneof_field_type_expand_isolated(msg.system_model(),
"system_dynamics", type,
false, 1);
263 SystemDynamicsInterface::Ptr dynamics = SystemDynamicsFactory::instance().create(type);
267 dynamics->fromMessage(msg.system_model(), issues);
268 setSystemDynamics(dynamics);
272 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown system model specified.\n";
275 int dim_x = dynamics->getStateDimension();
276 int dim_u = dynamics->getInputDimension();
279 if (!msg.has_discretization_grid())
281 if (issues) *issues <<
"StructuredOptimalControlProblem: no discretization grid specified.\n";
285 util::get_oneof_field_type(msg.discretization_grid(),
"discretization_grid", type,
false);
286 DiscretizationGridInterface::Ptr grid = DiscretizationGridFactory::instance().create(type);
290 grid->fromMessage(msg.discretization_grid(), issues);
291 setDiscretizationGrid(grid);
295 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown discretization grid specified.\n";
300 if (msg.has_stage_cost() && !msg.stage_cost().has_no_function())
303 util::get_oneof_field_type_expand_isolated(msg.stage_cost(),
"stage_cost", type,
false, 1);
304 StageCost::Ptr stage_cost = StageCostFactory::instance().create(type);
308 if (!stage_cost->fromMessage(msg.stage_cost(), issues))
return;
309 if (!stage_cost->checkParameters(_dynamics->getStateDimension(), _dynamics->getInputDimension(), issues))
return;
310 setStageCost(stage_cost);
314 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown stage_cost specified.\n";
322 if (msg.has_final_stage_cost() && !msg.final_stage_cost().has_no_function())
325 util::get_oneof_field_type_expand_isolated(msg.final_stage_cost(),
"final_stage_cost", type,
false, 1);
326 FinalStageCost::Ptr final_stage_cost = FinalStageCostFactory::instance().create(type);
328 if (final_stage_cost)
330 if (!final_stage_cost->fromMessage(msg.final_stage_cost(), issues))
return;
331 if (!final_stage_cost->checkParameters(_dynamics->getStateDimension(), _dynamics->getInputDimension(), issues))
return;
332 setFinalStageCost(final_stage_cost);
336 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown final_stage_cost specified.\n";
341 setFinalStageCost({});
344 if (msg.has_stage_equalities() && !msg.stage_equalities().has_no_function())
347 util::get_oneof_field_type_expand_isolated(msg.stage_equalities(),
"stage_equalities", type,
false, 1);
348 StageEqualityConstraint::Ptr stage_equalities = StageEqualitiesFactory::instance().create(type);
350 if (stage_equalities)
352 if (!stage_equalities->fromMessage(msg.stage_equalities(), issues))
return;
353 if (!stage_equalities->checkParameters(_dynamics->getStateDimension(), _dynamics->getInputDimension(), issues))
return;
355 setStageEqualityConstraint(stage_equalities);
359 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown stage_equalities specified.\n";
364 setStageEqualityConstraint({});
367 if (msg.has_stage_inequalities() && !msg.stage_inequalities().has_no_function())
370 util::get_oneof_field_type_expand_isolated(msg.stage_inequalities(),
"stage_inequalities", type,
false, 1);
371 StageInequalityConstraint::Ptr stage_inequalities = StageInequalitiesFactory::instance().create(type);
373 if (stage_inequalities)
375 if (!stage_inequalities->fromMessage(msg.stage_inequalities(), issues))
return;
376 if (!stage_inequalities->checkParameters(_dynamics->getStateDimension(), _dynamics->getInputDimension(), issues))
return;
378 setStageInequalityConstraint(stage_inequalities);
382 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown stage_inequalities specified.\n";
387 setStageInequalityConstraint({});
390 if (msg.has_final_stage_constraints() && !msg.final_stage_constraints().has_no_function())
393 util::get_oneof_field_type_expand_isolated(msg.final_stage_constraints(),
"final_stage_constraints", type,
false, 1);
394 FinalStageConstraint::Ptr final_stage_constraint = FinalStageConstraintFactory::instance().create(type);
396 if (final_stage_constraint)
398 if (!final_stage_constraint->fromMessage(msg.final_stage_constraints(), issues))
return;
399 if (!final_stage_constraint->checkParameters(_dynamics->getStateDimension(), _dynamics->getInputDimension(), _functions.final_stage_cost,
402 setFinalStageConstraint(final_stage_constraint);
406 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown final_stage_constraints specified.\n";
411 setFinalStageConstraint({});
414 if (msg.has_stage_preprocessors() && !msg.stage_preprocessors().has_no_function())
417 util::get_oneof_field_type_expand_isolated(msg.stage_preprocessors(),
"stage_preprocessors", type,
false, 1);
419 StagePreprocessor::Ptr stage_preprocessor = StagePreprocessorFactory::instance().create(type);
421 if (stage_preprocessor)
423 if (!stage_preprocessor->fromMessage(msg.stage_preprocessors(), issues))
return;
424 setStagePreprocessor(stage_preprocessor);
428 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown stage_processors specified.\n";
433 setStagePreprocessor({});
437 Eigen::VectorXd x_lb, x_ub, u_lb, u_ub;
438 if (msg.x_min_size() > 0 && msg.x_min_size() != dim_x && issues)
440 *issues <<
"StructuredOptimalControlProblem: x_min size does not match dimension of the system dynamics object" << dim_x <<
".\n";
447 if (msg.x_max_size() > 0 && msg.x_max_size() != dim_x && issues)
449 *issues <<
"StructuredOptimalControlProblem: x_max size does not match dimension of the system dynamics object " << dim_x <<
".\n";
456 if (msg.u_min_size() > 0 && msg.u_min_size() != dim_u && issues)
458 *issues <<
"StructuredOptimalControlProblem: u_min size does not match dimension of the system dynamics object" << dim_u <<
".\n";
465 if (msg.u_max_size() > 0 && msg.u_max_size() != dim_u && issues)
467 *issues <<
"StructuredOptimalControlProblem: u_max size does not match dimension of the system dynamics object " << dim_u <<
".\n";
473 setBounds(x_lb, x_ub, u_lb, u_ub);
476 if (!msg.has_hyper_graph_strategy())
478 if (issues) *issues <<
"StructuredOptimalControlProblem: no hyper_graph_strategy specified.\n";
482 util::get_oneof_field_type(msg.hyper_graph_strategy(),
"hyper_graph_optimization_problem", type,
false);
483 BaseHyperGraphOptimizationProblem::Ptr optim_prob = HyperGraphOptimizationProblemFactory::instance().create(type);
487 optim_prob->fromMessage(msg.hyper_graph_strategy(), issues);
488 setHyperGraphOptimizationProblem(optim_prob);
492 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown hyper_graph_strategy specified.\n";
497 if (!msg.has_nlp_solver())
499 if (issues) *issues <<
"StructuredOptimalControlProblem: no solver specified.\n";
503 util::get_oneof_field_type(msg.nlp_solver(),
"solver", type,
false);
504 NlpSolverInterface::Ptr solver = NlpSolverFactory::instance().create(type);
508 solver->fromMessage(msg.nlp_solver(), issues);
513 if (issues) *issues <<
"StructuredOptimalControlProblem: unknown solver specified.\n";
517 _increase_n_if_infeas = msg.increase_n_if_infeas();