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 <cmath>
00021 #include <sstream>
00022 #include <string>
00023 #include <limits>
00024
00025
00026
00027 #include "final_solutions_error.h"
00028
00029
00030
00031 #include "../../parsers/tinyxml/tinyxml.h"
00032
00033
00034 namespace OpenNN
00035 {
00036
00037
00038
00042
00043 FinalSolutionsError::FinalSolutionsError(void)
00044 : PerformanceTerm()
00045 {
00046 construct_numerical_differentiation();
00047
00048 set_default();
00049 }
00050
00051
00052
00053
00058
00059 FinalSolutionsError::FinalSolutionsError(NeuralNetwork* new_neural_network_pointer)
00060 : PerformanceTerm(new_neural_network_pointer)
00061 {
00062 construct_numerical_differentiation();
00063
00064 set_default();
00065 }
00066
00067
00068
00069
00074
00075 FinalSolutionsError::FinalSolutionsError(MathematicalModel* new_mathematical_model_pointer)
00076 : PerformanceTerm(new_mathematical_model_pointer)
00077 {
00078 construct_numerical_differentiation();
00079
00080 set_default();
00081 }
00082
00083
00084
00085
00091
00092 FinalSolutionsError::FinalSolutionsError(NeuralNetwork* new_neural_network_pointer, MathematicalModel* new_mathematical_model_pointer)
00093 : PerformanceTerm(new_neural_network_pointer, new_mathematical_model_pointer)
00094 {
00095 construct_numerical_differentiation();
00096
00097 set_default();
00098 }
00099
00100
00101
00102
00107
00108 FinalSolutionsError::FinalSolutionsError(TiXmlElement* final_state_error_element)
00109 : PerformanceTerm(final_state_error_element)
00110 {
00111 construct_numerical_differentiation();
00112
00113 set_default();
00114
00115 from_XML(final_state_error_element);
00116 }
00117
00118
00119
00120
00124
00125 FinalSolutionsError::FinalSolutionsError(const FinalSolutionsError& other_final_solutions_error)
00126 : PerformanceTerm()
00127 {
00128 set(other_final_solutions_error);
00129 }
00130
00131
00132
00133
00135
00136 FinalSolutionsError::~FinalSolutionsError(void)
00137 {
00138 }
00139
00140
00141
00142
00143
00144
00146
00147 FinalSolutionsError& FinalSolutionsError::operator = (const FinalSolutionsError& other_final_solutions_error)
00148 {
00149 if(this != &other_final_solutions_error)
00150 {
00151 *neural_network_pointer = *other_final_solutions_error.neural_network_pointer;
00152 *data_set_pointer = *other_final_solutions_error.data_set_pointer;
00153 *mathematical_model_pointer = *other_final_solutions_error.mathematical_model_pointer;
00154 *numerical_differentiation_pointer = *other_final_solutions_error.numerical_differentiation_pointer;
00155 display = other_final_solutions_error.display;
00156
00157 final_solutions_errors_weights = other_final_solutions_error.final_solutions_errors_weights;
00158 target_final_solutions = other_final_solutions_error.target_final_solutions;
00159 }
00160
00161 return(*this);
00162 }
00163
00164
00165
00166
00167
00168
00171
00172 bool FinalSolutionsError::operator == (const FinalSolutionsError& other_final_solutions_error) const
00173 {
00174 if(*neural_network_pointer == *other_final_solutions_error.neural_network_pointer
00175 && *mathematical_model_pointer == *other_final_solutions_error.mathematical_model_pointer
00176 && *numerical_differentiation_pointer == *other_final_solutions_error.numerical_differentiation_pointer
00177 && display == other_final_solutions_error.display
00178 && final_solutions_errors_weights == other_final_solutions_error.final_solutions_errors_weights
00179 && target_final_solutions == other_final_solutions_error.target_final_solutions)
00180 {
00181 return(true);
00182 }
00183 else
00184 {
00185 return(false);
00186 }
00187 }
00188
00189
00190
00191
00192
00193
00195
00196 const Vector<double>& FinalSolutionsError::get_final_solutions_errors_weights(void) const
00197 {
00198 return(final_solutions_errors_weights);
00199 }
00200
00201
00202
00203
00205
00206 const Vector<double>& FinalSolutionsError::get_target_final_solutions(void) const
00207 {
00208 return(target_final_solutions);
00209 }
00210
00211
00212
00213
00217
00218 void FinalSolutionsError::set(void)
00219 {
00220 neural_network_pointer = NULL;
00221 data_set_pointer = NULL;
00222 mathematical_model_pointer = NULL;
00223
00224 delete numerical_differentiation_pointer;
00225
00226 numerical_differentiation_pointer = NULL;
00227
00228 set_default();
00229 }
00230
00231
00232
00233
00235
00236 void FinalSolutionsError::set(NeuralNetwork*)
00237 {
00238 }
00239
00240
00241
00242
00244
00245 void FinalSolutionsError::set(MathematicalModel*)
00246 {
00247 }
00248
00249
00250
00251
00253
00254 void FinalSolutionsError::set(NeuralNetwork*, MathematicalModel*)
00255 {
00256 }
00257
00258
00259
00260
00263
00264 void FinalSolutionsError::set(const FinalSolutionsError& other_final_solutions_error)
00265 {
00266 neural_network_pointer = other_final_solutions_error.neural_network_pointer;
00267
00268 data_set_pointer = other_final_solutions_error.data_set_pointer;
00269
00270 mathematical_model_pointer = other_final_solutions_error.mathematical_model_pointer;
00271
00272 if(other_final_solutions_error.numerical_differentiation_pointer)
00273 {
00274 numerical_differentiation_pointer = new NumericalDifferentiation(*other_final_solutions_error.numerical_differentiation_pointer);
00275 }
00276
00277 display = other_final_solutions_error.display;
00278
00279 final_solutions_errors_weights = other_final_solutions_error.final_solutions_errors_weights;
00280
00281 target_final_solutions = other_final_solutions_error.target_final_solutions;
00282 }
00283
00284
00285
00286
00289
00290 void FinalSolutionsError::set_mathematical_model_pointer(MathematicalModel* new_mathematical_model_pointer)
00291 {
00292 mathematical_model_pointer = new_mathematical_model_pointer;
00293
00294 if(mathematical_model_pointer)
00295 {
00296 const unsigned int dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
00297
00298 target_final_solutions.set(dependent_variables_number, 0.0);
00299
00300 final_solutions_errors_weights.set(dependent_variables_number, 1.0);
00301 }
00302 }
00303
00304
00305
00306
00309
00310 void FinalSolutionsError::set_final_solutions_errors_weights(const Vector<double>& new_final_solutions_errors_weights)
00311 {
00312 final_solutions_errors_weights = new_final_solutions_errors_weights;
00313 }
00314
00315
00316
00317
00321
00322 void FinalSolutionsError::set_final_solution_error_weight(const unsigned int& i, const double& new_final_solution_error_weight)
00323 {
00324 final_solutions_errors_weights[i] = new_final_solution_error_weight;
00325 }
00326
00327
00328
00329
00332
00333 void FinalSolutionsError::set_target_final_solutions(const Vector<double>& new_target_final_solutions)
00334 {
00335 target_final_solutions = new_target_final_solutions;
00336 }
00337
00338
00339
00340
00344
00345 void FinalSolutionsError::set_target_final_solution(const unsigned int& i, const double& new_target_final_solution)
00346 {
00347 target_final_solutions[i] = new_target_final_solution;
00348 }
00349
00350
00351
00352
00359
00360 void FinalSolutionsError::set_default(void)
00361 {
00362 if(mathematical_model_pointer)
00363 {
00364 const unsigned int dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
00365
00366 target_final_solutions.set(dependent_variables_number, 0.0);
00367
00368 final_solutions_errors_weights.set(dependent_variables_number, 1.0);
00369 }
00370 else
00371 {
00372 target_final_solutions.set();
00373
00374 final_solutions_errors_weights.set();
00375 }
00376
00377 display = true;
00378 }
00379
00380
00381
00382
00386
00387 void FinalSolutionsError::check(void) const
00388 {
00389 std::ostringstream buffer;
00390
00391
00392
00393 if(!neural_network_pointer)
00394 {
00395 buffer << "OpenNN Exception: FinalSolutionsError class.\n"
00396 << "void check(void) const method.\n"
00397 << "Pointer to neural network is NULL.\n";
00398
00399 throw std::logic_error(buffer.str().c_str());
00400 }
00401
00402 const MultilayerPerceptron* multilayer_perceptron_pointer = neural_network_pointer->get_multilayer_perceptron_pointer();
00403
00404 if(!multilayer_perceptron_pointer)
00405 {
00406 buffer << "OpenNN Exception: FinalSolutionsError class.\n"
00407 << "void check(void) const method.\n"
00408 << "Pointer to multilayer perceptron is NULL.\n";
00409
00410 throw std::logic_error(buffer.str().c_str());
00411 }
00412
00413 const unsigned int inputs_number = multilayer_perceptron_pointer->count_inputs_number();
00414 const unsigned int outputs_number = multilayer_perceptron_pointer->count_outputs_number();
00415
00416 if(inputs_number == 0)
00417 {
00418 buffer << "OpenNN Exception: FinalSolutionsError class.\n"
00419 << "void check(void) const method.\n"
00420 << "Number of inputs in multilayer perceptron object is zero.\n";
00421
00422 throw std::logic_error(buffer.str().c_str());
00423 }
00424
00425 if(outputs_number == 0)
00426 {
00427 buffer << "OpenNN Exception: FinalSolutionsError class.\n"
00428 << "void check(void) const method.\n"
00429 << "Number of outputs in multilayer perceptron object is zero.\n";
00430
00431 throw std::logic_error(buffer.str().c_str());
00432 }
00433
00434
00435
00436 if(!mathematical_model_pointer)
00437 {
00438 buffer << "OpenNN Exception: FinalSolutionsError class.\n"
00439 << "void check(void) const method.\n"
00440 << "Pointer to mathematical model is NULL.\n";
00441
00442 throw std::logic_error(buffer.str().c_str());
00443 }
00444
00445
00446
00447 const unsigned int dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
00448
00449 const unsigned int target_final_solutions_size = target_final_solutions.size();
00450
00451 if(target_final_solutions_size != dependent_variables_number)
00452 {
00453 std::ostringstream buffer;
00454
00455 buffer << "OpenNN Exception: FinalSolutionsError class." << std::endl
00456 << "double calculate_evaluation(void) const method." << std::endl
00457 << "Size of target final solutions must be equal to number of dependent variables." << std::endl;
00458
00459 throw std::logic_error(buffer.str().c_str());
00460 }
00461
00462 }
00463
00464
00465
00466
00467 double FinalSolutionsError::calculate_evaluation(void) const
00468 {
00469
00470
00471 #ifdef _DEBUG
00472
00473 check();
00474
00475 #endif
00476
00477
00478
00479 const unsigned int independent_variables_number = mathematical_model_pointer->get_independent_variables_number();
00480 const unsigned int dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
00481
00482 const Vector<double> final_solutions = mathematical_model_pointer->calculate_final_solutions(*neural_network_pointer);
00483
00484 const Vector<double> dependent_variables_final_solutions = final_solutions.take_out(independent_variables_number, dependent_variables_number);
00485
00486 const Vector<double> final_solutions_errors = dependent_variables_final_solutions - target_final_solutions;
00487
00488 return((final_solutions_errors_weights*final_solutions_errors*final_solutions_errors).calculate_sum());
00489 }
00490
00491
00492
00493
00494 double FinalSolutionsError::calculate_evaluation(const Vector<double>& parameters) const
00495 {
00496
00497
00498 #ifdef _DEBUG
00499
00500 check();
00501
00502 #endif
00503
00504
00505
00506 #ifdef _DEBUG
00507
00508 const unsigned int size = parameters.size();
00509
00510 const unsigned int parameters_number = neural_network_pointer->count_parameters_number();
00511
00512 if(size != parameters_number)
00513 {
00514 std::ostringstream buffer;
00515
00516 buffer << "OpenNN Exception: FinalSolutionsError class." << std::endl
00517 << "double calculate_evaluation(const Vector<double>&) const method." << std::endl
00518 << "Size of parameters (" << size << ") must be equal to number of parameters (" << parameters_number << ")." << std::endl;
00519
00520 throw std::logic_error(buffer.str().c_str());
00521 }
00522
00523 #endif
00524
00525 NeuralNetwork neural_network_copy(*neural_network_pointer);
00526 neural_network_copy.set_parameters(parameters);
00527
00528 FinalSolutionsError final_solutions_error_copy(*this);
00529 final_solutions_error_copy.set_neural_network_pointer(&neural_network_copy);
00530 final_solutions_error_copy.set_mathematical_model_pointer(mathematical_model_pointer);
00531
00532 return(final_solutions_error_copy.calculate_evaluation());
00533 }
00534
00535
00536
00537
00539
00540 std::string FinalSolutionsError::write_performance_term_type(void) const
00541 {
00542 return("FINAL_SOLUTIONS_ERROR");
00543 }
00544
00545
00546
00547
00548 std::string FinalSolutionsError::write_information(void) const
00549 {
00550 std::ostringstream buffer;
00551
00552 const unsigned int independent_variables_number = mathematical_model_pointer->get_independent_variables_number();
00553 const unsigned int dependent_variables_number = mathematical_model_pointer->get_dependent_variables_number();
00554
00555 const Vector<double> final_solutions = mathematical_model_pointer->calculate_final_solutions(*neural_network_pointer);
00556
00557 const Vector<double> dependent_variables_final_solutions = final_solutions.take_out(independent_variables_number, dependent_variables_number);
00558
00559 const Vector<double> final_solutions_errors = dependent_variables_final_solutions - target_final_solutions;
00560
00561 const double evaluation = (final_solutions_errors_weights*final_solutions_errors*final_solutions_errors).calculate_sum();
00562
00563 buffer << "Final solutions error\n"
00564 << "Target final solutions: " << target_final_solutions << "\n"
00565 << "Final solutions: " << dependent_variables_final_solutions << "\n"
00566 << "Evaluation: " << evaluation << "\n";
00567
00568 return(buffer.str());
00569 }
00570
00571
00572
00573
00575
00576 TiXmlElement* FinalSolutionsError::to_XML(void) const
00577 {
00578 std::ostringstream buffer;
00579
00580
00581
00582 TiXmlElement* final_solution_error_element = new TiXmlElement("FinalSolutionsError");
00583 final_solution_error_element->SetAttribute("Version", 4);
00584
00585
00586 {
00587 if(numerical_differentiation_pointer)
00588 {
00589 TiXmlElement* element = numerical_differentiation_pointer->to_XML();
00590
00591 if(element)
00592 {
00593 final_solution_error_element->LinkEndChild(element);
00594 }
00595 }
00596 }
00597
00598
00599 {
00600 TiXmlElement* element = new TiXmlElement("FinalSolutionsErrorsWeights");
00601 final_solution_error_element->LinkEndChild(element);
00602
00603 buffer.str("");
00604 buffer << final_solutions_errors_weights;
00605
00606 TiXmlText* text = new TiXmlText(buffer.str().c_str());
00607 element->LinkEndChild(text);
00608 }
00609
00610
00611 {
00612 TiXmlElement* element = new TiXmlElement("TargetFinalSolution");
00613 final_solution_error_element->LinkEndChild(element);
00614
00615 buffer.str("");
00616 buffer << target_final_solutions;
00617
00618 TiXmlText* text = new TiXmlText(buffer.str().c_str());
00619 element->LinkEndChild(text);
00620 }
00621
00622
00623 {
00624 TiXmlElement* display_element = new TiXmlElement("Display");
00625 final_solution_error_element->LinkEndChild(display_element);
00626
00627 buffer.str("");
00628 buffer << display;
00629
00630 TiXmlText* display_text = new TiXmlText(buffer.str().c_str());
00631 display_element->LinkEndChild(display_text);
00632 }
00633
00634 return(final_solution_error_element);
00635 }
00636
00637
00638
00639
00642
00643 void FinalSolutionsError::from_XML(TiXmlElement* final_solutions_error_element)
00644 {
00645 if(final_solutions_error_element)
00646 {
00647
00648 {
00649 TiXmlElement* display_element = final_solutions_error_element->FirstChildElement("Display");
00650
00651 if(display_element)
00652 {
00653 std::string new_display_string = display_element->GetText();
00654
00655 try
00656 {
00657 set_display(new_display_string != "0");
00658 }
00659 catch(std::exception& e)
00660 {
00661 std::cout << e.what() << std::endl;
00662 }
00663 }
00664 }
00665 }
00666 }
00667
00668
00669 }
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686