solver_lqr
OcpDescription.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <vector>
12 #include <Eigen/Dense>
14 
15 namespace solverlqr {
16 
18  class StateBase
19  {
20  public:
21  StateBase(){}
22  StateBase(int xdim) { state_ = Eigen::VectorXd(xdim).setZero(); }
23  ~StateBase(){}
24 
25  StateBase& operator= (const StateBase& rhs) { state_ = rhs.stateVector(); return *this; }
26  StateBase operator* (double scalar) const { StateBase new_state; new_state.stateVector() = scalar*state_; return new_state; }
27  StateBase& operator*=(double scalar) { state_ *= scalar; return *this; }
28  StateBase operator+ (const StateBase& rhs) const { StateBase new_state; new_state.stateVector() = state_+rhs.stateVector(); return new_state; }
29  StateBase& operator+=(const StateBase& rhs) { state_ += rhs.stateVector(); return *this; }
30  StateBase operator/ (double scalar) const { StateBase new_state; new_state.stateVector() = state_/scalar; return new_state; }
31  StateBase& operator/=(double scalar) { state_ /= scalar; return *this; }
32 
33  Eigen::VectorXd& stateVector() { return state_; }
34  const Eigen::VectorXd& stateVector() const { return state_; }
35  void resize(int xdim) { state_ = Eigen::VectorXd(xdim).setZero(); }
36 
37  private:
38  Eigen::VectorXd state_;
39  };
40  StateBase operator* (double scalar, const StateBase& rhs);
41  StateBase operator- (const StateBase& lhs, const StateBase& rhs);
42 
43 
45  {
46  public:
47  StateSequence(){}
48  StateSequence(int tdim, int xdim) { this->resize(tdim, xdim); }
49  ~StateSequence(){}
50 
51  void resize(int tdim, int xdim);
52  void setRandom(double scaling = 1.0);
53  const int size() const { return stateseq_.size(); }
54  StateBase& state(int id) { return stateseq_[id]; }
55  const StateBase& state(int id) const { return stateseq_[id]; }
56 
57  private:
58  std::vector<StateBase> stateseq_;
59  };
60 
63  {
64  public:
65  ControlBase(){}
66  ControlBase(int xdim, int udim);
67  ~ControlBase(){}
68 
69  ControlBase& operator= (const ControlBase& rhs);
70  ControlBase operator* (double scalar) const;
71  ControlBase& operator*=(double scalar);
72  ControlBase operator+ (const ControlBase& rhs) const;
73  ControlBase& operator+=(const ControlBase& rhs);
74 
75  void setZero() { feedforward_.setZero(); }
76  Eigen::MatrixXd& feedback() { return feedback_; }
77  Eigen::VectorXd& feedforward() { return feedforward_; }
78  const Eigen::MatrixXd& feedback() const { return feedback_; }
79  const Eigen::VectorXd& feedforward() const { return feedforward_; }
80  void setRandom(double scaling = 1.0) { feedforward_.setRandom(); feedforward_ *= scaling; }
81 
82  private:
83  Eigen::MatrixXd feedback_;
84  Eigen::VectorXd feedforward_;
85  };
86  ControlBase operator*(double lhs_scalar, const ControlBase& rhs);
87 
89  {
90  public:
91  ControlSequence(){}
92  ControlSequence(int tdim, int xdim, int udim) { this->resize(tdim, xdim, udim); }
93  ~ControlSequence(){}
94 
95  void setRandom(double scaling = 1.0);
96  void resize(int tdim, int xdim, int udim);
97  const int size() const { return controlseq_.size(); }
98  ControlBase& control(int id) { return controlseq_[id]; }
99  const ControlBase& control(int id) const { return controlseq_[id]; }
100 
101  ControlSequence& operator= (const ControlSequence& rhs);
102  ControlSequence operator* (double scalar) const;
103  ControlSequence& operator*=(double scalar);
104  ControlSequence operator+ (const ControlSequence& rhs) const;
105  ControlSequence& operator+=(const ControlSequence& rhs);
106 
107  static double controlSequenceGradientNorm(const ControlSequence& u1, const ControlSequence& u2);
108 
109  private:
110  std::vector<ControlBase> controlseq_;
111  };
112  ControlSequence operator*(double lhs_scalar, const ControlSequence& rhs);
113 
115  class OcpBase
116  {
117  public:
118  OcpBase(){}
119  ~OcpBase(){}
120  void initialize(const SolverLqrSetting& setting);
121 
122  const double& dt() const { return this->getLqrSetting().get(SolverLqrDoubleParam_TimeStep); }
123  const int& tdim() const { return this->getLqrSetting().get(SolverLqrIntParam_TimeDimension); }
124  const int& xdim() const { return this->getLqrSetting().get(SolverLqrIntParam_StateDimension); }
125  const int& udim() const { return this->getLqrSetting().get(SolverLqrIntParam_ControlDimension); }
126 
127  StateSequence& stateSeq() { return stateseq_; }
128  ControlSequence& controlSeq() { return controlseq_; }
129  const StateSequence& stateSeq() const { return stateseq_; }
130  const ControlSequence& controlSeq() const { return controlseq_; }
131  const SolverLqrSetting& getLqrSetting() const { return *setting_; }
132 
133  virtual void configure(const YAML::Node& user_parameters) = 0;
134  virtual Eigen::MatrixXd processNoiseFilter(int time_id) const;
135  virtual Eigen::MatrixXd measurementNoiseFilter(int time_id) const;
136  virtual StateBase dynamics(const StateBase& state, const ControlBase& control, int time_id) = 0;
137  void internal_dynamics(const StateBase& state, const ControlBase& control, StateBase& new_state, int time_id);
138  virtual double objective(const StateBase& state, const ControlBase& control, int time_id, bool is_final_timestep) = 0;
139 
140  private:
141  friend class Estimator;
142  friend class ForwardPass;
143  friend class BackwardPass;
144  friend class FiniteDifferences;
145 
146  private:
147  StateSequence stateseq_;
148  ControlSequence controlseq_;
149  const SolverLqrSetting* setting_;
150  };
151 
152 }
Class to define Control of OCP.
Definition: OcpDescription.hpp:62
Definition: BackwardPass.hpp:17
Definition: FiniteDifferences.hpp:17
Definition: BackwardPass.hpp:15
Definition: OcpDescription.hpp:44
Optimal Control Problem Description Class.
Definition: OcpDescription.hpp:115
Definition: OcpDescription.hpp:88
Definition: ForwardPass.hpp:16
Definition: SolverLqrSetting.hpp:18
Class to define State of OCP.
Definition: OcpDescription.hpp:18