- 3.0.2 optimal control module.
NLOCBackendBase.hpp
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 #pragma once
7 
8 #include <atomic>
9 
12 
14 
17 
19 
20 #include "NLOCResults.hpp"
21 
22 #ifdef MATLAB
23 #include <ct/optcon/matlab.hpp>
24 #endif
25 
26 namespace ct {
27 namespace optcon {
28 
29 
42 template <size_t STATE_DIM,
43  size_t CONTROL_DIM,
44  size_t P_DIM,
45  size_t V_DIM,
46  typename SCALAR = double,
47  bool CONTINUOUS = true>
49 {
50 public:
51  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
52 
53  static const size_t state_dim = STATE_DIM;
54  static const size_t control_dim = CONTROL_DIM;
55 
57 
59 
60  typedef typename std::conditional<CONTINUOUS,
63 
66 
68  typedef std::shared_ptr<StateVectorArray> StateVectorArrayPtr;
69 
71  typedef std::shared_ptr<ControlVectorArray> ControlVectorArrayPtr;
72 
73  typedef std::vector<StateVectorArrayPtr, Eigen::aligned_allocator<StateVectorArrayPtr>> StateSubsteps;
74  typedef std::shared_ptr<StateSubsteps> StateSubstepsPtr;
75 
76  typedef std::vector<ControlVectorArrayPtr, Eigen::aligned_allocator<ControlVectorArrayPtr>> ControlSubsteps;
77  typedef std::shared_ptr<ControlSubsteps> ControlSubstepsPtr;
78 
80  typedef std::shared_ptr<systemInterface_t> systemInterfacePtr_t;
81 
88 
89  using state_matrix_t = Eigen::Matrix<SCALAR, STATE_DIM, STATE_DIM>;
90  using control_matrix_t = Eigen::Matrix<SCALAR, CONTROL_DIM, CONTROL_DIM>;
91  using control_state_matrix_t = Eigen::Matrix<SCALAR, CONTROL_DIM, STATE_DIM>;
92  using state_control_matrix_t = Eigen::Matrix<SCALAR, STATE_DIM, CONTROL_DIM>;
93 
97 
98  using scalar_t = SCALAR;
99  using scalar_array_t = std::vector<SCALAR, Eigen::aligned_allocator<SCALAR>>;
100 
101  NLOCBackendBase(const OptConProblem_t& optConProblem, const Settings_t& settings);
102  NLOCBackendBase(const OptConProblem_t& optConProblem,
103  const std::string& settingsFile,
104  bool verbose = true,
105  const std::string& ns = "alg");
106 
107  NLOCBackendBase(const systemInterfacePtr_t& systemInterface, const Settings_t& settings);
108  NLOCBackendBase(const systemInterfacePtr_t& systemInterface,
109  const std::string& settingsFile,
110  bool verbose = true,
111  const std::string& ns = "alg");
112 
113  virtual ~NLOCBackendBase();
114 
115  template <typename T = OptConProblem_t> // do not use this template argument
116  typename std::enable_if<std::is_same<T, ContinuousOptConProblem<STATE_DIM, CONTROL_DIM, SCALAR>>::value,
117  systemInterfacePtr_t>::type
118  createSystemInterface(const OptConProblem_t& optConProblem, const Settings_t& settings);
119 
120  template <typename T = OptConProblem_t> // do not use this template argument
121  typename std::enable_if<std::is_same<T, DiscreteOptConProblem<STATE_DIM, CONTROL_DIM, SCALAR>>::value,
122  systemInterfacePtr_t>::type
123  createSystemInterface(const OptConProblem_t& optConProblem, const Settings_t& settings);
124 
126 
130  virtual void configure(const Settings_t& settings);
131 
133  const Settings_t& getSettings() const;
134 
138  void setInitialGuess(const Policy_t& initialGuess);
139 
140 
147  void changeTimeHorizon(const SCALAR& tf);
148  void changeTimeHorizon(int numStages);
149 
150  SCALAR getTimeHorizon();
151 
152  int getNumSteps();
153 
154  int getNumStepsPerShot() const;
155 
160 
164  void changeCostFunction(const typename OptConProblem_t::CostFunctionPtr_t& cf);
165 
169  void changeNonlinearSystem(const typename OptConProblem_t::DynamicsPtr_t& dyn);
170 
174  void changeLinearSystem(const typename OptConProblem_t::LinearPtr_t& lin);
175 
179  void changeInputBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t& con);
180 
184  void changeStateBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t& con);
185 
189  void changeGeneralConstraints(const typename OptConProblem_t::ConstraintPtr_t& con);
190 
200  std::vector<typename OptConProblem_t::DynamicsPtr_t>& getNonlinearSystemsInstances();
201 
202  const std::vector<typename OptConProblem_t::DynamicsPtr_t>& getNonlinearSystemsInstances() const;
203 
213  std::vector<typename OptConProblem_t::LinearPtr_t>& getLinearSystemsInstances();
214 
215  const std::vector<typename OptConProblem_t::LinearPtr_t>& getLinearSystemsInstances() const;
216 
226  std::vector<typename OptConProblem_t::CostFunctionPtr_t>& getCostFunctionInstances();
227 
228  const std::vector<typename OptConProblem_t::CostFunctionPtr_t>& getCostFunctionInstances() const;
229 
240  std::vector<typename OptConProblem_t::ConstraintPtr_t>& getInputBoxConstraintsInstances();
241  std::vector<typename OptConProblem_t::ConstraintPtr_t>& getStateBoxConstraintsInstances();
242 
243  const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getInputBoxConstraintsInstances() const;
244  const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getStateBoxConstraintsInstances() const;
245 
256  std::vector<typename OptConProblem_t::ConstraintPtr_t>& getGeneralConstraintsInstances();
257 
258  const std::vector<typename OptConProblem_t::ConstraintPtr_t>& getGeneralConstraintsInstances() const;
259 
260 
265  bool testConsistency();
266 
267 
269 
273  void logToMatlab(const size_t& iteration);
274 
276  void logInitToMatlab();
277 
279  SCALAR getCost() const;
280 
282  SCALAR getTotalDefect() const;
283 
284  void reset();
285 
287 
289 
290 
291  const Policy_t& getSolution();
292 
293  const TimeArray& getTimeArray();
294 
295  bool isConfigured();
296 
297  bool isInitialized();
298 
299 
301 
304  void retrieveLastAffineModel(StateMatrixArray& A, StateControlMatrixArray& B, StateVectorArray& b);
305 
309  virtual void prepareSolveLQProblem(size_t startIndex);
310 
311  virtual void finishSolveLQProblem(size_t endIndex);
312 
316  virtual void solveFullLQProblem();
317 
322  void extractSolution();
323 
325  void updateCosts();
326 
328  bool nominalRollout();
329 
331  void checkProblem();
332 
334  size_t& iteration();
335 
337 
341  void printSummary();
342 
344  bool lineSearch();
345 
347  virtual void computeLQApproximation(size_t firstIndex, size_t lastIndex) = 0;
348 
352 
354  void resetDefects();
355 
357  void computeDefectsNorm();
358 
360  virtual void rolloutShots(size_t firstIndex, size_t lastIndex) = 0;
361 
363  bool rolloutShotsSingleThreaded(size_t threadId,
364  size_t firstIndex,
365  size_t lastIndex,
366  ControlVectorArray& u_ff_local,
367  StateVectorArray& x_local,
368  const StateVectorArray& x_ref_lqr,
369  StateVectorArray& xShot,
370  StateVectorArray& d,
371  StateSubsteps& substepsX,
372  ControlSubsteps& substepsU,
373  std::atomic_bool* terminationFlag = nullptr) const;
374 
376  virtual SCALAR performLineSearch() = 0;
377 
379  void doFullStepUpdate();
380 
381  void logSummaryToMatlab(const std::string& fileName);
382 
384 
385 protected:
387  bool rolloutSingleShot(const size_t threadId,
388  const size_t k,
389  ControlVectorArray& u_ff_local,
390  StateVectorArray& x_local,
391  const StateVectorArray& x_ref_lqr,
392  StateVectorArray& xShot,
393  StateSubsteps& substepsX,
394  ControlSubsteps& substepsU,
395  std::atomic_bool* terminationFlag = nullptr) const;
396 
397 
399 
405  void computeSingleDefect(size_t k,
406  const StateVectorArray& x_local,
407  const StateVectorArray& xShot,
408  StateVectorArray& d) const;
409 
410 
412 
420  void executeLQApproximation(size_t threadId, size_t k);
421 
422 
424 
433  void computeLinearizedConstraints(size_t threadId, size_t k);
434 
436 
440  void initializeCostToGo();
441 
443 
448  void computeCostToGo(size_t k);
449 
451 
456  void designController(size_t k);
457 
459 
468  void computeCostsOfTrajectory(size_t threadId,
471  scalar_t& intermediateCost,
472  scalar_t& finalCost) const;
473 
482  void computeBoxConstraintErrorOfTrajectory(size_t threadId,
485  scalar_t& e_tot) const;
486 
495  void computeGeneralConstraintErrorOfTrajectory(size_t threadId,
498  scalar_t& e_tot) const;
499 
501  void executeLineSearch(const size_t threadId,
502  const scalar_t alpha,
507  scalar_t& intermediateCost,
508  scalar_t& finalCost,
509  scalar_t& defectNorm,
510  scalar_t& e_box_norm,
511  scalar_t& e_gen_norm,
512  StateSubsteps& substepsX,
513  ControlSubsteps& substepsU,
514  std::atomic_bool* terminationFlag = nullptr) const;
515 
516 
518  bool acceptStep(
519  const SCALAR alpha,
520  const SCALAR intermediateCost,
521  const SCALAR finalCost,
522  const SCALAR defectNorm,
523  const SCALAR e_box_norm,
524  const SCALAR e_gen_norm,
525  const SCALAR lowestMeritPrevious,
526  SCALAR& new_merit);
527 
529 
534  void updateFFController(size_t k);
535 
537  template <typename ARRAY_TYPE, size_t ORDER = 1>
538  SCALAR computeDiscreteArrayNorm(const ARRAY_TYPE& d) const;
539 
541  template <typename ARRAY_TYPE, size_t ORDER = 1>
542  SCALAR computeDiscreteArrayNorm(const ARRAY_TYPE& a, const ARRAY_TYPE& b) const;
543 
545 
549  template <size_t ORDER = 1>
550  SCALAR computeDefectsNorm(const StateVectorArray& d) const;
551 
554 
555  size_t iteration_;
558 
559  Settings_t settings_;
560 
561  int K_;
563  StateVectorArray x_;
564  StateVectorArray xShot_;
565  StateVectorArray d_;
566  StateVectorArray x_prev_;
567  StateVectorArray x_ref_lqr_;
568 
569  ControlVectorArray u_ff_;
570  ControlVectorArray u_ff_prev_;
572 
573  ControlVectorArray delta_u_ff_;
574  StateVectorArray delta_x_;
575  StateVectorArray delta_x_ref_lqr_;
576 
577  StateSubstepsPtr substepsX_;
578  ControlSubstepsPtr substepsU_;
579 
580  SCALAR d_norm_;
581  SCALAR e_box_norm_;
582  SCALAR e_gen_norm_;
583  SCALAR lx_norm_;
584  SCALAR lu_norm_;
585 
589 
593 
595 
597  std::shared_ptr<LQOCProblem<STATE_DIM, CONTROL_DIM, SCALAR>> lqocProblem_;
598 
600  std::shared_ptr<LQOCSolver<STATE_DIM, CONTROL_DIM, SCALAR>> lqocSolver_;
601 
603  systemInterfacePtr_t systemInterface_;
604 
609  std::vector<typename OptConProblem_t::CostFunctionPtr_t> costFunctions_;
610  std::vector<typename OptConProblem_t::ConstraintPtr_t> inputBoxConstraints_;
611  std::vector<typename OptConProblem_t::ConstraintPtr_t> stateBoxConstraints_;
612  std::vector<typename OptConProblem_t::ConstraintPtr_t> generalConstraints_;
613 
615  size_t lqpCounter_;
616 
619 
621 
623 #ifdef MATLAB
624  matlab::MatFile matFile_;
625 #endif //MATLAB
626 };
627 
628 
629 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
630 template <typename ARRAY_TYPE, size_t ORDER>
632  const ARRAY_TYPE& d) const
633 {
634  SCALAR norm = 0.0;
635 
636  for (size_t k = 0; k < d.size(); k++)
637  {
638  norm += d[k].template lpNorm<ORDER>();
639  }
640  return norm;
641 }
642 
643 
644 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
645 template <typename ARRAY_TYPE, size_t ORDER>
647  const ARRAY_TYPE& a,
648  const ARRAY_TYPE& b) const
649 {
650  assert(a.size() == b.size());
651 
652  SCALAR norm = 0.0;
653 
654  for (size_t k = 0; k < a.size(); k++)
655  {
656  norm += (a[k] - b[k]).template lpNorm<ORDER>();
657  }
658  return norm;
659 }
660 
661 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t P_DIM, size_t V_DIM, typename SCALAR, bool CONTINUOUS>
662 template <size_t ORDER>
664  const StateVectorArray& d) const
665 {
666  return computeDiscreteArrayNorm<StateVectorArray, ORDER>(d);
667 }
668 
669 
670 } // namespace optcon
671 } // namespace ct
StateVectorArray xShot_
state array variables
Definition: NLOCBackendBase.hpp:564
std::vector< typename OptConProblem_t::DynamicsPtr_t > & getNonlinearSystemsInstances()
Direct accessor to the system instances.
Definition: NLOCBackendBase-impl.hpp:1542
void computeCostsOfTrajectory(size_t threadId, const core::StateVectorArray< STATE_DIM, SCALAR > &x_local, const core::ControlVectorArray< CONTROL_DIM, SCALAR > &u_local, scalar_t &intermediateCost, scalar_t &finalCost) const
Compute cost for a given set of state and input trajectory.
Definition: NLOCBackendBase-impl.hpp:572
void setInputBoxConstraintsForLQOCProblem()
sets the box constraints for the entire time horizon including terminal stage
Definition: NLOCBackendBase-impl.hpp:727
void computeSingleDefect(size_t k, const StateVectorArray &x_local, const StateVectorArray &xShot, StateVectorArray &d) const
computes the defect between shot and trajectory
Definition: NLOCBackendBase-impl.hpp:556
StateVectorArray delta_x_ref_lqr_
pointer to state increment
Definition: NLOCBackendBase.hpp:575
void executeLQApproximation(size_t threadId, size_t k)
Computes the linearized Dynamics and quadratic cost approximation at a specific point of the trajecto...
Definition: NLOCBackendBase-impl.hpp:685
virtual void computeLQApproximation(size_t firstIndex, size_t lastIndex)=0
build LQ approximation around trajectory (linearize dynamics and general constraints, quadratize cost, etc)
SCALAR getTimeHorizon()
Definition: NLOCBackendBase-impl.hpp:1647
systemInterfacePtr_t systemInterface_
pointer to instance of the system interface
Definition: NLOCBackendBase.hpp:603
std::vector< typename OptConProblem_t::ConstraintPtr_t > & getStateBoxConstraintsInstances()
Definition: NLOCBackendBase-impl.hpp:1613
Eigen::Matrix< SCALAR, STATE_DIM, CONTROL_DIM > state_control_matrix_t
Definition: NLOCBackendBase.hpp:92
OptconSystemInterface< STATE_DIM, CONTROL_DIM, OptConProblem_t, SCALAR > systemInterface_t
Definition: NLOCBackendBase.hpp:79
void computeDefectsNorm()
update the nominal defects
Definition: NLOCBackendBase-impl.hpp:1479
std::enable_if< std::is_same< T, ContinuousOptConProblem< STATE_DIM, CONTROL_DIM, SCALAR > >::value, systemInterfacePtr_t >::type createSystemInterface(const OptConProblem_t &optConProblem, const Settings_t &settings)
Definition: NLOCBackendBase-impl.hpp:105
void changeInitialState(const core::StateVector< STATE_DIM, SCALAR > &x0)
Change the initial state for the optimal control problem.
Definition: NLOCBackendBase-impl.hpp:238
int K_
Definition: NLOCBackendBase.hpp:561
StateVectorArray x_ref_lqr_
state array from previous iteration
Definition: NLOCBackendBase.hpp:567
scalar_t lowestCost_
Definition: NLOCBackendBase.hpp:588
scalar_t finalCostBest_
Definition: NLOCBackendBase.hpp:587
bool rolloutSingleShot(const size_t threadId, const size_t k, ControlVectorArray &u_ff_local, StateVectorArray &x_local, const StateVectorArray &x_ref_lqr, StateVectorArray &xShot, StateSubsteps &substepsX, ControlSubsteps &substepsU, std::atomic_bool *terminationFlag=nullptr) const
integrate the individual shots
Definition: NLOCBackendBase-impl.hpp:436
int getNumSteps()
Definition: NLOCBackendBase-impl.hpp:1653
std::shared_ptr< LQOCProblem< STATE_DIM, CONTROL_DIM, SCALAR > > lqocProblem_
shared pointer to the linear-quadratic optimal control problem that is constructed by NLOC ...
Definition: NLOCBackendBase.hpp:597
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const size_t state_dim
Definition: NLOCBackendBase.hpp:53
virtual void rolloutShots(size_t firstIndex, size_t lastIndex)=0
integrates the specified shots and computes the corresponding defects
size_t iteration_
Definition: NLOCBackendBase.hpp:555
virtual void solveFullLQProblem()
Definition: NLOCBackendBase-impl.hpp:1328
StateVectorArray d_
rolled-out state (at the end of a time step forward)
Definition: NLOCBackendBase.hpp:565
void reset()
Definition: NLOCBackendBase-impl.hpp:1365
void computeLinearizedConstraints(size_t threadId, size_t k)
Computes the linearized general constraints at a specific point of the trajectory.
Definition: NLOCBackendBase-impl.hpp:790
StateVectorArray x_
the time trajectory
Definition: NLOCBackendBase.hpp:563
bool isInitialized()
Definition: NLOCBackendBase-impl.hpp:1519
void changeGeneralConstraints(const typename OptConProblem_t::ConstraintPtr_t &con)
Change the general constraints.
Definition: NLOCBackendBase-impl.hpp:322
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: NLOCBackendBase.hpp:90
ct::core::tpl::TimeArray< SCALAR > t_
the number of stages in the overall OptConProblem
Definition: NLOCBackendBase.hpp:562
SCALAR lu_norm_
sum of the norms of state update
Definition: NLOCBackendBase.hpp:584
FeedbackArray L_
feed forward controls from previous iteration
Definition: NLOCBackendBase.hpp:571
void setInitialGuess(const Policy_t &initialGuess)
Definition: NLOCBackendBase-impl.hpp:126
void setStateBoxConstraintsForLQOCProblem()
Definition: NLOCBackendBase-impl.hpp:752
void changeLinearSystem(const typename OptConProblem_t::LinearPtr_t &lin)
Change the linear system.
Definition: NLOCBackendBase-impl.hpp:364
int getNumStepsPerShot() const
Definition: NLOCBackendBase-impl.hpp:1659
std::vector< StateVectorArrayPtr, Eigen::aligned_allocator< StateVectorArrayPtr > > StateSubsteps
Definition: NLOCBackendBase.hpp:73
const core::ControlTrajectory< CONTROL_DIM, SCALAR > getControlTrajectory() const
Definition: NLOCBackendBase-impl.hpp:980
SCALAR getCost() const
return the cost of the solution of the current iteration
Definition: NLOCBackendBase-impl.hpp:1003
virtual ~NLOCBackendBase()
Definition: NLOCBackendBase-impl.hpp:97
StateVectorArray x_prev_
defects in between end of rollouts and subsequent state decision vars
Definition: NLOCBackendBase.hpp:566
std::vector< typename OptConProblem_t::ConstraintPtr_t > & getInputBoxConstraintsInstances()
Direct accessor to the box constraint instances.
Definition: NLOCBackendBase-impl.hpp:1596
ControlVectorArray u_ff_prev_
feed forward controls
Definition: NLOCBackendBase.hpp:570
Definition: NLOCResults.hpp:18
std::vector< typename OptConProblem_t::ConstraintPtr_t > generalConstraints_
Definition: NLOCBackendBase.hpp:612
void extractSolution()
extract relevant quantities for the following rollout/solution update step from the LQ solver ...
Definition: NLOCBackendBase-impl.hpp:1381
std::shared_ptr< LQOCSolver< STATE_DIM, CONTROL_DIM, SCALAR > > lqocSolver_
shared pointer to the linear-quadratic optimal control solver, that solves above LQOCP ...
Definition: NLOCBackendBase.hpp:600
LQOCSolver< STATE_DIM, CONTROL_DIM, SCALAR > LQOCSolver_t
Definition: NLOCBackendBase.hpp:65
void changeCostFunction(const typename OptConProblem_t::CostFunctionPtr_t &cf)
Change the cost function.
Definition: NLOCBackendBase-impl.hpp:249
virtual void prepareSolveLQProblem(size_t startIndex)
Definition: NLOCBackendBase-impl.hpp:1280
StateVectorArray delta_x_
pointer to control increment
Definition: NLOCBackendBase.hpp:574
std::vector< typename OptConProblem_t::LinearPtr_t > & getLinearSystemsInstances()
Direct accessor to the linear system instances.
Definition: NLOCBackendBase-impl.hpp:1560
bool rolloutShotsSingleThreaded(size_t threadId, size_t firstIndex, size_t lastIndex, ControlVectorArray &u_ff_local, StateVectorArray &x_local, const StateVectorArray &x_ref_lqr, StateVectorArray &xShot, StateVectorArray &d, StateSubsteps &substepsX, ControlSubsteps &substepsU, std::atomic_bool *terminationFlag=nullptr) const
do a single threaded rollout and defect computation of the shots - useful for line-search ...
Definition: NLOCBackendBase-impl.hpp:524
interface base class for optimal control algorithms
Definition: OptconSystemInterface.h:25
bool firstRollout_
Definition: NLOCBackendBase.hpp:557
Settings for the NLOptCon algorithm.
Definition: NLOptConSettings.hpp:198
std::conditional< CONTINUOUS, ContinuousOptConProblem< STATE_DIM, CONTROL_DIM, SCALAR >, DiscreteOptConProblem< STATE_DIM, CONTROL_DIM, SCALAR > >::type OptConProblem_t
Definition: NLOCBackendBase.hpp:62
CppAD::AD< CppAD::cg::CG< double > > SCALAR
SCALAR getTotalDefect() const
return the sum of the L2-norm of the defects along the solution candidate
Definition: NLOCBackendBase-impl.hpp:1526
std::shared_ptr< StateSubsteps > StateSubstepsPtr
Definition: NLOCBackendBase.hpp:74
NLOCBackendBase(const OptConProblem_t &optConProblem, const Settings_t &settings)
Definition: NLOCBackendBase-impl.hpp:13
std::vector< SCALAR, Eigen::aligned_allocator< SCALAR > > scalar_array_t
Definition: NLOCBackendBase.hpp:99
Eigen::Matrix< SCALAR, STATE_DIM, STATE_DIM > state_matrix_t
Definition: NLOCBackendBase.hpp:89
size_t lqpCounter_
a counter used to identify lqp problems in derived classes, i.e. for thread management in MP ...
Definition: NLOCBackendBase.hpp:615
ct::core::StateFeedbackController< STATE_DIM, CONTROL_DIM, SCALAR > Policy_t
Definition: NLOCBackendBase.hpp:58
Settings_t settings_
Definition: NLOCBackendBase.hpp:559
Defines a Linear-Quadratic Optimal Control Problem, which is optionally constrained.
Definition: LQOCProblem.hpp:57
bool testConsistency()
Definition: NLOCBackendBase-impl.hpp:1533
void logToMatlab(const size_t &iteration)
Export all functions to matlab workspace.
Definition: NLOCBackendBase-impl.hpp:895
virtual void configure(const Settings_t &settings)
configure the solver
Definition: NLOCBackendBase-impl.hpp:385
void executeLineSearch(const size_t threadId, const scalar_t alpha, ct::core::StateVectorArray< STATE_DIM, SCALAR > &x_recorded, ct::core::StateVectorArray< STATE_DIM, SCALAR > &x_shot_recorded, ct::core::StateVectorArray< STATE_DIM, SCALAR > &defects_recorded, ct::core::ControlVectorArray< CONTROL_DIM, SCALAR > &u_recorded, scalar_t &intermediateCost, scalar_t &finalCost, scalar_t &defectNorm, scalar_t &e_box_norm, scalar_t &e_gen_norm, StateSubsteps &substepsX, ControlSubsteps &substepsU, std::atomic_bool *terminationFlag=nullptr) const
Check if controller with particular alpha is better.
Definition: NLOCBackendBase-impl.hpp:1107
ControlVectorArray u_ff_
reference for lqr
Definition: NLOCBackendBase.hpp:569
std::vector< ControlVectorArrayPtr, Eigen::aligned_allocator< ControlVectorArrayPtr > > ControlSubsteps
Definition: NLOCBackendBase.hpp:76
const Settings_t & getSettings() const
get the current SLQsolver settings
Definition: NLOCBackendBase-impl.hpp:1669
std::shared_ptr< systemInterface_t > systemInterfacePtr_t
Definition: NLOCBackendBase.hpp:80
SCALAR computeDiscreteArrayNorm(const ARRAY_TYPE &d) const
compute norm of a discrete array (todo move to core)
Definition: NLOCBackendBase.hpp:631
std::vector< typename OptConProblem_t::CostFunctionPtr_t > costFunctions_
Definition: NLOCBackendBase.hpp:609
ControlVectorArray delta_u_ff_
time-varying lqr feedback
Definition: NLOCBackendBase.hpp:573
void resetDefects()
reset all defects to zero
Definition: NLOCBackendBase-impl.hpp:1472
void updateFFController(size_t k)
Update feedforward controller.
const Policy_t & getSolution()
Definition: NLOCBackendBase-impl.hpp:1358
SCALAR scalar_t
Definition: NLOCBackendBase.hpp:98
virtual SCALAR performLineSearch()=0
performLineSearch: execute the line search, possibly with different threading schemes ...
scalar_t finalCostPrevious_
Definition: NLOCBackendBase.hpp:592
bool acceptStep(const SCALAR alpha, const SCALAR intermediateCost, const SCALAR finalCost, const SCALAR defectNorm, const SCALAR e_box_norm, const SCALAR e_gen_norm, const SCALAR lowestMeritPrevious, SCALAR &new_merit)
in case of line-search compute new merit and check if to accept step. Returns true if accept step ...
Definition: NLOCBackendBase-impl.hpp:1187
scalar_t alphaBest_
Definition: NLOCBackendBase.hpp:594
StateSubstepsPtr substepsX_
state array from previous iteration
Definition: NLOCBackendBase.hpp:577
const SummaryAllIterations< SCALAR > & getSummary() const
Definition: NLOCBackendBase-impl.hpp:1466
NLOCBackendBase::Policy_t policy_
The policy. currently only for returning the result, should eventually replace L_ and u_ff_ (todo) ...
Definition: NLOCBackendBase.hpp:618
std::vector< typename OptConProblem_t::ConstraintPtr_t > stateBoxConstraints_
Definition: NLOCBackendBase.hpp:611
ct::core::ControlVectorArray< CONTROL_DIM, SCALAR > ControlVectorArray
Definition: NLOCBackendBase.hpp:70
void retrieveLastAffineModel(StateMatrixArray &A, StateControlMatrixArray &B, StateVectorArray &b)
Retrieve Last Linearized Model.
Definition: NLOCBackendBase-impl.hpp:1346
virtual void finishSolveLQProblem(size_t endIndex)
Definition: NLOCBackendBase-impl.hpp:1304
void computeCostToGo(size_t k)
Computes cost to go.
SCALAR e_gen_norm_
sum of the norms of all box constraint violations
Definition: NLOCBackendBase.hpp:582
scalar_t intermediateCostBest_
sum of the norms of control update
Definition: NLOCBackendBase.hpp:586
ct::core::StateVectorArray< STATE_DIM, SCALAR > StateVectorArray
Definition: NLOCBackendBase.hpp:67
SCALAR lx_norm_
sum of the norms of all general constraint violations
Definition: NLOCBackendBase.hpp:583
bool initialized_
Definition: NLOCBackendBase.hpp:552
const TimeArray & getTimeArray()
Definition: NLOCBackendBase-impl.hpp:1505
ControlSubstepsPtr substepsU_
state substeps recorded by integrator during rollouts
Definition: NLOCBackendBase.hpp:578
void computeBoxConstraintErrorOfTrajectory(size_t threadId, const ct::core::StateVectorArray< STATE_DIM, SCALAR > &x_local, const ct::core::ControlVectorArray< CONTROL_DIM, SCALAR > &u_local, scalar_t &e_tot) const
Compute box constraint violations for a given set of state and input trajectory.
Definition: NLOCBackendBase-impl.hpp:596
C++ implementation of GNMS.
Definition: NLOCBackendBase.hpp:48
NLOptConSettings Settings_t
Definition: NLOCBackendBase.hpp:56
void changeStateBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t &con)
Change the state box constraints.
Definition: NLOCBackendBase-impl.hpp:299
Eigen::Matrix< SCALAR, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: NLOCBackendBase.hpp:91
a dummy class which is created for compatibility reasons if the MATLAB flag is not set...
Definition: matlab.hpp:17
void doFullStepUpdate()
simple full-step update for state and feedforward control (used for MPC-mode!)
Definition: NLOCBackendBase-impl.hpp:1447
void initializeCostToGo()
Initializes cost to go.
Definition: NLOCBackendBase-impl.hpp:819
void changeNonlinearSystem(const typename OptConProblem_t::DynamicsPtr_t &dyn)
Change the nonlinear system.
Definition: NLOCBackendBase-impl.hpp:270
bool isConfigured()
Definition: NLOCBackendBase-impl.hpp:1512
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
std::vector< typename OptConProblem_t::CostFunctionPtr_t > & getCostFunctionInstances()
Direct accessor to the cost function instances.
Definition: NLOCBackendBase-impl.hpp:1578
std::shared_ptr< ControlSubsteps > ControlSubstepsPtr
Definition: NLOCBackendBase.hpp:77
void printSummary()
Print iteration summary.
Definition: NLOCBackendBase-impl.hpp:850
void designController(size_t k)
Design controller.
const bool verbose
Definition: ConstraintComparison.h:18
bool nominalRollout()
nominal rollout using default thread and member variables for the results. // todo maybe rename (init...
Definition: NLOCBackendBase-impl.hpp:1493
void changeTimeHorizon(const SCALAR &tf)
Change the time horizon the solver operates on.
Definition: NLOCBackendBase-impl.hpp:229
std::vector< typename OptConProblem_t::ConstraintPtr_t > & getGeneralConstraintsInstances()
Direct accessor to the general constraints.
Definition: NLOCBackendBase-impl.hpp:1631
void changeInputBoxConstraints(const typename OptConProblem_t::ConstraintPtr_t &con)
Change the input box constraints.
Definition: NLOCBackendBase-impl.hpp:277
void logSummaryToMatlab(const std::string &fileName)
Definition: NLOCBackendBase-impl.hpp:1457
scalar_t intermediateCostPrevious_
costs of the previous iteration, required to determine convergence
Definition: NLOCBackendBase.hpp:591
bool configured_
Definition: NLOCBackendBase.hpp:553
std::vector< typename OptConProblem_t::ConstraintPtr_t > inputBoxConstraints_
Definition: NLOCBackendBase.hpp:610
void updateCosts()
compute costs of solution candidate
Definition: NLOCBackendBase-impl.hpp:1338
Definition: OptConProblemBase.h:40
std::shared_ptr< StateVectorArray > StateVectorArrayPtr
Definition: NLOCBackendBase.hpp:68
SCALAR d_norm_
control substeps recorded by integrator during rollouts
Definition: NLOCBackendBase.hpp:580
bool lineSearch()
perform line-search and update controller
Definition: NLOCBackendBase-impl.hpp:1010
size_t & iteration()
return the current iteration number
Definition: NLOCBackendBase-impl.hpp:1486
LQOCProblem< STATE_DIM, CONTROL_DIM, SCALAR > LQOCProblem_t
Definition: NLOCBackendBase.hpp:64
void logInitToMatlab()
log the initial guess to Matlab
Definition: NLOCBackendBase-impl.hpp:946
static const size_t control_dim
Definition: NLOCBackendBase.hpp:54
void checkProblem()
check problem for consistency
Definition: NLOCBackendBase-impl.hpp:371
SCALAR e_box_norm_
sum of the norms of all defects (internal constraint)
Definition: NLOCBackendBase.hpp:581
std::shared_ptr< ControlVectorArray > ControlVectorArrayPtr
Definition: NLOCBackendBase.hpp:71
SummaryAllIterations< SCALAR > summaryAllIterations_
Definition: NLOCBackendBase.hpp:620
Definition: LQOCSolver.hpp:22
void computeGeneralConstraintErrorOfTrajectory(size_t threadId, const ct::core::StateVectorArray< STATE_DIM, SCALAR > &x_local, const ct::core::ControlVectorArray< CONTROL_DIM, SCALAR > &u_local, scalar_t &e_tot) const
Compute general constraint violations for a given set of state and input trajectory.
Definition: NLOCBackendBase-impl.hpp:647
const core::StateTrajectory< STATE_DIM, SCALAR > getStateTrajectory() const
Definition: NLOCBackendBase-impl.hpp:993