BayesOpt
bayesoptmex.c
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 
23 #include "bayesoptextras.h"
24 
25 void mexFunction(int nlhs, mxArray *plhs[],
26  int nrhs, const mxArray *prhs[])
27 {
28  double *xptr;
29  mxArray *xopt;
30  const mxArray *func_name, *params;
31  user_function_data udata;
32  size_t nDim;
33  unsigned int ii;
34  bopt_params parameters;
35  double *ub, *lb; /* Upper and lower bound */
36  double fmin;
37  int error_code;
38 
39  /* Check correct number of parameters */
40  CHECK0(nlhs != 2 || nrhs != 3 || nrhs != 5,
41  "wrong number of arguments");
42 
43  /* TODO: Change This */
44  udata.neval = 0;
45  udata.verbose = 0;
46 
47  /* First term is the function handle or name */
48  func_name = prhs[0];
49 
50  if (mxIsChar(func_name))
51  {
52  CHECK0(mxGetString(func_name, udata.f, FLEN) == 0,
53  "error reading function name string (too long?)");
54  udata.nrhs = 1;
55  udata.xrhs = 0;
56  }
57 #ifndef HAVE_OCTAVE
58  else if (mxIsFunctionHandle(func_name))
59  {
60  udata.prhs[0] = (mxArray *)func_name;
61  strcpy(udata.f, "feval");
62  udata.nrhs = 2;
63  udata.xrhs = 1;
64  }
65 #endif
66  else
67  {
68  mexErrMsgTxt("First term should be a function name or function handle");
69  }
70 
71  /* Second parameter. nDim */
72  CHECK0(mxIsNumeric(prhs[1]) && !mxIsComplex(prhs[1])
73  && mxGetM(prhs[1]) * mxGetN(prhs[1]) == 1,
74  "nDim must be a scalar");
75  nDim = (unsigned int) mxGetScalar(prhs[1]);
76 
77  udata.prhs[udata.xrhs] = mxCreateDoubleMatrix(1, nDim, mxREAL);
78 
79  xopt = mxCreateDoubleMatrix(1, nDim, mxREAL);
80  xptr = mxGetPr(xopt);
81 
82  /* Third term. Parameters */
83  if (nrhs != 2)
84  {
85  CHECK0(mxIsStruct(prhs[2]), "3rd element must be a struct");
86  params = prhs[2];
87  }
88  else
89  {
90  params = mxCreateStructMatrix(1,1,0,NULL);
91  }
92  parameters = load_parameters(params);
93 
94  if(nrhs == 5)
95  {
96  /* Load bounds */
97  mexPrintf("Loading bounds...");
98  CHECK0(mxIsDouble(prhs[3]) && !mxIsComplex(prhs[3])
99  && (mxGetM(prhs[3]) == 1 || mxGetN(prhs[3]) == 1)
100  && (mxGetM(prhs[3]) == nDim || mxGetN(prhs[3]) == nDim),
101  "lowerBound must be real row or column vector");
102 
103  lb = mxGetPr(prhs[3]);
104 
105 
106  CHECK0(mxIsDouble(prhs[4]) && !mxIsComplex(prhs[4])
107  && (mxGetM(prhs[4]) == 1 || mxGetN(prhs[4]) == 1)
108  && (mxGetM(prhs[4]) == nDim || mxGetN(prhs[4]) == nDim),
109  "upperBound must be real row or column vector");
110 
111  ub = mxGetPr(prhs[4]);
112  mexPrintf("done. \n");
113  }
114  else
115  {
116  lb = (double*)(mxCalloc(nDim,sizeof(double)));
117  ub = (double*)(mxCalloc(nDim,sizeof(double)));
118 
119 
120 
121  for (ii = 0; ii < nDim; ++ii)
122  {
123  lb[ii] = 0.;
124  ub[ii] = 1.;
125  }
126  }
127 
128  error_code = bayes_optimization(nDim,user_function,&udata,lb,ub,xptr,
129  &fmin,parameters);
130 
131  if(nrhs != 5)
132  {
133  mxFree(lb);
134  mxFree(ub);
135  }
136 
137  mxDestroyArray(udata.prhs[udata.xrhs]);
138  plhs[0] = xopt;
139  if (nlhs > 1)
140  {
141  plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
142  *(mxGetPr(plhs[1])) = fmin;
143  }
144  if (nlhs > 2)
145  {
146  plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
147  *(mxGetPr(plhs[2])) = (double)(error_code);
148  }
149 
150 }
BAYESOPT_API int bayes_optimization(int nDim, eval_func f, void *f_data, const double *lb, const double *ub, double *x, double *minf, bopt_params parameters)
C wrapper for the Bayesian optimization algorithm.
Definition: bayesoptwpr.cpp:98
Configuration parameters.
Definition: parameters.h:83
Helper functions to Matlab/Octave wrappers.