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
10namespace fs = std::filesystem;
12int main(
int argc,
char *argv[]) {
16 fs::path so_name = ROSENBROCK_FUNC_DLL;
18 so_name = fs::path(argv[1]);
20 so_name = fs::canonical(fs::path(argv[0])).parent_path() / so_name;
21 std::cout <<
"Loading " << so_name << std::endl;
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.);
38 OuterSolver::Params almparam;
39 almparam.tolerance = 1e-8;
40 almparam.dual_tolerance = 1e-8;
41 almparam.penalty_update_factor = 10;
42 almparam.max_iter = 20;
43 almparam.print_interval = 1;
46 InnerSolver::Params panocparam;
47 panocparam.max_iter = 500;
48 panocparam.print_interval = 10;
50 Direction::AcceleratorParams lbfgsparam;
51 lbfgsparam.memory = 10;
56 {panocparam, lbfgsparam},
66 problem.param(0) = 100;
72 auto stats = solver(counted_problem, x, y);
75 std::cout <<
'\n' << *counted_problem.evaluations <<
'\n';
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'
int main(int argc, const char *argv[])
Augmented Lagrangian Method solver.
Box C
Constraints of the decision variables, .
Problem definition for a CasADi problem, loaded from a DLL.
#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.