alpaqa 1.0.0a12
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

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