Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Trid.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_TRID_HH
14#define OPTIMIST_TESTSET_TRID_HH
15
17
18namespace Optimist {
19
20 namespace TestSet {
21
37 template <typename Vector, Integer N>
38 class Trid : public DenseCostFunction<Vector, N, Trid<Vector, N>> {
39 public:
41 using typename Base::Dense;
42 using typename Base::DenseMatrix;
43 using typename Base::Scalar;
44
48 Trid() {
49 Dense x(N);
50 for (Integer i{0}; i < N; ++i) {
51 x(i) = static_cast<Scalar>((i + 1) * (N - i));
52 }
53 this->add_solution(x);
54 this->add_guess(Dense::Zero(N));
55 this->add_guess(Dense::Constant(N, 10.0));
56 }
57
58 constexpr std::string name_impl() const {
59 return "Trid<" + std::to_string(N) + ">";
60 }
61
65 static constexpr Scalar minimum() {
66 return -static_cast<Scalar>(N * (N + 4) * (N - 1)) / 6;
67 }
68
69 Scalar value(const Dense &x) const {
70 Scalar f{(x.array() - 1).square().sum()};
71 for (Integer i{1}; i < N; ++i) {
72 f -= x(i) * x(i - 1);
73 }
74 return f;
75 }
76
77 void gradient(const Dense &x, Dense &g) const {
78 for (Integer i{0}; i < N; ++i) {
79 g(i) = 2 * (x(i) - 1);
80 if (i > 0) {
81 g(i) -= x(i - 1);
82 }
83 if (i < N - 1) {
84 g(i) -= x(i + 1);
85 }
86 }
87 }
88
89 void hessian([[maybe_unused]] const Dense &x, DenseMatrix &H) const {
90 H.setZero();
91 for (Integer i{0}; i < N; ++i) {
92 H(i, i) = 2;
93 if (i > 0) {
94 H(i, i - 1) = -1;
95 H(i - 1, i) = -1;
96 }
97 }
98 }
99
100 }; // class Trid
101
102 template <typename Vector>
104
105 template <typename Vector>
107
108 } // namespace TestSet
109
110} // namespace Optimist
111
112#endif // OPTIMIST_TESTSET_TRID_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
typename Vector::Scalar Scalar
Definition DenseFunction.hh:46
Trid function.
Definition Trid.hh:38
DenseCostFunction< Vector, N, Trid< Vector, N > > Base
Definition Trid.hh:40
Scalar value(const Dense &x) const
Definition Trid.hh:69
void hessian(const Dense &x, DenseMatrix &H) const
Definition Trid.hh:89
void gradient(const Dense &x, Dense &g) const
Definition Trid.hh:77
constexpr std::string name_impl() const
Definition Trid.hh:58
Trid()
Definition Trid.hh:48
static constexpr Scalar minimum()
Definition Trid.hh:65
Trid< Vector, 6 > Trid6
Definition Trid.hh:106
Trid< Vector, 2 > Trid2
Definition Trid.hh:103
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