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.
11 real_t a = 2, b = 100;
14 length_t get_n()
const {
return 2; }
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));
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));
30 static real_t sq(real_t x) {
return x * x; }
36 RosenbrockProblem problem;
46 Solver::Params panocparam;
47 panocparam.max_iter = 500;
48 panocparam.print_interval = 1;
50 Direction::AcceleratorParams lbfgsparam;
51 lbfgsparam.memory = 2;
54 Solver solver{panocparam, lbfgsparam};
60 auto stats = solver(counted_problem, {.tolerance = 1e-8}, x);
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'
70 << std::chrono::duration<double>{stats.elapsed_time}.count()
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'
int main(int argc, const char *argv[])
#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...