1 /*---------------------------------------------------------------*/
2 /*                    NOMAD for a DLL-black-box                  */
3 /*---------------------------------------------------------------*/
4 #include "nomad.hpp"
5 #include <windows.h>
6 using namespace std;
7 using namespace NOMAD;
8 
9 // black-box outputs (m):
10 // ----------------------
11 //    . the first one is the objective function
12 //    . all the others (1 to m-1) are PB constraints with format g(x) <= 0
13 
14 
15 /*------------------------------------------*/
16 /*               DLL functions              */
17 /*------------------------------------------*/
18 typedef int  (_stdcall *GET_NM)( void );
19 typedef void (_stdcall *EVAL_X)( double * , double * , bool * );
20 typedef void (_stdcall *INIT  )( void );
21 
22 GET_NM get_n_dll;
23 GET_NM get_m_dll;
24 EVAL_X eval_x_dll;
25 //INIT init_dll;
26 //INIT clear_dll;
27 
28 // simulate the DLL functions (for tests):
29 /*
30 void init  ( void ) {}
31 void clear ( void ) {}
32 int  get_n_dll  ( void ) { return 5; }
33 int  get_m_dll  ( void ) { return 3; }
34 void eval_x_dll ( double * x , double * fx , bool & cnt_eval ) {
35   double c1 = 0.0 , c2 = 0.0;
36   for ( int i = 0 ; i < 5 ; ++i ) {
37     c1 += pow ( x[i]-1 , 2 );
38     c2 += pow ( x[i]+1 , 2 );
39   }
40   fx[0] = x[4];
41   fx[1] = c1 - 25;
42   fx[2] = 25 - c2;
43   cnt_eval = true;
44 }
45 */
46 
47 /*--------------------------------------*/
48 /*            custom evaluator          */
49 /*--------------------------------------*/
50 class My_Evaluator : public Multi_Obj_Evaluator {
51 
52 private:
53 
54   int      _n;
55   int      _m;
56   double * _px;
57   double * _fx;
58 
59 public:
60 
61   // ctor:
My_Evaluator(const Parameters & p,int n,int m)62   My_Evaluator ( const Parameters & p , int n , int m )
63     : Multi_Obj_Evaluator ( p ) ,
64       _n        ( n               ) ,
65       _m        ( m               ) ,
66       _px       ( new double [_n] ) ,
67       _fx       ( new double [_m] )   {}
68 
69   // dtor:
~My_Evaluator(void)70   ~My_Evaluator ( void ) { delete [] _px; delete [] _fx; }
71 
72   // eval_x:
73   bool eval_x ( Eval_Point          & x        ,
74 		const NOMAD::Double & h_max    ,
75 		bool                & cnt_eval   ) const;
76 };
77 
78 // eval_x:
eval_x(Eval_Point & x,const NOMAD::Double & h_max,bool & cnt_eval) const79 bool My_Evaluator::eval_x ( Eval_Point          & x        ,
80 			    const NOMAD::Double & h_max    ,
81 			    bool                & cnt_eval   ) const {
82   int i;
83   for ( i = 0 ; i < _n ; ++i )
84     _px[i] = x[i].value();
85   eval_x_dll ( _px , _fx , &cnt_eval );
86   for ( i = 0 ; i < _m ; ++i )
87     x.set_bb_output ( i , _fx[i] );
88   return true;
89 }
90 
91 /*------------------------------------------*/
92 /*            NOMAD main function           */
93 /*------------------------------------------*/
main(int argc,char ** argv)94 int main ( int argc , char ** argv ) {
95 
96   // use:
97   if ( argc != 2 ) {
98     cerr << "\nuse: nomad_for_DLL.exe param.txt\n\n";
99     return 1;
100   }
101 
102   // NOMAD initializations:
103   begin ( argc , argv );
104 
105   // display:
106   NOMAD::Display out ( std::cout );
107   out.precision ( NOMAD::DISPLAY_PRECISION_STD );
108 
109   // random seed:
110   unsigned seed = 0;
111   srand(seed);
112 
113   // define DLL functions:
114   // get handle to dll:
115   HINSTANCE hdll = LoadLibrary ( TEXT("bb.dll") );
116   if ( !hdll ) {
117     cerr << "ERROR: unable to open bb.dll" << endl;
118 	FreeLibrary ( hdll );
119 	return 1;
120   }
121 
122   // get function pointers:
123   get_n_dll = (GET_NM)GetProcAddress(hdll, "GET_N");
124   if ( !get_n_dll ) {
125     cerr << "ERROR: unable to find GET_N function in dll" << endl;
126 	FreeLibrary ( hdll );
127 	return 1;
128   }
129 
130   get_m_dll = (GET_NM)GetProcAddress(hdll, "GET_M");
131   if ( !get_m_dll ) {
132     cerr << "ERROR: unable to find GET_M function in dll" << endl;
133 	FreeLibrary ( hdll );
134 	return 1;
135   }
136 
137   eval_x_dll = (EVAL_X)GetProcAddress(hdll, "EVAL_X");
138   if ( !eval_x_dll ) {
139     cerr << "ERROR: unable to find EVAL_X function in dll" << endl;
140 	FreeLibrary ( hdll );
141 	return 1;
142   }
143 
144  /* init_dll = (INIT)GetProcAddress(hdll, "INIT");
145   if ( !init_dll ) {
146     cerr << "ERROR: unable to find INIT function in dll" << endl;
147 	FreeLibrary ( hdll );
148 	return 1;
149   }
150 
151   clear_dll = (INIT)GetProcAddress(hdll, "CLEAR");
152   if ( !clear_dll ) {
153     cerr << "ERROR: unable to find CLEAR function in dll" << endl;
154 	FreeLibrary ( hdll );
155 	return 1;
156   }*/
157 
158   // MADS:
159   try {
160 
161     /* init_dll(); */
162 
163     int n = get_n_dll();
164     int m = get_m_dll();
165 
166     // parameters creation:
167     // --------------------
168     Parameters p ( out );
169 
170     // dimension:
171     p.set_DIMENSION ( n );
172 
173     // definition of output types:
174     vector<bb_output_type> bbot (m);
175     bbot[0] = bbot[1] = OBJ;
176     for ( int i = 2 ; i < m ; ++i )
177       bbot[i] = PB;
178     p.set_BB_OUTPUT_TYPE ( bbot );
179 
180     // read parameters file:
181     p.read ( argv[1] );
182 
183     // parameters check:
184     p.check();
185 
186     // display parameters:
187     // out << p << endl;
188 
189     // custom evaluator creation:
190     My_Evaluator ev ( p , n , m );
191 
192     // algorithm creation and execution:
193     Mads mads ( p , &ev );
194     mads.multi_run();
195 
196     // algorithm display:
197     // out << mads << endl;
198 
199     /* clear_dll(); */
200   }
201   catch ( exception & e ) {
202     cerr << "\nNOMAD has been interrupted (" << e.what() << ")\n\n";
203   }
204 
205   // Release the Dll:
206   FreeLibrary ( hdll );
207 
208   NOMAD::Slave::stop_slaves ( out );
209   NOMAD::end();
210 
211   return EXIT_SUCCESS;
212 }
213