4175 |
01 Jun 22 |
peter |
// $Id$ |
4175 |
01 Jun 22 |
peter |
2 |
|
4175 |
01 Jun 22 |
peter |
3 |
/* |
4175 |
01 Jun 22 |
peter |
Copyright (C) 2022 Peter Johansson |
4175 |
01 Jun 22 |
peter |
5 |
|
4175 |
01 Jun 22 |
peter |
This file is part of the yat library, https://dev.thep.lu.se/yat |
4175 |
01 Jun 22 |
peter |
7 |
|
4175 |
01 Jun 22 |
peter |
The yat library is free software; you can redistribute it and/or |
4175 |
01 Jun 22 |
peter |
modify it under the terms of the GNU General Public License as |
4175 |
01 Jun 22 |
peter |
published by the Free Software Foundation; either version 3 of the |
4175 |
01 Jun 22 |
peter |
License, or (at your option) any later version. |
4175 |
01 Jun 22 |
peter |
12 |
|
4175 |
01 Jun 22 |
peter |
The yat library is distributed in the hope that it will be useful, |
4175 |
01 Jun 22 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4175 |
01 Jun 22 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
4175 |
01 Jun 22 |
peter |
General Public License for more details. |
4175 |
01 Jun 22 |
peter |
17 |
|
4175 |
01 Jun 22 |
peter |
You should have received a copy of the GNU General Public License |
4175 |
01 Jun 22 |
peter |
along with yat. If not, see <https://www.gnu.org/licenses/>. |
4175 |
01 Jun 22 |
peter |
20 |
*/ |
4175 |
01 Jun 22 |
peter |
21 |
|
4175 |
01 Jun 22 |
peter |
22 |
#include <config.h> |
4175 |
01 Jun 22 |
peter |
23 |
|
4175 |
01 Jun 22 |
peter |
24 |
#include "Suite.h" |
4175 |
01 Jun 22 |
peter |
25 |
|
4175 |
01 Jun 22 |
peter |
26 |
#include "yat/utility/MultiMinimizerDerivative.h" |
4175 |
01 Jun 22 |
peter |
27 |
#include "yat/utility/BFGS.h" |
4175 |
01 Jun 22 |
peter |
28 |
#include "yat/utility/BFGS2.h" |
4175 |
01 Jun 22 |
peter |
29 |
#include "yat/utility/FletcherReevesConjugate.h" |
4175 |
01 Jun 22 |
peter |
30 |
#include "yat/utility/PolakRibiereConjugate.h" |
4175 |
01 Jun 22 |
peter |
31 |
#include "yat/utility/SteepestDescent.h" |
4175 |
01 Jun 22 |
peter |
32 |
|
4175 |
01 Jun 22 |
peter |
33 |
using namespace theplu::yat; |
4175 |
01 Jun 22 |
peter |
34 |
using namespace theplu::yat::utility; |
4175 |
01 Jun 22 |
peter |
35 |
|
4175 |
01 Jun 22 |
peter |
36 |
struct MyFunction |
4175 |
01 Jun 22 |
peter |
37 |
{ |
4175 |
01 Jun 22 |
peter |
38 |
double operator()(const utility::VectorBase& x) const |
4175 |
01 Jun 22 |
peter |
39 |
{ |
4175 |
01 Jun 22 |
peter |
40 |
return x(0) * x(0) + 10 * x(1) * x(1); |
4175 |
01 Jun 22 |
peter |
41 |
} |
4175 |
01 Jun 22 |
peter |
42 |
|
4175 |
01 Jun 22 |
peter |
43 |
void operator()(const utility::VectorBase& x, |
4175 |
01 Jun 22 |
peter |
44 |
utility::VectorMutable& grad) const |
4175 |
01 Jun 22 |
peter |
45 |
{ |
4175 |
01 Jun 22 |
peter |
46 |
grad(0) = 2 * x(0); |
4175 |
01 Jun 22 |
peter |
47 |
grad(1) = 10 * 2 * x(1); |
4175 |
01 Jun 22 |
peter |
48 |
} |
4175 |
01 Jun 22 |
peter |
49 |
}; |
4175 |
01 Jun 22 |
peter |
50 |
|
4175 |
01 Jun 22 |
peter |
51 |
|
4175 |
01 Jun 22 |
peter |
52 |
void run_test(MultiMinimizerDerivative& minimizer, test::Suite& suite) |
4175 |
01 Jun 22 |
peter |
53 |
{ |
4175 |
01 Jun 22 |
peter |
54 |
MyFunction func; |
4175 |
01 Jun 22 |
peter |
55 |
Vector x(2); |
4252 |
18 Nov 22 |
peter |
56 |
minimizer(x, func, MultiMinimizerDerivative::Gradient(1e-5), 1000); |
4175 |
01 Jun 22 |
peter |
57 |
suite.out() << "result x: " << x(0) << " " << x(1) << "\n"; |
4252 |
18 Nov 22 |
peter |
58 |
if (minimizer.epochs() == 1000) { |
4252 |
18 Nov 22 |
peter |
59 |
suite.err() << "max epoch reached\n"; |
4252 |
18 Nov 22 |
peter |
60 |
suite.add(false); |
4252 |
18 Nov 22 |
peter |
61 |
} |
4175 |
01 Jun 22 |
peter |
62 |
if (!suite.equal_fix(x(0), 0.0, 1e-5)) |
4175 |
01 Jun 22 |
peter |
63 |
suite.err() << "error: incorrect x(0)\n"; |
4175 |
01 Jun 22 |
peter |
64 |
if (!suite.equal_fix(x(1), 0.0, 1e-5)) |
4175 |
01 Jun 22 |
peter |
65 |
suite.err() << "error: incorrect x(1)\n"; |
4252 |
18 Nov 22 |
peter |
66 |
|
4252 |
18 Nov 22 |
peter |
67 |
if (false) { |
4252 |
18 Nov 22 |
peter |
68 |
minimizer(x, func, MultiMinimizerDerivative::Gradient(1e-5)); |
4252 |
18 Nov 22 |
peter |
69 |
} |
4175 |
01 Jun 22 |
peter |
70 |
} |
4175 |
01 Jun 22 |
peter |
71 |
|
4175 |
01 Jun 22 |
peter |
72 |
|
4175 |
01 Jun 22 |
peter |
73 |
template<typename T> |
4175 |
01 Jun 22 |
peter |
74 |
void run_test(test::Suite& suite) |
4175 |
01 Jun 22 |
peter |
75 |
{ |
4175 |
01 Jun 22 |
peter |
76 |
T minimizer(2); |
4175 |
01 Jun 22 |
peter |
77 |
run_test(minimizer, suite); |
4175 |
01 Jun 22 |
peter |
78 |
} |
4175 |
01 Jun 22 |
peter |
79 |
|
4175 |
01 Jun 22 |
peter |
80 |
|
4175 |
01 Jun 22 |
peter |
81 |
int main(int argc, char* argv[]) |
4175 |
01 Jun 22 |
peter |
82 |
{ |
4175 |
01 Jun 22 |
peter |
83 |
test::Suite suite(argc, argv); |
4175 |
01 Jun 22 |
peter |
84 |
|
4175 |
01 Jun 22 |
peter |
85 |
run_test<BFGS>(suite); |
4175 |
01 Jun 22 |
peter |
86 |
run_test<BFGS2>(suite); |
4175 |
01 Jun 22 |
peter |
87 |
run_test<FletcherReevesConjugate>(suite); |
4175 |
01 Jun 22 |
peter |
88 |
run_test<PolakRibiereConjugate>(suite); |
4175 |
01 Jun 22 |
peter |
89 |
run_test<SteepestDescent>(suite); |
4175 |
01 Jun 22 |
peter |
90 |
return suite.return_value(); |
4175 |
01 Jun 22 |
peter |
91 |
} |