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