11 template <
typename OPTCON_SOLVER>
13 const typename OPTCON_SOLVER::Settings_t& solverSettings,
19 solver_(problem, solverSettings),
20 mpc_settings_(mpcsettings),
21 dynamics_(problem.getNonlinearSystem()->clone()),
22 forwardIntegrator_(dynamics_, mpcsettings.stateForwardIntegratorType_),
27 checkSettings(mpcsettings);
33 if (customPolicyHandler)
35 std::cout <<
"Initializing MPC with a custom policy handler (warmstarter) provided by the user." 37 policyHandler_ = customPolicyHandler;
44 policyHandler_ = std::shared_ptr<PolicyHandler<Policy_t, STATE_DIM, CONTROL_DIM, Scalar_t>>(
49 throw std::runtime_error(
50 "ERROR in MPC Constructor -- no default warm start strategy available for the selected " 58 if (customTimeHorizon)
60 std::cout <<
"Initializing MPC with a custom time-horizon strategy provided by the user." << std::endl;
61 timeHorizonStrategy_ = customTimeHorizon;
65 const Scalar_t initTimeHorizon = solver_.getTimeHorizon();
67 timeHorizonStrategy_ = std::shared_ptr<tpl::MpcTimeHorizon<Scalar_t>>(
77 template <
typename OPTCON_SOLVER>
84 template <
typename OPTCON_SOLVER>
87 timeHorizonStrategy_ = timeHorizonStrategy;
91 template <
typename OPTCON_SOLVER>
94 solver_.setInitialGuess(initGuess);
95 policyHandler_->setPolicy(initGuess);
96 currentPolicy_ = initGuess;
100 template <
typename OPTCON_SOLVER>
107 template <
typename OPTCON_SOLVER>
114 template <
typename OPTCON_SOLVER>
122 if (forwardIntegrationController)
125 integrateForward(t_forward_start, t_forward_stop, x_start, forwardIntegrationController);
130 std::shared_ptr<Policy_t> prevController(
new Policy_t(currentPolicy_));
131 integrateForward(t_forward_start, t_forward_stop, x_start, prevController);
137 template <
typename OPTCON_SOLVER>
140 #ifdef DEBUG_PRINT_MPC 141 std::cout <<
"DEBUG_PRINT_MPC: started to prepare MPC iteration() " << std::endl;
142 #endif //DEBUG_PRINT_MPC 146 const Scalar_t currTimeHorizon = solver_.getTimeHorizon();
153 timeKeeper_.
computeNewTimings(extTime, currTimeHorizon, newTimeHorizon, t_forward_start_, t_forward_stop_);
156 solver_.changeTimeHorizon(newTimeHorizon);
160 policyHandler_->designWarmStartingPolicy(t_forward_stop_, newTimeHorizon, currentPolicy_);
163 if (t_forward_stop_ < t_forward_start_)
164 throw std::runtime_error(
"ERROR: t_forward_stop < t_forward_start is impossible.");
170 solver_.setInitialGuess(currentPolicy_);
172 solver_.prepareMPCIteration();
176 template <
typename OPTCON_SOLVER>
183 #ifdef DEBUG_PRINT_MPC 184 std::cout <<
"DEBUG_PRINT_MPC: started mpc finish Iteration() with state-timestamp " << x_ts << std::endl;
185 #endif //DEBUG_PRINT_MPC 198 solver_.changeInitialState(x_start);
200 bool solveSuccessful = solver_.finishMPCIteration();
204 newPolicy_ts = newPolicy_ts + (t_forward_stop_ - t_forward_start_);
207 currentPolicy_ = solver_.getSolution();
215 if (dtp > t_forward_stop_ && !firstRun_)
218 Scalar_t dt_post_truncation = dtp - t_forward_stop_;
220 #ifdef DEBUG_PRINT_MPC 221 std::cout <<
"DEBUG_PRINT_MPC: additional post-truncation about " << dt_post_truncation <<
" [sec]." 223 #endif //DEBUG_PRINT_MPC 228 policyHandler_->truncateSolutionFront(dt_post_truncation, currentPolicy_, dt_truncated_eff);
231 newPolicy_ts += dt_truncated_eff;
233 else if (t_forward_stop_ >= dtp && !firstRun_)
235 #ifdef DEBUG_PRINT_MPC 236 std::cout <<
"DEBUG_PRINT_MPC: controller opt faster than pre-integration horizon. Consider tuning " 239 #endif //DEBUG_PRINT_MPC 247 #ifdef DEBUG_PRINT_MPC 248 std::cout <<
"DEBUG_PRINT_MPC: start timestamp outgoing policy: " << newPolicy_ts << std::endl;
249 std::cout <<
"DEBUG_PRINT_MPC: ended finishIteration() " << std::endl << std::endl;
250 #endif //DEBUG_PRINT_MPC 253 newPolicy = currentPolicy_;
265 return solveSuccessful;
269 template <
typename OPTCON_SOLVER>
277 timeHorizonStrategy_->updateInitialTimeHorizon(newTimeHorizon);
279 solver_.changeTimeHorizon(newTimeHorizon);
285 template <
typename OPTCON_SOLVER>
288 checkSettings(settings);
293 mpc_settings_ = settings;
295 timeHorizonStrategy_->updateSettings(settings);
299 template <
typename OPTCON_SOLVER>
302 std::cout << std::endl;
303 std::cout <<
"================ MPC Summary ================" << std::endl;
304 std::cout <<
"Number of MPC calls:\t\t\t" << runCallCounter_ << std::endl;
308 std::cout <<
"Max measured solving time [sec]:\t" << timeKeeper_.
getMaxMeasuredDelay() << std::endl;
309 std::cout <<
"Min measured solving time [sec]:\t" << timeKeeper_.
getMinMeasuredDelay() << std::endl;
310 std::cout <<
"Total sum of meas. delay [sec]: \t" << timeKeeper_.
getSummedDelay() << std::endl;
311 std::cout <<
"Average measured delay [sec]: \t" << timeKeeper_.
getSummedDelay() / runCallCounter_
316 std::cout <<
"Used fixed delay[sec]: \t" << 0.000001 * mpc_settings_.
fixedDelayUs_ << std::endl;
319 std::cout <<
"================ End Summary ================" << std::endl;
320 std::cout << std::endl;
324 template <
typename OPTCON_SOLVER>
330 dynamics_->setController(controller);
339 template <
typename OPTCON_SOLVER>
344 throw std::runtime_error(
345 "MPC: measuring delay internally contradicts using external Timing. Switch off one of those options.");
void computeNewTimings(const SCALAR externalTime, const SCALAR current_T, SCALAR &new_T, SCALAR &t_forw_start, SCALAR &t_forw_stop)
compute new mpc timings, based on current time horizon and the given time horizon strategy ...
Definition: MpcTimeKeeper.h:98
void printMpcSummary()
printout simple statistical data
Definition: MPC-impl.h:300
bool measureDelay_
Definition: MpcSettings.h:71
const bool finalPointReached() const
query this in order to find out if the final time horizon has been reached.
Definition: MpcTimeKeeper.h:192
void updateSettings(const mpc_settings &settings)
update mpc settings
Definition: MpcTimeKeeper.h:198
Definition: PolicyHandler.h:12
void integrate_n_steps(StateVector< STATE_DIM, SCALAR > &state, const SCALAR &startTime, size_t numSteps, SCALAR dt, StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, tpl::TimeArray< SCALAR > &timeTrajectory)
const Scalar_t timeSinceFirstSuccessfulSolve(const Scalar_t &extTime)
retrieve the time that elapsed since the first successful solve() call to an Optimal Control Problem ...
Definition: MPC-impl.h:108
bool finishIteration(const core::StateVector< STATE_DIM, Scalar_t > &x, const Scalar_t x_ts, Policy_t &newPolicy, Scalar_t &newPolicy_ts, const std::shared_ptr< core::Controller< STATE_DIM, CONTROL_DIM, Scalar_t >> forwardIntegrationController=nullptr)
finish MPC iteration
Definition: MPC-impl.h:177
SCALAR timeSincePreviousSuccessfulSolve(const SCALAR &externalTime)
retrieve the time that elapsed since the last successful solve() call to an Optimal Control Problem ...
Definition: MpcTimeKeeper.h:265
bool timeHorizonReached()
Check if final time horizon for this task was reached.
Definition: MPC-impl.h:101
MPC(const OptConProblem_t &problem, const typename OPTCON_SOLVER::Settings_t &solverSettings, const mpc_settings &mpcsettings=mpc_settings(), std::shared_ptr< PolicyHandler< Policy_t, STATE_DIM, CONTROL_DIM, Scalar_t >> customPolicyHandler=nullptr, std::shared_ptr< tpl::MpcTimeHorizon< Scalar_t >> customTimeHorizon=nullptr)
MPC solver constructor.
Definition: MPC-impl.h:12
void prepareIteration(const Scalar_t &ext_ts)
Definition: MPC-impl.h:138
const SCALAR & getMeasuredDelay() const
obtain the delay which was measured during solving the optimal control problem
Definition: MpcTimeKeeper.h:310
double stateForwardIntegration_dt_
Definition: MpcSettings.h:63
the default policy handler for iLQR
Definition: StateFeedbackPolicyHandler.h:13
bool coldStart_
Definition: MpcSettings.h:111
const SCALAR & getMaxMeasuredDelay() const
get the maximum measured delay (maximum over all cycles)
Definition: MpcTimeKeeper.h:312
const SCALAR & getSummedDelay() const
get the sum of all measured delays
Definition: MpcTimeKeeper.h:316
typename OPTCON_SOLVER::Policy_t Policy_t
Definition: MPC.h:67
bool postTruncation_
Definition: MpcSettings.h:94
const SCALAR & getMinMeasuredDelay() const
get the smallest measured delay (minimum over all cycles)
Definition: MpcTimeKeeper.h:314
void doForwardIntegration(const Scalar_t &t_forward_start, const Scalar_t &t_forward_stop, core::StateVector< STATE_DIM, Scalar_t > &x_start, const std::shared_ptr< core::Controller< STATE_DIM, CONTROL_DIM, Scalar_t >> forwardIntegrationController=nullptr)
perform forward integration of the measured system state, to compensate for expected or already occur...
Definition: MPC-impl.h:115
MPC Settings struct.
Definition: MpcSettings.h:45
Main MPC class.
Definition: MPC.h:55
void setTimeHorizonStrategy(std::shared_ptr< tpl::MpcTimeHorizon< Scalar_t >> timeHorizonStrategy)
Additional method to insert a custom time horizon strategy, independent from the constructor.
Definition: MPC-impl.h:85
Definition: MpcTimeHorizon.h:20
typename OPTCON_SOLVER::Scalar_t Scalar_t
Definition: MPC.h:66
const SCALAR timeSinceFirstSuccessfulSolve(const SCALAR &externalTime)
retrieve the time that elapsed since the first successful solve() call to an Optimal Control Problem ...
Definition: MpcTimeKeeper.h:290
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
bool useExternalTiming_
Definition: MpcSettings.h:118
bool stateForwardIntegration_
Definition: MpcSettings.h:53
ct::core::IntegrationType stateForwardIntegratorType_
Definition: MpcSettings.h:58
void resetMpc(const Scalar_t &newTimeHorizon)
reset the mpc problem and provide new problem time horizon (mandatory)
Definition: MPC-impl.h:270
Definition: NLOptConSolver.hpp:29
int fixedDelayUs_
Definition: MpcSettings.h:84
void startDelayMeasurement(const SCALAR &externalTime)
start measuring time elapsed during planning / solving the optimal control problem ...
Definition: MpcTimeKeeper.h:200
void setInitialGuess(const Policy_t &initGuess)
set a new initial guess for the policy
Definition: MPC-impl.h:92
void initialize()
initialize the Mpc Time Keeper (mandatory)
Definition: MpcTimeKeeper.h:64
void stopDelayMeasurement(const SCALAR &externalTime)
stop measuring time elapsed during solving the optimal control problem
Definition: MpcTimeKeeper.h:213
Definition: OptConProblemBase.h:40
void updateSettings(const mpc_settings &settings)
update the mpc settings in all instances (main class, time keeper class, etc)
Definition: MPC-impl.h:286
OPTCON_SOLVER & getSolver()
Allows access to the solver member, required mainly for unit testing.
Definition: MPC-impl.h:78