alpaqa 0.0.1
Nonconvex constrained optimization
Presentation/Cpp/main.cpp
Go to the documentation of this file.
1#include <alpaqa/alm.hpp>
4
5#include <iostream>
6
7int main() {
8 using alpaqa::vec;
9 using alpaqa::rvec;
10 using alpaqa::crvec;
11 using alpaqa::mat;
12 using alpaqa::inf;
13
14 // Problem specification
15 alpaqa::Problem problem(2, 1); // # decision variables, # constraints
16
17 // minimize ½ xᵀHx
18 // s.t. Ax ≤ b
19 mat H(2, 2); H << 3, -1,
20 -1, 3;
21 mat A(1, 2); A << 2, 1;
22 vec b(1); b << -1;
23
24 problem.f = [&](crvec x) { return 0.5 * x.dot(H * x); };
25 problem.grad_f = [&](crvec x, rvec gr) { gr = H * x; };
26 problem.g = [&](crvec x, rvec g) { g = A * x; };
27 problem.grad_g_prod = [&](crvec x, crvec y, rvec gr) { gr = A.transpose() * y; };
28
29 // Specify the bounds
30 problem.C.lowerbound = vec::Constant(problem.n, -inf);
31 problem.C.upperbound = vec::Constant(problem.n, inf);
32 problem.D.lowerbound = vec::Constant(problem.m, -inf);
33 problem.D.upperbound = b;
34
35 // Settings for the outer augmented Lagrangian method
37 almparam.ε = 1e-8; // tolerance
38 almparam.δ = 1e-8;
39 almparam.Δ = 10; // penalty update factor
40 almparam.max_iter = 20;
41 almparam.print_interval = 1;
42
43 // Settings for the inner PANOC solver
45 panocparam.max_iter = 500;
46 panocparam.print_interval = 10;
47 // Settings for the L-BFGS algorithm used by PANOC
49 lbfgsparam.memory = 2;
50
51 // Create an ALM solver using PANOC as inner solver
53 almparam, // params for outer solver
54 {panocparam, lbfgsparam}, // inner solver
55 };
56
57 // Initial guess
58 vec x(2); x << 2, 2; // decision variables
59 vec y(1); y << 1; // Lagrange multipliers
60
61 // Solve the problem
62 auto stats = solver(problem, y, x);
63 // y and x have been overwritten by the solution
64
65 // Print the results
66 std::cout << "status: " << stats.status << std::endl;
67 std::cout << "inner iterations: " << stats.inner.iterations << std::endl;
68 std::cout << "outer iterations: " << stats.outer_iterations << std::endl;
69 std::cout << "elapsed time: " << stats.elapsed_time.count() * 1e-6 << 's' << std::endl;
70 std::cout << "x = " << x.transpose() << std::endl;
71 std::cout << "y = " << y.transpose() << std::endl;
72 std::cout << "f = " << problem.f(x) << std::endl;
73}
int main()
Augmented Lagrangian Method solver.
Definition: decl/alm.hpp:82
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
constexpr real_t inf
Definition: vec.hpp:26
realmat mat
Default type for matrices.
Definition: vec.hpp:20
realvec vec
Default type for vectors.
Definition: vec.hpp:14
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
Parameters for the Augmented Lagrangian solver.
Definition: decl/alm.hpp:13
Parameters for the LBFGS and SpecializedLBFGS classes.
Definition: decl/lbfgs.hpp:12
Tuning parameters for the PANOC algorithm.
problem
Definition: main.py:16
A
Definition: main.py:10
lbfgsparam
Definition: main.py:38
almparam
Definition: main.py:25
panocparam
Definition: main.py:33
H
Definition: main.py:8
Problem description for minimization problems.