-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfit_main.c
More file actions
93 lines (73 loc) · 2.48 KB
/
fit_main.c
File metadata and controls
93 lines (73 loc) · 2.48 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* This file is part of the NloptWraper_Python-C distribution Copyright (c) 2017
* Jimmy Aguilar Mena.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nlopt.h"
#include <stdio.h>
#include <stdlib.h>
double opt_me(int n, const double *x, double *grad, void *func_data)
{
const double a = x[0];
const double b = x[1];
const double v1 = (a*a + b - 11);
const double v2 = (a + b*b - 7);
double ret = v1*v1 + v2*v2;
#ifndef NDEBUG
printf("f(%lf, %lf) = %lf\n", a, b, ret);
#endif
return ret;
}
int main() {
const int dim = 2;
int maxeval = 1000;
double minrms = 0.01;
double tol = 0.0001;
double lower[] = {-5., -5.};
double upper[] = {5., 5.};
double param_values[] = {0., 0.};
double minf = 0.0;
printf("%d %d\n", NLOPT_G_MLSL_LDS, NLOPT_LN_BOBYQA);
nlopt_opt opt = nlopt_create(NLOPT_G_MLSL_LDS, dim);
nlopt_set_local_optimizer(opt, nlopt_create(NLOPT_LN_BOBYQA, dim));
nlopt_set_lower_bounds(opt, lower);
nlopt_set_upper_bounds(opt, upper);
nlopt_set_min_objective(opt, opt_me, NULL);
nlopt_set_maxeval(opt, maxeval);
nlopt_set_stopval(opt, minrms);
nlopt_set_ftol_abs(opt, tol);
#ifndef NDEBUG
const int n = nlopt_get_dimension(opt);
printf("Algorithm: %d\n", nlopt_get_algorithm(opt));
printf("Dimensions: %u\n", n);
double lupper[n], llower[n];
nlopt_get_upper_bounds(opt, lupper);
nlopt_get_lower_bounds(opt, llower);
printf("Upper [%lf; %lf]\n", upper[0], upper[1]);
printf("Lower [%lf; %lf]\n", lower[0], lower[1]);
printf("Maxeval %d\n", nlopt_get_maxeval(opt));
printf("Stopval %lf\n", nlopt_get_stopval(opt));
printf("Ftol_abs %lf\n", nlopt_get_ftol_abs(opt));
#endif
int dbg = nlopt_optimize(opt, param_values, &minf);
if (dbg < 0) {
fprintf(stderr, "%s:%d %s -> Nlopt C function failed: %d expected: %d\n",
__FILE__, __LINE__, __FUNCTION__, dbg, NLOPT_SUCCESS);
} else {
printf("minimum: f(%lf, %lf) = %lf\n",
param_values[0], param_values[1], minf);
}
nlopt_destroy(opt);
return 0;
}