PANOC-ALM  quadratic-penalty
Nonconvex constrained optimization
CasADi/Rosenbrock/main.cpp

This example shows how to generate a problem using CasADi and how to load and solve it using PANOC-ALM.

Problem generation using CasADi

1 from casadi import SX, Function, CodeGenerator, vertcat, jtimes, gradient
2 from sys import argv
3 
4 if len(argv) < 2:
5  print(f"Usage: {argv[0]} <name>")
6  exit(0)
7 
8 x = SX.sym("x")
9 y = SX.sym("y")
10 z = SX.sym("z")
11 unknwns = vertcat(x, y, z)
12 
13 w = SX.sym("w")
14 
15 # Formulate the NLP
16 f = x**2 + 100*z**2
17 g = z + (1-x)**2 - y
18 
19 cg = CodeGenerator(f"{argv[1]}.c")
20 cg.add(Function("f", [unknwns],
21  [f],
22  ["x"], ["f"]))
23 cg.add(Function("grad_f", [unknwns],
24  [gradient(f, unknwns)],
25  ["x"], ["grad_f"]))
26 cg.add(Function("g", [unknwns],
27  [g],
28  ["x"], ["g"]))
29 cg.add(Function("grad_g", [unknwns, w],
30  [jtimes(g, unknwns, w, True)],
31  ["x", "w"], ["grad_g"]))
32 cg.generate()

Problem solution using PANOC-ALM

#include <iostream>
int main(int argc, char *argv[]) {
using pa::inf;
using pa::vec;
auto so_name = "examples/CasADi/Rosenbrock/librosenbrock_functions.so";
if (argc > 1)
so_name = argv[1];
// Load the problem (with 3 decision variables and 1 general constraint)
// Specify the bounds
p.C.upperbound = vec::Constant(3, inf);
p.C.lowerbound = vec::Constant(3, -inf);
p.D.upperbound = vec::Constant(1, 0.);
p.D.lowerbound = vec::Constant(1, 0.);
// Settings for the outer augmented Lagrangian method
almparam.ε = 1e-8; // tolerance
almparam.δ = 1e-8;
almparam.Δ = 10;
almparam.max_iter = 20;
almparam.print_interval = 1;
// Settings for the inner PANOC solver
panocparam.max_iter = 500;
panocparam.print_interval = 10;
// Settings for the L-BFGS algorithm used by PANOC
lbfgsparam.memory = 10;
// Create an ALM solver using PANOC as inner solver
almparam, // params for outer solver
{panocparam, lbfgsparam}, // inner solver
};
// Initial guess
vec x(3);
x << 2.5, 3.0, 0.75;
vec y(1);
y << 1;
// Solve the problem
auto stats = solver(p, y, x);
// Print the results
vec g(p.m);
p.g(x, g);
std::cout << "status: " << stats.status << std::endl;
std::cout << "x = " << x.transpose() << std::endl;
std::cout << "y = " << y.transpose() << std::endl;
std::cout << "g = " << g.transpose() << std::endl;
std::cout << "f = " << p.f(x) << std::endl;
std::cout << "inner: " << stats.inner.iterations << std::endl;
std::cout << "outer: " << stats.outer_iterations << std::endl;
}
panocpy.test.stats
stats
Definition: test.py:76
lbfgs.hpp
panocpy.test.y
y
Definition: test.py:76
main.panocparam
panocparam
Definition: main.py:33
alm.hpp
pa::vec
realvec vec
Default type for vectors.
Definition: vec.hpp:14
CasADiLoader.hpp
main.lbfgsparam
lbfgsparam
Definition: main.py:38
pa::ALMSolver
Augmented Lagrangian Method solver.
Definition: decl/alm.hpp:82
pa::PANOCParams
Tuning parameters for the PANOC algorithm.
Definition: inner/decl/panoc.hpp:20
panocpy.test.x
x
Definition: test.py:40
pa::ALMParams
Parameters for the Augmented Lagrangian solver.
Definition: decl/alm.hpp:13
pa::inf
constexpr real_t inf
Definition: vec.hpp:26
main.almparam
almparam
Definition: main.py:25
pa::LBFGSParams
Parameters for the LBFGS and SpecializedLBFGS classes.
Definition: decl/lbfgs.hpp:12
panoc.hpp
panocpy.test.solver
solver
Definition: test.py:8
panocpy.test.g
g
Definition: test.py:51
panocpy.test.p
p
Definition: test.py:57
main
int main(int argc, char *argv[])
Definition: CasADi/Rosenbrock/main.cpp:20
pa::Problem
Problem description for minimization problems.
Definition: include/panoc-alm/util/problem.hpp:24
pa::load_CasADi_problem
pa::Problem load_CasADi_problem(const std::string &filename, unsigned n, unsigned m, bool second_order=false)
Load a problem generated by CasADi (without parameters).
Definition: CasADiLoader.cpp:58