16 #include "bounding_layer.h"
40 set(bounding_neurons_number);
54 set(bounding_layer_document);
66 set(other_bounding_layer);
88 if(
this != &other_bounding_layer)
173 if(i >= bounding_neurons_number)
175 std::ostringstream buffer;
177 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
178 <<
"double get_lower_bound(const size_t&) const method.\n"
179 <<
"Index must be less than number of bounding neurons.\n";
181 throw std::logic_error(buffer.str());
213 if(bounding_neurons_number == 0)
215 std::ostringstream buffer;
217 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
218 <<
"double get_upper_bound(const size_t&) const method.\n"
219 <<
"Number of bounding neurons is zero.\n";
221 throw std::logic_error(buffer.str());
223 else if(i >= bounding_neurons_number)
225 std::ostringstream buffer;
227 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
228 <<
"double get_upper_bound(const size_t&) const method.\n"
229 <<
"Index must be less than number of bounding neurons.\n";
231 throw std::logic_error(buffer.str());
328 if(new_lower_bounds.size() != bounding_neurons_number)
330 std::ostringstream buffer;
332 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
333 <<
"void set_lower_bounds(const Vector<double>&) method.\n"
334 <<
"Size must be equal to number of bounding neurons number.\n";
336 throw std::logic_error(buffer.str());
362 if(index >= bounding_neurons_number)
364 std::ostringstream buffer;
366 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
367 <<
"void set_lower_bound(const size_t&, const double&) method.\n"
368 <<
"Index of bounding neurons must be less than number of bounding neurons.\n";
370 throw std::logic_error(buffer.str());
400 if(new_upper_bounds.size() != bounding_neurons_number)
402 std::ostringstream buffer;
404 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
405 <<
"void set_upper_bound(const Vector<double>&) method.\n"
406 <<
"Size must be equal to number of bounding neurons.\n";
408 throw std::logic_error(buffer.str());
434 if(index >= bounding_neurons_number)
436 std::ostringstream buffer;
438 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
439 <<
"void set_upper_bound(const size_t&, const double&) method.\n"
440 <<
"Index of bounding neuron must be less than number of bounding neurons.\n";
442 throw std::logic_error(buffer.str());
473 const size_t size = new_bounds.size();
479 std::ostringstream buffer;
481 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
482 <<
"void set_bounds(const Vector< Vector<double> >&) method.\n"
483 <<
"Number of rows must be 2.\n";
485 throw std::logic_error(buffer.str());
487 else if(new_bounds[0].size() != bounding_neurons_number
488 && new_bounds[1].size() != bounding_neurons_number)
490 std::ostringstream buffer;
492 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
493 <<
"void set_bounds(const Vector< Vector<double> >&) method.\n"
494 <<
"Number of columns must be equal to number of bounding neurons.\n";
496 throw std::logic_error(buffer.str());
547 if(index >= bounding_neurons_number)
549 std::ostringstream buffer;
551 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
552 <<
"void prune_bounding_neuron(const size_t&) method.\n"
553 <<
"Index of bounding neuron is equal or greater than number of bounding neurons.\n";
555 throw std::logic_error(buffer.str());
574 std::sort(random_vector.begin(), random_vector.end());
592 const size_t inputs_size = inputs.size();
596 if(inputs_size != bounding_neurons_number)
598 std::ostringstream buffer;
600 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
601 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
602 <<
"Size of inputs must be equal to number of bounding neurons.\n";
604 throw std::logic_error(buffer.str());
626 for(
size_t i = 0; i < bounding_neurons_number; i++)
630 derivatives[i] = 0.0;
634 derivatives[i] = 1.0;
649 std::ostringstream buffer;
655 for(
size_t i = 0; i < bounding_neurons_number; i++)
659 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
660 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
661 <<
"Output is equal to lower bound. The bounding function is not differentiable at this point.\n";
663 throw std::logic_error(buffer.str());
667 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
668 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
669 <<
"Output is equal to upper bound. The bounding function is not differentiable at this point.\n";
671 throw std::logic_error(buffer.str());
677 return(second_derivative);
695 const size_t derivatives_size = derivatives.size();
697 if(derivatives_size != bounding_neurons_number)
699 std::ostringstream buffer;
701 buffer <<
"OpenNN Exception: BoundingLayer class.\n"
702 <<
"Matrix<double> arrange_Jacobian(const Vector<double>&) method.\n"
703 <<
"Size of derivatives must be equal to number of bounding neurons.\n";
705 throw std::logic_error(buffer.str());
710 Matrix<double> Jacobian(bounding_neurons_number, bounding_neurons_number, 0.0);
728 for(
size_t i = 0; i < bounding_neurons_number; i++)
730 bounded_Hessian_form[i].
set(bounding_neurons_number, bounding_neurons_number, 0.0);
733 return(bounded_Hessian_form);
743 std::stringstream expression;
747 for(
size_t i = 0; i < bounding_neurons_number; i++)
749 expression << outputs_name[i] <<
" < " <<
lower_bounds[i] <<
" ? " <<
lower_bounds[i] <<
" : " << inputs_name[i] <<
"\n";
750 expression << outputs_name[i] <<
" > " <<
upper_bounds[i] <<
" ? " <<
upper_bounds[i] <<
" : " << inputs_name[i] <<
"\n";
753 return(expression.str());
763 std::ostringstream buffer;
765 buffer <<
"Bounding layer\n"
768 <<
"Display: " <<
display <<
"\n";
770 return(buffer.str());
781 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
783 std::ostringstream buffer;
785 tinyxml2::XMLElement* bounding_layer_element = document->NewElement(
"BoundingLayer");
787 document->InsertFirstChild(bounding_layer_element);
792 tinyxml2::XMLElement* element = document->NewElement(
"LowerBounds");
793 bounding_layer_element->LinkEndChild(element);
798 tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
799 element->LinkEndChild(text);
805 tinyxml2::XMLElement* element = document->NewElement(
"UpperBounds");
806 bounding_layer_element->LinkEndChild(element);
811 tinyxml2::XMLText* text = document->NewText(buffer.str().c_str());
812 element->LinkEndChild(text);
817 tinyxml2::XMLElement* display_element = document->NewElement(
"Display");
818 bounding_layer_element->LinkEndChild(display_element);
823 tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
824 display_element->LinkEndChild(display_text);
858 const tinyxml2::XMLElement* lower_bounds_element = document.FirstChildElement(
"LowerBounds");
860 if(lower_bounds_element)
862 const char* lower_bounds_text = lower_bounds_element->GetText();
864 if(lower_bounds_text)
867 new_lower_bounds.
parse(lower_bounds_text);
873 catch(
const std::logic_error& e)
875 std::cout << e.what() << std::endl;
883 const tinyxml2::XMLElement* upper_bounds_element = document.FirstChildElement(
"UpperBounds");
885 if(upper_bounds_element)
887 const char* upper_bounds_text = upper_bounds_element->GetText();
889 if(upper_bounds_text)
892 new_upper_bounds.
parse(upper_bounds_text);
898 catch(
const std::logic_error& e)
900 std::cout << e.what() << std::endl;
908 const tinyxml2::XMLElement* display_element = document.FirstChildElement(
"Display");
912 std::string new_display_string = display_element->GetText();
918 catch(
const std::logic_error& e)
920 std::cout << e.what() << std::endl;
void parse(const std::string &)
std::string to_string(void) const
Returns a string representation of the current bonding layer object.
Vector< Matrix< double > > arrange_Hessian_form(const Vector< double > &) const
tinyxml2::XMLDocument * to_XML(void) const
void randomize_uniform(const double &=-1.0, const double &=1.0)
void set_display(const bool &)
Vector< double > calculate_derivative(const Vector< double > &) const
const Vector< double > & get_upper_bounds(void) const
Returns the upper bounds values of all the bounding neurons in the layer.
Matrix< double > arrange_Jacobian(const Vector< double > &) const
bool display
Display messages to screen.
void set(void)
Sets the size of a vector to zero.
void from_XML(const tinyxml2::XMLDocument &)
bool is_empty(void) const
Returns true if the size of the layer is zero, and false otherwise.
bool operator==(const BoundingLayer &) const
size_t get_bounding_neurons_number(void) const
Returns the number of bounding neurons in the layer.
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
Returns a string with the expression of the lower and upper bounds functions.
void set_diagonal(const T &)
void set_lower_bounds(const Vector< double > &)
Vector< T > calculate_lower_upper_bounded(const T &, const T &) const
void set_bounds(const Vector< Vector< double > > &)
Vector< double > calculate_outputs(const Vector< double > &) const
void set_upper_bounds(const Vector< double > &)
void randomize_normal(const double &=0.0, const double &=1.0)
double get_lower_bound(const size_t &) const
Vector< double > lower_bounds
Lower bounds of output variables.
Vector< double > calculate_second_derivative(const Vector< double > &) const
Vector< Vector< double > * > get_bounds(void)
void initialize_random(void)
Initializes the lower and upper bounds of all the bounding neurons with random values.
void set_lower_bound(const size_t &, const double &)
Vector< double > upper_bounds
Upper bounds of output variables.
virtual ~BoundingLayer(void)
void prune_bounding_neuron(const size_t &)
const Vector< double > & get_lower_bounds(void) const
Returns the lower bounds values of all the bounding neurons in the layer.
double get_upper_bound(const size_t &) const
void set_upper_bound(const size_t &, const double &)
BoundingLayer & operator=(const BoundingLayer &)