Start using atomic counters and set a CMake policy to the new (2.6) type.
[PaGMO.git] / SolversThreads / SolversThreads.cpp
blob73f118cd12879135785d4f1aa233b85f741644d6
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 "ASA.h"
16 #include "DE.h"
17 #include "MPSO.h"
18 #include "PSO.h"
19 #include "SGA.h"
20 #include "SolversThreads.h"
21 #include "population.h"
22 #include "rng.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 rng_uint32_type rng;
43 rng_double_type drng;
46 clock_t start,end;
47 double dif;
50 lock_type lock(*PtrTP->TPmutex);
51 rng.seed(PtrTP->randomSeed);
52 drng.seed(PtrTP->randomSeed);
53 deme = PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng);
54 problem = PtrTP->problem;
55 problem->getBounds(LB, UB);
56 DE.initDE(PtrTP->generations,LB.size(),PtrTP->F,PtrTP->CR,PtrTP->strategy, rng());
59 oldfitness = deme.extractBestIndividual().getFitness();
61 start=clock();
62 deme = DE.evolve(deme, *problem);
63 end=clock();
64 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
67 lock_type lock(*PtrTP->TPmutex);
68 //insert deme in main population
69 PtrTP->Ptr_pop->insertDeme(deme,picks);
70 //log in cout
71 cout << "Thread ID: " << PtrTP->threadID << endl ;
72 cout << "\t\t\tDE:\t\t\t F "<< PtrTP->F << "\t\tCR: " << PtrTP->CR << "\t\tStrategy: " << PtrTP->strategy << "\t\tGenerations: " << PtrTP->generations << endl;
73 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
74 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
75 cout << "\t\t\tSeconds elapsed: " << dif << endl;
76 cout << "\t\t\tShutting down" << endl;
77 //log in logfile
78 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
79 //sinal exit
80 --*(PtrTP->isActive);
81 PtrTP->exit->notify_one();
83 return 0;
90 //******************************************************************************************
91 //MPSO thread type
92 //******************************************************************************************
94 void *MPSOthread(void *data)
96 threadParam *PtrTP;
97 PtrTP = (threadParam *)data;
98 Population deme;
99 double oldfitness;
100 vector <int> picks;
101 GOProblem* problem;
102 vector<double> LB, UB;
103 MPSOalgorithm MPSO;
104 rng_uint32_type rng;
105 rng_double_type drng;
107 clock_t start,end;
108 double dif;
111 lock_type lock(*PtrTP->TPmutex);
112 rng.seed(PtrTP->randomSeed);
113 drng.seed(PtrTP->randomSeed);
114 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng);
115 problem = PtrTP->problem;
116 problem->getBounds(LB, UB);
117 MPSO.initMPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, PtrTP->nswarms, rng());
120 oldfitness = deme.extractBestIndividual().getFitness();
122 start=clock();
123 deme = MPSO.evolve(deme, *problem);
124 end=clock();
125 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
128 lock_type lock(*PtrTP->TPmutex);
129 //insert deme in main population
130 PtrTP->Ptr_pop->insertDeme(deme,picks);
131 //log in cout
132 cout << "Thread ID: " << PtrTP->threadID << endl ;
133 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;
134 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
135 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
136 cout << "\t\t\tSeconds elapsed: " << dif << endl;
137 cout << "\t\t\tShutting down" << endl;
138 //log in logfile
139 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
140 //sinal exit
141 --*(PtrTP->isActive);
142 PtrTP->exit->notify_one();
144 return 0;
147 //******************************************************************************************
148 //PSO thread type
149 //******************************************************************************************
151 void *PSOthread(void *data)
153 threadParam *PtrTP;
154 PtrTP = (threadParam *)data;
155 Population deme;
156 double oldfitness;
157 vector <int> picks;
158 GOProblem* problem;
159 vector<double> LB,UB;
160 PSOalgorithm PSO;
161 rng_uint32_type rng;
162 rng_double_type drng;
164 clock_t start,end;
165 double dif;
168 lock_type lock(*PtrTP->TPmutex);
169 rng.seed(PtrTP->randomSeed);
170 drng.seed(PtrTP->randomSeed);
171 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng);
172 problem = PtrTP->problem;
173 problem->getBounds(LB, UB);
174 PSO.initPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, rng());
177 oldfitness = deme.extractBestIndividual().getFitness();
179 start=clock();
180 deme = PSO.evolve(deme, *problem);
181 end=clock();
182 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
185 lock_type lock(*PtrTP->TPmutex);
186 //insert deme in main population
187 PtrTP->Ptr_pop->insertDeme(deme,picks);
188 //log in cout
189 cout << "Thread ID: " << PtrTP->threadID << endl ;
190 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;
191 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
192 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
193 cout << "\t\t\tSeconds elapsed: " << dif << endl;
194 cout << "\t\t\tShutting down" << endl;
195 //log in logfile
196 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
197 //sinal exit
198 --*(PtrTP->isActive);
199 PtrTP->exit->notify_one();
201 return 0;
206 //******************************************************************************************
207 //SGA thread type
208 //******************************************************************************************
210 void *SGAthread(void *data)
212 threadParam *PtrTP;
213 PtrTP = (threadParam *)data;
214 Population deme;
215 double oldfitness;
216 vector <int> picks;
217 vector<double> LB,UB;
218 SGAalgorithm SGA;
219 rng_uint32_type rng;
220 rng_double_type drng;
221 GOProblem* problem;
223 clock_t start,end;
224 double dif;
227 lock_type lock(*PtrTP->TPmutex);
228 rng.seed(PtrTP->randomSeed);
229 drng.seed(PtrTP->randomSeed);
230 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng);
231 problem = PtrTP->problem;
232 problem->getBounds(LB, UB);
233 SGA.initSGA(PtrTP->generations,LB.size(),PtrTP->CRsga,PtrTP->M,PtrTP->insert_best, rng());
236 oldfitness = deme.extractBestIndividual().getFitness();
238 start=clock();
239 deme = SGA.evolve(deme, *problem);
240 end=clock();
241 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
244 lock_type lock(*PtrTP->TPmutex);
245 //insert deme in main population
246 PtrTP->Ptr_pop->insertDeme(deme,picks);
247 //log in cout
248 cout << "Thread ID: " << PtrTP->threadID << endl ;
249 cout << "\t\t\tSGA:\t\t CR "<< PtrTP->CRsga << "\t\tM: " << PtrTP->M << "\t\tInsertBest: " << PtrTP->insert_best << "\t\tGenerations: " << PtrTP->generations << endl;
250 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
251 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
252 cout << "\t\t\tSeconds elapsed: " << dif << endl;
253 cout << "\t\t\tShutting down" << endl;
254 //log in logfile
255 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
256 //sinal exit
257 --*(PtrTP->isActive);
258 PtrTP->exit->notify_one();
260 return 0;
264 //******************************************************************************************
265 //SA-AN thread type
266 //******************************************************************************************
268 void *ASAthread(void *data)
270 threadParam *PtrTP;
271 PtrTP = (threadParam *)data;
272 Population deme;
273 double oldfitness;
274 vector <int> picks;
275 vector<double> LB,UB;
276 ASAalgorithm ASA;
277 rng_uint32_type rng;
278 rng_double_type drng;
279 GOProblem* problem;
282 clock_t start,end;
283 double dif;
286 lock_type lock(*PtrTP->TPmutex);
287 rng.seed(PtrTP->randomSeed);
288 drng.seed(PtrTP->randomSeed);
289 deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng);
290 problem = PtrTP->problem;
291 problem->getBounds(LB, UB);
292 unsigned int temp;
293 temp = rng();
294 ASA.initASA(PtrTP->generations,LB.size(),PtrTP->Ts,PtrTP->Tf, rng());
297 oldfitness = deme.extractBestIndividual().getFitness();
299 start=clock();
300 deme = ASA.evolve(deme[0], *problem);
301 end=clock();
302 dif = (double)(end-start) / (double)CLOCKS_PER_SEC;
305 lock_type lock(*PtrTP->TPmutex);
306 //insert deme in main population
307 PtrTP->Ptr_pop->insertDeme(deme,picks);
308 //log in cout
309 cout << "Thread ID: " << PtrTP->threadID << endl ;
310 cout << "\t\t\tASA:\t\t\t Ts "<< PtrTP->Ts << "\t\tTf: " << PtrTP->Tf << "\t\tFunction Evaluations: " << PtrTP->generations << endl;
311 cout << "\t\t\tInitial fitness: " << oldfitness << endl;
312 cout << "\t\t\tFinal fitness: " << deme.extractBestIndividual().getFitness() << endl;
313 cout << "\t\t\tSeconds elapsed: " << dif << endl;
314 cout << "\t\t\tShutting down" << endl;
315 //log in logfile
316 *(PtrTP->Ptr_log) << PtrTP->generations * deme.size() << " " << (PtrTP->Ptr_pop->extractBestIndividual()).getFitness() << endl;
317 //sinal exit
318 --*PtrTP->isActive;
319 PtrTP->exit->notify_one();
321 return 0;