-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecommendSample.cpp
375 lines (325 loc) · 10.5 KB
/
recommendSample.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
*
* [xnew invK] = recommendSample(x,y,N,invK,lb,ub,nDim,ktype,ksigma,eps,msrSigma,acqFuncType)
*/
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "mex.h"
#include "nlopt.h"
#define MAX_DATA 1000
#define min(x1,x2) (((x1)<(x2))? (x1): (x2))
#define max(x1,x2) (((x1)>(x2))? (x1): (x2))
#define MATH_PI 3.14159
#define TINY 1.0e-20
double distv(double *x1, double *x2);
void print_vector(int n, double* v);
void print_matrix(int m, int n, double** v);
int nDim;
double OptTime;
double gValmax = 0.0;
typedef struct{
int ktype;
double nu;
double scale;
double kvar;
double rqalpha;
} kernelOptions;
void kernelFunction(double *kvec, double *xnew, double **x, int N, kernelOptions koptions);
typedef struct{
int criteria;
double msrSigma2;
double *lb;
double *ub;
double eps;
int if_global;
}bayesOptions;
typedef struct{
double *x;
double *y;
double *invK;
double *kvec;
int N;
double ymax;
int onlykernel;
kernelOptions koptions;
bayesOptions boptions;
}acqFuncData;
double acqFunc(unsigned n, const double *x, double *grad, void *my_func_data);
void recommendSample(double *maxf, double *xnew, double *x, double *y, double ymax, int N, double *kvec, double *invK, kernelOptions koptions, bayesOptions boptions);
/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/*checking number of arguments*/
if (nrhs != 19) {
mexErrMsgIdAndTxt("MyToolbox:recommendSample:nrhs", "19 inputs required.");
}
/* variable declarations here */
double *x = mxGetPr(prhs[0]);
double *y = mxGetPr(prhs[1]);
double ymax = mxGetScalar(prhs[2]);
int N = mxGetScalar(prhs[3]);
double *kvec = mxGetPr(prhs[4]);
double *invK = mxGetPr(prhs[5]);
double *lb = mxGetPr(prhs[6]);
double *ub = mxGetPr(prhs[7]);
nDim = mxGetScalar(prhs[8]);
OptTime = mxGetScalar(prhs[9]);
int ktype = mxGetScalar(prhs[10]);
double ksigma = mxGetScalar(prhs[11]);
double kvar = mxGetScalar(prhs[12]);
double rqalpha = mxGetScalar(prhs[13]);
double eps = mxGetScalar(prhs[14]);
double msrSigma2 = mxGetScalar(prhs[15]);
int acqFuncType = mxGetScalar(prhs[16]);
double *xinit = mxGetPr(prhs[17]);
int if_global = mxGetScalar(prhs[18]);
/* options here*/
kernelOptions koptions;
koptions.ktype = ktype;
koptions.nu = 0;
koptions.scale = ksigma;
koptions.kvar = kvar;
koptions.rqalpha = rqalpha;
bayesOptions boptions;
boptions.criteria = acqFuncType;
boptions.msrSigma2 = msrSigma2;
boptions.lb = lb;
boptions.ub = ub;
boptions.eps = eps;
boptions.if_global = if_global;
/* code here */
nlhs = 2;
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
double *maxf = mxGetPr(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(1, nDim, mxREAL);
double *xnew = mxGetPr(plhs[1]);
for (int ii = 0; ii < nDim; ii++)
xnew[ii] = xinit[ii];
recommendSample(maxf, xnew, x, y, ymax, N, kvec, invK, koptions, boptions);
mxSetPr(plhs[0], maxf);
mxSetPr(plhs[1], xnew);
return;
}
double acqFunc(unsigned n, const double *x, double *grad, void *my_func_data)
{
acqFuncData *param = static_cast<acqFuncData*>(my_func_data);
int N = param->N;
double *xmat = param->x;
if (param->koptions.ktype == 1) //SE kernel
{
for (int ii = 0; ii < N; ii++)
{
double dist = 0;
for (int jj = 0; jj < nDim; jj++)
dist = dist + (x[jj] - xmat[ii + jj*N]) * (x[jj] - xmat[ii + jj*N]);
param->kvec[ii] = exp(-0.5*dist / (param->koptions.scale*param->koptions.scale));
}
}
else
{
printf("Undefined kernel type\n");
exit(0);
}
double gVal;
double sm1 = 0;
double mux = 0;
for (int ii = 0; ii < N; ii++)
{
for (int jj = 0; jj < N; jj++)
{
sm1 = sm1 + param->kvec[ii] * param->invK[ii + jj*N] * param->kvec[jj];
mux = mux + param->kvec[ii] * param->invK[ii + jj*N] * param->y[jj];
}
}
double kTinvKk = sm1;
double sigmax = sqrt((1 + param->boptions.msrSigma2) - min(sm1, 1));
double ythr = param->ymax;
//used by EI
double z = (mux - ythr) / sigmax;
double Phiz = 0.5 + 0.5 * erf(z / 1.4142);
double phiz = (exp(-1 * z*z / 2) / 2.506628);
double nu = 1.0;// used by UCB
//Hardcoded for EI and no grad
// if (sigmax > 0)
// {
// gVal = (mux - ythr)*Phiz + sigmax*phiz;
// }
// else
// {
// gVal = 0;
// }
switch (param->boptions.criteria)
{
case 1:
//EI
if (sigmax > 0)
{
gVal = (mux - ythr)*Phiz + sigmax*phiz;
}
else
{
gVal = 0;
}
break;
case 2:
//PI
if (sigmax > 0)
{
gVal = 1 - 0.5 * (1 + erf((mux - ythr) / (sigmax * 1.4142)));
}
else
{
gVal = 0;
}
break;
case 3:
//UCB
if (sigmax > 0)
{
gVal = mux + (sqrt(nu*param->boptions.eps)*sigmax);
}
else
{
gVal = 0;
}
break;
default:
//EI
if (sigmax > 0)
{
gVal = (mux - ythr)*Phiz + sigmax*phiz;
}
else
{
gVal = 0;
}
break;
}
if (grad)
{
if (param->boptions.criteria==1)
{
if (sigmax > 0)
{
for (int dd = 0; dd < nDim; dd++)
{
double muxdx = 0.0, sigmaxdx = 0.0;
for (int ii = 0; ii < N; ii++)
{
double temp1 = 0.0, temp2 = 0.0;
for (int jj = 0; jj < N; jj++)
{
temp1 = temp1 + param->invK[ii + jj*N] * param->y[jj];
temp2 = temp2 + param->invK[ii + jj*N] * param->kvec[jj];
}
muxdx = muxdx - (x[dd] - xmat[ii + dd*N])* param->kvec[ii] * temp1;
sigmaxdx = sigmaxdx - (x[dd] - xmat[ii + dd*N])* param->kvec[ii] * temp2;
}
sigmaxdx = -1*sigmaxdx/sigmax;
muxdx = (muxdx-z*sigmaxdx)/sigmax;
grad[dd] = ((z*Phiz+phiz)*sigmaxdx + sigmax*Phiz*muxdx)/ (param->koptions.scale*param->koptions.scale);
}
/*printf("\ngrad: ");
* print_vector(nDim, grad);
* printf("\nx: ");
* print_vector(nDim, (double*)x);*/
}
else
{
for (int dd = 0; dd < nDim; dd++)
grad[dd] = 0.0;
}
}
}
return gVal;
}
void recommendSample(double *maxf, double *xnew, double *x, double *y, double ymax, int N, double *kvec, double *invK, kernelOptions koptions, bayesOptions boptions)
{
acqFuncData param;
param.x = x;
param.y = y;
param.invK = invK;
param.N = N;
param.kvec = kvec;
param.koptions = koptions;
param.boptions = boptions;
param.ymax = ymax;
param.onlykernel = 0;
void *myacqFuncData = static_cast<void*>(¶m);
nlopt_opt opt;
int info;
if (boptions.if_global)
{
opt = nlopt_create(NLOPT_GN_ORIG_DIRECT, nDim);// NLOPT_GN_ESCH, NLOPT_GN_ISRES, NLOPT_GN_CRS2_LM, NLOPT_GN_ORIG_DIRECT_L
nlopt_set_maxtime(opt, OptTime*nDim);
nlopt_set_lower_bounds(opt, boptions.lb);
nlopt_set_upper_bounds(opt, boptions.ub);
nlopt_set_max_objective(opt, acqFunc, myacqFuncData);
nlopt_set_xtol_rel(opt, 1e-4);
info = nlopt_optimize(opt, xnew, maxf);
if (info < 0 || info == 6)
printf("DIRECT returned with error: %d\n", info);
/*if (info < 0 || info == 6) {
* if (info < 0 || info == 6) {
* printf("Direct failed (%d): trying Nelder-Mead...\n", info);
* opt = nlopt_create(NLOPT_LN_NELDERMEAD, nDim);
* nlopt_set_maxtime(opt, OptTime*nDim);
* nlopt_set_lower_bounds(opt, boptions.lb);
* nlopt_set_upper_bounds(opt, boptions.ub);
* nlopt_set_max_objective(opt, acqFunc, myacqFuncData);
* nlopt_set_xtol_rel(opt, 1e-4);
* info = nlopt_optimize(opt, xnew, maxf);
* printf("Nelder-Mead completed (%d)\n", info);
* }
* }*/
}
else
{
opt = nlopt_create(NLOPT_LD_TNEWTON , nDim);// NLOPT_LD_LBFGS, NLOPT_LN_BOBYQA, NLOPT_LD_MMA
nlopt_set_maxtime(opt, OptTime*nDim);
nlopt_set_lower_bounds(opt, boptions.lb);
nlopt_set_upper_bounds(opt, boptions.ub);
nlopt_set_max_objective(opt, acqFunc, myacqFuncData);
nlopt_set_xtol_rel(opt, 1e-4);
nlopt_remove_inequality_constraints(opt);
nlopt_set_vector_storage(opt, 0);
info = nlopt_optimize(opt, xnew, maxf);
/*if (info < 0 || info == 6)
* printf("BOBYQA returned with error: %d\n", info);*/
}
}
double distv(double *x1, double *x2)
{
double sum = 0;
for (int ii = 0; ii < nDim; ii++)
sum = sum + (x1[ii] - x2[ii]) * (x1[ii] - x2[ii]);
return sum;
}
void print_vector(int n, double* v) {
int j;
char *ch;
ch = new char[50];
for (j = 0; j < n; j++)
{
sprintf(ch, " %3.3g, ", v[j]);
mexPrintf(ch);
}
mexPrintf("\n");
}
void print_matrix(int m, int n, double** v) {
int i, j;
char *ch;
ch = new char[100];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
sprintf(ch, " %3.3g", v[i][j]);
mexPrintf(ch);
}
mexPrintf("\n");
}
}