Commits

Ruben Martinez-Cantin committed 3abac4f

String based criteria definition. Buggy.

Comments (0)

Files changed (10)

   ./src/parameters.cpp
   ./src/kernel_functors.cpp
   ./src/criteria_functors.cpp
-  ./src/metacriteria_functors.cpp
+  ./src/criteria_combined.cpp
   ./src/mean_functors.cpp
   ./src/gauss_distribution.cpp
   ./src/student_t_distribution.cpp
 TARGET_LINK_LIBRARIES(bayesopt
   ${EXT_LIBS} ${PYTHON_LIB} )
 
-#Naive test
+#Continuous test
 ADD_EXECUTABLE(bo_cont ./app/bo_cont.cpp)
 add_dependencies(bo_cont bayesopt)
 TARGET_LINK_LIBRARIES(bo_cont bayesopt)
 
 #Test for random number generator
 #ADD_EXECUTABLE(randtest ./app/testrand.cpp)
+
 #Test for parsers
-ADD_EXECUTABLE(parsetest ./utils/parser.cpp ./app/testparser.cpp)
+#ADD_EXECUTABLE(parsetest ./utils/parser.cpp ./app/testparser.cpp)
 
 
 INSTALL(FILES ./include/bayesoptcont.hpp 
   parameters.n_iterations = 300;
   parameters.theta[0] = 1.0;
   parameters.n_theta = 1;
-  parameters.c_name = C_GP_HEDGE;
+  parameters.c_s_name = "cHedge(cEI,cLCB,cExpReturn,cOptimisticSampling)";
 
   ExampleOneD opt(dim,parameters);
   vectord result(dim);

include/bayesoptbase.hpp

 #include "specialtypes.hpp"
 
 #include "nonparametricprocess.hpp"
-#include "metacriteria_functors.hpp"
+#include "criteria_functors.hpp"
 
 /**
  * Namespace of the library interface
 
   protected:
     boost::scoped_ptr<NonParametricProcess> mGP;  ///< Pointer to surrogate model
-    boost::scoped_ptr<MetaCriteria> mCrit;                ///< Metacriteria model
+    boost::scoped_ptr<Criteria> mCrit;                    ///< Metacriteria model
     bopt_params mParameters;                        ///< Configuration parameters
     size_t mDims;                                       ///< Number of dimensions
 
      * surrogate, etc.)
      */
     int __init__();
+    CriteriaFactory mCFactory;
   };
 
   /**@}*/

include/criteria_functors.hpp

 
 #include <boost/scoped_ptr.hpp>
 #include "nonparametricprocess.hpp"
-#include "prob_distribution.hpp"
 
 namespace bayesopt
 {
   class Criteria
   {
   public:
-    Criteria(NonParametricProcess *proc):  mProc(proc) {};
     virtual ~Criteria() {};
+    virtual int init(NonParametricProcess *proc) { return 0; };
+    virtual int init(NonParametricProcess *proc, 
+		     const std::vector<Criteria*>& list) { return 0; };
+
+    virtual bool requireComparison() = 0;
     virtual double operator()(const vectord &x) = 0;
-    virtual void resetAnneal() {};  // dummy function
 
-    static Criteria* create(criterium_name name,
-			    NonParametricProcess* proc);
+    //Dummy functions.
+    virtual void reset() { assert(false); };
+    virtual bool checkIfBest(vectord& xNext,
+			     std::string& name,
+			     int& error_code) { assert(false); };
   protected:
     NonParametricProcess *mProc;
   };
 
 
-  /**
-   * \brief Expected improvement criterion by Mockus \cite Mockus78
+  template <typename CriteriaType> Criteria * create_func()
+  {
+    return new CriteriaType();
+  }
+
+
+  /** 
+   * \brief Factory model for criterion functions
+   * This factory is based on the libgp library by Manuel Blum
+   *      https://bitbucket.org/mblum/libgp
+   * which follows the squeme of GPML by Rasmussen and Nickisch
+   *     http://www.gaussianprocess.org/gpml/code/matlab/doc/
    */
-  class ExpectedImprovement: public Criteria
+  class CriteriaFactory
   {
   public:
-    explicit ExpectedImprovement(NonParametricProcess *proc): 
-      Criteria(proc), mExp(1) {};
-    virtual ~ExpectedImprovement(){};
-    inline void setExponent(size_t exp) {mExp = exp;};
-    double operator()(const vectord &x)
-    { 
-      const double min = mProc->getValueAtMinimum();
-      return mProc->prediction(x)->negativeExpectedImprovement(min,mExp); 
-    };
+    CriteriaFactory ();
+    virtual ~CriteriaFactory () {};
+  
+    Criteria* create(criterium_name name, NonParametricProcess* proc);
+    Criteria* create(std::string name, NonParametricProcess* proc);
+    
   private:
-    size_t mExp;
-  };
-
-
-  /**
-   * \brief Lower (upper) confidence bound criterion.
-   */
-  class LowerConfidenceBound: public Criteria
-  {
-  public:
-    explicit LowerConfidenceBound(NonParametricProcess *proc):
-      Criteria(proc),mBeta(1) {};
-    virtual ~LowerConfidenceBound(){};
-    inline void setBeta(double beta) { mBeta = beta; };
-    double operator()( const vectord &x)
-    { 
-      return mProc->prediction(x)->lowerConfidenceBound(mBeta); 
-    };
-  private:
-    double mBeta;
-  };
-
-
-  /**
-   * \brief Probability of improvement criterion based on (Kushner).
-   */
-  class ProbabilityOfImprovement: public Criteria
-  {
-  public:
-    explicit ProbabilityOfImprovement(NonParametricProcess *proc): 
-      Criteria(proc), mEpsilon(0.01) {};
-    virtual ~ProbabilityOfImprovement(){};
-    inline void setEpsilon(double eps) {mEpsilon = eps;};
-    double operator()( const vectord &x)
-    { 
-      const double min = mProc->getValueAtMinimum();
-      return mProc->prediction(x)->negativeProbabilityOfImprovement(min,mEpsilon); 
-    };
-  private:
-    double mEpsilon;
-  };
-
-  /**
-   * \brief Greedy A-Optimality criterion. 
-   * Used for learning the function, not to minimize.
-   */
-  class GreedyAOptimality: public Criteria
-  {
-  public:
-    explicit GreedyAOptimality(NonParametricProcess *proc): Criteria(proc){};
-    virtual ~GreedyAOptimality(){};
-    double operator()( const vectord &x)
-    {
-      return mProc->prediction(x)->getStd();
-    };
-  };
-
-  /**
-   * \brief Expected return criterion.
-   */
-  class ExpectedReturn: public Criteria
-  {
-  public:
-    explicit ExpectedReturn(NonParametricProcess *proc): Criteria(proc){};
-    virtual ~ExpectedReturn(){};
-    double operator()( const vectord &x)
-    {
-      return mProc->prediction(x)->getMean();
-    };
-  };
-
-  /**
-   * \brief Optimistic sampling. A simple variation of Thompson sampling
-   * that picks only samples that are better than the best outcome so
-   * far.
-   */
-  class OptimisticSampling: public Criteria
-  {
-  public:
-    explicit OptimisticSampling(NonParametricProcess *proc): 
-      Criteria(proc), mtRandom(100u) {};
-    virtual ~OptimisticSampling(){};
-    double operator()( const vectord &x)
-    {
-      ProbabilityDistribution* d_ = mProc->prediction(x);
-      const double yStar = d_->sample_query(mtRandom);
-      const double yPred = d_->getMean();
-      return (std::min)(yPred,yStar);
-    };
-  private:
-    randEngine mtRandom;
-  };
-
-  /**
-   * \brief Thompson sampling. 
-   * Picks a random realization of the surrogate model.
-   */
-  class ThompsonSampling: public Criteria
-  {
-  public:
-    explicit ThompsonSampling(NonParametricProcess *proc): 
-      Criteria(proc), mtRandom(100u) {};
-    virtual ~ThompsonSampling(){};
-    double operator()( const vectord &x)
-    {
-      ProbabilityDistribution* d_ = mProc->prediction(x);
-      return d_->sample_query(mtRandom);
-    };
-  private:
-    randEngine mtRandom;
-  };
-
-
-  /**
-   * \brief Expected improvement criterion using Schonlau annealing.
-   */
-  class AnnealedExpectedImprovement: public Criteria
-  {
-  public:
-    explicit AnnealedExpectedImprovement(NonParametricProcess *proc):
-      Criteria(proc){ resetAnneal(); };
-    virtual ~AnnealedExpectedImprovement(){};
-    inline void setExponent(size_t exp) {mExp = exp;};
-    void resetAnneal() { nCalls = 0; mExp = 10;};
-    double operator()( const vectord &x)
-    {
-      ++nCalls;
-      if (nCalls % 10)
-	mExp = ceil(mExp/2.0);
-
-      ProbabilityDistribution* d_ = mProc->prediction(x);
-      const double min = mProc->getValueAtMinimum();
-      return d_->negativeExpectedImprovement(min,mExp); 
-    };
-
-  private:
-    size_t mExp;
-    unsigned int nCalls;
-  };
-
-
-  /**
-   * \brief Lower (upper) confidence bound using Srinivas annealing
-   */
-  class AnnealedLowerConfindenceBound: public Criteria
-  {
-  public:
-    explicit AnnealedLowerConfindenceBound(NonParametricProcess *proc):
-      Criteria(proc){ resetAnneal(); };
-    virtual ~AnnealedLowerConfindenceBound(){};
-    inline void setBetaCoef(double betac) { mCoef = betac; };
-    void resetAnneal() { nCalls = 0; mCoef = 5.0;};
-    double operator()( const vectord &x)
-    {
-      ++nCalls;
-      size_t nDims = x.size();
-    
-      double beta = sqrt(2*log(static_cast<double>(nCalls*nCalls))*(nDims+1) 
-			 + log(static_cast<double>(nDims))*nDims*mCoef);
-
-      ProbabilityDistribution* d_ = mProc->prediction(x);
-      return d_->lowerConfidenceBound(beta); 
-    };
-  private:
-    double mCoef;
-    unsigned int nCalls;
-  };
-
-  /**
-   * \brief Distance in input space. Can be combined with other
-   * critera to trade off large changes in input space.
-   */
-  class InputDistance: public Criteria
-  {
-  public:
-    explicit InputDistance(NonParametricProcess *proc): 
-      Criteria(proc) {};
-    virtual ~InputDistance(){};
-    double operator()(const vectord &x)
-    { 
-      vectord x2(x.size());
-      mProc->getLastSample(x2);
-      return norm_2(x-x2);
-    };
+    typedef Criteria* (*create_func_definition)();
+    std::map<std::string , CriteriaFactory::create_func_definition> registry;
   };
 
 

include/metacriteria_functors.hpp

     virtual double operator()(const vectord &x) = 0;
 
     //Dummy functions.
-    virtual int initializeSearch() { assert(false); };
+    virtual int initialize() { assert(false); };
     virtual bool checkIfBest(vectord& xNext,
 			     std::string& name,
 			     int& error_code) { assert(false); };
     bool requireComparison(){ return true; };
     double operator()(const vectord &x) { return (*mCurrentCriterium)(x); };
 
-    int initializeSearch();
+    int initialize();
     bool checkIfBest(vectord& best, std::string& name,int& error_code);
 
   protected:

include/parameters.h

     surrogate_name s_name;       /**< Name of the surrogate function */
     kernel_name k_name;          /**< Name of the kernel funct. -DEPRECATED-*/
     char* k_s_name;              /**< Name of the kernel function */
-    criterium_name c_name;       /**< Name of the criterion */
+    criterium_name c_name;       /**< Name of the criterion -DEPRECATED-*/
+    char* c_s_name;              /**< Name of the criterion */
     mean_name m_name;            /**< Name of the mean funct. -DEPRECATED-*/
     char* m_s_name;              /**< Name of the mean function */
 

src/bayesoptbase.cpp

 	return -1;
       } 
 
-    mCrit.reset(MetaCriteria::create(mParameters.c_name,mGP.get()));
+    mCrit.reset(mCFactory.create(mParameters.c_s_name,mGP.get()));
     if (mCrit == NULL)       
       {
 	FILE_LOG(logERROR) << "Error in criterium"; 
 	bool check = false;
 	std::string name;
 
-	mCrit->initializeSearch();
+	mCrit->reset();
 	while (!check)
 	  {
 	    findOptimal(Xnext);

src/criteria_functors.cpp

    along with BayesOpt.  If not, see <http://www.gnu.org/licenses/>.
 ------------------------------------------------------------------------
 */
+#include "log.hpp"
+#include "parser.hpp"
 #include "criteria_functors.hpp"
-#include "log.hpp"
+#include "criteria_atomic.hpp"
+#include "criteria_combined.hpp"
 
 namespace bayesopt
 {
-  
-  Criteria* Criteria::create(criterium_name name,
-			     NonParametricProcess* proc)
+  CriteriaFactory::CriteriaFactory()
   {
+    registry["cEI"] = & create_func<ExpectedImprovement>;
+    registry["cEIa"] = & create_func<AnnealedExpectedImprovement>;
+    registry["cLCB"] = & create_func<LowerConfidenceBound>;
+    registry["cLCBa"] = & create_func<AnnealedLowerConfindenceBound>;
+    registry["cPOI"] = & create_func<ProbabilityOfImprovement>;
+    registry["cAopt"] = & create_func<GreedyAOptimality>;
+    registry["cExpReturn"] = & create_func<ExpectedReturn>;
+    registry["cOptimisticSampling"] = & create_func<OptimisticSampling>;
+    registry["cThompsonSampling"] = & create_func<ThompsonSampling>;
+    registry["cDistance"] = & create_func<InputDistance>;
+
+    registry["cSum"] = & create_func<SumCriteria>;
+    registry["cProd"] = & create_func<ProdCriteria>;
+    registry["cHedge"] = & create_func<GP_Hedge>;
+    registry["cHedgeRandom"] = & create_func<GP_Hedge_Random>;
+  }
+
+  /// Factory method for criterion functions.
+  Criteria* CriteriaFactory::create(criterium_name name,
+				    NonParametricProcess* proc)
+  {
+    Criteria* c_ptr;
+    std::vector<Criteria*> list;
     switch (name)
       {
-      case C_EI:     return new ExpectedImprovement(proc);
-      case C_EI_A:   return new AnnealedExpectedImprovement(proc);
-      case C_LCB:    return new LowerConfidenceBound(proc);
-      case C_LCB_A:  return new AnnealedLowerConfindenceBound(proc);
-      case C_POI:    return new ProbabilityOfImprovement(proc);
-      case C_GREEDY_A_OPTIMALITY: return new GreedyAOptimality(proc);
-      case C_EXPECTED_RETURN:     return new ExpectedReturn(proc);
-      case C_OPTIMISTIC_SAMPLING: return new OptimisticSampling(proc);
+      case C_EI:     c_ptr = new ExpectedImprovement(); break;
+      case C_EI_A:   c_ptr = new AnnealedExpectedImprovement(); break;
+      case C_LCB:    c_ptr = new LowerConfidenceBound(); break;
+      case C_LCB_A:  c_ptr = new AnnealedLowerConfindenceBound(); break;
+      case C_POI:    c_ptr = new ProbabilityOfImprovement(); break;
+      case C_GREEDY_A_OPTIMALITY: c_ptr = new GreedyAOptimality(); break;
+      case C_EXPECTED_RETURN: c_ptr = new ExpectedReturn(); break;
+      case C_OPTIMISTIC_SAMPLING: c_ptr = new OptimisticSampling(); break;
+      case C_GP_HEDGE: c_ptr = new GP_Hedge(); break;
+      case C_GP_HEDGE_RANDOM: c_ptr = new GP_Hedge_Random(); break;
       default:
 	FILE_LOG(logERROR) << "Error in criterium";
 	return NULL;
       }
+    if ((name = C_GP_HEDGE) || (name = C_GP_HEDGE_RANDOM))
+      {
+	for(size_t i = 0; i < N_ALGORITHMS_IN_GP_HEDGE; ++i)
+	  {
+	    list.push_back(create(ALGORITHMS_IN_GP_HEDGE[i],proc)); 
+	  }
+	c_ptr->init(proc,list);
+      }
+    else
+      {
+	c_ptr->init(proc);
+      }
+    return c_ptr;
   };
 
 
-  // Criteria* CriteriaFactory::create(std::string name,
-  // 				    NonParametricProcess* proc)
-  // {
-  //   Criteria *cFunc;
-  //   size_t pos = name.find("(");         // position of "(" in str
-  //   if (pos = std::string::npos)              // Atomic criterion
-  //     {
-  // 	std::map<std::string,CriteriaFactory::create_func_definition>::iterator it = registry.find(name);
-  //     }
+  /** 
+   * \brief Factory model for criterion functions
+   * This function is based on the libgp library by Manuel Blum
+   *      https://bitbucket.org/mblum/libgp
+   * which follows the squeme of GPML by Rasmussen and Nickisch
+   *     http://www.gaussianprocess.org/gpml/code/matlab/doc/
+   * @param name string with the criteria structure
+   * @param pointer to surrogate model
+   * @return criteria pointer
+   */
+  Criteria* CriteriaFactory::create(std::string name,
+				    NonParametricProcess* proc)
+  {
+    Criteria *cFunc;
+    std::string os;
+    std::vector<std::string> osc;
+    utils::parseExpresion(name,os,osc);
 
-  //   if (it == registry.end()) 
-  //     {
-  // 	FILE_LOG(logERROR) << "Error: Fatal error while parsing "
-  // 			   << "criteria function: " << os.str() 
-  // 			   << " not found" << std::endl;
-  // 	return NULL;
-  //     } 
-  //   kFunc = registry.find(name)->second();
-    
-  // std::string str3 = str.substr (pos);     // get from "live" to the end
-  // }
+    std::map<std::string,CriteriaFactory::create_func_definition>::iterator it = registry.find(os);
+    if (it == registry.end()) 
+      {
+	FILE_LOG(logERROR) << "Error: Fatal error while parsing "
+			   << "kernel function: " << os 
+			   << " not found" << std::endl;
+	return NULL;
+      } 
+    cFunc = it->second();
+    if (osc.size() == 0) 
+      {
+	cFunc->init(proc);
+      } 
+    else 
+      {
+	std::vector<Criteria*> list;
+	for(size_t i = 0; i < osc.size(); ++i)
+	  {
+	    list.push_back(create(osc[i],proc)); 
+	  }
+	cFunc->init(proc,list);
+      }
+    return cFunc;
+  };
+
 
 } //namespace bayesopt

src/metacriteria_functors.cpp

     cumprob_(zvectord(N_ALGORITHMS_IN_GP_HEDGE))
   {};
 
-  int GP_Hedge::initializeSearch()
+  int GP_Hedge::initialize()
   {
     mIndex = 0;
     mCurrentCriterium = mCriteriaList[mIndex];

src/parameters.cpp

 char DEF_LOG_FILE[] = "bayesopt.log";
 char DEF_KERNEL[] = "kMaternISO3";
 char DEF_MEAN[] = "mOne";
+char DEF_CRITERIA[] = "cEI";
 
 static const bopt_params DEFAULT_PARAMS = {
   DEFAULT_ITERATIONS, MAX_INNER_EVALUATIONS, DEFAULT_SAMPLES, 
   PRIOR_ALPHA, PRIOR_BETA, PRIOR_DELTA_SQ,
   DEFAULT_NOISE,
   S_GAUSSIAN_PROCESS,
-  K_MATERN_ISO3,
-  DEF_KERNEL,
-  C_EI, M_ONE, DEF_MEAN 
+  K_MATERN_ISO3, DEF_KERNEL,
+  C_EI, DEF_CRITERIA,
+  M_ONE, DEF_MEAN 
 };