alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiControlProblem.hpp
Go to the documentation of this file.
1#pragma once
2
9#include <filesystem>
10
11namespace alpaqa {
13
14namespace casadi_loader {
15template <Config>
17} // namespace casadi_loader
18
19template <Config Conf>
21 public:
28 mutable vec work;
29
30 /// Components of the constraint function with indices below this number are
31 /// handled using a quadratic penalty method rather than using an
32 /// augmented Lagrangian method. Specifically, the Lagrange multipliers for
33 /// these components (which determine the shifts in ALM) are kept at zero.
35 /// Same as @ref penalty_alm_split, but for the terminal constraint.
37
38 CasADiControlProblem(const std::string &filename, length_t N);
40
45
46 /// Load the numerical problem data (bounds and parameters) from a CSV file.
47 /// The file should contain 8 rows, with the following contents:
48 /// 1. @ref U lower bound [nu]
49 /// 2. @ref U upper bound [nu]
50 /// 3. @ref D lower bound [nc]
51 /// 4. @ref D upper bound [nc]
52 /// 5. @ref D_N lower bound [nc_N]
53 /// 6. @ref D_N upper bound [nc_N]
54 /// 7. @ref x_init [nx]
55 /// 8. @ref param [p]
56 ///
57 /// Line endings are encoded using a single line feed (`\n`), and the column
58 /// separator can be specified using the @p sep argument.
60 char sep = ',');
61
62 void get_U(Box &U) const { U = this->U; }
63 void get_D(Box &D) const { D = this->D; }
64 void get_D_N(Box &D_N) const { D_N = this->D_N; }
65 void get_x_init(rvec x_init) const { x_init = this->x_init; }
66
67 void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const;
68 void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const;
70 rvec grad_fxu_p) const;
71 void eval_h(index_t timestep, crvec x, crvec u, rvec h) const;
72 void eval_h_N(crvec x, rvec h) const;
74 [[nodiscard]] real_t eval_l_N(crvec h) const;
75 void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const;
76 void eval_q_N(crvec x, crvec h, rvec q) const;
77 void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const;
78 void eval_add_Q_N(crvec x, crvec h, rmat Q) const;
80 rmat R, rvec work) const;
82 rmat S, rvec work) const;
85 rvec out, rvec work) const;
88 rvec work) const;
91 void eval_constr(index_t timestep, crvec x, rvec c) const;
93 rvec grad_cx_p) const;
95 rmat out) const;
96 void eval_constr_N(crvec x, rvec c) const;
98 void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const;
99
100 void check() const {
102 "Length of problem.U.lowerbound does not "
103 "match problem size problem.nu");
105 "Length of problem.U.upperbound does not "
106 "match problem size problem.nu");
108 "Length of problem.D.lowerbound does not "
109 "match problem size problem.nc");
111 "Length of problem.D.upperbound does not "
112 "match problem size problem.nc");
114 "Length of problem.D_N.lowerbound does "
115 "not match problem size problem.nc_N");
117 "Length of problem.D_N.upperbound does "
118 "not match problem size problem.nc_N");
120 throw std::invalid_argument("Invalid penalty_alm_split");
122 throw std::invalid_argument("Invalid penalty_alm_split_N");
123 }
124
125 [[nodiscard]] length_t get_N() const { return N; }
126 [[nodiscard]] length_t get_nx() const { return nx; }
127 [[nodiscard]] length_t get_nu() const { return nu; }
128 [[nodiscard]] length_t get_nh() const { return nh; }
129 [[nodiscard]] length_t get_nh_N() const { return nh_N; }
130 [[nodiscard]] length_t get_nc() const { return nc; }
131 [[nodiscard]] length_t get_nc_N() const { return nc_N; }
132
133 /// @see @ref TypeErasedControlProblem::eval_proj_diff_g
134 void eval_proj_diff_g(crvec z, rvec e) const {
135 for (index_t t = 0; t < N; ++t)
136 e.segment(t * nc, nc) =
137 projecting_difference(z.segment(t * nc, nc), D);
138 e.segment(N * nc, nc_N) =
139 projecting_difference(z.segment(N * nc, nc_N), D_N);
140 }
141 /// @see @ref TypeErasedControlProblem::eval_proj_multipliers
143 // If there's no lower bound, the multipliers can only be positive
144 auto max_lb = [M](real_t y, real_t z_lb) {
146 return std::max(y, y_lb);
147 };
148 // If there's no upper bound, the multipliers can only be negative
149 auto min_ub = [M](real_t y, real_t z_ub) {
151 return std::min(y, y_ub);
152 };
153 for (index_t t = 0; t < N; ++t) {
155 auto &&yt = y.segment(t * nc, nc);
156 auto &&y_qpm = yt.topRows(penalty_alm_split);
157 auto &&y_alm = yt.bottomRows(num_alm);
158 auto &&z_alm_lb = D.lowerbound.bottomRows(num_alm);
159 auto &&z_alm_ub = D.upperbound.bottomRows(num_alm);
160 y_qpm.setZero();
161 y_alm =
162 y_alm.binaryExpr(z_alm_lb, max_lb).binaryExpr(z_alm_ub, min_ub);
163 }
164 {
165 auto &&yt = y.segment(N * nc, nc_N);
167 auto &&y_qpm = yt.topRows(penalty_alm_split_N);
168 auto &&y_alm = yt.bottomRows(num_alm);
169 auto &&z_alm_lb = D.lowerbound.bottomRows(num_alm);
170 auto &&z_alm_ub = D.upperbound.bottomRows(num_alm);
171 y_qpm.setZero();
172 y_alm =
173 y_alm.binaryExpr(z_alm_lb, max_lb).binaryExpr(z_alm_ub, min_ub);
174 }
175 }
176
177 private:
180};
181
185} // namespace alpaqa
#define BEGIN_ALPAQA_CASADI_LOADER_NAMESPACE
#define END_ALPAQA_CASADI_LOADER_NAMESPACE
#define CASADI_OCP_LOADER_EXPORT_EXTERN_TEMPLATE(strcls, name,...)
void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const
CasADiControlProblem(const CasADiControlProblem &)
void eval_add_R_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_J, crindexvec mask_K, crvec v, rvec out, rvec work) const
CasADiControlProblem & operator=(const CasADiControlProblem &)
void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const
void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const
index_t penalty_alm_split_N
Same as penalty_alm_split, but for the terminal constraint.
void eval_add_S_prod_masked(index_t timestep, crvec xu, crvec h, crindexvec mask_K, crvec v, rvec out, rvec work) const
void eval_constr_N(crvec x, rvec c) const
void load_numerical_data(const std::filesystem::path &filepath, char sep=',')
Load the numerical problem data (bounds and parameters) from a CSV file.
void eval_grad_constr_prod_N(crvec x, crvec p, rvec grad_cx_p) const
void eval_add_R_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat R, rvec work) const
real_t eval_l(index_t timestep, crvec h) const
void eval_grad_f_prod(index_t timestep, crvec x, crvec u, crvec p, rvec grad_fxu_p) const
index_t penalty_alm_split
Components of the constraint function with indices below this number are handled using a quadratic pe...
void eval_add_gn_hess_constr(index_t timestep, crvec x, crvec M, rmat out) const
void eval_constr(index_t timestep, crvec x, rvec c) const
void eval_h(index_t timestep, crvec x, crvec u, rvec h) const
util::copyable_unique_ptr< Functions > impl
void eval_add_S_masked(index_t timestep, crvec xu, crvec h, crindexvec mask, rmat S, rvec work) const
void get_x_init(rvec x_init) const
void eval_proj_multipliers(rvec y, real_t M) const
void eval_q_N(crvec x, crvec h, rvec q) const
void eval_add_Q_N(crvec x, crvec h, rmat Q) const
void eval_grad_constr_prod(index_t timestep, crvec x, crvec p, rvec grad_cx_p) const
void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const
void eval_proj_diff_g(crvec z, rvec e) const
void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const
void eval_h_N(crvec x, rvec h) const
CasADiControlProblem(CasADiControlProblem &&) noexcept
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
void check_dim_msg(const V &v, auto sz, std::string msg)
Definition check-dim.hpp:11
typename Conf::rmat rmat
Definition config.hpp:96
typename Conf::real_t real_t
Definition config.hpp:86
typename Conf::index_t index_t
Definition config.hpp:104
typename Conf::length_t length_t
Definition config.hpp:103
constexpr const auto inf
Definition config.hpp:112
typename Conf::rvec rvec
Definition config.hpp:91
typename Conf::crvec crvec
Definition config.hpp:92
typename Conf::vec vec
Definition config.hpp:88
typename Conf::crindexvec crindexvec
Definition config.hpp:107
Double-precision double configuration.
Definition config.hpp:174