Commits

Ruben Martinez-Cantin committed 835e724

Adding camelback and comparison tests

  • Participants
  • Parent commits 3249e95

Comments (0)

Files changed (4)

doxygen/branin.png

Added
New image

examples/CMakeLists.txt

 add_dependencies(bo_camelback bayesopt)
 TARGET_LINK_LIBRARIES(bo_camelback bayesopt)
 
+ADD_EXECUTABLE(bo_compare ./bo_compare.cpp )
+add_dependencies(bo_compare bayesopt)
+TARGET_LINK_LIBRARIES(bo_compare bayesopt)
+
+
 enable_language(C)  #For OpenGL and bo_display demo
 
 #Display test

examples/bo_camelback.cpp

+/*
+-------------------------------------------------------------------------
+   This file is part of BayesOpt, an efficient C++ library for 
+   Bayesian optimization.
+
+   Copyright (C) 2011-2013 Ruben Martinez-Cantin <rmcantin@unizar.es>
+ 
+   BayesOpt is free software: you can redistribute it and/or modify it 
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   BayesOpt is distributed in the hope that it will be useful, but 
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with BayesOpt.  If not, see <http://www.gnu.org/licenses/>.
+------------------------------------------------------------------------
+*/
+
+#include <ctime>
+#include <fstream>
+#include "testfunctions.hpp"
+
+int main(int nargs, char *args[])
+{
+  bopt_params par = initialize_parameters_to_default();
+  par.n_iterations = 190;
+  par.noise = 1e-10;
+  par.random_seed = 0;
+  par.verbose_level = 1;
+
+  
+  ExampleCamelback camel(par);
+
+  vectord lb(2); lb(0) = -2; lb(1) = -1;
+  vectord ub(2); ub(0) =  2; ub(1) = 1;
+
+  camel.setBoundingBox(lb,ub);
+
+  std::ofstream timelog;
+  timelog.open("time_camel.log");
+  std::clock_t curr_t;
+  std::clock_t prev_t = clock();
+
+  camel.initializeOptimization();
+      
+  for (size_t ii = 0; ii < par.n_iterations; ++ii)
+    {      
+      camel.stepOptimization();
+
+      curr_t = clock();
+      timelog << ii << ","
+	      << static_cast<double>(curr_t - prev_t) / CLOCKS_PER_SEC 
+	      << std::endl;
+      prev_t = curr_t;
+      }
+
+  timelog.close();
+
+  vectord result = camel.getFinalResult();
+  std::cout << "Result: " << result << "->" 
+	    << camel.evaluateSample(result) << std::endl;
+  camel.printOptimal();
+
+  return 0;
+}

examples/bo_compare.cpp

+/*
+-------------------------------------------------------------------------
+   This file is part of BayesOpt, an efficient C++ library for 
+   Bayesian optimization.
+
+   Copyright (C) 2011-2013 Ruben Martinez-Cantin <rmcantin@unizar.es>
+ 
+   BayesOpt is free software: you can redistribute it and/or modify it 
+   under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   BayesOpt is distributed in the hope that it will be useful, but 
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with BayesOpt.  If not, see <http://www.gnu.org/licenses/>.
+------------------------------------------------------------------------
+*/
+
+#include <ctime>
+#include <fstream>
+#include "testfunctions.hpp"
+
+int main(int nargs, char *args[])
+{
+  bopt_params par = initialize_parameters_to_default();
+  par.n_iterations = 190;
+  par.verbose_level = 0;
+  par.noise = 1e-10;
+
+  std::ofstream log;
+  std::clock_t start_t;
+
+
+  /* Branin */
+  log.open("branin.log");
+
+  for (size_t ii = 0; ii < 10; ++ii)
+    {
+      par.random_seed = ii;
+      par.n_iterations = 190;
+      BraninNormalized branin(par);
+      vectord result(2);
+
+      start_t = clock();
+      branin.initializeOptimization();
+      
+      for (size_t jj = 0; jj < par.n_iterations; ++jj)
+	{      
+	  branin.stepOptimization();
+	  if (jj == 50)
+	    {
+	      result = branin.getFinalResult();	      
+	      log << branin.evaluateSample(result) << ", ";
+	    }
+	}
+      result = branin.getFinalResult();	      
+      log << branin.evaluateSample(result) << ", ";
+      
+      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
+	  << std::endl;
+      }
+
+  log.close();
+
+
+  /* Camel */
+  log.open("camel.log");
+
+  for (size_t ii = 0; ii < 10; ++ii)
+    {
+      par.random_seed = ii;
+      par.n_iterations = 90;
+      ExampleCamelback camel(par);
+      vectord result(2);
+
+      vectord lb(2); lb(0) = -2; lb(1) = -1;
+      vectord ub(2); ub(0) =  2; ub(1) = 1;
+
+      camel.setBoundingBox(lb,ub);
+
+      start_t = clock();
+      camel.initializeOptimization();
+      
+      for (size_t jj = 0; jj < par.n_iterations; ++jj)
+	{      
+	  camel.stepOptimization();
+	  if (jj == 50)
+	    {
+	      result = camel.getFinalResult();	      
+	      log << camel.evaluateSample(result) << ", ";
+	    }
+	}
+      result = camel.getFinalResult();	      
+      log << camel.evaluateSample(result) << ", ";
+      
+      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
+	  << std::endl;
+      }
+
+  log.close();
+
+
+  /* Hart */
+  log.open("hart.log");
+
+  for (size_t ii = 0; ii < 10; ++ii)
+    {
+      par.random_seed = ii;
+      par.n_iterations = 190;
+      ExampleHartmann6 hart(par);
+      vectord result(6);
+
+      start_t = clock();
+      hart.initializeOptimization();
+      
+      for (size_t jj = 0; jj < par.n_iterations; ++jj)
+	{      
+	  hart.stepOptimization();
+	  if (jj == 50)
+	    {
+	      result = hart.getFinalResult();	      
+	      log << hart.evaluateSample(result) << ", ";
+	    }
+	}
+      result = hart.getFinalResult();	      
+      log << hart.evaluateSample(result) << ", ";
+      
+      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
+	  << std::endl;
+      }
+
+  log.close();
+
+
+  /***********************************************************************/
+  par.n_init_samples = 2;
+  par.n_iter_relearn = 1;
+  
+  par.l_type = L_MCMC;
+  par.sc_type = SC_MAP;
+
+
+  /* Branin */
+  log.open("branin_mcmc.log");
+
+  for (size_t ii = 0; ii < 10; ++ii)
+    {
+      par.random_seed = ii;
+      par.n_iterations = 190;
+      BraninNormalized branin(par);
+      vectord result(2);
+
+      start_t = clock();
+      branin.initializeOptimization();
+      
+      for (size_t jj = 0; jj < par.n_iterations; ++jj)
+	{      
+	  branin.stepOptimization();
+	  if (jj == 50)
+	    {
+	      result = branin.getFinalResult();	      
+	      log << branin.evaluateSample(result) << ", ";
+	    }
+	}
+      result = branin.getFinalResult();	      
+      log << branin.evaluateSample(result) << ", ";
+      
+      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
+	  << std::endl;
+      }
+
+  log.close();
+
+
+  /* Camel */
+  log.open("camel_mcmc.log");
+
+  for (size_t ii = 0; ii < 10; ++ii)
+    {
+      par.random_seed = ii;
+      par.n_iterations = 90;
+      ExampleCamelback camel(par);
+      vectord result(2);
+
+      vectord lb(2); lb(0) = -2; lb(1) = -1;
+      vectord ub(2); ub(0) =  2; ub(1) = 1;
+
+      camel.setBoundingBox(lb,ub);
+
+      start_t = clock();
+      camel.initializeOptimization();
+      
+      for (size_t jj = 0; jj < par.n_iterations; ++jj)
+	{      
+	  camel.stepOptimization();
+	  if (jj == 50)
+	    {
+	      result = camel.getFinalResult();	      
+	      log << camel.evaluateSample(result) << ", ";
+	    }
+	}
+      result = camel.getFinalResult();	      
+      log << camel.evaluateSample(result) << ", ";
+      
+      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
+	  << std::endl;
+      }
+
+  log.close();
+
+
+  /* Hart */
+  log.open("hart_mcmc.log");
+
+  for (size_t ii = 0; ii < 10; ++ii)
+    {
+      par.random_seed = ii;
+      par.n_iterations = 190;
+      ExampleHartmann6 hart(par);
+      vectord result(6);
+
+      start_t = clock();
+      hart.initializeOptimization();
+      
+      for (size_t jj = 0; jj < par.n_iterations; ++jj)
+	{      
+	  hart.stepOptimization();
+	  if (jj == 50)
+	    {
+	      result = hart.getFinalResult();	      
+	      log << hart.evaluateSample(result) << ", ";
+	    }
+	}
+      result = hart.getFinalResult();	      
+      log << hart.evaluateSample(result) << ", ";
+      
+      log << static_cast<double>(clock() - start_t) / static_cast<double>(CLOCKS_PER_SEC)
+	  << std::endl;
+      }
+
+  log.close();
+
+
+  return 0;
+}