alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
C++/CasADi/Rosenbrock/main.cpp

This example shows how to generate a problem using CasADi and how to load and solve it using alpaqa.

This example shows how to generate a problem using CasADi and how to load and solve it using alpaqa.

Problem generation using CasADi

1import casadi as cs
2from sys import argv, path
3from os.path import join, dirname
4
5py_path = join(dirname(__file__), '..', '..', '..', '..', 'python', 'alpaqa')
6path.insert(0, py_path)
7import casadi_generator
8
9if len(argv) < 2:
10 print(f"Usage: {argv[0]} <name>")
11 exit(0)
12
13x = cs.SX.sym("x")
14y = cs.SX.sym("y")
15z = cs.SX.sym("z")
16unknowns = cs.vertcat(x, y, z)
17
18p = cs.SX.sym("p")
19
20# Formulate the NLP
21f = x**2 + p * z**2
22g = z + (1 - x)**2 - y
23
24cg = casadi_generator.generate_casadi_problem(
25 cs.Function("f", [unknowns, p], [f]),
26 cs.Function("g", [unknowns, p], [g]),
27 second_order="full",
28 name=argv[1],
29)
30cg.generate()

Problem solution using alpaqa

1#include <alpaqa/example-util.hpp>
6
8
9#include <filesystem>
10#include <iostream>
11namespace fs = std::filesystem;
12
13int main(int argc, char *argv[]) {
16
17 // Find the problem to load
18 fs::path so_name = ROSENBROCK_FUNC_DLL;
19 if (argc > 1)
20 so_name = fs::path(argv[1]);
21 else if (argc > 0)
22 so_name = fs::canonical(fs::path(argv[0])).parent_path() / so_name;
23 std::cout << "Loading " << so_name << std::endl;
24
25 // Load the problem
26 alpaqa::CasADiProblem<config_t> problem{so_name.string()};
27
28 // Specify the bounds
29 problem.C.upperbound = vec::Constant(3, alpaqa::inf<config_t>);
30 problem.C.lowerbound = vec::Constant(3, -alpaqa::inf<config_t>);
31 problem.D.upperbound = vec::Constant(1, 0.);
32 problem.D.lowerbound = vec::Constant(1, 0.);
33
34 // Define the solvers to use
35 using Direction = alpaqa::LBFGSDirection<config_t>;
36 using InnerSolver = alpaqa::PANOCSolver<Direction>;
37 using OuterSolver = alpaqa::ALMSolver<InnerSolver>;
38
39 // Settings for the outer augmented Lagrangian method
40 OuterSolver::Params almparam;
41 almparam.tolerance = 1e-8; // tolerance
42 almparam.dual_tolerance = 1e-8;
43 almparam.penalty_update_factor = 10;
44 almparam.max_iter = 20;
45 almparam.print_interval = 1;
46
47 // Settings for the inner PANOC solver
48 InnerSolver::Params panocparam;
49 panocparam.max_iter = 500;
50 panocparam.print_interval = 10;
51 // Settings for the L-BFGS algorithm used by PANOC
52 Direction::AcceleratorParams lbfgsparam;
53 lbfgsparam.memory = 10;
54
55 // Create an ALM solver using PANOC as inner solver
56 OuterSolver solver{
57 almparam, // params for outer solver
58 {panocparam, lbfgsparam}, // inner solver
59 };
60
61 // Initial guess
62 vec x(3);
63 x << 2.5, 3.0, 0.75;
64 vec y(1);
65 y << 1;
66
67 // Parameter
68 problem.param(0) = 100;
69
70 // Wrap the problem to count the function evaluations
71 auto counted_problem = alpaqa::problem_with_counters_ref(problem);
72
73 // Solve the problem
74 auto stats = solver(counted_problem, x, y);
75
76 // Print the results
77 std::cout << '\n' << *counted_problem.evaluations << '\n';
78 vec g(problem.m);
79 problem.eval_g(x, g);
80 std::cout << "status: " << stats.status << '\n'
81 << "x = " << x.transpose() << '\n'
82 << "y = " << y.transpose() << '\n'
83 << "f = " << problem.eval_f(x) << '\n'
84 << "g = " << g.transpose() << '\n'
85 << "ε = " << stats.ε << '\n'
86 << "δ = " << stats.δ << '\n'
87 << "inner: " << stats.inner.iterations << '\n'
88 << "outer: " << stats.outer_iterations << '\n'
89 << std::endl;
90}
int main(int argc, const char *argv[])
Augmented Lagrangian Method solver.
Definition alm.hpp:69
Box C
Constraints of the decision variables, .
Problem definition for a CasADi problem, loaded from a DLL.
PANOC solver for ALM.
Definition panoc.hpp:142
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
auto problem_with_counters_ref(Problem &p)
Wraps the given problem into a ProblemWithCounters and keeps track of how many times each function is...
constexpr const auto inf
Definition config.hpp:112
Double-precision double configuration.
Definition config.hpp:176