Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Himmelblau.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2026, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_TESTSET_HIMMELBLAU_HH
14#define OPTIMIST_TESTSET_HIMMELBLAU_HH
15
17
18namespace Optimist {
19
20 namespace TestSet {
21
34 template <typename Vector>
36 : public DenseCostFunction<Vector, 2, Himmelblau<Vector>> {
37 public:
39 using typename Base::Dense;
40 using typename Base::DenseMatrix;
41 using typename Base::Scalar;
42
47 this->add_solution(this->make({3.0, 2.0}));
48 this->add_solution(this->make({-2.805118086952745, 3.131312518250573}));
49 this->add_solution(
50 this->make({-3.779310253377747, -3.283185991286170}));
51 this->add_solution(this->make({3.584428340330492, -1.848126526964404}));
52 this->add_guess(this->make({2.0, 2.0}));
53 this->add_guess(this->make({-2.0, 2.0}));
54 this->add_guess(this->make({-2.0, -2.0}));
55 this->add_guess(this->make({2.0, -2.0}));
56 }
57
58 constexpr std::string name_impl() const {
59 return "Himmelblau";
60 }
61
62 Scalar value(const Dense &x) const {
63 const Scalar a{x(0) * x(0) + x(1) - 11}, b{x(0) + x(1) * x(1) - 7};
64 return a * a + b * b;
65 }
66
67 void gradient(const Dense &x, Dense &g) const {
68 const Scalar a{x(0) * x(0) + x(1) - 11}, b{x(0) + x(1) * x(1) - 7};
69 g(0) = 4 * x(0) * a + 2 * b;
70 g(1) = 2 * a + 4 * x(1) * b;
71 }
72
73 void hessian(const Dense &x, DenseMatrix &H) const {
74 H(0, 0) = 12 * x(0) * x(0) + 4 * x(1) - 42;
75 H(0, 1) = 4 * (x(0) + x(1));
76 H(1, 0) = H(0, 1);
77 H(1, 1) = 4 * x(0) + 12 * x(1) * x(1) - 26;
78 }
79
80 }; // class Himmelblau
81
82 } // namespace TestSet
83
84} // namespace Optimist
85
86#endif // OPTIMIST_TESTSET_HIMMELBLAU_HH
Helper base class for scalar cost functions written once in terms of dense vectors.
Definition DenseFunction.hh:43
void add_solution(const Dense &x)
Definition DenseFunction.hh:94
Utils::DenseMatrix< Vector > DenseMatrix
Definition DenseFunction.hh:48
void add_guess(const Dense &x)
Definition DenseFunction.hh:101
Utils::DenseVector< Vector > Dense
Definition DenseFunction.hh:47
static Dense make(std::initializer_list< Scalar > values)
Definition DenseFunction.hh:108
typename Vector::Scalar Scalar
Definition DenseFunction.hh:46
void hessian(const Dense &x, DenseMatrix &H) const
Definition Himmelblau.hh:73
void gradient(const Dense &x, Dense &g) const
Definition Himmelblau.hh:67
DenseCostFunction< Vector, 2, Himmelblau< Vector > > Base
Definition Himmelblau.hh:38
Himmelblau()
Definition Himmelblau.hh:46
constexpr std::string name_impl() const
Definition Himmelblau.hh:58
Scalar value(const Dense &x) const
Definition Himmelblau.hh:62
Namespace for the Optimist library.
Definition Optimist.hh:90