Example #1
0
/*------------------------------------------------------------------*/
void NOMAD::Phase_One_Evaluator::compute_f(NOMAD::Eval_Point &x) const
{
    if (x.get_bb_outputs().size() != _p.get_bb_nb_outputs())
    {
        std::ostringstream err;
        err << "Phase_One_Evaluator::compute_f(x): "
            << "x has a wrong number of blackbox outputs ("
            << x.get_bb_outputs().size() <<  " != " << _p.get_bb_nb_outputs() << ")";
        throw NOMAD::Exception("Phase_One_Evaluator.cpp" , __LINE__ , err.str());
    }

    // objective value for MADS phase 1: the squared sum of all EB constraint violations
    // (each EB constraint has been previously transformed into OBJ values):
    const std::list<int>                &index_obj = _p.get_index_obj();
    const std::list<int>::const_iterator end       = index_obj.end();
    const NOMAD::Point                  &bbo       = x.get_bb_outputs();
    NOMAD::Double                        h_min     = _p.get_h_min();
    NOMAD::Double                        sum       = 0.0;
    NOMAD::Double                        v;

    for (std::list<int>::const_iterator it = index_obj.begin() ; it != end ; ++it)
    {
        v = bbo[*it];
        if (v > h_min)
            sum += v.pow2();
    }

    x.set_f(sum);
}
Example #2
0
/*---------------------------------------------------------*/
void NOMAD::TGP_Model::get_Ds2x_points(std::set<int> &pts_indexes) const
{
    pts_indexes.clear();
    if (!_Ds2x || _n_XX == 0)
        return;

    int i , j , k , m = _bbot.size();

    for (i = 0 ; i < m ; ++i)
        if (_Ds2x[i])
        {

            NOMAD::Double max;
            k = -1;
            for (j = 0 ; j < _n_XX ; ++j)
                if (!max.is_defined() || _Ds2x[i][j] > max)
                {
                    max = _Ds2x[i][j];
                    k   = j;
                }

            if (k >= 0)
                pts_indexes.insert(k);
        }
}
/*-----------------------------------------------*/
int NOMAD::Priority_Eval_Point::compare_hf_values ( const NOMAD::Double & hx1 ,
        const NOMAD::Double & fx1 ,
        const NOMAD::Double & hx2 ,
        const NOMAD::Double & fx2   ) const
{
    if ( fx1.is_defined() && fx2.is_defined() )
    {

        if ( hx1.is_defined() && hx2.is_defined() )
        {
            // x1 is feasible:
            if ( hx1 <= _h_min )
            {
                // both points are feasible:
                if ( hx2 <= _h_min  )
                {
                    if ( fx1 < fx2 )
                        return 1;
                    if ( fx2 < fx1 )
                        return -1;
                }

                // x1 feasible and x2 infeasible:
                else
                    return 1;
            }

            // x1 is infeasible:
            else
            {
                // x2 is feasible:
                if ( hx2 <= _h_min  )
                    return -1;

                // both points are infeasible:
                if ( ( hx1  < hx2 && fx1  < fx2 ) ||
                        ( hx1 == hx2 && fx1  < fx2 ) ||
                        ( hx1  < hx2 && fx1 == fx2 )    )
                    return 1;

                if ( ( hx2  < hx1 && fx2  < fx1 ) ||
                        ( hx2 == hx1 && fx2  < fx1 ) ||
                        ( hx2  < hx1 && fx2 == fx1 )    )
                    return -1;
            }
        }

        // we only have f values:
        else
        {
            if ( fx1 < fx2 )
                return 1;
            if ( fx2 < fx1 )
                return -1;
        }
    }
    return 0;
}
Example #4
0
/*--------------------------------------------------------*/
void NOMAD::Evaluator::compute_h ( NOMAD::Eval_Point & x ) const
{
  if ( x.get_bb_outputs().size() != _p.get_bb_nb_outputs() ) {
    std::ostringstream err;
    err << "Evaluator::compute_h(x): x has a wrong number of blackbox outputs ("
	<< x.get_bb_outputs().size() << " != "
	<< _p.get_bb_nb_outputs() << ")";
    throw NOMAD::Exception ( "Evaluator.cpp" , __LINE__ , err.str() );
  }

  int                                        nbo  = _p.get_bb_nb_outputs();
  const std::vector<NOMAD::bb_output_type> & bbot = _p.get_bb_output_type();
  const NOMAD::Point                       & bbo  = x.get_bb_outputs();
  NOMAD::Double                              h    = 0.0 , bboi;

  x.set_EB_ok ( true );

  for ( int i = 0 ; i < nbo ; ++i ) {

    bboi = bbo[i];

    if ( bboi.is_defined()                                  &&
	 (bbot[i] == NOMAD::EB || bbot[i] == NOMAD::PEB_E ) &&
	 bboi > _p.get_h_min() ) {
      h.clear();
      x.set_h     ( h     );
      x.set_EB_ok ( false );
      return;
    }
    
    if ( bboi.is_defined() &&
	 ( bbot[i] == NOMAD::FILTER ||
	   bbot[i] == NOMAD::PB     ||
	   bbot[i] == NOMAD::PEB_P     ) ) {
      if ( bboi > _p.get_h_min() ) {
	switch ( _p.get_h_norm() ) {
	case NOMAD::L1:
	  h += bboi;
	  break;
	case NOMAD::L2:
	  h += bboi * bboi;
	  break;
	case NOMAD::LINF:
	  if ( bboi > h )
	    h = bboi;
	  break;
	}
      }
    }
  }

  if ( _p.get_h_norm() == NOMAD::L2 )
    h = h.sqrt();

  x.set_h ( h );
}
Example #5
0
NOMAD::Double NOMAD::TGP_Model::get_Yw(void) const
{
    NOMAD::Double Yw , tmp;
    for (int i = 0 ; i < _n0 ; ++i)
    {
        tmp = _ub[i]-_lb[i];
        if (!Yw.is_defined() || tmp > Yw)
            Yw = tmp;
    }
    return Yw;
}
/*-----------------------------------------------*/
int NOMAD::Priority_Eval_Point::compare_h_values ( const NOMAD::Double & hx1 ,
        const NOMAD::Double & hx2   ) const
{
    if ( hx1.is_defined() && hx2.is_defined() )
    {
        if ( hx1 < hx2 )
            return 1;
        if ( hx2 < hx1 )
            return -1;
    }
    return 0;
}
 /*-----------------------------------------------------------------------*/
 bool NOMAD::Evaluator::interrupt_evaluations ( const NOMAD::Eval_Point & x     ,
                                               const NOMAD::Double     & h_max   ) const
 {
     int                                        nbo     = _p.get_bb_nb_outputs();
     const NOMAD::Point                       & bbo     = x.get_bb_outputs();
     const std::vector<NOMAD::bb_output_type> & bbot    = _p.get_bb_output_type();
     NOMAD::Double                              h       = 0.0;
     bool                                       check_h = h_max.is_defined();
     
     for ( int i = 0 ; i < nbo ; ++i ) {
         
         if ( bbo[i].is_defined()                                 &&
             ( bbot[i] == NOMAD::EB || bbot[i] == NOMAD::PEB_E ) &&
             bbo[i] > _p.get_h_min() )
             return true;
         
         if ( check_h && bbo[i].is_defined() &&
             (bbot[i] == NOMAD::FILTER ||
              bbot[i] == NOMAD::PB     ||
              bbot[i] == NOMAD::PEB_P     ) ) {
                 
                 if ( bbo[i] > _p.get_h_min() ) {
                     switch ( _p.get_h_norm() ) {
                         case NOMAD::L1:
                             h += bbo[i];
                             break;
                         case NOMAD::L2:
                             h += bbo[i].pow2();
                             break;
                         case NOMAD::LINF:
                             if ( bbo[i] > h )
                                 h = bbo[i];
                             break;
                     }
                     
                     if ( _p.get_h_norm() == NOMAD::L2 ) {
                         if ( h > h_max.pow2() )
                             return true;
                     }
                     else if ( h > h_max )
                         return true;
                 }
             }
     }
     return false;
 }
Example #8
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;
				}
		}
}
Example #9
0
/*---------------------------------------------------------*/
bool NOMAD::Directions::compute_halton_dir ( int                halton_index ,
					     int                mesh_index   ,
					     NOMAD::Double    & alpha_t_l    ,
					     NOMAD::Direction & halton_dir     ) const
{
  alpha_t_l.clear();

  int           i;
  NOMAD::Double d , norm = 0.0;
  NOMAD::Point  b ( _nc );

  for ( i = 0 ; i < _nc ; ++i ) 
  {
    d = Directions::get_phi ( halton_index , _primes[i] );
    b[i] = 2*d - 1.0;
    norm += b[i].pow2();
  }
  norm = norm.sqrt();

  // desired squared norm for q:

  NOMAD::direction_type hdt = halton_dir.get_type();

  NOMAD::Double target = ( hdt == NOMAD::ORTHO_2N || hdt == NOMAD::ORTHO_NP1_QUAD || hdt == NOMAD::ORTHO_NP1_NEG ) ?
    pow ( NOMAD::Mesh::get_mesh_update_basis() , abs(mesh_index) / 2.0 ) :
    pow ( NOMAD::Mesh::get_mesh_update_basis() , abs(mesh_index)       );
	

  NOMAD::Double x  = target.sqrt();
  NOMAD::Double fx = eval_ortho_norm ( x , norm , b , halton_dir );
  NOMAD::Double y  = 1.0 , fy , delta_x , abs_dx , min , delta_min , diff , eps;
  bool          inc , possible , set_eps = false;
  int           cnt = 0;

  while ( fx != target )
  {
	  
	  // safety test:
	  if ( ++cnt > 1000 )
	  {
#ifdef DEBUG
		  _out << std::endl << "Warning (" << "Directions.cpp" << ", " << __LINE__
		  << "): could not compute Halton direction for (t="
		  << halton_index << ", ell=" << mesh_index
		  << ")" << std::endl << std::endl;
#endif
		  alpha_t_l.clear();
		  halton_dir.clear();
		  return false;
	  }
	  
	  if ( set_eps ) 
	  {
		  eps     = 1e-8;
		  set_eps = false;
	  }
	  else
		  eps = 0.0;
	  
	  inc = ( fx < target );
	  
	  possible = false;
	  min      = 1e+20;
	  for ( i = 0 ; i < _nc ; ++i ) 
	  {
		  if ( b[i] != 0.0 ) 
		  {
			  if ( b[i] > 0.0 )
			  {
				  if ( inc )
					  diff  =  0.5+eps;
				  else
					  diff  = -0.5-eps;
			  }
			  else 
			  {
				  if ( inc )
					  diff  = -0.5-eps;
				  else
					  diff  =  0.5+eps;
			  }
			  
			  delta_x = norm * ( halton_dir[i] + diff) / b[i] - x;
			  
			  y = x + delta_x;
			  
			  if ( y > 0 )
			  {
				  abs_dx = delta_x.abs();
				  if ( abs_dx < min )
				  {
					  min       = abs_dx;
					  delta_min = delta_x;
					  possible  = true;
				  }
			  }
		  }
	  }
	  
	  if ( !possible ) {
#ifdef DEBUG
		  _out << std::endl << "Warning (" << "Directions.cpp" << ", " << __LINE__
		  << "): could not compute Halton direction for (t="
		  << halton_index << ", ell=" << mesh_index << ")"
		  << std::endl << std::endl;
#endif
		  alpha_t_l.clear();
		  halton_dir.clear();
		  return false;
	  }
	  
	  y  = x + delta_min;
	  fy = eval_ortho_norm ( y , norm , b , halton_dir );
	  
	  if ( fx == fy ) {
		  set_eps = true;
		  continue;
	  }
	  
	  if ( fy==target )
		  break;
	  
	  if ( inc && fy > target && fx > 0 ) {
		  eval_ortho_norm ( x , norm , b , halton_dir );
		  break;
	  }
	  
	  if ( !inc && fy < target && fy > 0 )
		  break;
	  
	  x  = y;
	  fx = fy;
  }

  alpha_t_l = y;

  return true;
}
Example #10
0
/*--------------------------------------------------*/
bool NOMAD::Eval_Point::check(int m , NOMAD::check_failed_type &cf) const
{
    if (size() <= 0 || !_signature || m != _bb_outputs.size())
    {
        std::string err = "Eval_Point::check() could not procede";
        if (!_signature)
            err += " (no signature)";
        else if (m != _bb_outputs.size())
            err += " (wrong number of blackbox outputs)";
        else
            err += " (point size <= 0 !)";
        throw NOMAD::Exception("Eval_Point.cpp" , __LINE__ , err);
    }

    cf = NOMAD::CHECK_OK;

    const std::vector<NOMAD::bb_input_type>
    &input_types = _signature->get_input_types();
    const NOMAD::Point &lb          = _signature->get_lb();
    const NOMAD::Point &ub          = _signature->get_ub();
    const NOMAD::Point &fv          = _signature->get_fixed_variables();
    int                  n           = size();
    NOMAD::bb_input_type iti;

    for (int i = 0 ; i < n ; ++i)
    {

        const NOMAD::Double xi = (*this)[i];

        // undefined coordinates ?
        if (!xi.is_defined())
            throw NOMAD::Exception("Eval_Point.cpp" , __LINE__ ,
                                   "Eval_Point::check() could not procede (undefined coordinates)");

        // check the bounds:
        const NOMAD::Double &lbi = lb[i];
        if (lbi.is_defined() && xi < lbi)
        {
            cf = NOMAD::LB_FAIL;
            return false;
        }

        const NOMAD::Double &ubi = ub[i];
        if (ubi.is_defined() && xi > ubi)
        {
            cf = NOMAD::UB_FAIL;
            return false;
        }

        // check the integer/categorical/binary variables:
        iti = input_types[i];
        if (iti == NOMAD::BINARY && !xi.is_binary())
        {
            cf = NOMAD::BIN_FAIL;
            return false;
        }
        if ((iti == NOMAD::INTEGER || iti == NOMAD::CATEGORICAL)
            && !xi.is_integer())
        {
            cf = (iti == NOMAD::INTEGER) ? NOMAD::INT_FAIL : NOMAD::CAT_FAIL;
            return false;
        }

        // check the fixed-variables:
        const NOMAD::Double &fvi = fv[i];
        if (fvi.is_defined() && fvi != xi)
        {
            cf = NOMAD::FIX_VAR_FAIL;
            return false;
        }
    }
    return true;
}
/*-----------------------------------*/
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 );
  }
}
/*------------------------------------------------*/
bool NOMAD::Priority_Eval_Point::dominates
( const NOMAD::Set_Element<NOMAD::Eval_Point> & x ) const
{
    if ( this == &x )
        return false;
    const NOMAD::Eval_Point * x1 = get_element();
    const NOMAD::Eval_Point * x2 = x.get_element();

    // criterion 0: lexicographic order
    if (_lexicographic_order)
        return NOMAD::Point(*x1) < NOMAD::Point(*x2);


    // criterion 1: user criterion:
    // ------------
    const NOMAD::Double uep1 = x1->get_user_eval_priority();
    if ( uep1.is_defined() )
    {
        const NOMAD::Double uep2 = x2->get_user_eval_priority();
        if ( uep2.is_defined() )
        {
            if ( uep1 > uep2 )
                return true;
            if ( uep2 > uep1 )
                return false;
        }
    }

    // specific Priority_Eval_Point elements of comparison:
    NOMAD::Double x_f_sgte;
    NOMAD::Double x_h_sgte;
    NOMAD::Double x_f_model;
    NOMAD::Double x_h_model;
    NOMAD::Double x_angle_success_dir;
    NOMAD::Double x_angle_simplex_grad;

    x.get_priority_criteria ( x_f_sgte             ,
                              x_h_sgte             ,
                              x_f_model            ,
                              x_h_model            ,
                              x_angle_success_dir  ,
                              x_angle_simplex_grad   );

    // criterion 2: give priority to already evaluated cache points:
    // ------------
    if ( x1->is_in_cache() && !x2->is_in_cache() )
        return true;
    if ( x2->is_in_cache() && !x1->is_in_cache() )
        return false;

    // criterion 3: give priority to already evaluated points
    // ------------ that are eval_ok:
    if ( x1->is_eval_ok() && !x2->is_eval_ok() )
        return true;
    if ( x2->is_eval_ok() && !x1->is_eval_ok() )
        return false;

    // criterion 4: true f and h values:
    // -----------
    int flag = compare_hf_values ( x1->get_h() ,
                                   x1->get_f() ,
                                   x2->get_h() ,
                                   x2->get_f()   );
    if ( flag )
        return ( flag > 0 );

    // criterion 5: surrogate f and h values:
    // ------------
    flag = compare_hf_values ( _h_sgte , _f_sgte , x_h_sgte , x_f_sgte );
    if ( flag )
        return ( flag > 0 );

    // criterion 6: model f and h values:
    // ------------
    flag = compare_hf_values ( _h_model , _f_model , x_h_model , x_f_model );
    if ( flag )
        return ( flag > 0 );



    // criterion 7: check the angle with the last successful direction:
    // ------------
    if ( _angle_success_dir.is_defined() && x_angle_success_dir.is_defined() )
    {
        if ( _angle_success_dir < x_angle_success_dir )
            return true;
        if ( x_angle_success_dir < _angle_success_dir )
            return false;
    }


    // criterion 8: take the point with the best h value:
    // ------------
    flag = compare_h_values ( x1->get_h() , x2->get_h() );
    if ( flag )
        return ( flag > 0 );

    flag = compare_h_values ( _h_sgte , x_h_sgte );
    if ( flag )
        return ( flag > 0 );

    flag = compare_h_values ( _h_model , x_h_model );
    if ( flag )
        return ( flag > 0 );

    // criterion 9: random criterion for randomly generated directions:
    // -------------
    const NOMAD::Double rep1 = x1->get_rand_eval_priority();
    if ( rep1.is_defined() )
    {
        const NOMAD::Double rep2 = x2->get_rand_eval_priority();
        if ( rep2.is_defined() )
        {
            if ( rep1 < rep2 )
                return true;
            if ( rep2 < rep1 )
                return false;
        }
    }

    // criterion 10: compare the tags:
    // -------------
    return x1->get_tag() < x2->get_tag();

}
Example #13
0
/*----------------------------------------------------------------*/
void NOMAD::TGP_Model::eval_hf(const NOMAD::Point   &bbo    ,
                               const NOMAD::Double &h_min  ,
                               NOMAD::hnorm_type     h_norm ,
                               NOMAD::Double        &h      ,
                               NOMAD::Double        &f) const
{
    f.clear();
    h = 0.0;

    int m = bbo.size();

    if (m != static_cast<int>(_bbot.size()))
        throw NOMAD::Exception("TGP_Model.cpp" , __LINE__ ,
                               "TGP_Model::eval_hf() called with an invalid bbo argument");

    NOMAD::Double bboi;

    for (int i = 0 ; i < m ; ++i)
    {

        bboi = bbo[i];

        if (bboi.is_defined())
        {

            if (_bbot[i] == NOMAD::EB || _bbot[i] == NOMAD::PEB_E)
            {

                if (bboi > h_min)
                {
                    h.clear();
                    return;
                }
            }

            else if ((_bbot[i] == NOMAD::FILTER ||
                      _bbot[i] == NOMAD::PB     ||
                      _bbot[i] == NOMAD::PEB_P))
            {
                if (bboi > h_min)
                {
                    switch (h_norm)
                    {
                        case NOMAD::L1:
                            h += bboi;
                            break;
                        case NOMAD::L2:
                            h += bboi * bboi;
                            break;
                        case NOMAD::LINF:
                            if (bboi > h)
                                h = bboi;
                            break;
                    }
                }
            }

            else if (_bbot[i] == NOMAD::OBJ)
                f = bboi;
        }

    }

    if (h_norm == NOMAD::L2)
        h = h.sqrt();
}
 /*-------------------------------------------------------------------*/
 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();
    }
Example #16
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;
    }
}
		/**
		 \return A double with the update basis tau.
		 */
		double get_update_basis ( void ) const { return _update_basis.value(); }