Source

Bayesian-Optimization / examples / bo_compare.cpp

Full commit
/*
-------------------------------------------------------------------------
   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 = 1;
  par.noise = 1e-10;
  par.force_jump = 30;

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