00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include<fstream>
00019 #include<iostream>
00020 #include<string>
00021 #include<sstream>
00022 #include <time.h>
00023
00024
00025
00026 #include "../utilities/vector.h"
00027 #include "../utilities/matrix.h"
00028 #include "mathematical_model.h"
00029
00030 namespace OpenNN
00031 {
00032
00033
00034
00037
00038 MathematicalModel::MathematicalModel(void)
00039 {
00040 set_default();
00041 }
00042
00043
00044
00045
00049
00050 MathematicalModel::MathematicalModel(TiXmlElement* mathematical_model_element)
00051 {
00052 set_default();
00053
00054 from_XML(mathematical_model_element);
00055 }
00056
00057
00058
00059
00063
00064 MathematicalModel::MathematicalModel(const std::string& filename)
00065 {
00066 set_default();
00067
00068 load(filename);
00069 }
00070
00071
00072
00073
00077
00078 MathematicalModel::MathematicalModel(const MathematicalModel& other_mathematical_model)
00079 {
00080 set(other_mathematical_model);
00081 }
00082
00083
00084
00085
00088
00089 MathematicalModel::~MathematicalModel(void)
00090 {
00091 }
00092
00093
00094
00095
00099
00100 MathematicalModel& MathematicalModel::operator = (const MathematicalModel& other_mathematical_model)
00101 {
00102 if(this != &other_mathematical_model)
00103 {
00104 independent_variables_number = other_mathematical_model.independent_variables_number;
00105 dependent_variables_number = other_mathematical_model.dependent_variables_number;
00106
00107 display = other_mathematical_model.display;
00108 }
00109
00110 return(*this);
00111 }
00112
00113
00114
00115
00120
00121 bool MathematicalModel::operator == (const MathematicalModel& other_mathematical_model) const
00122 {
00123 if(independent_variables_number == other_mathematical_model.independent_variables_number
00124 && dependent_variables_number == other_mathematical_model.dependent_variables_number
00125 && display == other_mathematical_model.display)
00126 {
00127 return(true);
00128 }
00129 else
00130 {
00131 return(false);
00132 }
00133 }
00134
00135
00136
00137
00138
00139
00141
00142 const unsigned int& MathematicalModel::get_independent_variables_number(void) const
00143 {
00144 return(independent_variables_number);
00145 }
00146
00147
00148
00149
00151
00152 const unsigned int& MathematicalModel::get_dependent_variables_number(void) const
00153 {
00154 return(dependent_variables_number);
00155 }
00156
00157
00158
00159
00162
00163 unsigned int MathematicalModel::count_variables_number(void) const
00164 {
00165 return(independent_variables_number + dependent_variables_number);
00166 }
00167
00168
00169
00170
00173
00174 const bool& MathematicalModel::get_display(void) const
00175 {
00176 return(display);
00177 }
00178
00179
00180
00181
00184
00185 void MathematicalModel::set(const MathematicalModel& other_mathematical_model)
00186 {
00187 independent_variables_number = other_mathematical_model.independent_variables_number;
00188 dependent_variables_number = other_mathematical_model.dependent_variables_number;
00189
00190 display = other_mathematical_model.display;
00191 }
00192
00193
00194
00195
00198
00199 void MathematicalModel::set_independent_variables_number(const unsigned int& new_independent_variables_number)
00200 {
00201 independent_variables_number = new_independent_variables_number;
00202 }
00203
00204
00205
00206
00209
00210 void MathematicalModel::set_dependent_variables_number(const unsigned int& new_dependent_variables_number)
00211 {
00212 dependent_variables_number = new_dependent_variables_number;
00213 }
00214
00215
00216
00217
00224
00225 void MathematicalModel::set_default(void)
00226 {
00227 dependent_variables_number = 0;
00228 independent_variables_number = 0;
00229
00230 display = true;
00231 }
00232
00233
00234
00235
00240
00241 void MathematicalModel::set_display(const bool& new_display)
00242 {
00243 display = new_display;
00244 }
00245
00246
00247
00248
00252
00253 Matrix<double> MathematicalModel::calculate_solutions(const NeuralNetwork&) const
00254 {
00255 std::ostringstream buffer;
00256
00257 buffer << "OpenNN Exception: MathematicalModel class.\n"
00258 << "Matrix<double> calculate_solutions(const NeuralNetwork&) const method.\n"
00259 << "This method has not been derived.\n";
00260
00261 throw std::logic_error(buffer.str());
00262 }
00263
00264
00265
00266
00270
00271 Vector<double> MathematicalModel::calculate_final_solutions(const NeuralNetwork&) const
00272 {
00273 std::ostringstream buffer;
00274
00275 buffer << "OpenNN Exception: MathematicalModel class.\n"
00276 << "Vector<double> calculate_final_solutions(const NeuralNetwork&) const method.\n"
00277 << "This method has not been derived.\n";
00278
00279 throw std::logic_error(buffer.str());
00280 }
00281
00282
00283
00284
00287
00288 Matrix<double> MathematicalModel::calculate_dependent_variables(const NeuralNetwork&, const Matrix<double>&) const
00289 {
00290 std::ostringstream buffer;
00291
00292 buffer << "OpenNN Exception: MathematicalModel class.\n"
00293 << "Matrix<double> calculate_dependent_variables(const NeuralNetwork&, const Matrix<double>&) const method.\n"
00294 << "This method has not been derived.\n";
00295
00296 throw std::logic_error(buffer.str());
00297 }
00298
00299
00300
00301
00303
00304 std::string MathematicalModel::to_string(void) const
00305 {
00306 std::ostringstream buffer;
00307
00308 buffer << "Mathematical model\n"
00309 << "Independent variables number: " << independent_variables_number << "\n"
00310 << "Dependent variables number: " << dependent_variables_number << "\n"
00311 << "Display: " << display << "\n";
00312
00313 return(buffer.str());
00314 }
00315
00316
00317
00318
00320
00321 void MathematicalModel::print(void) const
00322 {
00323 std::cout << to_string();
00324 }
00325
00326
00327
00328
00329
00332
00333 TiXmlElement* MathematicalModel::to_XML(void) const
00334 {
00335 std::ostringstream buffer;
00336
00337 TiXmlElement* mathematical_model_element = new TiXmlElement("MathematicalModel");
00338 mathematical_model_element->SetAttribute("Version", 4);
00339
00340
00341 {
00342 TiXmlElement* independent_variables_number_element = new TiXmlElement("IndependentVariablesNumber");
00343 mathematical_model_element->LinkEndChild(independent_variables_number_element);
00344
00345 buffer.str("");
00346 buffer << independent_variables_number;
00347
00348 TiXmlText* independent_variables_number_text = new TiXmlText(buffer.str().c_str());
00349 independent_variables_number_element->LinkEndChild(independent_variables_number_text);
00350 }
00351
00352
00353 {
00354 TiXmlElement* dependent_variables_number_element = new TiXmlElement("DependentVariablesNumber");
00355 mathematical_model_element->LinkEndChild(dependent_variables_number_element);
00356
00357 buffer.str("");
00358 buffer << dependent_variables_number;
00359
00360 TiXmlText* dependent_variables_number_text = new TiXmlText(buffer.str().c_str());
00361 dependent_variables_number_element->LinkEndChild(dependent_variables_number_text);
00362 }
00363
00364
00365 {
00366 TiXmlElement* display_element = new TiXmlElement("Display");
00367 mathematical_model_element->LinkEndChild(display_element);
00368
00369 buffer.str("");
00370 buffer << display;
00371
00372 TiXmlText* display_text = new TiXmlText(buffer.str().c_str());
00373 display_element->LinkEndChild(display_text);
00374 }
00375
00376 return(mathematical_model_element);
00377 }
00378
00379
00380
00381
00384
00385 void MathematicalModel::from_XML(TiXmlElement* mathematical_model_element)
00386 {
00387 std::ostringstream buffer;
00388
00389 if(mathematical_model_element != NULL)
00390 {
00391
00392 {
00393 const char* text = mathematical_model_element->GetText();
00394
00395 if(text)
00396 {
00397 const std::string string(text);
00398
00399 if(string != "MathematicalModel")
00400 {
00401 buffer << "OpenNN Exception: MathematicalModel class.\n"
00402 << "void from_XML(TiXmlElement*) method.\n"
00403 << "Unkown root element: " << string << ".\n";
00404
00405 throw std::logic_error(buffer.str());
00406 }
00407 }
00408 else
00409 {
00410 buffer << "OpenNN Exception: MathematicalModel class.\n"
00411 << "void from_XML(TiXmlElement*) method.\n"
00412 << "Unkown root element.\n";
00413
00414 throw std::logic_error(buffer.str());
00415 }
00416 }
00417
00418
00419 {
00420 TiXmlElement* element = mathematical_model_element->FirstChildElement("IndependentVariablesNumber");
00421
00422 if(element)
00423 {
00424 const char* text = element->GetText();
00425
00426 if(text)
00427 {
00428 try
00429 {
00430 set_independent_variables_number(atoi(text));
00431 }
00432 catch(std::exception& e)
00433 {
00434 std::cout << e.what() << std::endl;
00435 }
00436 }
00437 }
00438 }
00439
00440
00441 {
00442 TiXmlElement* element = mathematical_model_element->FirstChildElement("DependentVariablesNumber");
00443
00444 if(element)
00445 {
00446 const char* text = element->GetText();
00447
00448 if(text)
00449 {
00450 try
00451 {
00452 set_dependent_variables_number(atoi(text));
00453 }
00454 catch(std::exception& e)
00455 {
00456 std::cout << e.what() << std::endl;
00457 }
00458 }
00459 }
00460 }
00461
00462
00463 {
00464 TiXmlElement* element = mathematical_model_element->FirstChildElement("Display");
00465
00466 if(element)
00467 {
00468 const char* text = element->GetText();
00469
00470 if(text)
00471 {
00472 try
00473 {
00474 const std::string string(text);
00475
00476 set_display(string != "0");
00477 }
00478 catch(std::exception& e)
00479 {
00480 std::cout << e.what() << std::endl;
00481 }
00482 }
00483 }
00484 }
00485 }
00486 }
00487
00488
00489
00490
00493
00494 void MathematicalModel::save(const std::string& filename) const
00495 {
00496 TiXmlDocument document;
00497
00498
00499
00500 TiXmlDeclaration* declaration = new TiXmlDeclaration("1.0", "", "");
00501 document.LinkEndChild(declaration);
00502
00503
00504
00505 TiXmlElement* mathematical_model_plug_in_element = to_XML();
00506 document.LinkEndChild(mathematical_model_plug_in_element);
00507
00508
00509
00510 document.SaveFile(filename.c_str());
00511
00512 }
00513
00514
00515
00516
00519
00520 void MathematicalModel::load(const std::string& filename)
00521 {
00522 std::ostringstream buffer;
00523
00524 TiXmlDocument document(filename.c_str());
00525
00526 if(!document.LoadFile())
00527 {
00528 buffer << "OpenNN Exception: MathematicalModel class.\n"
00529 << "void load(const std::string&) method.\n"
00530 << "Cannot load XML file " << filename << ".\n";
00531
00532 throw std::logic_error(buffer.str());
00533 }
00534
00535
00536
00537 TiXmlElement* mathematical_model_element = document.FirstChildElement();
00538
00539 if(!mathematical_model_element)
00540 {
00541 buffer << "OpenNN Exception: MathematicalModel class.\n"
00542 << "void load(const std::string&) method.\n"
00543 << "File " << filename << " is not a valid mathematical model file.\n";
00544
00545 throw std::logic_error(buffer.str());
00546 }
00547
00548 from_XML(mathematical_model_element);
00549 }
00550
00551
00552
00553
00555
00556 void MathematicalModel::save_data(const NeuralNetwork&, const std::string&) const
00557 {
00558 std::ostringstream buffer;
00559
00560 buffer << "OpenNN Exception: MathematicalModel class.\n"
00561 << "void save_data(const NeuralNetwork&, const std::string&) const method.\n"
00562 << "This method has not been derived.\n";
00563
00564 throw std::logic_error(buffer.str());
00565 }
00566
00567 }
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585