12 #include <gtest/gtest.h> 14 #include "mpcTestSettings.h" 15 #include "../testSystems/LinearOscillator.h" 24 using std::shared_ptr;
30 TEST(MPCTestA, ForwardIntegratorTest)
38 Eigen::Matrix<double, state_dim, 1> x_final;
46 shared_ptr<ControlledSystem<state_dim, control_dim>> system(
new LinearOscillator);
47 shared_ptr<LinearSystem<state_dim, control_dim>> analyticLinearSystem(
new LinearOscillatorLinear);
48 shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction =
49 tpl::createCostFunctionLinearOscillator<double>(x_final);
52 optConProblem.setInitialState(x0);
53 optConProblem.setTimeHorizon(timeHorizon);
57 nloc_settings.
dt = 0.001;
58 nloc_settings.
K_sim = 1;
60 nloc_settings.
integrator = ct::core::IntegrationType::EULER;
61 nloc_settings.
discretization = NLOptConSettings::APPROXIMATION::FORWARD_EULER;
62 nloc_settings.
nlocp_algorithm = NLOptConSettings::NLOCP_ALGORITHM::ILQR;
63 nloc_settings.
lqocp_solver = NLOptConSettings::LQOCP_SOLVER::GNRICCATI_SOLVER;
67 size_t K = nloc_settings.
computeK(timeHorizon);
112 ASSERT_EQ(newPolicy.
uff().size(), perfectInitController.
uff().size());
116 for (
size_t i = 0;
i < mpcStateTrajectory.size();
i++)
118 ASSERT_NEAR(mpcStateTrajectory[
i](0), perfectStateTrajectory[
i](0), 1e-6);
119 ASSERT_NEAR(mpcStateTrajectory[
i](1), perfectStateTrajectory[
i](1), 1e-6);
133 std::shared_ptr<ct::core::StateFeedbackController<state_dim, control_dim>> prevController(
135 double time_window = 0.2;
136 for (
size_t i = 0;
i < mpcStateTrajectory.size() -
static_cast<size_t>(nloc_settings.
computeK(time_window));
139 double t_forward_start =
i * nloc_settings.
dt;
140 double t_forward_stop = t_forward_start + time_window;
142 mpcStateTrajectory.setInterpolationType(ct::core::InterpolationType::LIN);
151 }
catch (std::exception& e)
153 std::cout <<
"caught exception: " << e.what() << std::endl;
159 TEST(MPCTestB, NLOC_MPC_DoublePrecision)
164 for (
int solverType = 0; solverType <= 0; solverType++)
168 Eigen::Vector2d x_final;
177 shared_ptr<ControlledSystem<state_dim, control_dim>> system(
new LinearOscillator);
178 shared_ptr<LinearSystem<state_dim, control_dim>> analyticLinearSystem(
new LinearOscillatorLinear);
179 shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction =
180 tpl::createCostFunctionLinearOscillator<double>(x_final);
183 optConProblem.setTimeHorizon(timeHorizon);
184 optConProblem.setInitialState(x0);
190 nloc_settings.
dt = 0.01;
191 nloc_settings.
K_sim = 1;
195 nloc_settings.
discretization = NLOptConSettings::APPROXIMATION::FORWARD_EULER;
196 nloc_settings.
lqocp_solver = NLOptConSettings::LQOCP_SOLVER::GNRICCATI_SOLVER;
197 nloc_settings.
integrator = ct::core::IntegrationType::EULER;
206 nloc_settings.
nlocp_algorithm = NLOptConSettings::NLOCP_ALGORITHM::ILQR;
208 nloc_settings.
nlocp_algorithm = NLOptConSettings::NLOCP_ALGORITHM::GNMS;
211 int K = nloc_settings.
computeK(timeHorizon);
219 x_ref, u0_ff, u0_fb, nloc_settings.
dt);
257 std::vector<ct::core::StateTrajectory<state_dim>>
260 std::vector<double> timeStamps;
261 std::vector<ct::core::StateVector<state_dim>> initStates;
263 int maxNumRuns = 2000;
273 for (
int i = 0;
i < maxNumRuns;
i++)
279 x0 = tempStateTraj.
front();
288 bool success = mpcSolver.
finishIteration(x0, t, newPolicy, ts_newPolicy);
294 stateTrajContainer.push_back(tempStateTraj);
295 timeStamps.push_back(ts_newPolicy);
296 initStates.push_back(x0);
306 ASSERT_LT(std::fabs((refState - mpcTrajFirstState)(0)), 1.0);
307 ASSERT_LT(std::fabs((refState - mpcTrajFirstState)(1)), 2.0);
318 ASSERT_GT(numRuns, 10);
322 #ifdef MATLAB_LOG_MPC 324 std::cout <<
"Saving MPC trajectories to Matlab" << std::endl;
327 std::string dir = std::string(DATA_DIR_MPC) +
"/solution.mat";
330 for (
size_t i = 0;
i < timeStamps.size();
i++)
332 std::string x_varName =
"x_" + std::to_string(
i);
333 std::string t_varName =
"t_" + std::to_string(
i);
334 std::string ts_varName =
"ts_" + std::to_string(
i);
335 std::string x0_varName =
"x0_" + std::to_string(
i);
336 matFile.put(x_varName, stateTrajContainer[
i].getDataArray().toImplementation());
337 matFile.put(t_varName, stateTrajContainer[
i].getTimeArray().toEigenTrajectory());
338 matFile.put(ts_varName, timeStamps[
i]);
345 }
catch (std::exception& e)
347 std::cout <<
"caught exception: " << e.what() << std::endl;
LineSearchSettings lineSearchSettings
number of threads for eigen parallelization (applies both to MP and ST) Note. in order to activate Ei...
Definition: NLOptConSettings.hpp:275
virtual const core::StateTrajectory< STATE_DIM, SCALAR > getStateTrajectory() const override
Definition: NLOptConSolver-impl.hpp:152
void printMpcSummary()
printout simple statistical data
Definition: MPC-impl.h:300
LQOCP_SOLVER lqocp_solver
which nonlinear optimal control algorithm is to be used
Definition: NLOptConSettings.hpp:259
bool measureDelay_
Definition: MpcSettings.h:71
bool printSummary
Definition: NLOptConSettings.hpp:278
size_t nThreadsEigen
number of threads, for MP version
Definition: NLOptConSettings.hpp:274
double dt
Eigenvalue correction factor for Hessian regularization.
Definition: NLOptConSettings.hpp:262
TYPE type
Definition: NLOptConSettings.hpp:58
StateTrajectory< STATE_DIM, SCALAR > & getReferenceStateTrajectory()
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
bool timeHorizonReached()
Check if final time horizon for this task was reached.
Definition: MPC-impl.h:101
void prepareIteration(const Scalar_t &ext_ts)
Definition: MPC-impl.h:138
MPC_MODE mpc_mode
Definition: MpcSettings.h:99
bool debugPrint
Definition: NLOptConSettings.hpp:277
double stateForwardIntegration_dt_
Definition: MpcSettings.h:63
double delayMeasurementMultiplier_
Definition: MpcSettings.h:79
bool coldStart_
Definition: MpcSettings.h:111
void configure(const Settings_t &settings) override
Definition: NLOptConSolver-impl.hpp:62
double min_cost_improvement
duration of a shot as an integer multiple of dt
Definition: NLOptConSettings.hpp:265
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
void setInitialGuess(const Policy_t &initialGuess) override
Definition: NLOptConSolver-impl.hpp:118
NLOCP_ALGORITHM nlocp_algorithm
Definition: NLOptConSettings.hpp:258
bool postTruncation_
Definition: MpcSettings.h:94
Definition: LinearOscillator.h:45
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
virtual bool solve() override
Definition: NLOptConSolver-impl.hpp:126
int additionalDelayUs_
Definition: MpcSettings.h:89
ct::core::Time timeHorizon
Definition: ConstrainedNLOCTest.cpp:15
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
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
const DiscreteArray< control_vector_t > & uff() const
Definition: LinearOscillator.h:27
MPC Settings struct.
Definition: MpcSettings.h:45
Main MPC class.
Definition: MPC.h:55
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
virtual const Policy_t & getSolution() override
Definition: NLOptConSolver-impl.hpp:144
virtual T eval(const SCALAR &evalTime) override
int computeK(double timeHorizon) const
log to matlab (true/false)
Definition: NLOptConSettings.hpp:290
tpl::LinearOscillator< double > LinearOscillator
Definition: LinearOscillator.h:105
int max_iterations
trade off between external (general and path) constraint violation and cost
Definition: NLOptConSettings.hpp:269
a dummy class which is created for compatibility reasons if the MATLAB flag is not set...
Definition: matlab.hpp:17
bool useExternalTiming_
Definition: MpcSettings.h:118
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
FIXED_FINAL_TIME.
Definition: MpcSettings.h:34
tpl::LinearOscillatorLinear< double > LinearOscillatorLinear
Definition: LinearOscillator.h:106
bool stateForwardIntegration_
Definition: MpcSettings.h:53
ct::core::IntegrationType integrator
Definition: NLOptConSettings.hpp:255
ct::core::IntegrationType stateForwardIntegratorType_
Definition: MpcSettings.h:58
int nThreads
save the smallest eigenvalue of the Hessian
Definition: NLOptConSettings.hpp:272
int K_shot
number of sub-integration-steps
Definition: NLOptConSettings.hpp:264
Definition: NLOptConSolver.hpp:29
int fixedDelayUs_
Definition: MpcSettings.h:84
void setInitialGuess(const Policy_t &initGuess)
set a new initial guess for the policy
Definition: MPC-impl.h:92
ControlTrajectory< CONTROL_DIM, SCALAR > & getFeedforwardTrajectory()
Definition: OptConProblemBase.h:40
Definition: exampleDir.h:9