-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
67 lines (52 loc) · 1.76 KB
/
Copy pathexample.cpp
File metadata and controls
67 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**********************************************************************
Copyright (c) 2023 Marcel A. Samek. All rights reserved.
Licensed under the MIT License
See LICENSE file in the project root for full license information.
**********************************************************************/
#include "nm.h"
#include <math.h>
#include <stdio.h>
double myFunction(const std::vector<double> & x)
{
double a = -1.23456;
double b = 6.54321;
double v1 = b * b - a;
double v2 = x[0] * x[0] - x[1];
double w1 = a * a - b;
double w2 = x[1] * x[1] - x[0];
double v = sqrt((v1-v2)*(v1-v2) + (w1 - w2) * (w1 - w2));
return v;
}
void myConstraints(std::vector<double>& x)
{
for (auto & xi : x) {
if (xi < -600) {
xi = -600;
}
if (xi > 600) {
xi = 600;
}
}
}
void printResults(const NelderMeadResults& results)
{
printf(" %u Function Evaluations\n", results.evalCount);
printf(" %u Iterations through program\n", results.iterationCount);
printf(" Best result: %le\n", results.min);
for (uint32_t i = 0; i < results.minValues.size(); i++) {
printf(" Best variables: %le\n", results.minValues[i]);
}
}
int main()
{
// allocate the Nelder-Mead object searching for 2 variables
NelderMead* simp = new NelderMead(2, myFunction, nullptr);
simp->setMaxIterations(100000);
fprintf(stdout, "Trying Nelder Mead with tolerance 1.0e-6\n");
simp->exec(std::vector<double>{ 1, 1 }, 1.0e-6, 1.0);
printResults(simp->getLastExecResults());
fprintf(stdout, "Trying Nelder Mead with tolerance 1.0e-12\n");
simp->exec(std::vector<double>{ 1, 1 }, 1.0e-12, 1.0);
printResults(simp->getLastExecResults());
return 0;
}