示例#1
0
void LH_values_for_var_i ( int     ind ,
				int     p   ,
				NOMAD::Point & x, const NOMAD::Point &lb, const NOMAD::Point  & ub, 
				const vector<NOMAD::bb_input_type> &bbin) {

		NOMAD::Random_Pickup rp(p);
		int    i;
		NOMAD::Double v;
		double UB = ub[ind].value();
		double LB = lb[ind].value();

		for ( i = 0 ; i < p ; ++i ) {
				double w = (UB - LB)/p;
				v = LB + ( i + rand()/NOMAD::D_INT_MAX ) * w;
				if( bbin[ind]!= NOMAD::CONTINUOUS) 
				{
						x[rp.pickup()] =(int) v.value();
				}
				else{
						x[rp.pickup()] = v;
				}
		}
}
示例#2
0
/*-----------------------------------------------------------*/
void NOMAD::LH_Search::values_for_var_i ( int                          p           ,
                                         const NOMAD::Double        & delta     ,
                                         const NOMAD::Double        & delta_max ,
                                         const NOMAD::bb_input_type & bbit        ,
                                         const NOMAD::Double        & lb          ,
                                         const NOMAD::Double        & ub          ,
                                         NOMAD::Point               & x      ) const
{
    // categorical variables have already been treated as fixed variables:
    if ( bbit == NOMAD::CATEGORICAL )
        return;
    
    int                  i;
    NOMAD::Double        v;
    NOMAD::Random_Pickup rp (p);
    bool                 rounding = ( bbit != NOMAD::CONTINUOUS );
    bool                 lb_def   = lb.is_defined();
    bool                 ub_def   = ub.is_defined();
    double               w        = ( ( lb_def && ub_def ) ?
                                     ub.value()-lb.value() : 1.0 ) / p;
    // main loop:
    for ( i = 0 ; i < p ; ++i )
    {

        // both bounds exist:
        if ( lb_def && ub_def )
            v = lb + ( i + NOMAD::RNG::rand()/NOMAD::D_INT_MAX ) * w;
        // one of the bounds does not exist:
        else
        {
            
            // lb exists, and ub not: mapping [0;1] --> [lb;+INF[
            if ( lb_def )
                v = lb + 10 * delta_max * sqrt ( - log ( NOMAD::DEFAULT_EPSILON +
                                                          ( i + NOMAD::RNG::rand()/NOMAD::D_INT_MAX ) * w ) );
            
            // lb does not exist:
            else
            {
                
                // ub exists, and lb not: mapping [0;1] --> ]-INF;ub]
                if ( ub_def )
                    v = ub - delta_max * 10 *
                    sqrt ( -log ( NOMAD::DEFAULT_EPSILON +
                                 ( i +NOMAD::RNG::rand()/NOMAD::D_INT_MAX ) * w ) );
                
                // there are no bounds: mapping [0;1] --> ]-INF;+INF[
                else
                    v = (NOMAD::RNG::rand()%2 ? -1.0 : 1.0) * delta_max * 10 *
                    sqrt ( - log ( NOMAD::DEFAULT_EPSILON +
                                  ( i + NOMAD::RNG::rand()/NOMAD::D_INT_MAX ) * w ) );
            }
        }
        
        // rounding:
        if ( rounding )
            v = v.round();
        
        // projection to mesh (with ref=0):
        v.project_to_mesh ( 0.0 , delta , lb , ub );
        
        // affectation + permutation:
        x[rp.pickup()] = v;
    }
}
/*-----------------------------------*/
NOMAD::TGP_Output_Model::TGP_Output_Model
( const std::list<const NOMAD::Eval_Point *> & X_pts     ,
  int                                          bbo_index ,
  int                                          seed      ,
  const NOMAD::Display                       & out         )
  : _out         ( out            ) ,
    _p           ( X_pts.size()   ) ,
    _Z           ( new double[_p] ) ,
    _Z_is_scaled ( false          ) ,
    _is_binary   ( true           ) ,
    _bin_values  ( 2              ) ,
    _is_fixed    ( false          ) ,
    _tgp_state   ( NULL           ) ,
    _tgp_model   ( NULL           ) ,
    _tgp_its     ( NULL           )
{
  NOMAD::TGP_Output_Model::_force_quit = false;

  _Z_scaling[0] = _Z_scaling[1] = 0.0;

  std::list<const NOMAD::Eval_Point *>::const_iterator it , end = X_pts.end();

  NOMAD::Double tmp , Zmin , Zmax , Zsum = 0.0;
  int           j = 0;

  for ( it = X_pts.begin() ; it != end ; ++it ) {

    // the output value:
    tmp = (*it)->get_bb_outputs()[bbo_index];
    _Z[j++] = tmp.value();
    
    // Z scaling parameters (1/2):
    Zsum += tmp;
    if ( !Zmin.is_defined() || tmp < Zmin )
      Zmin = tmp;
    if ( !Zmax.is_defined() || tmp > Zmax )
      Zmax = tmp;

    // check if the output is binary:
    if ( _is_binary ) {
      if ( !_bin_values[0].is_defined() )
	_bin_values[0] = tmp;
      else if ( !_bin_values[1].is_defined() && tmp != _bin_values[0] )
	_bin_values[1] = tmp;
      else {
	if ( tmp != _bin_values[0] && tmp != _bin_values[1] )
	  _is_binary = false;
      }
    }
  }

  // Z scaling parameters (1/2):
  _Z_scaling[0] = (Zmax - Zmin).value();

  // the output is fixed:
  if ( _Z_scaling[0] == 0.0 )
    _is_fixed = true;

  else {

    _Z_scaling[1] = (Zsum/_p).value() / _Z_scaling[0];

    if ( !_is_binary )
      _bin_values = NOMAD::Point(2);

    // RNG (random number generator):
    int state[] = { 896 , 265 , 371 };

    // if seed==0, the model will be the same as the one constructed in R,
    // with values taken from the R tgp functions for:
    //   set.seed(0)
    //   state <- sample(seq(0, 999), 3)

    // otherwise use rand() to get three different integers in [0;999]:
    if ( seed != 0 ) {
      state[0] = NOMAD::RNG::rand()%1000;
      while ( state[1] == state[0] )
	state[1] = NOMAD::RNG::rand()%1000;
      while ( state[2] == state[0] || state[2] == state[1] )
	state[2] = NOMAD::RNG::rand()%1000;
    }
    _tgp_state = newRNGstate ( three2lstate ( state ) );

    // importance tempering:
    _tgp_its = new Temper ( NOMAD::TGP_Output_Model::_ditemps );
  }
}
		/**
		 \return A double with the update basis tau.
		 */
		double get_update_basis ( void ) const { return _update_basis.value(); }
 /*-------------------------------------------------------------------*/
 bool NOMAD::Evaluator::eval_x ( std::list<NOMAD::Eval_Point *>	& list_eval,
                                const NOMAD::Double				& h_max ,
                                std::list<bool>					& list_count_eval) const
 {
     
     std::list<NOMAD::Eval_Point *>::iterator it;
     std::list<NOMAD::Eval_Point *>::iterator it_begin=list_eval.begin();
     std::list<NOMAD::Eval_Point *>::iterator it_end=list_eval.end();
     std::list<bool>::iterator it_count=list_count_eval.begin();
     
     if ( list_eval.size() !=list_count_eval.size())
         throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ ,
                                 "Evaluator: inconsistent size of list" );
     
     if ( _bb_exe.empty())
         throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ ,
                                 "Evaluator: no BB_EXE is defined (blackbox executable names)" );
     
     bool sgte = ((*it_begin)->get_eval_type() == NOMAD::SGTE);
     if ( sgte && _sgte_exe.empty() )
         throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ ,
                                 "Evaluator: no SGTE_EXE is defined (surrogate executable names)" );
     
     
     
     int         pid     = NOMAD::get_pid();
     int         seed    = _p.get_seed();
     std::string tmp_dir = _p.get_tmp_dir();
     
     std::ostringstream oss;
     oss << "." << seed;
     if ( pid != seed )
         oss << "." << pid;
     
     for (it=it_begin;it!=it_end;++it)
     {
         if (!(*it)->is_complete() )
             throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ ,
                                     "Evaluator: points provided for evaluations are incomplete " );
         
     }
     // add the tag of the first point
     oss << "." << (*it_begin)->get_tag();
     
     oss << "." ;
     const std::string & sint = oss.str();
     
     // for the parallel version: no need to include the process rank in the names
     // as the point tags are unique for all the processes: each process creates
     // its own points and uses Eval_Point::set_tag()
     
     // blackbox input file writing:
     // ----------------------------
     std::string bb_input_file_name =
     tmp_dir + NOMAD::BLACKBOX_INPUT_FILE_PREFIX
     + sint + NOMAD::BLACKBOX_INPUT_FILE_EXT;
     
     std::string bb_output_file_name =
     tmp_dir + NOMAD::BLACKBOX_OUTPUT_FILE_PREFIX
     + sint + NOMAD::BLACKBOX_OUTPUT_FILE_EXT;
     
     std::ofstream fout ( bb_input_file_name.c_str() );
     if ( fout.fail() ) {
         std::string err = "could not open file blackbox input file " + bb_input_file_name;
         throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ , err );
     }
     
     
     for (it=it_begin;it!=it_end;++it)
     {
         // include seed:
         if ( _p.get_bb_input_include_seed() )
             fout << seed << " ";
         
         // include tag:
         if ( _p.get_bb_input_include_tag() )
             fout << (*it)->get_tag() << " ";
         
         fout.setf ( std::ios::fixed );
         fout.precision ( NOMAD::DISPLAY_PRECISION_BB );
         (*it)->Point::display ( fout , " " , -1 , -1 );
         fout << std::endl;
     }
     
     fout.close();
     
     if ( fout.fail() )
         return false;
     
     for (it=it_begin;it!=it_end;++it)
         (*it)->set_eval_status ( NOMAD::EVAL_IN_PROGRESS );
     
     
     std::string   cmd , bb_exe;
     std::ifstream fin;
     bool          failed;
     NOMAD::Double d;
     int           j , nbbok;
     int           ibbo = 0;
     
     // system call to evaluate the blackboxes:
     // -------------------------------------
     size_t bn = _bb_exe.size();
     for ( size_t k = 0 ; k < bn ; ++k )
     {
         
         // executable name:
         bb_exe = ( sgte ) ? _sgte_exe[k] : _bb_exe[k];
         
         // system command:
         cmd = bb_exe + " " + bb_input_file_name;
         
         // redirection ? if no, the blackbox has to create
         // the output file 'bb_output_file_name':
         if ( _p.get_bb_redirection() )
             cmd += " > " + bb_output_file_name;
         
         
         // the evaluation:
         {
             int signal = system ( cmd.c_str() );
             
             // catch the ctrl-c signal:
             if ( signal == SIGINT )
                 raise ( SIGINT );
             
             // other evaluation error:
             failed = ( signal != 0 );
         }
         
         // the evaluation failed (we stop the evaluations):
         if ( failed )
         {
             it_count=list_count_eval.begin();
             for (it=it_begin;it!=it_end;++it,++it_count)
             {
                 (*it)->set_eval_status ( NOMAD::EVAL_FAIL );
                 (*it_count)=true;    //
             }
             break;
         }
         
         // reading of the blackbox output file:
         // ------------------------------------
         else
         {
             
             // bb-output file reading:
             fin.open ( bb_output_file_name.c_str() );
             
             string s;
             bool is_defined,is_inf;
             
             bool list_all_failed_eval=true;
             bool list_all_interrupt=true;
             
             // loop on the points
             it_count=list_count_eval.begin();
             for (it=it_begin;it!=it_end;++it,++it_count)
             {
                 failed		= false;
                 is_defined	= true;
                 is_inf		= false;
                 
                 // loop on the number of outputs for this blackbox:
                 nbbok = _bb_nbo[k];
                 ibbo=0;
                 for ( j = 0 ; j < nbbok ; ++j )
                 {
                     
                     fin >> s;
                     
                     if ( fin.fail() )
                     {
                         failed = true;
                         break;
                     }
                     
                     toupper(s);
                     if (s.compare("REJECT")==0)
                     {
                         *it_count=false;   // Rejected points are not counted
                         (*it)->set_eval_status(NOMAD::EVAL_USER_REJECT);
                         break;
                     }
                     else
                     {
                         d.atof(s);
                         (*it_count)=true;
                     }
                     //
                     
                     if  (s.compare("FAIL")==0)
                     {
                         failed = true;
                         break;
                     }
                     
                     
                     if ( !d.is_defined() )
                     {
                         is_defined = false;
                         break;
                     }
                     
                     
                     if ( d.value() >= NOMAD::INF ) {
                         is_inf = true;
                         break;
                     }
                     
                     (*it)->set_bb_output ( ibbo++ , d );
                 }
                 
                 
                 // the evaluation failed:
                 if ( failed || !is_defined || is_inf )
                 {
                     (*it)->set_eval_status ( NOMAD::EVAL_FAIL );
                     
                 } else
                     list_all_failed_eval=false;
                 
                 
                 // stop the evaluations if h > h_max or if a 'EB' constraint is violated:
                 if ( !( k < _bb_exe.size() - 1 && interrupt_evaluations ( *(*it) , h_max ) && list_all_interrupt ))
                     list_all_interrupt=false;
             }
             
             fin.close();
             
             if (list_all_failed_eval || list_all_interrupt)
                 break;
             
         }
     }
     
     
     // delete the blackbox input and output files:
     // -------------------------------------------
     remove ( bb_input_file_name.c_str () );
     remove ( bb_output_file_name.c_str() );
     
     bool at_least_one_eval_ok=false;
     int index_cnt_eval = _p.get_index_cnt_eval();
     
     
     // update eval status and check that at least one was ok
     it_count=list_count_eval.begin();
     for (it=it_begin;it!=it_end;++it,++it_count)
     {
         if ( (*it)->get_eval_status() == NOMAD::EVAL_IN_PROGRESS )
             (*it)->set_eval_status ( NOMAD::EVAL_OK );
         
         if (!at_least_one_eval_ok && (*it)->is_eval_ok())
             at_least_one_eval_ok=true;
         
         // count_eval from bb_outputs:
         // --------------------------
         if ( index_cnt_eval >= 0 && (*it)->get_bb_outputs()[index_cnt_eval]==0)
             *it_count=false;
     }
     
     return at_least_one_eval_ok;
 }
    /*-------------------------------------------------------------------*/
    bool NOMAD::Evaluator::eval_x ( NOMAD::Eval_Point	& x          ,
                                   const NOMAD::Double	& h_max      ,
                                   bool					& count_eval   ) const
    {
        count_eval = false;
        
        if ( _bb_exe.empty() || !x.is_complete() )
            throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ ,
                                    "Evaluator: no BB_EXE is defined (blackbox executable names)" );
        
        bool sgte = x.get_eval_type() == NOMAD::SGTE;
        if ( sgte && _sgte_exe.empty() )
            throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ ,
                                    "Evaluator: no SGTE_EXE is defined (surrogate executable names)" );
        
        int         pid     = NOMAD::get_pid();
        int         seed    = _p.get_seed();
        std::string tmp_dir = _p.get_tmp_dir();
        
        std::ostringstream oss;
        oss << "." << seed;
        if ( pid != seed )
            oss << "." << pid;
        oss << "." << x.get_tag() << ".";
        const std::string & sint = oss.str();
        
        // for the parallel version: no need to include the process rank in the names
        // as the point tags are unique for all the processes: each process creates
        // its own points and uses Eval_Point::set_tag()
        
        // blackbox input file writing:
        // ----------------------------
        std::string bb_input_file_name =
        tmp_dir + NOMAD::BLACKBOX_INPUT_FILE_PREFIX
        + sint + NOMAD::BLACKBOX_INPUT_FILE_EXT;
        
        std::string bb_output_file_name =
        tmp_dir + NOMAD::BLACKBOX_OUTPUT_FILE_PREFIX
        + sint + NOMAD::BLACKBOX_OUTPUT_FILE_EXT;
        
        std::ofstream fout ( bb_input_file_name.c_str() );
        if ( fout.fail() )
        {
            std::string err = "could not create file blackbox input file " + bb_input_file_name + ". \n \n #### Please check that write permission are granted for the working directory. #### ";
            throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ , err );
       }
        
        // include seed:
        if ( _p.get_bb_input_include_seed() )
            fout << seed << " ";
        
        // include tag:
        if ( _p.get_bb_input_include_tag() )
            fout << x.get_tag() << " ";
        
        fout.setf ( std::ios::fixed );
        fout.precision ( NOMAD::DISPLAY_PRECISION_BB );
        x.Point::display ( fout , " " , -1 , -1 );
        fout << std::endl;
        
        fout.close();
        
        if ( fout.fail() )
            return false;
        
        x.set_eval_status ( NOMAD::EVAL_IN_PROGRESS );
        
        std::string   cmd , bb_exe;
        std::ifstream fin;
        bool          failed;
        NOMAD::Double d;
        int           j , nbbok;
        int           ibbo = 0;
        
        // system call to evaluate the blackbox:
        // -------------------------------------
        size_t bn = _bb_exe.size();
        for ( size_t k = 0 ; k < bn ; ++k )
        {
            
            // executable name:
            bb_exe = ( sgte ) ? _sgte_exe[k] : _bb_exe[k];
            
            // system command:
            cmd = bb_exe + " " + bb_input_file_name;
            
            // redirection ? if no, the blackbox has to create
            // the output file 'bb_output_file_name':
            if ( _p.get_bb_redirection() )
                cmd += " > " + bb_output_file_name;
            
#ifdef DEBUG
#ifdef USE_MPI
            int rank;
            MPI_Comm_rank ( MPI_COMM_WORLD, &rank);
            _p.out() << "command(rank=" << rank
            << ") = \'" << cmd << "\'" << std::endl;
#else
            _p.out() << "command=\'" << cmd << "\'" << std::endl;
#endif
#endif
            
            // the evaluation:
            {
                int signal = system ( cmd.c_str() );
                
                // catch the ctrl-c signal:
                if ( signal == SIGINT )
                    raise ( SIGINT );
                
                // other evaluation error:
                failed = ( signal != 0 );
                count_eval = true;
            }
            
            // the evaluation failed (we stop the evaluations):
            if ( failed )
            {
                x.set_eval_status ( NOMAD::EVAL_FAIL );
                break;
            }
            
            // reading of the blackbox output file:
            // ------------------------------------
            else
            {
                
                // bb-output file reading:
                fin.open ( bb_output_file_name.c_str() );
                
                failed          = false;
                bool is_defined = true;
                bool is_inf     = false;
                
                // loop on the number of outputs for this blackbox:
                nbbok = _bb_nbo[k];
                for ( j = 0 ; j < nbbok ; ++j )
                {
                    
                    fin >> d;
                    
                    if ( !d.is_defined() )
                    {
                        is_defined = false;
                        break;
                    }
                    
                    if ( fin.fail() )
                    {
                        failed = true;
                        break;
                    }
                    
                    if ( d.value() >= NOMAD::INF )
                    {
                        is_inf = true;
                        break;
                    }
                    
                    x.set_bb_output ( ibbo++ , d );
                }
                
                fin.close();
                
                // the evaluation failed:
                if ( failed || !is_defined || is_inf )
                {
                    x.set_eval_status ( NOMAD::EVAL_FAIL );
                    break;
                }
                
                // stop the evaluations if h > h_max or if a 'EB' constraint is violated:
                if ( k < _bb_exe.size() - 1 && interrupt_evaluations ( x , h_max ) )
                    break;
            }
        }
        
        if ( x.get_eval_status() == NOMAD::EVAL_IN_PROGRESS )
            x.set_eval_status ( NOMAD::EVAL_OK );
        
        // delete the blackbox input and output files:
        // -------------------------------------------
        remove ( bb_input_file_name.c_str () );
        remove ( bb_output_file_name.c_str() );
        
        // check the CNT_EVAL output:
        // --------------------------
        int index_cnt_eval = _p.get_index_cnt_eval();
        if ( index_cnt_eval >= 0 && x.get_bb_outputs()[index_cnt_eval] == 0.0 )
            count_eval = false;
        
        return x.is_eval_ok();
    }