Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Beale.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_BEALE_HH
14#define OPTIMIST_TESTSET_BEALE_HH
15
17
18namespace Optimist {
19
20 namespace TestSet {
21
34 template <typename Vector>
35 class Beale : public DenseCostFunction<Vector, 2, Beale<Vector>> {
36 public:
38 using typename Base::Dense;
39 using typename Base::DenseMatrix;
40 using typename Base::Scalar;
41
46 this->add_solution(this->make({3.0, 0.5}));
47 this->add_guess(this->make({1.0, 1.0}));
48 this->add_guess(this->make({0.0, 0.0}));
49 this->add_guess(this->make({2.0, 0.2}));
50 }
51
52 constexpr std::string name_impl() const {
53 return "Beale";
54 }
55
56 Scalar value(const Dense &x) const {
57 Scalar f{0.0};
58 for (Integer i{1}; i <= 3; ++i) {
59 const Scalar t{c(i) - x(0) + x(0) * std::pow(x(1), Scalar(i))};
60 f += t * t;
61 }
62 return f;
63 }
64
65 void gradient(const Dense &x, Dense &g) const {
66 g.setZero();
67 for (Integer i{1}; i <= 3; ++i) {
68 const Scalar yi{std::pow(x(1), Scalar(i))};
69 const Scalar t{c(i) - x(0) + x(0) * yi};
70 g(0) += 2 * t * (yi - 1);
71 g(1) += 2 * t * (i * x(0) * std::pow(x(1), Scalar(i - 1)));
72 }
73 }
74
75 void hessian(const Dense &x, DenseMatrix &H) const {
76 H.setZero();
77 for (Integer i{1}; i <= 3; ++i) {
78 const Scalar si{static_cast<Scalar>(i)};
79 const Scalar yi{std::pow(x(1), si)};
80 const Scalar yi1{std::pow(x(1), si - 1)};
81 const Scalar yi2{i >= 2 ? std::pow(x(1), si - 2) : Scalar(0)};
82 const Scalar t{c(i) - x(0) + x(0) * yi};
83 const Scalar tx{yi - 1}, ty{si * x(0) * yi1};
84 const Scalar txy{si * yi1}, tyy{si * (si - 1) * x(0) * yi2};
85 H(0, 0) += 2 * tx * tx;
86 H(0, 1) += 2 * (tx * ty + t * txy);
87 H(1, 1) += 2 * (ty * ty + t * tyy);
88 }
89 H(1, 0) = H(0, 1);
90 }
91
92 private:
93 static Scalar c(const Integer i) {
94 constexpr Scalar values[3]{1.5, 2.25, 2.625};
95 return values[i - 1];
96 }
97
98 }; // class Beale
99
100 } // namespace TestSet
101
102} // namespace Optimist
103
104#endif // OPTIMIST_TESTSET_BEALE_HH
Scalar value(const Dense &x) const
Definition Beale.hh:56
constexpr std::string name_impl() const
Definition Beale.hh:52
Utils::DenseVector< Vector > Dense
Definition DenseFunction.hh:47
DenseCostFunction< Vector, 2, Beale< Vector > > Base
Definition Beale.hh:37
Beale()
Definition Beale.hh:45
void gradient(const Dense &x, Dense &g) const
Definition Beale.hh:65
Utils::DenseMatrix< Vector > DenseMatrix
Definition DenseFunction.hh:48
void hessian(const Dense &x, DenseMatrix &H) const
Definition Beale.hh:75
static Scalar c(const Integer i)
Definition Beale.hh:93
typename Vector::Scalar Scalar
Definition DenseFunction.hh:46
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
Namespace for the Optimist library.
Definition Optimist.hh:90
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:98