Commit 65a3fb19 authored by Miroslav Shaltev's avatar Miroslav Shaltev
Browse files

Update to Nomad 3.7.2

parent fee81ae9
This diff is collapsed.
#ifndef __CACHE_SERVER__
#define __CACHE_SERVER__
#include "mpi.h"
#include "nomad.hpp"
using namespace NOMAD;
using namespace std;
// Cache server:
class Cache_Server : public Cache {
private:
int _rank; // process rank
int _np; // number of processes
bool _debug; // debug display flag
Clock _clock; // clock
Double _h_min; // h_min (min feasibility)
int _max_bbe; // max number of bb evaluations
const Eval_Point * _bf; // best points
const Eval_Point * _bi1;
const Eval_Point * _bi2;
Eval_Point * _stop_point; // stopping point
mutable int _multiple_evals; // number of multiple evaluations
mutable int _cache_hits; // number of cache hits
mutable int _cache_search_pts; // number of cache search points
Point ** _waited_pts; // list of points beeing evaluated
list<const Eval_Point*> * _clients_ext_pts; // replaces _extern_pts
// process the best feasible point signal:
void process_bf_signal ( int source ) const;
// process the extern point signal:
void process_ep_signal ( int source ) const;
// process the find signal:
void process_find_signal ( int source ) const;
// process the insertion signal:
void process_insert_signal ( int source );
// update and display the best points:
void update_best_points ( const Eval_Point & x , int source );
public:
static const int TAG_SIGNAL;
static const int TAG_CACHE_HIT;
static const int TAG_X1;
static const int TAG_X2;
static const int TAG_X3;
static const int TAG_X4;
static const int TAG_X5;
static const int TAG_X6;
static const int TAG_X7;
static const int TAG_BBOR;
static const int TAG_BBOC;
static const int TAG_NB_EP;
static const int TAG_EP;
static const int TAG_BF;
static char STOP_SIGNAL;
static char FIND_SIGNAL;
static char INSERT_SIGNAL;
static char NB_EP_SIGNAL;
static char EP_SIGNAL;
static char BF_SIGNAL;
// Constructor:
Cache_Server ( const Display & out ,
int rank ,
int np ,
const Double & h_min ,
int max_bbe ,
bool allow_multiple_evals ,
bool debug );
// Destructor:
virtual ~Cache_Server ( void );
// Start the server:
void start ( void );
// Stop the server:
void stop ( void ) const;
// Display the clients extern points:
void display_extern_pts ( void ) const { display_extern_pts(_out); }
void display_extern_pts ( const Display & out ) const;
// Display the best points:
void display_best_points ( void ) const { display_best_points(_out); }
void display_best_points ( const Display & out ) const;
// Display the current best solution:
void display_current_solution ( void ) const;
// Find a point:
virtual const Eval_Point * find ( const Eval_Point & x ) const;
// Insert a point:
virtual void insert ( const NOMAD::Eval_Point & x );
// Get the number of extern points:
virtual int get_nb_extern_points ( void ) const;
// Get and remove an extern point:
virtual const Eval_Point * get_and_remove_extern_point ( void ) const;
};
#endif
This diff is collapsed.
#ifndef __MASTER_SLAVES__
#define __MASTER_SLAVES__
#include "Cache_Server.hpp"
using namespace NOMAD;
using namespace std;
// Cache server:
class Master_Slaves {
private:
int _rank; // process rank
int _np; // number of processes
int _bbe; // max number of evaluations for each process
int _ns; // number of free variables for each process
Parameters & _p; // parameters
bool _debug; // debug display flag
Evaluator & _ev; // Evaluator
static const int TAG_SIGNAL;
static const int TAG_I1;
static const int TAG_I2;
static const int TAG_R1;
static const int TAG_D1;
static const int TAG_CSTOP;
static char STOP_SIGNAL;
static char OPTI_RES_SIGNAL;
static char OPTI_DATA_SIGNAL;
// Receive an optimization result from the pollster:
void receive_optimization_result ( int & pollster_mesh_index ,
bool & stop_algo ,
double *& best_feasible ,
double *& best_infeasible ,
int source ) const;
// Send an optimization result to the master:
void send_optimization_result ( int pollster_mesh_index ,
bool stop_algo ,
const Eval_Point * bf ,
const Eval_Point * bi ,
stop_type st ) const;
// Send optimization data from the master to a slave:
void send_optimization_data ( int pollster_mesh_index ,
bool stop_algo ,
const double * best_feasible ,
const double * best_infeasible ,
int source ) const;
// Receive optimization data from the master:
void receive_optimization_data ( bool & stop_algo ,
Point & x0 ,
Double & fx0 ) const;
void receive_optimization_data ( bool & stop_algo ,
Point & x0 ,
Double & fx0 ,
int & pollster_mesh_index ,
int * free_vars ) const;
// Check the initial mesh size values:
static bool check_delta ( const Point & delta );
public:
// Constructor:
Master_Slaves ( int rank ,
int np ,
int bbe ,
int ns ,
Parameters & p ,
bool debug ,
Evaluator &ev )
: _rank ( rank ) ,
_np ( np ) ,
_bbe ( bbe ) ,
_ns ( ns ) ,
_p ( p ) ,
_debug ( debug ) ,
_ev ( ev ){}
// Destructor:
virtual ~Master_Slaves ( void ) {}
// Start the master:
void start ( void ) const;
// Stop the master:
void stop ( void ) const;
// MADS run:
void mads_run ( Cache & cache );
};
#endif
/*-------------------------------------------------------------*/
/* PSD-MADS */
/*-------------------------------------------------------------*/
/* */
/* usage: */
/* */
/* "mpirun -np p psdmads param_file bbe ns" */
/* with p > 2, bbe > 0, and 1 <= ns <= number of variables */
/* . ns is the number of free variables for each process */
/* . bbe is the max number of evaluations for each process */
/* */
/*-------------------------------------------------------------*/
/* */
/* processes: */
/* */
/* 0: master */
/* 1: pollster slave (1 direction) */
/* 2 to p-2: regular slaves (2ns directions) */
/* p-1: cache server */
/* */
/*-------------------------------------------------------------*/
/* See the user guide for other details and the description */
/* of the algorithm */
/*-------------------------------------------------------------*/
/*-----------------------------------------------------------*/
#include "Master_Slaves.hpp"
using namespace std;
using namespace NOMAD;
const bool DEBUG = false;
/*---------------------------------------------------*/
/* The evaluator for G2_50 problem */
/*---------------------------------------------------*/
class My_Evaluator : public NOMAD::Evaluator {
public:
My_Evaluator ( const NOMAD::Parameters & p ) :
NOMAD::Evaluator ( p ) {}
~My_Evaluator ( void ) {}
bool eval_x ( NOMAD::Eval_Point & x ,
const NOMAD::Double & h_max ,
bool & count_eval ) const
{
int N=50;
long double sum1 = 0.0 , sum2 = 0.0 , sum3 = 0.0 , prod1 = 1.0 , prod2 = 1.0, g1=0, g2=0;
long double Xi;
for (int i = 0 ; i < N ; i++ )
{
Xi=x[i].value();
sum1 += pow ( cos(Xi) , 4 );
sum2 += Xi;
sum3 += (i+1)*Xi*Xi;
prod1 *= pow ( cos(Xi) , 2 );
prod2 *= Xi;
}
g1 = -prod2+0.75;
g2 = sum2 -7.5*N;
long double z = - fabs ( ( sum1 - 2 * prod1 ) / sqrt(sum3) );
x.set_bb_output ( 0 , g1 ); // constraint 1
x.set_bb_output ( 1 , g2 ); // constraint 2
x.set_bb_output ( 2 , z ); // objective value
count_eval = true; // count a black-box evaluation
return true; // the evaluation succeeded
}
};
/*-----------------------------------*/
/* main function */
/*-----------------------------------*/
int main ( int argc , char ** argv ) {
// MPI initialization:
MPI_Init ( &argc , &argv );
int rank , np;
MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
MPI_Comm_size ( MPI_COMM_WORLD, &np );
// check the arguments and the number of processes:
if ( np <= 2 || argc != 4 ) {
if ( rank == 0 )
cerr << "usage: mpirun -np p " << argv[0]
<< " param_file bbe ns, with p>2,"
<< " bbe>1, and 1<=ns<=n."
<< endl;
MPI_Finalize();
return 1;
}
// display:
Display out ( cout );
out.precision ( 16 );
// parameters:
NOMAD::Parameters p ( out );
int bbe = atoi ( argv[2] );
int ns = atoi ( argv[3] );
try {
// read the parameters file:
p.read ( argv[1] );
// This option is needed to have the same mesh index for all variables
p.set_ANISOTROPIC_MESH ( false );
// check the parameters:
p.check();
if ( ns < 1 || ns > p.get_dimension() )
throw Exception ( __FILE__ , __LINE__ ,
"Bad value for ns the number of free variables for each process" );
if ( p.get_nb_obj() > 1 )
throw Exception ( __FILE__ , __LINE__ ,
"PSD-MADS is not designed for multi-objective optimization" );
}
catch ( exception & e ) {
if ( rank == 0 )
cerr << "error with parameters" << endl;
MPI_Finalize();
return 1;
}
// custom evaluator creation:
My_Evaluator ev ( p );
// start the master:
Master_Slaves master_slaves ( rank , np , bbe , ns , p , DEBUG ,ev);
master_slaves.start();
// cache server:
Cache_Server cache ( out ,
rank ,
np ,
p.get_h_min() ,
p.get_max_bb_eval() ,
false , // ALLOW_MULTIPLE_EVALS
DEBUG );
// start the cache server:
if ( rank == np-1 ) {
if ( !DEBUG )
out << endl << "TIME\tBBE\tOBJ" << endl << endl;
cache.start();
}
// slaves: algorithm creation and execution:
if ( rank != 0 && rank != np-1 ) {
// MADS run:
master_slaves.mads_run ( cache );
// stop the master:
master_slaves.stop();
}
// stop the cache server:
cache.stop();
// display the final solution:
if ( !DEBUG && rank == np-1 )
cache.display_best_points ( out );
// MPI finalization:
MPI_Finalize();
return 0;
}
EXE = psdmads.exe
COMPILATOR = mpic++
SUNAME = $(shell uname)
OSS=$(findstring MINGW32,$(SUNAME))
ifneq "$(strip $(OSS))" ""
COMPILATOR = g++
endif
COMPILATOR_OPTIONS = -ansi -O2
L1 = $(NOMAD_HOME)/lib/nomad.a
LIBS = $(L1) -lm -lmpi
INCLUDE = -I$(NOMAD_HOME)/src -I.
COMPILE = $(COMPILATOR) $(COMPILATOR_OPTIONS) $(INCLUDE) -c
OBJS = main.o Cache_Server.o Master_Slaves.o
ifndef NOMAD_HOME
define ECHO_NOMAD
@echo Please set NOMAD_HOME environment variable!
@false
endef
endif
$(EXE): $(OBJS) $(L1)
$(ECHO_NOMAD)
$(COMPILATOR) -o $(EXE) $(OBJS) $(LIBS) $(COMPILATOR_OPTIONS)
main.o: main.cpp Master_Slaves.o
$(COMPILE) main.cpp
Master_Slaves.o: Master_Slaves.cpp Master_Slaves.hpp Cache_Server.o
$(ECHO_NOMAD)
$(COMPILE) Master_Slaves.cpp
Cache_Server.o: Cache_Server.cpp Cache_Server.hpp
$(ECHO_NOMAD)
$(COMPILE) Cache_Server.cpp
$(L1): ;
$(ECHO_NOMAD)
clean:
@echo "cleaning obj files"
@rm -f $(OBJS)
del:
@echo "cleaning trash files"
@rm -f core *~
@echo "cleaning obj files"
@rm -f $(OBJS)
@echo "cleaning exe file"
@rm -f $(EXE)
\ No newline at end of file
DIMENSION 50
BB_OUTPUT_TYPE PB PB OBJ
x0 x0.txt
LOWER_BOUND * 0
UPPER_BOUND * 10
max_bb_eval 5000
DIRECTION_TYPE ORTHO 2N
display_stats time bbe obj
initial_mesh_size r0.2
snap_to_bounds no
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
5.0
6.4149169921875 6.2413330078125 6.3720703125 6.1068115234375 3.0206298828125 0.585693359375 0.435791015625 0.391357421875 3.0928955078125 3.171875 0.173583984375 3.1939697265625 2.92333984375 6.1531982421875 2.8271484375 0.27392578125 3.1434326171875 0.511474609375 0.1761474609375 2.9149169921875 0.27294921875 0.4522705078125 3.0953369140625 3.0416259765625 0.3453369140625 0.275390625 2.8414306640625 3.1175537109375 3.0628662109375 3.126953125 3.0113525390625 3.0687255859375 0.182861328125 2.9844970703125 0.2940673828125 0.161865234375 2.83740234375 2.681884765625 0.2615966796875 1.843994140625 0.2763671875 0.1839599609375 0.3106689453125 0.1778564453125 0.330810546875 0.3873291015625 0.313720703125 0.44140625 0.2330322265625 0.290283203125
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment