Substitute rng_type with rng_uint32_type for clearness.
[PaGMO.git] / main.cpp
blobb0a09a55f45f296ffe9e9e92265ddf8c00235ec3
1 #include <boost/thread/condition_variable.hpp>
2 #include <boost/thread/locks.hpp>
3 #include <boost/thread/mutex.hpp>
4 #include <iostream>
5 #include <fstream>
6 #include <iomanip>
7 #include <vector>
8 #include <ctime> //for time()
9 #include "population.h"
10 #include "ASA.h"
11 #include "PSO.h"
12 #include "DE.h"
13 #include "SGA.h"
14 #include "MPSO.h"
15 #include "LOCAL.h"
16 #include "TrajectoryProblems.h"
17 #include "ClassicProblems.h"
18 #include <pthread.h>
19 #include "SolversThreads.h"
20 #include "PkRandom.h"
22 using namespace std;
24 // Useful typedefs.
25 typedef boost::unique_lock<boost::mutex> lock_type;
26 typedef messengerfullProb problem_type;
28 int main(){
30 int D =0; //Problem dimension will be assigned later
31 int choice=0; //User choice
32 cout.precision(9);
34 //We prepare the pseudorandom sequence (TODO: check the randomnumbers of different threads are different)
36 rng_uint32_type rng(time(0));
38 //we set the problem
39 problem_type problem;
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;
47 Individual x;
48 vector <int> picksDE,picksPSO,picksASA,picksLOCAL,picksSGA;
50 time_t start,end,start1,end1;
51 double dif;
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): ";
63 cin >> choice;
65 switch (choice){
66 case 1:
68 //Experiment Settings
69 int NP = 1; //population size
70 int trials = 300; //number of trials
71 int niterTot = 10000; //generations per algorithm call
72 int niterRange = 20;
73 int niterTemp = 1;
74 double T0 = 10;
75 double Tf = T0/1000;
76 double Tcoeff = 0.85;
77 double StartStep =1;
79 //stopping criteria
80 int itermax = 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
82 //Experiment Outputs
83 double mean = 0, dev= 0, max = 0, min = 200000000;
84 vector <double> results;
86 //Instanciate the algorithm
87 //Adaptive Simulated Annealing
88 ASAalgorithm ASA;
89 //ASA.initASA(niterTot,niterTemp,niterRange,LB.size(),T0,Tcoeff,StartStep, rng());
90 ASA.initASA(niterTot,LB.size(),T0,Tf, rng());
92 //Pruned bounds
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);
99 int iter = 0;
101 time(&start);
102 while( iter < itermax){
103 iter ++;
104 //we print the best
105 cout << "Initial fitness: " << pop.extractBestIndividual().getFitness() << endl;
106 //we evolve it
107 start1=clock();
108 if (pop.extractBestIndividual().getFitness() < 5){
109 ASA.initASA(niterTot,LB.size(),1,0.01, rng());
111 pop = ASA.evolve(pop[0],problem);
112 end1=clock();
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;
121 time(&end);
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++){
133 mean += results[i];
134 if (results[i] > max) max = results[i];
135 if (results[i] < min) min = results[i];
137 mean /= trials;
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;
149 //print results
150 cout << "\nMean: " << mean << endl;
151 cout << "Std: " << dev << endl;
152 cout << "Max: " << max << endl;
153 cout << "Min: " << min << endl;
155 //Lines to test ASA
156 break;
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
164 double omega = 0.6;
165 double eta1 = 2;
166 double eta2 = 2;
167 int vcoeff = 1;
169 //stopping criteria
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
173 //Experiment Outputs
174 double mean = 0, dev= 0, max = 0, min = 200000000;
175 vector <double> results;
177 //Instanciate the algorithm
178 PSOalgorithm PSO;
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);
186 int iter = 0;
188 time(&start);
189 while(iter < itermax){
190 iter ++;
191 //we print the best
192 cout << "Initial fitness: " << pop.extractBestIndividual().getFitness() << endl;
193 //we evolve it
194 start1=clock();
195 pop = PSO.evolve(pop,problem);
196 end1=clock();
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;
205 time(&end);
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++){
213 mean += results[i];
214 if (results[i] > max) max = results[i];
215 if (results[i] < min) min = results[i];
217 mean /= trials;
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;
229 //print results
230 cout << "\nMean: " << mean << endl;
231 cout << "Std: " << dev << endl;
232 cout << "Max: " << max << endl;
233 cout << "Min: " << min << endl;
235 break;
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
243 double omega = 0.65;
244 double eta1 = 2.0;
245 double eta2 = 2.0;
246 int vcoeff = 1;
247 int nswarms = 4;
249 //stopping criteria
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
253 //Experiment Outputs
254 double mean = 0, dev= 0, max = 0, min = 200000000;
255 vector <double> results;
257 //Instanciate the algorithm
258 MPSOalgorithm MPSO;
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);
266 int iter = 0;
268 time(&start);
269 while(iter < itermax){
270 iter ++;
271 //we print the best
272 cout << "Initial fitness: " << pop.extractBestIndividual().getFitness() << endl;
273 //we evolve it
274 start1=clock();
275 pop = MPSO.evolve(pop,problem);
276 end1=clock();
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;
285 time(&end);
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++){
293 mean += results[i];
294 if (results[i] > max) max = results[i];
295 if (results[i] < min) min = results[i];
297 mean /= trials;
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;
309 //print results
310 cout << "\nMean: " << mean << endl;
311 cout << "Std: " << dev << endl;
312 cout << "Max: " << max << endl;
313 cout << "Min: " << min << endl;
315 break;
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
329 //stopping criteria
330 int itermax = 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
332 //Experiment Outputs
333 double mean = 0, dev= 0, max = 0, min = 200000000;
334 vector <double> results;
336 //Instanciate the algorithm
337 DEalgorithm DE;
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);
347 int iter = 0;
349 time(&start);
350 while( iter < itermax){
351 iter ++;
352 //we print the best
353 cout << "Initial fitness: " << pop.extractBestIndividual().getFitness() << endl;
354 //we evolve it
355 start1=clock();
356 pop = DE.evolve(pop,problem);
357 end1=clock();
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;
366 time(&end);
367 //if (iter<itermax){
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++){
381 mean += results[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;
396 //print results
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;
403 break;
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
413 double CR = 0.7;
414 double M = 0.2;
415 int insert_best = 1;
417 //stopping criteria
418 int itermax = 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
420 //Experiment Outputs
421 double mean = 0, dev= 0, max = 0, min = 200000000;
422 vector <double> results;
424 //Instanciate the algorithm
425 //Simple Genetic Algorithm
426 SGAalgorithm SGA;
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);
434 int iter = 0;
436 time(&start);
437 while( iter < itermax){
438 iter ++;
439 //we print the best
440 cout << "Initial fitness: " << pop.extractBestIndividual().getFitness() << endl;
441 //we evolve it
442 start1=clock();
443 pop = SGA.evolve(pop,problem);
444 end1=clock();
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;
453 time(&end);
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++){
461 mean += results[i];
462 if (results[i] > max) max = results[i];
463 if (results[i] < min) min = results[i];
465 mean /= trials;
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;
477 //print results
478 cout << "\nMean: " << mean << endl;
479 cout << "Std: " << dev << endl;
480 cout << "Max: " << max << endl;
481 cout << "Min: " << min << endl;
483 //Lines to test SGA
484 break;
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
498 //stopping criteria
499 int itermax = 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
501 //Experiment Outputs
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
519 pthread_t *threads;
520 threads = new pthread_t [islandsN];
521 boost::mutex mutex;
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)
525 bool *isActive;
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
530 threadParam* data;
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
539 data[i].NP = NP;
540 data[i].generations = gen;
542 //Initialise DE specific data
543 data[i].strategy = 1;
544 data[i].F = F;
545 data[i].CR = CR;
547 //Initialise PSO/MPSO specific data
548 data[i].omega = 0.65;
549 data[i].eta1 = 2.0;
550 data[i].eta2 = 2.0;
551 data[i].vcoeff = 1;
552 data[i].nswarms = 4;
554 //Initialise SGA specific data
555 data[i].CRsga = 0.95;
556 data[i].M = 0.2;
557 data[i].insert_best =1;
559 //Initialise ASA specific data
560 data[i].Ts = 1;
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;
569 int iter=0;
570 int rc;
571 int IslandType = 0;
572 double loweststd = 1000000;
575 lock_type lock(mutex);
576 time(&start);
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
581 IslandType = 3;
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;
600 data[i].NP = 1;
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]);
604 else {
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]);
610 if (rc){
611 //Problem creating the thread
612 printf("ERROR; return code from pthread_create() is %d\n", rc);
613 exit(-1);
615 else{
616 //Thread Successfully Created
617 iter += 1;
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);
647 exitcond.wait(lock);
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
655 //deallocate memory
656 delete[] data;
657 delete[] threads;
658 delete[] isActive;
659 time(&end);
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();
667 //if (iter<itermax){
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++){
678 mean += results[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;
693 //print results
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;
700 break;
702 case 7:
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
714 //stopping criteria
715 int itermax = 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
717 //Experiment Outputs
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
734 pthread_t *threads;
735 threads = new pthread_t [islandsN];
736 boost::mutex mutex;
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)
740 bool *isActive;
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
745 threadParam* data;
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
754 data[i].NP = 20;
755 data[i].generations = gen;
757 //Initialise DE specific data
758 data[i].strategy = 1;
759 data[i].F = F;
760 data[i].CR = CR;
762 //Initialise PSO/MPSO specific data
763 data[i].omega = 0.65;
764 data[i].eta1 = 2.0;
765 data[i].eta2 = 2.0;
766 data[i].vcoeff = 1;
767 data[i].nswarms = 4;
769 //Initialise SGA specific data
770 data[i].CRsga = 0.95;
771 data[i].M = 0.2;
772 data[i].insert_best =1;
774 //Initialise ASA specific data
775 data[i].Ts = 1;
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;
784 int iter=0;
785 int rc;
786 int IslandType = 0;
787 double loweststd = 1000000;
790 lock_type lock(mutex);
791 time(&start);
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
796 IslandType = 4;
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;
815 data[i].NP = 1;
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]);
819 else {
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]);
825 if (rc){
826 //Problem creating the thread
827 printf("ERROR; return code from pthread_create() is %d\n", rc);
828 exit(-1);
830 else{
831 //Thread Successfully Created
832 iter += 1;
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
857 exitcond.wait(lock);
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
865 //deallocate memory
866 delete[] data;
867 delete[] threads;
868 delete[] isActive;
869 time(&end);
870 dif = difftime(end,start);
871 cout << "\nSeconds elapsed: " << dif << endl<<endl;
873 double res=IslandPop.extractBestIndividual().getFitness();
874 //if (iter<itermax){
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++){
885 mean += results[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;
900 //print results
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;
907 break;
909 }//end case
911 } //end while
917 cout << "\nBest fitness found: " << pop.extractBestIndividual().getFitness() << endl;
919 return 0;