alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
CasADiControlProblem.hpp
Go to the documentation of this file.
1#pragma once
2
10#include <filesystem>
11
12namespace alpaqa {
14
15namespace casadi_loader {
16template <Config>
18} // namespace casadi_loader
19
20template <Config Conf>
22 public:
29 mutable vec work;
30
31 /// Components of the constraint function with indices below this number are
32 /// handled using a quadratic penalty method rather than using an
33 /// augmented Lagrangian method. Specifically, the Lagrange multipliers for
34 /// these components (which determine the shifts in ALM) are kept at zero.
36 /// Same as @ref penalty_alm_split, but for the terminal constraint.
38
39 CasADiControlProblem(const std::string &filename, length_t N,
40 DynamicLoadFlags dl_flags = {});
42
47
48 /// Load the numerical problem data (bounds and parameters) from a CSV file.
49 /// The file should contain 8 rows, with the following contents:
50 /// 1. @ref U lower bound [nu]
51 /// 2. @ref U upper bound [nu]
52 /// 3. @ref D lower bound [nc]
53 /// 4. @ref D upper bound [nc]
54 /// 5. @ref D_N lower bound [nc_N]
55 /// 6. @ref D_N upper bound [nc_N]
56 /// 7. @ref x_init [nx]
57 /// 8. @ref param [p]
58 ///
59 /// Line endings are encoded using a single line feed (`\n`), and the column
60 /// separator can be specified using the @p sep argument.
62 char sep = ',');
63
64 void get_U(Box &U) const { U = this->U; }
65 void get_D(Box &D) const { D = this->D; }
66 void get_D_N(Box &D_N) const { D_N = this->D_N; }
67 void get_x_init(rvec x_init) const { x_init = this->x_init; }
68
69 void eval_f(index_t timestep, crvec x, crvec u, rvec fxu) const;
70 void eval_jac_f(index_t timestep, crvec x, crvec u, rmat J_fxu) const;
72 rvec grad_fxu_p) const;
73 void eval_h(index_t timestep, crvec x, crvec u, rvec h) const;
74 void eval_h_N(crvec x, rvec h) const;
76 [[nodiscard]] real_t eval_l_N(crvec h) const;
77 void eval_qr(index_t timestep, crvec xu, crvec h, rvec qr) const;
78 void eval_q_N(crvec x, crvec h, rvec q) const;
79 void eval_add_Q(index_t timestep, crvec xu, crvec h, rmat Q) const;
80 void eval_add_Q_N(crvec x, crvec h, rmat Q) const;
82 rmat R, rvec work) const;
84 rmat S, rvec work) const;
87 rvec out, rvec work) const;
90 rvec work) const;
93 void eval_constr(index_t timestep, crvec x, rvec c) const;
95 rvec grad_cx_p) const;
97 rmat out) const;
98 void eval_constr_N(crvec x, rvec c) const;
100 void eval_add_gn_hess_constr_N(crvec x, crvec M, rmat out) const;
101
102 void check() const {
104 "Length of problem.U.lowerbound does not "
105 "match problem size problem.nu");
107 "Length of problem.U.upperbound does not "
108 "match problem size problem.nu");
110 "Length of problem.D.lowerbound does not "
111 "match problem size problem.nc");
113 "Length of problem.D.upperbound does not "
114 "match problem size problem.nc");
116 "Length of problem.D_N.lowerbound does "
117 "not match problem size problem.nc_N");
119 "Length of problem.D_N.upperbound does "
120 "not match problem size problem.nc_N");
122 throw std::invalid_argument("Invalid penalty_alm_split");
124 throw std::invalid_argument("Invalid penalty_alm_split_N");
125 }
126
127 [[nodiscard]] length_t get_N() const { return N; }
128 [[nodiscard]] length_t get_nx() const { return nx; }
129 [[nodiscard]] length_t get_nu() const { return nu; }
130 [[nodiscard]] length_t get_nh() const { return nh; }
131 [[nodiscard]] length_t get_nh_N() const { return nh_N; }
132 [[nodiscard]] length_t get_nc() const { return nc; }
133 [[nodiscard]] length_t get_nc_N() const { return nc_N; }
134
135 /// @see @ref TypeErasedControlProblem::eval_proj_diff_g
136 void eval_proj_diff_g(crvec z, rvec e) const {
137 for (index_t t = 0; t < N; ++t)
138 e.segment(t * nc, nc) =
139 projecting_difference(z.segment(t * nc, nc), D);
140 e.segment(N * nc, nc_N) =
141 projecting_difference(z.segment(N * nc, nc_N), D_N);
142 }
143 /// @see @ref TypeErasedControlProblem::eval_proj_multipliers
145 // If there's no lower bound, the multipliers can only be positive
146 auto max_lb = [M](real_t y, real_t z_lb) {
148 return std::max(y, y_lb);
149 };
150 // If there's no upper bound, the multipliers can only be negative
151 auto min_ub = [M](real_t y, real_t z_ub) {
153 return std::min(y, y_ub);
154 };
155 for (index_t t = 0; t < N; ++t) {
157 auto &&yt = y.segment(t * nc, nc);
158 auto &&y_qpm = yt.topRows(penalty_alm_split);
159 auto &&y_alm = yt.bottomRows(num_alm);
160 auto &&z_alm_lb = D.lowerbound.bottomRows(num_alm);
161 auto &&z_alm_ub = D.upperbound.bottomRows(num_alm);
162 y_qpm.setZero();
163 y_alm =
164 y_alm.binaryExpr(z_alm_lb, max_lb).binaryExpr(z_alm_ub, min_ub);
165 }
166 {
167 auto &&yt = y.segment(N * nc, nc_N);
169 auto &&y_qpm = yt.topRows(penalty_alm_split_N);
170 auto &&y_alm = yt.bottomRows(num_alm);
171 auto &&z_alm_lb = D.lowerbound.bottomRows(num_alm);
172 auto &&z_alm_ub = D.upperbound.bottomRows(num_alm);
173 y_qpm.setZero();
174 y_alm =
175 y_alm.binaryExpr(z_alm_lb, max_lb).binaryExpr(z_alm_ub, min_ub);
176 }
177 }
178
179 private:
182};
183
187} // 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
Flags to be passed to dlopen.
Definition dl-flags.hpp:8
Double-precision double configuration.
Definition config.hpp:176