2#include <alpaqa/example-util.hpp>
24 Problem() : A(nx, nx), B(nx, nu) {
30 [[nodiscard]] length_t get_N()
const {
return N; }
32 [[nodiscard]] length_t get_nu()
const {
return nu; }
34 [[nodiscard]] length_t get_nx()
const {
return nx; }
36 [[nodiscard]] length_t get_nh()
const {
return nh; }
38 [[nodiscard]] length_t get_nh_N()
const {
return nh_N; }
40 [[nodiscard]] length_t get_nc()
const {
return nc; }
42 [[nodiscard]] length_t get_nc_N()
const {
return nc_N; }
45 void get_U(Box &U)
const {
46 U.lowerbound.setConstant(-1);
47 U.upperbound.setConstant(+1);
50 void get_D([[maybe_unused]] Box &D)
const {}
52 void get_D_N([[maybe_unused]] Box &D)
const {}
55 void get_x_init(rvec x_init)
const { x_init.setConstant(10.); }
58 void eval_f([[maybe_unused]] index_t timestep, crvec x, crvec u,
60 fxu.noalias() = A * x + B * u;
63 void eval_jac_f([[maybe_unused]] index_t timestep, [[maybe_unused]] crvec x,
64 [[maybe_unused]] crvec u, rmat J_fxu)
const {
65 J_fxu.leftCols(nx).noalias() = A;
66 J_fxu.rightCols(nu).noalias() = B;
69 void eval_grad_f_prod([[maybe_unused]] index_t timestep,
70 [[maybe_unused]] crvec x, [[maybe_unused]] crvec u,
71 crvec p, rvec grad_fxu_p)
const {
72 grad_fxu_p.topRows(nx).noalias() = A.transpose() * p;
73 grad_fxu_p.bottomRows(nu).noalias() = B.transpose() * p;
76 void eval_h([[maybe_unused]] index_t timestep, crvec x, crvec u,
82 void eval_h_N(crvec x, rvec h)
const { h = x; }
84 [[nodiscard]] real_t eval_l([[maybe_unused]] index_t timestep,
86 return 0.5 * h.squaredNorm();
89 [[nodiscard]] real_t eval_l_N(crvec h)
const {
90 return 5.0 * h.squaredNorm();
93 void eval_qr([[maybe_unused]] index_t timestep, [[maybe_unused]] crvec xu,
94 crvec h, rvec qr)
const {
95 auto Jh_xu = mat::Identity(nx + nu, nx + nu);
97 qr = Jh_xu.transpose() * grad_l;
100 void eval_q_N([[maybe_unused]] crvec x, crvec h, rvec q)
const {
101 auto Jh_x = mat::Identity(nx, nx);
102 auto &&grad_l = 10 * h;
103 q = Jh_x.transpose() * grad_l;
106 void eval_add_Q([[maybe_unused]] index_t timestep,
107 [[maybe_unused]] crvec xu, [[maybe_unused]] crvec h,
109 Q += mat::Identity(nx, nx);
112 void eval_add_Q_N([[maybe_unused]] crvec x, [[maybe_unused]] crvec h,
114 Q += 10 * mat::Identity(nx, nx);
117 void eval_add_R_masked([[maybe_unused]] index_t timestep,
118 [[maybe_unused]] crvec xu, [[maybe_unused]] crvec h,
119 crindexvec mask, rmat R,
120 [[maybe_unused]] rvec work)
const {
121 auto R_full = mat::Identity(nu, nu);
122 R.noalias() += R_full(mask, mask);
125 void eval_add_S_masked([[maybe_unused]] index_t timestep,
126 [[maybe_unused]] crvec xu, [[maybe_unused]] crvec h,
127 crindexvec mask, rmat S,
128 [[maybe_unused]] rvec work)
const {
130 using Eigen::indexing::all;
131 auto S_full = mat::Zero(nu, nx);
132 S += S_full(mask, all);
135 void eval_add_R_prod_masked([[maybe_unused]] index_t timestep,
136 [[maybe_unused]] crvec xu,
137 [[maybe_unused]] crvec h, crindexvec mask_J,
138 crindexvec mask_K, crvec v, rvec out,
139 [[maybe_unused]] rvec work)
const {
141 auto R_full = mat::Identity(nu, nu);
142 out.noalias() += R_full(mask_J, mask_K) * v(mask_K);
145 void eval_add_S_prod_masked([[maybe_unused]] index_t timestep,
146 [[maybe_unused]] crvec xu,
147 [[maybe_unused]] crvec h,
148 [[maybe_unused]] crindexvec mask_K,
149 [[maybe_unused]] crvec v,
150 [[maybe_unused]] rvec out,
151 [[maybe_unused]] rvec work)
const {
153 using Eigen::indexing::all;
154 auto Sᵀ = mat::Zero(nx, nu);
155 out.noalias() += Sᵀ(all, mask_K) * v(mask_K);
157 [[nodiscard]] length_t get_R_work_size()
const {
162 [[nodiscard]] length_t get_S_work_size()
const {
169 void eval_proj_multipliers([[maybe_unused]] rvec y,
170 [[maybe_unused]] real_t M)
const {
175 void eval_proj_diff_g([[maybe_unused]] crvec z,
176 [[maybe_unused]] rvec e)
const {
197 vec u = vec::Zero(n);
198 vec y = vec::Zero(m);
199 vec μ = vec::Ones(m);
210 auto stats = solver(problem, {.tolerance = 1e-8}, u, y, μ, e);
213 auto δ = e.lpNorm<Eigen::Infinity>();
214 auto time_s = std::chrono::duration<double>(stats.elapsed_time).count();
217 <<
"solver: " << solver.get_name() <<
'\n'
218 <<
"status: " << stats.status <<
'\n'
223 <<
"iter: " << std::setw(6) << stats.iterations <<
'\n'
224 <<
"line search backtrack: " << std::setw(6)
225 << stats.linesearch_backtracks <<
'\n'
226 <<
"step size backtrack: " << std::setw(6)
227 << stats.stepsize_backtracks <<
'\n'
int main(int argc, const char *argv[])
#define USING_ALPAQA_CONFIG(Conf)
unsigned print_interval
When to print progress.
unsigned gn_interval
How often to use a Gauss-Newton step. Zero to disable GN entirely.
PANOCStopCrit stop_crit
What stopping criterion to use.
Tuning parameters for the PANOC algorithm.
@ ProjGradUnitNorm
∞-norm of the projected gradient with unit step size:
std::ostream & print_python(std::ostream &os, const Eigen::DenseBase< Derived > &M, Args &&...args)
std::string float_to_str(F value, int precision)
length_t get_nc_N() const
std::shared_ptr< OCPEvalCounter > evaluations
Double-precision double configuration.