alpaqa 0.0.1
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 alpaqa.

Problem generation using CasADi

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

Problem solution using alpaqa

/**
* @example CasADi/Rosenbrock/main.cpp
*
* This example shows how to generate a problem using CasADi and how to load
* and solve it using alpaqa.
*
* # Problem generation using CasADi
* @include CasADi/Rosenbrock/codegen-rosenbrock.py
* # Problem solution using alpaqa
*/
#include <iostream>
int main(int argc, char *argv[]) {
using alpaqa::inf;
using alpaqa::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;
}
int main(int argc, char *argv[])
Augmented Lagrangian Method solver.
Definition: decl/alm.hpp:82
alpaqa::Problem load_CasADi_problem(const std::string &filename, unsigned n=0, unsigned m=0, bool second_order=false)
Load a problem generated by CasADi (without parameters).
constexpr real_t inf
Definition: vec.hpp:26
realvec vec
Default type for vectors.
Definition: vec.hpp:14
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.
lbfgsparam
Definition: main.py:38
almparam
Definition: main.py:25
panocparam
Definition: main.py:33
Problem description for minimization problems.