quadratic_cost.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_FUNCTIONS_QUADRATIC_COST_H_
26 #define SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_FUNCTIONS_QUADRATIC_COST_H_
27 
30 
31 #include <memory>
32 
33 namespace corbo {
34 
36 {
37  public:
38  using Ptr = std::shared_ptr<QuadraticFormCost>;
39 
41  {
42  _Q_sqrt = Eigen::MatrixXd::Constant(1, 1, 1);
43  _R_sqrt = Eigen::MatrixXd::Constant(1, 1, 1);
44  }
45 
47  bool lsq_form = false)
48  : _integral_form(integral_form), _lsq_form(lsq_form)
49  {
50  setWeightQ(Q);
51  setWeightR(R);
52  }
53 
54  StageCost::Ptr getInstance() const override { return std::make_shared<QuadraticFormCost>(); }
55 
56  bool hasNonIntegralTerms(int k) const override { return !_integral_form; }
57  bool hasIntegralTerms(int k) const override { return _integral_form; }
58 
59  int getNonIntegralStateTermDimension(int k) const override { return !_integral_form ? (_lsq_form ? _Q.rows() : 1) : 0; }
60  int getNonIntegralControlTermDimension(int k) const override { return !_integral_form ? (_lsq_form ? _R.rows() : 1) : 0; }
61  int getIntegralStateControlTermDimension(int k) const override { return _integral_form ? 1 : 0; }
62 
63  bool isLsqFormNonIntegralStateTerm(int k) const override { return !_integral_form && _lsq_form; }
64  bool isLsqFormNonIntegralControlTerm(int k) const override { return !_integral_form && _lsq_form; }
65 
70 
71  void setLsqForm(bool lsq_form) { _lsq_form = lsq_form; }
72  void setIntegralForm(bool integral_form) { _integral_form = integral_form; }
73 
75 
77 
79  Eigen::Ref<Eigen::VectorXd> cost) const override;
80 
82  bool single_dt, const Eigen::VectorXd& x0, StagePreprocessor::Ptr stage_preprocessor, const std::vector<double>& dts,
83  const DiscretizationGridInterface* /*grid*/) override
84  {
85  _x_ref = &xref;
86  _u_ref = &uref;
87 
90 
91  return false;
92  }
93 
94  bool checkParameters(int state_dim, int control_dim, std::stringstream* issues) const override;
95 
96  void scaleCurrentWeightQ(double scale);
97  void scaleCurrentWeightR(double scale);
98 
99  const Eigen::MatrixXd& getWeightQ() const { return _Q; }
100  const Eigen::MatrixXd& getWeightR() const { return _R; }
101 
102 #ifdef MESSAGE_SUPPORT
103  virtual bool fromMessage(const messages::QuadraticFormCost& message, std::stringstream* issues);
104  virtual void toMessage(messages::QuadraticFormCost& message) const;
105 
106  bool fromMessage(const messages::StageCost& message, std::stringstream* issues) override
107  {
108  return fromMessage(message.quadratic_form_cost(), issues);
109  }
110  void toMessage(messages::StageCost& message) const override { toMessage(*message.mutable_quadratic_form_cost()); }
111 #endif
112 
113  protected:
114  Eigen::MatrixXd _Q_sqrt;
115  Eigen::MatrixXd _R_sqrt;
116  Eigen::MatrixXd _Q;
117  Eigen::MatrixXd _R;
122  bool _Q_diagonal_mode = false;
124  bool _R_diagonal_mode = false;
126 
127  bool _lsq_form = true;
128  bool _integral_form = false;
129 
132  bool _zero_x_ref = false;
133  bool _zero_u_ref = false;
134 };
136 
137 } // namespace corbo
138 
139 #endif // SRC_OPTIMAL_CONTROL_INCLUDE_CORBO_OPTIMAL_CONTROL_FUNCTIONS_QUADRATIC_COST_H_
Eigen::DiagonalMatrix< double, -1 > _Q_diag_sqrt
void computeNonIntegralControlTerm(int k, const Eigen::Ref< const Eigen::VectorXd > &u_k, Eigen::Ref< Eigen::VectorXd > cost) const override
bool hasNonIntegralTerms(int k) const override
bool update(int n, double t, ReferenceTrajectoryInterface &xref, ReferenceTrajectoryInterface &uref, ReferenceTrajectoryInterface *sref, bool single_dt, const Eigen::VectorXd &x0, StagePreprocessor::Ptr stage_preprocessor, const std::vector< double > &dts, const DiscretizationGridInterface *) override
Generic interface class for discretization grids.
Eigen::DiagonalMatrix< double, -1 > _R_diag_sqrt
bool setWeightR(const Eigen::Ref< const Eigen::MatrixXd > &R)
const ReferenceTrajectoryInterface * _x_ref
const Eigen::MatrixXd & getWeightQ() const
int getNonIntegralControlTermDimension(int k) const override
void computeIntegralStateControlTerm(int k, const Eigen::Ref< const Eigen::VectorXd > &x_k, const Eigen::Ref< const Eigen::VectorXd > &u_k, Eigen::Ref< Eigen::VectorXd > cost) const override
void computeNonIntegralStateTerm(int k, const Eigen::Ref< const Eigen::VectorXd > &x_k, Eigen::Ref< Eigen::VectorXd > cost) const override
Eigen::DiagonalMatrix< double, -1 > _Q_diag
bool hasIntegralTerms(int k) const override
#define FACTORY_REGISTER_STAGE_COST(type)
StageCost::Ptr getInstance() const override
Return a newly created shared instance of the implemented class.
const Eigen::MatrixXd & getWeightR() const
std::shared_ptr< QuadraticFormCost > Ptr
bool checkParameters(int state_dim, int control_dim, std::stringstream *issues) const override
QuadraticFormCost(const Eigen::Ref< const Eigen::MatrixXd > &Q, const Eigen::Ref< const Eigen::MatrixXd > &R, bool integral_form=false, bool lsq_form=false)
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:192
int getNonIntegralStateTermDimension(int k) const override
Interface class for reference trajectories.
bool setWeightQ(const Eigen::Ref< const Eigen::MatrixXd > &Q)
bool isLsqFormNonIntegralControlTerm(int k) const override
Eigen::DiagonalMatrix< double, -1 > _R_diag
std::shared_ptr< StagePreprocessor > Ptr
std::shared_ptr< StageCost > Ptr
const ReferenceTrajectoryInterface * _u_ref
void scaleCurrentWeightQ(double scale)
PlainMatrixType mat * n
Definition: eigenvalues.cpp:41
void setIntegralForm(bool integral_form)
bool isLsqFormNonIntegralStateTerm(int k) const override
void setLsqForm(bool lsq_form)
void scaleCurrentWeightR(double scale)
int getIntegralStateControlTermDimension(int k) const override


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:14