alpaqa guanaqo
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[]) {
14 alpaqa::init_stdout();
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 const auto inf = alpaqa::inf<config_t>;
30 problem.variable_bounds.upper = vec::Constant(3, +inf);
31 problem.variable_bounds.lower = vec::Constant(3, -inf);
32 problem.general_bounds.upper = vec::Constant(1, 0.);
33 problem.general_bounds.lower = vec::Constant(1, 0.);
34
35 // Define the solvers to use
36 using Direction = alpaqa::LBFGSDirection<config_t>;
37 using InnerSolver = alpaqa::PANOCSolver<Direction>;
38 using OuterSolver = alpaqa::ALMSolver<InnerSolver>;
39
40 // Settings for the outer augmented Lagrangian method
41 OuterSolver::Params almparam;
42 almparam.tolerance = 1e-8; // tolerance
43 almparam.dual_tolerance = 1e-8;
44 almparam.penalty_update_factor = 10;
45 almparam.max_iter = 20;
46 almparam.print_interval = 1;
47
48 // Settings for the inner PANOC solver
49 InnerSolver::Params panocparam;
50 panocparam.max_iter = 500;
51 panocparam.print_interval = 10;
52 // Settings for the L-BFGS algorithm used by PANOC
53 Direction::AcceleratorParams lbfgsparam;
54 lbfgsparam.memory = 10;
55
56 // Create an ALM solver using PANOC as inner solver
57 OuterSolver solver{
58 almparam, // params for outer solver
59 {panocparam, lbfgsparam}, // inner solver
60 };
61
62 // Initial guess
63 vec x(3);
64 x << 2.5, 3.0, 0.75;
65 vec y(1);
66 y << 1;
67
68 // Parameter
69 problem.param(0) = 100;
70
71 // Wrap the problem to count the function evaluations
72 auto counted_problem = alpaqa::problem_with_counters_ref(problem);
73
74 // Solve the problem
75 auto stats = solver(counted_problem, x, y);
76
77 // Print the results
78 std::cout << '\n' << *counted_problem.evaluations << '\n';
79 vec g(problem.num_constraints);
80 problem.eval_constraints(x, g);
81 std::cout << "status: " << stats.status << '\n'
82 << "x = " << x.transpose() << '\n'
83 << "y = " << y.transpose() << '\n'
84 << "f = " << problem.eval_objective(x) << '\n'
85 << "g = " << g.transpose() << '\n'
86 << "ε = " << stats.ε << '\n'
87 << "δ = " << stats.δ << '\n'
88 << "inner: " << stats.inner.iterations << '\n'
89 << "outer: " << stats.outer_iterations << '\n'
90 << std::endl;
91}
int main(int argc, const char *argv[])
Augmented Lagrangian Method solver.
Definition alm.hpp:69
Box general_bounds
Other constraints, .
Box variable_bounds
Constraints of the decision variables, .
length_t num_constraints
Number of constraints, dimension of g(x) and z.
Problem definition for a CasADi problem, loaded from a DLL.
real_t eval_objective(crvec x) const
void eval_constraints(crvec x, rvec g) const
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