00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cmath>
00019 #include <cstdlib>
00020 #include <fstream>
00021 #include <iostream>
00022 #include <string>
00023 #include <sstream>
00024
00025
00026
00027 #include "conditions_layer.h"
00028
00029
00030
00031 #include "../../parsers/tinyxml/tinyxml.h"
00032
00033 namespace OpenNN
00034 {
00035
00036
00037
00040
00041 ConditionsLayer::ConditionsLayer(void)
00042 {
00043 set();
00044
00045 set_default();
00046 }
00047
00048
00049
00050
00054
00055 ConditionsLayer::ConditionsLayer(const unsigned int& new_inputs_number, const unsigned int& new_conditions_neurons_number)
00056 {
00057 set(new_inputs_number, new_conditions_neurons_number);
00058
00059 set_default();
00060 }
00061
00062
00063
00064
00067
00068 ConditionsLayer::ConditionsLayer(TiXmlElement* conditions_layer_element)
00069 {
00070 set_default();
00071
00072 from_XML(conditions_layer_element);
00073 }
00074
00075
00076
00077
00080
00081 ConditionsLayer::ConditionsLayer(const ConditionsLayer& other_conditions_layer)
00082 {
00083 set(other_conditions_layer);
00084 }
00085
00086
00087
00088
00090
00091 ConditionsLayer::~ConditionsLayer(void)
00092 {
00093 }
00094
00095
00096
00097
00101
00102 ConditionsLayer& ConditionsLayer::operator = (const ConditionsLayer& other_conditions_layer)
00103 {
00104 if(this != &other_conditions_layer)
00105 {
00106 external_inputs_number = other_conditions_layer.external_inputs_number;
00107 conditions_neurons_number = other_conditions_layer.conditions_neurons_number;
00108 conditions_method = other_conditions_layer.conditions_method;
00109 external_input_values = other_conditions_layer.external_input_values;
00110 output_values = other_conditions_layer.output_values;
00111 display = other_conditions_layer.display;
00112 }
00113
00114 return(*this);
00115 }
00116
00117
00118
00119
00120
00121
00126
00127 bool ConditionsLayer::operator == (const ConditionsLayer& other_conditions_layer) const
00128 {
00129 if(external_inputs_number == other_conditions_layer.external_inputs_number
00130 && conditions_neurons_number == other_conditions_layer.conditions_neurons_number
00131 && conditions_method == other_conditions_layer.conditions_method
00132 && external_input_values == other_conditions_layer.external_input_values
00133 && output_values == other_conditions_layer.output_values
00134 && display == other_conditions_layer.display)
00135 {
00136 return(true);
00137 }
00138 else
00139 {
00140 return(false);
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00150
00151 const unsigned int& ConditionsLayer::get_external_inputs_number(void) const
00152 {
00153 return(external_inputs_number);
00154 }
00155
00156
00157
00158
00160
00161 const unsigned int& ConditionsLayer::get_conditions_neurons_number(void) const
00162 {
00163 return(conditions_neurons_number);
00164 }
00165
00166
00167
00168
00171
00172 const ConditionsLayer::ConditionsMethod& ConditionsLayer::get_conditions_method(void) const
00173 {
00174 return(conditions_method);
00175 }
00176
00177
00178
00179
00182
00183 std::string ConditionsLayer::write_conditions_method(void) const
00184 {
00185 if(conditions_method == OneCondition)
00186 {
00187 return("OneCondition");
00188 }
00189 else if(conditions_method == TwoConditions)
00190 {
00191 return("TwoConditions");
00192 }
00193 else
00194 {
00195 std::ostringstream buffer;
00196
00197 buffer << "OpenNN Exception: ConditionsLayer class.\n"
00198 << "std::string write_conditions_method(void) const method.\n"
00199 << "Unknown conditions method.\n";
00200
00201 throw std::logic_error(buffer.str());
00202 }
00203 }
00204
00205
00206
00207
00209
00210 const Vector<double>& ConditionsLayer::get_external_input_values(void) const
00211 {
00212 return(external_input_values);
00213 }
00214
00215
00216
00217
00220
00221 const double& ConditionsLayer::get_external_input_value(const unsigned int& i) const
00222 {
00223 return(external_input_values[i]);
00224 }
00225
00226
00227
00228
00230
00231 const Matrix<double>& ConditionsLayer::get_output_values(void) const
00232 {
00233 return(output_values);
00234 }
00235
00236
00237
00238
00242
00243 const double& ConditionsLayer::get_output_value(const unsigned int& i, const unsigned int& j) const
00244 {
00245 return(output_values[i][j]);
00246 }
00247
00248
00249
00250
00253
00254 const bool& ConditionsLayer::get_display(void) const
00255 {
00256 return(display);
00257 }
00258
00259
00260
00261
00264
00265 void ConditionsLayer::set(void)
00266 {
00267 external_inputs_number = 0;
00268
00269 conditions_neurons_number = 0;
00270
00271 set_default();
00272 }
00273
00274
00275
00276
00279
00280 void ConditionsLayer::set(const unsigned int& new_external_inputs_number, const unsigned int& new_conditions_neurons_number)
00281 {
00282 external_inputs_number = new_external_inputs_number;
00283
00284 conditions_neurons_number = new_conditions_neurons_number;
00285
00286 set_default();
00287 }
00288
00289
00290
00291
00294
00295 void ConditionsLayer::set(const ConditionsLayer& other_conditions_layer)
00296 {
00297 external_inputs_number = other_conditions_layer.external_inputs_number;
00298
00299 conditions_neurons_number = other_conditions_layer.conditions_neurons_number;
00300
00301 conditions_method = other_conditions_layer.conditions_method;
00302
00303 external_input_values = other_conditions_layer.external_input_values;
00304 output_values = other_conditions_layer.output_values;
00305
00306 display = other_conditions_layer.display;
00307 }
00308
00309
00310
00311
00313
00314 void ConditionsLayer::set_default(void)
00315 {
00316 conditions_method = TwoConditions;
00317
00318 if(conditions_neurons_number == 0)
00319 {
00320 external_input_values.set();
00321
00322 output_values.set();
00323 }
00324 else
00325 {
00326 external_input_values.set(2, 0.0);
00327
00328 output_values.set(conditions_neurons_number, 2, 0.0);
00329 }
00330
00331 display = true;
00332 }
00333
00334
00335
00336
00339
00340 void ConditionsLayer::set_external_inputs_number(const unsigned int& new_external_inputs_number)
00341 {
00342 external_inputs_number = new_external_inputs_number;
00343 }
00344
00345
00346
00347
00350
00351 void ConditionsLayer::set_conditions_neurons_number(const unsigned int& new_conditions_neurons_number)
00352 {
00353 conditions_neurons_number = new_conditions_neurons_number;
00354 }
00355
00356
00357
00358
00361
00362 void ConditionsLayer::set_conditions_method(const ConditionsMethod& new_conditions_method)
00363 {
00364 conditions_method = new_conditions_method;
00365 }
00366
00367
00368
00369
00372
00373 void ConditionsLayer::set_conditions_method(const std::string& new_conditions_method)
00374 {
00375 if(new_conditions_method == "OneCondition")
00376 {
00377 set_conditions_method(OneCondition);
00378 }
00379 else if(new_conditions_method == "TwoConditions")
00380 {
00381 set_conditions_method(TwoConditions);
00382 }
00383 else if(new_conditions_method == "UserConditionsMethod")
00384 {
00385 set_conditions_method(UserConditionsMethod);
00386 }
00387 else
00388 {
00389 std::ostringstream buffer;
00390
00391 buffer << "OpenNN Exception: ConditionsLayer class.\n"
00392 << "void set_conditions_method(const std::string&) method.\n"
00393 << "Unknown conditions method: " << new_conditions_method << ".\n";
00394
00395 throw std::logic_error(buffer.str());
00396 }
00397 }
00398
00399
00400
00401
00404
00405 void ConditionsLayer::set_external_input_values(const Vector<double>& new_external_input_values)
00406 {
00407 external_input_values = new_external_input_values;
00408 }
00409
00410
00411
00412
00416
00417 void ConditionsLayer::set_external_input_value(const unsigned int& i, const double& new_external_input_value)
00418 {
00419 external_input_values[i] = new_external_input_value;
00420 }
00421
00422
00423
00424
00427
00428 void ConditionsLayer::set_output_values(const Matrix<double>& new_output_values)
00429 {
00430 output_values = new_output_values;
00431 }
00432
00433
00434
00435
00440
00441 void ConditionsLayer::set_output_value(const unsigned int& i, const unsigned int& j, const double& new_output_value)
00442 {
00443 output_values[i][j] = new_output_value;
00444 }
00445
00446
00447
00448
00453
00454 void ConditionsLayer::set_display(const bool& new_display)
00455 {
00456 display = new_display;
00457 }
00458
00459
00460
00461
00463
00464 void ConditionsLayer::initialize_random(void)
00465 {
00466
00467 }
00468
00469
00470
00471
00473
00474 void ConditionsLayer::check(void) const
00475 {
00476 std::ostringstream buffer;
00477
00478 if(conditions_method == OneCondition || conditions_method == TwoConditions)
00479 {
00480 if(external_inputs_number != 1)
00481 {
00482 buffer << "OpenNN Exception: BoundingLayer class.\n"
00483 << "void check(void) const method.\n"
00484 << "Number of external inputs must be one.\n";
00485
00486 throw std::logic_error(buffer.str());
00487 }
00488 }
00489
00490 const unsigned int external_input_values_size = external_input_values.size();
00491
00492 const unsigned int output_values_rows_number = output_values.get_rows_number();
00493 const unsigned int output_values_columns_number = output_values.get_rows_number();
00494
00495 if(output_values_rows_number != conditions_neurons_number)
00496 {
00497 buffer << "OpenNN Exception: BoundingLayer class.\n"
00498 << "void check(void) const method.\n"
00499 << "Number of rows in output values is not equal to number of conditions neurons.\n";
00500
00501 throw std::logic_error(buffer.str());
00502 }
00503
00504 if(conditions_method == OneCondition)
00505 {
00506 if(external_input_values_size != 1)
00507 {
00508 buffer << "OpenNN Exception: BoundingLayer class.\n"
00509 << "void check(void) const method.\n"
00510 << "Size of input values is not 1.\n";
00511
00512 throw std::logic_error(buffer.str());
00513 }
00514
00515 if(output_values_columns_number != 1)
00516 {
00517 buffer << "OpenNN Exception: BoundingLayer class.\n"
00518 << "void check(void) const method.\n"
00519 << "Number of columns in output values is not 1.\n";
00520
00521 throw std::logic_error(buffer.str());
00522 }
00523 }
00524 else if(conditions_method == OneCondition)
00525 {
00526 if(external_input_values_size != 2)
00527 {
00528 buffer << "OpenNN Exception: BoundingLayer class.\n"
00529 << "void check(void) const method.\n"
00530 << "Size of input values is not 2.\n";
00531
00532 throw std::logic_error(buffer.str());
00533 }
00534
00535 if(output_values_columns_number != 2)
00536 {
00537 buffer << "OpenNN Exception: BoundingLayer class.\n"
00538 << "void check(void) const method.\n"
00539 << "Number of columns in output values is not 2.\n";
00540
00541 throw std::logic_error(buffer.str());
00542 }
00543 }
00544 }
00545
00546
00547
00548
00551
00552 Vector<double> ConditionsLayer::calculate_particular_solution(const Vector<double>& external_inputs) const
00553 {
00554 switch(conditions_method)
00555 {
00556 case OneCondition:
00557 {
00558 return(calculate_one_condition_particular_solution(external_inputs));
00559 }
00560 break;
00561
00562 case TwoConditions:
00563 {
00564 return(calculate_two_conditions_particular_solution(external_inputs));
00565 }
00566 break;
00567
00568 default:
00569 {
00570 std::ostringstream buffer;
00571
00572 buffer << "OpenNN Exception: ScalingLayer class\n"
00573 << "Vector<double> calculate_particular_solution(const Vector<double>&) const method.\n"
00574 << "Unknown conditions method.\n";
00575
00576 throw std::logic_error(buffer.str());
00577 }
00578 break;
00579 }
00580 }
00581
00582
00583
00584
00587
00588 Matrix<double> ConditionsLayer::calculate_particular_solution_Jacobian(const Vector<double>& external_inputs) const
00589 {
00590 switch(conditions_method)
00591 {
00592 case OneCondition:
00593 {
00594 return(calculate_one_condition_particular_solution_Jacobian(external_inputs));
00595 }
00596 break;
00597
00598 case TwoConditions:
00599 {
00600 return(calculate_two_conditions_particular_solution_Jacobian(external_inputs));
00601 }
00602 break;
00603
00604 default:
00605 {
00606 std::ostringstream buffer;
00607
00608 buffer << "OpenNN Exception: ScalingLayer class\n"
00609 << "Matrix<double> calculate_particular_solution_Jacobian(const Vector<double>&) const method.\n"
00610 << "Unknown conditions method.\n";
00611
00612 throw std::logic_error(buffer.str());
00613 }
00614 break;
00615 }
00616 }
00617
00618
00619
00620
00623
00624 Vector< Matrix<double> > ConditionsLayer::calculate_particular_solution_Hessian_form(const Vector<double>& external_inputs) const
00625 {
00626 switch(conditions_method)
00627 {
00628 case OneCondition:
00629 {
00630 return(calculate_one_condition_particular_solution_Hessian_form(external_inputs));
00631 }
00632 break;
00633
00634 case TwoConditions:
00635 {
00636 return(calculate_two_conditions_particular_solution_Hessian_form(external_inputs));
00637 }
00638 break;
00639
00640 default:
00641 {
00642 std::ostringstream buffer;
00643
00644 buffer << "OpenNN Exception: ScalingLayer class\n"
00645 << "Vector< Matrix<double> > calculate_particular_solution_Hessian_form(const Vector<double>&) const method.\n"
00646 << "Unknown conditions method.\n";
00647
00648 throw std::logic_error(buffer.str());
00649 }
00650 break;
00651 }
00652 }
00653
00654
00655
00656
00659
00660 Vector<double> ConditionsLayer::calculate_homogeneous_solution(const Vector<double>& external_inputs) const
00661 {
00662 switch(conditions_method)
00663 {
00664 case OneCondition:
00665 {
00666 return(calculate_one_condition_homogeneous_solution(external_inputs));
00667 }
00668 break;
00669
00670 case TwoConditions:
00671 {
00672 return(calculate_two_conditions_homogeneous_solution(external_inputs));
00673 }
00674 break;
00675
00676 default:
00677 {
00678 std::ostringstream buffer;
00679
00680 buffer << "OpenNN Exception: ScalingLayer class\n"
00681 << "Vector<double> calculate_homogeneous_solution(const Vector<double>&) const method.\n"
00682 << "Unknown conditions method.\n";
00683
00684 throw std::logic_error(buffer.str());
00685 }
00686 break;
00687 }
00688 }
00689
00690
00691
00692
00695
00696 Matrix<double> ConditionsLayer::calculate_homogeneous_solution_Jacobian(const Vector<double>& external_inputs) const
00697 {
00698 switch(conditions_method)
00699 {
00700 case OneCondition:
00701 {
00702 return(calculate_one_condition_homogeneous_solution_Jacobian(external_inputs));
00703 }
00704 break;
00705
00706 case TwoConditions:
00707 {
00708 return(calculate_two_conditions_homogeneous_solution_Jacobian(external_inputs));
00709 }
00710 break;
00711
00712 default:
00713 {
00714 std::ostringstream buffer;
00715
00716 buffer << "OpenNN Exception: ScalingLayer class\n"
00717 << "Matrix<double> calculate_homogeneous_solution_Jacobian(const Vector<double>&) const method.\n"
00718 << "Unknown conditions method.\n";
00719
00720 throw std::logic_error(buffer.str());
00721 }
00722 break;
00723 }
00724 }
00725
00726
00727
00728
00731
00732 Vector< Matrix<double> > ConditionsLayer::calculate_homogeneous_solution_Hessian_form(const Vector<double>& external_inputs) const
00733 {
00734 switch(conditions_method)
00735 {
00736 case OneCondition:
00737 {
00738 return(calculate_one_condition_homogeneous_solution_Hessian_form(external_inputs));
00739 }
00740 break;
00741
00742 case TwoConditions:
00743 {
00744 return(calculate_two_conditions_homogeneous_solution_Hessian_form(external_inputs));
00745 }
00746 break;
00747
00748 default:
00749 {
00750 std::ostringstream buffer;
00751
00752 buffer << "OpenNN Exception: ScalingLayer class\n"
00753 << "Matrix<double> calculate_homogeneous_solution_Hessian_form(const Vector<double>&) const method.\n"
00754 << "Unknown conditions method.\n";
00755
00756 throw std::logic_error(buffer.str());
00757 }
00758 break;
00759 }
00760 }
00761
00762
00763
00764
00768
00769 Vector<double> ConditionsLayer::calculate_outputs(const Vector<double>& external_inputs, const Vector<double>& inputs) const
00770 {
00771 const Vector<double> particular_solution = calculate_particular_solution(external_inputs);
00772 const Vector<double> homogeneous_solution = calculate_homogeneous_solution(external_inputs);
00773
00774 return(particular_solution + homogeneous_solution*inputs);
00775 }
00776
00777
00778
00779
00784
00785 Matrix<double> ConditionsLayer::calculate_Jacobian(const Vector<double>& external_inputs, const Vector<double>& outputs, const Matrix<double>& Jacobian) const
00786 {
00787 const Vector<double> homogeneous_solution = calculate_homogeneous_solution(external_inputs);
00788
00789 const Matrix<double> particular_solution_Jacobian = calculate_particular_solution_Jacobian(external_inputs);
00790 const Matrix<double> homogeneous_solution_Jacobian = calculate_homogeneous_solution_Jacobian(external_inputs);
00791
00792 return(particular_solution_Jacobian + homogeneous_solution_Jacobian*outputs + homogeneous_solution*Jacobian);
00793 }
00794
00795
00796
00797
00799
00800 Vector< Matrix<double> > ConditionsLayer::calculate_Hessian_form(const Vector<double>&, const Vector<double>&) const
00801 {
00802 Vector< Matrix<double> > Hessian_form(conditions_neurons_number);
00803
00804 return(Hessian_form);
00805 }
00806
00807
00808
00809
00812
00813 Vector<double> ConditionsLayer::calculate_one_condition_particular_solution(const Vector<double>& external_inputs) const
00814 {
00815 #ifdef _DEBUG
00816
00817 check();
00818
00819 #endif
00820
00821 const Vector<double> particular_solution(conditions_neurons_number, external_inputs[0]);
00822
00823 return(particular_solution);
00824 }
00825
00826
00827
00828
00830
00831 Matrix<double> ConditionsLayer::calculate_one_condition_particular_solution_Jacobian(const Vector<double>&) const
00832 {
00833 #ifdef _DEBUG
00834
00835 check();
00836
00837 #endif
00838
00839 const Matrix<double> particular_solution_Jacobian(conditions_neurons_number, 1, 0.0);
00840
00841 return(particular_solution_Jacobian);
00842 }
00843
00844
00845
00846
00849
00850 Vector< Matrix<double> > ConditionsLayer::calculate_one_condition_particular_solution_Hessian_form(const Vector<double>&) const
00851 {
00852 #ifdef _DEBUG
00853
00854 check();
00855
00856 #endif
00857
00858 Vector< Matrix<double> > particular_solution_Hessian_form(conditions_neurons_number);
00859
00860 return(particular_solution_Hessian_form);
00861 }
00862
00863
00864
00865
00868
00869 Vector<double> ConditionsLayer::calculate_one_condition_homogeneous_solution(const Vector<double>& external_inputs) const
00870 {
00871 #ifdef _DEBUG
00872
00873 check();
00874
00875 #endif
00876
00877 const Vector<double> homogeneous_solution(conditions_neurons_number, external_inputs[0]-external_input_values[0]);
00878
00879 return(homogeneous_solution);
00880 }
00881
00882
00883
00884
00885
00887
00888 Matrix<double> ConditionsLayer::calculate_one_condition_homogeneous_solution_Jacobian(const Vector<double>&) const
00889 {
00890 #ifdef _DEBUG
00891
00892 check();
00893
00894 #endif
00895
00896 const Matrix<double> homogeneous_solution_Jacobian(conditions_neurons_number, 1, 1.0);
00897
00898 return(homogeneous_solution_Jacobian);
00899 }
00900
00901
00902
00903
00906
00907 Vector< Matrix<double> > ConditionsLayer::calculate_one_condition_homogeneous_solution_Hessian_form(const Vector<double>&) const
00908 {
00909 #ifdef _DEBUG
00910
00911 check();
00912
00913 #endif
00914
00915 Vector< Matrix<double> > homogeneous_solution_Hessian_form(conditions_neurons_number);
00916
00917 return(homogeneous_solution_Hessian_form);
00918 }
00919
00920
00921
00922
00925
00926 Vector<double> ConditionsLayer::calculate_two_conditions_particular_solution(const Vector<double>& external_inputs) const
00927 {
00928 #ifdef _DEBUG
00929
00930 check();
00931
00932 #endif
00933
00934 const double x = external_inputs[0];
00935
00936 const double xa = external_input_values[0];
00937 const double xb = external_input_values[1];
00938
00939 #ifdef _DEBUG
00940
00941 if(xb - xa < 1.0e-99)
00942 {
00943 std::ostringstream buffer;
00944
00945 buffer << "OpenNN Exception: BoundingLayer class.\n"
00946 << "Vector<double> calculate_two_conditions_particular_solution(const Vector<double>&) const method.\n"
00947 << "Both input values are the same.\n";
00948
00949 throw std::logic_error(buffer.str());
00950 }
00951
00952 #endif
00953
00954 double ya;
00955 double yb;
00956
00957 Vector<double> particular_solutions(conditions_neurons_number);
00958
00959 for(unsigned int i = 0; i < conditions_neurons_number; i++)
00960 {
00961 ya = output_values[i][0];
00962 yb = output_values[i][1];
00963
00964 particular_solutions[i] = ya + (yb-ya)*(x-xa)/(double)(xb-xa);
00965 }
00966
00967 return(particular_solutions);
00968 }
00969
00970
00971
00972
00975
00976 Matrix<double> ConditionsLayer::calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const
00977 {
00978 #ifdef _DEBUG
00979
00980 check();
00981
00982 #endif
00983
00984 const double xa = external_input_values[0];
00985 const double xb = external_input_values[1];
00986
00987 #ifdef _DEBUG
00988
00989 if(xb - xa < 1.0e-99)
00990 {
00991 std::ostringstream buffer;
00992
00993 buffer << "OpenNN Exception: BoundingLayer class.\n"
00994 << "Matrix<double> calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const method.\n"
00995 << "Both input values are the same.\n";
00996
00997 throw std::logic_error(buffer.str());
00998 }
00999
01000 #endif
01001
01002 const double ya = output_values[0][0];
01003 const double yb = output_values[0][1];
01004
01005 Matrix<double> particular_solution_Jacobian(1, 1, (yb-ya)/(xb-xa));
01006
01007 return(particular_solution_Jacobian);
01008 }
01009
01010
01011
01012
01015
01016 Vector< Matrix<double> > ConditionsLayer::calculate_two_conditions_particular_solution_Hessian_form(const Vector<double>&) const
01017 {
01018 #ifdef _DEBUG
01019
01020 check();
01021
01022 #endif
01023
01024 Vector< Matrix<double> > particular_solution_Hessian_form(conditions_neurons_number);
01025
01026 return(particular_solution_Hessian_form);
01027 }
01028
01029
01030
01031
01034
01035 Vector<double> ConditionsLayer::calculate_two_conditions_homogeneous_solution(const Vector<double>& external_inputs) const
01036 {
01037 #ifdef _DEBUG
01038
01039 check();
01040
01041 #endif
01042
01043 const double x = external_inputs[0];
01044
01045 const double xa = external_input_values[0];
01046 const double xb = external_input_values[1];
01047
01048 #ifdef _DEBUG
01049
01050 if(xb - xa < 1.0e-99)
01051 {
01052 std::ostringstream buffer;
01053
01054 buffer << "OpenNN Exception: BoundingLayer class.\n"
01055 << "Matrix<double> calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const method.\n"
01056 << "Both input values are the same.\n";
01057
01058 throw std::logic_error(buffer.str());
01059 }
01060
01061 #endif
01062
01063 const Vector<double> homogeneous_solutions(conditions_neurons_number, (x-xa)*(x-xb));
01064
01065 return(homogeneous_solutions);
01066 }
01067
01068
01069
01070
01073
01074 Matrix<double> ConditionsLayer::calculate_two_conditions_homogeneous_solution_Jacobian(const Vector<double>& external_inputs) const
01075 {
01076 #ifdef _DEBUG
01077
01078 check();
01079
01080 #endif
01081
01082 const double x = external_inputs[0];
01083
01084 const double xa = external_input_values[0];
01085 const double xb = external_input_values[1];
01086
01087 #ifdef _DEBUG
01088
01089 if(xb - xa < 1.0e-99)
01090 {
01091 std::ostringstream buffer;
01092
01093 buffer << "OpenNN Exception: BoundingLayer class.\n"
01094 << "Matrix<double> calculate_two_conditions_particular_solution_Jacobian(const Vector<double>&) const method.\n"
01095 << "Both input values are the same.\n";
01096
01097 throw std::logic_error(buffer.str());
01098 }
01099
01100 #endif
01101
01102 Matrix<double> homogeneous_solution_Jacobian(1, 1, (x-xa) + (x-xb));
01103
01104 return(homogeneous_solution_Jacobian);
01105 }
01106
01107
01108
01109
01111
01112
01113 Vector< Matrix<double> > ConditionsLayer::calculate_two_conditions_homogeneous_solution_Hessian_form(const Vector<double>&) const
01114 {
01115 #ifdef _DEBUG
01116
01117 check();
01118
01119 #endif
01120
01121 Vector< Matrix<double> > homogeneous_solution_Hessian_form(conditions_neurons_number);
01122
01123 return(homogeneous_solution_Hessian_form);
01124 }
01125
01126
01127
01128
01134
01135 std::string ConditionsLayer::write_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& inputs_names, const Vector<std::string>& outputs_names) const
01136 {
01137 std::ostringstream buffer;
01138
01139 Vector<std::string> particular_solutions_names(conditions_neurons_number);
01140 Vector<std::string> homogeneous_solutions_names(conditions_neurons_number);
01141
01142 for(unsigned int i = 0; i < conditions_neurons_number; i++)
01143 {
01144 buffer.str("");
01145 buffer << "particular_solution_" << inputs_names[i];
01146 particular_solutions_names[i] = buffer.str();
01147
01148 buffer.str("");
01149 buffer << "homogeneous_solution_" << inputs_names[i];
01150 homogeneous_solutions_names[i] = buffer.str();
01151
01152 buffer.str("");
01153 }
01154
01155 buffer << write_particular_solution_expression(external_inputs_names, particular_solutions_names)
01156 << write_homogeneous_solution_expression(external_inputs_names, particular_solutions_names);
01157
01158
01159 for(unsigned int i = 0; i < conditions_neurons_number; i++)
01160 {
01161 buffer << outputs_names[i] << "=\n";
01162 }
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175 return(buffer.str());
01176 }
01177
01178
01179
01180
01185
01186 std::string ConditionsLayer::write_particular_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& particular_solutions_names) const
01187 {
01188 switch(conditions_method)
01189 {
01190 case OneCondition:
01191 {
01192 return(write_one_condition_particular_solution_expression(external_inputs_names, particular_solutions_names));
01193 }
01194 break;
01195
01196 case TwoConditions:
01197 {
01198 return(write_two_conditions_particular_solution_expression(external_inputs_names, particular_solutions_names));
01199 }
01200 break;
01201
01202 default:
01203 {
01204 std::ostringstream buffer;
01205
01206 buffer << "OpenNN Exception: ScalingLayer class\n"
01207 << "std::string write_particular_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
01208 << "Unknown conditions method.\n";
01209
01210 throw std::logic_error(buffer.str());
01211 }
01212 break;
01213 }
01214 }
01215
01216
01217
01218
01223
01224 std::string ConditionsLayer::write_homogeneous_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& homogeneous_solutions_names) const
01225 {
01226 switch(conditions_method)
01227 {
01228 case OneCondition:
01229 {
01230 return(write_one_condition_homogeneous_solution_expression(external_inputs_names, homogeneous_solutions_names));
01231 }
01232 break;
01233
01234 case TwoConditions:
01235 {
01236 return(write_two_conditions_homogeneous_solution_expression(external_inputs_names, homogeneous_solutions_names));
01237 }
01238 break;
01239
01240 default:
01241 {
01242 std::ostringstream buffer;
01243
01244 buffer << "OpenNN Exception: ScalingLayer class\n"
01245 << "std::string write_homogeneous_solution_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
01246 << "Unknown conditions method.\n";
01247
01248 throw std::logic_error(buffer.str());
01249 }
01250 break;
01251 }
01252 }
01253
01254
01255
01256
01260
01261 std::string ConditionsLayer::write_one_condition_particular_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& particular_solutions_names) const
01262 {
01263 std::ostringstream buffer;
01264
01265 for(unsigned int i = 0; i < conditions_neurons_number; i++)
01266 {
01267 buffer << particular_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
01268 }
01269
01270 return(buffer.str());
01271 }
01272
01273
01274
01275
01279
01280 std::string ConditionsLayer::write_one_condition_homogeneous_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& homogeneous_solutions_names) const
01281 {
01282 std::ostringstream buffer;
01283
01284 for(unsigned int i = 0; i < conditions_neurons_number; i++)
01285 {
01286 buffer << homogeneous_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
01287 }
01288
01289 return(buffer.str());
01290 }
01291
01292
01293
01294
01298
01299 std::string ConditionsLayer::write_two_conditions_particular_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& particular_solutions_names) const
01300 {
01301 std::ostringstream buffer;
01302
01303 for(unsigned int i = 0; i < conditions_neurons_number; i++)
01304 {
01305 buffer << particular_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
01306 }
01307
01308 return(buffer.str());
01309 }
01310
01311
01312
01313
01317
01318 std::string ConditionsLayer::write_two_conditions_homogeneous_solution_expression(const Vector<std::string>& external_inputs_names, const Vector<std::string>& homogeneous_solutions_names) const
01319 {
01320 std::ostringstream buffer;
01321
01322 for(unsigned int i = 0; i < conditions_neurons_number; i++)
01323 {
01324 buffer << homogeneous_solutions_names[i] << " = " << external_inputs_names[i] << "\n";
01325 }
01326
01327 return(buffer.str());
01328 }
01329
01330
01331
01332
01338
01339 std::string ConditionsLayer::write_output_expression(const Vector<std::string>& particular_solutions_names, const Vector<std::string>& homogeneous_solutions_names, const Vector<std::string>& inputs_names, const Vector<std::string>& outputs_names) const
01340 {
01341 std::ostringstream buffer;
01342
01343 for(unsigned int i = 0; i < conditions_neurons_number; i++)
01344 {
01345 buffer << outputs_names[i] << "=" << particular_solutions_names[i] << "+" << homogeneous_solutions_names[i] << "*" << inputs_names[i] << ";\n";
01346 }
01347
01348 return(buffer.str());
01349 }
01350
01351
01352
01353
01355
01356 std::string ConditionsLayer::to_string(void) const
01357 {
01358 std::ostringstream buffer;
01359
01360 buffer << "Conditions layer\n"
01361 << "External inputs number: " << external_inputs_number << "\n"
01362 << "Conditions neurons number: " <<conditions_neurons_number << "\n"
01363 << "Conditions method: " << write_conditions_method() << "\n"
01364 << "Input values: " <<external_input_values << "\n"
01365 << "Output values: " << output_values
01366 << "Display: " << display << "\n";
01367
01368 return(buffer.str());
01369 }
01370
01371
01372
01373
01376
01377 TiXmlElement* ConditionsLayer::to_XML(void) const
01378 {
01379 std::ostringstream buffer;
01380
01381 TiXmlElement* conditions_layer_element = new TiXmlElement("ConditionsLayer");
01382 conditions_layer_element->SetAttribute("Version", 4);
01383
01384
01385 {
01386 TiXmlElement* element = new TiXmlElement("ExternalInputsNumber");
01387 conditions_layer_element->LinkEndChild(element);
01388
01389 buffer.str("");
01390 buffer << external_inputs_number;
01391
01392 TiXmlText* text = new TiXmlText(buffer.str().c_str());
01393 element->LinkEndChild(text);
01394 }
01395
01396
01397 {
01398 TiXmlElement* element = new TiXmlElement("ConditionsNeuronsNumber");
01399 conditions_layer_element->LinkEndChild(element);
01400
01401 buffer.str("");
01402 buffer << conditions_neurons_number;
01403
01404 TiXmlText* text = new TiXmlText(buffer.str().c_str());
01405 element->LinkEndChild(text);
01406 }
01407
01408
01409 {
01410 TiXmlElement* element = new TiXmlElement("ConditionsMethod");
01411 conditions_layer_element->LinkEndChild(element);
01412
01413 buffer.str("");
01414 buffer << write_conditions_method();
01415
01416 TiXmlText* text = new TiXmlText(buffer.str().c_str());
01417 element->LinkEndChild(text);
01418 }
01419
01420
01421 {
01422 TiXmlElement* element = new TiXmlElement("InputValues");
01423 conditions_layer_element->LinkEndChild(element);
01424
01425 buffer.str("");
01426 buffer << external_input_values;
01427
01428 TiXmlText* text = new TiXmlText(buffer.str().c_str());
01429 element->LinkEndChild(text);
01430 }
01431
01432
01433 {
01434 TiXmlElement* element = new TiXmlElement("OutputValues");
01435 conditions_layer_element->LinkEndChild(element);
01436
01437 buffer.str("");
01438 buffer << output_values.to_vector();
01439
01440 TiXmlText* text = new TiXmlText(buffer.str().c_str());
01441 element->LinkEndChild(text);
01442 }
01443
01444
01445 {
01446 TiXmlElement* display_element = new TiXmlElement("Display");
01447 conditions_layer_element->LinkEndChild(display_element);
01448
01449 buffer.str("");
01450 buffer << display;
01451
01452 TiXmlText* display_text = new TiXmlText(buffer.str().c_str());
01453 display_element->LinkEndChild(display_text);
01454 }
01455
01456 return(conditions_layer_element);
01457 }
01458
01459
01460
01461
01465
01466 void ConditionsLayer::from_XML(TiXmlElement* conditions_layer_element)
01467 {
01468 if(conditions_layer_element)
01469 {
01470
01471 {
01472 TiXmlElement* element = conditions_layer_element->FirstChildElement("ExternalInputsNumber");
01473
01474 if(element)
01475 {
01476 const char* text = element->GetText();
01477
01478 if(text)
01479 {
01480 try
01481 {
01482 set_external_inputs_number(atoi(text));
01483 }
01484 catch(std::exception& e)
01485 {
01486 std::cout << e.what() << std::endl;
01487 }
01488 }
01489 }
01490 }
01491
01492
01493 {
01494 TiXmlElement* element = conditions_layer_element->FirstChildElement("ConditionsNeuronsNumber");
01495
01496 if(element)
01497 {
01498 const char* text = element->GetText();
01499
01500 if(text)
01501 {
01502 try
01503 {
01504 set_conditions_neurons_number(atoi(text));
01505 }
01506 catch(std::exception& e)
01507 {
01508 std::cout << e.what() << std::endl;
01509 }
01510 }
01511 }
01512 }
01513
01514
01515 {
01516 TiXmlElement* element = conditions_layer_element->FirstChildElement("ConditionsMethod");
01517
01518 if(element)
01519 {
01520 const char* text = element->GetText();
01521
01522 if(text)
01523 {
01524 try
01525 {
01526 std::string new_conditions_method(text);
01527
01528 set_conditions_method(new_conditions_method);
01529 }
01530 catch(std::exception& e)
01531 {
01532 std::cout << e.what() << std::endl;
01533 }
01534 }
01535 }
01536 }
01537
01538
01539 {
01540 TiXmlElement* element = conditions_layer_element->FirstChildElement("InputValues");
01541
01542 if(element)
01543 {
01544 const char* text = element->GetText();
01545
01546 if(text)
01547 {
01548 try
01549 {
01550 Vector<double> new_external_input_values;
01551 new_external_input_values.parse(text);
01552
01553 set_external_input_values(new_external_input_values);
01554 }
01555 catch(std::exception& e)
01556 {
01557 std::cout << e.what() << std::endl;
01558 }
01559 }
01560 }
01561 }
01562
01563
01564 {
01565 TiXmlElement* element = conditions_layer_element->FirstChildElement("OutputValues");
01566
01567 if(element)
01568 {
01569 const char* text = element->GetText();
01570
01571 if(text)
01572 {
01573 try
01574 {
01575 Vector<double> new_output_values_vector;
01576 new_output_values_vector.parse(text);
01577
01578 Matrix<double> new_output_values = new_output_values_vector.to_matrix(conditions_neurons_number, 2);
01579
01580 set_output_values(new_output_values);
01581 }
01582 catch(std::exception& e)
01583 {
01584 std::cout << e.what() << std::endl;
01585 }
01586 }
01587 }
01588 }
01589
01590
01591 {
01592 TiXmlElement* element = conditions_layer_element->FirstChildElement("Display");
01593
01594 if(element)
01595 {
01596 const char* text = element->GetText();
01597
01598 if(text)
01599 {
01600 try
01601 {
01602 const std::string string(text);
01603
01604 set_display(string != "0");
01605 }
01606 catch(std::exception& e)
01607 {
01608 std::cout << e.what() << std::endl;
01609 }
01610 }
01611 }
01612 }
01613 }
01614 }
01615
01616 }
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633