/*------------------------------------------*/ int main ( int argc , char ** argv ) { // display: NOMAD::Display out ( NOMAD::rout ); //zhenghua out.precision ( NOMAD::DISPLAY_PRECISION_STD ); std::string error; { // NOMAD initializations: NOMAD::begin ( argc , argv ); // usage: if ( argc < 2 ) { NOMAD::display_usage ( out); //zhenghua NOMAD::end(); return EXIT_FAILURE; } // parameters file: std::string param_file_name = argv[1]; std::string opt = param_file_name; NOMAD::toupper ( opt ); // display version if option '-v' has been specified: if ( opt == "-U" ) { NOMAD::display_usage ( out ); NOMAD::end(); return EXIT_SUCCESS; } // display version if option '-v' has been specified: if ( opt == "-V" || opt =="-VERSION") { NOMAD::display_version ( out ); NOMAD::end(); return EXIT_SUCCESS; } // display info if option '-i' has been specified: if ( opt == "-I" || opt == "-INFO" ) { NOMAD::display_info ( out ); NOMAD::display_usage ( out ); //zhenghua NOMAD::end(); return EXIT_SUCCESS; } // parameters creation: NOMAD::Parameters p ( out ); // display help on parameters if option '-h' has been specified: if ( opt == "-H" || opt == "-HELP" ) { p.help ( argc , argv ); NOMAD::end(); return EXIT_SUCCESS; } // display developer help on parameters if option '-d' has been specified: if ( opt == "-D" ) { p.help ( argc , argv,true ); NOMAD::end(); return EXIT_SUCCESS; } // check the number of processess: #ifdef USE_MPI if ( NOMAD::Slave::get_nb_processes() < 2 ) { NOMAD::rout << "ERROR: Incorrect command to run with MPI." << std::endl; //zhenghua NOMAD::display_usage ( out ); //zhenghua NOMAD::end(); return EXIT_FAILURE; } #endif try { // read parameters file: p.read ( param_file_name ); // parameters check: p.check(); // display NOMAD info: if ( p.get_display_degree() > NOMAD::MINIMAL_DISPLAY) NOMAD::display_info ( out ); // parameters display: if ( NOMAD::Slave::is_master() && p.get_display_degree() == NOMAD::FULL_DISPLAY ) out << std::endl << NOMAD::open_block ( "parameters" ) << std::endl << p << NOMAD::close_block(); // algorithm creation and execution: NOMAD::Mads mads ( p , NULL ); if ( p.get_nb_obj() == 1 ) mads.run(); else mads.multi_run(); #ifdef MODEL_STATS mads.display_model_stats ( out ); #endif } catch ( std::exception & e ) { if ( NOMAD::Slave::is_master() ) { error = std::string ( "NOMAD has been interrupted: " ) + e.what(); NOMAD::rout << std::endl << error << std::endl << std::endl; //zhenghua } } NOMAD::Slave::stop_slaves ( out ); NOMAD::end(); } #ifdef MEMORY_DEBUG NOMAD::display_cardinalities ( out ); #endif return ( error.empty() ) ? EXIT_SUCCESS : EXIT_FAILURE; }
/*--------------------------------------------------*/ void NOMAD::Eval_Point::display(const NOMAD::Display &out , bool in_block) const { if (in_block) { std::ostringstream oss; oss << "#" << _tag; out << NOMAD::open_block(oss.str()) << "x = ( "; NOMAD::Point::display(out , " " , 2 , NOMAD::Point::get_display_limit()); out << " )" << std::endl << "F(x) = [ "; _bb_outputs.display(out , " " , 2 , NOMAD::Point::get_display_limit()); out << " ]" << std::endl; if (_h.is_defined()) out << "h = " << _h << std::endl; if (_f.is_defined()) out << "f = " << _f << std::endl; out.close_block(); } else { display_tag(out); out << " x=( "; NOMAD::Point::display(out , " " , 2 , NOMAD::Point::get_display_limit()); out << " ) F(x)=[ "; _bb_outputs.display(out , " " , 2 , NOMAD::Point::get_display_limit()); out << " ]"; if (_h.is_defined()) out << " h=" << _h; if (_f.is_defined()) out << " f=" << _f; } }
/*---------------------------------------------------------*/ void NOMAD::Cache::display ( const NOMAD::Display & out ) const { out << "number of cache points: " << size() << std::endl << "size in memory : "; out.display_size_of ( _sizeof ); out << std::endl << "cache file : "; if ( _locked_file.empty() ) out << "-" << std::endl; else out << _locked_file << std::endl; #ifdef DEBUG int nb = size(); int cnt = 0; out << NOMAD::open_block ( "cache points" ) << std::endl; const NOMAD::Eval_Point * cur = begin(); while ( cur ) { out << "point "; out.display_int_w ( ++cnt , nb ); out << "/" << nb << ": "; cur->display_eval ( out , false ); out << std::endl; cur = next(); } out.close_block(); // this display is repeated here as there can // be a lot of points displayed just before: out << "number of cache points: " << size() << std::endl << "size in memory : "; out.display_size_of ( _sizeof ); out << std::endl << "cache file : "; if ( _locked_file.empty() ) out << "-"; else out << _locked_file; out << std::endl; #endif }
/*---------------------------------------------------------*/ void NOMAD::Cache::display_extern_pts ( const NOMAD::Display & out ) const { int nb = static_cast<int>(_extern_pts.size()); int cnt = 0; std::list<const NOMAD::Eval_Point *>::const_iterator it , end = _extern_pts.end(); for ( it = _extern_pts.begin() ; it != end ; ++it ) { out << "point "; out.display_int_w ( ++cnt , nb ); out << "/" << nb << ": "; (*it)->display_eval ( out , false ); out << std::endl; } }
/*--------------------------------------------*/ void NOMAD::TGP_Model::display_X(const NOMAD::Display &out , int display_limit) const { if (_p == 0 || !_X) { out << "no interpolation points" << std::endl; return; } int i , j; int m = _bbot.size(); int i0 = (display_limit > 0) ? _p - display_limit : 0; NOMAD::Point x(_n) , bbo(m); out << NOMAD::open_block("interpolation points (X)"); if (i0 > 0) out << "..." << std::endl; else if (i0 < 0) i0 = 0; for (i = i0 ; i < _p ; ++i) { for (j = 0 ; j < _n ; ++j) x[j] = _X[i][j]; bbo = NOMAD::Point(m); if (_tgp_models) for (j = 0 ; j < m ; ++j) if (_tgp_models[j]) bbo[j] = (_tgp_models[j]->get_Z())[i]; out << "#"; out.display_int_w(i , _p); out << " x=("; x.display(out , " " , 15 , -1); out << " ) f(x)=["; bbo.display(out , " " , 15 , -1); out << " ]" << std::endl; } std::ostringstream oss; oss << "(size=" << _p << ")"; out << NOMAD::close_block(oss.str()) << std::endl; }
/*--------------------------------------------------*/ void NOMAD::Eval_Point::display_tag(const NOMAD::Display &out) const { out << "#"; out.display_int_w(_tag , NOMAD::Eval_Point::_current_tag); }
void NOMAD::display_cardinalities ( const NOMAD::Display & out ) { #ifdef USE_MPI if ( !NOMAD::Slave::is_master() ) return; #endif // compute the biggest int value for appropriate display width: int max = (NOMAD::Double::get_max_cardinality() > NOMAD::Point::get_max_cardinality()) ? NOMAD::Double::get_max_cardinality() : NOMAD::Point::get_max_cardinality(); if ( NOMAD::Direction::get_max_cardinality() > max ) max = NOMAD::Direction::get_max_cardinality(); if ( NOMAD::Set_Element<NOMAD::Eval_Point>::get_max_cardinality() > max ) max = NOMAD::Set_Element<NOMAD::Eval_Point>::get_max_cardinality(); if ( NOMAD::Set_Element<NOMAD::Signature>::get_max_cardinality() > max ) max = NOMAD::Set_Element<NOMAD::Signature>::get_max_cardinality(); if ( NOMAD::Cache_File_Point::get_max_cardinality() > max ) max = NOMAD::Cache_File_Point::get_max_cardinality(); // cardinalities display: // ---------------------- out << std::endl << NOMAD::open_block ( "important objects in memory" ); // NOMAD::Signature: out << "Signature : "; out.display_int_w ( NOMAD::Signature::get_cardinality() , max ); out << " (max="; out.display_int_w ( NOMAD::Signature::get_max_cardinality() , max ); out << ")" << std::endl; // NOMAD::Double: out << "Double : "; out.display_int_w ( NOMAD::Double::get_cardinality() , max ); out << " (max="; out.display_int_w ( NOMAD::Double::get_max_cardinality() , max ); out << ")" << std::endl; // NOMAD::Point: out << "Point : "; out.display_int_w ( NOMAD::Point::get_cardinality() , max ); out << " (max="; out.display_int_w ( NOMAD::Point::get_max_cardinality() , max ); out << ")" << std::endl; // NOMAD::Direction: out << "Direction : "; out.display_int_w ( NOMAD::Direction::get_cardinality() , max ); out << " (max="; out.display_int_w ( NOMAD::Direction::get_max_cardinality() , max ); out << ")" << std::endl; // Set_Element<Eval_Point>: out << "Set_Element<Eval_Point>: "; out.display_int_w (NOMAD::Set_Element<NOMAD::Eval_Point>::get_cardinality(), max); out << " (max="; out.display_int_w (NOMAD::Set_Element<NOMAD::Eval_Point>::get_max_cardinality(), max); out << ")" << std::endl; // Set_Element<NOMAD::Signature>: out << "Set_Element<Signature> : "; out.display_int_w (NOMAD::Set_Element<NOMAD::Signature>::get_cardinality(), max); out << " (max="; out.display_int_w (NOMAD::Set_Element<NOMAD::Signature>::get_max_cardinality(), max); out << ")" << std::endl; // NOMAD::Cache_File_Point: out << "Cache_File_Point : "; out.display_int_w ( NOMAD::Cache_File_Point::get_cardinality() , max ); out << " (max="; out.display_int_w ( NOMAD::Cache_File_Point::get_max_cardinality() , max ); out << ")" << std::endl; out << NOMAD::close_block(); }
/*--------------------------------------------*/ void NOMAD::TGP_Model::display(const NOMAD::Display &out) const { if (!_error_str.empty()) { out << "error with model" << std::endl; return; } int i , j; // fixed variables: out << "fixed_var = [ " << _fv << "]" << std::endl; out << "av_index = "; if (_av_index) { out << "[ "; for (i = 0 ; i < _n0 ; ++i) out << _av_index[i] << " "; out << "]" << std::endl; } else out << "NULL" << std::endl; out << "fv_index = "; if (_fv_index) { out << "[ "; for (i = 0 ; i < _n ; ++i) out << _fv_index[i] << " "; out << "]" << std::endl; } else out << "NULL" << std::endl; // bounds: out << "lb = [ " << _lb << "]" << std::endl << "ub = [ " << _ub << "]" << std::endl << std::endl; // display X: if (!_X) out << "X = NULL" << std::endl; else { out << "X = ["; for (i = 0 ; i < _p ; ++i) { out << "\t"; for (j = 0 ; j < _n ; ++j) out << std::setw(15) << _X[i][j] << " "; out << ((i==_p-1) ? "]" : ";") << std::endl; } out << "size(X)=" << _p << "x" << _n << std::endl << std::endl; } // display XX: if (!_XX) out << "XX = NULL" << std::endl; else { out << "XX = ["; for (i = 0 ; i < _n_XX ; ++i) { out << "\t"; for (j = 0 ; j < _n ; ++j) out << std::setw(15) << _XX[i][j] << " "; out << ((i==_n_XX-1) ? "]" : ";") << std::endl; } out << "size(XX)=" << _n_XX << "x" << _n << std::endl << std::endl; } // display models: out << std::endl; if (_tgp_models) { int m = _bbot.size(); for (i = 0 ; i < m ; ++i) { if (_tgp_models[i]) { std::ostringstream oss; oss << "model #" << i; out.open_block(oss.str()); _tgp_models[i]->display(out); out.close_block(); } else out << "model #" << i << ": NULL" << std::endl; out << std::endl; } } else out << "no models" << std::endl << std::endl; }
/*-------------------------------------------------------*/ void NOMAD::TGP_Model::display_X_errors(const NOMAD::Display &out) { if (_p == 0 || !_X) { out << "no interpolation points" << std::endl; return; } int i , j , m = _bbot.size(); NOMAD::Point min_err(m) , max_err(m) , avg_err(m,0.0) , sd_err(m,0.0); NOMAD::Eval_Point x(_n , m); double **err = new double * [_p]; for (i = 0 ; i < _p ; ++i) { err[i] = new double[m]; for (j = 0 ; j < _n ; ++j) x[j] = _X[i][j]; if (predict(x , true)) { for (j = 0 ; j < m ; ++j) if (_tgp_models[j]) { // relative error (in %) for point #i and output #j: err[i][j] = (x.get_bb_outputs()[j].rel_err((_tgp_models[j]->get_Z())[i]) * 100.0).value(); // out << "f=" << (_tgp_models[j]->get_Z())[i] << " " // << "m=" << x.get_bb_outputs()[j] << " err=" << err[i][j] // << std::endl; if (!min_err[j].is_defined() || err[i][j] < min_err[j].value()) min_err[j] = err[i][j]; if (!max_err[j].is_defined() || err[i][j] > max_err[j].value()) max_err[j] = err[i][j]; avg_err[j] += err[i][j]; } } else { for (j = 0 ; j <= i ; ++j) delete [] err[j]; delete [] err; out << "cannot predict interpolation errors (" << _error_str << ")" << std::endl; return; } } for (j = 0 ; j < m ; ++j) if (_tgp_models[j]) { // compute the median error: NOMAD::Double med_err; { if (_p == 1) med_err = err[0][j]; else if (_p == 2) med_err = (err[0][j] + err[1][j]) / 2.0; else { std::multiset<double> sorted_errors; for (i = 0 ; i < _p ; ++i) sorted_errors.insert(err[i][j]); std::multiset<double>::const_iterator it , end = sorted_errors.end(); --end; for (it = sorted_errors.begin() , i = 0 ; it != end ; ++it , ++i) { if (i == (_p+1)/2-1) { med_err = *it; if (_p%2==0) { ++it; med_err = (med_err + *it) / 2.0; } break; } } } } // compute the mean and the standard deviation: avg_err[j] /= _p; for (i = 0 ; i < _p ; ++i) sd_err[j] += (avg_err[j] - err[i][j]).pow2(); sd_err[j] = (sd_err[j] / _p).sqrt(); // display: if (m > 1) { std::ostringstream oss; oss << "output #" << j; if (_tgp_models[j]->is_fixed()) oss << " (fixed)"; else if (_tgp_models[j]->is_binary()) oss << " (binary)"; out << NOMAD::open_block(oss.str()); } out << "min : "; min_err[j].display(out , "%6.2f"); out << std::endl << "max : "; max_err[j].display(out , "%6.2f"); out << std::endl << "median: "; med_err.display(out , "%6.2f"); out << std::endl << "mean : "; avg_err[j].display(out , "%6.2f"); out << std::endl << "sd : "; sd_err[j].display(out , "%6.2f"); out << std::endl; if (m > 1) out.close_block(); } for (i = 0 ; i < _p ; ++i) delete [] err[i]; delete [] err; }
/*----------------------------------------------------------------*/ void NOMAD::TGP_Model::display_Ds2x(const NOMAD::Display &out , int display_limit) const { if (!_Ds2x || _n_XX == 0) { out << "matrix Ds2x has not been computed" << std::endl; return; } int i , j , k , m = _bbot.size(); NOMAD::Point *XX_pt; std::multiset<NOMAD::Model_Sorted_Point> pts; std::multiset<NOMAD::Model_Sorted_Point>::const_iterator it , end; for (i = 0 ; i < m ; ++i) { if (m > 1) { std::ostringstream oss; oss << "output #" << i; out << NOMAD::open_block(oss.str()); } if (_Ds2x[i]) { // 1. sort: for (j = 0 ; j < _n_XX ; ++j) { // construct a NOMAD::Point from matrix _XX: XX_pt = new NOMAD::Point(_fv); for (k = 0 ; k < _n0 ; ++k) if (_av_index[k] >= 0) (*XX_pt)[k] = _XX[j][_av_index[k]]; // insert this point in the sorted list: pts.insert(NOMAD::Model_Sorted_Point(XX_pt , -_Ds2x[i][j])); } // 2. display: end = pts.end(); for (j = 0 , it = pts.begin() ; it != end ; ++it , ++j) { if (display_limit <= 0 || j < display_limit) { out << "x=( "; it->get_point()->display(out , " " , 6 , -1); out << " ) Ds2x=" << it->get_dist()*-1.0 << std::endl; } else if (j == display_limit) out << "..." << std::endl; delete it->get_point(); } pts.clear(); } else out << "NULL" << std::endl; if (m > 1) out.close_block(); } }