Changeset 7445


Ignore:
Timestamp:
05/03/12 10:53:45 (2 years ago)
Author:
jdsiiro
Message:

TGPO:

  • always run the local searcher if the TGP model identifies an improving solution
  • parameterize the filter used for rejecting LS points from the TGP model.
  • fix termination criterion so that it is a 1-sided tolerance test
Location:
scolib/trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • scolib/trunk/src/libs/TGPO.cpp

    r7444 r7445  
    131131{ 
    132132   Data() 
    133       : X(NULL), Z(NULL) 
     133      : X(NULL), Z(NULL), found_improving_point(false) 
    134134   { 
    135135      const char *argv[]= {"REmbeddedAcro", "--silent", "--no-save"}; 
     
    154154   PROTECT_INDEX X_index; 
    155155   PROTECT_INDEX Z_index; 
     156 
     157   bool found_improving_point; 
    156158 
    157159   void resize_X(int nrow, int ncol) 
     
    198200     optimality_tolerance(utilib::Property::Bind<double>(0.05)), 
    199201     maximum_iterations(utilib::Property::Bind<int>(100)), 
     202     data_filter_threshold(utilib::Property::Bind<double>(1)), 
    200203     MCMC_BTE(utilib::Property::Bind<std::vector<int> >(), 
    201204              MCMC_BTE_setfcn,  
     
    270273       "[default: 100]", 
    271274       maximum_iterations); 
     275 
     276   properties.declare 
     277      ("data_filter_threshold", 
     278       "Scaling threshold for filtering data from the evaluation cache " 
     279       "[default: 1.0]", 
     280       data_filter_threshold); 
    272281 
    273282   MCMC_BTE.validate().connect(MCMC_BTE_validate); 
     
    305314   best_point.application_context = problem->get_handle(); 
    306315   best_point.set_core_cache(eval_mngr().evaluation_cache()); 
     316   best_point.onInsert.connect 
     317      ( boost::bind(&TGPO::cb_new_best_point, this, _1) ); 
    307318 
    308319   data = new Data; 
     320   data->found_improving_point = false; 
    309321 
    310322   /* R code goes here :) */ 
     
    316328        << best_point.begin()->second.asResponse(problem); 
    317329 
    318  
     330   double sense = problem->sense == colin::maximization ? -1.0 : 1.0; 
     331 
     332   size_t iter_since_ls = 0; 
    319333   size_t iter = 0; 
    320334   size_t max_iter = maximum_iterations; 
     
    323337      cerr << "*****************************************************" << endl; 
    324338      cerr << "Iteration " << iter << endl; 
     339      ++iter_since_ls; 
    325340 
    326341      tgp_results_t tgp = TGP(); 
     
    328343      cerr << "EGO: {" << tgp.ego_mean << ", " << tgp.ego_q1 << "}: "  
    329344           << tgp.ego_domain << endl; 
    330       cerr << "CB: {" << tgp.cb_mean << ", " << tgp.cb_q1 << "}: "  
    331            << tgp.cb_domain << endl; 
    332  
    333       double tol; 
    334       best_point.begin()->second.asResponse(problem).get(colin::f_info, tol); 
    335       tol =  std::fabs((tol - tgp.cb_q1) / tol); 
    336       if ( tol < optimality_tolerance.as<double>() ) 
     345      cerr << "CB: {" << tgp.conf_mean << ", " << tgp.conf_q1 << "}: "  
     346           << tgp.conf_domain << endl; 
     347 
     348      double fVal; 
     349      best_point.begin()->second.asResponse(problem).get(colin::f_info, fVal); 
     350      fVal = sense * ( fVal - tgp.conf_q1 ) / std::fabs(fVal); 
     351      if ( fVal <= optimality_tolerance.as<double>()  
     352           && ! data->found_improving_point ) 
    337353         break; 
    338354 
     355      processEvaluationCache(new_tgp_points); 
     356 
    339357      if ( ! local_optimizer.as<std::string>().empty() &&  
    340            iter % local_search_frequency.as<int>() == 0 ) 
     358           ( data->found_improving_point  
     359             || iter_since_ls >= local_search_frequency.as<int>() ) ) 
    341360      { 
    342361         colin::SolverHandle lo 
    343362            = colin::SolverMngr().create_solver(local_optimizer); 
    344363         lo->set_problem(problem); 
    345          lo->add_initial_point(tgp.ego_domain); 
     364         if ( data->found_improving_point ) 
     365            lo->add_initial_point(best_point.begin()->second.asResponse(problem)); 
     366         else 
     367            lo->add_initial_point(tgp.ego_domain); 
    346368         lo->reset(); 
    347369         lo->optimize(); 
    348370         cerr << "Local optimization: "  
    349371              << lo->get_final_points()->begin()->second.asResponse(problem); 
     372         processEvaluationCache(new_tgp_points); 
     373         data->found_improving_point = false; 
     374         iter_since_ls = 0; 
    350375      } 
    351  
    352       processEvaluationCache(new_tgp_points); 
    353376 
    354377      cerr << "Best: "  
     
    360383 
    361384   delete data; 
     385} 
     386 
     387void TGPO::cb_new_best_point(colin::Cache::cache_t::iterator) 
     388{ 
     389   data->found_improving_point = true; 
    362390} 
    363391 
     
    425453 
    426454   int num_var = problem->num_real_vars; 
     455    
     456   // NB: TGP assumes *minimization* (as does our using std::set for sorting) 
     457   bool invertSense = problem->sense == colin::maximization; 
    427458 
    428459   // Retrieve all points 
     
    438469      r.get_domain(domain, problem); 
    439470      r.get(colin::f_info, response); 
     471      if ( invertSense ) 
     472         response *= -1; 
    440473      new_points.insert(make_pair(response, domain)); 
    441474   } 
     
    443476 
    444477   // Filter points that are "unnecessarily close" 
     478   double threshold = data_filter_threshold.as<double>(); 
     479   threshold *= threshold; 
    445480   // 
    446481   // TBD: should we filter against points that are already in the TGP model? 
     
    451486   std::vector<double> scale = problem->real_upper_bounds; 
    452487   std::vector<double> tmp = problem->real_lower_bounds; 
    453    double pts_per_dim = 1.0 * get_tgp_num_samples() / num_var; 
     488   double pts_per_dim = get_tgp_num_samples() / num_var; 
    454489   for( size_t i = 0; i < num_var; ++i ) 
    455       scale[i] = (scale[i] - tmp[i]) / pts_per_dim / 2.0; 
     490      scale[i] = (scale[i] - tmp[i]) / pts_per_dim; 
    456491   int filtered = new_points.size(); 
    457492   while ( it != itEnd ) 
     
    467502            dist += tmp*tmp; 
    468503         } 
    469          if ( dist < 1.0 ) 
     504         if ( dist < threshold ) 
    470505            new_points.erase(test_it++); 
    471506         else 
     
    487522   double* x = REAL(data->X); 
    488523   double* z = REAL(data->Z); 
    489    // NB: TGP assumes *minimization* 
    490    bool invertSense = problem->sense == colin::maximization; 
    491524 
    492525   for ( it = new_points.begin() ; it != itEnd; ++it, ++r ) 
     
    495528      for ( size_t j = 0; j < num_var; ++j ) 
    496529         x[r + j*nr] = it->second[j]; 
    497       if ( invertSense ) 
    498          z[r] = -1.0 * it->first; 
    499       else 
    500          z[r] = it->first; 
     530      z[r] = it->first; 
    501531   } 
    502532 
     
    550580   size_t ego_idx = 0; 
    551581   size_t sd_idx = 0; 
    552    size_t cb_idx = 0; 
     582   size_t conf_idx = 0; 
    553583   for( size_t r = 1; r < samples; ++r ) 
    554584   { 
     
    557587      if ( sd[r] > sd[sd_idx] ) 
    558588         sd_idx = r; 
    559       if ( cb[r] < cb[cb_idx] ) 
    560          cb_idx = r; 
     589      if ( cb[r] < cb[conf_idx] ) 
     590         conf_idx = r; 
    561591   } 
    562592 
     
    565595   ans.ego_mean = REAL(getRListElement(tgp, "ZZ.mean"))[ego_idx]; 
    566596   ans.ego_q1 = cb[ego_idx]; 
    567    ans.cb_mean = REAL(getRListElement(tgp, "ZZ.mean"))[cb_idx]; 
    568    ans.cb_q1 = cb[cb_idx]; 
     597   ans.conf_mean = REAL(getRListElement(tgp, "ZZ.mean"))[conf_idx]; 
     598   ans.conf_q1 = cb[conf_idx]; 
    569599   std::vector<double> &ego_v = ans.ego_domain.set<std::vector<double> >(); 
    570600   ego_v.resize(num_var); 
    571601   for( size_t c = 0; c < num_var; ++c ) 
    572602      ego_v[c] = r_XX[ego_idx + c*samples]; 
    573    std::vector<double> &cb_v = ans.cb_domain.set<std::vector<double> >(); 
    574    cb_v.resize(num_var); 
     603   std::vector<double> &conf_v = ans.conf_domain.set<std::vector<double> >(); 
     604   conf_v.resize(num_var); 
    575605   for( size_t c = 0; c < num_var; ++c ) 
    576       cb_v[c] = r_XX[cb_idx + c*samples]; 
     606      conf_v[c] = r_XX[conf_idx + c*samples]; 
    577607 
    578608   if ( eval_ego_point ) 
     
    599629      domain.resize(num_var); 
    600630      for( size_t c = 0; c < num_var; ++c ) 
    601          domain[c] = r_XX[cb_idx + c*samples]; 
     631         domain[c] = r_XX[conf_idx + c*samples]; 
    602632      problem->AsyncEvalF(eval_mngr(), tmp); 
    603633      cerr << "eval(cb)  " << tmp << endl; 
  • scolib/trunk/src/scolib/TGPO.h

    r7443 r7445  
    6767   utilib::Property maximum_iterations; 
    6868 
     69   /// Scaled proxmity threshold for excluding data from the evaluation cache 
     70   /** Scaled proxmity threshold for excluding data from the evaluation cache 
     71    *    0 == no filter,  
     72    *    1 == force points to be roughly as far apart as the TGP sample grid 
     73    *    > TGP samples / Num variables: filter all but the best point 
     74    */ 
     75   utilib::Property data_filter_threshold; 
     76 
    6977private: 
    7078   struct Data; 
     
    7280 
    7381   struct tgp_results_t { 
     82      // Solution with best expected improvement 
    7483      utilib::Any ego_domain; 
    7584      double      ego_mean; 
    7685      double      ego_q1; 
    7786 
    78       utilib::Any cb_domain; 
    79       double      cb_mean; 
    80       double      cb_q1; 
     87      // Best solution based on lower confidence bound 
     88      utilib::Any conf_domain; 
     89      double      conf_mean; 
     90      double      conf_q1; 
    8191   }; 
     92 
     93   void cb_new_best_point(colin::Cache::cache_t::iterator); 
    8294 
    8395   int get_lhs_num_samples(); 
Note: See TracChangeset for help on using the changeset viewer.