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
00029 #include "mathematical_model.h"
00030 #include "plug_in.h"
00031
00032 #include "../neural_network/independent_parameters.h"
00033
00034 namespace OpenNN
00035 {
00036
00037
00038
00041
00042 PlugIn::PlugIn(void) : MathematicalModel()
00043 {
00044 set_default();
00045 }
00046
00047
00048
00049
00053
00054 PlugIn::PlugIn(TiXmlElement* plug_in_element)
00055 : MathematicalModel(plug_in_element)
00056 {
00057 }
00058
00059
00060
00061
00064
00065 PlugIn::~PlugIn(void)
00066 {
00067 }
00068
00069
00070
00071
00072
00073
00077
00078 PlugIn& PlugIn::operator = (const PlugIn& other_plug_in)
00079 {
00080 if(this != &other_plug_in)
00081 {
00082 input_method = other_plug_in.input_method;
00083
00084 template_filename = other_plug_in.template_filename;
00085 input_filename = other_plug_in.input_filename;
00086
00087 script_filename = other_plug_in.script_filename;
00088
00089 output_filename = other_plug_in.output_filename;
00090
00091 input_flags = other_plug_in.input_flags;
00092
00093 output_rows_number = other_plug_in.output_rows_number;
00094 output_columns_number = other_plug_in.output_columns_number;
00095 }
00096
00097 return(*this);
00098 }
00099
00100
00101
00102
00103
00104
00109
00110 bool PlugIn::operator == (const PlugIn& other_plug_in) const
00111 {
00112 if(input_method == other_plug_in.input_method
00113 && template_filename == other_plug_in.template_filename
00114 && input_filename == other_plug_in.input_filename
00115 && script_filename == other_plug_in.script_filename
00116 && output_filename == other_plug_in.output_filename
00117 && input_flags == other_plug_in.input_flags
00118 && output_rows_number == other_plug_in.output_rows_number
00119 && output_columns_number == other_plug_in.output_columns_number)
00120 {
00121 return(true);
00122 }
00123 else
00124 {
00125 return(false);
00126 }
00127 }
00128
00129
00130
00131
00132
00133
00135
00136 const PlugIn::InputMethod& PlugIn::get_input_method(void) const
00137 {
00138 return(input_method);
00139 }
00140
00141
00142
00143
00145
00146 std::string PlugIn::write_input_method(void) const
00147 {
00148 switch(input_method)
00149 {
00150 case IndependentParametersInput:
00151 {
00152 return("IndependentParameters");
00153 }
00154 break;
00155
00156 default:
00157 {
00158 std::ostringstream buffer;
00159
00160 buffer << "OpenNN Exception: PlugIn class.\n"
00161 << "std::string get_input_method_name(void) const method.\n"
00162 << "Unknown inputs method.\n";
00163
00164 throw std::logic_error(buffer.str());
00165 }
00166 break;
00167 }
00168 }
00169
00170
00171
00172
00174
00175 const std::string& PlugIn::get_template_filename(void) const
00176 {
00177 return(template_filename);
00178 }
00179
00180
00181
00182
00184
00185 const std::string& PlugIn::get_input_filename(void) const
00186 {
00187 return(input_filename);
00188 }
00189
00190
00191
00192
00194
00195 const std::string& PlugIn::get_script_filename(void) const
00196 {
00197 return(script_filename);
00198 }
00199
00200
00201
00202
00204
00205 const std::string& PlugIn::get_output_filename(void) const
00206 {
00207 return(output_filename);
00208 }
00209
00210
00211
00212
00214
00215 const Vector<std::string>& PlugIn::get_input_flags(void) const
00216 {
00217 return(input_flags);
00218 }
00219
00220
00221
00222
00225
00226 const std::string& PlugIn::get_input_flag(const unsigned int& i) const
00227 {
00228 return(input_flags[i]);
00229 }
00230
00231
00232
00233
00242
00243 void PlugIn::set_default(void)
00244 {
00245 input_method = IndependentParametersInput;
00246
00247 template_filename = "template.dat";
00248 input_filename = "input.dat";
00249
00250 script_filename = "batch.bat";
00251
00252 output_filename = "output.dat";
00253
00254 display = true;
00255 }
00256
00257
00258
00259
00262
00263 void PlugIn::set_input_method(const InputMethod& new_input_method)
00264 {
00265 input_method = new_input_method;
00266 }
00267
00268
00269
00270
00273
00274 void PlugIn::set_input_method(const std::string& new_input_method)
00275 {
00276 if(new_input_method == "IndependentParameters")
00277 {
00278 set_input_method(IndependentParametersInput);
00279 }
00280 else
00281 {
00282 std::ostringstream buffer;
00283
00284 buffer << "OpenNN Exception: PlugIn class.\n"
00285 << "void set_input_method(const std::string&) method.\n"
00286 << "Unknown plug-in method: " << new_input_method << ".\n";
00287
00288 throw std::logic_error(buffer.str());
00289 }
00290 }
00291
00292
00293
00294
00297
00298 void PlugIn::set_template_filename(const std::string& new_template_filename)
00299 {
00300 template_filename = new_template_filename;
00301 }
00302
00303
00304
00305
00308
00309 void PlugIn::set_input_filename(const std::string& new_input_filename)
00310 {
00311 input_filename = new_input_filename;
00312 }
00313
00314
00315
00316
00319
00320 void PlugIn::set_script_filename(const std::string& new_script_filename)
00321 {
00322 script_filename = new_script_filename;
00323 }
00324
00325
00326
00327
00330
00331 void PlugIn::set_output_filename(const std::string& new_output_filename)
00332 {
00333 output_filename = new_output_filename;
00334 }
00335
00336
00337
00338
00341
00342 void PlugIn::set_input_flags(const Vector<std::string>& new_input_flags)
00343 {
00344 input_flags = new_input_flags;
00345 }
00346
00347
00348
00349
00352
00353 void PlugIn::write_input_file(const NeuralNetwork& neural_network) const
00354 {
00355 switch(input_method)
00356 {
00357 case IndependentParametersInput:
00358 {
00359 write_input_file_independent_parameters(neural_network);
00360 }
00361 break;
00362
00363 default:
00364 {
00365 std::ostringstream buffer;
00366
00367 buffer << "OpenNN Exception: PlugIn class.\n"
00368 << "void write_input_file(const NeuralNetwork&) const method.\n"
00369 << "Unknown input method.\n";
00370
00371 throw std::logic_error(buffer.str());
00372 }
00373 break;
00374 }
00375 }
00376
00377
00378
00379
00381
00382 void PlugIn::write_input_file_independent_parameters(const NeuralNetwork& neural_network) const
00383 {
00384 const IndependentParameters* independent_parameters_pointer = neural_network.get_independent_parameters_pointer();
00385
00386 #ifdef _DEBUG
00387
00388 if(!independent_parameters_pointer)
00389 {
00390 std::ostringstream buffer;
00391
00392 buffer << "OpenNN Exception: PlugIn class.\n"
00393 << "void write_input_file_independent_parameters(void) const method.\n"
00394 << "Pointer to independent parameters is null.\n";
00395
00396 throw std::logic_error(buffer.str());
00397 }
00398
00399 #endif
00400
00401 const Vector<double> independent_parameters = independent_parameters_pointer->get_parameters();
00402
00403
00404
00405
00406
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00443
00444
00445
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 }
00478
00479
00480
00481
00483
00484 void PlugIn::run_script(void) const
00485 {
00486 if(!script_filename.empty())
00487 {
00488 system(script_filename.c_str());
00489 }
00490 else
00491 {
00492 std::ostringstream buffer;
00493
00494 buffer << "OpenNN Exception: PlugIn class.\n"
00495 << "void run_script(void) const.\n"
00496 << "Batch filename is empty.\n";
00497
00498 throw std::logic_error(buffer.str());
00499 }
00500 }
00501
00502
00503
00504
00507
00508 Matrix<double> PlugIn::read_output_file(void) const
00509 {
00510 Matrix<double> data(output_filename);
00511
00512 return(data);
00513 }
00514
00515
00516
00517
00520
00521 Matrix<double> PlugIn::read_output_file_header(void) const
00522 {
00523
00524
00525 std::ifstream output_file(output_filename.c_str());
00526
00527 if(!output_file.is_open())
00528 {
00529 std::ostringstream buffer;
00530
00531 buffer << "OpenNN Exception: PlugIn class.\n"
00532 << "Matrix<double> read_output_file_header(void) const method.\n"
00533 << "Cannot open outputs file.\n";
00534
00535 throw std::logic_error(buffer.str().c_str());
00536 }
00537
00538 std::string header;
00539 getline(output_file, header);
00540
00541 Matrix<double> data;
00542
00543 output_file >> data;
00544
00545 output_file.close();
00546
00547 return(data);
00548 }
00549
00550
00551
00552
00561
00562 Matrix<double> PlugIn::calculate_solutions(const NeuralNetwork& neural_network) const
00563 {
00564 write_input_file(neural_network);
00565
00566 run_script();
00567
00568 return(read_output_file());
00569 }
00570
00571
00572
00573
00575
00576 std::string PlugIn::to_string(void) const
00577 {
00578 std::ostringstream buffer;
00579
00580 buffer << "Plug-in\n"
00581 << "Independent variables number: " << independent_variables_number << "\n"
00582 << "Dependent variables number: " << dependent_variables_number << "\n"
00583 << "Input method: " << input_method << "\n"
00584 << "Template filename: " << template_filename << "\n"
00585 << "Input filename: " << input_filename << "\n"
00586 << "Script filename: " << script_filename << "\n"
00587 << "Output filename: " << output_filename << "\n"
00588 << "Input flags: " << input_flags << "\n"
00589 << "Output rows number: " << output_rows_number << "\n"
00590 << "Output columns number: " << output_columns_number << "\n"
00591 << "Display: " << display << "\n";
00592
00593 return(buffer.str());
00594 }
00595
00596
00597
00598
00601
00602 TiXmlElement* PlugIn::to_XML(void) const
00603 {
00604 std::ostringstream buffer;
00605
00606 TiXmlElement* plug_in_element = new TiXmlElement("PlugIn");
00607 plug_in_element->SetAttribute("Version", 4);
00608
00609
00610
00611 {
00612 TiXmlElement* independent_variables_number_element = new TiXmlElement("IndependentVariablesNumber");
00613 plug_in_element->LinkEndChild(independent_variables_number_element);
00614
00615 buffer.str("");
00616 buffer << independent_variables_number;
00617
00618 TiXmlText* independent_variables_number_text = new TiXmlText(buffer.str().c_str());
00619 independent_variables_number_element->LinkEndChild(independent_variables_number_text);
00620 }
00621
00622
00623
00624 {
00625 TiXmlElement* dependent_variables_number_element = new TiXmlElement("DependentVariablesNumber");
00626 plug_in_element->LinkEndChild(dependent_variables_number_element);
00627
00628 buffer.str("");
00629 buffer << dependent_variables_number;
00630
00631 TiXmlText* dependent_variables_number_text = new TiXmlText(buffer.str().c_str());
00632 dependent_variables_number_element->LinkEndChild(dependent_variables_number_text);
00633 }
00634
00635
00636
00637 {
00638 TiXmlElement* input_method_element = new TiXmlElement("InputMethod");
00639 plug_in_element->LinkEndChild(input_method_element);
00640
00641 std::string input_method_name = write_input_method();
00642
00643 TiXmlText* input_method_text = new TiXmlText(input_method_name.c_str());
00644 input_method_element->LinkEndChild(input_method_text);
00645 }
00646
00647
00648
00649 {
00650 TiXmlElement* template_filename_element = new TiXmlElement("TemplateFilename");
00651 plug_in_element->LinkEndChild(template_filename_element);
00652
00653 TiXmlText* template_filename_text = new TiXmlText(template_filename.c_str());
00654 template_filename_element->LinkEndChild(template_filename_text);
00655 }
00656
00657
00658
00659 {
00660 TiXmlElement* input_filename_element = new TiXmlElement("InputFilename");
00661 plug_in_element->LinkEndChild(input_filename_element);
00662
00663 TiXmlText* input_filename_text = new TiXmlText(input_filename.c_str());
00664 input_filename_element->LinkEndChild(input_filename_text);
00665 }
00666
00667
00668
00669 {
00670 TiXmlElement* script_filename_element = new TiXmlElement("BatchFilename");
00671 plug_in_element->LinkEndChild(script_filename_element);
00672
00673 TiXmlText* script_filename_text = new TiXmlText(script_filename.c_str());
00674 script_filename_element->LinkEndChild(script_filename_text);
00675 }
00676
00677
00678
00679 {
00680 TiXmlElement* output_filename_element = new TiXmlElement("OutputFilename");
00681 plug_in_element->LinkEndChild(output_filename_element);
00682
00683 TiXmlText* output_filename_text = new TiXmlText(output_filename.c_str());
00684 output_filename_element->LinkEndChild(output_filename_text);
00685 }
00686
00687
00688
00689 {
00690 TiXmlElement* input_flags_element = new TiXmlElement("InputFlags");
00691 plug_in_element->LinkEndChild(input_flags_element);
00692
00693 buffer.str("");
00694 buffer << input_flags;
00695
00696 TiXmlText* input_flags_text = new TiXmlText(buffer.str().c_str());
00697 input_flags_element->LinkEndChild(input_flags_text);
00698 }
00699
00700
00701
00702 {
00703 TiXmlElement* output_rows_number_element = new TiXmlElement("OutputRowsNumber");
00704 plug_in_element->LinkEndChild(output_rows_number_element);
00705
00706 buffer.str("");
00707 buffer << output_rows_number;
00708
00709 TiXmlText* output_rows_number_text = new TiXmlText(buffer.str().c_str());
00710 output_rows_number_element->LinkEndChild(output_rows_number_text);
00711 }
00712
00713
00714
00715 {
00716 TiXmlElement* output_columns_number_element = new TiXmlElement("OutputColumnsNumber");
00717 plug_in_element->LinkEndChild(output_columns_number_element);
00718
00719 buffer.str("");
00720 buffer << output_columns_number;
00721
00722 TiXmlText* output_columns_number_text = new TiXmlText(buffer.str().c_str());
00723 output_columns_number_element->LinkEndChild(output_columns_number_text);
00724 }
00725
00726
00727
00728 {
00729 TiXmlElement* display_element = new TiXmlElement("Display");
00730 plug_in_element->LinkEndChild(display_element);
00731
00732 buffer.str("");
00733 buffer << display;
00734
00735 TiXmlText* display_text = new TiXmlText(buffer.str().c_str());
00736 display_element->LinkEndChild(display_text);
00737 }
00738
00739 return(plug_in_element);
00740 }
00741
00742
00743
00744
00748
00749 void PlugIn::from_XML(TiXmlElement* plug_in_element)
00750 {
00751 if(plug_in_element)
00752 {
00753
00754 {
00755 TiXmlElement* element = plug_in_element->FirstChildElement("IndependentVariablesNumber");
00756
00757 if(element)
00758 {
00759 const char* text = element->GetText();
00760
00761 if(text)
00762 {
00763 try
00764 {
00765 set_independent_variables_number(atoi(text));
00766 }
00767 catch(std::exception& e)
00768 {
00769 std::cout << e.what() << std::endl;
00770 }
00771 }
00772 }
00773 }
00774
00775
00776 {
00777 TiXmlElement* element = plug_in_element->FirstChildElement("DependentVariablesNumber");
00778
00779 if(element)
00780 {
00781 const char* text = element->GetText();
00782
00783 if(text)
00784 {
00785 try
00786 {
00787 set_dependent_variables_number(atoi(text));
00788 }
00789 catch(std::exception& e)
00790 {
00791 std::cout << e.what() << std::endl;
00792 }
00793 }
00794 }
00795 }
00796
00797
00798 {
00799 TiXmlElement* input_method_element = plug_in_element->FirstChildElement("InputMethod");
00800
00801 if(input_method_element)
00802 {
00803 const char* input_method_text = input_method_element->GetText();
00804
00805 if(input_method_text)
00806 {
00807 try
00808 {
00809 set_input_method(input_method_text);
00810 }
00811 catch(std::exception& e)
00812 {
00813 std::cout << e.what() << std::endl;
00814 }
00815 }
00816 }
00817 }
00818
00819
00820 {
00821 TiXmlElement* template_filename_element = plug_in_element->FirstChildElement("TemplateFilename");
00822
00823 if(template_filename_element)
00824 {
00825 const char* template_filename_text = template_filename_element->GetText();
00826
00827 if(template_filename_text)
00828 {
00829 try
00830 {
00831 set_template_filename(template_filename_text);
00832 }
00833 catch(std::exception& e)
00834 {
00835 std::cout << e.what() << std::endl;
00836 }
00837 }
00838 }
00839 }
00840
00841
00842 {
00843 TiXmlElement* input_filename_element = plug_in_element->FirstChildElement("InputFilename");
00844
00845 if(input_filename_element)
00846 {
00847 const char* input_filename_text = input_filename_element->GetText();
00848
00849 if(input_filename_text)
00850 {
00851 try
00852 {
00853 set_input_filename(input_filename_text);
00854 }
00855 catch(std::exception& e)
00856 {
00857 std::cout << e.what() << std::endl;
00858 }
00859 }
00860 }
00861 }
00862
00863
00864 {
00865 TiXmlElement* script_filename_element = plug_in_element->FirstChildElement("BatchFilename");
00866
00867 if(script_filename_element)
00868 {
00869 const char* script_filename_text = script_filename_element->GetText();
00870
00871 if(script_filename_text)
00872 {
00873 try
00874 {
00875 set_script_filename(script_filename_text);
00876 }
00877 catch(std::exception& e)
00878 {
00879 std::cout << e.what() << std::endl;
00880 }
00881 }
00882 }
00883 }
00884
00885
00886 {
00887 TiXmlElement* output_filename_element = plug_in_element->FirstChildElement("OutputFilename");
00888
00889 if(output_filename_element)
00890 {
00891 const char* output_filename_text = output_filename_element->GetText();
00892
00893 if(output_filename_text)
00894 {
00895 try
00896 {
00897 set_output_filename(output_filename_text);
00898 }
00899 catch(std::exception& e)
00900 {
00901 std::cout << e.what() << std::endl;
00902 }
00903 }
00904 }
00905 }
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933 {
00934 TiXmlElement* display_element = plug_in_element->FirstChildElement("Display");
00935
00936 if(display_element)
00937 {
00938 const char* display_text = display_element->GetText();
00939
00940 if(display_text)
00941 {
00942 try
00943 {
00944 std::string display_string(display_text);
00945
00946 set_display(display_string != "0");
00947 }
00948 catch(std::exception& e)
00949 {
00950 std::cout << e.what() << std::endl;
00951 }
00952 }
00953 }
00954 }
00955 }
00956
00957 }
00958
00959 }
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976