Exemplo n.º 1
0
int main(int nargs, char *args[])
{    
  int n = 6;                   // Number of dimensions

  // Common configuration
  // See parameters.h for the available options
  // If we initialize the struct with the DEFAUL_PARAMS,
  // the we can optionally change only few of them 
  bopt_params par = initialize_parameters_to_default();

  par.kernel.hp_mean[0] = KERNEL_THETA;
  par.kernel.hp_std[0] = 1.0;
  par.kernel.n_hp = 1;
  par.mean.coef_mean[0] = 0.0;
  par.mean.coef_std[0] = MEAN_SIGMA;
  par.mean.n_coef = 1;
  par.noise = DEFAULT_NOISE;
  par.surr_name = "sStudentTProcessJef";
  par.n_iterations = 20;       // Number of iterations
  par.n_init_samples = 20;
  /*******************************************/
  
  size_t nPoints = 1000;

  randEngine mtRandom;
  matrixd xPoints(nPoints,n);
  vecOfvec xP;

  //Thanks to the quirks of Visual Studio, the following expression is invalid,
  //so we have to replace it by a literal.
  //WARNING: Make sure to update the size of the array if the number of points
  //or dimensions change.
#ifdef _MSC_VER
  double xPointsArray[6000];
#else
  const size_t nPinArr = n*nPoints;
  double xPointsArray[nPinArr];
#endif
  
  bayesopt::utils::lhs(xPoints,mtRandom);

  for(size_t i = 0; i<nPoints; ++i)
    {
      vectord point = row(xPoints,i);  
      xP.push_back(point);
      for(size_t j=0; j<n; ++j)
	{
	  xPointsArray[i*n+j] = point(j);	  
	}
    }
    

  
  // Run C++ interface
  std::cout << "Running C++ interface" << std::endl;
  ExampleDisc opt(xP,par);
  vectord result(n);
  opt.optimize(result);

  
  // Run C interface
  std::cout << "Running C interface" << std::endl;
  double x[128], fmin[128];
  bayes_optimization_disc(n, &testFunction, NULL, xPointsArray, nPoints,
			  x, fmin, par);

  // Find the optimal value
  size_t min = 0;
  double minvalue = opt.evaluateSample(row(xPoints,min));
for(size_t i = 1; i<nPoints; ++i)
{
  vectord point = row(xPoints,i);  
  if (opt.evaluateSample(point) < minvalue)
    {
      min = i;
      minvalue = opt.evaluateSample(row(xPoints,min));
      std::cout << i << "," << minvalue << std::endl;
    }
}

  std::cout << "Final result C++: " << result << std::endl;
  std::cout << "Final result C: (";
  for (int i = 0; i < n; i++ )
    std::cout << x[i] << ", ";
  std::cout << ")" << std::endl;
  std::cout << "Optimal: " << row(xPoints,min) << std::endl;
}
Exemplo n.º 2
0
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
  double *xptr;
  mxArray *xopt;
  const mxArray *func_name, *params;
  user_function_data udata;
  size_t nDim,nPoints;
  double* xset;
  bopt_params parameters;
  double fmin = 0.0;
  int error_code;
    
  /* Check correct number of parameters */
  CHECK0(nlhs != 2 || nrhs != 3, "wrong number of arguments");
    
  /* TODO: Change This */
  udata.neval = 0;
  udata.verbose = 0;
   
  /* First term is the function handle or name */
  func_name = prhs[0];

  if (mxIsChar(func_name))
    {
      CHECK0(mxGetString(func_name, udata.f, FLEN) == 0,
	     "error reading function name string (too long?)");
      udata.nrhs = 1;
      udata.xrhs = 0;
    }
#ifndef HAVE_OCTAVE
  else if (mxIsFunctionHandle(func_name))
    {
      udata.prhs[0] = (mxArray *)func_name;
      strcpy(udata.f, "feval");
      udata.nrhs = 2;
      udata.xrhs = 1;
      }
#endif
  else
    {
      mexErrMsgTxt("First term should be a function name "
		   "(Matlab/Octave) or function handle (Matlab)");
    }

  /* Second parameter. Set of values. */
  CHECK0(mxIsDouble(prhs[1]) && !mxIsComplex(prhs[1]) &&
	 mxGetNumberOfDimensions(prhs[1]) == 2,
	 "The set of values must be a 2D real matrix.");

  nDim = mxGetM(prhs[1]);
  nPoints = mxGetN(prhs[1]);
  xset = mxGetPr(prhs[1]);
  mexPrintf("Loading set of values. nDims=%i, nPoints=%i\n",nDim,nPoints);

  udata.prhs[udata.xrhs] = mxCreateDoubleMatrix(1, nDim, mxREAL);

  xopt = mxCreateDoubleMatrix(1, nDim, mxREAL);
  xptr = mxGetPr(xopt);
    

  /* Third term. Parameters  */
  if (nrhs != 2)
    {
      CHECK0(mxIsStruct(prhs[2]), "3rd element must be a struct");
      params = prhs[2];
    }
  else
    {
      params = mxCreateStructMatrix(1,1,0,NULL);
    }

  parameters = load_parameters(params);
  
  error_code = bayes_optimization_disc(nDim,user_function,&udata,xset,nPoints,
				       xptr,&fmin,parameters);

  mxDestroyArray(udata.prhs[udata.xrhs]);
  plhs[0] = xopt;
  if (nlhs > 1) 
    {
      plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
      *(mxGetPr(plhs[1])) = fmin;
    }
    
  if (nlhs > 2)
    {
      plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
      *(mxGetPr(plhs[2])) = (double)(error_code);
    }
    

}