00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream>
00019 #include <fstream>
00020 #include <string>
00021 #include <sstream>
00022 #include <cmath>
00023
00024
00025
00026 #include "FunctionRegressionUtilities.h"
00027
00028 namespace Flood
00029 {
00030
00031
00032
00035
00036 FunctionRegressionUtilities::FunctionRegressionUtilities(void)
00037 {
00038 multilayer_perceptron_pointer = NULL;
00039 input_target_data_set_pointer = NULL;
00040
00041 display = true;
00042 }
00043
00044
00045
00046
00051
00052 FunctionRegressionUtilities::FunctionRegressionUtilities(MultilayerPerceptron* new_multilayer_perceptron_pointer)
00053 {
00054 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00055 input_target_data_set_pointer = NULL;
00056
00057 display = true;
00058 }
00059
00060
00061
00062
00067
00068 FunctionRegressionUtilities::FunctionRegressionUtilities(InputTargetDataSet* new_input_target_data_set_pointer)
00069 {
00070 multilayer_perceptron_pointer = NULL;
00071 input_target_data_set_pointer = new_input_target_data_set_pointer;
00072
00073 display = true;
00074 }
00075
00076
00077
00078
00084
00085 FunctionRegressionUtilities::FunctionRegressionUtilities
00086 (MultilayerPerceptron* new_multilayer_perceptron_pointer, InputTargetDataSet* new_input_target_data_set_pointer)
00087 {
00088 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00089
00090 input_target_data_set_pointer = new_input_target_data_set_pointer;
00091
00092 display = true;
00093 }
00094
00095
00096
00097
00099
00100 FunctionRegressionUtilities::~FunctionRegressionUtilities()
00101 {
00102 }
00103
00104
00105
00106
00107
00108
00111
00112 MultilayerPerceptron* FunctionRegressionUtilities::get_multilayer_perceptron_pointer(void)
00113 {
00114 return(multilayer_perceptron_pointer);
00115 }
00116
00117
00118
00119
00122
00123 InputTargetDataSet* FunctionRegressionUtilities::get_input_target_data_set_pointer(void)
00124 {
00125 return(input_target_data_set_pointer);
00126 }
00127
00128
00129
00130
00133
00134 bool FunctionRegressionUtilities::get_display(void)
00135 {
00136 return(display);
00137 }
00138
00139
00140
00141
00145
00146 void FunctionRegressionUtilities::set_multilayer_perceptron_pointer(MultilayerPerceptron* new_multilayer_perceptron_pointer)
00147 {
00148 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00149 }
00150
00151
00152
00153
00158
00159 void FunctionRegressionUtilities
00160 ::set_input_target_data_set_pointer(InputTargetDataSet* new_input_target_data_set_pointer)
00161 {
00162 input_target_data_set_pointer = new_input_target_data_set_pointer;
00163 }
00164
00165
00166
00167
00173
00174 void FunctionRegressionUtilities::set_display(bool new_display)
00175 {
00176 display = new_display;
00177 }
00178
00179
00180
00181
00184
00185 Vector< Matrix<double> > FunctionRegressionUtilities::calculate_testing_target_output_data(void)
00186 {
00187
00188
00189 #ifdef _DEBUG
00190
00191 if(multilayer_perceptron_pointer == NULL)
00192 {
00193 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00194 << "Vector< Matrix<double> > calculate_testing_target_output_data(void)." << std::endl
00195 << "Multilayer perceptron object cannot be null." << std::endl;
00196
00197 exit(1);
00198 }
00199 else if(input_target_data_set_pointer == NULL)
00200 {
00201 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00202 << "Vector< Matrix<double> > calculate_testing_target_output_data(void)." << std::endl
00203 << "Input-target data set object cannot be null." << std::endl;
00204
00205 exit(1);
00206 }
00207
00208 #endif
00209
00210 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00211
00212 int testing_instances_number = input_target_data_set_pointer->get_testing_instances_number();
00213
00214 Vector< Matrix<double> > testing_target_output_data(outputs_number);
00215
00216 Matrix<double> testing_input_data = input_target_data_set_pointer->get_testing_input_data();
00217
00218 Matrix<double> testing_target_data = input_target_data_set_pointer->get_testing_target_data();
00219
00220 Matrix<double> testing_output_data = multilayer_perceptron_pointer->calculate_output_matrix(testing_input_data);
00221
00222 for(int i = 0; i < outputs_number; i++)
00223 {
00224 testing_target_output_data[i].set(testing_instances_number, 2);
00225
00226 for(int j = 0; j < testing_instances_number; j++)
00227 {
00228 testing_target_output_data[i][j][0] = testing_target_data[j][i];
00229 testing_target_output_data[i][j][1] = testing_output_data[j][i];
00230 }
00231 }
00232
00233 return(testing_target_output_data);
00234 }
00235
00236
00237
00238
00242
00243 std::string FunctionRegressionUtilities::get_testing_target_output_data(bool show_declaration)
00244 {
00245 std::stringstream buffer;
00246
00247
00248
00249 if(show_declaration)
00250 {
00251 buffer << "<Flood version='3.0' class='FunctionRegressionUtilities' content='TestingTargetOutputData'>" << std::endl;
00252 }
00253
00254 Vector< Matrix<double> > testing_target_output_data = calculate_testing_target_output_data();
00255
00256 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00257
00258 for(int i = 0; i < outputs_number; i++)
00259 {
00260 buffer << "<OutputVariable>" << std::endl;
00261 buffer << testing_target_output_data[i];
00262 buffer << "</OutputVariable>" << std::endl;
00263 }
00264
00265 return(buffer.str());
00266 }
00267
00268
00269
00270
00273
00274 void FunctionRegressionUtilities::print_testing_target_output_data(void)
00275 {
00276 std::cout << get_testing_target_output_data(true);
00277 }
00278
00279
00280
00281
00282
00285
00286 void FunctionRegressionUtilities::save_testing_target_output_data(const char* filename)
00287 {
00288 std::fstream file;
00289
00290 file.open(filename, std::ios::out);
00291
00292 if(!file.is_open())
00293 {
00294 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00295 << "void save_testing_target_output_data(const char*) method." << std::endl
00296 << "Cannot open testing target output data file." << std::endl;
00297
00298 exit(1);
00299 }
00300
00301 file << get_testing_target_output_data(true);
00302
00303 file.close();
00304 }
00305
00306
00307
00308
00309
00316
00317 Vector< Vector<double> > FunctionRegressionUtilities::calculate_linear_regression_parameters(void)
00318 {
00319
00320
00321 #ifdef _DEBUG
00322
00323 if(multilayer_perceptron_pointer == NULL)
00324 {
00325 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00326 << "Matrix<double> calculate_linear_regression_parameters(void)." << std::endl
00327 << "Multilayer perceptron object cannot be null." << std::endl;
00328
00329 exit(1);
00330 }
00331 else if(input_target_data_set_pointer == NULL)
00332 {
00333 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00334 << "Matrix<double> calculate_linear_regression_parameters(void)." << std::endl
00335 << "Input-target data set object cannot be null." << std::endl;
00336
00337 exit(1);
00338 }
00339
00340 #endif
00341
00342
00343
00344 int inputs_number = multilayer_perceptron_pointer->get_outputs_number();
00345 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00346
00347
00348
00349 int testing_instances_number = input_target_data_set_pointer->get_testing_instances_number();
00350
00351
00352
00353 Vector<double> input(inputs_number);
00354 Vector<double> target(outputs_number);
00355 Vector<double> output(outputs_number);
00356
00357 Vector<double> targets_sum(outputs_number, 0.0);
00358 Vector<double> squared_targets_sum(outputs_number, 0.0);
00359 Vector<double> outputs_sum(outputs_number, 0.0);
00360 Vector<double> targets_outputs_sum(outputs_number, 0.0);
00361
00362 for(int i = 0; i < testing_instances_number; i++)
00363 {
00364 target = input_target_data_set_pointer->get_testing_target_instance(i);
00365
00366 input = input_target_data_set_pointer->get_testing_input_instance(i);
00367 output = multilayer_perceptron_pointer->calculate_output(input);
00368
00369 targets_sum += target;
00370 squared_targets_sum += target*target;
00371 outputs_sum += output;
00372 targets_outputs_sum += target*output;
00373 }
00374
00375 Vector<double> intercept = (outputs_sum*squared_targets_sum-targets_sum*targets_outputs_sum)
00376 /(squared_targets_sum*testing_instances_number - targets_sum*targets_sum);
00377
00378
00379 Vector<double> slope = (targets_outputs_sum*testing_instances_number - outputs_sum*targets_sum)
00380 /(squared_targets_sum*testing_instances_number - targets_sum*targets_sum);
00381
00382 Vector< Vector<double> > linear_regression_parameters(outputs_number);
00383
00384 for(int i = 0; i < outputs_number; i++)
00385 {
00386 linear_regression_parameters[i].set(2);
00387
00388 linear_regression_parameters[i][0] = intercept[i];
00389 linear_regression_parameters[i][1] = slope[i];
00390 }
00391
00392 return(linear_regression_parameters);
00393 }
00394
00395
00396
00397
00401
00402 std::string FunctionRegressionUtilities::get_linear_regression_parameters(bool show_declaration)
00403 {
00404 std::stringstream buffer;
00405
00406
00407
00408 if(show_declaration)
00409 {
00410 buffer << "<Flood version='3.0' class='FunctionRegressionUtilities' content='LinearRegressionParameters'>" << std::endl;
00411 }
00412
00413 Vector< Vector<double> > linear_regression_parameters = calculate_linear_regression_parameters();
00414
00415 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00416
00417 for(int i = 0; i < outputs_number; i++)
00418 {
00419 buffer << "<OutputVariable>" << std::endl;
00420
00421 buffer << "<Intercept>" << std::endl
00422 << linear_regression_parameters[i][0] << std::endl
00423 << "</Intercept>" << std::endl;
00424
00425 buffer << "<Slope>" << std::endl
00426 << linear_regression_parameters[i][1] << std::endl
00427 << "</Slope>" << std::endl;
00428
00429 buffer << "</OutputVariable>" << std::endl;
00430 }
00431
00432 return(buffer.str());
00433 }
00434
00435
00436
00437
00440
00441 void FunctionRegressionUtilities::print_linear_regression_parameters(void)
00442 {
00443 std::cout << get_linear_regression_parameters(true);
00444 }
00445
00446
00447
00448
00452
00453 void FunctionRegressionUtilities::save_linear_regression_parameters(const char* filename)
00454 {
00455 std::fstream file;
00456
00457 file.open(filename, std::ios::out);
00458
00459 if(!file.is_open())
00460 {
00461 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00462 << "void save_linear_regression_parameters(const char*) method." << std::endl
00463 << "Cannot open testing target output data file." << std::endl;
00464
00465 exit(1);
00466 }
00467
00468 file << get_linear_regression_parameters(true);
00469
00470 file.close();
00471 }
00472
00473
00474
00475
00482
00483 std::string FunctionRegressionUtilities::get_linear_regression_analysis(bool show_declaration)
00484 {
00485 std::stringstream buffer;
00486
00487
00488
00489 if(show_declaration)
00490 {
00491 buffer << "<Flood version='3.0' class='FunctionRegressionUtilities' content='LinearRegressionAnalysis'>" << std::endl;
00492 }
00493
00494 Vector< Vector<double> > linear_regression_parameters = calculate_linear_regression_parameters();
00495 Vector< Matrix<double> > testing_target_output_data = calculate_testing_target_output_data();
00496
00497 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00498
00499 for(int i = 0; i < outputs_number; i++)
00500 {
00501 buffer << "<OutputVariable>" << std::endl;
00502
00503 buffer << "<Intercept>" << std::endl
00504 << linear_regression_parameters[i][0] << std::endl
00505 << "</Intercept>" << std::endl;
00506
00507 buffer << "<Slope>" << std::endl
00508 << linear_regression_parameters[i][1] << std::endl
00509 << "</Slope>" << std::endl;
00510
00511 buffer << "<TestingTargetOutputData>" << std::endl
00512 << testing_target_output_data[i]
00513 << "</TestingTargetOutputData>" << std::endl;
00514
00515 buffer << "</OutputVariable>" << std::endl;
00516 }
00517
00518 return(buffer.str());
00519 }
00520
00521
00522
00523
00526
00527 void FunctionRegressionUtilities::print_linear_regression_analysis(void)
00528 {
00529 std::cout << get_linear_regression_analysis(true);
00530 }
00531
00532
00533
00534
00538
00539 void FunctionRegressionUtilities::save_linear_regression_analysis(const char* filename)
00540 {
00541 std::fstream file;
00542
00543 file.open(filename, std::ios::out);
00544
00545 if(!file.is_open())
00546 {
00547 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00548 << "void save_linear_regression_analysis(const char*) method." << std::endl
00549 << "Cannot open regression analysis XML-type file." << std::endl;
00550
00551 exit(1);
00552 }
00553
00554 file << get_linear_regression_analysis(true);
00555
00556 file.close();
00557
00558 }
00559
00560
00561
00562
00574
00575 void FunctionRegressionUtilities::generate_artificial_data
00576 (const Vector<double>& mean, const Vector<double>& standard_deviation, const Vector<std::string>& function_names)
00577 {
00578 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00579 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00580
00581
00582
00583 #ifdef _DEBUG
00584
00585 int mean_size = mean.get_size();
00586 int standard_deviation_size = standard_deviation.get_size();
00587
00588 if(mean_size != input_variables_number)
00589 {
00590 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00591 << "void generate_artificial_data(const Vector<double>&, const Vector<double>&, const Vector<std::string>&) method." << std::endl
00592 << "Size of mean must be equal to number of input variables." << std::endl;
00593
00594 exit(1);
00595 }
00596
00597 if(standard_deviation_size != input_variables_number)
00598 {
00599 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00600 << "void generate_artificial_data(const Vector<double>&, const Vector<double>&, const Vector<std::string>&) method." << std::endl
00601 << "Size of standard_deviation must be equal to number of input variables." << std::endl;
00602
00603 exit(1);
00604 }
00605
00606 int function_names_size = function_names.get_size();
00607
00608 if(function_names_size != target_variables_number)
00609 {
00610 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00611 << "void generate_artificial_data(const Vector<double>&, const Vector<double>&, const Vector<std::string>&) method." << std::endl
00612 << "Size must be equal to number of target variables." << std::endl;
00613
00614 exit(1);
00615 }
00616
00617 #endif
00618
00619
00620
00621 int instances_number = input_target_data_set_pointer->get_instances_number();
00622
00623 Vector<double> input(input_variables_number);
00624 Vector<double> target(target_variables_number);
00625
00626 for(int i = 0; i < instances_number; i++)
00627 {
00628 input.initialize_normal(mean, standard_deviation);
00629
00630 for(int j = 0; j < target_variables_number; j++)
00631 {
00632 if(function_names[j] == "ZeroFunction")
00633 {
00634 target[j] = 0.0;
00635 }
00636 else if(function_names[j] == "DeJongFunction")
00637 {
00638 target[j] = calculate_De_Jong_function_evaluation(input);
00639 }
00640 else if(function_names[j] == "RosenbrockFunction")
00641 {
00642 target[j] = calculate_Rosenbrock_function_evaluation(input);
00643 }
00644 else if(function_names[j] == "RastriginFunction")
00645 {
00646 target[j] = calculate_Rastrigin_function_evaluation(input);
00647 }
00648 else
00649 {
00650 std::cerr << "Flood Error: FunctionRegressionUtilities class." << std::endl
00651 << "void generate_artificial_data(const Vector<double>&, const Vector<double>&, const Vector<std::string>&) method." << std::endl
00652 << "Unknown function name: " << function_names[j] << "." << std::endl;
00653
00654 exit(1);
00655 }
00656 }
00657
00658 input_target_data_set_pointer->set_input_instance(i, input);
00659 input_target_data_set_pointer->set_target_instance(i, target);
00660 }
00661 }
00662
00663
00664
00665
00668
00669 double FunctionRegressionUtilities::calculate_De_Jong_function_evaluation(const Vector<double>& input)
00670 {
00671 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00672
00673 double evaluation = 0.0;
00674
00675 for(int i = 0; i < input_variables_number; i++)
00676 {
00677 evaluation += pow(input[i], 2);
00678 }
00679
00680 return(evaluation);
00681 }
00682
00683
00684
00685
00688
00689 double FunctionRegressionUtilities::calculate_Rosenbrock_function_evaluation(const Vector<double>& input)
00690 {
00691 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00692
00693 double evaluation = 0.0;
00694
00695 for(int i = 0; i < input_variables_number-1; i++)
00696 {
00697 evaluation += 100.0*pow(input[i+1] - pow(input[i],2), 2) + pow(1.0 - input[i], 2);
00698 }
00699
00700 return(evaluation);
00701 }
00702
00703
00704
00705
00708
00709 double FunctionRegressionUtilities::calculate_Rastrigin_function_evaluation(const Vector<double>& input)
00710 {
00711 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00712
00713 const double pi = 3.1415927;
00714
00715 double evaluation = 10.0*input_variables_number;
00716
00717 for(int i = 0; i < input_variables_number; i++)
00718 {
00719 evaluation += pow(input[i], 2) - 10.0*cos(2.0*pi*input[i]);
00720 }
00721
00722 return(evaluation);
00723 }
00724
00725 }
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743