-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNelderMead.cpp
240 lines (215 loc) · 7.19 KB
/
NelderMead.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* NelderMead.cpp
*
* Based on the implementations by Kyle Klein and Jeff Borggaard.
*
*/
#include "NelderMead.hpp"
#include <iostream>
#include "string.h"
#include <algorithm>
NelderMead::NelderMead(double *guess, double step, int dimension,
double (*objFunction)(double *vector, int dimension)) {
indices = new int[dimension + 1];
for (int i = 0; i < dimension + 1; i++) {
indices[i] = i;
}
this->simplex = new double[dimension * (dimension + 1)];
for (int i = 0; i < dimension + 1; i++) {
for (int j = 0; j < dimension; j++) {
SIMPLEX(i,j) = guess[j];
if (i == j + 1)
SIMPLEX(i,j) += step;
}
}
this->dimension = dimension;
this->obj_function = objFunction;
M = new double[dimension];
obj_function_results = new double[dimension + 1];
AR = new double[dimension];
AE = new double[dimension];
AC = new double[dimension];
rho = RHO;
xi = XI;
gam = GAM;
sig = SIG;
feval = 0;
}
NelderMead::NelderMead(int dimension,
double (*objFunction)(double *vector, int dimension)) {
indices = new int[dimension + 1];
for (int i = 0; i < dimension + 1; i++) {
indices[i] = i;
}
this->simplex = new double[dimension * (dimension + 1)];
for (int i = 0; i < dimension + 1; i++) {
for (int j = 0; j < dimension; j++) {
SIMPLEX(i,j) = 1.0;
if (i == j + 1) {
SIMPLEX(i,j) += 1;
}
}
}
this->dimension = dimension;
this->obj_function = objFunction;
M = new double[dimension + 1];
obj_function_results = new double[dimension + 1];
AR = new double[dimension];
AE = new double[dimension];
AC = new double[dimension];
rho = RHO;
xi = XI;
gam = GAM;
sig = SIG;
feval = 0;
}
NelderMead::~NelderMead() {
delete simplex;
delete M;
delete obj_function_results;
delete AR;
delete AE;
delete AC;
}
double* NelderMead::solve(int max_iter) {
// Compute objective function
for (int i = 0; i < dimension + 1; i++) {
obj_function_results[i] = obj_function(&SIMPLEX(i,0), dimension);
feval++;
}
sort_simplex(); //Sort the simplex
double best = obj_function_results[indices[0]];
int iter = 0;
double worst = obj_function_results[indices[dimension]];
while (best > 1e-6 && (max_iter <= 0 || iter < max_iter)) {
// compute centroid
centroid();
// compute reflection and store function value in fAR
reflection();
fAR = obj_function(AR, dimension);
feval++;
if(best <= fAR && fAR <= obj_function_results[indices[dimension - 1]]) {
// accept reflection point
memmove(&SIMPLEX(dimension, 0), AR, dimension * sizeof(double));
obj_function_results[indices[dimension]] = fAR;
} else if( fAR < best ) {
// test for expansion
expansion();
fAE = obj_function(AE, dimension);
feval++;
if(fAE < fAR) {
// accept expansion point
memmove(&SIMPLEX(dimension, 0), AE, dimension * sizeof(double));
obj_function_results[indices[dimension]] = fAE;
} else {
// eventual accept reflection point
memmove(&SIMPLEX(dimension, 0), AR, dimension * sizeof(double));
obj_function_results[indices[dimension]] = fAR;
}
} else if(obj_function_results[indices[dimension - 1]] <=fAR && fAR < worst) {
// do outside contraction
outsidecontraction();
fAC = obj_function(AC, dimension);
feval++;
if(fAC <= fAR) {
// accept outside contraction point
memmove(&SIMPLEX(dimension, 0), AC, dimension * sizeof(double));
obj_function_results[indices[dimension]] = fAC;
} else {
// shrink
minimize();
//re-evaluate for next iteration
for (int i = 0; i < dimension + 1; i++) {
obj_function_results[indices[i]] = obj_function(&SIMPLEX(i,0), dimension);
feval++;
}
}
} else {
// do inside contraction
insidecontraction();
fAC = obj_function(AC, dimension);
feval++;
if(fAC < worst) {
// accept inside contraction point
memmove(&SIMPLEX(dimension, 0), AC, dimension * sizeof(double));
obj_function_results[indices[dimension]] = fAC;
} else {
// shrink
minimize();
//re-evaluate for next iteration
for (int i = 0; i < dimension + 1; i++) {
obj_function_results[indices[i]] = obj_function(&SIMPLEX(i,0), dimension);
feval++;
}
}
}
// Resort the indices, as they might be out of order now
sort_simplex(); //Sort the simplex
// Find the new best
best = obj_function_results[indices[0]];
// Find the new worst
worst = obj_function_results[indices[dimension]];
iter++;
}
std::cout << iter << " total iterations\n";
std::cout << "Total Function Evaluations: " << feval << std::endl;
return &SIMPLEX(0,0);
}
void NelderMead::centroid() {
for (int i = 0; i < dimension; i++) {
M[i] = 0.0;
}
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
M[j] += SIMPLEX(i, j);
}
}
for (int i = 0; i < dimension; i++) {
M[i] /= dimension;
}
}
void NelderMead::reflection() {
for (int i = 0; i < dimension; i++) {
AR[i] = (1 + rho) * M[i] - rho * SIMPLEX(dimension,i);
}
}
void NelderMead::expansion() {
for (int i = 0; i < dimension; i++) {
AE[i] = (1 + rho * xi) * M[i] - rho * xi * SIMPLEX(dimension,i);
}
}
void NelderMead::insidecontraction() {
for (int i = 0; i < dimension; i++) {
AC[i] = (1 - gam) * M[i] + gam * SIMPLEX(dimension,i);
}
}
void NelderMead::outsidecontraction() {
for (int i = 0; i < dimension; i++) {
AC[i] = (1 + rho * gam) * M[i] - rho * gam * SIMPLEX(dimension,i);
}
}
void NelderMead::minimize() {
for (int i = 1; i < dimension + 1; i++) {
daxpy(&SIMPLEX(i,0), sig, &SIMPLEX(i, 0), (1.0 - sig), &SIMPLEX(0, 0), dimension);
}
}
// result = scalar1*a + scalar2*b
void NelderMead::daxpy(double *result, double scalar1, double *a,
double scalar2, double *b, int length) {
for (int i = 0; i < length; i++) {
result[i] = scalar1 * a[i] + scalar2 * b[i];
}
}
// debugging purposes
void NelderMead::print_simplex() {
for (int i = 0; i < dimension + 1; i++) {
for (int j = 0; j < dimension; j++) {
std::cout << SIMPLEX(i,j) << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void NelderMead::sort_simplex() {
std::sort(indices, indices + dimension + 1, IndexSorter(obj_function_results));
}