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>
15 #include "SolversThreads.h"
16 #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
;
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();
60 deme
= DE
.evolve(deme
, *problem
);
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
);
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
;
76 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
78 *(PtrTP
->isActive
) = false;
79 PtrTP
->exit
->notify_one();
88 //******************************************************************************************
90 //******************************************************************************************
92 void *MPSOthread(void *data
)
95 PtrTP
= (threadParam
*)data
;
100 vector
<double> LB
, UB
;
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();
119 deme
= MPSO
.evolve(deme
, *problem
);
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
);
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
;
135 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
137 *(PtrTP
->isActive
) = false;
138 PtrTP
->exit
->notify_one();
143 //******************************************************************************************
145 //******************************************************************************************
147 void *PSOthread(void *data
)
150 PtrTP
= (threadParam
*)data
;
155 vector
<double> LB
,UB
;
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();
174 deme
= PSO
.evolve(deme
, *problem
);
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
);
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
;
190 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
192 *(PtrTP
->isActive
) = false;
193 PtrTP
->exit
->notify_one();
200 //******************************************************************************************
202 //******************************************************************************************
204 void *SGAthread(void *data
)
207 PtrTP
= (threadParam
*)data
;
211 vector
<double> LB
,UB
;
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();
231 deme
= SGA
.evolve(deme
, *problem
);
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
);
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
;
247 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
249 *(PtrTP
->isActive
) = false;
250 PtrTP
->exit
->notify_one();
256 //******************************************************************************************
258 //******************************************************************************************
260 void *ASAthread(void *data
)
263 PtrTP
= (threadParam
*)data
;
267 vector
<double> LB
,UB
;
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
);
284 ASA
.initASA(PtrTP
->generations
,LB
.size(),PtrTP
->Ts
,PtrTP
->Tf
, rng
.next());
287 oldfitness
= deme
.extractBestIndividual().getFitness();
290 deme
= ASA
.evolve(deme
[0], *problem
);
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
);
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
;
306 *(PtrTP
->Ptr_log
) << PtrTP
->generations
* deme
.size() << " " << (PtrTP
->Ptr_pop
->extractBestIndividual()).getFitness() << endl
;
308 *(PtrTP
->isActive
) = false;
309 PtrTP
->exit
->notify_one();