alpaqa 1.0.0a19
Nonconvex constrained optimization
Loading...
Searching...
No Matches
C++/SimpleUnconstrProblem/main.cpp

This example shows how to define a simple unconstrained optimization problem using ordinary C++ functions.

This example shows how to define a simple unconstrained optimization problem using ordinary C++ functions.

The problem is the unconstrained minimization of the Rosenbrock function:

\[ \begin{aligned} & \underset{x, y}{\text{minimize}} && (a - x)^2 + b (y - x^2)^2 \\ \end{aligned} \]

Only the inner solver (Inner solvers) is used, without an augmented Lagrangian outer solver.

1#include <alpaqa/example-util.hpp>
5
6#include <iostream>
7
8// Problem specification
9// minimize (a - x)² + b(y - x²)²
10struct RosenbrockProblem : alpaqa::UnconstrProblem<alpaqa::DefaultConfig> {
11 // Specify the number of unknowns
12 RosenbrockProblem() : UnconstrProblem{2} {}
13
14 // Problem parameters
15 real_t a = 2, b = 100;
16
17 // Cost
18 real_t eval_f(crvec xy) const {
19 auto x = xy(0), y = xy(1);
20 return sq(a - x) + b * sq(y - sq(x));
21 }
22
23 // Gradient of cost
24 void eval_grad_f(crvec xy, rvec grad) const {
25 auto x = xy(0), y = xy(1);
26 grad(0) = (2 * x) - (2 * a) - (4 * b * x * y) + (4 * b * x * sq(x));
27 grad(1) = 2 * b * (y - sq(x));
28 }
29
30 // Helper function
31 static real_t sq(real_t x) { return x * x; }
32};
33
34int main() {
36 USING_ALPAQA_CONFIG(RosenbrockProblem::config_t);
37
38 // Instantiate a problem
39 RosenbrockProblem problem;
40
41 // Wrap the problem to count the function evaluations
42 auto counted_problem = alpaqa::problem_with_counters_ref(problem);
43
44 // Define the solver to use
45 using Direction = alpaqa::LBFGSDirection<config_t>;
46 using Solver = alpaqa::PANOCSolver<Direction>;
47
48 // Settings for the inner PANOC solver
49 Solver::Params panocparam;
50 panocparam.max_iter = 500;
51 panocparam.print_interval = 1;
52 // Settings for the L-BFGS algorithm used by PANOC
53 Direction::AcceleratorParams lbfgsparam;
54 lbfgsparam.memory = 2;
55
56 // Create a PANOC solver
57 Solver solver{panocparam, lbfgsparam};
58
59 // Initial guess
60 vec x = vec::Zero(2); // decision variables
61
62 // Solve the problem
63 auto stats = solver(counted_problem, {.tolerance = 1e-8}, x);
64 // y and x have been overwritten by the solution
65
66 // Print the results
67 std::cout << '\n' << *counted_problem.evaluations << '\n';
68 std::cout << "status: " << stats.status << '\n'
69 << "f = " << problem.eval_f(x) << '\n'
70 << "iterations: " << stats.iterations << '\n'
71 << "ε = " << stats.ε << '\n'
72 << "elapsed time: "
73 << std::chrono::duration<double>{stats.elapsed_time}.count()
74 << " s" << '\n'
75 << "x = " << x.transpose() << '\n'
76 << "avg τ = " << (stats.sum_τ / stats.count_τ) << '\n'
77 << "L-BFGS rejected = " << stats.lbfgs_rejected << '\n'
78 << "L-BFGS failures = " << stats.lbfgs_failures << '\n'
79 << "Line search failures = " << stats.linesearch_failures << '\n'
80 << std::endl;
81}
int main(int argc, const char *argv[])
PANOC solver for ALM.
Definition panoc.hpp:142
Implements common problem functions for minimization problems without constraints.
#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
typename Conf::vec vec
Definition config.hpp:88