OpenNN  2.2
Open Neural Networks Library
outputs.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* O U T P U T S C L A S S */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 // OpenNN includes
15 
16 #include "outputs.h"
17 
18 namespace OpenNN
19 {
20 
21 // DEFAULT CONSTRUCTOR
22 
25 
27 {
28  set();
29 }
30 
31 
32 // OUTPUTS NUMBER CONSTRUCTOR
33 
38 
39 Outputs::Outputs(const size_t& new_outputs_number)
40 {
41  set(new_outputs_number);
42 }
43 
44 
45 // XML CONSTRUCTOR
46 
50 
51 Outputs::Outputs(const tinyxml2::XMLDocument& outputs_document)
52 {
53  from_XML(outputs_document);
54 }
55 
56 
57 // COPY CONSTRUCTOR
58 
62 
63 Outputs::Outputs(const Outputs& other_outputs)
64 {
65  set(other_outputs);
66 }
67 
68 
69 // DESTRUCTOR
70 
72 
74 {
75 }
76 
77 
78 // ASSIGNMENT OPERATOR
79 
83 
84 Outputs& Outputs::operator = (const Outputs& other_outputs)
85 {
86  if(this != &other_outputs)
87  {
88  items = other_outputs.items;
89 
90  display = other_outputs.display;
91  }
92 
93  return(*this);
94 }
95 
96 
97 // METHODS
98 
99 
100 // EQUAL TO OPERATOR
101 
102 // bool operator == (const Outputs&) const method
103 
108 
109 bool Outputs::operator == (const Outputs& other_outputs) const
110 {
111  if(/*items == other_outputs.items
112  */ display == other_outputs.display)
113  {
114  return(true);
115  }
116  else
117  {
118  return(false);
119  }
120 }
121 
122 
123 // bool is_empty(void) const method
124 
126 
127 bool Outputs::is_empty(void) const
128 {
129  const size_t outputs_number = get_outputs_number();
130 
131  if(outputs_number == 0)
132  {
133  return(true);
134  }
135  else
136  {
137  return(false);
138  }
139 }
140 
141 
142 // Vector<std::string> arrange_names(void) const method
143 
146 
148 {
149  const size_t outputs_number = get_outputs_number();
150 
151  Vector<std::string> names(outputs_number);
152 
153  for(size_t i = 0; i < outputs_number; i++)
154  {
155  names[i] = items[i].name;
156  }
157 
158  return(names);
159 }
160 
161 
162 // const std::string& get_name(const size_t&) const method
163 
167 
168 const std::string& Outputs::get_name(const size_t& index) const
169 {
170  // Control sentence (if debug)
171 
172  #ifndef NDEBUG
173 
174  const size_t outputs_number = get_outputs_number();
175 
176  if(index >= outputs_number)
177  {
178  std::ostringstream buffer;
179 
180  buffer << "OpenNN Exception: Outputs class.\n"
181  << "const std::string get_name(const size_t&) const method.\n"
182  << "Output variable index must be less than number of outputs.\n";
183 
184  throw std::logic_error(buffer.str());
185  }
186 
187  #endif
188 
189  return(items[index].name);
190 }
191 
192 
193 // Vector<std::string> arrange_descriptions(void) const method
194 
197 
199 {
200  return(descriptions);
201 }
202 
203 
204 // const std::string& get_description(const size_t&) const method
205 
209 
210 const std::string& Outputs::get_description(const size_t& index) const
211 {
212  // Control sentence (if debug)
213 
214  #ifndef NDEBUG
215 
216  const size_t outputs_number = get_outputs_number();
217 
218  if(index >= outputs_number)
219  {
220  std::ostringstream buffer;
221 
222  buffer << "OpenNN Exception: Outputs class.\n"
223  << "const std::string& get_description(const size_t&) const method.\n"
224  << "Index of output variable must be less than number of outputs.\n";
225 
226  throw std::logic_error(buffer.str());
227  }
228 
229  #endif
230 
231  return(descriptions[index]);
232 }
233 
234 
235 // Vector<std::string> arrange_units(void) const method
236 
239 
241 {
242  return(units);
243 }
244 
245 
246 // const std::string& get_unit(const size_t&) const method
247 
251 
252 const std::string& Outputs::get_unit(const size_t& index) const
253 {
254  // Control sentence (if debug)
255 
256  #ifndef NDEBUG
257 
258  const size_t outputs_number = get_outputs_number();
259 
260  if(index >= outputs_number)
261  {
262  std::ostringstream buffer;
263 
264  buffer << "OpenNN Exception: Outputs class.\n"
265  << "const std::string get_unit(const size_t&) const method.\n"
266  << "Index of output variable must be less than number of outputs.\n";
267 
268  throw std::logic_error(buffer.str());
269  }
270 
271  #endif
272 
273  return(units[index]);
274 }
275 
276 
277 // Matrix<std::string> arrange_information(void) const method
278 
283 
285 {
286  const size_t outputs_number = get_outputs_number();
287 
288  Matrix<std::string> information(outputs_number, 3);
289 
290  for(size_t i = 0; i < outputs_number; i++)
291  {
292  information(i,0) = items[i].name;
293  information(i,1) = items[i].units;
294  information(i,2) = items[i].description;
295  }
296 
297  return(information);
298 }
299 
300 
301 // const bool& get_display(void) const method
302 
305 
306 const bool& Outputs::get_display(void) const
307 {
308  return(display);
309 }
310 
311 
312 // void set(void) method
313 
316 
317 void Outputs::set(void)
318 {
320 
321  set_default();
322 }
323 
324 
325 // void set(const size_t&) method
326 
330 
331 void Outputs::set(const size_t& new_outputs_number)
332 {
333  set_outputs_number(new_outputs_number);
334 
335  set_default();
336 }
337 
338 
339 // void set(const Outputs&) method
340 
343 
344 void Outputs::set(const Outputs& other_outputs)
345 {
346  items = other_outputs.items;
347 
348  display = other_outputs.display;
349 }
350 
351 
352 // void set(const Vector<Item>&) method
353 
356 
357 void Outputs::set(const Vector<Item>& new_items)
358 {
359  items = new_items;
360 }
361 
362 
363 // void set_outputs_number(const size_t&) method
364 
367 
368 void Outputs::set_outputs_number(const size_t& new_outputs_number)
369 {
370  items.set(new_outputs_number);
371 }
372 
373 
374 // void set_default(void) method
375 
377 
379 {
380  std::ostringstream buffer;
381 
382  const size_t outputs_number = get_outputs_number();
383 
384  for(size_t i = 0; i < outputs_number; i++)
385  {
386  buffer.str("");
387  buffer << "output_" << i+1;
388 
389  items[i].name = buffer.str();
390  items[i].units = "";
391  items[i].description = "";
392  }
393 
394  set_display(true);
395 }
396 
397 
398 // void set_names(const Vector<std::string>&) method
399 
403 
405 {
406  const size_t outputs_number = get_outputs_number();
407 
408  // Control sentence (if debug)
409 
410  #ifndef NDEBUG
411 
412  const size_t size = new_names.size();
413 
414  if(size != outputs_number)
415  {
416  std::ostringstream buffer;
417 
418  buffer << "OpenNN Exception: Outputs class.\n"
419  << "void set_names(const Vector<std::string>&) method.\n"
420  << "Size of name of outputs vector must be equal to number of output variables.\n";
421 
422  throw std::logic_error(buffer.str());
423  }
424 
425  #endif
426 
427  // Set name of output variables
428 
429  for(size_t i = 0; i < outputs_number; i++)
430  {
431  items[i].name = new_names[i];
432  }
433 }
434 
435 
436 // void set_name(const size_t&, const std::string&) method
437 
442 
443 void Outputs::set_name(const size_t& index, const std::string& new_name)
444 {
445  // Control sentence (if debug)
446 
447  #ifndef NDEBUG
448 
449  const size_t outputs_number = get_outputs_number();
450 
451  if(index >= outputs_number)
452  {
453  std::ostringstream buffer;
454 
455  buffer << "OpenNN Exception: Outputs class.\n"
456  << "void set_name(const size_t&, const std::string&) method.\n"
457  << "Index of output variable must be less than number of outputs.\n";
458 
459  throw std::logic_error(buffer.str());
460  }
461 
462  #endif
463 
464  // Set name of single output variable
465 
466  items[index].name = new_name;
467 }
468 
469 
470 // void set_units(const Vector<std::string>&) method
471 
475 
477 {
478  // Control sentence (if debug)
479 
480  #ifndef NDEBUG
481 
482  const size_t outputs_number = get_outputs_number();
483 
484  const size_t size = new_units.size();
485 
486  if(size != outputs_number)
487  {
488  std::ostringstream buffer;
489 
490  buffer << "OpenNN Exception: Outputs class.\n"
491  << "void set_units(const Vector<std::string>&) method.\n"
492  << "Size must be equal to number of output variables.\n";
493 
494  throw std::logic_error(buffer.str());
495  }
496 
497  #endif
498 
499  // Set units of output variables
500 
501  units = new_units;
502 }
503 
504 
505 // void set_units(const size_t&, const std::string&) method
506 
511 
512 void Outputs::set_unit(const size_t& index, const std::string& new_units)
513 {
514  const size_t outputs_number = get_outputs_number();
515 
516  // Control sentence (if debug)
517 
518  #ifndef NDEBUG
519 
520  if(index >= outputs_number)
521  {
522  std::ostringstream buffer;
523 
524  buffer << "OpenNN Exception: Outputs class.\n"
525  << "void set_units(const size_t&, const std::string&) method.\n"
526  << "Index of output variable must be less than number of outputs.\n";
527 
528  throw std::logic_error(buffer.str());
529  }
530 
531  #endif
532 
533  if(units.size() != outputs_number)
534  {
535  units.set(outputs_number);
536  }
537 
538  // Set units of single output variable
539 
540  units[index] = new_units;
541 }
542 
543 
544 // void set_descriptions(const Vector<std::string>&) method
545 
549 
550 void Outputs::set_descriptions(const Vector<std::string>& new_descriptions)
551 {
552  // Control sentence (if debug)
553 
554  #ifndef NDEBUG
555 
556  const size_t size = new_descriptions.size();
557 
558  const size_t outputs_number = get_outputs_number();
559 
560  if(size != outputs_number)
561  {
562  std::ostringstream buffer;
563 
564  buffer << "OpenNN Exception: Outputs class.\n"
565  << "void set_descriptions(const Vector<std::string>&) method.\n"
566  << "Size must be equal to number of outputs.\n";
567 
568  throw std::logic_error(buffer.str());
569  }
570 
571  #endif
572 
573  // Set description of output variables
574 
575  descriptions = new_descriptions;
576 }
577 
578 
579 // void set_description(const size_t&, const std::string&) method
580 
585 
586 void Outputs::set_description(const size_t& index, const std::string& new_description)
587 {
588  const size_t outputs_number = get_outputs_number();
589 
590  // Control sentence (if debug)
591 
592  #ifndef NDEBUG
593 
594  if(index >= outputs_number)
595  {
596  std::ostringstream buffer;
597 
598  buffer << "OpenNN Exception: Outputs class.\n"
599  << "void set_description(const size_t&, const std::string&) method.\n"
600  << "Index of output variable must be less than number of outputs.\n";
601 
602  throw std::logic_error(buffer.str());
603  }
604 
605  #endif
606 
607  if(descriptions.size() != outputs_number)
608  {
609  descriptions.set(outputs_number);
610  }
611 
612  // Set description of single output variable
613 
614  descriptions[index] = new_description;
615 }
616 
617 
618 // void set_information(const Matrix<std::string>&) method
619 
628 
629 void Outputs::set_information(const Matrix<std::string>& new_information)
630 {
631  const size_t outputs_number = get_outputs_number();
632 
633  // Set all information
634 
635  for(size_t i = 0; i < outputs_number; i++)
636  {
637  items[i].name = new_information(i,0);
638  items[i].units = new_information(i,1);
639  items[i].description = new_information(i,2);
640  }
641 }
642 
643 
644 // void set_display(const bool&) method
645 
650 
651 void Outputs::set_display(const bool& new_display)
652 {
653  display = new_display;
654 }
655 
656 
657 // void grow_output(void) method
658 
660 
662 {
663  const Item item;
664 
665  items.push_back(item);
666 }
667 
668 
669 // void prune_output(const size_t&) method
670 
673 
674 void Outputs::prune_output(const size_t& index)
675 {
676  // Control sentence (if debug)
677 
678  #ifndef NDEBUG
679 
680  const size_t outputs_number = get_outputs_number();
681 
682  if(index >= outputs_number)
683  {
684  std::ostringstream buffer;
685 
686  buffer << "OpenNN Exception: Outputs class.\n"
687  << "void prune_output(const size_t&) method.\n"
688  << "Index of output is equal or greater than number of outputs.\n";
689 
690  throw std::logic_error(buffer.str());
691  }
692 
693  #endif
694 
695  items.erase(items.begin()+index);
696 }
697 
698 
699 // Vector<std::string> write_default_outputs_name(void) const method
700 
707 
709 {
710  const size_t outputs_number = get_outputs_number();
711 
712  Vector<std::string> default_names(outputs_number);
713 
714  std::ostringstream buffer;
715 
716  for(size_t i = 0; i < outputs_number; i++)
717  {
718  buffer.str("");
719 
720  buffer << "output_" << i+1;
721 
722  default_names[i] = buffer.str();
723  }
724 
725  return(default_names);
726 }
727 
728 
729 // std::string to_string(void) const method
730 
732 
733 std::string Outputs::to_string(void) const
734 {
735  std::ostringstream buffer;
736 
737  buffer << "Outputs\n";
738 
739  const size_t outputs_number = get_outputs_number();
740 
741  for(size_t i = 0; i < outputs_number; i++)
742  {
743  buffer << "Item " << i+1 << ":\n"
744  << "Name:" << items[i].name << "\n"
745  << "Units:" << items[i].units << "\n"
746  << "Description:" << items[i].description << "\n";
747  }
748 
749  buffer << "Display:" << display << "\n";
750 
751  return(buffer.str());
752 }
753 
754 
755 // tinyxml2::XMLDocument* to_XML(void) const method
756 
759 
760 tinyxml2::XMLDocument* Outputs::to_XML(void) const
761 {
762  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
763 
764  const size_t outputs_number = get_outputs_number();
765 
766  std::ostringstream buffer;
767 
768  tinyxml2::XMLElement* outputs_element = document->NewElement("Outputs");
769 
770  document->InsertFirstChild(outputs_element);
771 
772  tinyxml2::XMLElement* element = NULL;
773  tinyxml2::XMLText* text = NULL;
774 
775  // Outputs number
776  {
777  element = document->NewElement("OutputsNumber");
778  outputs_element->LinkEndChild(element);
779 
780  buffer.str("");
781  buffer << outputs_number;
782 
783  text = document->NewText(buffer.str().c_str());
784  element->LinkEndChild(text);
785  }
786 
787  for(size_t i = 0; i < outputs_number; i++)
788  {
789  element = document->NewElement("Item");
790  element->SetAttribute("Index", (unsigned)i+1);
791  outputs_element->LinkEndChild(element);
792 
793  // Name
794 
795  tinyxml2::XMLElement* name_element = document->NewElement("Name");
796  element->LinkEndChild(name_element);
797 
798  tinyxml2::XMLText* name_text = document->NewText(items[i].name.c_str());
799  name_element->LinkEndChild(name_text);
800 
801  // Units
802 
803  tinyxml2::XMLElement* units_element = document->NewElement("Units");
804  element->LinkEndChild(units_element);
805 
806  tinyxml2::XMLText* units_text = document->NewText(items[i].units.c_str());
807  units_element->LinkEndChild(units_text);
808 
809  // Description
810 
811  tinyxml2::XMLElement* description_element = document->NewElement("Description");
812  element->LinkEndChild(description_element);
813 
814  tinyxml2::XMLText* descriptionText = document->NewText(items[i].description.c_str());
815  description_element->LinkEndChild(descriptionText);
816  }
817 
818  // Display
819  {
820  tinyxml2::XMLElement* display_element = document->NewElement("Display");
821  outputs_element->LinkEndChild(display_element);
822 
823  buffer.str("");
824  buffer << display;
825 
826  tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
827  display_element->LinkEndChild(display_text);
828  }
829 
830  return(document);
831 }
832 
833 
834 // void from_XML(const tinyxml2::XMLDocument&) method
835 
838 
839 void Outputs::from_XML(const tinyxml2::XMLDocument& document)
840 {
841  std::ostringstream buffer;
842 
843  const tinyxml2::XMLElement* outputs_element = document.FirstChildElement("Outputs");
844 
845  if(!outputs_element)
846  {
847  buffer << "OpenNN Exception: Outputs class.\n"
848  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
849  << "Outputs element is NULL.\n";
850 
851  throw std::logic_error(buffer.str());
852  }
853 
854  // Outputs number
855 
856  const tinyxml2::XMLElement* outputs_number_element = outputs_element->FirstChildElement("OutputsNumber");
857 
858  if(!outputs_number_element)
859  {
860  buffer << "OpenNN Exception: Outputs class.\n"
861  << "void from_XML(const tinyxml2::XMLDocument&) method.\n"
862  << "Outputs number element is NULL.\n";
863 
864  throw std::logic_error(buffer.str());
865  }
866 
867  const size_t outputs_number = atoi(outputs_number_element->GetText());
868 
869  set(outputs_number);
870 
871  unsigned index = 0; // size_t does not work
872 
873  const tinyxml2::XMLElement* start_element = outputs_number_element;
874 
875  for(size_t i = 0; i < outputs_number; i++)
876  {
877  const tinyxml2::XMLElement* item_element = start_element->NextSiblingElement("Item");
878  start_element = item_element;
879 
880  if(!item_element)
881  {
882  buffer << "OpenNN Exception: Outputs class.\n"
883  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
884  << "Item " << i+1 << " is NULL.\n";
885 
886  throw std::logic_error(buffer.str());
887  }
888 
889  item_element->QueryUnsignedAttribute("Index", &index);
890 
891  if(index != i+1)
892  {
893  buffer << "OpenNN Exception: Outputs class.\n"
894  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
895  << "Index " << index << " is not correct.\n";
896 
897  throw std::logic_error(buffer.str());
898  }
899 
900  // Name
901 
902  const tinyxml2::XMLElement* name_element = item_element->FirstChildElement("Name");
903 
904  if(!name_element)
905  {
906  buffer << "OpenNN Exception: Outputs class.\n"
907  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
908  << "Pointer to name element is NULL.\n";
909 
910  throw std::logic_error(buffer.str());
911  }
912 
913  if(name_element->GetText())
914  {
915  items[index-1].name = name_element->GetText();
916  }
917 
918  // Units
919 
920  const tinyxml2::XMLElement* units_element = item_element->FirstChildElement("Units");
921 
922  if(!units_element)
923  {
924  buffer << "OpenNN Exception: Outputs class.\n"
925  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
926  << "Pointer to units element is NULL.\n";
927 
928  throw std::logic_error(buffer.str());
929  }
930 
931  if(units_element->GetText())
932  {
933  items[index-1].units = units_element->GetText();
934  }
935 
936  // Description
937 
938  const tinyxml2::XMLElement* description_element = item_element->FirstChildElement("Description");
939 
940  if(!description_element)
941  {
942  buffer << "OpenNN Exception: Outputs class.\n"
943  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
944  << "Pointer to description element is NULL.\n";
945 
946  throw std::logic_error(buffer.str());
947  }
948 
949  if(description_element->GetText())
950  {
951  items[index-1].description = description_element->GetText();
952  }
953  }
954 }
955 
956 }
957 
958 // OpenNN: Open Neural Networks Library.
959 // Copyright (c) 2005-2015 Roberto Lopez.
960 //
961 // This library is free software; you can redistribute it and/or
962 // modify it under the terms of the GNU Lesser General Public
963 // License as published by the Free Software Foundation; either
964 // version 2.1 of the License, or any later version.
965 //
966 // This library is distributed in the hope that it will be useful,
967 // but WITHOUT ANY WARRANTY; without even the implied warranty of
968 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
969 // Lesser General Public License for more details.
970 
971 // You should have received a copy of the GNU Lesser General Public
972 // License along with this library; if not, write to the Free Software
973 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Vector< std::string > arrange_names(void) const
Definition: outputs.cpp:147
const bool & get_display(void) const
Definition: outputs.cpp:306
void grow_output(void)
Appends a new item to the outputs.
Definition: outputs.cpp:661
Vector< std::string > arrange_descriptions(void) const
Definition: outputs.cpp:198
void set_display(const bool &)
Definition: outputs.cpp:651
void set_name(const size_t &, const std::string &)
Definition: outputs.cpp:443
void set_unit(const size_t &, const std::string &)
Definition: outputs.cpp:512
std::string to_string(void) const
Returns a string representation of the current outputs object.
Definition: outputs.cpp:733
void set(void)
Sets the size of a vector to zero.
Definition: vector.h:656
Vector< std::string > arrange_units(void) const
Definition: outputs.cpp:240
void set_descriptions(const Vector< std::string > &)
Definition: outputs.cpp:550
Outputs & operator=(const Outputs &)
Definition: outputs.cpp:84
bool display
Display messages to screen.
Definition: outputs.h:194
void set_information(const Matrix< std::string > &)
Definition: outputs.cpp:629
virtual tinyxml2::XMLDocument * to_XML(void) const
Definition: outputs.cpp:760
const std::string & get_description(const size_t &) const
Definition: outputs.cpp:210
virtual void from_XML(const tinyxml2::XMLDocument &)
Definition: outputs.cpp:839
bool operator==(const Outputs &) const
Definition: outputs.cpp:109
bool is_empty(void) const
Returns true if both the number of outputs is zero, and false otherwise.
Definition: outputs.cpp:127
virtual void set_default(void)
Sets the members of this object to their default values.
Definition: outputs.cpp:378
Vector< std::string > write_default_names(void) const
Definition: outputs.cpp:708
Vector< std::string > units
Units of output variables.
Definition: outputs.h:186
void set(void)
Definition: outputs.cpp:317
Vector< std::string > descriptions
Description of output variables.
Definition: outputs.h:190
const std::string & get_unit(const size_t &) const
Definition: outputs.cpp:252
void set_description(const size_t &, const std::string &)
Definition: outputs.cpp:586
void set_outputs_number(const size_t &)
Definition: outputs.cpp:368
Matrix< std::string > arrange_information(void) const
Definition: outputs.cpp:284
void prune_output(const size_t &)
Definition: outputs.cpp:674
Vector< Item > items
Name of output variables.
Definition: outputs.h:182
void set_units(const Vector< std::string > &)
Definition: outputs.cpp:476
void set_names(const Vector< std::string > &)
Definition: outputs.cpp:404
virtual ~Outputs(void)
Destructor.
Definition: outputs.cpp:73
const std::string & get_name(const size_t &) const
Definition: outputs.cpp:168
size_t get_outputs_number(void) const
Returns the number of outputs neurons in the neural network.
Definition: outputs.h:107