Merge branch 'master' into boost.thread
[PaGMO.git] / SolversThreads / SolversThreads.cpp
blob9aaa17d92b96fd97b809e48370465e5acdb83b1d
1 /*
2 * SolverThreads.cpp
3 * PaGMO
5 * Created by Dario Izzo on 9/21/08.
6 * Copyright 2008 __MyCompanyName__. All rights reserved.
8 */
10 #include <boost/thread/locks.hpp>
11 #include <boost/thread/mutex.hpp>
12 #include <fstream>
13 #include <vector>
15 #include "SolversThreads.h"
16 #include "population.h"
17 #include "DE.h"
18 #include "PSO.h"
19 #include "MPSO.h"
20 #include "SGA.h"
21 #include "ASA.h"
22 #include "PkRandom.h"
24 using namespace std;
26 // Shortcut definition for the lock type.
27 typedef boost::unique_lock<boost::mutex> lock_type;
29 //******************************************************************************************
30 //DE thread type
31 //******************************************************************************************
33 void *DEthread(void *data)
35 threadParam *PtrTP = (threadParam *)data;
36 Population deme;
37 double oldfitness;
38 vector <int> picks;
39 GOProblem* problem;
40 vector<double> LB,UB;
41 DEalgorithm DE;
42 Pk::Random32 rng;
45 clock_t start,end;
46 double dif;
49 lock_type lock(*PtrTP->TPmutex);
50 rng = Pk::Random32(PtrTP->randomSeed);
51 deme = PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng);
52 problem = PtrTP->problem;
53 problem->getBounds(LB, UB);
54 DE.initDE(PtrTP->generations,LB.size(),PtrTP->F,PtrTP->CR,PtrTP->strategy, rng.next());
57 oldfitness = deme.extractBestIndividual().getFitness();
59 start=clock();
60 deme = DE.evolve(deme, *problem);
61 end=clock();
62 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
65 lock_type lock(*PtrTP->TPmutex);
66 //insert deme in main population
67 PtrTP->Ptr_pop->insertDeme(deme,picks);
68 //log in cout
69 cout << "Thread ID: " << PtrTP->threadID << endl ;
70 cout << "\t\t\tDE:\t\t\t F "<< PtrTP->F << "\t\tCR: " << PtrTP->CR << "\t\tStrategy: " << PtrTP->strategy << "\t\tGenerations: " << PtrTP->generations << endl;
71 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
72 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
73 cout << "\t\t\tSeconds elapsed: " << dif << endl;
74 cout << "\t\t\tShutting down" << endl;
75 //log in logfile
76 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
77 //sinal exit
78 *(PtrTP->isActive) = false;
79 PtrTP->exit->notify_one();
81 return 0;
88 //******************************************************************************************
89 //MPSO thread type
90 //******************************************************************************************
92 void *MPSOthread(void *data)
94 threadParam *PtrTP;
95 PtrTP = (threadParam *)data;
96 Population deme;
97 double oldfitness;
98 vector <int> picks;
99 GOProblem* problem;
100 vector<double> LB, UB;
101 MPSOalgorithm MPSO;
102 Pk::Random32 rng;
104 clock_t start,end;
105 double dif;
108 lock_type lock(*PtrTP->TPmutex);
109 rng = Pk::Random32(PtrTP->randomSeed);
110 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng);
111 problem = PtrTP->problem;
112 problem->getBounds(LB, UB);
113 MPSO.initMPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, PtrTP->nswarms, rng.next());
116 oldfitness = deme.extractBestIndividual().getFitness();
118 start=clock();
119 deme = MPSO.evolve(deme, *problem);
120 end=clock();
121 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
124 lock_type lock(*PtrTP->TPmutex);
125 //insert deme in main population
126 PtrTP->Ptr_pop->insertDeme(deme,picks);
127 //log in cout
128 cout << "Thread ID: " << PtrTP->threadID << endl ;
129 cout << "\t\t\tMPSO:\t\t omega "<< PtrTP->omega << "\t\teta1: " << PtrTP->eta1 << "\t\teta2: " << PtrTP->eta2 << "\t\tVcoeff: " << PtrTP->vcoeff<< "\t\tNswarms" <<PtrTP->vcoeff << "\t\tGenerations: " << PtrTP->generations << endl;
130 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
131 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
132 cout << "\t\t\tSeconds elapsed: " << dif << endl;
133 cout << "\t\t\tShutting down" << endl;
134 //log in logfile
135 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
136 //sinal exit
137 *(PtrTP->isActive) = false;
138 PtrTP->exit->notify_one();
140 return 0;
143 //******************************************************************************************
144 //PSO thread type
145 //******************************************************************************************
147 void *PSOthread(void *data)
149 threadParam *PtrTP;
150 PtrTP = (threadParam *)data;
151 Population deme;
152 double oldfitness;
153 vector <int> picks;
154 GOProblem* problem;
155 vector<double> LB,UB;
156 PSOalgorithm PSO;
157 Pk::Random32 rng;
159 clock_t start,end;
160 double dif;
163 lock_type lock(*PtrTP->TPmutex);
164 rng = Pk::Random32(PtrTP->randomSeed);
165 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng);
166 problem = PtrTP->problem;
167 problem->getBounds(LB, UB);
168 PSO.initPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, rng.next());
171 oldfitness = deme.extractBestIndividual().getFitness();
173 start=clock();
174 deme = PSO.evolve(deme, *problem);
175 end=clock();
176 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
179 lock_type lock(*PtrTP->TPmutex);
180 //insert deme in main population
181 PtrTP->Ptr_pop->insertDeme(deme,picks);
182 //log in cout
183 cout << "Thread ID: " << PtrTP->threadID << endl ;
184 cout << "\t\t\tPSO:\t\t omega "<< PtrTP->omega << "\t\teta1: " << PtrTP->eta1 << "\t\teta2: " << PtrTP->eta2 << "\t\tVcoeff: " << PtrTP->vcoeff << "\t\tGenerations: " << PtrTP->generations << endl;
185 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
186 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
187 cout << "\t\t\tSeconds elapsed: " << dif << endl;
188 cout << "\t\t\tShutting down" << endl;
189 //log in logfile
190 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
191 //sinal exit
192 *(PtrTP->isActive) = false;
193 PtrTP->exit->notify_one();
195 return 0;
200 //******************************************************************************************
201 //SGA thread type
202 //******************************************************************************************
204 void *SGAthread(void *data)
206 threadParam *PtrTP;
207 PtrTP = (threadParam *)data;
208 Population deme;
209 double oldfitness;
210 vector <int> picks;
211 vector<double> LB,UB;
212 SGAalgorithm SGA;
213 Pk::Random32 rng;
214 GOProblem* problem;
216 clock_t start,end;
217 double dif;
220 lock_type lock(*PtrTP->TPmutex);
221 rng = Pk::Random32(PtrTP->randomSeed);
222 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng);
223 problem = PtrTP->problem;
224 problem->getBounds(LB, UB);
225 SGA.initSGA(PtrTP->generations,LB.size(),PtrTP->CRsga,PtrTP->M,PtrTP->insert_best, rng.next());
228 oldfitness = deme.extractBestIndividual().getFitness();
230 start=clock();
231 deme = SGA.evolve(deme, *problem);
232 end=clock();
233 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
236 lock_type lock(*PtrTP->TPmutex);
237 //insert deme in main population
238 PtrTP->Ptr_pop->insertDeme(deme,picks);
239 //log in cout
240 cout << "Thread ID: " << PtrTP->threadID << endl ;
241 cout << "\t\t\tSGA:\t\t CR "<< PtrTP->CRsga << "\t\tM: " << PtrTP->M << "\t\tInsertBest: " << PtrTP->insert_best << "\t\tGenerations: " << PtrTP->generations << endl;
242 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
243 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
244 cout << "\t\t\tSeconds elapsed: " << dif << endl;
245 cout << "\t\t\tShutting down" << endl;
246 //log in logfile
247 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
248 //sinal exit
249 *(PtrTP->isActive) = false;
250 PtrTP->exit->notify_one();
252 return 0;
256 //******************************************************************************************
257 //SA-AN thread type
258 //******************************************************************************************
260 void *ASAthread(void *data)
262 threadParam *PtrTP;
263 PtrTP = (threadParam *)data;
264 Population deme;
265 double oldfitness;
266 vector <int> picks;
267 vector<double> LB,UB;
268 ASAalgorithm ASA;
269 Pk::Random32 rng;
270 GOProblem* problem;
273 clock_t start,end;
274 double dif;
277 lock_type lock(*PtrTP->TPmutex);
278 rng = Pk::Random32(PtrTP->randomSeed);
279 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng);
280 problem = PtrTP->problem;
281 problem->getBounds(LB, UB);
282 unsigned int temp;
283 temp = rng.next();
284 ASA.initASA(PtrTP->generations,LB.size(),PtrTP->Ts,PtrTP->Tf, rng.next());
287 oldfitness = deme.extractBestIndividual().getFitness();
289 start=clock();
290 deme = ASA.evolve(deme[0], *problem);
291 end=clock();
292 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
295 lock_type lock(*PtrTP->TPmutex);
296 //insert deme in main population
297 PtrTP->Ptr_pop->insertDeme(deme,picks);
298 //log in cout
299 cout << "Thread ID: " << PtrTP->threadID << endl ;
300 cout << "\t\t\tASA:\t\t\t Ts "<< PtrTP->Ts << "\t\tTf: " << PtrTP->Tf << "\t\tFunction Evaluations: " << PtrTP->generations << endl;
301 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
302 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
303 cout << "\t\t\tSeconds elapsed: " << dif << endl;
304 cout << "\t\t\tShutting down" << endl;
305 //log in logfile
306 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
307 //sinal exit
308 *(PtrTP->isActive) = false;
309 PtrTP->exit->notify_one();
311 return 0;