5 #include <ctime> //for time()
6 #include "population.h"
13 #include "TrajectoryProblems.h"
14 #include "ClassicProblems.h"
16 #include "SolversThreads.h"
23 int D
=0; //Problem dimension will be assigned later
24 int choice
=0; //User choice
27 //We prepare the pseudorandom sequence (TODO: check the randomnumbers of different threads are different)
29 Pk::Random32
rng(time(0));
32 messengerfullProb problem
;
33 //we extract its information into local variables
34 const vector
<double>& LB
= problem
.getLB();
35 const vector
<double>& UB
= problem
.getUB();
36 D
= problem
.getDimension();
38 //we declare populations and individuals
39 Population demeDE
,demePSO
,demeASA
,demeLOCA
,demeSGA
,pop
;
41 vector
<int> picksDE
,picksPSO
,picksASA
,picksLOCAL
,picksSGA
;
43 time_t start
,end
,start1
,end1
;
46 //We create and open the logfile
47 ofstream
logfile("log.txt");
51 while (choice
!= -1) {
53 //we choose the algorithm
55 cout
<< "Choose: 1-ASA, 2-PSO, 3-MPSO, 4-DE, 5-SGA, 6-IslandModel(ring), Default-DiGMO: ";
62 int NP
= 1; //population size
63 int trials
= 300; //number of trials
64 int niterTot
= 10000; //generations per algorithm call
73 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
76 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
77 vector
<double> results
;
79 //Instanciate the algorithm
80 //Adaptive Simulated Annealing
82 //ASA.initASA(niterTot,niterTemp,niterRange,LB.size(),T0,Tcoeff,StartStep, rng.next());
83 ASA
.initASA(niterTot
,LB
.size(),T0
,Tf
, rng
.next());
87 for (int i
=0;i
<trials
;i
++){
88 cout
<< "\nTrial number #" << i
+1 << endl
;
89 //we create a random population
90 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
91 pop
.evaluatePopulation(problem
);
95 while( iter
< itermax
){
98 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
101 if (pop
.extractBestIndividual().getFitness() < 5){
102 ASA
.initASA(niterTot
,LB
.size(),1,0.01, rng
.next());
104 pop
= ASA
.evolve(pop
[0],problem
);
106 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
107 //we print the result
108 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
109 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
110 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
111 cout
<< "Std : " << pop
.evaluateStd() << endl
;
112 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
115 results
.push_back(pop
.extractBestIndividual().getFitness());
116 dif
= difftime(end
,start
);
117 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
118 for (int i
=0;i
<D
;i
++){
119 logfile
<< pop
.extractBestIndividual()[i
] << " ";
121 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
124 //evaluate experiment results
125 for (int i
=0;i
<trials
;i
++){
127 if (results
[i
] > max
) max
= results
[i
];
128 if (results
[i
] < min
) min
= results
[i
];
132 for (int i
=0;i
<trials
;i
++){
133 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
135 dev
= sqrt(dev
/trials
);
137 for (int i
=0;i
<trials
;i
++){
138 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
143 cout
<< "\nMean: " << mean
<< endl
;
144 cout
<< "Std: " << dev
<< endl
;
145 cout
<< "Max: " << max
<< endl
;
146 cout
<< "Min: " << min
<< endl
;
151 case 2: //PSO sequential experiment
153 //Experiment Settings
154 int NP
= 20; //population size
155 int trials
= 100; //number of trials
156 int gen
= 500; //generations per algorithm call
163 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
164 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
167 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
168 vector
<double> results
;
170 //Instanciate the algorithm
172 PSO
.initPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
, rng
.next());
174 for (int i
=0;i
<trials
;i
++){
175 cout
<< "\nTrial number #" << i
+1 << endl
;
176 //we create a random population
177 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
178 pop
.evaluatePopulation(problem
);
182 while(iter
< itermax
){
185 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
188 pop
= PSO
.evolve(pop
,problem
);
190 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
191 //we print the result
192 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
193 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
194 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
195 cout
<< "Std : " << pop
.evaluateStd() << endl
;
196 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
199 results
.push_back(pop
.extractBestIndividual().getFitness());
200 dif
= difftime(end
,start
);
201 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
204 //evaluate experiment results
205 for (int i
=0;i
<trials
;i
++){
207 if (results
[i
] > max
) max
= results
[i
];
208 if (results
[i
] < min
) min
= results
[i
];
212 for (int i
=0;i
<trials
;i
++){
213 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
215 dev
= sqrt(dev
/trials
);
217 for (int i
=0;i
<trials
;i
++){
218 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
223 cout
<< "\nMean: " << mean
<< endl
;
224 cout
<< "Std: " << dev
<< endl
;
225 cout
<< "Max: " << max
<< endl
;
226 cout
<< "Min: " << min
<< endl
;
230 case 3: //MPSO sequential experiment
232 //Experiment Settings
233 int NP
= 20; //population size
234 int trials
= 100; //number of trials
235 int gen
= 500; //generations per algorithm call
243 int itermax
= 100; //Maximum number of iterations allowed (i.e. output printed on the screen)
244 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
247 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
248 vector
<double> results
;
250 //Instanciate the algorithm
252 MPSO
.initMPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
,nswarms
, rng
.next());
254 for (int i
=0;i
<trials
;i
++){
255 cout
<< "\nTrial number #" << i
+1 << endl
;
256 //we create a random population
257 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
258 pop
.evaluatePopulation(problem
);
262 while(iter
< itermax
){
265 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
268 pop
= MPSO
.evolve(pop
,problem
);
270 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
271 //we print the result
272 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
273 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
274 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
275 cout
<< "Std : " << pop
.evaluateStd() << endl
;
276 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
279 results
.push_back(pop
.extractBestIndividual().getFitness());
280 dif
= difftime(end
,start
);
281 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
284 //evaluate experiment results
285 for (int i
=0;i
<trials
;i
++){
287 if (results
[i
] > max
) max
= results
[i
];
288 if (results
[i
] < min
) min
= results
[i
];
292 for (int i
=0;i
<trials
;i
++){
293 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
295 dev
= sqrt(dev
/trials
);
297 for (int i
=0;i
<trials
;i
++){
298 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
303 cout
<< "\nMean: " << mean
<< endl
;
304 cout
<< "Std: " << dev
<< endl
;
305 cout
<< "Max: " << max
<< endl
;
306 cout
<< "Min: " << min
<< endl
;
312 case 4: //Sequential Differential Evolution Experiment
314 //Experiment Settings
315 int NP
= 20; //population size for each island
316 int trials
= 100; //number of trials
317 int gen
= 500; //generations per algorithm call
318 double F
= 0.8; //F in DE
319 double CR
= 0.8; //CR in DE
320 int strategy
= 2; //DE startegy
323 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
326 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
327 vector
<double> results
;
329 //Instanciate the algorithm
331 DE
.initDE(gen
,LB
.size(),F
,CR
,strategy
, rng
.next());
333 for (int i
=0;i
<trials
;i
++){
334 cout
<< "\nTrial number #" << i
+1 << endl
;
335 //we create a random population
338 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
339 pop
.evaluatePopulation(problem
);
343 while( iter
< itermax
){
346 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
349 pop
= DE
.evolve(pop
,problem
);
351 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
352 //we print the result
353 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
354 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
355 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
356 cout
<< "Std : " << pop
.evaluateStd() << endl
;
357 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
361 //results.push_back(iter*NP*gen);
362 results
.push_back(pop
.extractBestIndividual().getFitness());
364 dif
= difftime(end
,start
);
365 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
366 for (int i
=0;i
<D
;i
++){
367 logfile
<< pop
.extractBestIndividual()[i
] << " ";
369 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
372 //evaluate experiment results
373 for (unsigned int i
=0;i
<results
.size();i
++){
375 if (results
[i
] > max
) max
= results
[i
];
376 if (results
[i
] < min
) min
= results
[i
];
378 mean
/= results
.size();
380 for (unsigned int i
=0;i
<results
.size();i
++){
381 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
383 dev
= sqrt(dev
/results
.size());
385 for (unsigned int i
=0;i
<results
.size();i
++){
386 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
390 cout
<< "\nMean: " << mean
<< endl
;
391 cout
<< "Std: " << dev
<< endl
;
392 cout
<< "Max: " << max
<< endl
;
393 cout
<< "Min: " << min
<< endl
;
394 cout
<< "Success: " << results
.size() << endl
;
400 case 5: //SGA experiment
402 //Experiment Settings
403 int NP
= 20; //population size
404 int trials
= 100; //number of trials
405 int gen
= 500; //generations per algorithm call
411 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
414 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
415 vector
<double> results
;
417 //Instanciate the algorithm
418 //Simple Genetic Algorithm
420 SGA
.initSGA(gen
,LB
.size(),CR
,M
,insert_best
, rng
.next());
422 for (int i
=0;i
<trials
;i
++){
423 cout
<< "\nTrial number #" << i
+1 << endl
;
424 //we create a random population
425 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
426 pop
.evaluatePopulation(problem
);
430 while( iter
< itermax
){
433 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
436 pop
= SGA
.evolve(pop
,problem
);
438 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
439 //we print the result
440 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
441 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
442 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
443 cout
<< "Std : " << pop
.evaluateStd() << endl
;
444 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
447 results
.push_back(pop
.extractBestIndividual().getFitness());
448 dif
= difftime(end
,start
);
449 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
452 //evaluate experiment results
453 for (int i
=0;i
<trials
;i
++){
455 if (results
[i
] > max
) max
= results
[i
];
456 if (results
[i
] < min
) min
= results
[i
];
460 for (int i
=0;i
<trials
;i
++){
461 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
463 dev
= sqrt(dev
/trials
);
465 for (int i
=0;i
<trials
;i
++){
466 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
471 cout
<< "\nMean: " << mean
<< endl
;
472 cout
<< "Std: " << dev
<< endl
;
473 cout
<< "Max: " << max
<< endl
;
474 cout
<< "Min: " << min
<< endl
;
479 case 6: //Parallel asynchronous island model
482 //Experiment Settings
483 int NP
= 1; //population size for each island
484 int trials
= 20; //number of trials
485 int gen
= 500; //generations per algorithm call
486 double F
= 0.8; //F in DE
487 double CR
= 0.8; //CR in DE
488 int strategy
= 2; //DE startegy
489 int islandsN
= 1; //Number of Islands
492 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
495 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
496 vector
<double> results
;
498 //Main cycle creating threads
499 for (int i
=0;i
<trials
;i
++){
500 cout
<< "\nTrial number #" << i
+1 << endl
;
501 //We create a random population in each island (this is done by the main thread - i.e. no parallelization here)
502 vector
<Population
> IslandPop(islandsN
);
503 vector
<GOProblem
*> parallelProblems(islandsN
);
504 for (int i
=0;i
<islandsN
;i
++){
505 parallelProblems
[i
] = new tandemProb();
507 IslandPop
[i
].createRandomPopulation(LB
,UB
,NP
, rng
);
508 IslandPop
[i
].evaluatePopulation(*parallelProblems
[i
]);
511 //We instanciate the objects needed for pthreads allocating memory for the threads array
513 threads
= new pthread_t
[islandsN
];
514 pthread_mutex_t mutex
;
515 pthread_cond_t exitcond
;
516 pthread_mutex_init(&mutex
, NULL
);
517 pthread_cond_init (&exitcond
, NULL
);
519 //We allocate memory for the isActive array containing the status ofeach thread and we initialise it to false (no threads opened)
521 isActive
= new bool[islandsN
];
522 for (int i
=0;i
<islandsN
;i
++) isActive
[i
]=false; //all threads are inactive
524 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
526 data
= new threadParam
[islandsN
];
527 for (int i
=0;i
<islandsN
;i
++){
528 data
[i
].threadID
= i
;
530 data
[i
].problem
= parallelProblems
[i
];
531 data
[i
].Ptr_pop
= &IslandPop
[i
]; //Each Island has a different population
535 data
[i
].generations
= gen
;
537 //Initialise DE specific data
538 data
[i
].strategy
= 1;
542 //Initialise PSO/MPSO specific data
543 data
[i
].omega
= 0.65;
549 //Initialise SGA specific data
550 data
[i
].CRsga
= 0.95;
552 data
[i
].insert_best
=1;
554 //Initialise ASA specific data
556 data
[i
].Tf
= data
[i
].Ts
/1000;
557 data
[i
].isActive
= &isActive
[i
];
558 data
[i
].TPmutex
= &mutex
;
559 data
[i
].exit
= &exitcond
;
561 data
[i
].Ptr_log
= &logfile
;
567 double loweststd
= 1000000;
569 pthread_mutex_lock (&mutex
);
571 while(iter
< itermax
){
572 for (int i
=0;i
<islandsN
;i
++){
573 if ( !*(data
[i
].isActive
) ){
574 //Create again the i-th thread to simulate an Island
576 if (IslandType
== 0){
577 data
[i
].randomSeed
= rng
.next();
578 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
;
579 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
581 else if (IslandType
== 1){
582 data
[i
].randomSeed
= rng
.next();
583 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
;
584 rc
= pthread_create(&threads
[i
], NULL
, MPSOthread
, (void *)&data
[i
]);
586 else if (IslandType
== 2){
587 data
[i
].randomSeed
= rng
.next();
588 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
;
589 rc
= pthread_create(&threads
[i
], NULL
, SGAthread
, (void *)&data
[i
]);
591 else if (IslandType
== 3){
592 data
[i
].randomSeed
= rng
.next();
593 data
[i
].generations
=10000;
595 cout
<< "\t\t\tASA:\t\t Ts: "<< data
[i
].Ts
<< "\t\tTf: " << data
[i
].Tf
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
596 rc
= pthread_create(&threads
[i
], NULL
, ASAthread
, (void *)&data
[i
]);
599 data
[i
].randomSeed
= rng
.next();
600 data
[i
].strategy
= strategy
;
601 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
;
602 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
605 //Problem creating the thread
606 printf("ERROR; return code from pthread_create() is %d\n", rc
);
610 //Thread Successfully Created
612 *(data
[i
].isActive
) = true;
613 //thread is detached as it will never be joined
614 //(on OSx Leopard this avoids that at a certain point
615 //threads cannot be created anymore resulting in rc=35)
616 pthread_detach(threads
[i
]);
619 //evaluate highest standard deviation across Islands
620 //loweststd = IslandPop[0].evaluateStd();
621 loweststd
= IslandPop
[0].extractBestIndividual().getFitness();
622 for (int i2
=1;i2
<islandsN
;i2
++){
623 if (loweststd
< IslandPop
[i2
].extractBestIndividual().getFitness()) loweststd
= IslandPop
[i2
].extractBestIndividual().getFitness();
626 //log islands best and std
627 for (int i2
=0;i2
<islandsN
;i2
++){
628 cout
<< "CPU#:" << i2
<< ": " << IslandPop
[i2
].extractBestIndividual().getFitness() << " " << IslandPop
[i2
].evaluateStd() << endl
;
630 cout
<< "HighestStd: " << loweststd
<< endl
;
631 cout
<< "Iter: " << iter
+1 << endl
;
632 cout
<< "\nmain():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
634 //ring topology migration
635 if (Pk::nextDouble(rng
) < 0.2){
636 IslandPop
[(i
+1) % islandsN
].substituteIndividual(IslandPop
[i
].extractBestIndividual(), rng
.next() % data
[i
].NP
);
641 pthread_cond_wait(&exitcond
, &mutex
);
643 pthread_mutex_unlock (&mutex
);
644 //The main cycle has finished: we wait for all threads to finish
645 for (int i
=0; i
<islandsN
;i
++){
646 while (*data
[i
].isActive
); //infinite loop if a thread never ends
649 //we clean the thread objects and deallocate memory
650 pthread_mutex_destroy(&mutex
);
651 pthread_cond_destroy(&exitcond
);
656 dif
= difftime(end
,start
);
657 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
659 double res
=IslandPop
[0].extractBestIndividual().getFitness();
660 for (int i
=1;i
<islandsN
;i
++){
661 if (res
> IslandPop
[i
].extractBestIndividual().getFitness()) res
= IslandPop
[i
].extractBestIndividual().getFitness();
664 results
.push_back(res
);
667 for (int i
= 0; i
< islandsN
; i
++) {
668 delete parallelProblems
[i
];
672 //evaluate experiment results
673 for (unsigned int i
=0;i
<results
.size();i
++){
675 if (results
[i
] > max
) max
= results
[i
];
676 if (results
[i
] < min
) min
= results
[i
];
678 mean
/= results
.size();
680 for (unsigned int i
=0;i
<results
.size();i
++){
681 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
683 dev
= sqrt(dev
/results
.size());
685 for (unsigned int i
=0;i
<results
.size();i
++){
686 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
690 cout
<< "\nMean: " << mean
<< endl
;
691 cout
<< "Std: " << dev
<< endl
;
692 cout
<< "Max: " << max
<< endl
;
693 cout
<< "Min: " << min
<< endl
;
694 cout
<< "Success: " << results
.size() << endl
;
701 x
.createRandomIndividual(LB
,UB
,rng
);
707 pop
.createRandomPopulation(LB
,UB
,5,rng
);
708 pop
.evaluatePopulation(problem
);
709 cout
<< pop
[3] << endl
;
711 cout
<< pop
[3] << endl
;
720 //We create a random population and evaluate its fitness (this is done by the main thread - i.e. no parallelization here)
721 pop
.createRandomPopulation(LB
,UB
,20*5, rng
);
722 pop
.evaluatePopulation(problem
);
724 //We instanciate the objects needed for pthreads allocating memory for the threads array
726 threads
= new pthread_t
[NUM_THREADS
];
727 //pthread_attr_t attr;
728 //pthread_attr_init(&attr);
729 //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
730 pthread_mutex_t mutex
;
731 pthread_cond_t exitcond
;
732 pthread_mutex_init(&mutex
, NULL
);
733 pthread_cond_init (&exitcond
, NULL
);
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[NUM_THREADS
]; //all threads are inactive
741 for (int i
=0;i
<NUM_THREADS
;i
++) {isActive
[i
]=false;}
743 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
745 data
= new threadParam
[NUM_THREADS
];
746 for (int i
=0;i
<NUM_THREADS
;i
++){
747 data
[i
].threadID
= i
;
749 data
[i
].problem
= &problem
;
750 data
[i
].Ptr_pop
= &pop
;
752 data
[i
].generations
= 500;
754 //Initialise DE specific data
755 data
[i
].strategy
= 2;
759 //Initialise PSO specific data
763 data
[i
].vcoeff
= 1000;
765 data
[i
].isActive
= &isActive
[i
];
766 data
[i
].TPmutex
= &mutex
;
767 data
[i
].exit
= &exitcond
;
769 data
[i
].Ptr_log
= &logfile
;
772 //Main cycle creating threads
774 int count1
=0,count2
=0;
776 int algorithmchoice
= 0;
778 pthread_mutex_lock (&mutex
);
780 while (globalCount
<120){
781 //while(pop.extractBestIndividual().getFitness()!=0){
782 for (int i
=0;i
<NUM_THREADS
;i
++){
783 if ( !*(data
[i
].isActive
) ){
784 cout
<< "main():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
785 algorithmchoice
= 4;//r250()%10;
786 if (algorithmchoice
== 0){
788 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
;
789 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
792 data
[i
].strategy
= i
+1;
793 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
;
794 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
799 printf("ERROR; return code from pthread_create() is %d\n", rc
);
804 *(data
[i
].isActive
) = true;
808 pthread_cond_wait(&exitcond
, &mutex
);
810 pthread_mutex_unlock (&mutex
);
812 //The main cycle has finished: we wait for all threads to finish
813 for (int i
=0; i
<NUM_THREADS
;i
++){
814 pthread_join(threads
[i
],NULL
);
817 //we clean the thread objects and deallocate memory
818 pthread_mutex_destroy(&mutex
);
819 //pthread_attr_destroy(&attr);
820 pthread_cond_destroy(&exitcond
);
825 dif
= difftime(end
,start
);
826 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
827 cout
<< "\nCount1: " << count1
<< endl
;
828 cout
<< "\nCount2: " << count2
<< endl
;
837 //The following lines are commented out as they implement exactly the DiGMO random strategy in a random
838 //way. This creates a very unefficient cooperation between the algorithms as it does not have the implicit
839 //learning of the best strategy. This is the mechanism that allow, in the distributed version of the algorithm,
840 //the same individual to be evolved by different algorithms and only the most succesful one to be selected
842 //It seems that the reinsertion strategy (only improved individuals substitute their old versions) together with
843 //the contemporary evolution of the same individuals is key to the efficiency of algorithm cooperation (mimicking co-evolution)
844 //In a sequential version this can be enforced by evolving with DE, PSO and SA and only at the end perform reinsertion
845 //as in the uncommented lines above
847 /*algorithmchoice = r250()%3;
848 if ( algorithmchoice == 0 ){
849 deme=pop.extractRandomDeme(20,picks);
851 cout << "Using DE" << endl;
852 cout << "Initial fitness: " << deme.extractBestIndividual().getFitness() << endl;
853 //DE.initDE(50,LB.size(),dr250(),dr250(),(int)dr250()*11+1);
854 deme = DE.evolve(deme,&rosenbrock,LB,UB);
857 else if ( algorithmchoice == 1 ){
858 deme=pop.extractRandomDeme(20,picks);
859 //deme.resetVelocities(LB,UB);
861 cout << "Using PSO" << endl;
862 cout << "Initial fitness: " << deme.extractBestIndividual().getFitness() << endl;
863 //PSO.initPSO(500,LB.size(),(dr250()+1)/2,4.0*dr250(),4.0*dr250(),(int)dr250()*10000);
864 deme = PSO.evolve(deme,&rosenbrock,LB,UB);
867 else if ( algorithmchoice == 2 ){
868 deme=pop.extractRandomDeme(1,picks);
869 //deme.resetVelocities(LB,UB);
871 cout << "Using ASA" << endl;
872 cout << "Initial fitness: " << deme.extractBestIndividual().getFitness() << endl;
873 //ASA.initASA(10000,1,8,LB.size(),dr250(),dr250(),1.0);
874 deme = ASA.evolve(deme[0],&rosenbrock,LB,UB);
877 //we print the result
878 cout << "Final fitness: " << deme.extractBestIndividual().getFitness() << endl;
879 //we insert it in the main population
880 pop.insertDeme(deme,picks);
884 cout
<< "\nBest fitness found: " << pop
.extractBestIndividual().getFitness() << endl
;