Exemple #1
0
void checkInputs(const mxArray *prhs[], int nrhs)
{    
    size_t Mlb, Mub, Mx0;
    
    if(nrhs < 5)
        mexErrMsgTxt("You must supply at least 5 arguments to lbfsgb!\nlbfgsb(fun,grad,lb,ub,x0)");
       
    //Check Types
    if(!mxIsFunctionHandle(prhs[0]) && !mxIsChar(prhs[0]))
        mexErrMsgTxt("fun must be a function handle or function name!");
    if(!mxIsFunctionHandle(prhs[1]) && !mxIsChar(prhs[1]))
        mexErrMsgTxt("grad must be a function handle or function name!");
    if(!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxIsEmpty(prhs[2]))
        mexErrMsgTxt("lb must be a real double column vector!");
    if(!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxIsEmpty(prhs[3]))
        mexErrMsgTxt("ub must be a real double column vector!");
    if(!mxIsDouble(prhs[4]) || mxIsComplex(prhs[4]) || mxIsEmpty(prhs[4]))
        mexErrMsgTxt("x0 must be a real double column vector!");
    if((nrhs > 5) && !mxIsStruct(prhs[5]))
        mexErrMsgTxt("Options must be supplied as a structure!");
    
    //Check Sizes
    Mlb = mxGetNumberOfElements(prhs[2]);
    Mub = mxGetNumberOfElements(prhs[3]);
    Mx0 = mxGetNumberOfElements(prhs[4]);
    
    if(Mlb != Mub)
        mexErrMsgTxt("The bound vectors are not the same length!");
    if(Mlb != Mx0)
        mexErrMsgTxt("x0 is not the same length as the bounds!");
}
void checkInputs(const mxArray *prhs[], int nrhs)
{
    size_t ndec;

    if(nrhs < 4)
        mexErrMsgTxt("You must supply at least 4 arguments to nl2sol!\n\nnl2sol(fun,grad,x0,ydata)\n");

    //Check Types
    if(!mxIsFunctionHandle(prhs[0]) && !mxIsChar(pFUN))
        mexErrMsgTxt("fun must be a function handle or function name!");
    if(!mxIsEmpty(pGRAD) && (!mxIsFunctionHandle(pGRAD) && !mxIsChar(pGRAD)))
        mexErrMsgTxt("grad must be a function handle or function name!");
    if(!mxIsDouble(pX0) || mxIsComplex(pX0) || mxIsEmpty(pX0))
        mexErrMsgTxt("x0 must be a real double column vector!");
    if(!mxIsDouble(pYDATA) || mxIsComplex(pYDATA) || mxIsEmpty(pYDATA))
        mexErrMsgTxt("ydata must be a real double column vector!");

    ndec = mxGetNumberOfElements(pX0);

    //Check for lb + ub
    if(nrhs == eUB)
        mexErrMsgTxt("You must supply both lb and ub for bounded problems!");

    //Check Bounds
    if(nrhs > eUB)
    {
        if(mxIsEmpty(pLB) ^ mxIsEmpty(pUB))
            mexErrMsgTxt("You must supply both lb and ub for bounded problems");
        if(!mxIsEmpty(pLB)) {
            if(!mxIsDouble(pLB) || mxIsComplex(pLB))
                mexErrMsgTxt("lb must be a real double column vector!");
            if(mxGetNumberOfElements(pLB) != ndec)
                mexErrMsgTxt("The number of elements in lb does not match x0");
        }
        if(!mxIsEmpty(pUB)) {
            if(!mxIsDouble(pUB) || mxIsComplex(pUB))
                mexErrMsgTxt("ub must be a real double column vector!");
            if(mxGetNumberOfElements(pUB) != ndec)
                mexErrMsgTxt("The number of elements in ub does not match x0");
        }
    }

    //Check Options
    if(nrhs > eOPTS) {
        if(!mxIsStruct(pOPTS))
            mexErrMsgTxt("The specified options must be a structure!");
    }

}
Exemple #3
0
void checkInputs(const mxArray *prhs[], int nrhs, int *lincon)
{    
    size_t Mx0;
    
    if(nrhs < 4)
        mexErrMsgTxt("You must supply at least 4 arguments to pswarm!\n\npswarm(fun,x0,lb,ub)\n");
       
    //Check Types
    if(!mxIsFunctionHandle(prhs[0]) && !mxIsChar(prhs[0]))
        mexErrMsgTxt("fun must be a function handle or function name!");
    if(!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || mxIsEmpty(prhs[1]))
        mexErrMsgTxt("x0 must be a real double column vector!");

    //Get ndec
    Mx0 = mxGetNumberOfElements(prhs[2]);
    
    //Check Bounds
    if(!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]))
        mexErrMsgTxt("lb must be a real double column vector!");
    if(!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]))
        mexErrMsgTxt("ub must be a real double column vector!");
    //Check Empty
    if(mxIsEmpty(prhs[2]))
        mexErrMsgTxt("lb is empty! PSwarm requires finite bounds");
    if(mxIsEmpty(prhs[3]))
        mexErrMsgTxt("ub is empty! PSwarm requires finite bounds");
    //Check Sizes
    if(Mx0 != mxGetNumberOfElements(prhs[2]))
        mexErrMsgTxt("lb is not the same length as x0! Ensure they are both Column Vectors");
    if(Mx0 != mxGetNumberOfElements(prhs[3]))
        mexErrMsgTxt("ub is not the same length as x0! Ensure they are both Column Vectors");
    
    //Check Linear Inequality
    if(nrhs > 4) {
        if(nrhs == 5)
            mexErrMsgTxt("You must supply both A & b!");
        if(!mxIsEmpty(prhs[4]) && (!mxIsDouble(prhs[4]) || mxIsComplex(prhs[4]) || mxIsSparse(prhs[4])))
            mexErrMsgTxt("A must be a full, real double matrix!");
        if(!mxIsEmpty(prhs[5]) && (!mxIsDouble(prhs[5]) || mxIsComplex(prhs[5])))
            mexErrMsgTxt("b must be a real double column vector!");
        //Check Sizes
        if(!mxIsEmpty(prhs[4])) {
            if(mxGetN(prhs[4]) != Mx0)
                mexErrMsgTxt("A is not the right size!");
            if(mxGetM(prhs[4]) != mxGetM(prhs[5]))
                mexErrMsgTxt("A & b sizes do not correspond");
            //Confirm no linear equations
            *lincon = (int)mxGetM(prhs[5]);
        }
    }
    
    //Check Options
    if(nrhs > 6) {
        if(!mxIsStruct(prhs[6]))
            mexErrMsgTxt("The specified options must be a structure!");
    }
}
static mxArray *struct_funcval(const mxArray *s, const char *name)
{
     mxArray *val = mxGetField(s, 0, name);
     if (val) {
	  CHECK0(mxIsChar(val) || mxIsFunctionHandle(val),
		 "opt function field is not a function handle/name");
	  return val;
     }
     return NULL;
}
Exemple #5
0
void checkInputs(const mxArray *prhs[], int nrhs)
{    
    if(nrhs < 3)
        mexErrMsgTxt("You must supply at least 3 arguments to m1qn3!\n\nm1qn3(fun,grad,x0)\n");
       
    //Check Types
    if(!mxIsFunctionHandle(prhs[0]) && !mxIsChar(pFUN))
        mexErrMsgTxt("fun must be a function handle or function name!");
    if(!mxIsFunctionHandle(pGRAD) && !mxIsChar(pGRAD))
        mexErrMsgTxt("grad must be a function handle or function name!");
    if(!mxIsDouble(pX0) || mxIsComplex(pX0) || mxIsEmpty(pX0))
        mexErrMsgTxt("x0 must be a real double column vector!");

    //Check Options
    if(nrhs > eOPTS) {
        if(!mxIsStruct(pOPTS))
            mexErrMsgTxt("The specified options must be a structure!");
    }

}
Exemple #6
0
void checkInputs(const mxArray *prhs[], int nrhs)
{    
    if(nrhs < 4)
        mexErrMsgTxt("You must supply at least 4 arguments to lmder!\n\nlmder(fun,grad,x0,ydata)\n");
       
    //Check Types
    if(!mxIsFunctionHandle(prhs[0]) && !mxIsChar(prhs[0]))
        mexErrMsgTxt("fun must be a function handle or function name!");
    if(!mxIsEmpty(prhs[1]) && (!mxIsFunctionHandle(prhs[1]) && !mxIsChar(prhs[1])))
        mexErrMsgTxt("grad must be a function handle or function name!");
    if(!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxIsEmpty(prhs[2]))
        mexErrMsgTxt("x0 must be a real double column vector!");
    if(!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxIsEmpty(prhs[3]))
        mexErrMsgTxt("ydata must be a real double column vector!");

    //Check Options
    if(nrhs > 4) {
        if(!mxIsStruct(prhs[4]))
            mexErrMsgTxt("The specified options must be a structure!");
    }

}
Exemple #7
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;
  unsigned int ii;
  bopt_params parameters;
  double *cat_d;
  int *cat_i;
  double fmin;
  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 or function handle");
    }

  /* Second parameter. Categories */
  CHECK0(mxIsDouble(prhs[1]) && !mxIsComplex(prhs[1])
	 && (  (mxGetM(prhs[1]) == 1) && (mxGetN(prhs[1]) == nDim)   
	    || (mxGetN(prhs[1]) == 1) && (mxGetM(prhs[1]) == nDim)),
	 "categories must be real row or column vector");

  cat_d = mxGetPr(prhs[3]);

  nDim = (unsigned int) mxGetM(prhs[1]) * mxGetN(prhs[1]);

  cat_i = (int*)(mxCalloc(nDim,sizeof(int)));
  for (ii = 0; ii < nDim; ++ii) 
    {
      cat_i[ii] = (int)(cat_d[ii]+0.5);    
    }

  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_categorical(nDim,user_function,&udata,cat_i,xptr,
					      &fmin,parameters);

  mxFree(cat_i); 
  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);
    }
    
}
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
     unsigned n;
     double *x, *x0, opt_f;
     nlopt_result ret;
     mxArray *x_mx, *mx;
     user_function_data d, dpre, *dfc = NULL, *dh = NULL;
     nlopt_opt opt = NULL;

     CHECK(nrhs == 2 && nlhs <= 3, "wrong number of arguments");

     /* options = prhs[0] */
     CHECK(mxIsStruct(prhs[0]), "opt must be a struct");
     
     /* x0 = prhs[1] */
     CHECK(mxIsDouble(prhs[1]) && !mxIsComplex(prhs[1])
	   && (mxGetM(prhs[1]) == 1 || mxGetN(prhs[1]) == 1),
	   "x must be real row or column vector");
     n = mxGetM(prhs[1]) * mxGetN(prhs[1]),
     x0 = mxGetPr(prhs[1]);

     CHECK(opt = make_opt(prhs[0], n), "error initializing nlopt options");

     d.neval = 0;
     d.verbose = (int) struct_val_default(prhs[0], "verbose", 0);
     d.opt = opt;

     /* function f = prhs[1] */
     mx = struct_funcval(prhs[0], "min_objective");
     if (!mx) mx = struct_funcval(prhs[0], "max_objective");
     CHECK(mx, "either opt.min_objective or opt.max_objective must exist");
     if (mxIsChar(mx)) {
	  CHECK(mxGetString(mx, d.f, FLEN) == 0,
		"error reading function name string (too long?)");
	  d.nrhs = 1;
	  d.xrhs = 0;
     }
     else {
	  d.prhs[0] = mx;
	  strcpy(d.f, "feval");
	  d.nrhs = 2;
	  d.xrhs = 1;
     }
     d.prhs[d.xrhs] = mxCreateDoubleMatrix(1, n, mxREAL);

     if ((mx = struct_funcval(prhs[0], "pre"))) {
	  CHECK(mxIsChar(mx) || mxIsFunctionHandle(mx),
		"pre must contain function handles or function names");
	  if (mxIsChar(mx)) {
	       CHECK(mxGetString(mx, dpre.f, FLEN) == 0,
                     "error reading function name string (too long?)");
	       dpre.nrhs = 2;
	       dpre.xrhs = 0;
	  }
	  else {
	       dpre.prhs[0] = mx;
	       strcpy(dpre.f, "feval");
	       dpre.nrhs = 3;
	       dpre.xrhs = 1;
	  }
	  dpre.verbose = d.verbose > 2;
	  dpre.opt = opt;
	  dpre.neval = 0;
	  dpre.prhs[dpre.xrhs] = d.prhs[d.xrhs];
	  dpre.prhs[d.xrhs+1] = mxCreateDoubleMatrix(1, n, mxREAL);
	  d.dpre = &dpre;

	  if (struct_funcval(prhs[0], "min_objective"))
	       nlopt_set_precond_min_objective(opt, user_function,user_pre,&d);
	  else
	       nlopt_set_precond_max_objective(opt, user_function,user_pre,&d);
     }
     else {
	  dpre.nrhs = 0;
	  if (struct_funcval(prhs[0], "min_objective"))
	       nlopt_set_min_objective(opt, user_function, &d);
	  else
	       nlopt_set_max_objective(opt, user_function, &d);
     }

     if ((mx = mxGetField(prhs[0], 0, "fc"))) {
	  int j, m;
	  double *fc_tol;
	  
	  CHECK(mxIsCell(mx), "fc must be a Cell array");
	  m = mxGetM(mx) * mxGetN(mx);;
	  dfc = (user_function_data *) mxCalloc(m, sizeof(user_function_data));
	  fc_tol = struct_arrval(prhs[0], "fc_tol", m, NULL);

	  for (j = 0; j < m; ++j) {
	       mxArray *fc = mxGetCell(mx, j);
	       CHECK(mxIsChar(fc) || mxIsFunctionHandle(fc),
		     "fc must contain function handles or function names");
	       if (mxIsChar(fc)) {
		    CHECK(mxGetString(fc, dfc[j].f, FLEN) == 0,
		     "error reading function name string (too long?)");
		    dfc[j].nrhs = 1;
		    dfc[j].xrhs = 0;
	       }
	       else {
		    dfc[j].prhs[0] = fc;
		    strcpy(dfc[j].f, "feval");
		    dfc[j].nrhs = 2;
		    dfc[j].xrhs = 1;
	       }
	       dfc[j].verbose = d.verbose > 1;
	       dfc[j].opt = opt;
	       dfc[j].neval = 0;
	       dfc[j].prhs[dfc[j].xrhs] = d.prhs[d.xrhs];
	       CHECK(nlopt_add_inequality_constraint(opt, user_function,
						     dfc + j,
						     fc_tol ? fc_tol[j] : 0)
		     > 0, "nlopt error adding inequality constraint");
	  }
     }


     if ((mx = mxGetField(prhs[0], 0, "h"))) {
	  int j, m;
	  double *h_tol;
	  
	  CHECK(mxIsCell(mx), "h must be a Cell array");
	  m = mxGetM(mx) * mxGetN(mx);;
	  dh = (user_function_data *) mxCalloc(m, sizeof(user_function_data));
	  h_tol = struct_arrval(prhs[0], "h_tol", m, NULL);

	  for (j = 0; j < m; ++j) {
	       mxArray *h = mxGetCell(mx, j);
	       CHECK(mxIsChar(h) || mxIsFunctionHandle(h),
		     "h must contain function handles or function names");
	       if (mxIsChar(h)) {
		    CHECK(mxGetString(h, dh[j].f, FLEN) == 0,
		     "error reading function name string (too long?)");
		    dh[j].nrhs = 1;
		    dh[j].xrhs = 0;
	       }
	       else {
		    dh[j].prhs[0] = h;
		    strcpy(dh[j].f, "feval");
		    dh[j].nrhs = 2;
		    dh[j].xrhs = 1;
	       }
	       dh[j].verbose = d.verbose > 1;
	       dh[j].opt = opt;
	       dh[j].neval = 0;
	       dh[j].prhs[dh[j].xrhs] = d.prhs[d.xrhs];
	       CHECK(nlopt_add_equality_constraint(opt, user_function,
						     dh + j,
						   h_tol ? h_tol[j] : 0)
		     > 0, "nlopt error adding equality constraint");
	  }
     }


     x_mx = mxCreateDoubleMatrix(mxGetM(prhs[1]), mxGetN(prhs[1]), mxREAL);
     x = mxGetPr(x_mx);
     memcpy(x, x0, sizeof(double) * n);

     ret = nlopt_optimize(opt, x, &opt_f);

     mxFree(dh);
     mxFree(dfc);
     mxDestroyArray(d.prhs[d.xrhs]);
     if (dpre.nrhs > 0) mxDestroyArray(dpre.prhs[d.xrhs+1]);
     nlopt_destroy(opt);

     plhs[0] = x_mx;
     if (nlhs > 1) {
	  plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
	  *(mxGetPr(plhs[1])) = opt_f;
     }
     if (nlhs > 2) {
	  plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
	  *(mxGetPr(plhs[2])) = (int) ret;
     }
}
void checkInputs(const mxArray *prhs[], int nrhs, int *conMode)
{    
    size_t Mx0;
    
    if(nrhs < 4)
        mexErrMsgTxt("You must supply at least 4 arguments to levmar!\n\nlevmar(fun,grad,x0,ydata) or\nlevmar(fun,grad,x0,ydata,lb,ub,A,b,Aeq,beq,opts)");
       
    //Check Types
    if(!mxIsFunctionHandle(prhs[0]) && !mxIsChar(prhs[0]))
        mexErrMsgTxt("fun must be a function handle or function name!");
    if(!mxIsEmpty(prhs[1]) && (!mxIsFunctionHandle(prhs[1]) && !mxIsChar(prhs[1])))
        mexErrMsgTxt("grad must be a function handle or function name!");
    if(!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxIsEmpty(prhs[2]))
        mexErrMsgTxt("x0 must be a real double column vector!");
    if(!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxIsEmpty(prhs[3]))
        mexErrMsgTxt("ydata must be a real double column vector!");
    
    //Get ndec
    Mx0 = mxGetNumberOfElements(prhs[2]);
    
    //Check Bounds
    if(nrhs > 4) {
        if(!mxIsDouble(prhs[4]) || mxIsComplex(prhs[4]))
            mexErrMsgTxt("lb must be a real double column vector!");
        if(nrhs > 5 && (!mxIsDouble(prhs[5]) || mxIsComplex(prhs[5])))
            mexErrMsgTxt("ub must be a real double column vector!");
        //Check Sizes
        if(!mxIsEmpty(prhs[4]) && (Mx0 != mxGetNumberOfElements(prhs[4])))
            mexErrMsgTxt("lb is not the same length as x0! Ensure they are both Column Vectors");
        if(nrhs > 5 && !mxIsEmpty(prhs[5]) && (Mx0 != mxGetNumberOfElements(prhs[5])))
            mexErrMsgTxt("ub is not the same length as x0! Ensure they are both Column Vectors");
        //Confirm Bounds
        if(!mxIsEmpty(prhs[4]) || (nrhs > 5 && !mxIsEmpty(prhs[5])))
            *conMode |= 1;
    }
    
    //Check Linear Inequality
    if(nrhs > 6) {
        if(nrhs == 7)
            mexErrMsgTxt("You must supply both A & b!");
        if(!mxIsEmpty(prhs[6]) && (!mxIsDouble(prhs[6]) || mxIsComplex(prhs[6]) || mxIsSparse(prhs[6])))
            mexErrMsgTxt("A must be a full, real double matrix!");
        if(!mxIsEmpty(prhs[7]) && (!mxIsDouble(prhs[7]) || mxIsComplex(prhs[7])))
            mexErrMsgTxt("b must be a real double column vector!");
        //Check Sizes
        if(!mxIsEmpty(prhs[6])) {
            if(mxGetM(prhs[6]) != Mx0)
                mexErrMsgTxt("A is not the right size! Remember to transpose it for levmar.");
            if(mxGetN(prhs[6]) != mxGetM(prhs[7]))
                mexErrMsgTxt("A & b sizes do not correspond");
            //Confirm Lin Eq
            *conMode |= 2;  
        }
    }    
    
    //Check Linear Equality
    if(nrhs > 8) {
        if(nrhs == 9)
            mexErrMsgTxt("You must supply both Aeq & beq!");
        if(!mxIsEmpty(prhs[8]) && (!mxIsDouble(prhs[8]) || mxIsComplex(prhs[8]) || mxIsSparse(prhs[8])))
            mexErrMsgTxt("Aeq must be a full, real double matrix!");
        if(!mxIsEmpty(prhs[9]) && (!mxIsDouble(prhs[9]) || mxIsComplex(prhs[9])))
            mexErrMsgTxt("beq must be a real double column vector!");
        //Check Sizes
        if(!mxIsEmpty(prhs[8])) {
            if(mxGetM(prhs[8]) != Mx0)
                mexErrMsgTxt("Aeq is not the right size! Remember to transpose it for levmar.");
            if(mxGetN(prhs[8]) != mxGetM(prhs[9]))
                mexErrMsgTxt("Aeq & beq sizes do not correspond");
            //Confirm Lin Eq
            *conMode |= 4;    
        }
    }
    
    //Check Options
    if(nrhs > 10) {
        if(!mxIsStruct(prhs[10]))
            mexErrMsgTxt("The specified options must be a structure!");
    }

}
Exemple #10
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;
  unsigned int ii;
  bopt_params parameters;
  double *ub, *lb;    /* Upper and lower bound */
  double fmin;
  int error_code;
     
  /* Check correct number of parameters */
  CHECK0(nlhs != 2 || nrhs != 3 || nrhs != 5, 
	 "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 or function handle");
    }

  /* Second parameter. nDim */
  CHECK0(mxIsNumeric(prhs[1]) && !mxIsComplex(prhs[1]) 
	 && mxGetM(prhs[1]) * mxGetN(prhs[1]) == 1,
	 "nDim must be a scalar");
  nDim = (unsigned int) mxGetScalar(prhs[1]);

  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);

  if(nrhs == 5)
    {
      /* Load bounds */
      mexPrintf("Loading bounds...");
      CHECK0(mxIsDouble(prhs[3]) && !mxIsComplex(prhs[3])
	     && (mxGetM(prhs[3]) == 1    || mxGetN(prhs[3]) == 1)
	     && (mxGetM(prhs[3]) == nDim || mxGetN(prhs[3]) == nDim),
	     "lowerBound must be real row or column vector");

      lb = mxGetPr(prhs[3]);


      CHECK0(mxIsDouble(prhs[4]) && !mxIsComplex(prhs[4])
	     && (mxGetM(prhs[4]) == 1    || mxGetN(prhs[4]) == 1)
	     && (mxGetM(prhs[4]) == nDim || mxGetN(prhs[4]) == nDim),
	     "upperBound must be real row or column vector");

      ub = mxGetPr(prhs[4]);
      mexPrintf("done. \n");
    }
  else
    {
      lb = (double*)(mxCalloc(nDim,sizeof(double)));
      ub = (double*)(mxCalloc(nDim,sizeof(double)));
	 

      
      for (ii = 0; ii < nDim; ++ii) 
	{
	  lb[ii] = 0.;    
	  ub[ii] = 1.;
	}
    }

  error_code = bayes_optimization(nDim,user_function,&udata,lb,ub,xptr,
				  &fmin,parameters);

  if(nrhs != 5)
    {
      mxFree(lb); 
      mxFree(ub);
    }

  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);
    }

}
Exemple #11
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);
    }
    

}