BayesOpt
bayesoptcatmex.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 *cat_d;
36  int *cat_i;
37  double fmin;
38  int error_code;
39 
40  /* Check correct number of parameters */
41  CHECK0(nlhs != 2 || nrhs != 3,
42  "wrong number of arguments");
43 
44  /* TODO: Change This */
45  udata.neval = 0;
46  udata.verbose = 0;
47 
48  /* First term is the function handle or name */
49  func_name = prhs[0];
50 
51  if (mxIsChar(func_name))
52  {
53  CHECK0(mxGetString(func_name, udata.f, FLEN) == 0,
54  "error reading function name string (too long?)");
55  udata.nrhs = 1;
56  udata.xrhs = 0;
57  }
58 #ifndef HAVE_OCTAVE
59  else if (mxIsFunctionHandle(func_name))
60  {
61  udata.prhs[0] = (mxArray *)func_name;
62  strcpy(udata.f, "feval");
63  udata.nrhs = 2;
64  udata.xrhs = 1;
65  }
66 #endif
67  else
68  {
69  mexErrMsgTxt("First term should be a function name or function handle");
70  }
71 
72  /* Second parameter. Categories */
73  CHECK0(mxIsDouble(prhs[1]) && !mxIsComplex(prhs[1])
74  && ( (mxGetM(prhs[1]) == 1) && (mxGetN(prhs[1]) == nDim)
75  || (mxGetN(prhs[1]) == 1) && (mxGetM(prhs[1]) == nDim)),
76  "categories must be real row or column vector");
77 
78  cat_d = mxGetPr(prhs[3]);
79 
80  nDim = (unsigned int) mxGetM(prhs[1]) * mxGetN(prhs[1]);
81 
82  cat_i = (int*)(mxCalloc(nDim,sizeof(int)));
83  for (ii = 0; ii < nDim; ++ii)
84  {
85  cat_i[ii] = (int)(cat_d[ii]+0.5);
86  }
87 
88  udata.prhs[udata.xrhs] = mxCreateDoubleMatrix(1, nDim, mxREAL);
89 
90  xopt = mxCreateDoubleMatrix(1, nDim, mxREAL);
91  xptr = mxGetPr(xopt);
92 
93  /* Third term. Parameters */
94  if (nrhs != 2)
95  {
96  CHECK0(mxIsStruct(prhs[2]), "3rd element must be a struct");
97  params = prhs[2];
98  }
99  else
100  {
101  params = mxCreateStructMatrix(1,1,0,NULL);
102  }
103  parameters = load_parameters(params);
104 
105  error_code = bayes_optimization_categorical(nDim,user_function,&udata,cat_i,xptr,
106  &fmin,parameters);
107 
108  mxFree(cat_i);
109  mxDestroyArray(udata.prhs[udata.xrhs]);
110  plhs[0] = xopt;
111  if (nlhs > 1)
112  {
113  plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
114  *(mxGetPr(plhs[1])) = fmin;
115  }
116 
117  if (nlhs > 2)
118  {
119  plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
120  *(mxGetPr(plhs[2])) = (double)(error_code);
121  }
122 
123 }
Configuration parameters.
Definition: parameters.h:83
Helper functions to Matlab/Octave wrappers.
BAYESOPT_API int bayes_optimization_categorical(int nDim, eval_func f, void *f_data, int *categories, double *x, double *minf, bopt_params parameters)
C wrapper for the Bayesian optimization algorithm.