12 #include <gtest/gtest.h> 14 #include "../testSystems/LinearOscillator.h" 20 TEST(SystemInterfaceTest, ContinuousSystemInterface)
24 const size_t P_DIM = STATE_DIM / 2;
25 const size_t V_DIM = STATE_DIM / 2;
31 std::shared_ptr<ControlledSystem<STATE_DIM, CONTROL_DIM>> nonlinearSystem(
new LinearOscillator());
34 Eigen::Vector2d x_final;
37 std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction =
38 tpl::createCostFunctionLinearOscillator<SCALAR>(x_final);
41 OptConProblem_t optConProblem(nonlinearSystem, costFunction, analyticLinearSystem);
45 nloc_settings.
dt = 0.01;
48 for (
size_t sensInt = 0; sensInt <= 1; sensInt++)
53 for (
size_t ksim = 1; ksim <= 5; ksim = ksim + 4)
55 nloc_settings.
K_sim = ksim;
62 for (
size_t integratortype = 0; integratortype <= 1; integratortype++)
64 if (integratortype == 0)
65 nloc_settings.
integrator = ct::core::IntegrationType::EULERCT;
69 nloc_settings.
integrator = ct::core::IntegrationType::RK4CT;
79 typedef std::shared_ptr<systemInterface_t> systemInterfacePtr_t;
80 systemInterfacePtr_t systemInterface(
82 optConProblem, nloc_settings));
83 systemInterface->initialize();
86 systemInterface_t::state_vector_t x_start, x_next;
88 systemInterface_t::control_vector_t control;
91 systemInterface_t::StateSubstepsPtr subStepsX(
new systemInterface_t::StateSubsteps());
92 systemInterface_t::ControlSubstepsPtr subStepsU(
new systemInterface_t::ControlSubsteps());
96 systemInterface->propagateControlledDynamics(x_start, 0, control, x_next, 0);
98 systemInterface->getSubstates(subStepsX->at(0), 0);
99 systemInterface->getSubcontrols(subStepsU->at(0), 0);
103 systemInterface_t::state_matrix_t A;
104 systemInterface_t::state_control_matrix_t B;
105 systemInterface->setSubstepTrajectoryReference(subStepsX, subStepsU, 0);
106 systemInterface->getAandB(x_start, control, x_next, 0, nloc_settings.
K_sim, A, B, 0);
110 discretizer_t discretizer(
111 nonlinearSystem, nloc_settings.
dt, nloc_settings.
integrator, nloc_settings.
K_sim);
112 discretizer.initialize();
114 systemInterface_t::state_vector_t x_next_discretizer;
115 systemInterface_t::StateSubstepsPtr subStepsX_discretizer(
new systemInterface_t::StateSubsteps());
116 systemInterface_t::ControlSubstepsPtr subStepsU_discretizer(
new systemInterface_t::ControlSubsteps());
117 subStepsX_discretizer->resize(1);
118 subStepsU_discretizer->resize(1);
119 discretizer.propagateControlledDynamics(x_start, 0, control, x_next_discretizer);
120 subStepsX_discretizer->at(0) = discretizer.getSubstates();
121 subStepsU_discretizer->at(0) = discretizer.getSubcontrols();
123 ASSERT_TRUE(x_next.isApprox(x_next_discretizer, 1e-8));
125 ASSERT_TRUE(subStepsX->at(0)->size() == subStepsX_discretizer->at(0)->size());
126 ASSERT_TRUE(subStepsU->at(0)->size() == subStepsU_discretizer->at(0)->size());
127 ASSERT_TRUE(subStepsX->at(0)->size() == subStepsU_discretizer->at(0)->size());
129 for (
size_t i = 0;
i < subStepsX->at(0)->size(); ++
i)
131 ASSERT_TRUE(subStepsX->at(0)->at(
i).isApprox(subStepsX_discretizer->at(0)->at(
i), 1e-8));
132 ASSERT_TRUE(subStepsU->at(0)->at(
i).isApprox(subStepsU_discretizer->at(0)->at(
i), 1e-8));
134 ASSERT_TRUE(subStepsU->at(0)->at(
i).isApprox(control, 1e-8));
139 typedef std::shared_ptr<Sensitivity_t> SensitivityPtr;
140 SensitivityPtr sensitivity;
144 if (nloc_settings.
integrator != ct::core::IntegrationType::EULER &&
145 nloc_settings.
integrator != ct::core::IntegrationType::EULERCT &&
146 nloc_settings.
integrator != ct::core::IntegrationType::RK4 &&
147 nloc_settings.
integrator != ct::core::IntegrationType::RK4CT &&
148 nloc_settings.
integrator != ct::core::IntegrationType::EULER_SYM)
149 throw std::runtime_error(
"sensitivity integrator only available for Euler and RK4 integrators");
152 typedef std::shared_ptr<constant_controller_t> ConstantControllerPtr;
153 ConstantControllerPtr controller(
new constant_controller_t());
163 STATE_DIM / 2, SCALAR>(nloc_settings.
dt, analyticLinearSystem, nloc_settings.
discretization));
166 systemInterface_t::state_matrix_t A_sensitivity;
167 systemInterface_t::state_control_matrix_t B_sensitivity;
168 sensitivity->setSubstepTrajectoryReference(subStepsX.get(), subStepsU.get());
169 sensitivity->getAandB(x_start, control, x_next, 0, nloc_settings.
K_sim, A_sensitivity, B_sensitivity);
171 ASSERT_TRUE(A.isApprox(A_sensitivity, 1e-8));
172 ASSERT_TRUE(B.isApprox(B_sensitivity, 1e-8));
178 TEST(SystemInterfaceTest, DiscreteSystemInterface)
double dt
Eigenvalue correction factor for Hessian regularization.
Definition: NLOptConSettings.hpp:262
const size_t state_dim
Definition: ConstraintComparison.h:20
double getSimulationTimestep() const
compute the simulation timestep
Definition: NLOptConSettings.hpp:300
interface base class for optimal control algorithms
Definition: OptconSystemInterface.h:25
bool useSensitivityIntegrator
Definition: NLOptConSettings.hpp:279
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
CppAD::AD< CppAD::cg::CG< double > > SCALAR
interface class for optimal control algorithms
Definition: OptconContinuousSystemInterface.h:28
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
for i
Definition: mpc_unittest_plotting.m:14
APPROXIMATION discretization
which integrator to use during the NLOptCon forward rollout
Definition: NLOptConSettings.hpp:256
int K_sim
sampling time for the control input (seconds)
Definition: NLOptConSettings.hpp:263
bool timeVaryingDiscretization
Definition: NLOptConSettings.hpp:257
tpl::LinearOscillator< double > LinearOscillator
Definition: LinearOscillator.h:105
tpl::LinearOscillatorLinear< double > LinearOscillatorLinear
Definition: LinearOscillator.h:106
ct::core::IntegrationType integrator
Definition: NLOptConSettings.hpp:255
Definition: OptConProblemBase.h:40
const size_t control_dim
Definition: ConstraintComparison.h:21