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"
24 typedef boost::unique_lock
<boost::mutex
> lock_type
;
28 int D
=0; //Problem dimension will be assigned later
29 int choice
=0; //User choice
32 //We prepare the pseudorandom sequence (TODO: check the randomnumbers of different threads are different)
34 Pk::Random32
rng(time(0));
37 typedef messengerfullProb problem_type
;
39 //we extract its information into local variables
40 const vector
<double>& LB
= problem
.getLB();
41 const vector
<double>& UB
= problem
.getUB();
42 D
= problem
.getDimension();
44 //we declare populations and individuals
45 Population demeDE
,demePSO
,demeASA
,demeLOCA
,demeSGA
,pop
;
47 vector
<int> picksDE
,picksPSO
,picksASA
,picksLOCAL
,picksSGA
;
49 time_t start
,end
,start1
,end1
;
52 //We create and open the logfile
53 ofstream
logfile("log.txt");
57 while (choice
!= -1) {
59 //we choose the algorithm
61 cout
<< "Choose: 1-ASA, 2-PSO, 3-MPSO, 4-DE, 5-SGA, 6-IslandModel(ring), 7-DiGMO(simulator): ";
68 int NP
= 1; //population size
69 int trials
= 300; //number of trials
70 int niterTot
= 10000; //generations per algorithm call
79 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
82 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
83 vector
<double> results
;
85 //Instanciate the algorithm
86 //Adaptive Simulated Annealing
88 //ASA.initASA(niterTot,niterTemp,niterRange,LB.size(),T0,Tcoeff,StartStep, rng.next());
89 ASA
.initASA(niterTot
,LB
.size(),T0
,Tf
, rng
.next());
93 for (int i
=0;i
<trials
;i
++){
94 cout
<< "\nTrial number #" << i
+1 << endl
;
95 //we create a random population
96 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
97 pop
.evaluatePopulation(problem
);
101 while( iter
< itermax
){
104 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
107 if (pop
.extractBestIndividual().getFitness() < 5){
108 ASA
.initASA(niterTot
,LB
.size(),1,0.01, rng
.next());
110 pop
= ASA
.evolve(pop
[0],problem
);
112 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
113 //we print the result
114 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
115 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
116 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
117 cout
<< "Std : " << pop
.evaluateStd() << endl
;
118 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
121 results
.push_back(pop
.extractBestIndividual().getFitness());
122 dif
= difftime(end
,start
);
123 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
124 for (int i
=0;i
<D
;i
++){
125 logfile
<< pop
.extractBestIndividual()[i
] << " ";
127 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
130 //evaluate experiment results
131 for (int i
=0;i
<trials
;i
++){
133 if (results
[i
] > max
) max
= results
[i
];
134 if (results
[i
] < min
) min
= results
[i
];
138 for (int i
=0;i
<trials
;i
++){
139 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
141 dev
= sqrt(dev
/trials
);
143 for (int i
=0;i
<trials
;i
++){
144 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
149 cout
<< "\nMean: " << mean
<< endl
;
150 cout
<< "Std: " << dev
<< endl
;
151 cout
<< "Max: " << max
<< endl
;
152 cout
<< "Min: " << min
<< endl
;
157 case 2: //PSO sequential experiment
159 //Experiment Settings
160 int NP
= 20; //population size
161 int trials
= 100; //number of trials
162 int gen
= 500; //generations per algorithm call
169 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
170 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
173 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
174 vector
<double> results
;
176 //Instanciate the algorithm
178 PSO
.initPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
, rng
.next());
180 for (int i
=0;i
<trials
;i
++){
181 cout
<< "\nTrial number #" << i
+1 << endl
;
182 //we create a random population
183 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
184 pop
.evaluatePopulation(problem
);
188 while(iter
< itermax
){
191 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
194 pop
= PSO
.evolve(pop
,problem
);
196 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
197 //we print the result
198 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
199 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
200 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
201 cout
<< "Std : " << pop
.evaluateStd() << endl
;
202 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
205 results
.push_back(pop
.extractBestIndividual().getFitness());
206 dif
= difftime(end
,start
);
207 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
210 //evaluate experiment results
211 for (int i
=0;i
<trials
;i
++){
213 if (results
[i
] > max
) max
= results
[i
];
214 if (results
[i
] < min
) min
= results
[i
];
218 for (int i
=0;i
<trials
;i
++){
219 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
221 dev
= sqrt(dev
/trials
);
223 for (int i
=0;i
<trials
;i
++){
224 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
229 cout
<< "\nMean: " << mean
<< endl
;
230 cout
<< "Std: " << dev
<< endl
;
231 cout
<< "Max: " << max
<< endl
;
232 cout
<< "Min: " << min
<< endl
;
236 case 3: //MPSO sequential experiment
238 //Experiment Settings
239 int NP
= 20; //population size
240 int trials
= 100; //number of trials
241 int gen
= 500; //generations per algorithm call
249 int itermax
= 100; //Maximum number of iterations allowed (i.e. output printed on the screen)
250 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
253 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
254 vector
<double> results
;
256 //Instanciate the algorithm
258 MPSO
.initMPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
,nswarms
, rng
.next());
260 for (int i
=0;i
<trials
;i
++){
261 cout
<< "\nTrial number #" << i
+1 << endl
;
262 //we create a random population
263 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
264 pop
.evaluatePopulation(problem
);
268 while(iter
< itermax
){
271 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
274 pop
= MPSO
.evolve(pop
,problem
);
276 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
277 //we print the result
278 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
279 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
280 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
281 cout
<< "Std : " << pop
.evaluateStd() << endl
;
282 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
285 results
.push_back(pop
.extractBestIndividual().getFitness());
286 dif
= difftime(end
,start
);
287 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
290 //evaluate experiment results
291 for (int i
=0;i
<trials
;i
++){
293 if (results
[i
] > max
) max
= results
[i
];
294 if (results
[i
] < min
) min
= results
[i
];
298 for (int i
=0;i
<trials
;i
++){
299 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
301 dev
= sqrt(dev
/trials
);
303 for (int i
=0;i
<trials
;i
++){
304 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
309 cout
<< "\nMean: " << mean
<< endl
;
310 cout
<< "Std: " << dev
<< endl
;
311 cout
<< "Max: " << max
<< endl
;
312 cout
<< "Min: " << min
<< endl
;
318 case 4: //Sequential Differential Evolution Experiment
320 //Experiment Settings
321 int NP
= 20; //population size for each island
322 int trials
= 100; //number of trials
323 int gen
= 500; //generations per algorithm call
324 double F
= 0.8; //F in DE
325 double CR
= 0.8; //CR in DE
326 int strategy
= 2; //DE startegy
329 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
332 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
333 vector
<double> results
;
335 //Instanciate the algorithm
337 DE
.initDE(gen
,LB
.size(),F
,CR
,strategy
, rng
.next());
339 for (int i
=0;i
<trials
;i
++){
340 cout
<< "\nTrial number #" << i
+1 << endl
;
341 //we create a random population
344 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
345 pop
.evaluatePopulation(problem
);
349 while( iter
< itermax
){
352 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
355 pop
= DE
.evolve(pop
,problem
);
357 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
358 //we print the result
359 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
360 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
361 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
362 cout
<< "Std : " << pop
.evaluateStd() << endl
;
363 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
367 //results.push_back(iter*NP*gen);
368 results
.push_back(pop
.extractBestIndividual().getFitness());
370 dif
= difftime(end
,start
);
371 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
372 for (int i
=0;i
<D
;i
++){
373 logfile
<< pop
.extractBestIndividual()[i
] << " ";
375 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
378 //evaluate experiment results
379 for (unsigned int i
=0;i
<results
.size();i
++){
381 if (results
[i
] > max
) max
= results
[i
];
382 if (results
[i
] < min
) min
= results
[i
];
384 mean
/= results
.size();
386 for (unsigned int i
=0;i
<results
.size();i
++){
387 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
389 dev
= sqrt(dev
/results
.size());
391 for (unsigned int i
=0;i
<results
.size();i
++){
392 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
396 cout
<< "\nMean: " << mean
<< endl
;
397 cout
<< "Std: " << dev
<< endl
;
398 cout
<< "Max: " << max
<< endl
;
399 cout
<< "Min: " << min
<< endl
;
400 cout
<< "Success: " << results
.size() << endl
;
406 case 5: //SGA experiment
408 //Experiment Settings
409 int NP
= 20; //population size
410 int trials
= 100; //number of trials
411 int gen
= 500; //generations per algorithm call
417 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
420 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
421 vector
<double> results
;
423 //Instanciate the algorithm
424 //Simple Genetic Algorithm
426 SGA
.initSGA(gen
,LB
.size(),CR
,M
,insert_best
, rng
.next());
428 for (int i
=0;i
<trials
;i
++){
429 cout
<< "\nTrial number #" << i
+1 << endl
;
430 //we create a random population
431 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
432 pop
.evaluatePopulation(problem
);
436 while( iter
< itermax
){
439 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
442 pop
= SGA
.evolve(pop
,problem
);
444 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
445 //we print the result
446 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
447 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
448 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
449 cout
<< "Std : " << pop
.evaluateStd() << endl
;
450 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
453 results
.push_back(pop
.extractBestIndividual().getFitness());
454 dif
= difftime(end
,start
);
455 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
458 //evaluate experiment results
459 for (int i
=0;i
<trials
;i
++){
461 if (results
[i
] > max
) max
= results
[i
];
462 if (results
[i
] < min
) min
= results
[i
];
466 for (int i
=0;i
<trials
;i
++){
467 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
469 dev
= sqrt(dev
/trials
);
471 for (int i
=0;i
<trials
;i
++){
472 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
477 cout
<< "\nMean: " << mean
<< endl
;
478 cout
<< "Std: " << dev
<< endl
;
479 cout
<< "Max: " << max
<< endl
;
480 cout
<< "Min: " << min
<< endl
;
485 case 6: //Parallel asynchronous island model
488 //Experiment Settings
489 int NP
= 1; //population size for each island
490 int trials
= 20; //number of trials
491 int gen
= 500; //generations per algorithm call
492 double F
= 0.8; //F in DE
493 double CR
= 0.8; //CR in DE
494 int strategy
= 2; //DE startegy
495 int islandsN
= 6; //Number of Islands
498 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
501 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
502 vector
<double> results
;
504 //Main cycle creating threads
505 for (int i
=0;i
<trials
;i
++){
506 cout
<< "\nTrial number #" << i
+1 << endl
;
507 //We create a random population in each island (this is done by the main thread - i.e. no parallelization here)
508 vector
<Population
> IslandPop(islandsN
);
509 vector
<GOProblem
*> parallelProblems(islandsN
);
510 for (int i
=0;i
<islandsN
;i
++){
511 parallelProblems
[i
] = new problem_type();
513 IslandPop
[i
].createRandomPopulation(LB
,UB
,NP
, rng
);
514 IslandPop
[i
].evaluatePopulation(*parallelProblems
[i
]);
517 //We instanciate the objects needed for pthreads allocating memory for the threads array
519 threads
= new pthread_t
[islandsN
];
521 boost::condition_variable exitcond
;
523 //We allocate memory for the isActive array containing the status ofeach thread and we initialise it to false (no threads opened)
525 isActive
= new bool[islandsN
];
526 for (int i
=0;i
<islandsN
;i
++) isActive
[i
]=false; //all threads are inactive
528 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
530 data
= new threadParam
[islandsN
];
531 for (int i
=0;i
<islandsN
;i
++){
532 data
[i
].threadID
= i
;
534 data
[i
].problem
= parallelProblems
[i
];
535 data
[i
].Ptr_pop
= &IslandPop
[i
]; //Each Island has a different population
539 data
[i
].generations
= gen
;
541 //Initialise DE specific data
542 data
[i
].strategy
= 1;
546 //Initialise PSO/MPSO specific data
547 data
[i
].omega
= 0.65;
553 //Initialise SGA specific data
554 data
[i
].CRsga
= 0.95;
556 data
[i
].insert_best
=1;
558 //Initialise ASA specific data
560 data
[i
].Tf
= data
[i
].Ts
/1000;
561 data
[i
].isActive
= &isActive
[i
];
562 data
[i
].TPmutex
= &mutex
;
563 data
[i
].exit
= &exitcond
;
565 data
[i
].Ptr_log
= &logfile
;
571 double loweststd
= 1000000;
574 lock_type
lock(mutex
);
576 while(iter
< itermax
){
577 for (int i
=0;i
<islandsN
;i
++){
578 if ( !*(data
[i
].isActive
) ){
579 //Create again the i-th thread to simulate an Island
581 if (IslandType
== 0){
582 data
[i
].randomSeed
= rng
.next();
583 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
;
584 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
586 else if (IslandType
== 1){
587 data
[i
].randomSeed
= rng
.next();
588 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
;
589 rc
= pthread_create(&threads
[i
], NULL
, MPSOthread
, (void *)&data
[i
]);
591 else if (IslandType
== 2){
592 data
[i
].randomSeed
= rng
.next();
593 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
;
594 rc
= pthread_create(&threads
[i
], NULL
, SGAthread
, (void *)&data
[i
]);
596 else if (IslandType
== 3){
597 data
[i
].randomSeed
= rng
.next();
598 data
[i
].generations
=10000;
600 cout
<< "\t\t\tASA:\t\t Ts: "<< data
[i
].Ts
<< "\t\tTf: " << data
[i
].Tf
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
601 rc
= pthread_create(&threads
[i
], NULL
, ASAthread
, (void *)&data
[i
]);
604 data
[i
].randomSeed
= rng
.next();
605 data
[i
].strategy
= strategy
;
606 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
;
607 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
610 //Problem creating the thread
611 printf("ERROR; return code from pthread_create() is %d\n", rc
);
615 //Thread Successfully Created
617 *(data
[i
].isActive
) = true;
618 //thread is detached as it will never be joined
619 //(on OSx Leopard this avoids that at a certain point
620 //threads cannot be created anymore resulting in rc=35)
621 pthread_detach(threads
[i
]);
624 //evaluate highest standard deviation across Islands
625 //loweststd = IslandPop[0].evaluateStd();
626 loweststd
= IslandPop
[0].extractBestIndividual().getFitness();
627 for (int i2
=1;i2
<islandsN
;i2
++){
628 if (loweststd
< IslandPop
[i2
].extractBestIndividual().getFitness()) loweststd
= IslandPop
[i2
].extractBestIndividual().getFitness();
631 //log islands best and std
632 for (int i2
=0;i2
<islandsN
;i2
++){
633 cout
<< "CPU#:" << i2
<< ": " << IslandPop
[i2
].extractBestIndividual().getFitness() << " " << IslandPop
[i2
].evaluateStd() << endl
;
635 cout
<< "HighestStd: " << loweststd
<< endl
;
636 cout
<< "Iter: " << iter
+1 << endl
;
637 cout
<< "\nmain():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
639 //ring topology migration
640 if (Pk::nextDouble(rng
) < 0.2){
641 IslandPop
[(i
+1) % islandsN
].substituteIndividual(IslandPop
[i
].extractBestIndividual(), rng
.next() % data
[i
].NP
);
649 //The main cycle has finished: we wait for all threads to finish
650 for (int i
=0; i
<islandsN
;i
++){
651 while (*data
[i
].isActive
); //infinite loop if a thread never ends
659 dif
= difftime(end
,start
);
660 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
662 double res
=IslandPop
[0].extractBestIndividual().getFitness();
663 for (int i
=1;i
<islandsN
;i
++){
664 if (res
> IslandPop
[i
].extractBestIndividual().getFitness()) res
= IslandPop
[i
].extractBestIndividual().getFitness();
667 results
.push_back(res
);
670 for (int i
= 0; i
< islandsN
; i
++) {
671 delete parallelProblems
[i
];
675 //evaluate experiment results
676 for (unsigned int i
=0;i
<results
.size();i
++){
678 if (results
[i
] > max
) max
= results
[i
];
679 if (results
[i
] < min
) min
= results
[i
];
681 mean
/= results
.size();
683 for (unsigned int i
=0;i
<results
.size();i
++){
684 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
686 dev
= sqrt(dev
/results
.size());
688 for (unsigned int i
=0;i
<results
.size();i
++){
689 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
693 cout
<< "\nMean: " << mean
<< endl
;
694 cout
<< "Std: " << dev
<< endl
;
695 cout
<< "Max: " << max
<< endl
;
696 cout
<< "Min: " << min
<< endl
;
697 cout
<< "Success: " << results
.size() << endl
;
704 //Experiment Settings
705 int NP
= 200; //main population size
706 int trials
= 20; //number of trials
707 int gen
= 500; //generations per algorithm call
708 double F
= 0.8; //F in DE
709 double CR
= 0.8; //CR in DE
710 int strategy
= 2; //DE startegy
711 int islandsN
= 1; //Number of Islands
714 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
717 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
718 vector
<double> results
;
720 //Main cycle creating threads
721 for (int i
=0;i
<trials
;i
++){
722 cout
<< "\nTrial number #" << i
+1 << endl
;
723 //We create a random population in each island (this is done by the main thread - i.e. no parallelization here)
724 Population IslandPop
;
725 vector
<GOProblem
*> parallelProblems(islandsN
);
726 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
727 //locations when the problem is mga or mga-1dsm for th r,v,and DV variables in the mgaproblem structures
728 IslandPop
.createRandomPopulation(LB
,UB
,NP
, rng
);
729 IslandPop
.evaluatePopulation(*parallelProblems
[0]); //all the problems are identical.... evaluation is done for the [0] one.
732 //We instanciate the objects needed for pthreads allocating memory for the threads array
734 threads
= new pthread_t
[islandsN
];
736 boost::condition_variable exitcond
;
738 //We allocate memory for the isActive array containing the status ofeach thread and we initialise it to false (no threads opened)
740 isActive
= new bool[islandsN
];
741 for (int i
=0;i
<islandsN
;i
++) isActive
[i
]=false; //all threads are inactive
743 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
745 data
= new threadParam
[islandsN
];
746 for (int i
=0;i
<islandsN
;i
++){
747 data
[i
].threadID
= i
;
749 data
[i
].problem
= parallelProblems
[i
];
750 data
[i
].Ptr_pop
= &IslandPop
; //Only one population in DiGMO
754 data
[i
].generations
= gen
;
756 //Initialise DE specific data
757 data
[i
].strategy
= 1;
761 //Initialise PSO/MPSO specific data
762 data
[i
].omega
= 0.65;
768 //Initialise SGA specific data
769 data
[i
].CRsga
= 0.95;
771 data
[i
].insert_best
=1;
773 //Initialise ASA specific data
775 data
[i
].Tf
= data
[i
].Ts
/1000;
776 data
[i
].isActive
= &isActive
[i
];
777 data
[i
].TPmutex
= &mutex
;
778 data
[i
].exit
= &exitcond
;
780 data
[i
].Ptr_log
= &logfile
;
786 double loweststd
= 1000000;
789 lock_type
lock(mutex
);
791 while(iter
< itermax
){
792 for (int i
=0;i
<islandsN
;i
++){
793 if ( !*(data
[i
].isActive
) ){
794 //Create again the i-th thread to simulate an Island
796 if (IslandType
== 0){
797 data
[i
].randomSeed
= rng
.next();
798 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
;
799 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
801 else if (IslandType
== 1){
802 data
[i
].randomSeed
= rng
.next();
803 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
;
804 rc
= pthread_create(&threads
[i
], NULL
, MPSOthread
, (void *)&data
[i
]);
806 else if (IslandType
== 2){
807 data
[i
].randomSeed
= rng
.next();
808 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
;
809 rc
= pthread_create(&threads
[i
], NULL
, SGAthread
, (void *)&data
[i
]);
811 else if (IslandType
== 3){
812 data
[i
].randomSeed
= rng
.next();
813 data
[i
].generations
=10000;
815 cout
<< "\t\t\tASA:\t\t Ts: "<< data
[i
].Ts
<< "\t\tTf: " << data
[i
].Tf
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
816 rc
= pthread_create(&threads
[i
], NULL
, ASAthread
, (void *)&data
[i
]);
819 data
[i
].randomSeed
= rng
.next();
820 data
[i
].strategy
= strategy
;
821 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
;
822 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
825 //Problem creating the thread
826 printf("ERROR; return code from pthread_create() is %d\n", rc
);
830 //Thread Successfully Created
832 *(data
[i
].isActive
) = true;
833 //thread is detached as it will never be joined
834 //(on OSx Leopard this avoids that at a certain point
835 //threads cannot be created anymore resulting in rc=35)
836 pthread_detach(threads
[i
]);
839 //evaluate standard deviation in main population
840 //loweststd = IslandPop[0].evaluateStd();
841 loweststd
= IslandPop
.evaluateStd();
843 //log islands best and std
844 for (int i2
=0;i2
<islandsN
;i2
++){
845 cout
<< "CPU#:" << i2
<< ": " << IslandPop
.extractBestIndividual().getFitness() << " " << IslandPop
.evaluateStd() << endl
;
847 cout
<< "HighestStd: " << loweststd
<< endl
;
848 cout
<< "Iter: " << iter
+1 << endl
;
849 cout
<< "\nmain():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
851 //ring topology migration
859 //The main cycle has finished: we wait for all threads to finish
860 for (int i
=0; i
<islandsN
;i
++){
861 while (*data
[i
].isActive
); //infinite loop if a thread never ends
869 dif
= difftime(end
,start
);
870 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
872 double res
=IslandPop
.extractBestIndividual().getFitness();
874 results
.push_back(res
);
877 for (int i
= 0; i
< islandsN
; i
++) {
878 delete parallelProblems
[i
];
882 //evaluate experiment results
883 for (unsigned int i
=0;i
<results
.size();i
++){
885 if (results
[i
] > max
) max
= results
[i
];
886 if (results
[i
] < min
) min
= results
[i
];
888 mean
/= results
.size();
890 for (unsigned int i
=0;i
<results
.size();i
++){
891 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
893 dev
= sqrt(dev
/results
.size());
895 for (unsigned int i
=0;i
<results
.size();i
++){
896 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
900 cout
<< "\nMean: " << mean
<< endl
;
901 cout
<< "Std: " << dev
<< endl
;
902 cout
<< "Max: " << max
<< endl
;
903 cout
<< "Min: " << min
<< endl
;
904 cout
<< "Success: " << results
.size() << endl
;
916 cout
<< "\nBest fitness found: " << pop
.extractBestIndividual().getFitness() << endl
;