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));
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
= 100; //number of trials
64 int niterTot
= 10000; //generations per algorithm call
72 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
75 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
76 vector
<double> results
;
78 //Instanciate the algorithm
79 //Adaptive Simulated Annealing
81 ASA
.initASA(niterTot
,niterTemp
,niterRange
,LB
.size(),T0
,Tcoeff
,StartStep
, rng
.next());
85 for (int i
=0;i
<trials
;i
++){
86 cout
<< "\nTrial number #" << i
+1 << endl
;
87 //we create a random population
88 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
89 pop
.evaluatePopulation(problem
);
93 while( iter
< itermax
){
96 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
99 pop
= ASA
.evolve(pop
[0],problem
);
101 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
102 //we print the result
103 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
104 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
105 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
106 cout
<< "Std : " << pop
.evaluateStd() << endl
;
107 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
110 results
.push_back(pop
.extractBestIndividual().getFitness());
111 dif
= difftime(end
,start
);
112 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
113 for (int i
=0;i
<D
;i
++){
114 logfile
<< pop
.extractBestIndividual()[i
] << " ";
116 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
119 //evaluate experiment results
120 for (int i
=0;i
<trials
;i
++){
122 if (results
[i
] > max
) max
= results
[i
];
123 if (results
[i
] < min
) min
= results
[i
];
127 for (int i
=0;i
<trials
;i
++){
128 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
130 dev
= sqrt(dev
/trials
);
132 for (int i
=0;i
<trials
;i
++){
133 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
138 cout
<< "\nMean: " << mean
<< endl
;
139 cout
<< "Std: " << dev
<< endl
;
140 cout
<< "Max: " << max
<< endl
;
141 cout
<< "Min: " << min
<< endl
;
146 case 2: //PSO sequential experiment
148 //Experiment Settings
149 int NP
= 20; //population size
150 int trials
= 100; //number of trials
151 int gen
= 500; //generations per algorithm call
158 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
159 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
162 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
163 vector
<double> results
;
165 //Instanciate the algorithm
167 PSO
.initPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
, rng
.next());
169 for (int i
=0;i
<trials
;i
++){
170 cout
<< "\nTrial number #" << i
+1 << endl
;
171 //we create a random population
172 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
173 pop
.evaluatePopulation(problem
);
177 while(iter
< itermax
){
180 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
183 pop
= PSO
.evolve(pop
,problem
);
185 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
186 //we print the result
187 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
188 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
189 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
190 cout
<< "Std : " << pop
.evaluateStd() << endl
;
191 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
194 results
.push_back(pop
.extractBestIndividual().getFitness());
195 dif
= difftime(end
,start
);
196 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
199 //evaluate experiment results
200 for (int i
=0;i
<trials
;i
++){
202 if (results
[i
] > max
) max
= results
[i
];
203 if (results
[i
] < min
) min
= results
[i
];
207 for (int i
=0;i
<trials
;i
++){
208 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
210 dev
= sqrt(dev
/trials
);
212 for (int i
=0;i
<trials
;i
++){
213 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
218 cout
<< "\nMean: " << mean
<< endl
;
219 cout
<< "Std: " << dev
<< endl
;
220 cout
<< "Max: " << max
<< endl
;
221 cout
<< "Min: " << min
<< endl
;
225 case 3: //MPSO sequential experiment
227 //Experiment Settings
228 int NP
= 20; //population size
229 int trials
= 100; //number of trials
230 int gen
= 500; //generations per algorithm call
238 int itermax
= 100; //Maximum number of iterations allowed (i.e. output printed on the screen)
239 double stdmin
= 1e-5; //Standard deviation of the population that determines a stop
242 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
243 vector
<double> results
;
245 //Instanciate the algorithm
247 MPSO
.initMPSO(gen
,LB
.size(),omega
,eta1
,eta2
,vcoeff
,nswarms
, rng
.next());
249 for (int i
=0;i
<trials
;i
++){
250 cout
<< "\nTrial number #" << i
+1 << endl
;
251 //we create a random population
252 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
253 pop
.evaluatePopulation(problem
);
257 while(iter
< itermax
){
260 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
263 pop
= MPSO
.evolve(pop
,problem
);
265 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
266 //we print the result
267 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
268 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
269 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
270 cout
<< "Std : " << pop
.evaluateStd() << endl
;
271 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
274 results
.push_back(pop
.extractBestIndividual().getFitness());
275 dif
= difftime(end
,start
);
276 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
279 //evaluate experiment results
280 for (int i
=0;i
<trials
;i
++){
282 if (results
[i
] > max
) max
= results
[i
];
283 if (results
[i
] < min
) min
= results
[i
];
287 for (int i
=0;i
<trials
;i
++){
288 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
290 dev
= sqrt(dev
/trials
);
292 for (int i
=0;i
<trials
;i
++){
293 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
298 cout
<< "\nMean: " << mean
<< endl
;
299 cout
<< "Std: " << dev
<< endl
;
300 cout
<< "Max: " << max
<< endl
;
301 cout
<< "Min: " << min
<< endl
;
307 case 4: //Sequential Differential Evolution Experiment
309 //Experiment Settings
310 int NP
= 20; //population size for each island
311 int trials
= 100; //number of trials
312 int gen
= 500; //generations per algorithm call
313 double F
= 0.8; //F in DE
314 double CR
= 0.8; //CR in DE
315 int strategy
= 2; //DE startegy
318 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
321 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
322 vector
<double> results
;
324 //Instanciate the algorithm
326 DE
.initDE(gen
,LB
.size(),F
,CR
,strategy
, rng
.next());
328 for (int i
=0;i
<trials
;i
++){
329 cout
<< "\nTrial number #" << i
+1 << endl
;
330 //we create a random population
333 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
334 pop
.evaluatePopulation(problem
);
338 while( iter
< itermax
){
341 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
344 pop
= DE
.evolve(pop
,problem
);
346 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
347 //we print the result
348 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
349 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
350 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
351 cout
<< "Std : " << pop
.evaluateStd() << endl
;
352 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
356 //results.push_back(iter*NP*gen);
357 results
.push_back(pop
.extractBestIndividual().getFitness());
359 dif
= difftime(end
,start
);
360 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
361 for (int i
=0;i
<D
;i
++){
362 logfile
<< pop
.extractBestIndividual()[i
] << " ";
364 logfile
<< exp(-pop
.extractBestIndividual().getFitness()) <<endl
;
367 //evaluate experiment results
368 for (unsigned int i
=0;i
<results
.size();i
++){
370 if (results
[i
] > max
) max
= results
[i
];
371 if (results
[i
] < min
) min
= results
[i
];
373 mean
/= results
.size();
375 for (unsigned int i
=0;i
<results
.size();i
++){
376 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
378 dev
= sqrt(dev
/results
.size());
380 for (unsigned int i
=0;i
<results
.size();i
++){
381 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
385 cout
<< "\nMean: " << mean
<< endl
;
386 cout
<< "Std: " << dev
<< endl
;
387 cout
<< "Max: " << max
<< endl
;
388 cout
<< "Min: " << min
<< endl
;
389 cout
<< "Success: " << results
.size() << endl
;
395 case 5: //SGA experiment
397 //Experiment Settings
398 int NP
= 20; //population size
399 int trials
= 100; //number of trials
400 int gen
= 500; //generations per algorithm call
406 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
409 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
410 vector
<double> results
;
412 //Instanciate the algorithm
413 //Simple Genetic Algorithm
415 SGA
.initSGA(gen
,LB
.size(),CR
,M
,insert_best
, rng
.next());
417 for (int i
=0;i
<trials
;i
++){
418 cout
<< "\nTrial number #" << i
+1 << endl
;
419 //we create a random population
420 pop
.createRandomPopulation(LB
,UB
,NP
, rng
);
421 pop
.evaluatePopulation(problem
);
425 while( iter
< itermax
){
428 cout
<< "Initial fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
431 pop
= SGA
.evolve(pop
,problem
);
433 dif
= (double)(end1
-start1
) / (double)CLOCKS_PER_SEC
;
434 //we print the result
435 cout
<< "Final fitness: " << pop
.extractBestIndividual().getFitness() << endl
;
436 cout
<< "Worst fitness: " << pop
.extractWorstIndividual().getFitness() << endl
;
437 cout
<< "Mean : " << pop
.evaluateMean() << endl
;
438 cout
<< "Std : " << pop
.evaluateStd() << endl
;
439 cout
<< "\t\tSeconds elapsed: " << dif
<< endl
;
442 results
.push_back(pop
.extractBestIndividual().getFitness());
443 dif
= difftime(end
,start
);
444 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
447 //evaluate experiment results
448 for (int i
=0;i
<trials
;i
++){
450 if (results
[i
] > max
) max
= results
[i
];
451 if (results
[i
] < min
) min
= results
[i
];
455 for (int i
=0;i
<trials
;i
++){
456 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
458 dev
= sqrt(dev
/trials
);
460 for (int i
=0;i
<trials
;i
++){
461 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
466 cout
<< "\nMean: " << mean
<< endl
;
467 cout
<< "Std: " << dev
<< endl
;
468 cout
<< "Max: " << max
<< endl
;
469 cout
<< "Min: " << min
<< endl
;
474 case 6: //Parallel asynchronous island model
477 //Experiment Settings
478 int NP
= 1; //population size for each island
479 int trials
= 20; //number of trials
480 int gen
= 500; //generations per algorithm call
481 double F
= 0.8; //F in DE
482 double CR
= 0.8; //CR in DE
483 int strategy
= 2; //DE startegy
484 int islandsN
= 5; //Number of Islands
487 int itermax
= 120; //Maximum number of iterations allowed (i.e. output printed on the screen)
490 double mean
= 0, dev
= 0, max
= 0, min
= 200000000;
491 vector
<double> results
;
493 //Main cycle creating threads
494 for (int i
=0;i
<trials
;i
++){
495 cout
<< "\nTrial number #" << i
+1 << endl
;
496 //We create a random population in each island (this is done by the main thread - i.e. no parallelization here)
497 vector
<Population
> IslandPop(islandsN
);
498 vector
<GOProblem
*> parallelProblems(islandsN
);
499 for (int i
=0;i
<islandsN
;i
++){
500 parallelProblems
[i
] = new tandemProb();
502 IslandPop
[i
].createRandomPopulation(LB
,UB
,NP
, rng
);
503 IslandPop
[i
].evaluatePopulation(*parallelProblems
[i
]);
506 //We instanciate the objects needed for pthreads allocating memory for the threads array
508 threads
= new pthread_t
[islandsN
];
509 pthread_mutex_t mutex
;
510 pthread_cond_t exitcond
;
511 pthread_mutex_init(&mutex
, NULL
);
512 pthread_cond_init (&exitcond
, NULL
);
514 //We allocate memory for the isActive array containing the status ofeach thread and we initialise it to false (no threads opened)
516 isActive
= new bool[islandsN
];
517 for (int i
=0;i
<islandsN
;i
++) isActive
[i
]=false; //all threads are inactive
519 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
521 data
= new threadParam
[islandsN
];
522 for (int i
=0;i
<islandsN
;i
++){
523 data
[i
].threadID
= i
;
525 data
[i
].problem
= parallelProblems
[i
];
526 data
[i
].Ptr_pop
= &IslandPop
[i
]; //Each Island has a different population
530 data
[i
].generations
= gen
;
532 //Initialise DE specific data
533 data
[i
].strategy
= 1;
537 //Initialise PSO/MPSO specific data
538 data
[i
].omega
= 0.65;
544 //Initialise SGA specific data
545 data
[i
].CRsga
= 0.95;
547 data
[i
].insert_best
=1;
549 //Initialise ASA specific data
551 data
[i
].Tf
= data
[i
].Ts
/1000;
552 data
[i
].isActive
= &isActive
[i
];
553 data
[i
].TPmutex
= &mutex
;
554 data
[i
].exit
= &exitcond
;
556 data
[i
].Ptr_log
= &logfile
;
562 double loweststd
= 1000000;
564 pthread_mutex_lock (&mutex
);
566 while(iter
< itermax
){
567 for (int i
=0;i
<islandsN
;i
++){
568 if ( !*(data
[i
].isActive
) ){
569 //Create again the i-th thread to simulate an Island
571 if (IslandType
== 0){
572 data
[i
].randomSeed
= rng
.next();
573 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
;
574 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
576 else if (IslandType
== 1){
577 data
[i
].randomSeed
= rng
.next();
578 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
;
579 rc
= pthread_create(&threads
[i
], NULL
, MPSOthread
, (void *)&data
[i
]);
581 else if (IslandType
== 2){
582 data
[i
].randomSeed
= rng
.next();
583 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
;
584 rc
= pthread_create(&threads
[i
], NULL
, SGAthread
, (void *)&data
[i
]);
586 else if (IslandType
== 3){
587 data
[i
].randomSeed
= rng
.next();
588 data
[i
].generations
=10000;
590 cout
<< "\t\t\tASA:\t\t Ts: "<< data
[i
].Ts
<< "\t\tTf: " << data
[i
].Tf
<< "\t\tGenerations: " << data
[i
].generations
<< endl
;
591 rc
= pthread_create(&threads
[i
], NULL
, ASAthread
, (void *)&data
[i
]);
594 data
[i
].randomSeed
= rng
.next();
595 data
[i
].strategy
= strategy
;
596 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
;
597 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
600 //Problem creating the thread
601 printf("ERROR; return code from pthread_create() is %d\n", rc
);
605 //Thread Successfully Created
607 *(data
[i
].isActive
) = true;
608 //thread is detached as it will never be joined
609 //(on OSx Leopard this avoids that at a certain point
610 //threads cannot be created anymore resulting in rc=35)
611 pthread_detach(threads
[i
]);
614 //evaluate highest standard deviation across Islands
615 //loweststd = IslandPop[0].evaluateStd();
616 loweststd
= IslandPop
[0].extractBestIndividual().getFitness();
617 for (int i2
=1;i2
<islandsN
;i2
++){
618 if (loweststd
< IslandPop
[i2
].extractBestIndividual().getFitness()) loweststd
= IslandPop
[i2
].extractBestIndividual().getFitness();
621 //log islands best and std
622 for (int i2
=0;i2
<islandsN
;i2
++){
623 cout
<< "CPU#:" << i2
<< ": " << IslandPop
[i2
].extractBestIndividual().getFitness() << " " << IslandPop
[i2
].evaluateStd() << endl
;
625 cout
<< "HighestStd: " << loweststd
<< endl
;
626 cout
<< "Iter: " << iter
+1 << endl
;
627 cout
<< "\nmain():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
629 //ring topology migration
630 if (Pk::nextDouble(rng
) < 0.2){
631 IslandPop
[(i
+1) % islandsN
].substituteIndividual(IslandPop
[i
].extractBestIndividual(), rng
.next() % data
[i
].NP
);
636 pthread_cond_wait(&exitcond
, &mutex
);
638 pthread_mutex_unlock (&mutex
);
639 //The main cycle has finished: we wait for all threads to finish
640 for (int i
=0; i
<islandsN
;i
++){
641 while (*data
[i
].isActive
); //infinite loop if a thread never ends
644 //we clean the thread objects and deallocate memory
645 pthread_mutex_destroy(&mutex
);
646 pthread_cond_destroy(&exitcond
);
651 dif
= difftime(end
,start
);
652 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
654 double res
=IslandPop
[0].extractBestIndividual().getFitness();
655 for (int i
=1;i
<islandsN
;i
++){
656 if (res
> IslandPop
[i
].extractBestIndividual().getFitness()) res
= IslandPop
[i
].extractBestIndividual().getFitness();
659 results
.push_back(res
);
662 for (int i
= 0; i
< islandsN
; i
++) {
663 delete parallelProblems
[i
];
667 //evaluate experiment results
668 for (unsigned int i
=0;i
<results
.size();i
++){
670 if (results
[i
] > max
) max
= results
[i
];
671 if (results
[i
] < min
) min
= results
[i
];
673 mean
/= results
.size();
675 for (unsigned int i
=0;i
<results
.size();i
++){
676 dev
+= (mean
-results
[i
])*(mean
-results
[i
]);
678 dev
= sqrt(dev
/results
.size());
680 for (unsigned int i
=0;i
<results
.size();i
++){
681 cout
<< "\nTrial #" << i
<< ": " << results
[i
] << endl
;
685 cout
<< "\nMean: " << mean
<< endl
;
686 cout
<< "Std: " << dev
<< endl
;
687 cout
<< "Max: " << max
<< endl
;
688 cout
<< "Min: " << min
<< endl
;
689 cout
<< "Success: " << results
.size() << endl
;
696 x
.createRandomIndividual(LB
,UB
,rng
);
702 pop
.createRandomPopulation(LB
,UB
,5,rng
);
703 pop
.evaluatePopulation(problem
);
704 cout
<< pop
[3] << endl
;
706 cout
<< pop
[3] << endl
;
715 //We create a random population and evaluate its fitness (this is done by the main thread - i.e. no parallelization here)
716 pop
.createRandomPopulation(LB
,UB
,20*5, rng
);
717 pop
.evaluatePopulation(problem
);
719 //We instanciate the objects needed for pthreads allocating memory for the threads array
721 threads
= new pthread_t
[NUM_THREADS
];
722 //pthread_attr_t attr;
723 //pthread_attr_init(&attr);
724 //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
725 pthread_mutex_t mutex
;
726 pthread_cond_t exitcond
;
727 pthread_mutex_init(&mutex
, NULL
);
728 pthread_cond_init (&exitcond
, NULL
);
733 //We allocate memory for the isActive array containing the status ofeach thread and we initialise it to false (no threads opened)
735 isActive
= new bool[NUM_THREADS
]; //all threads are inactive
736 for (int i
=0;i
<NUM_THREADS
;i
++) {isActive
[i
]=false;}
738 //We allocate memory and initialise the data threadParam array containing the information to be passed to the different threads
740 data
= new threadParam
[NUM_THREADS
];
741 for (int i
=0;i
<NUM_THREADS
;i
++){
742 data
[i
].threadID
= i
;
744 data
[i
].problem
= &problem
;
745 data
[i
].Ptr_pop
= &pop
;
747 data
[i
].generations
= 500;
749 //Initialise DE specific data
750 data
[i
].strategy
= 2;
754 //Initialise PSO specific data
758 data
[i
].vcoeff
= 1000;
760 data
[i
].isActive
= &isActive
[i
];
761 data
[i
].TPmutex
= &mutex
;
762 data
[i
].exit
= &exitcond
;
764 data
[i
].Ptr_log
= &logfile
;
767 //Main cycle creating threads
769 int count1
=0,count2
=0;
771 int algorithmchoice
= 0;
773 pthread_mutex_lock (&mutex
);
775 while (globalCount
<120){
776 //while(pop.extractBestIndividual().getFitness()!=0){
777 for (int i
=0;i
<NUM_THREADS
;i
++){
778 if ( !*(data
[i
].isActive
) ){
779 cout
<< "main():" << endl
<< "\t\t\tcreating thread, ID " << i
<< endl
;
780 algorithmchoice
= 4;//r250()%10;
781 if (algorithmchoice
== 0){
783 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
;
784 rc
= pthread_create(&threads
[i
], NULL
, PSOthread
, (void *)&data
[i
]);
787 data
[i
].strategy
= i
+1;
788 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
;
789 rc
= pthread_create(&threads
[i
], NULL
, DEthread
, (void *)&data
[i
]);
794 printf("ERROR; return code from pthread_create() is %d\n", rc
);
799 *(data
[i
].isActive
) = true;
803 pthread_cond_wait(&exitcond
, &mutex
);
805 pthread_mutex_unlock (&mutex
);
807 //The main cycle has finished: we wait for all threads to finish
808 for (int i
=0; i
<NUM_THREADS
;i
++){
809 pthread_join(threads
[i
],NULL
);
812 //we clean the thread objects and deallocate memory
813 pthread_mutex_destroy(&mutex
);
814 //pthread_attr_destroy(&attr);
815 pthread_cond_destroy(&exitcond
);
820 dif
= difftime(end
,start
);
821 cout
<< "\nSeconds elapsed: " << dif
<< endl
<<endl
;
822 cout
<< "\nCount1: " << count1
<< endl
;
823 cout
<< "\nCount2: " << count2
<< endl
;
832 //The following lines are commented out as they implement exactly the DiGMO random strategy in a random
833 //way. This creates a very unefficient cooperation between the algorithms as it does not have the implicit
834 //learning of the best strategy. This is the mechanism that allow, in the distributed version of the algorithm,
835 //the same individual to be evolved by different algorithms and only the most succesful one to be selected
837 //It seems that the reinsertion strategy (only improved individuals substitute their old versions) together with
838 //the contemporary evolution of the same individuals is key to the efficiency of algorithm cooperation (mimicking co-evolution)
839 //In a sequential version this can be enforced by evolving with DE, PSO and SA and only at the end perform reinsertion
840 //as in the uncommented lines above
842 /*algorithmchoice = r250()%3;
843 if ( algorithmchoice == 0 ){
844 deme=pop.extractRandomDeme(20,picks);
846 cout << "Using DE" << endl;
847 cout << "Initial fitness: " << deme.extractBestIndividual().getFitness() << endl;
848 //DE.initDE(50,LB.size(),dr250(),dr250(),(int)dr250()*11+1);
849 deme = DE.evolve(deme,&rosenbrock,LB,UB);
852 else if ( algorithmchoice == 1 ){
853 deme=pop.extractRandomDeme(20,picks);
854 //deme.resetVelocities(LB,UB);
856 cout << "Using PSO" << endl;
857 cout << "Initial fitness: " << deme.extractBestIndividual().getFitness() << endl;
858 //PSO.initPSO(500,LB.size(),(dr250()+1)/2,4.0*dr250(),4.0*dr250(),(int)dr250()*10000);
859 deme = PSO.evolve(deme,&rosenbrock,LB,UB);
862 else if ( algorithmchoice == 2 ){
863 deme=pop.extractRandomDeme(1,picks);
864 //deme.resetVelocities(LB,UB);
866 cout << "Using ASA" << endl;
867 cout << "Initial fitness: " << deme.extractBestIndividual().getFitness() << endl;
868 //ASA.initASA(10000,1,8,LB.size(),dr250(),dr250(),1.0);
869 deme = ASA.evolve(deme[0],&rosenbrock,LB,UB);
872 //we print the result
873 cout << "Final fitness: " << deme.extractBestIndividual().getFitness() << endl;
874 //we insert it in the main population
875 pop.insertDeme(deme,picks);
879 cout
<< "\nBest fitness found: " << pop
.extractBestIndividual().getFitness() << endl
;