00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <string>
00019 #include <sstream>
00020 #include <iostream>
00021 #include <fstream>
00022 #include <algorithm>
00023 #include <functional>
00024 #include <cmath>
00025 #include <time.h>
00026
00027
00028
00029 #include "EvolutionaryAlgorithm.h"
00030
00031 namespace Flood
00032 {
00033
00034
00035
00079
00080 EvolutionaryAlgorithm::EvolutionaryAlgorithm(ObjectiveFunctional* new_objective_functional_pointer)
00081 : TrainingAlgorithm(new_objective_functional_pointer)
00082 {
00083 set_default();
00084 }
00085
00086
00087
00088
00130
00131 EvolutionaryAlgorithm::EvolutionaryAlgorithm(void) : TrainingAlgorithm()
00132 {
00133 set_default();
00134 }
00135
00136
00137
00138
00140
00141 EvolutionaryAlgorithm::~EvolutionaryAlgorithm(void)
00142 {
00143
00144 }
00145
00146
00147
00148
00149
00150
00152
00153 EvolutionaryAlgorithm::FitnessAssignmentMethod& EvolutionaryAlgorithm::get_fitness_assignment_method(void)
00154 {
00155 return(fitness_assignment_method);
00156 }
00157
00158
00159
00160
00162
00163 std::string EvolutionaryAlgorithm::get_fitness_assignment_method_name(void)
00164 {
00165 switch(fitness_assignment_method)
00166 {
00167 case LinearRanking:
00168 {
00169 return("LinearRanking");
00170 }
00171 break;
00172
00173 default:
00174 {
00175 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00176 << "std::string get_fitness_assignment_method(void) method." << std::endl
00177 << "Unknown fitness assignment method." << std::endl;
00178
00179 exit(1);
00180 }
00181 break;
00182 }
00183 }
00184
00185
00186
00187
00189
00190 EvolutionaryAlgorithm::SelectionMethod& EvolutionaryAlgorithm::get_selection_method(void)
00191 {
00192 return(selection_method);
00193 }
00194
00195
00196
00197
00199
00200 std::string EvolutionaryAlgorithm::get_selection_method_name(void)
00201 {
00202 switch(selection_method)
00203 {
00204 case RouletteWheel:
00205 {
00206 return("RouletteWheel");
00207 }
00208 break;
00209
00210 case StochasticUniversalSampling:
00211 {
00212 return("StochasticUniversalSampling");
00213 }
00214 break;
00215
00216 default:
00217 {
00218 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00219 << "std::string get_selection_method(void) method." << std::endl
00220 << "Unknown selection method." << std::endl;
00221
00222 exit(1);
00223 }
00224 break;
00225 }
00226 }
00227
00228
00229
00230
00232
00233 EvolutionaryAlgorithm::RecombinationMethod& EvolutionaryAlgorithm::get_recombination_method(void)
00234 {
00235 return(recombination_method);
00236 }
00237
00238
00239
00240
00242
00243 std::string EvolutionaryAlgorithm::get_recombination_method_name(void)
00244 {
00245 switch(recombination_method)
00246 {
00247 case Line:
00248 {
00249 return("Line");
00250 }
00251 break;
00252
00253 case Intermediate:
00254 {
00255 return("Intermediate");
00256 }
00257 break;
00258
00259 default:
00260 {
00261 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00262 << "std::string get_recombination_method_name(void) method." << std::endl
00263 << "Unknown recombination method." << std::endl;
00264
00265 exit(1);
00266 }
00267 break;
00268 }
00269 }
00270
00271
00272
00273
00275
00276 EvolutionaryAlgorithm::MutationMethod& EvolutionaryAlgorithm::get_mutation_method(void)
00277 {
00278 return(mutation_method);
00279 }
00280
00281
00282
00283
00285
00286 std::string EvolutionaryAlgorithm::get_mutation_method_name(void)
00287 {
00288 switch(mutation_method)
00289 {
00290 case Normal:
00291 {
00292 return("Normal");
00293 }
00294 break;
00295
00296 case Uniform:
00297 {
00298 return("Uniform");
00299 }
00300 break;
00301
00302 default:
00303 {
00304 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00305 << "std::string get_mutation_method_name(void) method." << std::endl
00306 << "Unknown mutation method." << std::endl;
00307
00308 exit(1);
00309 }
00310 break;
00311 }
00312 }
00313
00314
00315
00316
00318
00319 int EvolutionaryAlgorithm::get_population_size(void)
00320 {
00321 return(population.get_rows_number());
00322 }
00323
00324
00325
00326
00328
00329 Matrix<double>& EvolutionaryAlgorithm::get_population(void)
00330 {
00331 return(population);
00332 }
00333
00334
00335
00336
00338
00339 Vector<double>& EvolutionaryAlgorithm::get_evaluation(void)
00340 {
00341 return(evaluation);
00342 }
00343
00344
00345
00346
00348
00349 Vector<double>& EvolutionaryAlgorithm::get_fitness(void)
00350 {
00351 return(fitness);
00352 }
00353
00354
00355
00356
00358
00359 Vector<bool>& EvolutionaryAlgorithm::get_selection(void)
00360 {
00361 return(selection);
00362 }
00363
00364
00365
00366
00368
00369 bool EvolutionaryAlgorithm::get_reserve_population_history(void)
00370 {
00371 return(reserve_population_history);
00372 }
00373
00374
00375
00376
00378
00379 bool EvolutionaryAlgorithm::get_reserve_mean_norm_history(void)
00380 {
00381 return(reserve_mean_norm_history);
00382 }
00383
00384
00385
00386
00389
00390 bool EvolutionaryAlgorithm::get_reserve_standard_deviation_norm_history(void)
00391 {
00392 return(reserve_standard_deviation_norm_history);
00393 }
00394
00395
00396
00397
00400
00401 bool EvolutionaryAlgorithm::get_reserve_best_norm_history(void)
00402 {
00403 return(reserve_best_norm_history);
00404 }
00405
00406
00407
00408
00410
00411 bool EvolutionaryAlgorithm::get_reserve_mean_evaluation_history(void)
00412 {
00413 return(reserve_mean_evaluation_history);
00414 }
00415
00416
00417
00418
00421
00422 bool EvolutionaryAlgorithm::get_reserve_standard_deviation_evaluation_history(void)
00423 {
00424 return(reserve_standard_deviation_evaluation_history);
00425 }
00426
00427
00428
00429
00431
00432 bool EvolutionaryAlgorithm::get_reserve_best_evaluation_history(void)
00433 {
00434 return(reserve_best_evaluation_history);
00435 }
00436
00437
00438
00439
00441
00442 Vector< Matrix<double> >& EvolutionaryAlgorithm::get_population_history(void)
00443 {
00444 return(population_history);
00445 }
00446
00447
00448
00449
00451
00452 Vector<double>& EvolutionaryAlgorithm::get_mean_norm_history(void)
00453 {
00454 return(mean_norm_history);
00455 }
00456
00457
00458
00459
00461
00462 Vector<double>& EvolutionaryAlgorithm::get_standard_deviation_norm_history(void)
00463 {
00464 return(standard_deviation_norm_history);
00465 }
00466
00467
00468
00469
00471
00472 Vector<double>& EvolutionaryAlgorithm::get_best_norm_history(void)
00473 {
00474 return(best_norm_history);
00475 }
00476
00477
00478
00479
00481
00482 Vector<double>& EvolutionaryAlgorithm::get_mean_evaluation_history(void)
00483 {
00484 return(mean_evaluation_history);
00485 }
00486
00487
00488
00489
00491
00492 Vector<double>& EvolutionaryAlgorithm::get_standard_deviation_evaluation_history(void)
00493 {
00494 return(standard_deviation_evaluation_history);
00495 }
00496
00497
00498
00499
00501
00502 Vector<double>& EvolutionaryAlgorithm::get_best_evaluation_history(void)
00503 {
00504 return(best_evaluation_history);
00505 }
00506
00507
00508
00509
00512
00513 void EvolutionaryAlgorithm::set(void)
00514 {
00515 objective_functional_pointer = NULL;
00516
00517 set_default();
00518 }
00519
00520
00521
00522
00525
00526 void EvolutionaryAlgorithm::set(ObjectiveFunctional* new_objective_functional_pointer)
00527 {
00528 objective_functional_pointer = new_objective_functional_pointer;
00529
00530 set_default();
00531 }
00532
00533
00534
00535
00576
00577 void EvolutionaryAlgorithm::set_default(void)
00578 {
00579 MultilayerPerceptron* multilayer_perceptron_pointer = NULL;
00580
00581 if(objective_functional_pointer != NULL)
00582 {
00583 multilayer_perceptron_pointer = objective_functional_pointer->get_multilayer_perceptron_pointer();
00584
00585 multilayer_perceptron_pointer = objective_functional_pointer->get_multilayer_perceptron_pointer();
00586 }
00587
00588 int parameters_number = 0;
00589
00590 if(multilayer_perceptron_pointer != NULL)
00591 {
00592 parameters_number = multilayer_perceptron_pointer->get_parameters_number();
00593 }
00594 else
00595 {
00596 parameters_number = 0;
00597 }
00598
00599
00600
00601 fitness_assignment_method = LinearRanking;
00602
00603
00604
00605 selection_method = StochasticUniversalSampling;
00606
00607
00608
00609 recombination_method = Intermediate;
00610
00611
00612
00613 mutation_method = Normal;
00614
00615
00616
00617 int population_size = 10*parameters_number;
00618
00619 elitism = false;
00620 selective_pressure = 1.5;
00621
00622 recombination_size = 0.25;
00623
00624 if(parameters_number != 0)
00625 {
00626 mutation_rate = 1.0/(double)parameters_number;
00627 }
00628 else
00629 {
00630 mutation_rate = 0.0;
00631 }
00632
00633 mutation_range = 0.1;
00634
00635
00636
00637 evaluation_goal = -1.0e99;
00638 mean_evaluation_goal = -1.0e99;
00639 standard_deviation_evaluation_goal = 0.0;
00640
00641 maximum_time = 1.0e6;
00642
00643 maximum_generations_number = 1000;
00644
00645
00646
00647 population.set(population_size, parameters_number);
00648
00649 initialize_population_normal();
00650
00651
00652
00653 evaluation.set(population_size);
00654
00655
00656
00657 fitness.set(population_size);
00658
00659
00660
00661 selection.set(population_size);
00662
00663
00664
00665 reserve_population_history = false;
00666
00667 reserve_best_individual_history = false;
00668
00669 reserve_mean_norm_history = false;
00670 reserve_standard_deviation_norm_history = false;
00671 reserve_best_norm_history = false;
00672
00673 reserve_mean_evaluation_history = false;
00674 reserve_standard_deviation_evaluation_history = false;
00675 reserve_best_evaluation_history = false;
00676
00677 reserve_elapsed_time_history = false;
00678
00679
00680
00681 display_period = 100;
00682 }
00683
00684
00685
00686
00692
00693 void EvolutionaryAlgorithm::set_population_size(int new_population_size)
00694 {
00695
00696
00697 #ifdef _DEBUG
00698
00699 if(objective_functional_pointer == NULL)
00700 {
00701 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00702 << "void set_population_size(int) method." << std::endl
00703 << "Objective functional pointer cannot be NULL." << std::endl;
00704 exit(1);
00705 }
00706
00707 #endif
00708
00709 MultilayerPerceptron* multilayer_perceptron_pointer
00710 = objective_functional_pointer->get_multilayer_perceptron_pointer();
00711
00712
00713
00714 #ifdef _DEBUG
00715
00716 if(multilayer_perceptron_pointer == NULL)
00717 {
00718 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00719 << "void set_population_size(int) method." << std::endl
00720 << "Multilayer perceptron pointer cannot be NULL." << std::endl;
00721 exit(1);
00722 }
00723
00724 #endif
00725
00726 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
00727
00728 if(new_population_size < 4)
00729 {
00730 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00731 << "void set_population_size(int) method." << std::endl
00732 << "New population size must be equal or greater than 4." << std::endl;
00733
00734 exit(1);
00735 }
00736 else if(new_population_size%2 != 0)
00737 {
00738 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00739 << "void set_population_size(int) method." << std::endl
00740 << "New population size is not divisible by 2." << std::endl;
00741
00742 exit(1);
00743 }
00744 else
00745 {
00746
00747
00748 population.resize(new_population_size, parameters_number);
00749
00750 initialize_population_normal();
00751
00752
00753
00754 evaluation.resize(new_population_size);
00755
00756
00757
00758 fitness.resize(new_population_size);
00759
00760
00761
00762 selection.resize(new_population_size);
00763 }
00764 }
00765
00766
00767
00768
00775
00776 void EvolutionaryAlgorithm::set_fitness_assignment_method(const std::string& new_fitness_assignment_method_name)
00777 {
00778 if(new_fitness_assignment_method_name == "LinearRanking")
00779 {
00780 fitness_assignment_method = LinearRanking;
00781 }
00782 else
00783 {
00784 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00785 << "void set_fitness_assignment_method(const std::string&) method." << std::endl
00786 << "Unknown fitness assignment method: " << new_fitness_assignment_method_name << "." <<std::endl;
00787
00788 exit(1);
00789 }
00790 }
00791
00792
00793
00794
00802
00803 void EvolutionaryAlgorithm::set_selection_method(const std::string& new_selection_method_name)
00804 {
00805 if(new_selection_method_name == "RouletteWheel")
00806 {
00807 selection_method = RouletteWheel;
00808 }
00809 else if(new_selection_method_name == "StochasticUniversalSampling")
00810 {
00811 selection_method = StochasticUniversalSampling;
00812 }
00813 else
00814 {
00815 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00816 << "void set_selection_method(const std::string&) method." << std::endl
00817 << "Unknown selection method: " << new_selection_method_name << "." <<std::endl;
00818
00819 exit(1);
00820 }
00821 }
00822
00823
00824
00825
00833
00834 void EvolutionaryAlgorithm::set_recombination_method(const std::string& new_recombination_method_name)
00835 {
00836 if(new_recombination_method_name == "Line")
00837 {
00838 recombination_method = Line;
00839 }
00840 else if(new_recombination_method_name == "Intermediate")
00841 {
00842 recombination_method = Intermediate;
00843 }
00844 else
00845 {
00846 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00847 << "void set_recombination_method(const std::string&) method." << std::endl
00848 << "Unknown recombination method: " << new_recombination_method_name << "." <<std::endl;
00849
00850 exit(1);
00851 }
00852 }
00853
00854
00855
00856
00864
00865 void EvolutionaryAlgorithm::set_mutation_method(const std::string& new_mutation_method_name)
00866 {
00867 if(new_mutation_method_name == "Normal")
00868 {
00869 mutation_method = Normal;
00870 }
00871 else if(new_mutation_method_name == "Uniform")
00872 {
00873 mutation_method = Uniform;
00874 }
00875 else
00876 {
00877 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00878 << "void set_mutation_method(const std::string&) method." << std::endl
00879 << "Unknown mutationg method: " << new_mutation_method_name << "." <<std::endl;
00880
00881 exit(1);
00882 }
00883 }
00884
00885
00886
00887
00891
00892 void EvolutionaryAlgorithm::set_population(const Matrix<double>& new_population)
00893 {
00894
00895
00896 #ifdef _DEBUG
00897
00898 if(objective_functional_pointer == NULL)
00899 {
00900 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00901 << "void set_population(const Matrix<double>&) method." << std::endl
00902 << "Objective functional pointer cannot be NULL." << std::endl;
00903
00904 exit(1);
00905 }
00906
00907 MultilayerPerceptron* multilayer_perceptron_pointer
00908 = objective_functional_pointer->get_multilayer_perceptron_pointer();
00909
00910 if(multilayer_perceptron_pointer == NULL)
00911 {
00912 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00913 << "void set_population(const Matrix<double>&) method." << std::endl
00914 << "Multilayer perceptron pointer cannot be NULL." << std::endl;
00915
00916 exit(1);
00917 }
00918
00919 int population_size = get_population_size();
00920 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
00921
00922 if(new_population.get_rows_number() != population_size)
00923 {
00924 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00925 << "void set_population(const Matrix<double>&) method." << std::endl
00926 << "New population size is not equal to population size." << std::endl;
00927
00928 exit(1);
00929 }
00930 else if(new_population.get_columns_number() != parameters_number)
00931 {
00932 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00933 << "void set_population(const Matrix<double>&) method." << std::endl
00934 << "New number of parameters is not equal to number of parameters." << std::endl;
00935
00936 exit(1);
00937 }
00938
00939 #endif
00940
00941
00942
00943 population = new_population;
00944 }
00945
00946
00947
00948
00952
00953 void EvolutionaryAlgorithm::set_evaluation(const Vector<double>& new_evaluation)
00954 {
00955
00956
00957 #ifdef _DEBUG
00958
00959 int population_size = get_population_size();
00960
00961 if(new_evaluation.get_size() != population_size)
00962 {
00963 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00964 << "void set_evaluation(const Vector<double>&) method." << std::endl
00965 << "Size is not equal to population size." << std::endl;
00966
00967 exit(1);
00968 }
00969
00970 #endif
00971
00972
00973
00974 evaluation = new_evaluation;
00975 }
00976
00977
00978
00979
00983
00984 void EvolutionaryAlgorithm::set_fitness(const Vector<double>& new_fitness)
00985 {
00986
00987
00988 #ifdef _DEBUG
00989
00990 int population_size = get_population_size();
00991
00992 if(new_fitness.get_size() != population_size)
00993 {
00994 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
00995 << "void set_fitness(Vector<double>) method." << std::endl
00996 << "Size is not equal to population size." << std::endl;
00997
00998 exit(1);
00999 }
01000
01001 #endif
01002
01003
01004
01005 fitness = new_fitness;
01006 }
01007
01008
01009
01010
01014
01015 void EvolutionaryAlgorithm::set_selection(const Vector<bool>& new_selection)
01016 {
01017
01018
01019 #ifdef _DEBUG
01020
01021 int population_size = get_population_size();
01022
01023 if(new_selection.get_size() != population_size)
01024 {
01025 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01026 << "void set_selection(Vector<double>) method." << std::endl
01027 << "Size is not equal to population size." << std::endl
01028 << std::endl;
01029
01030 exit(1);
01031 }
01032
01033 #endif
01034
01035
01036
01037 selection = new_selection;
01038 }
01039
01040
01041
01042
01047
01048 void EvolutionaryAlgorithm::set_reserve_population_history(bool new_reserve_population_history)
01049 {
01050 reserve_population_history = new_reserve_population_history;
01051 }
01052
01053
01054
01055
01059
01060 void EvolutionaryAlgorithm::set_reserve_mean_norm_history(bool new_reserve_mean_norm_history)
01061 {
01062 reserve_mean_norm_history = new_reserve_mean_norm_history;
01063 }
01064
01065
01066
01067
01072
01073 void EvolutionaryAlgorithm::
01074 set_reserve_standard_deviation_norm_history(bool new_reserve_standard_deviation_norm_history)
01075 {
01076 reserve_standard_deviation_norm_history = new_reserve_standard_deviation_norm_history;
01077 }
01078
01079
01080
01081
01085
01086 void EvolutionaryAlgorithm::set_reserve_best_norm_history(bool new_reserve_best_norm_history)
01087 {
01088 reserve_best_norm_history = new_reserve_best_norm_history;
01089 }
01090
01091
01092
01093
01098
01099 void EvolutionaryAlgorithm::set_reserve_mean_evaluation_history(bool new_reserve_mean_evaluation_history)
01100 {
01101 reserve_mean_evaluation_history = new_reserve_mean_evaluation_history;
01102 }
01103
01104
01105
01106
01111
01112 void EvolutionaryAlgorithm
01113 ::set_reserve_standard_deviation_evaluation_history(bool new_reserve_standard_deviation_evaluation_history)
01114 {
01115 reserve_standard_deviation_evaluation_history = new_reserve_standard_deviation_evaluation_history;
01116 }
01117
01118
01119
01120
01125
01126 void EvolutionaryAlgorithm::set_reserve_best_evaluation_history(bool new_reserve_best_evaluation_history)
01127 {
01128 reserve_best_evaluation_history = new_reserve_best_evaluation_history;
01129 }
01130
01131
01132
01133
01138
01139 void EvolutionaryAlgorithm::set_reserve_all_training_history(bool new_reserve_all_training_history)
01140 {
01141
01142
01143 reserve_population_history = new_reserve_all_training_history;
01144
01145 reserve_best_individual_history = new_reserve_all_training_history;
01146
01147 reserve_mean_norm_history = new_reserve_all_training_history;
01148 reserve_standard_deviation_norm_history = new_reserve_all_training_history;
01149 reserve_best_norm_history = new_reserve_all_training_history;
01150
01151
01152
01153 reserve_mean_evaluation_history = new_reserve_all_training_history;
01154 reserve_standard_deviation_evaluation_history = new_reserve_all_training_history;
01155 reserve_best_evaluation_history = new_reserve_all_training_history;
01156
01157
01158
01159 reserve_elapsed_time_history = new_reserve_all_training_history;
01160 }
01161
01162
01163
01164
01169
01170 void EvolutionaryAlgorithm::set_population_history(const Vector< Matrix<double> >& new_population_history)
01171 {
01172 population_history = new_population_history;
01173 }
01174
01175
01176
01177
01182
01183 void EvolutionaryAlgorithm::set_mean_norm_history(const Vector<double>& new_mean_norm_history)
01184 {
01185 mean_norm_history = new_mean_norm_history;
01186 }
01187
01188
01189
01190
01195
01196 void EvolutionaryAlgorithm::
01197 set_standard_deviation_norm_history(const Vector<double>& new_standard_deviation_norm_history)
01198 {
01199 standard_deviation_norm_history = new_standard_deviation_norm_history;
01200 }
01201
01202
01203
01204
01209
01210 void EvolutionaryAlgorithm::set_best_norm_history(const Vector<double>& new_best_norm_history)
01211 {
01212 best_norm_history = new_best_norm_history;
01213 }
01214
01215
01216
01217
01222
01223 void EvolutionaryAlgorithm::set_mean_evaluation_history(const Vector<double>& new_mean_evaluation_history)
01224 {
01225 mean_evaluation_history = new_mean_evaluation_history;
01226 }
01227
01228
01229
01230
01235
01236 void EvolutionaryAlgorithm
01237 ::set_standard_deviation_evaluation_history(const Vector<double>& new_standard_evaluation_evaluation_history)
01238 {
01239 standard_deviation_evaluation_history = new_standard_evaluation_evaluation_history;
01240 }
01241
01242
01243
01244
01249
01250 void EvolutionaryAlgorithm::set_best_evaluation_history(const Vector<double>& new_best_evaluation_history)
01251 {
01252 best_evaluation_history = new_best_evaluation_history;
01253 }
01254
01255
01256
01257
01261
01262 Vector<double> EvolutionaryAlgorithm::get_individual(int i)
01263 {
01264
01265
01266 #ifdef _DEBUG
01267
01268 int population_size = get_population_size();
01269
01270 if(i >= population_size)
01271 {
01272 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01273 << "Vector<double> get_individual(int) method." << std::endl
01274 << "Index must be less than population size." << std::endl;
01275
01276 exit(1);
01277 }
01278
01279 #endif
01280
01281
01282
01283 Vector<double> individual = population.get_row(i);
01284
01285 return(individual);
01286 }
01287
01288
01289
01290
01291
01296
01297 void EvolutionaryAlgorithm::set_individual(int i, const Vector<double>& individual)
01298 {
01299
01300
01301 #ifdef _DEBUG
01302
01303 int size = individual.get_size();
01304
01305 MultilayerPerceptron* multilayer_perceptron_pointer
01306 = objective_functional_pointer->get_multilayer_perceptron_pointer();
01307
01308 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
01309
01310 int population_size = get_population_size();
01311
01312 if(i >= population_size)
01313 {
01314 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01315 << "set_individual(int, Vector<double>) method." << std::endl
01316 << "Index must be less than population size." << std::endl;
01317
01318 exit(1);
01319 }
01320 else if(size != parameters_number)
01321 {
01322 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01323 << "set_individual(int, Vector<double>) method." << std::endl
01324 << "Size must be equal to number of parameters." << std::endl;
01325
01326 exit(1);
01327 }
01328
01329 #endif
01330
01331
01332
01333 population.set_row(i, individual);
01334 }
01335
01336
01337
01338
01340
01341 Vector<double> EvolutionaryAlgorithm::calculate_population_norm(void)
01342 {
01343 int population_size = get_population_size();
01344
01345 Vector<double> population_norm(population_size);
01346
01347 for(int i = 0; i < population_size; i++)
01348 {
01349 Vector<double> individual = get_individual(i);
01350
01351 population_norm[i] = individual.calculate_norm();
01352 }
01353
01354 return(population_norm);
01355 }
01356
01357
01358
01359
01360 double EvolutionaryAlgorithm::calculate_mean_evaluation(void)
01361 {
01362 return(evaluation.calculate_mean());
01363 }
01364
01365
01366
01367
01368 double EvolutionaryAlgorithm::calculate_standard_deviation_evaluation(void)
01369 {
01370 return(evaluation.calculate_standard_deviation());
01371 }
01372
01373
01374
01375
01376
01377
01378
01379 bool EvolutionaryAlgorithm::get_elitism(void)
01380 {
01381 return(elitism);
01382 }
01383
01384
01385
01386
01387 double EvolutionaryAlgorithm::get_selective_pressure(void)
01388 {
01389 return(selective_pressure);
01390 }
01391
01392
01393
01394
01396
01397 double EvolutionaryAlgorithm::get_recombination_size(void)
01398 {
01399 return(recombination_size);
01400 }
01401
01402
01403
01404
01406
01407 double EvolutionaryAlgorithm::get_mutation_rate(void)
01408 {
01409 return(mutation_rate);
01410 }
01411
01412
01413
01414
01416
01417 double EvolutionaryAlgorithm::get_mutation_range(void)
01418 {
01419 return(mutation_range);
01420 }
01421
01422
01423
01424
01426
01427 double EvolutionaryAlgorithm::get_maximum_generations_number(void)
01428 {
01429 return(maximum_generations_number);
01430 }
01431
01432
01433
01434
01435 double EvolutionaryAlgorithm::get_mean_evaluation_goal(void)
01436 {
01437 return(mean_evaluation_goal);
01438 }
01439
01440
01441
01442
01443 double EvolutionaryAlgorithm::get_standard_deviation_evaluation_goal(void)
01444 {
01445 return(standard_deviation_evaluation_goal);
01446 }
01447
01448
01449
01450
01451
01452
01453 void EvolutionaryAlgorithm::set_elitism(bool new_elitism)
01454 {
01455 elitism = new_elitism;
01456 }
01457
01458
01459
01460
01466
01467 void EvolutionaryAlgorithm::set_selective_pressure(double new_selective_pressure)
01468 {
01469 switch(fitness_assignment_method)
01470 {
01471 case LinearRanking:
01472 {
01473 if(new_selective_pressure < 1.0 || new_selective_pressure > 2.0)
01474 {
01475 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01476 << "void set_selective_pressure(double) method. "
01477 << "Case linear ranking." << std::endl
01478 << "Selective pressure must be a value between 1 and 2." << std::endl;
01479
01480 exit(1);
01481 }
01482
01483
01484
01485 selective_pressure = new_selective_pressure;
01486 }
01487 break;
01488 }
01489 }
01490
01491
01492
01493
01498
01499 void EvolutionaryAlgorithm::set_recombination_size(double new_recombination_size)
01500 {
01501 if(new_recombination_size < 0.0)
01502 {
01503 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01504 << "void set_recombination_size(double) method." << std::endl
01505 << "Recombination size must be equal or greater than 0." << std::endl;
01506
01507 exit(1);
01508 }
01509
01510
01511
01512 recombination_size = new_recombination_size;
01513 }
01514
01515
01516
01517
01522
01523 void EvolutionaryAlgorithm::set_mutation_rate(double new_mutation_rate)
01524 {
01525
01526
01527 if(new_mutation_rate < 0.0 || new_mutation_rate > 1.0)
01528 {
01529 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01530 << "void set_mutation_rate(double) method." << std::endl
01531 << "Mutation rate must be a value between 0 and 1. " << std::endl;
01532
01533 exit(1);
01534 }
01535
01536
01537
01538 mutation_rate = new_mutation_rate;
01539 }
01540
01541
01542
01543
01548
01549 void EvolutionaryAlgorithm::set_mutation_range(double new_mutation_range)
01550 {
01551
01552
01553 if(new_mutation_range < 0.0)
01554 {
01555 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01556 << "void set_mutation_range(double) method." << std::endl
01557 << "Mutation range must be equal or greater than 0. " << std::endl;
01558
01559 exit(1);
01560 }
01561
01562
01563
01564 mutation_range = new_mutation_range;
01565 }
01566
01567
01568
01569
01574
01575 void EvolutionaryAlgorithm::set_maximum_generations_number(int new_maximum_generations_number)
01576 {
01577
01578
01579 #ifdef _DEBUG
01580
01581 if(new_maximum_generations_number == 0)
01582 {
01583 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01584 << "void set_maximum_generations_number(int) method." << std::endl
01585 << "Maximum number of generations must be greater than 0. " << std::endl;
01586
01587 exit(1);
01588 }
01589
01590 #endif
01591
01592
01593
01594 maximum_generations_number = new_maximum_generations_number;
01595 }
01596
01597
01598
01599
01600
01601 void EvolutionaryAlgorithm::set_mean_evaluation_goal(double new_mean_evaluation_goal)
01602 {
01603 mean_evaluation_goal = new_mean_evaluation_goal;
01604 }
01605
01606
01607
01608
01609 void EvolutionaryAlgorithm::set_standard_deviation_evaluation_goal(double new_standard_deviation_evaluation_goal)
01610 {
01611
01612
01613 #ifdef _DEBUG
01614
01615 if(new_standard_deviation_evaluation_goal < 0.0)
01616 {
01617 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01618 << "void set_standard_deviation_evaluation_goal(double) method." << std::endl
01619 << "Standard deviation of evaluation goal must be equal or greater than 0." << std::endl;
01620
01621 exit(1);
01622 }
01623
01624 #endif
01625
01626
01627
01628 standard_deviation_evaluation_goal = new_standard_deviation_evaluation_goal;
01629
01630 }
01631
01632
01633
01637
01638 void EvolutionaryAlgorithm::set_fitness_assignment_method
01639 (const EvolutionaryAlgorithm::FitnessAssignmentMethod& new_fitness_assignment_method)
01640 {
01641 fitness_assignment_method = new_fitness_assignment_method;
01642 }
01643
01644
01645
01646
01650
01651 void EvolutionaryAlgorithm::
01652 set_selection_method(const EvolutionaryAlgorithm::SelectionMethod& new_selection_method)
01653 {
01654 selection_method = new_selection_method;
01655 }
01656
01657
01658
01659
01663
01664 void EvolutionaryAlgorithm
01665 ::set_recombination_method(const EvolutionaryAlgorithm::RecombinationMethod& new_recombination_method)
01666 {
01667 recombination_method = new_recombination_method;
01668 }
01669
01670
01671
01672
01676
01677 void EvolutionaryAlgorithm::set_mutation_method(const EvolutionaryAlgorithm::MutationMethod& new_mutation_method)
01678 {
01679 mutation_method = new_mutation_method;
01680 }
01681
01682
01683
01684
01685
01686
01687 void EvolutionaryAlgorithm::initialize_population(double new_value)
01688 {
01689 population.initialize(new_value);
01690 }
01691
01692
01693
01694
01695
01698
01699 void EvolutionaryAlgorithm::initialize_population_uniform(void)
01700 {
01701 population.initialize_uniform();
01702 }
01703
01704
01705
01706
01712
01713 void EvolutionaryAlgorithm::initialize_population_uniform(double minimum, double maximum)
01714 {
01715 population.initialize_uniform(minimum, maximum);
01716 }
01717
01718
01719
01720
01726
01727 void EvolutionaryAlgorithm::
01728 initialize_population_uniform(const Vector<double>& minimum, const Vector<double>& maximum)
01729 {
01730 MultilayerPerceptron* multilayer_perceptron_pointer
01731 = objective_functional_pointer->get_multilayer_perceptron_pointer();
01732
01733 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
01734
01735
01736
01737 #ifdef _DEBUG
01738
01739 int minimum_size = minimum.get_size();
01740 int maximum_size = maximum.get_size();
01741
01742 if(minimum_size != parameters_number || maximum_size != parameters_number)
01743 {
01744 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01745 << "void initialize_population_uniform(Vector<double>, Vector<double>)." << std::endl
01746 << "Minimum value and maximum value sizes must be equal to number of parameters." << std::endl;
01747
01748 exit(1);
01749 }
01750
01751 #endif
01752
01753 Vector<double> individual(parameters_number);
01754
01755 int population_size = get_population_size();
01756
01757 for(int i = 0; i < population_size; i++)
01758 {
01759 individual.initialize_uniform(minimum, maximum);
01760
01761 set_individual(i, individual);
01762 }
01763 }
01764
01765
01766
01767
01770
01771 void EvolutionaryAlgorithm::initialize_population_normal(void)
01772 {
01773 population.initialize_normal();
01774 }
01775
01776
01777
01778
01784
01785 void EvolutionaryAlgorithm::initialize_population_normal(double mean, double standard_deviation)
01786 {
01787 population.initialize_normal(mean, standard_deviation);
01788 }
01789
01790
01791
01792
01798
01799 void EvolutionaryAlgorithm::
01800 initialize_population_normal(const Vector<double>& mean, const Vector<double>& standard_deviation)
01801 {
01802 MultilayerPerceptron* multilayer_perceptron_pointer
01803 = objective_functional_pointer->get_multilayer_perceptron_pointer();
01804
01805 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
01806
01807
01808
01809 #ifdef _DEBUG
01810
01811 int mean_size = mean.get_size();
01812 int standard_deviation_size = standard_deviation.get_size();
01813
01814 if(mean_size != parameters_number || standard_deviation_size != parameters_number)
01815 {
01816 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01817 << "void initialize_population_normal(Vector<double>, Vector<double>)." << std::endl
01818 << "Mean and standard deviation sizes must be equal to number of parameters." << std::endl;
01819
01820 exit(1);
01821 }
01822
01823 #endif
01824
01825 Vector<double> individual(parameters_number);
01826
01827 int population_size = get_population_size();
01828
01829 for(int i = 0; i < population_size; i++)
01830 {
01831 individual.initialize_normal(mean, standard_deviation);
01832
01833 set_individual(i, individual);
01834 }
01835 }
01836
01837
01838
01839
01840 void EvolutionaryAlgorithm::perform_fitness_assignment(void)
01841 {
01842 switch(fitness_assignment_method)
01843 {
01844 case LinearRanking:
01845 {
01846 perform_linear_ranking_fitness_assignment();
01847 }
01848
01849 break;
01850
01851 default:
01852 {
01853 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01854 << "void perform_fitness_assignment(void)." << std::endl
01855 << "Unknown fitness assignment method." << std::endl;
01856
01857 exit(1);
01858 }
01859 break;
01860 }
01861 }
01862
01863
01864
01865
01866 void EvolutionaryAlgorithm::perform_selection(void)
01867 {
01868 switch(selection_method)
01869 {
01870 case RouletteWheel:
01871 {
01872 perform_roulette_wheel_selection();
01873 }
01874 break;
01875
01876 case StochasticUniversalSampling:
01877 {
01878 perform_stochastic_universal_sampling_selection();
01879 }
01880 break;
01881
01882 default:
01883 {
01884 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01885 << "void perform_selection(void)." << std::endl
01886 << "Unknown selection method." << std::endl;
01887
01888 exit(1);
01889 }
01890 break;
01891 }
01892
01893 }
01894
01895
01896
01897
01898 void EvolutionaryAlgorithm::perform_recombination(void)
01899 {
01900 switch(recombination_method)
01901 {
01902 case Intermediate:
01903 {
01904 perform_intermediate_recombination();
01905 }
01906 break;
01907
01908 case Line:
01909 {
01910 perform_line_recombination();
01911 }
01912 break;
01913
01914 default:
01915 {
01916 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01917 << "void perform_recombination(void)." << std::endl
01918 << "Unknown recombination method." << std::endl;
01919
01920 exit(1);
01921 }
01922 break;
01923 }
01924 }
01925
01926
01927
01928
01929 void EvolutionaryAlgorithm::perform_mutation(void)
01930 {
01931 switch(mutation_method)
01932 {
01933 case Normal:
01934 {
01935 perform_normal_mutation();
01936 }
01937 break;
01938
01939 case Uniform:
01940 {
01941 perform_uniform_mutation();
01942 }
01943 break;
01944
01945 default:
01946 {
01947 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01948 << "void perform_mutationg(void)." << std::endl
01949 << "Unknown mutation method." << std::endl;
01950
01951 exit(1);
01952 }
01953 break;
01954 }
01955 }
01956
01957
01958
01959
01960 void EvolutionaryAlgorithm::evolve_population(void)
01961 {
01962
01963
01964 perform_fitness_assignment();
01965
01966
01967
01968 perform_selection();
01969
01970
01971
01972 perform_recombination();
01973
01974
01975
01976 perform_mutation();
01977 }
01978
01979
01980
01981
01984
01985 void EvolutionaryAlgorithm::evaluate_population(void)
01986 {
01987
01988
01989 #ifdef _DEBUG
01990
01991 if(objective_functional_pointer == NULL)
01992 {
01993 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
01994 << "void evaluate_population(void)." << std::endl
01995 << "Objective functional pointer cannot be NULL." << std::endl;
01996
01997 exit(1);
01998 }
01999
02000 #endif
02001
02002
02003
02004 MultilayerPerceptron* multilayer_perceptron_pointer
02005 = objective_functional_pointer->get_multilayer_perceptron_pointer();
02006
02007
02008
02009 #ifdef _DEBUG
02010
02011 if(multilayer_perceptron_pointer == NULL)
02012 {
02013 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
02014 << "void evaluate_population(void)." << std::endl
02015 << "Multilayer perceptron pointer cannot be NULL." << std::endl;
02016
02017 exit(1);
02018 }
02019
02020 #endif
02021
02022 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
02023
02024 Vector<double> individual(parameters_number);
02025
02026
02027
02028 int population_size = get_population_size();
02029
02030 for(int i = 0; i < population_size; i++)
02031 {
02032 individual = get_individual(i);
02033
02034 evaluation[i] = objective_functional_pointer->calculate_potential_evaluation(individual);
02035
02036 if(!(evaluation[i] > -1.0e99 && evaluation[i] < 1.0e99))
02037 {
02038 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
02039 << "void evaluate_population(void) method." << std::endl
02040 << "Evaluation of individual " << i << " is not a real number." << std::endl;
02041
02042 exit(1);
02043 }
02044 }
02045 }
02046
02047
02048
02049
02054
02055 void EvolutionaryAlgorithm::perform_linear_ranking_fitness_assignment(void)
02056 {
02057
02058
02059 int population_size = get_population_size();
02060
02061 Vector<double> sorted_evaluation(population_size);
02062
02063 sorted_evaluation = evaluation;
02064
02065 std::sort(sorted_evaluation.begin(), sorted_evaluation.end(), std::less<double>());
02066
02067
02068
02069 Vector<int> rank(population_size);
02070
02071 for(int i = 0; i < population_size; i++)
02072 {
02073 for(int j = 0; j < population_size; j++)
02074 {
02075 if(evaluation[j] == sorted_evaluation[i])
02076 {
02077 rank[j] = population_size - i;
02078 }
02079 }
02080 }
02081
02082
02083
02084 for(int i = 0; i < population_size; i++)
02085 {
02086 fitness[i] = 2.0 - selective_pressure
02087 + 2.0*(selective_pressure - 1.0)*(rank[i] - 1.0)/(population_size - 1.0);
02088
02089 if(!(fitness[i] > -1.0e99 && fitness[i] < 1.0e99))
02090 {
02091 std::cerr << "Flooe Error: EvolutionaryAlgorithm class." << std::endl
02092 << "void perform_linear_ranking_fitness_assignment(void) method." << std::endl
02093 << "Fitness of individual " << i << " is not a real number." << std::endl;
02094
02095 exit(1);
02096 }
02097 }
02098 }
02099
02100
02101
02102
02106
02107 void EvolutionaryAlgorithm::perform_roulette_wheel_selection(void)
02108 {
02109
02110
02111 selection.initialize(false);
02112
02113 int population_size = get_population_size();
02114
02115 int selected_individuals_number = population_size/2;
02116
02117
02118
02119 Vector<double> cumulative_fitness(population_size);
02120
02121 cumulative_fitness[0] = fitness[0];
02122
02123 for(int i = 1; i < population_size; i++)
02124 {
02125 cumulative_fitness[i] = cumulative_fitness[i-1] + fitness[i];
02126 }
02127
02128
02129
02130 int selected_individuals_count = 0;
02131
02132 if(elitism)
02133 {
02134 int best_individual_index = evaluation.calculate_minimal_index();
02135
02136 selection[best_individual_index] = true;
02137
02138 selected_individuals_count++;
02139 }
02140
02141 do
02142 {
02143
02144
02145 double random = (double)rand()/(RAND_MAX+1.0);
02146
02147 double pointer = cumulative_fitness[population_size-1]*random;
02148
02149
02150
02151 if(pointer < cumulative_fitness[0])
02152 {
02153 if(selection[0] == false)
02154 {
02155 selection[0] = true;
02156 selected_individuals_count++;
02157 }
02158 }
02159
02160 for(int i = 1; i < population_size; i++)
02161 {
02162 if(pointer < cumulative_fitness[i] && pointer >= cumulative_fitness[i-1])
02163 {
02164 if(selection[i] == false)
02165 {
02166 selection[i] = true;
02167 selected_individuals_count++;
02168 }
02169 }
02170 }
02171 }while(selected_individuals_count != selected_individuals_number);
02172
02173
02174
02175 if(selected_individuals_count != selected_individuals_number)
02176 {
02177 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
02178 << "void perform_roulette_wheel_selection(void) method." << std::endl
02179 << "Count number of selected individuals is not equal to number of selected individuals." << std::endl;
02180
02181 exit(1);
02182 }
02183 }
02184
02185
02186
02187
02191
02192 void EvolutionaryAlgorithm::perform_stochastic_universal_sampling_selection(void)
02193 {
02194
02195
02196 selection.initialize(false);
02197
02198 int population_size = get_population_size();
02199
02200 int selected_individuals_number = population_size/2;
02201
02202 Vector<double> cumulative_fitness(population_size);
02203
02204 Vector<double> pointer(selected_individuals_number);
02205
02206
02207
02208 cumulative_fitness[0] = fitness[0];
02209
02210 for(int i = 1; i < population_size; i++)
02211 {
02212 cumulative_fitness[i] = cumulative_fitness[i-1] + fitness[i];
02213 }
02214
02215
02216
02217
02218
02219
02220 double random = (double)rand()/(RAND_MAX+1.0);
02221
02222 pointer[0] = random
02223 *cumulative_fitness[population_size-1]/(double)selected_individuals_number;
02224
02225 for(int i = 1; i < selected_individuals_number; i++)
02226 {
02227 pointer[i] = pointer[i-1]
02228 + cumulative_fitness[population_size-1]/(double)selected_individuals_number;
02229 }
02230
02231
02232
02233 int selected_individuals_count = 0;
02234
02235
02236
02237
02238
02239
02240
02241
02242
02243
02244 if(pointer[0] <= cumulative_fitness[0])
02245 {
02246 selection[0] = true;
02247 selected_individuals_count++;
02248 }
02249
02250 for(int i = 0; i < selected_individuals_number; i++)
02251 {
02252 for(int j = 1; j < population_size; j++)
02253 {
02254 if(pointer[i] <= cumulative_fitness[j] && pointer[i] > cumulative_fitness[j-1])
02255 {
02256 selection[j] = true;
02257 selected_individuals_count++;
02258 }
02259 }
02260 }
02261
02262
02263
02264 if(selected_individuals_count != selected_individuals_number)
02265 {
02266 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
02267 << "void perform_stochastic_universal_sampling_selection(void) method." << std::endl
02268 << "Count number of selected individuals is not equal to number of selected individuals." << std::endl;
02269
02270 exit(1);
02271 }
02272 }
02273
02274
02275
02276
02281
02282 void EvolutionaryAlgorithm::perform_intermediate_recombination(void)
02283 {
02284 int population_size = get_population_size();
02285
02286 MultilayerPerceptron* multilayer_perceptron_pointer
02287 = objective_functional_pointer->get_multilayer_perceptron_pointer();
02288
02289 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
02290
02291 Matrix<double> new_population(population_size, parameters_number);
02292
02293 int count = 0;
02294 for(int i = 0; i < population_size; i++)
02295 {
02296 if(selection[i] == true)
02297 count ++;
02298 }
02299
02300 Vector<double> parent_1(parameters_number);
02301 Vector<double> parent_2(parameters_number);
02302
02303 Vector<double> offspring(parameters_number);
02304
02305 Matrix<int> recombination(population_size, 2);
02306
02307
02308
02309 int new_population_size_count = 0;
02310
02311 for(int i = 0; i < population_size; i++)
02312 {
02313 if(selection[i] == true)
02314 {
02315
02316
02317 parent_1 = get_individual(i);
02318
02319
02320
02321 for(int j = 0; j < 2; j++)
02322 {
02323
02324
02325 bool parent_2_candidate = false;
02326
02327 do{
02328
02329
02330 double random = (double)rand()/(RAND_MAX+1.0);
02331
02332 int parent_2_candidate_index = (int)(population_size*random);
02333
02334
02335
02336 if(selection[parent_2_candidate_index] == true && parent_2_candidate_index != i)
02337 {
02338 parent_2_candidate = true;
02339
02340 recombination[new_population_size_count][0] = i;
02341
02342 recombination[new_population_size_count][1] = parent_2_candidate_index;
02343
02344 parent_2 = get_individual(parent_2_candidate_index);
02345
02346
02347
02348 for(int j = 0; j < parameters_number; j++)
02349 {
02350
02351
02352
02353
02354 double random = (double)rand()/(RAND_MAX+1.0);
02355
02356 double scaling_factor = -1.0*recombination_size + (1.0 + recombination_size)*random;
02357
02358 offspring[j] = scaling_factor*parent_1[j] + (1.0 - scaling_factor)*parent_2[j];
02359 }
02360
02361
02362
02363 new_population.set_row(new_population_size_count, offspring);
02364
02365 new_population_size_count++;
02366 }
02367 }while(parent_2_candidate != true);
02368 }
02369 }
02370 }
02371
02372
02373
02374 if(new_population_size_count != population_size)
02375 {
02376 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
02377 << "void perform_intermediate_recombination(void) method." << std::endl
02378 << "Count new population size is not equal to population size." << std::endl;
02379
02380 exit(1);
02381 }
02382
02383
02384
02385 population = new_population;
02386 }
02387
02388
02389
02390
02394
02395 void EvolutionaryAlgorithm::perform_line_recombination(void)
02396 {
02397 int population_size = get_population_size();
02398
02399 MultilayerPerceptron* multilayer_perceptron_pointer
02400 = objective_functional_pointer->get_multilayer_perceptron_pointer();
02401
02402 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
02403
02404 Matrix<double> new_population(population_size, parameters_number);
02405
02406 Vector<double> parent_1(parameters_number);
02407 Vector<double> parent_2(parameters_number);
02408
02409 Vector<double> offspring(parameters_number);
02410 Vector<double> parent_1_term(parameters_number);
02411 Vector<double> parent_2_term(parameters_number);
02412
02413 Matrix<int> recombination(population_size, 2);
02414
02415
02416
02417 int new_population_size_count = 0;
02418
02419 for(int i = 0; i < population_size; i++)
02420 {
02421 if(selection[i] == true)
02422 {
02423
02424
02425 parent_1 = get_individual(i);
02426
02427
02428
02429 for(int j = 0; j < 2; j++)
02430 {
02431
02432
02433 bool parent_2_candidate = false;
02434
02435 do
02436 {
02437
02438
02439 double random = (double)rand()/(RAND_MAX + 1.0);
02440
02441 int parent_2_candidate_index = (int)(population_size*random);
02442
02443
02444
02445 if(selection[parent_2_candidate_index] == true && parent_2_candidate_index != i)
02446 {
02447 parent_2_candidate = true;
02448
02449 recombination[new_population_size_count][0] = i;
02450 recombination[new_population_size_count][1] = parent_2_candidate_index;
02451
02452 parent_2 = get_individual(parent_2_candidate_index);
02453
02454
02455
02456
02457
02458
02459
02460
02461 double random = (double)rand()/(RAND_MAX+1.0);
02462
02463 double scaling_factor = -1.0*recombination_size
02464 + (1.0 + recombination_size)*random;
02465
02466 parent_1_term = parent_1*scaling_factor;
02467 parent_2_term = parent_2*(1.0 - scaling_factor);
02468
02469 offspring = parent_1_term + parent_2_term;
02470
02471
02472
02473 new_population.set_row(new_population_size_count, offspring);
02474
02475 new_population_size_count++;
02476 }
02477 }while(parent_2_candidate == false);
02478 }
02479 }
02480 }
02481
02482
02483
02484 if(new_population_size_count != population_size)
02485 {
02486 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
02487 << "void perform_line_recombination(void) method." << std::endl
02488 << "Count new population size is not equal to population size." << std::endl;
02489
02490 exit(1);
02491 }
02492
02493
02494
02495 population = new_population;
02496 }
02497
02498
02499
02500
02503
02504 void EvolutionaryAlgorithm::perform_normal_mutation(void)
02505 {
02506 int population_size = get_population_size();
02507
02508 MultilayerPerceptron* multilayer_perceptron_pointer = objective_functional_pointer->get_multilayer_perceptron_pointer();
02509
02510 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
02511
02512 Vector<double> individual(parameters_number);
02513
02514 for(int i = 0; i < population_size; i++)
02515 {
02516 individual = get_individual(i);
02517
02518 for(int j = 0; j < parameters_number; j++)
02519 {
02520
02521
02522 double pointer = calculate_random_uniform(0.0, 1.0);
02523
02524 if(pointer < mutation_rate)
02525 {
02526 individual[j] += calculate_random_normal(0.0, mutation_range);
02527 }
02528 }
02529
02530 set_individual(i, individual);
02531 }
02532 }
02533
02534
02535
02536
02539
02540 void EvolutionaryAlgorithm::perform_uniform_mutation(void)
02541 {
02542 int population_size = get_population_size();
02543
02544 MultilayerPerceptron* multilayer_perceptron_pointer = objective_functional_pointer->get_multilayer_perceptron_pointer();
02545
02546 int parameters_number = multilayer_perceptron_pointer->get_parameters_number();
02547
02548 Vector<double> individual(parameters_number, 0.0);
02549
02550 for(int i = 0; i < population_size; i++)
02551 {
02552 individual = get_individual(i);
02553
02554 for(int j = 0; j < parameters_number; j++)
02555 {
02556
02557
02558 double pointer = (double)rand()/(RAND_MAX+1.0);
02559
02560 if(pointer < mutation_rate)
02561 {
02562
02563
02564 double random = (double)rand()/(RAND_MAX+1.0);
02565
02566 double uniformlyDistributedRandomNumber
02567 = (-1.0 + 2.0*random)*mutation_range;
02568
02569 individual[j] += uniformlyDistributedRandomNumber;
02570 }
02571 }
02572
02573 set_individual(i, individual);
02574 }
02575 }
02576
02577
02578
02579
02584
02585 void EvolutionaryAlgorithm::train(void)
02586 {
02587
02588
02589 #ifdef _DEBUG
02590
02591 if(objective_functional_pointer == NULL)
02592 {
02593 std::cout << "Flood Error: EvolutionaryAlgorithm class." << std::endl
02594 << "void train(void) method." << std::endl
02595 << "Pointer to objective functional object cannot be NULL." << std::endl;
02596
02597 exit(1);
02598 }
02599
02600 #endif
02601
02602 if(display)
02603 {
02604 std::cout << "Training with the evolutionary algorithm..." << std::endl;
02605 }
02606
02607
02608
02609 MultilayerPerceptron* multilayer_perceptron_pointer = objective_functional_pointer->get_multilayer_perceptron_pointer();
02610
02611 Vector<double> population_norm;
02612
02613 double mean_norm;
02614 double standard_deviation_norm;
02615
02616 Vector<double> best_individual;
02617 double best_norm = 0.0;
02618
02619
02620
02621 double best_evaluation = 1.0e99;
02622
02623
02624
02625 time_t beginning_time, current_time;
02626 time(&beginning_time);
02627 double elapsed_time;
02628
02629 bool stop_training = false;
02630
02631 resize_training_history(maximum_generations_number+1);
02632
02633 for(int generation = 0; generation <= maximum_generations_number; generation++)
02634 {
02635
02636
02637 if(reserve_population_history)
02638 {
02639 population_history[generation] = population;
02640 }
02641
02642 population_norm = calculate_population_norm();
02643
02644
02645
02646 mean_norm = population_norm.calculate_mean();
02647
02648 if(reserve_mean_norm_history)
02649 {
02650 mean_norm_history[generation] = mean_norm;
02651 }
02652
02653
02654
02655 standard_deviation_norm = population_norm.calculate_standard_deviation();
02656
02657 if(reserve_standard_deviation_norm_history)
02658 {
02659 standard_deviation_norm_history[generation] = standard_deviation_norm;
02660 }
02661
02662 evaluate_population();
02663
02664 int population_size = get_population_size();
02665
02666 for(int i = 0; i < population_size; i++)
02667 {
02668 if(evaluation[i] < best_evaluation)
02669 {
02670 best_individual = get_individual(i);
02671
02672 best_norm = best_individual.calculate_norm();
02673
02674 best_evaluation = evaluation[i];
02675 }
02676 }
02677
02678
02679
02680 if(reserve_best_individual_history)
02681 {
02682 best_individual_history[generation] = best_individual;
02683 }
02684
02685
02686
02687 if(reserve_best_norm_history)
02688 {
02689 best_norm_history[generation] = best_norm;
02690 }
02691
02692
02693
02694 double mean_evaluation = evaluation.calculate_mean();
02695
02696 if(reserve_mean_evaluation_history)
02697 {
02698 mean_evaluation_history[generation] = mean_evaluation;
02699 }
02700
02701
02702
02703 double standard_deviation_evaluation = evaluation.calculate_standard_deviation();
02704
02705 if(reserve_standard_deviation_evaluation_history)
02706 {
02707 standard_deviation_evaluation_history[generation] = standard_deviation_evaluation;
02708 }
02709
02710
02711
02712 if(reserve_best_evaluation_history)
02713 {
02714 best_evaluation_history[generation] = best_evaluation;
02715 }
02716
02717
02718
02719 time(¤t_time);
02720 elapsed_time = difftime(current_time, beginning_time);
02721
02722 if(reserve_elapsed_time_history)
02723 {
02724 elapsed_time_history[generation] = elapsed_time;
02725 }
02726
02727
02728
02729 if(reserve_population_history)
02730 {
02731 population_history[generation] = population;
02732 }
02733
02734 if(reserve_best_individual_history)
02735 {
02736 best_individual_history[generation] = best_individual;
02737 }
02738
02739 if(reserve_mean_norm_history)
02740 {
02741 mean_norm_history[generation] = mean_norm;
02742 }
02743
02744 if(reserve_standard_deviation_norm_history)
02745 {
02746 standard_deviation_norm_history[generation] = standard_deviation_norm;
02747 }
02748
02749 if(reserve_best_norm_history)
02750 {
02751 best_norm_history[generation] = best_norm;
02752 }
02753
02754
02755
02756 if(reserve_mean_evaluation_history)
02757 {
02758 mean_evaluation_history[generation] = mean_evaluation;
02759 }
02760
02761 if(reserve_standard_deviation_evaluation_history)
02762 {
02763 standard_deviation_evaluation_history[generation] = standard_deviation_evaluation;
02764 }
02765
02766 if(reserve_best_evaluation_history)
02767 {
02768 best_evaluation_history[generation] = best_evaluation;
02769 }
02770
02771 if(reserve_elapsed_time_history)
02772 {
02773 elapsed_time_history[generation] = elapsed_time;
02774 }
02775
02776
02777
02778 if(best_evaluation <= evaluation_goal)
02779 {
02780 if(display)
02781 {
02782 std::cout << "Generation " << generation << ": Evaluation goal reached." << std::endl;
02783
02784 objective_functional_pointer->print_information();
02785 }
02786
02787 stop_training = true;
02788 }
02789
02790 if(mean_evaluation <= mean_evaluation_goal)
02791 {
02792 if(display)
02793 {
02794 std::cout << "Generation " << generation << ": Mean evaluation goal reached." << std::endl;
02795 }
02796
02797 stop_training = true;
02798 }
02799
02800 if(standard_deviation_evaluation <= standard_deviation_evaluation_goal)
02801 {
02802 if(display)
02803 {
02804 std::cout << "Generation " << generation << ": Standard deviation of evaluation goal reached." << std::endl;
02805 }
02806
02807 stop_training = true;
02808 }
02809
02810 else if(elapsed_time >= maximum_time)
02811 {
02812 if(display)
02813 {
02814 std::cout << "Generation " << generation << ": Maximum training time reached." << std::endl;
02815 }
02816
02817 stop_training = true;
02818 }
02819
02820 else if(generation == maximum_generations_number)
02821 {
02822 if(display)
02823 {
02824 std::cout << "Generation " << generation << ": Maximum number of generations reached." << std::endl;
02825 }
02826
02827 break;
02828 }
02829
02830 if(stop_training)
02831 {
02832
02833 if(display)
02834 {
02835 std::cout << "Mean norm: " << mean_norm << std::endl;
02836 std::cout << "Standard deviation of norm: " << standard_deviation_norm << std::endl;
02837 std::cout << "Best norm: " << best_norm << std::endl;
02838
02839 std::cout << "Mean evaluation: " << mean_evaluation << std::endl;
02840 std::cout << "Standard deviation of evaluation: " << standard_deviation_evaluation << std::endl;
02841 std::cout << "Best evaluation: " << best_evaluation << std::endl;
02842
02843 std::cout << "Elapsed time: " << elapsed_time << ";" << std::endl;
02844
02845 objective_functional_pointer->print_information();
02846
02847 resize_training_history(generation+1);
02848 }
02849
02850 break;
02851 }
02852 else if(display && generation % display_period == 0)
02853 {
02854 std::cout << "Generation " << generation << "; " << std::endl;
02855
02856 std::cout << "Mean norm: " << mean_norm << std::endl;
02857 std::cout << "Standard deviation of norm: " << standard_deviation_norm << std::endl;
02858 std::cout << "Best norm: " << best_norm << std::endl;
02859
02860 std::cout << "Mean evaluation: " << mean_evaluation << std::endl;
02861 std::cout << "Standard deviation of evaluation: " << standard_deviation_evaluation << std::endl;
02862 std::cout << "Best evaluation: " << best_evaluation << std::endl;
02863
02864 std::cout << "Elapsed time: " << elapsed_time << ";" << std::endl;
02865
02866 objective_functional_pointer->print_information();
02867
02868 objective_functional_pointer->print_information();
02869 }
02870
02871
02872
02873 multilayer_perceptron_pointer->set_parameters(best_individual);
02874
02875
02876
02877 selection.initialize(false);
02878
02879 evolve_population();
02880 }
02881 }
02882
02883
02884
02885
02928
02929 std::string EvolutionaryAlgorithm::to_XML(bool show_declaration)
02930 {
02931 std::stringstream buffer; int parameters_number = 0;
02932
02933 if(objective_functional_pointer != NULL)
02934 {
02935 MultilayerPerceptron* multilayer_perceptron_pointer = objective_functional_pointer->get_multilayer_perceptron_pointer();
02936
02937 if(multilayer_perceptron_pointer != NULL)
02938 {
02939 parameters_number = multilayer_perceptron_pointer->get_parameters_number();
02940 }
02941 }
02942
02943 int population_size = get_population_size();
02944
02945
02946
02947 if(show_declaration)
02948 {
02949 buffer << "<Flood version='3.0' class='EvolutionaryAlgorithm'>" << std::endl;
02950 }
02951
02952
02953
02954 buffer << "<PopulationSize>" << std::endl
02955 << population_size << std::endl
02956 << "</PopulationSize>" << std::endl;
02957
02958 buffer << "<ParametersNumber>" << std::endl
02959 << parameters_number << std::endl
02960 << "</ParametersNumber>" << std::endl;
02961
02962
02963
02964
02965
02966 buffer << "<FitnessAssignmentMethod>" << std::endl
02967 << get_fitness_assignment_method_name() << std::endl
02968 << "</FitnessAssignmentMethod>" << std::endl;
02969
02970
02971
02972 buffer << "<SelectionMethod>" << std::endl
02973 << get_selection_method_name() << std::endl
02974 << "</SelectionMethod>" << std::endl;
02975
02976
02977
02978 buffer << "<RecombinationMethod>" << std::endl
02979 << get_recombination_method_name() << std::endl
02980 << "</RecombinationMethod>" << std::endl;
02981
02982
02983
02984 buffer << "<MutationMethod>" << std::endl
02985 << get_mutation_method_name() << std::endl
02986 << "</MutationMethod>" << std::endl;
02987
02988
02989
02990 buffer << "<Elitism>" << std::endl
02991 << elitism << std::endl
02992 << "</Elitism>" << std::endl;
02993
02994 buffer << "<SelectivePressure>" << std::endl
02995 << selective_pressure << std::endl
02996 << "</SelectivePressure>" << std::endl;
02997
02998 buffer << "<RecombinationSize>" << std::endl
02999 << recombination_size << std::endl
03000 << "</RecombinationSize>" << std::endl;
03001
03002 buffer << "<MutationRate>" << std::endl
03003 << mutation_rate << std::endl
03004 << "</MutationRate>" << std::endl;
03005
03006 buffer << "<MutationRange>" << std::endl
03007 << mutation_range << std::endl
03008 << "</MutationRange>" << std::endl;
03009
03010
03011
03012 buffer << "<EvaluationGoal>" << std::endl
03013 << evaluation_goal << std::endl
03014 << "</EvaluationGoal>" << std::endl;
03015
03016 buffer << "<MeanEvaluationGoal>" << std::endl
03017 << mean_evaluation_goal << std::endl
03018 << "</MeanEvaluationGoal>" << std::endl;
03019
03020 buffer << "<StandardDeviationEvaluationGoal>" << std::endl
03021 << standard_deviation_evaluation_goal << std::endl
03022 << "</StandardDeviationEvaluationGoal>" << std::endl;
03023
03024 buffer << "<MaximumGenerationsNumber>" << std::endl
03025 << maximum_generations_number << std::endl
03026 << "</MaximumGenerationsNumber>" << std::endl;
03027
03028 buffer << "<MaximumTime>" << std::endl
03029 << maximum_time << std::endl
03030 << "</MaximumTime>" << std::endl;
03031
03032
03033
03034 buffer << "<ReservePopulationHistory>" << std::endl
03035 << reserve_population_history << std::endl
03036 << "</ReservePopulationHistory>" << std::endl;
03037
03038 buffer << "<ReserveBestIndividualHistory>" << std::endl
03039 << reserve_best_individual_history << std::endl
03040 << "</ReserveBestIndividualHistory>" << std::endl;
03041
03042 buffer << "<ReserveMeanNormHistory>" << std::endl
03043 << reserve_mean_norm_history << std::endl
03044 << "</ReserveMeanNormHistory>" << std::endl;
03045
03046 buffer << "<ReserveStandardDeviationNormHistory>" << std::endl
03047 << reserve_standard_deviation_norm_history << std::endl
03048 << "</ReserveStandardDeviationNormHistory>" << std::endl;
03049
03050 buffer << "<ReserveBestNormHistory>" << std::endl
03051 << reserve_best_norm_history << std::endl
03052 << "</ReserveBestNormHistory>" << std::endl;
03053
03054 buffer << "<ReserveMeanEvaluationHistory>" << std::endl
03055 << reserve_mean_evaluation_history << std::endl
03056 << "</ReserveMeanEvaluationHistory>" << std::endl;
03057
03058 buffer << "<ReserveStandardDeviationEvaluationHistory>" << std::endl
03059 << reserve_standard_deviation_evaluation_history << std::endl
03060 << "</ReserveStandardDeviationEvaluationHistory>" << std::endl;
03061
03062 buffer << "<ReserveBestEvaluationHistory>" << std::endl
03063 << reserve_best_evaluation_history << std::endl
03064 << "</ReserveBestEvaluationHistory>" << std::endl;
03065
03066 buffer << "<ReserveElapsedTimeHistory>" << std::endl
03067 << reserve_elapsed_time_history << std::endl
03068 << "</ReserveElapsedTimeHistory>" << std::endl;
03069
03070 buffer << "<Display>" << std::endl
03071 << display << std::endl
03072 << "</Display>" << std::endl;
03073
03074 buffer << "<DisplayPeriod>" << std::endl
03075 << display_period << std::endl
03076 << "</DisplayPeriod>" << std::endl;
03077
03078 buffer << "<Population>" << std::endl;
03079
03080 if(population_size != 0 && parameters_number != 0)
03081 {
03082 buffer << population;
03083 }
03084
03085 buffer << "</Population>" << std::endl;
03086
03087 return(buffer.str());
03088 }
03089
03090
03091
03092
03136
03137 void EvolutionaryAlgorithm::load(const char* filename)
03138 {
03139
03140
03141 std::fstream file;
03142
03143 file.open(filename, std::ios::in);
03144
03145 if(!file.is_open())
03146 {
03147 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03148 << "void load(const char*) method." << std::endl
03149 << "Cannot open evolutionary algorithm object XML-type file." << std::endl;
03150
03151 exit(1);
03152 }
03153
03154
03155
03156 std::string line;
03157 std::string word;
03158
03159
03160
03161 getline(file, line);
03162
03163 if(line != "<Flood version='3.0' class='EvolutionaryAlgorithm'>")
03164 {
03165 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03166 << "void load(const char*) method." << std::endl
03167 << "Unknown file declaration: " << line << std::endl;
03168
03169 exit(1);
03170 }
03171
03172
03173
03174 file >> word;
03175
03176 if(word != "<PopulationSize>")
03177 {
03178 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03179 << "void load(const char*) method." << std::endl
03180 << "Unknown population size begin tag: " << word << std::endl;
03181
03182 exit(1);
03183 }
03184
03185 int new_population_size;
03186 file >> new_population_size;
03187
03188 file >> word;
03189
03190 if(word != "</PopulationSize>")
03191 {
03192 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03193 << "void load(const char*) method." << std::endl
03194 << "Unknown population size end tag: " << word << std::endl;
03195
03196 exit(1);
03197 }
03198
03199
03200
03201 file >> word;
03202
03203 if(word != "<ParametersNumber>")
03204 {
03205 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03206 << "void load(const char*) method." << std::endl
03207 << "Unknown parameters number begin tag: " << word << std::endl;
03208
03209 exit(1);
03210 }
03211
03212 int new_parameters_number;
03213 file >> new_parameters_number;
03214
03215 file >> word;
03216
03217 if(word != "</ParametersNumber>")
03218 {
03219 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03220 << "void load(const char*) method." << std::endl
03221 << "Unknown population size end tag: " << word << std::endl;
03222
03223 exit(1);
03224 }
03225
03226
03227 while(!file.eof())
03228 {
03229 file >> word;
03230
03231
03232
03233 if(word == "<FitnessAssignmentMethod>")
03234 {
03235 std::string new_fitness_assignment_method_name;
03236 file >> new_fitness_assignment_method_name;
03237 set_fitness_assignment_method(new_fitness_assignment_method_name);
03238
03239 file >> word;
03240
03241 if(word != "</FitnessAssignmentMethod>")
03242 {
03243 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03244 << "void load(const char*) method." << std::endl
03245 << "Unknown fitness assignment method end tag: " << word << std::endl;
03246
03247 exit(1);
03248 }
03249 }
03250
03251 else if(word == "<SelectionMethod>")
03252 {
03253 std::string new_selection_method_name;
03254 file >> new_selection_method_name;
03255 set_selection_method(new_selection_method_name);
03256
03257 file >> word;
03258
03259 if(word != "</SelectionMethod>")
03260 {
03261 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03262 << "void load(const char*) method." << std::endl
03263 << "Unknown selection method end tag: " << word << std::endl;
03264
03265 exit(1);
03266 }
03267 }
03268
03269 else if(word == "<RecombinationMethod>")
03270 {
03271 std::string new_recombination_method_name;
03272 file >> new_recombination_method_name;
03273 set_recombination_method(new_recombination_method_name);
03274
03275 file >> word;
03276
03277 if(word != "</RecombinationMethod>")
03278 {
03279 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03280 << "void load(const char*) method." << std::endl
03281 << "Unknown recombination method end tag: " << word << std::endl;
03282
03283 exit(1);
03284 }
03285 }
03286
03287 else if(word == "<MutationMethod>")
03288 {
03289 std::string new_mutation_method_name;
03290 file >> new_mutation_method_name;
03291 set_mutation_method(new_mutation_method_name);
03292
03293 file >> word;
03294
03295 if(word != "</MutationMethod>")
03296 {
03297 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03298 << "void load(const char*) method." << std::endl
03299 << "Unknown mutation method end tag: " << word << std::endl;
03300
03301 exit(1);
03302 }
03303 }
03304
03305 else if(word == "<Elitism>")
03306 {
03307 bool new_elitism;
03308 file >> new_elitism;
03309 set_elitism(new_elitism);
03310
03311 file >> word;
03312
03313 if(word != "</Elitism>")
03314 {
03315 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03316 << "void load(const char*) method." << std::endl
03317 << "Unknown elitism end tag: " << word << std::endl;
03318
03319 exit(1);
03320 }
03321 }
03322
03323 else if(word == "<SelectivePressure>")
03324 {
03325 double new_selective_pressure;
03326 file >> new_selective_pressure;
03327 set_selective_pressure(new_selective_pressure);
03328
03329 file >> word;
03330
03331 if(word != "</SelectivePressure>")
03332 {
03333 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03334 << "void load(const char*) method." << std::endl
03335 << "Unknown selective pressure end tag: " << word << std::endl;
03336
03337 exit(1);
03338 }
03339 }
03340
03341 else if(word == "<RecombinationSize>")
03342 {
03343 double new_recombination_size;
03344 file >> new_recombination_size;
03345 set_recombination_size(new_recombination_size);
03346
03347 file >> word;
03348
03349 if(word != "</RecombinationSize>")
03350 {
03351 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03352 << "void load(const char*) method." << std::endl
03353 << "Unknown recombination size end tag: " << word << std::endl;
03354
03355 exit(1);
03356 }
03357 }
03358
03359 else if(word == "<MutationRate>")
03360 {
03361 double new_mutation_rate;
03362 file >> new_mutation_rate;
03363 set_mutation_rate(new_mutation_rate);
03364
03365 file >> word;
03366
03367 if(word != "</MutationRate>")
03368 {
03369 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03370 << "void load(const char*) method." << std::endl
03371 << "Unknown mutation rate end tag: " << word << std::endl;
03372
03373 exit(1);
03374 }
03375 }
03376
03377 else if(word == "<MutationRange>")
03378 {
03379 double new_mutation_range;
03380 file >> new_mutation_range;
03381 set_mutation_range(new_mutation_range);
03382
03383 file >> word;
03384
03385 if(word != "</MutationRange>")
03386 {
03387 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03388 << "void load(const char*) method." << std::endl
03389 << "Unknown mutation range end tag: " << word << std::endl;
03390
03391 exit(1);
03392 }
03393 }
03394
03395 else if(word == "<EvaluationGoal>")
03396 {
03397 double new_evaluation_goal;
03398 file >> new_evaluation_goal;
03399 set_evaluation_goal(new_evaluation_goal);
03400
03401 file >> word;
03402
03403 if(word != "</EvaluationGoal>")
03404 {
03405 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03406 << "void load(const char*) method." << std::endl
03407 << "Unknown evaluation goal end tag: " << word << std::endl;
03408
03409 exit(1);
03410 }
03411 }
03412
03413 else if(word == "<MeanEvaluationGoal>")
03414 {
03415 double new_mean_evaluation_goal;
03416 file >> new_mean_evaluation_goal;
03417 set_mean_evaluation_goal(new_mean_evaluation_goal);
03418
03419 file >> word;
03420
03421 if(word != "</MeanEvaluationGoal>")
03422 {
03423 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03424 << "void load(const char*) method." << std::endl
03425 << "Unknown mean evaluation goal end tag: " << word << std::endl;
03426
03427 exit(1);
03428 }
03429 }
03430
03431 else if(word == "<StandardDeviationEvaluationGoal>")
03432 {
03433 double new_standard_deviation_evaluation_goal;
03434 file >> new_standard_deviation_evaluation_goal;
03435 set_standard_deviation_evaluation_goal(new_standard_deviation_evaluation_goal);
03436
03437 file >> word;
03438
03439 if(word != "</StandardDeviationEvaluationGoal>")
03440 {
03441 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03442 << "void load(const char*) method." << std::endl
03443 << "Unknown standard deviation of evaluation goal end tag: " << word << std::endl;
03444
03445 exit(1);
03446 }
03447 }
03448
03449 else if(word == "<MaximumGenerationsNumber>")
03450 {
03451 double new_maximum_generations_number;
03452 file >> new_maximum_generations_number;
03453 set_maximum_time(new_maximum_generations_number);
03454
03455 file >> word;
03456
03457 if(word != "</MaximumGenerationsNumber>")
03458 {
03459 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03460 << "void load(const char*) method." << std::endl
03461 << "Unknown maximum generations number end tag: " << word << std::endl;
03462
03463 exit(1);
03464 }
03465 }
03466
03467 else if(word == "<MaximumTime>")
03468 {
03469 double new_maximum_time;
03470 file >> new_maximum_time;
03471 set_maximum_time(new_maximum_time);
03472
03473 file >> word;
03474
03475 if(word != "</MaximumTime>")
03476 {
03477 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03478 << "void load(const char*) method." << std::endl
03479 << "Unknown maximum time end tag: " << word << std::endl;
03480
03481 exit(1);
03482 }
03483 }
03484
03485 else if(word == "<ReservePopulationHistory>")
03486 {
03487 file >> reserve_population_history;
03488
03489 file >> word;
03490
03491 if(word != "</ReservePopulationHistory>")
03492 {
03493 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03494 << "void load(const char*) method." << std::endl
03495 << "Unknown reserve population history end tag: " << word << std::endl;
03496
03497 exit(1);
03498 }
03499 }
03500
03501 else if(word == "<ReserveBestIndividualHistory>")
03502 {
03503 file >> reserve_best_individual_history;
03504
03505 file >> word;
03506
03507 if(word != "</ReserveBestIndividualHistory>")
03508 {
03509 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03510 << "void load(const char*) method." << std::endl
03511 << "Unknown reserve best individual history end tag: " << word << std::endl;
03512
03513 exit(1);
03514 }
03515 }
03516
03517 else if(word == "<ReserveMeanNormHistory>")
03518 {
03519 file >> reserve_mean_norm_history;
03520
03521 file >> word;
03522
03523 if(word != "</ReserveMeanNormHistory>")
03524 {
03525 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03526 << "void load(const char*) method." << std::endl
03527 << "Unknown reserve mean norm history end tag: " << word << std::endl;
03528
03529 exit(1);
03530 }
03531 }
03532
03533 else if(word == "<ReserveStandardDeviationNormHistory>")
03534 {
03535 file >> reserve_standard_deviation_norm_history;
03536
03537 file >> word;
03538
03539 if(word != "</ReserveStandardDeviationNormHistory>")
03540 {
03541 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03542 << "void load(const char*) method." << std::endl
03543 << "Unknown reserve standard deviation norm history end tag: " << word << std::endl;
03544
03545 exit(1);
03546 }
03547 }
03548
03549 else if(word == "<ReserveBestNormHistory>")
03550 {
03551 file >> reserve_best_norm_history;
03552
03553 file >> word;
03554
03555 if(word != "</ReserveBestNormHistory>")
03556 {
03557 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03558 << "void load(const char*) method." << std::endl
03559 << "Unknown reserve best norm history end tag: " << word << std::endl;
03560
03561 exit(1);
03562 }
03563 }
03564
03565 else if(word == "<ReserveMeanEvaluationHistory>")
03566 {
03567 file >> reserve_mean_evaluation_history;
03568
03569 file >> word;
03570
03571 if(word != "</ReserveMeanEvaluationHistory>")
03572 {
03573 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03574 << "void load(const char*) method." << std::endl
03575 << "Unknown reserve mean evaluation history end tag: " << word << std::endl;
03576
03577 exit(1);
03578 }
03579 }
03580
03581 else if(word == "<ReserveStandardDeviationEvaluationHistory>")
03582 {
03583 file >> reserve_standard_deviation_evaluation_history;
03584
03585 file >> word;
03586
03587 if(word != "</ReserveStandardDeviationEvaluationHistory>")
03588 {
03589 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03590 << "void load(const char*) method." << std::endl
03591 << "Unknown reserve standard deviation evaluation history end tag: " << word << std::endl;
03592
03593 exit(1);
03594 }
03595 }
03596
03597 else if(word == "<ReserveBestEvaluationHistory>")
03598 {
03599 file >> reserve_best_evaluation_history;
03600
03601 file >> word;
03602
03603 if(word != "</ReserveBestEvaluationHistory>")
03604 {
03605 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03606 << "void load(const char*) method." << std::endl
03607 << "Unknown reserve best evaluation history end tag: " << word << std::endl;
03608
03609 exit(1);
03610 }
03611 }
03612
03613 else if(word == "<ReserveElapsedTimeHistory>")
03614 {
03615 file >> reserve_elapsed_time_history;
03616
03617 file >> word;
03618
03619 if(word != "</ReserveElapsedTimeHistory>")
03620 {
03621 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03622 << "void load(const char*) method." << std::endl
03623 << "Unknown reserve elapsed time history end tag: " << word << std::endl;
03624
03625 exit(1);
03626 }
03627 }
03628
03629 else if(word == "<Display>")
03630 {
03631 file >> display;
03632
03633 file >> word;
03634
03635 if(word != "</Display>")
03636 {
03637 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03638 << "void load(const char*) method." << std::endl
03639 << "Unknown display end tag: " << word << std::endl;
03640
03641 exit(1);
03642 }
03643 }
03644
03645 else if(word == "<DisplayPeriod>")
03646 {
03647 int new_display_period;
03648 file >> new_display_period;
03649 set_display_period(new_display_period);
03650
03651 file >> word;
03652
03653 if(word != "</DisplayPeriod>")
03654 {
03655 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03656 << "void load(const char*) method." << std::endl
03657 << "Unknown display period end tag: " << word << std::endl;
03658
03659 exit(1);
03660 }
03661 }
03662
03663 else if(word == "<Population>")
03664 {
03665 if(new_population_size != 0 && new_parameters_number != 0)
03666 {
03667 Matrix<double> new_population(new_population_size, new_parameters_number);
03668
03669 file >> new_population;
03670
03671 set_population(new_population);
03672 }
03673
03674 file >> word;
03675
03676 if(word != "</Population>")
03677 {
03678 std::cerr << "Flood Error: EvolutionaryAlgorithm class." << std::endl
03679 << "void load(const char*) method." << std::endl
03680 << "Unknown population end tag: " << word << std::endl;
03681
03682 exit(1);
03683 }
03684 }
03685 }
03686
03687
03688
03689 file.close();
03690 }
03691
03692
03693
03694
03709
03710 void EvolutionaryAlgorithm::resize_training_history(int new_size)
03711 {
03712
03713
03714 if(reserve_population_history)
03715 {
03716 population_history.resize(new_size);
03717 }
03718
03719
03720
03721 if(reserve_best_individual_history)
03722 {
03723 best_individual_history.resize(new_size);
03724 }
03725
03726
03727
03728 if(reserve_mean_norm_history)
03729 {
03730 mean_norm_history.resize(new_size);
03731 }
03732
03733
03734
03735 if(reserve_standard_deviation_norm_history)
03736 {
03737 standard_deviation_norm_history.resize(new_size);
03738 }
03739
03740
03741
03742 if(reserve_best_norm_history)
03743 {
03744 best_norm_history.resize(new_size);
03745 }
03746
03747
03748
03749 if(reserve_mean_evaluation_history)
03750 {
03751 mean_evaluation_history.resize(new_size);
03752 }
03753
03754
03755
03756 if(reserve_standard_deviation_evaluation_history)
03757 {
03758 standard_deviation_evaluation_history.resize(new_size);
03759 }
03760
03761
03762
03763 if(reserve_best_evaluation_history)
03764 {
03765 best_evaluation_history.resize(new_size);
03766 }
03767
03768
03769
03770 if(reserve_elapsed_time_history)
03771 {
03772 elapsed_time_history.resize(new_size);
03773 }
03774 }
03775
03776
03777
03778
03782
03783 std::string EvolutionaryAlgorithm::get_training_history_XML(bool show_declaration)
03784 {
03785 std::stringstream buffer;
03786
03787 if(show_declaration)
03788 {
03789 buffer << "<Flood version='3.0' class='EvolutionaryAlgorithm' content='TrainingHistory'>" << std::endl;
03790 }
03791
03792
03793
03794 if(reserve_population_history)
03795 {
03796 buffer << "<PopulationHistory>" << std::endl;
03797
03798 for(int i = 0; i < population_history.get_size(); i++)
03799 {
03800 buffer << "<Population>" << std::endl;
03801
03802 for(int i = 0; i < population_history.get_size(); i++)
03803 {
03804 buffer << population_history[i];
03805 }
03806
03807 buffer << "</Population>" << std::endl;
03808
03809 }
03810
03811 buffer << "</PopulationHistory>" << std::endl;
03812 }
03813
03814 if(reserve_best_individual_history)
03815 {
03816 buffer << "<BestIndividualHistory>" << std::endl;
03817
03818 for(int i = 0; i < best_individual_history.get_size(); i++)
03819 {
03820 buffer << best_individual_history[i] << std::endl;
03821 }
03822
03823 buffer << "</BestIndividualHistory>" << std::endl;
03824 }
03825
03826 if(reserve_mean_norm_history)
03827 {
03828 buffer << "<MeanNormHistory>" << std::endl
03829 << mean_norm_history << std::endl
03830 << "</MeanNormHistory>" << std::endl;
03831 }
03832
03833 if(reserve_standard_deviation_norm_history)
03834 {
03835 buffer << "<StandardDeviationNormHistory>" << std::endl
03836 << standard_deviation_norm_history << std::endl
03837 << "</StandardDeviationNormHistory>" << std::endl;
03838 }
03839
03840 if(reserve_best_norm_history)
03841 {
03842 buffer << "<BestNormHistory>" << std::endl
03843 << best_norm_history << std::endl
03844 << "</BestNormHistory>" << std::endl;
03845 }
03846
03847
03848
03849 if(reserve_mean_evaluation_history)
03850 {
03851 buffer << "<MeanEvaluationHistory>" << std::endl
03852 << mean_evaluation_history << std::endl
03853 << "</MeanEvaluationHistory>" << std::endl;
03854 }
03855
03856 if(reserve_standard_deviation_evaluation_history)
03857 {
03858 buffer << "<StandardDeviationEvaluationHistory>" << std::endl
03859 << standard_deviation_evaluation_history << std::endl
03860 << "</StandardDeviationEvaluationHistory>" << std::endl;
03861 }
03862
03863 if(reserve_best_evaluation_history)
03864 {
03865 buffer << "<BestEvaluationHistory>" << std::endl
03866 << best_evaluation_history << std::endl
03867 << "</BestEvaluationHistory>" << std::endl;
03868 }
03869
03870
03871
03872 if(reserve_elapsed_time_history)
03873 {
03874 buffer << "<ElapsedTimeHistory>" << std::endl
03875 << elapsed_time_history << std::endl
03876 << "</ElapsedTimeHistory>" << std::endl;
03877 }
03878
03879 return(buffer.str());
03880 }
03881
03882
03883
03884
03885 double EvolutionaryAlgorithm::calculate_random_uniform(double minimum, double maximum)
03886 {
03887 double random = (double)rand()/(RAND_MAX+1.0);
03888
03889 double random_uniform = minimum + (maximum-minimum)*random;
03890
03891 return(random_uniform);
03892 }
03893
03894
03895
03896
03897 double EvolutionaryAlgorithm::calculate_random_normal(double mean, double standard_deviation)
03898 {
03899 double random_normal = 0.0;
03900
03901 const double pi = 4.0*atan(1.0);
03902
03903 double random_uniform_1;
03904 double random_uniform_2;
03905
03906 do
03907 {
03908 random_uniform_1 = (double)rand()/(RAND_MAX+1.0);
03909
03910 }while(random_uniform_1 == 0.0);
03911
03912 random_uniform_2 = (double)rand()/(RAND_MAX+1.0);
03913
03914
03915
03916 random_normal = mean + sqrt(-2.0*log(random_uniform_1))*sin(2.0*pi*random_uniform_2)*standard_deviation;
03917
03918 return(random_normal);
03919 }
03920
03921 }
03922
03923
03924
03925
03926
03927
03928
03929
03930
03931
03932
03933
03934
03935
03936
03937
03938
03939