Source

BayesOpt / wrappers / bayesoptwpr.cpp

#include "log.hpp"
#include "ublas_extra.hpp"
#include "bayesoptwpr.h"
#include "bayesoptcont.hpp"      
#include "bayesoptdisc.hpp"      

/**
 * \brief Version of ContinuousModel for the C wrapper
 */
class CContinuousModel: public bayesopt::ContinuousModel 
{
 public:

  CContinuousModel(size_t dim, bopt_params params):
    ContinuousModel(dim,params)  {}; 

  virtual ~CContinuousModel(){};

  double evaluateSample( const vectord &Xi ) 
  {
    int n = static_cast<int>(Xi.size());
    return  mF(n,&Xi[0],NULL,mOtherData);
  };

  void set_eval_funct(eval_func f)
  {  mF = f; }


  void save_other_data(void* other_data)
  {  mOtherData = other_data; }
 
protected:
  void* mOtherData;
  eval_func mF;
};

/**
 * \brief Version of DiscreteModel for the C wrapper
 */
class CDiscreteModel: public bayesopt::DiscreteModel
{
 public:

  CDiscreteModel(const vecOfvec &validX, bopt_params params):
    DiscreteModel(validX, params)
  {}; 


  double evaluateSample( const vectord &Xi ) 
  {
    int n = static_cast<int>(Xi.size());
    return  mF(n,&Xi[0],NULL,mOtherData);
  };

  void set_eval_funct(eval_func f)
  {  mF = f; }


  void save_other_data(void* other_data)
  {  mOtherData = other_data; }
 
protected:
  void* mOtherData;
  eval_func mF;
};



int bayes_optimization(int nDim, eval_func f, void* f_data,
		       const double *lb, const double *ub,
		       double *x, double *minf, bopt_params parameters)
{
  vectord result(nDim);

  vectord lowerBound = bayesopt::utils::array2vector(lb,nDim); 
  vectord upperBound = bayesopt::utils::array2vector(ub,nDim); 

  CContinuousModel optimizer(nDim, parameters);

  optimizer.set_eval_funct(f);
  optimizer.save_other_data(f_data);
  optimizer.setBoundingBox(lowerBound,upperBound);
  optimizer.optimize(result);

  std::copy(result.begin(), result.end(), x);

  *minf = optimizer.getMinimumValue();

  return 1; /* everything ok*/
};

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)
{
  vectord result(nDim);
  vectord input(nDim);
  vecOfvec xSet;

  for(size_t i = 0; i<n_points;++i)
    {
      for(int j = 0; j<nDim; ++j)
	{
	 input(j) = valid_x[i*nDim+j]; 
	}
      xSet.push_back(input);
    }

  if(parameters.n_init_samples > n_points)
    {
      parameters.n_init_samples = n_points;
      parameters.n_iterations = 0;
    }

  CDiscreteModel optimizer(xSet,parameters);

  optimizer.set_eval_funct(f);
  optimizer.save_other_data(f_data);
  optimizer.optimize(result);

  std::copy(result.begin(), result.end(), x);

  *minf = optimizer.getMinimumValue();

  return 1; /* everything ok*/
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.