/*----------------------------------------*/ void Master_Slaves::mads_run ( Cache & cache ) { const Eval_Point * best_feasible = NULL; const Eval_Point * best_infeasible = NULL; Double old_f = INF; bool stop_algo = false; int run_index = 0; int mesh_index = 0; double default_eps = Double::get_epsilon(); int n = _p.get_dimension(); int * free_vars = new int [_ns]; Point x0 ( n ) , delta_0 , delta_min; /*---------------------*/ /* main loop */ /*---------------------*/ while ( !stop_algo ) { best_feasible = NULL; best_infeasible = NULL; // random SEED: _p.set_SEED ( get_pid() + run_index ); // Halton seed: _p.set_HALTON_SEED ( 7999 + 99 * _rank + 20 * run_index ); // first run: if ( run_index == 0 ) { // max number of evaluations for regular slaves: if ( _rank != 1 ) _p.set_MAX_BB_EVAL ( _bbe ); // display: // p.set_DISPLAY_STATS ( "process #" + itos(_rank) + " BBE OBJ" ); // p.set_DISPLAY_DEGREE ( FULL_DISPLAY ); _p.set_DISPLAY_DEGREE ( NO_DISPLAY ); } // force the parameters check: _p.force_check_flag(); /*------------------*/ /* pollster slave */ /*------------------*/ if ( _rank == 1 ) { stop_type stop_reason = UNKNOWN_STOP_REASON; Mesh::get_delta_m ( delta_0 , _initial_mesh_size , _mesh_update_basis , _initial_mesh_index , mesh_index ); // we set a very small epsilon in order to accept // very small initial mesh sizes: Double::set_epsilon ( 1e-16 ); if ( !check_delta ( delta_0 ) ) stop_algo = true; else { // first run: if ( run_index == 0 ) { // directions: { int halton_seed = _p.get_halton_seed(); bool use_orthomads = _p.has_orthomads_directions(); _p.reset_directions ( halton_seed ); _p.set_DIRECTION_TYPE ( (use_orthomads) ? ORTHO_1 : LT_1 ); } // cache search: _p.set_CACHE_SEARCH ( true ); _p.set_OPPORTUNISTIC_CACHE_SEARCH ( false ); } // other runs: else { // stop_algo may be set to 'false' here: receive_optimization_data ( stop_algo , x0 , old_f ); // starting point: _p.reset_X0(); _p.set_X0 ( x0 ); } if ( !stop_algo ) { // pollster mesh: _p.set_INITIAL_MESH_INDEX ( mesh_index ); _p.set_MAX_MESH_INDEX ( mesh_index ); _p.set_INITIAL_MESH_SIZE ( delta_0 ); _p.set_MIN_MESH_SIZE ( delta_0 ); // check the parameters: _p.check(); Double::set_epsilon ( default_eps ); Mads mads ( _p , NULL , NULL , &cache , NULL ); stop_reason = mads.run(); best_feasible = mads.get_best_feasible(); best_infeasible = mads.get_best_infeasible(); bool success = false; if ( best_feasible ) { success = (best_feasible->get_f() < old_f); if ( _debug ) _p.out() << "POLLSTER: ELL=" << mesh_index << " BBE=" << mads.get_stats().get_bb_eval() << " OLD_F=" << old_f << " NEW_F=" << best_feasible->get_f() << " SUCCESS=" << success << endl; } // pollster mesh update: if ( success ) --mesh_index; else ++mesh_index; } } send_optimization_result ( mesh_index , stop_algo , best_feasible , best_infeasible , stop_reason ); } /*------------------*/ /* regular slaves */ /*------------------*/ else { int i , j , pollster_mesh_index; receive_optimization_data ( stop_algo , x0 , old_f , pollster_mesh_index , free_vars ); if ( _debug ) { _p.out() << "SLAVE #" << _rank << ": OPTIM. DATA: [STOP=" << stop_algo << "] [POLLSTER_MESH_INDEX=" << pollster_mesh_index << "] [X0=" << x0 << " ] [f(X0)=" << old_f << "] [FREE VARS= "; for ( i = 0 ; i < _ns ; ++i ) _p.out() << free_vars[i] << " "; _p.out() << " ]" << endl; } if ( !stop_algo ) { // starting point: _p.reset_X0(); _p.set_X0 ( x0 ); // mesh of the regular slave: int ell_0 = 0; if ( pollster_mesh_index < ell_0 ) ell_0 = pollster_mesh_index; Mesh::get_delta_m ( delta_0 , _initial_mesh_size , _mesh_update_basis , _initial_mesh_index , ell_0 ); Mesh::get_delta_m ( delta_min , _initial_mesh_size , _mesh_update_basis , _initial_mesh_index , pollster_mesh_index ); Double::set_epsilon ( 1e-16 ); if ( !check_delta ( delta_0 ) || !check_delta ( delta_min ) ) stop_algo = true; else { // mesh of the regular slave: _p.set_INITIAL_MESH_INDEX ( ell_0 ); _p.set_MAX_MESH_INDEX ( pollster_mesh_index ); _p.set_INITIAL_MESH_SIZE ( delta_0 ); _p.set_MIN_MESH_SIZE ( delta_min ); // free variables: { _p.reset_fixed_variables(); bool fix_var; for ( i = 0 ; i < n ; ++i ) { fix_var = true; for ( j = 0 ; j < _ns ; ++j ) if ( free_vars[j] == i ) { fix_var = false; break; } if ( fix_var ) _p.set_FIXED_VARIABLE ( i ); } } // check the parameters: _p.check(); Double::set_epsilon ( default_eps ); // MADS run: Mads mads ( _p , NULL , NULL , &cache , NULL ); mads.run(); best_feasible = mads.get_best_feasible(); best_infeasible = mads.get_best_infeasible(); if ( _debug && best_feasible ) { _p.out() << "RANK #" << _rank << ": POLLSTER_ELL=" << pollster_mesh_index << " VARS = ["; for ( i = 0 ; i < _ns ; ++i ) _p.out() << free_vars[i] << " "; _p.out() << " ] BBE=" << mads.get_stats().get_bb_eval() << " OLD_F=" << old_f << " NEW_F=" << best_feasible->get_f() << " SUCCESS=" << (best_feasible->get_f() < old_f) << endl; } } } { int tmp1 = -1; bool tmp2 = false; stop_type tmp3 = UNKNOWN_STOP_REASON; send_optimization_result ( tmp1 , tmp2 , best_feasible , best_infeasible , tmp3 ); } } // loop increment: ++run_index; } delete [] free_vars; }
/*------------------------------------------*/ int main ( int argc , char ** argv ) { // display: Display out ( std::cout ); out.precision ( DISPLAY_PRECISION_STD ); // NOMAD initializations: begin ( argc , argv ); try { // usage: if ( argc < 2 ) { display_usage ( cerr ); return EXIT_SUCCESS; } // parameters file: string param_file_name = argv[1]; string opt = param_file_name; NOMAD::toupper(opt); // display version if option '-v' has been specified: if ( opt == "-V" ) { display_version ( out ); return EXIT_SUCCESS; } // display info if option '-i' has been specified: if ( opt == "-I" || opt == "-INFO" ) { display_info ( out ); display_usage ( out ); return EXIT_SUCCESS; } // parameters creation: Parameters p ( out ); // display help on parameters if option '-h' has been specified: if ( opt == "-H" || opt == "-HELP" ) { p.help( (argc>2) ? argv[2] : "all" ); return EXIT_SUCCESS; } // read parameters file: p.read ( param_file_name ); // parameters check: p.check(); // display NOMAD info: if ( p.get_display_degree() > 1 ) display_info ( out ); // parameters display: if ( p.get_display_degree() > 2 ) out << endl << "parameters:" << endl << p << endl; // single-objective: if ( p.get_nb_obj() == 1 ) { // custom evaluator: My_Evaluator ev ( p ); // algorithm creation and execution: Mads mads ( p , &ev ); mads.run(); // plot the last point: const Eval_Point * bf = mads.get_best_feasible(); if ( bf ) ev.plot_success ( mads.get_stats().get_bb_eval() , bf->get_f() ); } // bi-objective: else { // custom evaluator: My_Multi_Obj_Evaluator ev ( p ); // algorithm creation and execution: Mads mads ( p , &ev ); mads.multi_run(); } } catch ( exception & e ) { cerr << "\nNOMAD has been interrupted (" << e.what() << ")\n\n"; } GUI_wait(); Slave::stop_slaves ( out ); end(); return EXIT_SUCCESS; }