This example shows how to generate a problem using CasADi and how to load and solve it using alpaqa.
Problem generation using CasADi
1from casadi
import SX, Function, CodeGenerator, vertcat, jtimes, gradient
5 print(f
"Usage: {argv[0]} <name>")
11unknwns = vertcat(x, y, z)
19cg = CodeGenerator(f
"{argv[1]}.c")
20cg.add(Function(
"f", [unknwns],
23cg.add(Function(
"grad_f", [unknwns],
24 [gradient(f, unknwns)],
26cg.add(Function(
"g", [unknwns],
29cg.add(Function(
"grad_g", [unknwns, w],
30 [jtimes(g, unknwns, w,
True)],
31 [
"x",
"w"], [
"grad_g"]))
Problem solution using alpaqa
#include <iostream>
int main(
int argc,
char *argv[]) {
auto so_name = "examples/CasADi/Rosenbrock/librosenbrock_functions.so";
if (argc > 1)
so_name = argv[1];
p.C.upperbound = vec::Constant(3,
inf);
p.C.lowerbound = vec::Constant(3, -
inf);
p.D.upperbound = vec::Constant(1, 0.);
p.D.lowerbound = vec::Constant(1, 0.);
};
std::cout <<
"status: " <<
stats.status << std::endl;
std::cout <<
"x = " <<
x.transpose() << std::endl;
std::cout <<
"y = " <<
y.transpose() << std::endl;
std::cout <<
"g = " <<
g.transpose() << std::endl;
std::cout <<
"f = " <<
p.f(
x) << std::endl;
std::cout <<
"inner: " <<
stats.inner.iterations << std::endl;
std::cout <<
"outer: " <<
stats.outer_iterations << std::endl;
}
int main(int argc, char *argv[])
Augmented Lagrangian Method solver.
alpaqa::Problem load_CasADi_problem(const std::string &filename, unsigned n=0, unsigned m=0, bool second_order=false)
Load a problem generated by CasADi (without parameters).
realvec vec
Default type for vectors.
Parameters for the Augmented Lagrangian solver.
Parameters for the LBFGS and SpecializedLBFGS classes.
Tuning parameters for the PANOC algorithm.
Problem description for minimization problems.