- 3.0.2 optimal control module.
NonlinearSystemTest.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4  **********************************************************************************************************************/
5 
6 #include <chrono>
7 #include <fenv.h>
8 
9 #include <gtest/gtest.h>
10 
11 #include "DiehlSystem.h"
12 #include "nloc_test_dir.h"
13 
14 
23 namespace ct {
24 namespace optcon {
25 namespace example {
26 
27 using namespace ct::core;
28 using namespace ct::optcon;
29 using std::shared_ptr;
30 
31 
32 TEST(NLOCTest, NonlinearSystemAlgorithmComparison)
33 {
35 
36  std::cout << "setting up problem " << std::endl;
37 
38  std::string configFile = std::string(NLOC_TEST_DIR) + "/nonlinear/solver.info";
39  std::string costFunctionFile = std::string(NLOC_TEST_DIR) + "/nonlinear/cost.info";
40 
41  Eigen::Matrix<double, 1, 1> x_0;
42  ct::core::loadMatrix(costFunctionFile, "x_0", x_0);
43 
44  Eigen::Matrix<double, 1, 1> x_f;
45  ct::core::loadMatrix(costFunctionFile, "term1.x_f.weigths.x_des", x_f);
46 
47  NLOptConSettings gnms_settings;
48  gnms_settings.load(configFile, true, "gnms");
49 
50  NLOptConSettings ilqr_settings;
51  ilqr_settings.load(configFile, true, "ilqr");
52 
53  std::shared_ptr<ControlledSystem<state_dim, control_dim>> nonlinearSystem(new Dynamics);
54  std::shared_ptr<LinearSystem<state_dim, control_dim>> analyticLinearSystem(new LinearizedSystem);
55  std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction(
57 
58  // times
59  ct::core::Time tf = 3.0;
60  ct::core::loadScalar(configFile, "timeHorizon", tf);
61 
62  size_t nSteps = gnms_settings.computeK(tf);
63 
64  // provide initial guess
65  ControlVector<control_dim> uff_init_guess;
66  uff_init_guess << -(x_0(0) + 1) * x_0(0);
67  ControlVectorArray<control_dim> u0(nSteps, uff_init_guess);
68  StateVectorArray<state_dim> x0(nSteps + 1, x_0);
69 
72  NLOptConSolver::Policy_t initController(x0, u0, u0_fb, gnms_settings.dt);
73 
74 
75  // construct single-core single subsystem OptCon Problem
77  tf, x0[0], nonlinearSystem, costFunction, analyticLinearSystem);
79  tf, x0[0], nonlinearSystem, costFunction, analyticLinearSystem);
80 
81 
82  std::cout << "initializing solvers" << std::endl;
83  NLOptConSolver gnms(optConProblem1, gnms_settings);
84  NLOptConSolver ilqr(optConProblem2, ilqr_settings);
85 
86 
87  gnms.configure(gnms_settings);
88  gnms.setInitialGuess(initController);
89 
90  ilqr.configure(ilqr_settings);
91  ilqr.setInitialGuess(initController);
92 
93 
94  std::cout << "============ running solvers ==============" << std::endl;
95 
96  gnms.solve();
97 
98  // print trajectories
99  StateTrajectory<state_dim> xRollout_gnms = gnms.getStateTrajectory();
101 
102  ilqr.solve();
103 
104  // print trajectories
105  StateTrajectory<state_dim> xRollout_ilqr = ilqr.getStateTrajectory();
107 
108  // Assert that the solutions are equal
109  for (size_t i = 0; i < xRollout_gnms.size(); i++)
110  {
111  ASSERT_NEAR(xRollout_gnms[i](0), xRollout_ilqr[i](0), 1e-4);
112  }
113 
114  for (size_t i = 0; i < uRollout_ilqr.size(); i++)
115  {
116  ASSERT_NEAR(uRollout_gnms[i](0), uRollout_ilqr[i](0), 1e-4);
117  }
118 }
119 } // namespace example
120 } // namespace optcon
121 } // namespace ct
virtual const core::StateTrajectory< STATE_DIM, SCALAR > getStateTrajectory() const override
Definition: NLOptConSolver-impl.hpp:152
Dynamics class for the GNMS unit test, slightly nonlinear dynamics.
Definition: DiehlSystem.h:20
void loadMatrix(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &ns="")
double dt
Eigenvalue correction factor for Hessian regularization.
Definition: NLOptConSettings.hpp:262
virtual const core::ControlTrajectory< CONTROL_DIM, SCALAR > getControlTrajectory() const override
Definition: NLOptConSolver-impl.hpp:160
void loadScalar(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const std::string &ns="")
NLOCAlgorithm_t::Policy_t Policy_t
Definition: NLOptConSolver.hpp:62
std::string costFunctionFile
Definition: LoadFromFileTest.cpp:13
void configure(const Settings_t &settings) override
Definition: NLOptConSolver-impl.hpp:62
A cost function which contains only terms that have analytical derivatives.
Definition: CostFunctionAnalytical.hpp:31
void setInitialGuess(const Policy_t &initialGuess) override
Definition: NLOptConSolver-impl.hpp:118
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
virtual bool solve() override
Definition: NLOptConSolver-impl.hpp:126
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
for i
Definition: mpc_unittest_plotting.m:14
int computeK(double timeHorizon) const
log to matlab (true/false)
Definition: NLOptConSettings.hpp:290
Linear system class for the GNMS unit test.
Definition: DiehlSystem.h:37
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
Definition: NLOptConSolver.hpp:29
Definition: OptConProblemBase.h:40
Definition: exampleDir.h:9
double Time
void load(const std::string &filename, bool verbose=true, const std::string &ns="alg")
load NLOptCon Settings from file
Definition: NLOptConSettings.hpp:396