1 #include <boost/thread/condition_variable.hpp>
2 #include <boost/thread/locks.hpp>
3 #include <boost/thread/mutex.hpp>
8 #include <ctime> //for time()
9 #include "population.h"
16 #include "TrajectoryProblems.h"
17 #include "ClassicProblems.h"
19 #include "SolversThreads.h"
25 typedef boost::unique_lock
<boost::mutex
> lock_type
;
26 typedef messengerfullProb problem_type
;
30 int D
=0; //Problem dimension will be assigned later
31 int choice
=0; //User choice
34 //We prepare the pseudorandom sequence (TODO: check the randomnumbers of different threads are different)
36 rng_uint32_type
rng(time(0));
40 //we extract its information into local variables
41 const vector
<double>& LB
= problem
.getLB();
42 const vector
<double>& UB
= problem
.getUB();
43 D
= problem
.getDimension();
45 //we declare populations and individuals
46 Population demeDE
,demePSO
,demeASA
,demeLOCA
,demeSGA
,pop
;
48 vector
<int> picksDE
,picksPSO
,picksASA
,picksLOCAL
,picksSGA
;
50 time_t start
,end
,start1
,end1
;
53 //We create and open the logfile
54 ofstream
logfile("log.txt");
58 while (choice
!= -1) {
60 //we choose the algorithm
62 cout
<< "Choose: 1-ASA, 2-PSO, 3-MPSO, 4-DE, 5-SGA, 6-IslandModel(ring), 7-DiGMO(simulator): ";
69 int NP
= 1; //population size
70 int trials
= 300; //number of trials
71 int niterTot
= 10000; //generations per algorithm call
80 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
83 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
84 vector
<double> results
;
86 //Instanciate the algorithm
87 //Adaptive Simulated Annealing
89 //ASA.initASA(niterTot,niterTemp,niterRange,LB.size(),T0,Tcoeff,StartStep, rng());
90 ASA
.initASA(niterTot
,LB
.size(),T0
,Tf
, rng());
94 for (int i
=0;i
<trials
;i
++){
95 cout
<< "\nTrial number #" << i
+1 << endl
;
96 //we create a random population
97 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
98 pop
.evaluatePopulation(problem
);
102 while( iter
< itermax
){
105 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
108 if (pop
.extractBestIndividual().getFitness() < 5){
109 ASA
.initASA(niterTot
,LB
.size(),1,0.01, rng());
111 pop
= ASA
.evolve(pop
[0],problem
);
113 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
114 //we print the result
115 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
116 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
117 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
118 cout
<< "Std : " << pop
.evaluateStd() << endl
;
119 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
122 results
.push_back(pop
.extractBestIndividual().getFitness());
123 dif
= difftime(end
,start
);
124 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
125 for (int i
=0;i
<D
;i
++){
126 logfile
<< pop
.extractBestIndividual()[i
] << " ";
128 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
131 //evaluate experiment results
132 for (int i
=0;i
<trials
;i
++){
134 if (results
[i
] > max
) max
= results
[i
];
135 if (results
[i
] < min
) min
= results
[i
];
139 for (int i
=0;i
<trials
;i
++){
140 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
142 dev
= sqrt(dev
/trials
);
144 for (int i
=0;i
<trials
;i
++){
145 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
150 cout
<< "\nMean: " << mean
<< endl
;
151 cout
<< "Std: " << dev
<< endl
;
152 cout
<< "Max: " << max
<< endl
;
153 cout
<< "Min: " << min
<< endl
;
158 case 2: //PSO sequential experiment
160 //Experiment Settings
161 int NP
= 20; //population size
162 int trials
= 100; //number of trials
163 int gen
= 500; //generations per algorithm call
170 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
171 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
174 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
175 vector
<double> results
;
177 //Instanciate the algorithm
179 PSO
.initPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
, rng());
181 for (int i
=0;i
<trials
;i
++){
182 cout
<< "\nTrial number #" << i
+1 << endl
;
183 //we create a random population
184 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
185 pop
.evaluatePopulation(problem
);
189 while(iter
< itermax
){
192 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
195 pop
= PSO
.evolve(pop
,problem
);
197 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
198 //we print the result
199 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
200 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
201 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
202 cout
<< "Std : " << pop
.evaluateStd() << endl
;
203 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
206 results
.push_back(pop
.extractBestIndividual().getFitness());
207 dif
= difftime(end
,start
);
208 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
211 //evaluate experiment results
212 for (int i
=0;i
<trials
;i
++){
214 if (results
[i
] > max
) max
= results
[i
];
215 if (results
[i
] < min
) min
= results
[i
];
219 for (int i
=0;i
<trials
;i
++){
220 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
222 dev
= sqrt(dev
/trials
);
224 for (int i
=0;i
<trials
;i
++){
225 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
230 cout
<< "\nMean: " << mean
<< endl
;
231 cout
<< "Std: " << dev
<< endl
;
232 cout
<< "Max: " << max
<< endl
;
233 cout
<< "Min: " << min
<< endl
;
237 case 3: //MPSO sequential experiment
239 //Experiment Settings
240 int NP
= 20; //population size
241 int trials
= 100; //number of trials
242 int gen
= 500; //generations per algorithm call
250 int itermax
= 100; //Maximum number of iterations allowed (i.e. output printed on the screen)
251 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
254 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
255 vector
<double> results
;
257 //Instanciate the algorithm
259 MPSO
.initMPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
,nswarms
, rng());
261 for (int i
=0;i
<trials
;i
++){
262 cout
<< "\nTrial number #" << i
+1 << endl
;
263 //we create a random population
264 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
265 pop
.evaluatePopulation(problem
);
269 while(iter
< itermax
){
272 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
275 pop
= MPSO
.evolve(pop
,problem
);
277 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
278 //we print the result
279 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
280 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
281 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
282 cout
<< "Std : " << pop
.evaluateStd() << endl
;
283 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
286 results
.push_back(pop
.extractBestIndividual().getFitness());
287 dif
= difftime(end
,start
);
288 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
291 //evaluate experiment results
292 for (int i
=0;i
<trials
;i
++){
294 if (results
[i
] > max
) max
= results
[i
];
295 if (results
[i
] < min
) min
= results
[i
];
299 for (int i
=0;i
<trials
;i
++){
300 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
302 dev
= sqrt(dev
/trials
);
304 for (int i
=0;i
<trials
;i
++){
305 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
310 cout
<< "\nMean: " << mean
<< endl
;
311 cout
<< "Std: " << dev
<< endl
;
312 cout
<< "Max: " << max
<< endl
;
313 cout
<< "Min: " << min
<< endl
;
319 case 4: //Sequential Differential Evolution Experiment
321 //Experiment Settings
322 int NP
= 20; //population size for each island
323 int trials
= 100; //number of trials
324 int gen
= 500; //generations per algorithm call
325 double F
= 0.8; //F in DE
326 double CR
= 0.8; //CR in DE
327 int strategy
= 2; //DE startegy
330 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
333 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
334 vector
<double> results
;
336 //Instanciate the algorithm
338 DE
.initDE(gen
,LB
.size(),F
,CR
,strategy
, rng());
340 for (int i
=0;i
<trials
;i
++){
341 cout
<< "\nTrial number #" << i
+1 << endl
;
342 //we create a random population
345 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
346 pop
.evaluatePopulation(problem
);
350 while( iter
< itermax
){
353 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
356 pop
= DE
.evolve(pop
,problem
);
358 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
359 //we print the result
360 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
361 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
362 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
363 cout
<< "Std : " << pop
.evaluateStd() << endl
;
364 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
368 //results.push_back(iter*NP*gen);
369 results
.push_back(pop
.extractBestIndividual().getFitness());
371 dif
= difftime(end
,start
);
372 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
373 for (int i
=0;i
<D
;i
++){
374 logfile
<< pop
.extractBestIndividual()[i
] << " ";
376 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
379 //evaluate experiment results
380 for (unsigned int i
=0;i
<results
.size();i
++){
382 if (results
[i
] > max
) max
= results
[i
];
383 if (results
[i
] < min
) min
= results
[i
];
385 mean
/= results
.size();
387 for (unsigned int i
=0;i
<results
.size();i
++){
388 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
390 dev
= sqrt(dev
/results
.size());
392 for (unsigned int i
=0;i
<results
.size();i
++){
393 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
397 cout
<< "\nMean: " << mean
<< endl
;
398 cout
<< "Std: " << dev
<< endl
;
399 cout
<< "Max: " << max
<< endl
;
400 cout
<< "Min: " << min
<< endl
;
401 cout
<< "Success: " << results
.size() << endl
;
407 case 5: //SGA experiment
409 //Experiment Settings
410 int NP
= 20; //population size
411 int trials
= 100; //number of trials
412 int gen
= 500; //generations per algorithm call
418 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
421 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
422 vector
<double> results
;
424 //Instanciate the algorithm
425 //Simple Genetic Algorithm
427 SGA
.initSGA(gen
,LB
.size(),CR
,M
,insert_best
, rng());
429 for (int i
=0;i
<trials
;i
++){
430 cout
<< "\nTrial number #" << i
+1 << endl
;
431 //we create a random population
432 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
433 pop
.evaluatePopulation(problem
);
437 while( iter
< itermax
){
440 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
443 pop
= SGA
.evolve(pop
,problem
);
445 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
446 //we print the result
447 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
448 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
449 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
450 cout
<< "Std : " << pop
.evaluateStd() << endl
;
451 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
454 results
.push_back(pop
.extractBestIndividual().getFitness());
455 dif
= difftime(end
,start
);
456 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
459 //evaluate experiment results
460 for (int i
=0;i
<trials
;i
++){
462 if (results
[i
] > max
) max
= results
[i
];
463 if (results
[i
] < min
) min
= results
[i
];
467 for (int i
=0;i
<trials
;i
++){
468 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
470 dev
= sqrt(dev
/trials
);
472 for (int i
=0;i
<trials
;i
++){
473 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
478 cout
<< "\nMean: " << mean
<< endl
;
479 cout
<< "Std: " << dev
<< endl
;
480 cout
<< "Max: " << max
<< endl
;
481 cout
<< "Min: " << min
<< endl
;
486 case 6: //Parallel asynchronous island model
489 //Experiment Settings
490 int NP
= 1; //population size for each island
491 int trials
= 20; //number of trials
492 int gen
= 500; //generations per algorithm call
493 double F
= 0.8; //F in DE
494 double CR
= 0.8; //CR in DE
495 int strategy
= 2; //DE startegy
496 int islandsN
= 8; //Number of Islands
499 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
502 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
503 vector
<double> results
;
505 //Main cycle creating threads
506 for (int i
=0;i
<trials
;i
++){
507 cout
<< "\nTrial number #" << i
+1 << endl
;
508 //We create a random population in each island (this is done by the main thread - i.e. no parallelization here)
509 vector
<Population
> IslandPop(islandsN
);
510 vector
<GOProblem
*> parallelProblems(islandsN
);
511 for (int i
=0;i
<islandsN
;i
++){
512 parallelProblems
[i
] = new problem_type();
514 IslandPop
[i
].createRandomPopulation(LB
,UB
,NP
, rng
);
515 IslandPop
[i
].evaluatePopulation(*parallelProblems
[i
]);
518 //We instanciate the objects needed for pthreads allocating memory for the threads array
520 threads
= new pthread_t
[islandsN
];
522 boost::condition_variable exitcond
;
524 //We allocate memory for the isActive array containing the status ofeach thread and we initialise it to false (no threads opened)
526 isActive
= new bool[islandsN
];
527 for (int i
=0;i
<islandsN
;i
++) isActive
[i
]=false; //all threads are inactive
529 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
531 data
= new threadParam
[islandsN
];
532 for (int i
=0;i
<islandsN
;i
++){
533 data
[i
].threadID
= i
;
535 data
[i
].problem
= parallelProblems
[i
];
536 data
[i
].Ptr_pop
= &IslandPop
[i
]; //Each Island has a different population
540 data
[i
].generations
= gen
;
542 //Initialise DE specific data
543 data
[i
].strategy
= 1;
547 //Initialise PSO/MPSO specific data
548 data
[i
].omega
= 0.65;
554 //Initialise SGA specific data
555 data
[i
].CRsga
= 0.95;
557 data
[i
].insert_best
=1;
559 //Initialise ASA specific data
561 data
[i
].Tf
= data
[i
].Ts
/1000;
562 data
[i
].isActive
= &isActive
[i
];
563 data
[i
].TPmutex
= &mutex
;
564 data
[i
].exit
= &exitcond
;
566 data
[i
].Ptr_log
= &logfile
;
572 double loweststd
= 1000000;
575 lock_type
lock(mutex
);
577 while(iter
< itermax
){
578 for (int i
=0;i
<islandsN
;i
++){
579 if ( !*(data
[i
].isActive
) ){
580 //Create again the i-th thread to simulate an Island
582 if (IslandType
== 0){
583 data
[i
].randomSeed
= rng();
584 cout
<< "\t\t\tPSO:\t\t omega: "<< data
[i
].omega
<< "\t\teta1: " << data
[i
].eta1
<< "\t\teta2: " << data
[i
].eta2
<< "\t\tVcoeff: " << data
[i
].vcoeff
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
585 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
587 else if (IslandType
== 1){
588 data
[i
].randomSeed
= rng();
589 cout
<< "\t\t\tMPSO:\t\t omega: "<< data
[i
].omega
<< "\t\teta1: " << data
[i
].eta1
<< "\t\teta2: " << data
[i
].eta2
<< "\t\tVcoeff: " << data
[i
].vcoeff
<< "\t\tNswamrs: " << data
[i
].nswarms
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
590 rc
= pthread_create(&threads
[i
], NULL
, MPSOthread
, (void *)&data
[i
]);
592 else if (IslandType
== 2){
593 data
[i
].randomSeed
= rng();
594 cout
<< "\t\t\tSGA:\t\t CR: "<< data
[i
].CRsga
<< "\t\tM: " << data
[i
].M
<< "\t\tInsertBest: " << data
[i
].insert_best
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
595 rc
= pthread_create(&threads
[i
], NULL
, SGAthread
, (void *)&data
[i
]);
597 else if (IslandType
== 3){
598 data
[i
].randomSeed
= rng();
599 data
[i
].generations
=10000;
601 cout
<< "\t\t\tASA:\t\t Ts: "<< data
[i
].Ts
<< "\t\tTf: " << data
[i
].Tf
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
602 rc
= pthread_create(&threads
[i
], NULL
, ASAthread
, (void *)&data
[i
]);
605 data
[i
].randomSeed
= rng();
606 data
[i
].strategy
= strategy
;
607 cout
<< "\t\t\tDE: \t\t F: "<< data
[i
].F
<< "\t\tCR: " << data
[i
].CR
<< "\t\tStrategy: " << data
[i
].strategy
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
608 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
611 //Problem creating the thread
612 printf("ERROR; return code from pthread_create() is %d\n", rc
);
616 //Thread Successfully Created
618 *(data
[i
].isActive
) = true;
619 //thread is detached as it will never be joined
620 //(on OSx Leopard this avoids that at a certain point
621 //threads cannot be created anymore resulting in rc=35)
622 pthread_detach(threads
[i
]);
625 //evaluate highest standard deviation across Islands
626 //loweststd = IslandPop[0].evaluateStd();
627 loweststd
= IslandPop
[0].extractBestIndividual().getFitness();
628 for (int i2
=1;i2
<islandsN
;i2
++){
629 if (loweststd
< IslandPop
[i2
].extractBestIndividual().getFitness()) loweststd
= IslandPop
[i2
].extractBestIndividual().getFitness();
632 //log islands best and std
633 for (int i2
=0;i2
<islandsN
;i2
++){
634 cout
<< "CPU#:" << i2
<< ": " << IslandPop
[i2
].extractBestIndividual().getFitness() << " " << IslandPop
[i2
].evaluateStd() << endl
;
636 cout
<< "HighestStd: " << loweststd
<< endl
;
637 cout
<< "Iter: " << iter
+1 << endl
;
638 cout
<< "\nmain():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
640 //ring topology migration
641 if (Pk::nextDouble(rng
) < 0.2){
642 IslandPop
[(i
+1) % islandsN
].substituteIndividual(IslandPop
[i
].extractBestIndividual(), rng() % data
[i
].NP
);
650 //The main cycle has finished: we wait for all threads to finish
651 for (int i
=0; i
<islandsN
;i
++){
652 while (*data
[i
].isActive
); //infinite loop if a thread never ends
660 dif
= difftime(end
,start
);
661 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
663 double res
=IslandPop
[0].extractBestIndividual().getFitness();
664 for (int i
=1;i
<islandsN
;i
++){
665 if (res
> IslandPop
[i
].extractBestIndividual().getFitness()) res
= IslandPop
[i
].extractBestIndividual().getFitness();
668 results
.push_back(res
);
671 for (int i
= 0; i
< islandsN
; i
++) {
672 delete parallelProblems
[i
];
676 //evaluate experiment results
677 for (unsigned int i
=0;i
<results
.size();i
++){
679 if (results
[i
] > max
) max
= results
[i
];
680 if (results
[i
] < min
) min
= results
[i
];
682 mean
/= results
.size();
684 for (unsigned int i
=0;i
<results
.size();i
++){
685 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
687 dev
= sqrt(dev
/results
.size());
689 for (unsigned int i
=0;i
<results
.size();i
++){
690 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
694 cout
<< "\nMean: " << mean
<< endl
;
695 cout
<< "Std: " << dev
<< endl
;
696 cout
<< "Max: " << max
<< endl
;
697 cout
<< "Min: " << min
<< endl
;
698 cout
<< "Success: " << results
.size() << endl
;
705 //Experiment Settings
706 int NP
= 200; //main population size
707 int trials
= 20; //number of trials
708 int gen
= 500; //generations per algorithm call
709 double F
= 0.8; //F in DE
710 double CR
= 0.8; //CR in DE
711 int strategy
= 2; //DE startegy
712 int islandsN
= 1; //Number of Islands
715 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
718 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
719 vector
<double> results
;
721 //Main cycle creating threads
722 for (int i
=0;i
<trials
;i
++){
723 cout
<< "\nTrial number #" << i
+1 << endl
;
724 //We create a random population in each island (this is done by the main thread - i.e. no parallelization here)
725 Population IslandPop
;
726 vector
<GOProblem
*> parallelProblems(islandsN
);
727 for (int i
=0;i
<islandsN
;i
++) { parallelProblems
[i
] = new problem_type(); } //It is necessary to create a different object per CPU as to make sure to access different memory
728 //locations when the problem is mga or mga-1dsm for th r,v,and DV variables in the mgaproblem structures
729 IslandPop
.createRandomPopulation(LB
,UB
,NP
, rng
);
730 IslandPop
.evaluatePopulation(*parallelProblems
[0]); //all the problems are identical.... evaluation is done for the [0] one.
733 //We instanciate the objects needed for pthreads allocating memory for the threads array
735 threads
= new pthread_t
[islandsN
];
737 boost::condition_variable exitcond
;
739 //We allocate memory for the isActive array containing the status ofeach thread and we initialise it to false (no threads opened)
741 isActive
= new bool[islandsN
];
742 for (int i
=0;i
<islandsN
;i
++) isActive
[i
]=false; //all threads are inactive
744 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
746 data
= new threadParam
[islandsN
];
747 for (int i
=0;i
<islandsN
;i
++){
748 data
[i
].threadID
= i
;
750 data
[i
].problem
= parallelProblems
[i
];
751 data
[i
].Ptr_pop
= &IslandPop
; //Only one population in DiGMO
755 data
[i
].generations
= gen
;
757 //Initialise DE specific data
758 data
[i
].strategy
= 1;
762 //Initialise PSO/MPSO specific data
763 data
[i
].omega
= 0.65;
769 //Initialise SGA specific data
770 data
[i
].CRsga
= 0.95;
772 data
[i
].insert_best
=1;
774 //Initialise ASA specific data
776 data
[i
].Tf
= data
[i
].Ts
/1000;
777 data
[i
].isActive
= &isActive
[i
];
778 data
[i
].TPmutex
= &mutex
;
779 data
[i
].exit
= &exitcond
;
781 data
[i
].Ptr_log
= &logfile
;
787 double loweststd
= 1000000;
790 lock_type
lock(mutex
);
792 while(iter
< itermax
){
793 for (int i
=0;i
<islandsN
;i
++){
794 if ( !*(data
[i
].isActive
) ){
795 //Create again the i-th thread to simulate an Island
797 if (IslandType
== 0){
798 data
[i
].randomSeed
= rng();
799 cout
<< "\t\t\tPSO:\t\t omega: "<< data
[i
].omega
<< "\t\teta1: " << data
[i
].eta1
<< "\t\teta2: " << data
[i
].eta2
<< "\t\tVcoeff: " << data
[i
].vcoeff
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
800 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
802 else if (IslandType
== 1){
803 data
[i
].randomSeed
= rng();
804 cout
<< "\t\t\tMPSO:\t\t omega: "<< data
[i
].omega
<< "\t\teta1: " << data
[i
].eta1
<< "\t\teta2: " << data
[i
].eta2
<< "\t\tVcoeff: " << data
[i
].vcoeff
<< "\t\tNswamrs: " << data
[i
].nswarms
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
805 rc
= pthread_create(&threads
[i
], NULL
, MPSOthread
, (void *)&data
[i
]);
807 else if (IslandType
== 2){
808 data
[i
].randomSeed
= rng();
809 cout
<< "\t\t\tSGA:\t\t CR: "<< data
[i
].CRsga
<< "\t\tM: " << data
[i
].M
<< "\t\tInsertBest: " << data
[i
].insert_best
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
810 rc
= pthread_create(&threads
[i
], NULL
, SGAthread
, (void *)&data
[i
]);
812 else if (IslandType
== 3){
813 data
[i
].randomSeed
= rng();
814 data
[i
].generations
=10000;
816 cout
<< "\t\t\tASA:\t\t Ts: "<< data
[i
].Ts
<< "\t\tTf: " << data
[i
].Tf
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
817 rc
= pthread_create(&threads
[i
], NULL
, ASAthread
, (void *)&data
[i
]);
820 data
[i
].randomSeed
= rng();
821 data
[i
].strategy
= strategy
;
822 cout
<< "\t\t\tDE: \t\t F: "<< data
[i
].F
<< "\t\tCR: " << data
[i
].CR
<< "\t\tStrategy: " << data
[i
].strategy
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
823 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
826 //Problem creating the thread
827 printf("ERROR; return code from pthread_create() is %d\n", rc
);
831 //Thread Successfully Created
833 *(data
[i
].isActive
) = true;
834 //thread is detached as it will never be joined
835 //(on OSx Leopard this avoids that at a certain point
836 //threads cannot be created anymore resulting in rc=35)
837 pthread_detach(threads
[i
]);
840 //evaluate standard deviation in main population
841 //loweststd = IslandPop[0].evaluateStd();
842 loweststd
= IslandPop
.evaluateStd();
844 //log islands best and std
845 for (int i2
=0;i2
<islandsN
;i2
++){
846 cout
<< "CPU#:" << i2
<< ": " << IslandPop
.extractBestIndividual().getFitness() << " " << IslandPop
.evaluateStd() << endl
;
848 cout
<< "HighestStd: " << loweststd
<< endl
;
849 cout
<< "Iter: " << iter
+1 << endl
;
850 cout
<< "\nmain():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
852 //ring topology migration
860 //The main cycle has finished: we wait for all threads to finish
861 for (int i
=0; i
<islandsN
;i
++){
862 while (*data
[i
].isActive
); //infinite loop if a thread never ends
870 dif
= difftime(end
,start
);
871 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
873 double res
=IslandPop
.extractBestIndividual().getFitness();
875 results
.push_back(res
);
878 for (int i
= 0; i
< islandsN
; i
++) {
879 delete parallelProblems
[i
];
883 //evaluate experiment results
884 for (unsigned int i
=0;i
<results
.size();i
++){
886 if (results
[i
] > max
) max
= results
[i
];
887 if (results
[i
] < min
) min
= results
[i
];
889 mean
/= results
.size();
891 for (unsigned int i
=0;i
<results
.size();i
++){
892 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
894 dev
= sqrt(dev
/results
.size());
896 for (unsigned int i
=0;i
<results
.size();i
++){
897 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
901 cout
<< "\nMean: " << mean
<< endl
;
902 cout
<< "Std: " << dev
<< endl
;
903 cout
<< "Max: " << max
<< endl
;
904 cout
<< "Min: " << min
<< endl
;
905 cout
<< "Success: " << results
.size() << endl
;
917 cout
<< "\nBest fitness found: " << pop
.extractBestIndividual().getFitness() << endl
;