22 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
26 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42 : cacheData_(false), cacheSensitivities_(false)
61 case ct::core::IntegrationType::EULERCT:
63 stepperState_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_vector, SCALAR>>(
65 stepperDX0_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_matrix, SCALAR>>(
67 stepperDU0_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_control_matrix, SCALAR>>(
69 stepperCost_ = std::shared_ptr<ct::core::internal::StepperCTBase<SCALAR, SCALAR>>(
71 stepperCostDX0_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_vector, SCALAR>>(
73 stepperCostDU0_ = std::shared_ptr<ct::core::internal::StepperCTBase<control_vector, SCALAR>>(
78 case ct::core::IntegrationType::RK4CT:
80 stepperState_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_vector, SCALAR>>(
82 stepperDX0_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_matrix, SCALAR>>(
84 stepperDU0_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_control_matrix, SCALAR>>(
86 stepperCost_ = std::shared_ptr<ct::core::internal::StepperCTBase<SCALAR, SCALAR>>(
88 stepperCostDX0_ = std::shared_ptr<ct::core::internal::StepperCTBase<state_vector, SCALAR>>(
90 stepperCostDU0_ = std::shared_ptr<ct::core::internal::StepperCTBase<control_vector, SCALAR>>(
96 throw std::runtime_error(
"Invalid CT integration type");
109 linearSystem_ = linearSystem;
112 dX0dot_ = [
this](
const state_matrix& dX0In, state_matrix& dX0dt,
const SCALAR t) {
113 if (cacheSensitivities_)
114 arraydX0_.push_back(dX0In);
116 dX0dt = arrayA_[dX0Index_] * dX0In;
120 dU0dot_ = [
this](
const state_control_matrix& dU0In, state_control_matrix& dU0dt,
const SCALAR t) {
121 if (cacheSensitivities_)
122 arraydU0_.push_back(dU0In);
124 dU0dt = arrayA_[dU0Index_] * dU0In +
126 controlledSystem_->getController()->getDerivativeU0(
127 statesCached_[dU0Index_], timesCached_[dU0Index_]);
131 dUfdot_ = [
this](
const state_control_matrix& dUfIn, state_control_matrix& dUfdt,
const SCALAR t) {
132 if (cacheSensitivities_)
133 arraydUf_.push_back(dUfIn);
135 dUfdt = arrayA_[dU0Index_] * dUfIn +
137 controlledSystem_->getController()->getDerivativeUf(
138 statesCached_[dU0Index_], timesCached_[dU0Index_]);
151 controlledSystem_ = controlledSystem;
152 xDot_ = [
this](
const state_vector&
x, state_vector& dxdt,
const SCALAR t) {
153 control_vector controlAction;
154 controlledSystem_->getController()->computeControl(x,
t, controlAction);
158 statesCached_.push_back(x);
159 controlsCached_.push_back(controlAction);
160 timesCached_.push_back(
t);
163 controlledSystem_->computeControlledDynamics(x,
t, controlAction, dxdt);
176 costFunction_ = costFun;
177 cacheSensitivities_ =
true;
179 costFunction_->setCurrentStateAndControl(
180 statesCached_[costIndex_], controlsCached_[costIndex_], timesCached_[costIndex_]);
181 cost = costFunction_->evaluateIntermediate();
185 costdX0dot_ = [
this](
const state_vector& costdX0In, state_vector& costdX0dt,
const SCALAR t) {
186 costFunction_->setCurrentStateAndControl(
187 statesCached_[costIndex_], controlsCached_[costIndex_], timesCached_[costIndex_]);
188 costdX0dt = arraydX0_[costIndex_].transpose() * costFunction_->stateDerivativeIntermediate();
192 costdU0dot_ = [
this](
const control_vector& costdU0In, control_vector& costdU0dt,
const SCALAR t) {
193 costFunction_->setCurrentStateAndControl(
194 statesCached_[costIndex_], controlsCached_[costIndex_], timesCached_[costIndex_]);
195 costdU0dt = arraydU0_[costIndex_].transpose() * costFunction_->stateDerivativeIntermediate() +
196 controlledSystem_->getController()->getDerivativeU0(
197 statesCached_[costIndex_], timesCached_[costIndex_]) *
198 costFunction_->controlDerivativeIntermediate();
202 costdUfdot_ = [
this](
const control_vector& costdUfIn, control_vector& costdUfdt,
const SCALAR t) {
203 costFunction_->setCurrentStateAndControl(
204 statesCached_[costIndex_], controlsCached_[costIndex_], timesCached_[costIndex_]);
205 costdUfdt = arraydUf_[costIndex_].transpose() * costFunction_->stateDerivativeIntermediate() +
206 controlledSystem_->getController()->getDerivativeUf(
207 statesCached_[costIndex_], timesCached_[costIndex_]) *
208 costFunction_->controlDerivativeIntermediate();
227 const size_t numSteps,
235 stateTrajectory.clear();
236 timeTrajectory.clear();
238 stateTrajectory.push_back(state);
239 timeTrajectory.push_back(time);
241 for (
size_t i = 0;
i < numSteps; ++
i)
243 stepperState_->do_step(xDot_, state, time, dt);
245 stateTrajectory.push_back(state);
246 timeTrajectory.push_back(time);
265 for (
size_t i = 0;
i < numSteps; ++
i)
267 stepperState_->do_step(xDot_, state, time, dt);
287 for (
size_t i = 0;
i < numSteps; ++
i)
289 stepperDX0_->do_step(dX0dot_, dX0, time, dt);
305 const size_t numSteps,
311 for (
size_t i = 0;
i < numSteps; ++
i)
313 stepperDU0_->do_step(dU0dot_, dU0, time, dt);
329 const size_t numSteps,
335 for (
size_t i = 0;
i < numSteps; ++
i)
337 stepperDU0_->do_step(dUfdot_, dUf, time, dt);
355 if (statesCached_.size() == 0 || controlsCached_.size() == 0 || timesCached_.size() == 0)
356 throw std::runtime_error(
"States cached are empty");
358 for (
size_t i = 0;
i < numSteps; ++
i)
360 stepperCost_->do_step(costDot_, cost, time, dt);
380 for (
size_t i = 0;
i < numSteps; ++
i)
382 stepperCostDX0_->do_step(costdX0dot_, dX0, time, dt);
398 const size_t numSteps,
404 for (
size_t i = 0;
i < numSteps; ++
i)
406 stepperCostDU0_->do_step(costdU0dot_, dU0, time, dt);
422 const size_t numSteps,
428 for (
size_t i = 0;
i < numSteps; ++
i)
430 stepperCostDU0_->do_step(costdUfdot_, dUf, time, dt);
441 for (
size_t i = 0;
i < statesCached_.size(); ++
i)
443 arrayA_.push_back(linearSystem_->getDerivativeState(statesCached_[
i], controlsCached_[i], timesCached_[i]));
445 linearSystem_->getDerivativeControl(statesCached_[i], controlsCached_[i], timesCached_[i]));
454 statesCached_.clear();
455 controlsCached_.clear();
456 timesCached_.clear();
481 std::shared_ptr<ct::core::ControlledSystem<STATE_DIM, CONTROL_DIM, SCALAR>> controlledSystem_;
482 std::shared_ptr<ct::core::LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>> linearSystem_;
483 std::shared_ptr<optcon::CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>> costFunction_;
486 std::function<void(const SCALAR, SCALAR&, const SCALAR)> costDot_;
487 std::function<void(const state_vector&, state_vector&, const SCALAR)> costdX0dot_;
488 std::function<void(const control_vector&, control_vector&, const SCALAR)> costdU0dot_;
489 std::function<void(const control_vector&, control_vector&, const SCALAR)> costdUfdot_;
492 std::function<void(const state_matrix&, state_matrix&, const SCALAR)> dX0dot_;
493 std::function<void(const state_control_matrix&, state_control_matrix&, const SCALAR)> dU0dot_;
494 std::function<void(const state_control_matrix&, state_control_matrix&, const SCALAR)> dUfdot_;
498 bool cacheSensitivities_;
510 std::shared_ptr<ct::core::internal::StepperCTBase<state_vector, SCALAR>> stepperState_;
511 std::shared_ptr<ct::core::internal::StepperCTBase<state_matrix, SCALAR>> stepperDX0_;
512 std::shared_ptr<ct::core::internal::StepperCTBase<state_control_matrix, SCALAR>> stepperDU0_;
514 std::shared_ptr<ct::core::internal::StepperCTBase<SCALAR, SCALAR>> stepperCost_;
515 std::shared_ptr<ct::core::internal::StepperCTBase<state_vector, SCALAR>> stepperCostDX0_;
516 std::shared_ptr<ct::core::internal::StepperCTBase<control_vector, SCALAR>> stepperCostDU0_;
522 std::function<void(const state_vector&, state_vector&, const SCALAR)> xDot_;
void integrateCostSensitivityDUf(control_vector &dUf, const SCALAR &startTime, const size_t numSteps, const SCALAR dt)
Integrates the sensitivity of the cost with respect to the final control input uF.
Definition: SensitivityIntegratorCT.h:420
void integrateCost(SCALAR &cost, const SCALAR startTime, const size_t numSteps, const SCALAR dt)
Integrates the costfunction using the states and controls from the costintegration.
Definition: SensitivityIntegratorCT.h:351
void integrateSensitivityDU0(state_control_matrix &dU0, const SCALAR startTime, const size_t numSteps, const SCALAR dt)
Integrates the sensitivity ODE of the integrator with respec to the initial control input u0...
Definition: SensitivityIntegratorCT.h:303
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef ct::core::StateVector< STATE_DIM, SCALAR > state_vector
Definition: SensitivityIntegratorCT.h:27
void integrate(state_vector &state, const SCALAR startTime, const size_t numSteps, const SCALAR dt)
Integrates the system starting from state and startTime for numSteps integration steps. Returns only the final state and time.
Definition: SensitivityIntegratorCT.h:260
void integrateCostSensitivityDX0(state_vector &dX0, const SCALAR startTime, const size_t numSteps, const SCALAR dt)
Integrates the sensitivity of the cost with respect to the initial state x0.
Definition: SensitivityIntegratorCT.h:375
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
void clearSensitivities()
Clears the cached sensitivities.
Definition: SensitivityIntegratorCT.h:463
const double dt
Definition: LQOCSolverTiming.cpp:18
Describes a cost function with a quadratic approximation, i.e. one that can compute first and second ...
Definition: CostFunctionQuadratic.hpp:29
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< SCALAR, STATE_DIM, STATE_DIM > state_matrix
Definition: SensitivityIntegratorCT.h:29
for i
Definition: mpc_unittest_plotting.m:14
~SensitivityIntegratorCT()=default
Destroys the object.
void clearStates()
Clears the cached states, controls and times.
Definition: SensitivityIntegratorCT.h:452
void integrate(state_vector &state, const SCALAR startTime, const size_t numSteps, const SCALAR dt, ct::core::StateVectorArray< STATE_DIM, SCALAR > &stateTrajectory, ct::core::tpl::TimeArray< SCALAR > &timeTrajectory)
Integrates the system starting from state and startTime for numSteps integration steps. Returns the full state and time trajectories.
Definition: SensitivityIntegratorCT.h:225
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
void clearLinearization()
Clears the linearized matrices.
Definition: SensitivityIntegratorCT.h:474
void setControlledSystem(const std::shared_ptr< ct::core::ControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR >> &controlledSystem)
Changes the controlledsystem to be integrated.
Definition: SensitivityIntegratorCT.h:148
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix
Definition: SensitivityIntegratorCT.h:30
void initializeDerived(const ct::core::IntegrationType stepperType)
Initializes the steppers.
Definition: SensitivityIntegratorCT.h:57
This class can integrate a controlled system and a costfunction. Furthermore, it provides first order...
Definition: SensitivityIntegratorCT.h:23
Eigen::Matrix< SCALAR, STATE_DIM, CONTROL_DIM > state_control_matrix
Definition: SensitivityIntegratorCT.h:31
SensitivityIntegratorCT(const std::shared_ptr< ct::core::ControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR >> &system, const ct::core::IntegrationType stepperType=ct::core::IntegrationType::EULERCT)
Constructor.
Definition: SensitivityIntegratorCT.h:40
void setCostFunction(const std::shared_ptr< CostFunctionQuadratic< STATE_DIM, CONTROL_DIM, SCALAR >> costFun)
Prepares the integrator to provide cost integration and first order cost derivatives. This is done by enabling sensitivity caching and setting up the function objects.
Definition: SensitivityIntegratorCT.h:174
void integrateCostSensitivityDU0(control_vector &dU0, const SCALAR startTime, const size_t numSteps, const SCALAR dt)
Integrates the sensitivity of the cost with respect to the initial control input u0.
Definition: SensitivityIntegratorCT.h:396
void integrateSensitivityDX0(state_matrix &dX0, const SCALAR startTime, const size_t numSteps, const SCALAR dt)
Integrates the sensitivity ODE of the integrator with respec to the initial state x0...
Definition: SensitivityIntegratorCT.h:282
ct::core::ControlVector< CONTROL_DIM, SCALAR > control_vector
Definition: SensitivityIntegratorCT.h:28
void linearize()
Linearizes the system around the rollout from the state interation.
Definition: SensitivityIntegratorCT.h:439
void setLinearSystem(const std::shared_ptr< ct::core::LinearSystem< STATE_DIM, CONTROL_DIM, SCALAR >> &linearSystem)
Prepares the integrator to provide first order sensitivity generation by setting a linearsystem...
Definition: SensitivityIntegratorCT.h:107
void integrateSensitivityDUf(state_control_matrix &dUf, const SCALAR startTime, const size_t numSteps, const SCALAR dt)
Integrates the sensitivity ODE of the integrator with respec to the final control input uf...
Definition: SensitivityIntegratorCT.h:327