SedSat3 1.1.6
Sediment Source Apportionment Tool - Advanced statistical methods for environmental pollution research
Loading...
Searching...
No Matches
GA.cpp
Go to the documentation of this file.
1// GA.cpp: implementation of the CGA class.
3#include "GA.h"
4#include "StringOP.h"
5#include <stdlib.h>
7// Construction/Destruction
9
10
11template<class T>
13{
14 GA_params.maxpop = 100;
15 Ind.resize(GA_params.maxpop);
16 Ind_old.resize(GA_params.maxpop);
17 fitdist = CDistribution(GA_params.maxpop);
18 GA_params.N = 1; //MM N=1 by default
19 GA_params.pcross = 1;
20 GA_params.cross_over_type = 1;
21 MaxFitness = 0;
22}
23
24template<class T>
25CGA<T>::CGA(int n)
26{
27 GA_params.maxpop = n;
28 Ind.resize(GA_params.maxpop);
29 Ind_old.resize(GA_params.maxpop);
30 fitdist = CDistribution(GA_params.maxpop);
31 GA_params.N = 1;
32 GA_params.pcross = 1;
33 GA_params.cross_over_type = 1;
34 MaxFitness = 0;
35}
36
37template<class T>
38CGA<T>::CGA(int n, int nParam)
39{
40 GA_params.maxpop = n;
41 GA_params.N = 1;
42 GA_params.pcross = 1;
43 Ind.resize(GA_params.maxpop);
44 Ind_old.resize(GA_params.maxpop);
45 for (int i=0; i<n; i++)
46 {
47 Ind[i] = CIndividual(GA_params.nParam);
48 Ind_old[i] = CIndividual(GA_params.nParam);
49
50 }
51 fitdist = CDistribution(GA_params.maxpop);
52 GA_params.cross_over_type = 1;
53 MaxFitness = 0;
54}
55
56template<class T>
57CGA<T>::CGA(string filename, const T &model)
58{
59 Model = model;
60 ifstream file(filename);
61 GA_params.nParam = 0;
62 GA_params.pcross = 1;
63 GA_params.N = 1;
64 GA_params.fixedstd = true;
65 GA_params.RCGA = false;
66
67 vector<string> s;
68 while (file.eof() == false)
69 {
70 s = getline(file);
71 if (s.size()>0)
72 { if (s[0] == "maxpop") GA_params.maxpop = atoi(s[1].c_str());
73 if (s[0] == "ngen") GA_params.nGen = atoi(s[1].c_str());
74 if (s[0] == "pcross") GA_params.pcross = atof(s[1].c_str());
75 if (s[0] == "pmute") GA_params.pmute = atof(s[1].c_str());
76 if (s[0] == "shakescale") GA_params.shakescale = atof(s[1].c_str());
77 if (s[0] == "shakescalered") GA_params.shakescalered = atof(s[1].c_str());
78 if (s[0] == "outputfile") filenames.outputfilename = s[1];
79 if (s[0] == "getfromfilename") filenames.getfromfilename = s[1].c_str();
80 if (s[0] == "initial_population") filenames.initialpopfilemame = s[1];
81 }
82 }
83
84 file.close();
85 for (int i=0; i<Model.Parameters().size(); i++)
86 {
87 GA_params.nParam++;
88 params.push_back(i);
89 if (Model.Parameters()[i]->GetPriorDistribution() == "lognormal")
90 { minval.push_back(log10(Model.Parameters()[i]->GetRange().low));
91 maxval.push_back(log10(Model.Parameters()[i]->GetRange().high));
92
93 }
94 else
95 {
96 minval.push_back(Model.Parameters()[i]->GetRange().low);
97 maxval.push_back(Model.Parameters()[i]->GetRange().high);
98 }
99 apply_to_all.push_back(false);
100 if (Model.Parameters()[i]->GetPriorDistribution() == "lognormal")
101 loged.push_back(1);
102 else
103 loged.push_back(0);
104
105 paramname.push_back(Model.Parameters().getKeyAtIndex(i));
106
107 }
108
109
110 Ind.resize(GA_params.maxpop);
111 Ind_old.resize(GA_params.maxpop);
112
113 fitdist = CDistribution(GA_params.maxpop);
114 GA_params.cross_over_type = 1;
115
116 for (int i=0; i<GA_params.maxpop; i++)
117 {
118 Ind[i] = CIndividual(GA_params.nParam);
119 Ind_old[i] = CIndividual(GA_params.nParam);
120 }
121
122 for (int i = 0; i<GA_params.nParam; i++)
123 Setminmax(i, minval[i], maxval[i],4);
124
125 MaxFitness = 0;
126}
127
128template<class T>
129void CGA<T>::setnparams(int n_params)
130{
131 Ind.resize(GA_params.maxpop);
132 Ind_old.resize(GA_params.maxpop);
133 for (int i=0; i<GA_params.maxpop; i++)
134 {
135 Ind[i] = CIndividual(n_params);
136 Ind_old[i] = CIndividual(n_params);
137
138 }
139}
140
141template<class T>
143{
144 GA_params.maxpop = n;
145 CIndividual TempInd = Ind[0];
146
147 int nParam = Ind[0].nParams;
148 Ind.resize(GA_params.maxpop);
149 Ind_old.resize(GA_params.maxpop);
150 for (int i=0; i<n; i++)
151 {
152 Ind[i] = CIndividual(GA_params.nParam);
153 Ind_old[i] = CIndividual(GA_params.nParam);
154 for (int j = 0; j<nParam; j++)
155 {
156 Ind[i].minrange[j] = TempInd.minrange[j];
157 Ind[i].maxrange[j] = TempInd.maxrange[j];
158 Ind[i].precision[j] = TempInd.precision[j];
159 Ind_old[i].minrange[j] = TempInd.minrange[j];
160 Ind_old[i].maxrange[j] = TempInd.maxrange[j];
161 Ind_old[i].precision[j] = TempInd.precision[j];
162 }
163
164 }
165 fitdist = CDistribution(GA_params.maxpop);
166}
167
168template<class T>
170{
171 GA_params.maxpop = C.maxpop;
172 Ind.resize(GA_params.maxpop);
173 Ind_old.resize(GA_params.maxpop);
174 Ind = C.Ind;
175 GA_params = C.GA_params;
176 filenames = C.filenames;
177 params = C.params;
178 loged = C.loged;
179 fitdist = C.fitdist;
180 MaxFitness = C.MaxFitness;
181 paramname = C.paramname;
182
183}
184
185template<class T>
187{
188 GA_params = C.GA_params;
189 filenames = C.filenames;
190 Ind = C.Ind;
191 Ind_old = C.Ind_old;
192 params = C.params;
193 loged = C.loged;
194 fitdist = C.fitdist;
195 MaxFitness = C.MaxFitness;
196 paramname = C.paramname;
197
198 return *this;
199
200}
201
202template<class T>
204{
205
206}
207
208template<class T>
210{
211 for (int i=0; i<GA_params.maxpop; i++)
212 {
213 Ind[i].initialize();
214 }
215
216 if (filenames.initialpopfilemame!="")
217 {
218 getinifromoutput(filenames.pathname+filenames.initialpopfilemame);
219 for (int i=0; i<initial_pop.size(); i++)
220 for (int j=0; j<max(int(initial_pop[i].size()),GA_params.nParam); j++)
221 if (loged[j]==1)
222 Ind[i].x[j] = log10(initial_pop[i][j]);
223 else
224 Ind[i].x[j] = initial_pop[i][j];
225 }
226}
227
228template<class T>
229void CGA<T>::Setminmax(int a, double minrange, double maxrange, int prec)
230{
231 for (int i=0; i<GA_params.maxpop; i++)
232 {
233 Ind[i].maxrange[a] = maxrange;
234 Ind[i].minrange[a] = minrange;
235 Ind[i].precision[a] = prec;
236 }
237
238}
239
240template<class T>
242{
243 sumfitness = 0;
244
245 vector<vector<double>> inp;
246
247 inp.resize(GA_params.maxpop);
248
249
250 for (int k=0; k<GA_params.maxpop; k++)
251 inp[k].resize(GA_params.nParam);
252
253 vector<double> time_(GA_params.maxpop);
254 vector<int> epochs(GA_params.maxpop);
255 clock_t t0,t1;
256
257 for (int k = 0; k < GA_params.maxpop; k++)
258 {
259 for (int i = 0; i < GA_params.nParam; i++)
260 {
261 if (loged[get_act_paramno(i)] != 1)
262 {
263 inp[k][i] = Ind[k].x[i]; //Ind
264 }
265 else
266 {
267 inp[k][i] = pow(10, Ind[k].x[i]);
268 }
269 }
270
271 int jj = 0;
272 Ind[k].actual_fitness = 0;
273
274 Models[k] = Model;
275
276 for (int i = 0; i < GA_params.nParam; i++)
277 Models[k].SetParams(params[i], inp[k][i]);
278
279
280 }
281
282
283omp_set_num_threads(numberOfThreads);
284#pragma omp parallel for //private(ts,l)
285 for (int k=0; k<GA_params.maxpop; k++)
286 {
287 FILE *FileOut;
288 FileOut = fopen((filenames.pathname+"detail_GA.txt").c_str(),"a");
289
290
291 fprintf(FileOut, "%i, ", k);
292 for (int l=0; l<Ind[0].nParams; l++)
293 if (loged[get_act_paramno(l)]==1)
294 fprintf(FileOut, "%le, ", pow(10,Ind[k].x[l]));
295 else
296 fprintf(FileOut, "%le, ", Ind[k].x[l]);
297
298 //fprintf(FileOut, "%le, %le, %i, %e, %i, %i", Ind[k].actual_fitness, Ind[k].fitness, Ind[k].rank, time_[k], threads_num[k],num_threads[k]);
299 //fprintf(FileOut, "%le, %le, %i, %e", Ind[k].actual_fitness, Ind[k].fitness, Ind[k].rank, time_[k]);
300 fprintf(FileOut, "\n");
301 fclose(FileOut);
302
303 clock_t t0 = clock();
304
305 Ind[k].actual_fitness -= Models[k].GetObjectiveFunctionValue();
306 epochs[k] += Models[k].EpochCount();
307 time_[k] = ((float)(clock() - t0))/CLOCKS_PER_SEC;
308 fprintf(FileOut, "%i, fitness=%le, time=%e, epochs=%i\n", k, Ind[k].actual_fitness, time_[k], epochs[k]);
309 fclose(FileOut);
310
311 }
312
313 Model_out = Models[maxfitness()];
314
315
316 inp.clear();
317 assignfitness_rank(GA_params.N);
318
319}
320
321template<class T>
323{
324
325 for (int i=0; i<GA_params.maxpop; i++)
326 Ind_old[i] = Ind[i];
327 int a = maxfitness();
328 Ind[0] = Ind_old[a];
329 Ind[1] = Ind_old[a];
330 for (int i=2; i<GA_params.maxpop; i+=2)
331 {
333 int j1 = fitdist.GetRand();
334 int j2 = fitdist.GetRand();
335 double x = GetRndUniF(0,1);
336 if (x<GA_params.pcross)
337 if (GA_params.cross_over_type == 1)
338 cross(Ind_old[j1], Ind_old[j2], Ind[i], Ind[min(i+1,GA_params.maxpop-1)]); //1 Breaking point
339 else
340 cross2p(Ind_old[j1], Ind_old[j2], Ind[i], Ind[min(i + 1, GA_params.maxpop - 1)]); //2 Breaking point
341 else
342 {
343 Ind[i] = Ind_old[j1];
344 Ind[i+1] = Ind_old[j2];
345 }
346
347 }
348
349}
350template<class T>
352{
353
354 for (int i=0; i<GA_params.maxpop; i++)
355 Ind_old[i] = Ind[i];
356 int a = maxfitness();
357 Ind[0] = Ind_old[a];
358 Ind[1] = Ind_old[a];
359 for (int i=2; i<GA_params.maxpop; i+=2)
360 {
361 int j1 = fitdist.GetRand();
362 int j2 = fitdist.GetRand();
363 double x = GetRndUnif(0,1);
364 if (x<GA_params.pcross)
365 cross_RC_L(Ind_old[j1], Ind_old[j2], Ind[i], Ind[i+1]);
366 else
367 {
368 Ind[i] = Ind_old[j1];
369 Ind[i+1] = Ind_old[j2];
370 }
371 }
372}
373template<class T>
375{
376 double sum=0;
377 for (int i=0; i<GA_params.maxpop; i++)
378 sum += Ind[i].fitness;
379 return sum/GA_params.maxpop;
380}
381
382template<class T>
384{
385 FILE *FileOut;
386
387 FileOut = fopen((filenames.pathname + "detail_GA.txt").c_str(), "a");
388 fprintf(FileOut, "%s\n", s.c_str());
389 fclose(FileOut);
390
391}
392
393template<class T>
395{
396 string RunFileName = filenames.pathname + filenames.outputfilename;
397
398 FILE *FileOut;
399 FILE *FileOut1;
400
401 FileOut = fopen(RunFileName.c_str(),"w");
402 fclose(FileOut);
403 FileOut1 = fopen((filenames.pathname + "detail_GA.txt").c_str(), "w");
404 fclose(FileOut1);
405
406 double shakescaleini = GA_params.shakescale;
407
408 vector<double> X(Ind[0].nParams);
409
410 Models.resize(GA_params.maxpop);
411
412 initialize();
413 double ininumenhancements = GA_params.numenhancements;
414 GA_params.numenhancements = 0;
415
416 CMatrix Fitness(GA_params.nGen, 3);
417
418 for (int i=0; i<GA_params.nGen; i++)
419 {
420
421 write_to_detailed_GA("Assigning fitnesses ...");
422 assignfitnesses();
423
424 write_to_detailed_GA("Assigning fitnesses done!");
425 FileOut = fopen(RunFileName.c_str(),"a");
426 printf("Generation: %i\n", i);
427 fprintf(FileOut, "Generation: %i\n", i);
428 fprintf(FileOut, "ID, ");
429 for (int k=0; k<Ind[0].nParams; k++)
430 fprintf(FileOut, "%s, ", paramname[k].c_str());
431 fprintf(FileOut, "%s, %s, %s", "likelihood", "Fitness", "Rank");
432 fprintf(FileOut, "\n");
433
434 for (int j1=0; j1<GA_params.maxpop; j1++)
435 {
436 write_to_detailed_GA("Generation: " + numbertostring(i));
437 fprintf(FileOut, "%i, ", j1);
438
439 for (int k=0; k<Ind[0].nParams; k++)
440 if (loged[get_act_paramno(k)] == 1)
441 fprintf(FileOut, "%le, ", pow(10, Ind[j1].x[k]));
442 else
443 fprintf(FileOut, "%le, ", Ind[j1].x[k]);
444
445 fprintf(FileOut, "%le, %le, %i", Ind[j1].actual_fitness, Ind[j1].fitness, Ind[j1].rank);
446 fprintf(FileOut, "\n");
447 }
448 fclose(FileOut);
449
450 int j = maxfitness();
451
452
453 Fitness[i][0] = Ind[j].actual_fitness;
454
455 if (i>10)
456 {
457 if ((Fitness[i][0] == Fitness[i - 3][0]) && GA_params.shakescale>pow(10.0, -Ind[0].precision[0]))
458 GA_params.shakescale *= GA_params.shakescalered;
459
460
461 if ((Fitness[i][0]>Fitness[i - 1][0]) && (GA_params.shakescale<shakescaleini))
462 GA_params.shakescale /= GA_params.shakescalered;
463 GA_params.numenhancements = 0;
464 }
465
466 if (i>50)
467 {
468 if ((Fitness[i][0] == Fitness[i - 20][0]))
469 {
470 GA_params.numenhancements *= 1.05;
471 if (GA_params.numenhancements == 0) GA_params.numenhancements = ininumenhancements;
472 }
473
474 if ((Fitness[i][0] == Fitness[i - 50][0]))
475 GA_params.numenhancements = ininumenhancements * 10;
476 }
477
478 Fitness[i][1] = GA_params.shakescale;
479 Fitness[i][2] = GA_params.pmute;
480
481 if (i>20)
482 {
483 if (GA_params.shakescale == Fitness[i - 20][1])
484 GA_params.shakescale = shakescaleini;
485 }
486
487
488 j = maxfitness();
489 MaxFitness = Ind[j].actual_fitness;
490
491 Fitness[i][0] = Ind[j].actual_fitness;
492
493
494 fillfitdist();
495
496 write_to_detailed_GA("Cross-over ...");
497
498 if (GA_params.RCGA == true)
499 crossoverRC();
500 else
501 crossover();
502
503 write_to_detailed_GA("Cross-over done! ");
504
505 write_to_detailed_GA("Mutation ...");
506
507 mutate(GA_params.pmute);
508 write_to_detailed_GA("Mutation done!");
509 write_to_detailed_GA("Shake...!");
510 shake();
511 write_to_detailed_GA("Shake done!");
512
513
514 }
515 assignfitnesses();
516 FileOut = fopen(RunFileName.c_str(), "a");
517 fprintf(FileOut, "Final Enhancements\n");
518 double l_MaxFitness = 1;
519 int j = maxfitness();
520
522 MaxFitness = Ind[j].actual_fitness;
523 final_params.resize(GA_params.nParam);
524
525
526 for (int k = 0; k<Ind[0].nParams; k++)
527 {
528 if (loged[get_act_paramno(k)] == 1) final_params[k] = pow(10, Ind[j].x[k]); else final_params[k] = Ind[j].x[k];
529 fprintf(FileOut, "%s, ", paramname[k].c_str());
530 fprintf(FileOut, "%le, ", final_params[k]);
531 fprintf(FileOut, "%le, %le\n", Ind[j].actual_fitness, Ind[j].fitness);
532 }
533 fclose(FileOut);
534
535 assignfitnesses(final_params);
537 Models.clear();
538
539 return maxfitness();
540}
541
542template<class T>
543double CGA<T>::assignfitnesses(vector<double> inp)
544{
546 double likelihood = 0;
547 T Models;
548 Models = Model;
549
550 int l = 0;
551 for (int i = 0; i < GA_params.nParam; i++)
552 Models.SetParam(i, inp[i]);
553
554 Models.FinalizeSetParams();
555
556 likelihood -= Models.EvaluateObjectiveFunction();
557
558 Model_out = Models;
559
560 return likelihood;
561
562}
563/*
564template<class T>
565vector<T>& CGA<T>::assignfitnesses_p(vector<double> inp) //Generates an instance of the model with the provided parameters
566{
567 double likelihood = 0;
568 vector<T> Models(1);
569 for (int ts = 0; ts<1; ts++)
570 {
571 Models[ts] = Model;
572
573 int l = 0;
574 for (int i = 0; i<GA_params.nParam; i++)
575 Models[ts].SetParam(params[i], inp[getparamno(i, ts)]);
576 Models[ts].FinalizeSetParams();
577 likelihood -= Models[ts].EvaluateObjectiveFunction();
579 return Models;
580}
581
582template<class T>
583int CGA<T>::get_act_paramno(int i)
584{
585 int l = -1;
586 for (int j = 0; j<GA_params.nParam; j++)
587 {
588 if (apply_to_all[j]) l++; else l += 1;
589 if (l >= i)
590 {
591 if (apply_to_all[j]) l -= 1; else l--;
592 return j;
593 }
594 }
595}
596*/
597template<class T>
598double CGA<T>::getfromoutput(string filename)
599{
600 ifstream file(filename);
601 vector<string> s;
602 final_params.resize(GA_params.nParam);
603 while (file.eof() == false)
604 {
605 s = getline(file);
606 if (s.size()>0)
607 {
608 if (s[0] == "Final Enhancements")
609 for (int i = 0; i<GA_params.nParam; i++)
610 {
611 s = getline(file);
612 if (s.size() == 0)
613 write_to_detailed_GA("The number of parameters in GA output file does not match the number of unknown parameters");
614 else
615 final_params[i] = atof(s[1].c_str());
616 }
617 }
618 }
619 double ret = assignfitnesses(final_params);
620 return ret;
621}
622
623
624template<class T>
625int CGA<T>::getparamno(int i, int ts)
626{
627 int l = 0;
628 for (int j = 0; j<i; j++)
629 if (apply_to_all[j]) l++; else l += 1;
630
631 if (apply_to_all[i])
632 return l;
633 else
634 return l + ts;
635
636}
637template<class T>
639{
640 int l = 0;
641 for (int j = 0; j<GA_params.nParam; j++)
642 {
643 if (apply_to_all[j]) l += 1; else l += 1;
644 if (l >= i)
645 {
646 if (apply_to_all[j]) l -= 1; else l--;
647 return i - l;
648 }
649 }
650}
651
652
653template<class T>
655{
656 for (int i=1; i<GA_params.maxpop; i++)
657 Ind[i].shake(GA_params.shakescale);
658
659}
660
661template<class T>
662void CGA<T>::mutate(double mu)
663{
664 for (int i=2; i<GA_params.maxpop; i++)
665 Ind[i].mutate(mu);
666
667}
668
669template<class T>
671{
672 double max_fitness = 1E+308 ;
673 int i_max = 0;
674 for (int i=0; i<GA_params.maxpop; i++)
675 if (max_fitness>Ind[i].actual_fitness)
676 {
677 max_fitness = Ind[i].actual_fitness;
678 i_max = i;
679 }
680 return i_max;
681
682}
683
684template<class T>
686{
687 double sum = 0;
688 double a = avgfitness();
689 for (int i=0; i<GA_params.maxpop; i++)
690 sum += (a - Ind[i].fitness)*(a - Ind[i].fitness);
691 return sum;
692
693}
694
695template<class T>
697{
698 double sum = 0;
699 double a = avg_inv_actual_fitness();
700 for (int i=0; i<GA_params.maxpop; i++)
701 sum += (a - 1/Ind[i].actual_fitness)*(a - 1/Ind[i].actual_fitness);
702 return sqrt(sum)/GA_params.maxpop/a;
703
704}
705
706template<class T>
708{
709 double sum=0;
710 for (int i=0; i<GA_params.maxpop; i++)
711 sum += Ind[i].actual_fitness;
712 return sum/GA_params.maxpop;
713
714}
715
716template<class T>
718{
719 double sum=0;
720 for (int i=0; i<GA_params.maxpop; i++)
721 sum += 1/Ind[i].actual_fitness;
722 return sum/GA_params.maxpop;
723
724}
725
726
727template<class T>
729{
730 for (int i=0; i<GA_params.maxpop; i++)
731 {
732 int r = 1;
733 for (int j=0; j<GA_params.maxpop; j++)
734 {
735 if (Ind[i].actual_fitness > Ind[j].actual_fitness) r++;
736 }
737 Ind[i].rank = r;
739
740}
741
742template<class T>
744{
745 assignrank();
746 for (int i=0; i<GA_params.maxpop; i++)
747 {
748 Ind[i].fitness = pow(1.0/static_cast<double>(Ind[i].rank),GA_params.N);
750}
751
752
753template<class T>
755{
756 double sum=0;
757 for (int i=0; i<GA_params.maxpop; i++)
758 {
759 sum+=Ind[i].fitness;
760 }
761
762 fitdist.s[0] = 0;
763 fitdist.e[0] = Ind[0].fitness/sum;
764 for (int i=1; i<GA_params.maxpop-1; i++)
765 {
766 fitdist.e[i] = fitdist.e[i-1] + Ind[i].fitness/sum;
767 fitdist.s[i] = fitdist.e[i-1];
768 }
769 fitdist.s[GA_params.maxpop-1] = fitdist.e[GA_params.maxpop-2];
770 fitdist.e[GA_params.maxpop-1] = 1;
771
772}
773
774template<class T>
776{
777 vector<double> v(1);
778 v[0] = 1;
779 CVector out(1);
780 int x_nParam = GA_params.nParam;
781 vector<int> x_params = params;
782 GA_params.nParam = 1;
783 params.resize(1);
784 params[0] = 100;
785 out[0] = assignfitnesses(v);
786
787 out.writetofile(filenames.pathname + "likelihood.txt");
788 params = x_params;
789 GA_params.nParam = x_nParam;
790 return out[0];
791}
792
793template<class T>
794void CGA<T>::getinifromoutput(string filename)
795{
796 ifstream file(filename);
797 vector<string> s;
798 initial_pop.resize(1);
799 initial_pop[0].resize(GA_params.nParam);
800 while (file.eof() == false)
801 {
802 s = getline(file);
803 if (s.size()>0)
804 { if (s[0] == "Final Enhancements")
805 for (int i=0; i<GA_params.nParam; i++)
806 {
807 s = getline(file);
808 if (loged[get_act_paramno(i)]==1)
809 initial_pop[0][i] = atof(s[1].c_str());
810 else
811 initial_pop[0][i] = atof(s[1].c_str());
812
813 }
815 }
816
817}
818
819template<class T>
820void CGA<T>::getinitialpop(string filename)
821{
822 ifstream file(filename);
823 vector<string> s;
824
825 while (file.eof() == false)
826 {
827 s = getline(file);
828
829 if (s.size()>0)
830 {
831 vector<double> x;
832 for (int j=0; j<s.size(); j++)
833 initial_pop.push_back(ATOF(s));
834
835 }
837 file.close();
838}
839
void cross2p(CBinary &B1, CBinary &B2, int p1, int p2)
Definition Binary.cpp:190
void cross(CBinary &B1, CBinary &B2, int p)
Definition Binary.cpp:142
void cross_RC_L(const CIndividual &I1, const CIndividual &I2, CIndividual &IR1, CIndividual &IR2)
double GetRndUnif(double xmin, double xmax)
Genetic Algorithm optimizer for global parameter estimation.
Definition GA.h:350
double MaxFitness
Maximum fitness value in current population.
Definition GA.h:366
void fillfitdist()
Fill fitness distribution for roulette wheel selection.
Definition GA.cpp:754
std::vector< int > params
Parameter indices being optimized.
Definition GA.h:415
int maxfitness()
Find index of individual with maximum fitness.
Definition GA.cpp:670
void assignrank()
Assign ranks to individuals based on fitness.
Definition GA.cpp:728
int get_time_series(int i)
Get time series index for parameter.
Definition GA.cpp:638
double avg_actual_fitness()
Calculate average of actual (untransformed) fitness values.
Definition GA.cpp:707
GA_Tweaking_parameters GA_params
GA algorithm configuration parameters.
Definition GA.h:374
void shake()
Apply shake/perturbation to population.
Definition GA.cpp:654
virtual ~CGA()
Destructor.
Definition GA.cpp:203
std::vector< CIndividual > Ind
Current population of individuals.
Definition GA.h:475
GADistribution fitdist
Fitness distribution for parent selection.
Definition GA.h:986
CGA operator=(CGA &C)
Assignment operator.
Definition GA.cpp:186
void setnumpop(int n)
Set population size.
Definition GA.cpp:142
double evaluateforward()
Evaluate model forward (compute predictions)
Definition GA.cpp:775
std::vector< std::string > paramname
Names of parameters being optimized.
Definition GA.h:491
void Setminmax(int a, double minrange, double maxrange, int prec)
Set parameter bounds and precision.
Definition GA.cpp:229
double avg_inv_actual_fitness()
Calculate average of inverse actual fitness.
Definition GA.cpp:717
void crossoverRC()
Real-coded crossover operation.
Definition GA.cpp:351
CGA()
Default constructor.
Definition GA.cpp:12
void assignfitnesses()
Evaluate fitness for all individuals in population.
Definition GA.cpp:241
void setnparams(int n)
Set number of parameters.
Definition GA.cpp:129
int optimize()
Main optimization loop.
Definition GA.cpp:394
void getinifromoutput(std::string filename)
Initialize population from previous output file.
Definition GA.cpp:794
double stdfitness()
Calculate standard deviation of population fitness.
Definition GA.cpp:696
int getparamno(int i, int ts)
Get parameter index for variable and time series.
Definition GA.cpp:625
void mutate(double mu)
Apply mutation to population.
Definition GA.cpp:662
std::vector< CIndividual > Ind_old
Previous generation's population.
Definition GA.h:483
void initialize()
Initialize GA structures and allocate memory.
Definition GA.cpp:209
void crossover()
Perform crossover operation on population.
Definition GA.cpp:322
double variancefitness()
Calculate variance of population fitness.
Definition GA.cpp:685
std::vector< int > loged
Flags indicating if parameters are log-transformed.
Definition GA.h:423
double getfromoutput(std::string filename)
Read best solution from previous output file.
Definition GA.cpp:598
void write_to_detailed_GA(std::string s)
Write detailed GA information to file.
Definition GA.cpp:383
void assignfitness_rank(double N)
Assign fitness using rank-based scheme.
Definition GA.cpp:743
void getinitialpop(std::string filename)
Read initial population from file.
Definition GA.cpp:820
double avgfitness()
Calculate average fitness of population.
Definition GA.cpp:374
_filenames filenames
File paths for GA input/output.
Definition GA.h:382
std::vector< int > precision
Definition Individual.h:22
std::vector< double > minrange
Definition Individual.h:23
std::vector< double > maxrange
Definition Individual.h:23