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
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
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
27
28
34
35
39
40
41 OuterSolver::Params almparam;
42 almparam.tolerance = 1e-8;
43 almparam.dual_tolerance = 1e-8;
44 almparam.penalty_update_factor = 10;
45 almparam.max_iter = 20;
46 almparam.print_interval = 1;
47
48
49 InnerSolver::Params panocparam;
50 panocparam.max_iter = 500;
51 panocparam.print_interval = 10;
52
53 Direction::AcceleratorParams lbfgsparam;
54 lbfgsparam.memory = 10;
55
56
57 OuterSolver solver{
58 almparam,
59 {panocparam, lbfgsparam},
60 };
61
62
63 vec x(3);
64 x << 2.5, 3.0, 0.75;
65 vec y(1);
66 y << 1;
67
68
69 problem.
param(0) = 100;
70
71
73
74
75 auto stats = solver(counted_problem, x, y);
76
77
78 std::cout << '\n' << *counted_problem.evaluations << '\n';
81 std::cout << "status: " << stats.status << '\n'
82 << "x = " << x.transpose() << '\n'
83 << "y = " << y.transpose() << '\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.
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
#define USING_ALPAQA_CONFIG(Conf)
auto problem_with_counters_ref(Problem &p)
Wraps the given problem into a ProblemWithCounters and keeps track of how many times each function is...
Double-precision double configuration.