5 * Created by Dario Izzo on 9/21/08.
6 * Copyright 2008 __MyCompanyName__. All rights reserved.
10 #include <boost/thread/locks.hpp>
11 #include <boost/thread/mutex.hpp>
20 #include "SolversThreads.h"
21 #include "population.h"
26 // Shortcut definition for the lock type.
27 typedef boost::unique_lock
<boost::mutex
> lock_type
;
29 //******************************************************************************************
31 //******************************************************************************************
33 void *DEthread(void *data
)
35 threadParam
*PtrTP
= (threadParam
*)data
;
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();
62 deme
= DE
.evolve(deme
, *problem
);
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
);
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
;
78 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
81 PtrTP
->exit
->notify_one();
90 //******************************************************************************************
92 //******************************************************************************************
94 void *MPSOthread(void *data
)
97 PtrTP
= (threadParam
*)data
;
102 vector
<double> LB
, UB
;
105 rng_double_type drng
;
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();
123 deme
= MPSO
.evolve(deme
, *problem
);
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
);
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
;
139 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
141 --*(PtrTP
->isActive
);
142 PtrTP
->exit
->notify_one();
147 //******************************************************************************************
149 //******************************************************************************************
151 void *PSOthread(void *data
)
154 PtrTP
= (threadParam
*)data
;
159 vector
<double> LB
,UB
;
162 rng_double_type drng
;
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();
180 deme
= PSO
.evolve(deme
, *problem
);
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
);
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
;
196 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
198 --*(PtrTP
->isActive
);
199 PtrTP
->exit
->notify_one();
206 //******************************************************************************************
208 //******************************************************************************************
210 void *SGAthread(void *data
)
213 PtrTP
= (threadParam
*)data
;
217 vector
<double> LB
,UB
;
220 rng_double_type drng
;
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();
239 deme
= SGA
.evolve(deme
, *problem
);
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
);
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
;
255 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
257 --*(PtrTP
->isActive
);
258 PtrTP
->exit
->notify_one();
264 //******************************************************************************************
266 //******************************************************************************************
268 void *ASAthread(void *data
)
271 PtrTP
= (threadParam
*)data
;
275 vector
<double> LB
,UB
;
278 rng_double_type drng
;
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
);
294 ASA
.initASA(PtrTP
->generations
,LB
.size(),PtrTP
->Ts
,PtrTP
->Tf
, rng());
297 oldfitness
= deme
.extractBestIndividual().getFitness();
300 deme
= ASA
.evolve(deme
[0], *problem
);
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
);
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
;
316 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
319 PtrTP
->exit
->notify_one();