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