alpaqa 1.0.0a14
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.

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