OpenNN  2.2
Open Neural Networks Library
variables.cpp
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* V A R I A B L E 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 "variables.h"
17 
18 #include<limits>
19 #include<climits>
20 
21 
22 namespace OpenNN
23 {
24 
25 // DEFAULT CONSTRUCTOR
26 
30 
32 {
33  set();
34 }
35 
36 
37 // VARIABLES NUMBER CONSTRUCTOR
38 
44 
45 Variables::Variables(const size_t& new_variables_number)
46 {
47  set(new_variables_number);
48 }
49 
50 
51 // INPUT AND TARGET VARIABLES NUMBERS CONSTRUCTOR
52 
58 
59 Variables::Variables(const size_t& new_inputs_number, const size_t& new_targets_number)
60 {
61  set(new_inputs_number, new_targets_number);
62 }
63 
64 
65 // XML CONSTRUCTOR
66 
70 
71 Variables::Variables(const tinyxml2::XMLDocument& variables_document)
72 {
73  set(variables_document);
74 }
75 
76 
77 // COPY CONSTRUCTOR
78 
82 
83 Variables::Variables(const Variables& other_variables)
84 {
85  // Items
86 
87  items = other_variables.items;
88 
89  // Utilities
90 
91  display = other_variables.display;
92 }
93 
94 
95 // DESTRUCTOR
96 
98 
100 {
101 }
102 
103 
104 // ASSIGNMENT OPERATOR
105 
109 
110 Variables& Variables::operator=(const Variables& other_variables)
111 {
112  if(this != &other_variables)
113  {
114  // Items
115 
116  items = other_variables.items;
117 
118  // Utilities
119 
120  display = other_variables.display;
121  }
122 
123  return(*this);
124 }
125 
126 
127 // EQUAL TO OPERATOR
128 
129 // bool operator == (const Variables&) const method
130 
135 
136 bool Variables::operator == (const Variables& other_variables) const
137 {
138  if(/*items == other_variables.items
139  &&*/ display == other_variables.display)
140  {
141  return(true);
142  }
143  else
144  {
145  return(false);
146  }
147 }
148 
149 
150 // METHODS
151 
152 // const Vector<Item>& get_items(void) const method
153 
155 
157 {
158  return(items);
159 }
160 
161 
162 // const Item& get_item(const size_t&) const method
163 
166 
167 const Variables::Item& Variables::get_item(const size_t& i) const
168 {
169  // Control sentence (if debug)
170 
171  #ifndef NDEBUG
172 
173  const size_t variables_number = get_variables_number();
174 
175  if(i >= variables_number)
176  {
177  std::ostringstream buffer;
178 
179  buffer << "OpenNN Exception: Variables class.\n"
180  << "const Item& get_item(const size_t&) method.\n"
181  << "Index of variable must be less than number of variables.\n";
182 
183  throw std::logic_error(buffer.str());
184  }
185 
186  #endif
187 
188  return(items[i]);
189 }
190 
191 
192 // bool empty(void) const method
193 
195 
196 bool Variables::empty(void) const
197 {
198  if(items.empty())
199  {
200  return(true);
201  }
202  else
203  {
204  return(false);
205  }
206 }
207 
208 
209 // size_t count_used_variables_number(void) const method
210 
212 
214 {
215  const size_t variables_number = get_variables_number();
216 
217  const size_t unused_variables_number = count_unused_variables_number();
218 
219  return(variables_number - unused_variables_number);
220 }
221 
222 
223 // size_t count_unused_variables_number(void) const method
224 
226 
228 {
229  const size_t variables_number = get_variables_number();
230 
231  size_t count = 0;
232 
233  for(size_t i = 0; i < variables_number; i++)
234  {
235  if(items[i].use == Unused)
236  {
237  count++;
238  }
239  }
240 
241  return(count);
242 }
243 
244 
245 // size_t count_inputs_number(void) const method
246 
248 
250 {
251  const size_t variables_number = get_variables_number();
252 
253  size_t count = 0;
254 
255  for(size_t i = 0; i < variables_number; i++)
256  {
257  if(items[i].use == Input)
258  {
259  count++;
260  }
261  }
262 
263  return(count);
264 }
265 
266 
267 // size_t count_targets_number(void) const method
268 
270 
272 {
273  const size_t variables_number = get_variables_number();
274 
275  size_t count = 0;
276 
277  for(size_t i = 0; i < variables_number; i++)
278  {
279  if(items[i].use == Target)
280  {
281  count++;
282  }
283  }
284 
285  return(count);
286 }
287 
288 
289 // Vector<size_t> count_uses(void) const method
290 
294 
296 {
297  Vector<size_t> count(3, 0);
298 
299  const size_t variables_number = get_variables_number();
300 
301  for(size_t i = 0; i < variables_number; i++)
302  {
303  if(items[i].use == Input)
304  {
305  count[0]++;
306  }
307  else if(items[i].use == Target)
308  {
309  count[1]++;
310  }
311  else
312  {
313  count[2]++;
314  }
315  }
316 
317  return(count);
318 }
319 
320 
321 // Vector<Use> arrange_uses(void) const method
322 
325 
327 {
328  const size_t variables_number = get_variables_number();
329 
330  Vector<Variables::Use> uses(variables_number);
331 
332  for(size_t i = 0; i < variables_number; i++)
333  {
334  uses[i] = items[i].use;
335  }
336 
337  return(uses);
338 }
339 
340 
341 // Vector<std::string> write_uses(void) const method
342 
346 
348 {
349  const size_t variables_number = get_variables_number();
350 
351  Vector<std::string> uses_string(variables_number);
352 
353  for(size_t i = 0; i < variables_number; i++)
354  {
355  if(items[i].use == Unused)
356  {
357  uses_string[i] = "Unused";
358  }
359  else if(items[i].use == Input)
360  {
361  uses_string[i] = "Input";
362  }
363  else if(items[i].use == Target)
364  {
365  uses_string[i] = "Target";
366  }
367  else
368  {
369  std::ostringstream buffer;
370 
371  buffer << "OpenNN Exception Variables class.\n"
372  << "Vector<std::string> write_uses(void) const method.\n"
373  << "Unknown use.\n";
374 
375  throw std::logic_error(buffer.str());
376  }
377  }
378 
379  return(uses_string);
380 }
381 
382 
383 // const Use& get_use(const size_t&) const method
384 
387 
388 const Variables::Use& Variables::get_use(const size_t& i) const
389 {
390  return(items[i].use);
391 }
392 
393 
394 // std::string write_use(const size_t&) const method
395 
398 
399 std::string Variables::write_use(const size_t& i) const
400 {
401  if(items[i].use == Unused)
402  {
403  return("Unused");
404  }
405  else if(items[i].use == Input)
406  {
407  return("Input");
408  }
409  else if(items[i].use == Target)
410  {
411  return("Target");
412  }
413  else
414  {
415  std::ostringstream buffer;
416 
417  buffer << "OpenNN Exception Variables class.\n"
418  << "std::string write_use(const size_t&) const method.\n"
419  << "Unknown use.\n";
420 
421  throw std::logic_error(buffer.str());
422  }
423 }
424 
425 
426 // bool is_used(const size_t&) const method
427 
430 
431 bool Variables::is_used(const size_t& index) const
432 {
433  // Control sentence (if debug)
434 
435  #ifndef NDEBUG
436 
437  const size_t variables_number = get_variables_number();
438 
439  if(index >= variables_number)
440  {
441  std::ostringstream buffer;
442 
443  buffer << "OpenNN Exception: Variables class.\n"
444  << "bool is_used(const size_t&) const method.\n"
445  << "Index of variable must be less than number of variables.\n";
446 
447  throw std::logic_error(buffer.str());
448  }
449 
450  #endif
451 
452  if(items[index].use == Variables::Unused)
453  {
454  return(false);
455  }
456  else
457  {
458  return(true);
459  }
460 }
461 
462 
463 
464 // Matrix<std::string> arrange_information(void) const method
465 
470 
472 {
473  const size_t variables_number = get_variables_number();
474 
475  Matrix<std::string> information(variables_number, 4);
476 
477  for(size_t i = 0; i < variables_number; i++)
478  {
479  information(i,0) = items[i].name;
480  information(i,1) = items[i].units;
481  information(i,2) = items[i].description;
482  information(i,3) = write_use(i);
483  }
484 
485  return(information);
486 }
487 
488 
489 // Vector<size_t> arrange_inputs_indices(void) const method
490 
492 
494 {
495  const size_t variables_number = get_variables_number();
496  const size_t inputs_number = count_inputs_number();
497 
498  Vector<size_t> inputs_indices(inputs_number);
499 
500  size_t index = 0;
501 
502  for(size_t i = 0; i < variables_number; i++)
503  {
504  if(items[i].use == Input)
505  {
506  inputs_indices[index] = i;
507  index++;
508  }
509  }
510 
511  return(inputs_indices);
512 }
513 
514 
515 // Vector<size_t> arrange_targets_indices(void) const method
516 
518 
520 {
521  const size_t variables_number = get_variables_number();
522  const size_t targets_number = count_targets_number();
523 
524  Vector<size_t> targets_indices(targets_number);
525 
526  size_t index = 0;
527 
528  for(size_t i = 0; i < variables_number; i++)
529  {
530  if(items[i].use == Target)
531  {
532  targets_indices[index] = i;
533  index++;
534  }
535  }
536 
537  return(targets_indices);
538 }
539 
540 
541 // Vector<std::string> arrange_names(void) const method
542 
544 
546 {
547  const size_t variables_number = get_variables_number();
548 
549  Vector<std::string> names(variables_number);
550 
551  for(size_t i = 0; i < variables_number; i++)
552  {
553  names[i] = items[i].name;
554  }
555 
556  return(names);
557 }
558 
559 
560 // std::string& get_name(size_t) method
561 
564 
565 const std::string& Variables::get_name(const size_t& i) const
566 {
567  // Control sentence (if debug)
568 
569  #ifndef NDEBUG
570 
571  const size_t variables_number = get_variables_number();
572 
573  if(i >= variables_number)
574  {
575  std::ostringstream buffer;
576 
577  buffer << "OpenNN Exception: Variables class.\n"
578  << "std::string& get_name(size_t) method.\n"
579  << "Index of variable must be less than number of variables.\n";
580 
581  throw std::logic_error(buffer.str());
582  }
583 
584  #endif
585 
586  return(items[i].name);
587 }
588 
589 
590 // bool has_names(void) const method
591 
593 
594 bool Variables::has_names(void) const
595 {
596  const size_t variables_number = get_variables_number();
597 
598  for(size_t i = 0; i < variables_number; i++)
599  {
600  if(items[i].name.empty())
601  {
602  return(false);
603  }
604  }
605 
606  return(true);
607 }
608 
609 
610 
611 // Vector<std::string> arrange_inputs_name(void) const method
612 
614 
616 {
617  const size_t inputs_number = count_inputs_number();
618 
619  const Vector<size_t> inputs_indices = arrange_inputs_indices();
620 
621  Vector<std::string> inputs_name(inputs_number);
622 
623  size_t index;
624 
625  for(size_t i = 0; i < inputs_number; i++)
626  {
627  index = inputs_indices[i];
628 
629  inputs_name[i] = items[index].name;
630  }
631 
632  return(inputs_name);
633 }
634 
635 
636 // Vector<std::string> arrange_targets_name(void) const method
637 
639 
641 {
642  const size_t targets_number = count_targets_number();
643 
644  const Vector<size_t> targets_indices = arrange_targets_indices();
645 
646  Vector<std::string> targets_name(targets_number);
647 
648  size_t index;
649 
650  for(size_t i = 0; i < targets_number; i++)
651  {
652  index = targets_indices[i];
653 
654  targets_name[i] = items[index].name;
655  }
656 
657  return(targets_name);
658 }
659 
660 
661 // Vector<std::string> arrange_units(void) const method
662 
664 
666 {
667  const size_t variables_number = get_variables_number();
668 
669  Vector<std::string> units(variables_number);
670 
671  for(size_t i = 0; i < variables_number; i++)
672  {
673  units[i] = items[i].units;
674  }
675 
676  return(units);
677 }
678 
679 
680 // const std::string& get_units(const size_t&) const method
681 
684 
685 const std::string& Variables::get_unit(const size_t& i) const
686 {
687  // Control sentence (if debug)
688 
689  #ifndef NDEBUG
690 
691  const size_t variables_number = get_variables_number();
692 
693  if(i >= variables_number)
694  {
695  std::ostringstream buffer;
696 
697  buffer << "OpenNN Exception: Variables class.\n"
698  << "const std::string& get_units(const size_t&) const method.\n"
699  << "Index of variable must be less than number of variables.\n";
700 
701  throw std::logic_error(buffer.str());
702  }
703 
704  #endif
705 
706  return(items[i].units);
707 }
708 
709 
710 // Vector<std::string> arrange_inputs_units(void) const method
711 
713 
715 {
716  const Vector<size_t> inputs_indices = arrange_inputs_indices();
717 
718  const size_t inputs_number = count_inputs_number();
719 
720  Vector<std::string> inputs_units(inputs_number);
721 
722  size_t index;
723 
724  for(size_t i = 0; i < inputs_number; i++)
725  {
726  index = inputs_indices[i];
727 
728  inputs_units[i] = items[index].units;
729  }
730 
731  return(inputs_units);
732 }
733 
734 
735 // Vector<std::string> arrange_targets_units(void) const method
736 
738 
740 {
741  const Vector<size_t> targets_indices = arrange_targets_indices();
742 
743  const size_t targets_number = count_targets_number();
744 
745  Vector<std::string> targets_units(targets_number);
746 
747  size_t index;
748 
749  for(size_t i = 0; i < targets_number; i++)
750  {
751  index = targets_indices[i];
752 
753  targets_units[i] = items[index].units;
754  }
755 
756  return(targets_units);
757 }
758 
759 
760 // Vector<std::string> arrange_descriptions(void) const method
761 
763 
765 {
766  const size_t variables_number = get_variables_number();
767 
768  Vector<std::string> descriptions(variables_number);
769 
770  for(size_t i = 0; i < variables_number; i++)
771  {
772  descriptions[i] = items[i].description;
773  }
774 
775  return(descriptions);
776 }
777 
778 
779 // const std::string& get_description(const size_t&) const method
780 
783 
784 const std::string& Variables::get_description(const size_t& i) const
785 {
786  // Control sentence (if debug)
787 
788  #ifndef NDEBUG
789 
790  const size_t variables_number = get_variables_number();
791 
792  if(i >= variables_number)
793  {
794  std::ostringstream buffer;
795 
796  buffer << "OpenNN Exception: Variables class.\n"
797  << "const std::string& get_description(const size_t&) const method.\n"
798  << "Index of variable must be less than number of variables.\n";
799 
800  throw std::logic_error(buffer.str());
801  }
802 
803  #endif
804 
805  return(items[i].description);
806 }
807 
808 
809 // Vector<std::string> arrange_inputs_description(void) const method
810 
812 
814 {
815  const size_t inputs_number = count_inputs_number();
816 
817  const Vector<size_t> inputs_indices = arrange_inputs_indices();
818 
819  Vector<std::string> inputs_description(inputs_number);
820 
821  size_t index;
822 
823  for(size_t i = 0; i < inputs_number; i++)
824  {
825  index = inputs_indices[i];
826 
827  inputs_description[i] = items[index].description;
828  }
829 
830  return(inputs_description);
831 }
832 
833 
834 // Vector<std::string> arrange_targets_descriptions(void) const method
835 
837 
839 {
840  const Vector<size_t> targets_indices = arrange_targets_indices();
841 
842  const size_t targets_number = count_targets_number();
843 
844  Vector<std::string> target_descriptions(targets_number);
845 
846  size_t index;
847 
848  for(size_t i = 0; i < targets_number; i++)
849  {
850  index = targets_indices[i];
851 
852  target_descriptions[i] = items[index].description;
853  }
854 
855  return(target_descriptions);
856 }
857 
858 
859 // Matrix<std::string> arrange_inputs_information(void) const method
860 
863 
865 {
866  const size_t inputs_number = count_inputs_number();
867 
868  const Vector<size_t> inputs_indices = arrange_inputs_indices();
869 
870  size_t input_index;
871 
872  Matrix<std::string> inputs_information(inputs_number, 3);
873 
874  for(size_t i = 0; i < inputs_number; i++)
875  {
876  input_index = inputs_indices[i];
877 
878  inputs_information(i,0) = items[input_index].name;
879  inputs_information(i,1) = items[input_index].units;
880  inputs_information(i,2) = items[input_index].description;
881  }
882 
883  return(inputs_information);
884 }
885 
886 
887 // Matrix<std::string> arrange_targets_information(void) const method
888 
891 
893 {
894  const size_t targets_number = count_targets_number();
895 
896  const Vector<size_t> targets_indices = arrange_targets_indices();
897 
898  size_t target_index;
899 
900  Matrix<std::string> targets_information(targets_number, 3);
901 
902  for(size_t i = 0; i < targets_number; i++)
903  {
904  target_index = targets_indices[i];
905 
906  targets_information(i,0) = items[target_index].name;
907  targets_information(i,1) = items[target_index].units;
908  targets_information(i,2) = items[target_index].description;
909  }
910 
911  return(targets_information);
912 }
913 
914 
915 // const bool& get_display(void) const method
916 
919 
920 const bool& Variables::get_display(void) const
921 {
922  return(display);
923 }
924 
925 
926 // void set(void) method
927 
929 
930 void Variables::set(void)
931 {
933 
934  set_default();
935 }
936 
937 
938 // void set(const size_t&) method
939 
943 
944 void Variables::set(const size_t& new_variables_number)
945 {
946  set_variables_number(new_variables_number);
947 
948  set_default();
949 }
950 
951 
952 // void set(const size_t&, const size_t&) method
953 
959 
960 void Variables::set(const size_t& inputs_number, const size_t& targets_number)
961 {
962  const size_t variables_number = inputs_number + targets_number;
963 
964  set_variables_number(variables_number);
965 
966  for(size_t i = 0; i < inputs_number; i++)
967  {
968  items[i].use = Input;
969  }
970 
971  for(size_t i = inputs_number; i < variables_number; i++)
972  {
973  items[i].use = Target;
974  }
975 
976  set_default();
977 }
978 
979 
980 // void set(const tinyxml2::XMLElement*) method
981 
984 
985 void Variables::set(const tinyxml2::XMLDocument& variables_document)
986 {
987  set_default();
988 
989  from_XML(variables_document);
990 }
991 
992 
993 // void set_default(void) method
994 
999 
1001 {
1002  display = true;
1003 }
1004 
1005 
1006 // void set_items(const Vector<Item>&) method
1007 
1010 
1011 void Variables::set_items(const Vector<Item>& new_items)
1012 {
1013  items = new_items;
1014 }
1015 
1016 
1017 // void set_uses(const Vector<Use>&) method
1018 
1022 
1024 {
1025  const size_t variables_number = get_variables_number();
1026 
1027  for(size_t i = 0; i < variables_number; i++)
1028  {
1029  items[i].use = new_uses[i];
1030  }
1031 }
1032 
1033 
1034 // void set_uses(const Vector<std::string>&) method
1035 
1039 
1041 {
1042  const size_t new_variables_number = new_uses.size();
1043 
1044  for(size_t i = 0; i < new_variables_number; i++)
1045  {
1046  if(new_uses[i] == "Input")
1047  {
1048  items[i].use = Input;
1049  }
1050  else if(new_uses[i] == "Target")
1051  {
1052  items[i].use = Target;
1053  }
1054  else if(new_uses[i] == "Unused")
1055  {
1056  items[i].use = Unused;
1057  }
1058  else
1059  {
1060  std::ostringstream buffer;
1061 
1062  buffer << "OpenNN Exception Variables class.\n"
1063  << "void set_uses(const Vector<std::string>&) method.\n"
1064  << "Unknown use: " << new_uses[i] << "\n";
1065 
1066  throw std::logic_error(buffer.str());
1067  }
1068  }
1069 }
1070 
1071 
1072 // void set_use(const size_t&, const Use&) method
1073 
1077 
1078 void Variables::set_use(const size_t& i, const Use& new_use)
1079 {
1080  // Control sentence (if debug)
1081 
1082  #ifndef NDEBUG
1083 
1084  const size_t variables_number = get_variables_number();
1085 
1086  if(i >= variables_number)
1087  {
1088  std::ostringstream buffer;
1089 
1090  buffer << "OpenNN Exception Variables class.\n"
1091  << "void set_use(const size_t&, const Use&) method.\n"
1092  << "Index of variable must be less than number of variables.\n";
1093 
1094  throw std::logic_error(buffer.str());
1095  }
1096 
1097  #endif
1098 
1099  items[i].use = new_use;
1100 }
1101 
1102 
1103 // void set_use(const size_t&, const std::string&) method
1104 
1109 
1110 void Variables::set_use(const size_t& i, const std::string& new_use)
1111 {
1112  // Control sentence (if debug)
1113 
1114  #ifndef NDEBUG
1115 
1116  const size_t variables_number = get_variables_number();
1117 
1118  if(i >= variables_number)
1119  {
1120  std::ostringstream buffer;
1121 
1122  buffer << "OpenNN Exception Variables class.\n"
1123  << "void set_use(const size_t&, const std::string&) method.\n"
1124  << "Index of variable must be less than number of variables.\n";
1125 
1126  throw std::logic_error(buffer.str());
1127  }
1128 
1129  #endif
1130 
1131  if(new_use == "Unused")
1132  {
1133  items[i].use = Unused;
1134  }
1135  else if(new_use == "Input")
1136  {
1137  items[i].use = Input;
1138  }
1139  else if(new_use == "Target")
1140  {
1141  items[i].use = Target;
1142  }
1143  else
1144  {
1145  std::ostringstream buffer;
1146 
1147  buffer << "OpenNN Exception Variables class.\n"
1148  << "void set_use(const std::string&) method.\n"
1149  << "Unknown use.\n";
1150 
1151  throw std::logic_error(buffer.str());
1152  }
1153 }
1154 
1155 
1156 // void set_input(void) method
1157 
1159 
1161 {
1162  const size_t variables_number = get_variables_number();
1163 
1164  for(size_t i = 0; i < variables_number; i++)
1165  {
1166  items[i].use = Input;
1167  }
1168 }
1169 
1170 
1171 // void set_target(void) method
1172 
1174 
1176 {
1177  const size_t variables_number = get_variables_number();
1178 
1179  for(size_t i = 0; i < variables_number; i++)
1180  {
1181  items[i].use = Target;
1182  }
1183 }
1184 
1185 
1186 // void set_default_uses(void) method
1187 
1193 
1195 {
1196  const size_t variables_number = get_variables_number();
1197 
1198  if(variables_number == 0)
1199  {
1200  return;
1201  }
1202  else if(variables_number == 1)
1203  {
1204  items[0].use = Unused;
1205  }
1206  else
1207  {
1208  set_input();
1209 
1210  items[variables_number-1].use = Target;
1211  }
1212 }
1213 
1214 
1215 // void set_names(const Vector<std::string>&) method
1216 
1220 
1222 {
1223  const size_t variables_number = get_variables_number();
1224 
1225  // Control sentence (if debug)
1226 
1227  #ifndef NDEBUG
1228 
1229  const size_t size = new_names.size();
1230 
1231  if(size != variables_number)
1232  {
1233  std::ostringstream buffer;
1234 
1235  buffer << "OpenNN Exception: Variables class.\n"
1236  << "void set_names(const Vector<std::string>&) method.\n"
1237  << "Size must be equal to number of variables.\n";
1238 
1239  throw std::logic_error(buffer.str());
1240  }
1241 
1242  #endif
1243 
1244  for(size_t i = 0; i < variables_number; i++)
1245  {
1246  items[i].name = new_names[i];
1247  }
1248 }
1249 
1250 
1251 // void set_name(const size_t&, const std::string&) method
1252 
1257 
1258 void Variables::set_name(const size_t& i, const std::string& new_name)
1259 {
1260  // Control sentence (if debug)
1261 
1262  #ifndef NDEBUG
1263 
1264  const size_t variables_number = get_variables_number();
1265 
1266  if(i >= variables_number)
1267  {
1268  std::ostringstream buffer;
1269 
1270  buffer << "OpenNN Exception: Variables class.\n"
1271  << "void set_name(const size_t&, const std::string&) method.\n"
1272  << "Index of variable must be less than number of variables.\n";
1273 
1274  throw std::logic_error(buffer.str());
1275  }
1276 
1277  #endif
1278 
1279  // Set name of single variable
1280 
1281  items[i].name = new_name;
1282 }
1283 
1284 
1285 // void set_units(const Vector<std::string>&) method
1286 
1290 
1292 {
1293  const size_t variables_number = get_variables_number();
1294 
1295  // Control sentence (if debug)
1296 
1297  #ifndef NDEBUG
1298 
1299  const size_t size = new_units.size();
1300 
1301  if(size != variables_number)
1302  {
1303  std::ostringstream buffer;
1304 
1305  buffer << "OpenNN Exception: Variables class.\n"
1306  << "void set_units(const Vector<std::string>&) method.\n"
1307  << "Size must be equal to number of variables.\n";
1308 
1309  throw std::logic_error(buffer.str());
1310  }
1311 
1312  #endif
1313 
1314  for(size_t i = 0; i < variables_number; i++)
1315  {
1316  items[i].units = new_units[i];
1317  }
1318 }
1319 
1320 
1321 // void set_units(const size_t&, const std::string&) method
1322 
1327 
1328 void Variables::set_units(const size_t& i, const std::string& new_units)
1329 {
1330  // Control sentence (if debug)
1331 
1332  #ifndef NDEBUG
1333 
1334  const size_t variables_number = get_variables_number();
1335 
1336  if(i >= variables_number)
1337  {
1338  std::ostringstream buffer;
1339 
1340  buffer << "OpenNN Exception: Variables class.\n"
1341  << "void set_units(const size_t&, const std::string&) method.\n"
1342  << "Index of variable must be less than number of variables.\n";
1343 
1344  throw std::logic_error(buffer.str());
1345  }
1346 
1347  #endif
1348 
1349  // Set units of single variable
1350 
1351  items[i].units = new_units;
1352 }
1353 
1354 
1355 // void set_descriptions(const Vector<std::string>&) method
1356 
1360 
1362 {
1363  const size_t variables_number = get_variables_number();
1364 
1365  // Control sentence (if debug)
1366 
1367  #ifndef NDEBUG
1368 
1369  const size_t size = new_descriptions.size();
1370 
1371  if(size != variables_number)
1372  {
1373  std::ostringstream buffer;
1374 
1375  buffer << "OpenNN Exception: Variables class.\n"
1376  << "void set_descriptions(const Vector<std::string>&) method.\n"
1377  << "Size must be equal to number of variables.\n";
1378 
1379  throw std::logic_error(buffer.str());
1380  }
1381 
1382  #endif
1383 
1384  for(size_t i = 0; i < variables_number; i++)
1385  {
1386  items[i].description = new_descriptions[i];
1387  }
1388 }
1389 
1390 
1391 // void set_description(const size_t&, const std::string&) method
1392 
1397 
1398 void Variables::set_description(const size_t& i, const std::string& new_description)
1399 {
1400  // Control sentence (if debug)
1401 
1402  #ifndef NDEBUG
1403 
1404  const size_t variables_number = get_variables_number();
1405 
1406  if(i >= variables_number)
1407  {
1408  std::ostringstream buffer;
1409 
1410  buffer << "OpenNN Exception: Variables class.\n"
1411  << "void set_description(const size_t&, const std::string&) method.\n"
1412  << "Index of variable must be less than number of variables.\n";
1413 
1414  throw std::logic_error(buffer.str());
1415  }
1416 
1417  #endif
1418 
1419  // Set description of single variable
1420 
1421  items[i].description = new_description;
1422 }
1423 
1424 
1425 // void set_names(const Vector<std::string>& , const Vector< Vector<std::string> >&) method
1426 
1431 
1432 void Variables::set_names(const Vector<std::string>& columns_name, const Vector< Vector<std::string> >& nominal_labels)
1433 {
1434  #ifndef NDEBUG
1435 
1436  if(columns_name.size() != nominal_labels.size())
1437  {
1438  std::ostringstream buffer;
1439 
1440  buffer << "OpenNN Exception: Variables class.\n"
1441  << "void set_names(const Vector<std::string>& , const Vector< Vector<std::string> >&) method.\n"
1442  << "Size of columns name (" << columns_name.size() << ") must be equal to size of nominal labels " << nominal_labels.size() << ".\n";
1443 
1444  throw std::logic_error(buffer.str());
1445  }
1446 
1447  #endif
1448 
1449  Vector<std::string> names;
1450 
1451  for(size_t i = 0; i < nominal_labels.size(); i++)
1452  {
1453  if(nominal_labels[i].size() == 0)
1454  {
1455  names.push_back(columns_name[i]);
1456  }
1457  else if(nominal_labels[i].size() == 2)
1458  {
1459  if(nominal_labels[i].contains("true") || nominal_labels[i].contains("True") || nominal_labels[i].contains("TRUE"))
1460  {
1461  names.push_back(columns_name[i] + "_true");
1462  }
1463  else if(nominal_labels[i].contains("positive") || nominal_labels[i].contains("Positive") || nominal_labels[i].contains("POSITIVE"))
1464  {
1465  names.push_back(columns_name[i] + "positive");
1466  }
1467  else
1468  {
1469  names.push_back(columns_name[i]);
1470  }
1471  }
1472  else
1473  {
1474  for(size_t j = 0; j < nominal_labels[i].size(); j++)
1475  {
1476  names.push_back(nominal_labels[i][j]);
1477  }
1478  }
1479  }
1480 
1481  set_names(names);
1482 
1483 
1484 /*
1485  // Control sentence (if debug)
1486 
1487 
1488  size_t index = 0;
1489 
1490  for(size_t i = 0; i < new_names.size(); i++)
1491  {
1492  if(new_names[i].size() == 1)
1493  {
1494  items[index].name = new_names[i][0];
1495  index++;
1496  }
1497  else if(new_names[i].size() == 2)
1498  {
1499  if(new_names[i].contains("true") || new_names[i].contains("True") || new_names[i].contains("TRUE"))
1500  {
1501  items[index].name = "True";
1502  index++;
1503  }
1504  else if(new_names[i].contains("positive") || new_names[i].contains("Positive") || new_names[i].contains("POSITIVE"))
1505  {
1506  items[index].name = "Positive";
1507  index++;
1508  }
1509  else
1510  {
1511  items[index].name = new_names[i][0];
1512  index++;
1513  }
1514  }
1515  else
1516  {
1517  for(size_t j = 0; j < new_names[i].size(); j++)
1518  {
1519  items[index].name = new_names[i][j];
1520  index++;
1521  }
1522  }
1523  }
1524 */
1525 }
1526 
1527 
1528 // void set_display(const bool&) method
1529 
1534 
1535 void Variables::set_display(const bool& new_display)
1536 {
1537  display = new_display;
1538 }
1539 
1540 
1541 // void convert_time_series(const size_t&) method
1542 
1546 
1547 void Variables::convert_time_series(const size_t& lags_number)
1548 {
1549  const size_t variables_number = get_variables_number();
1550 
1551  const size_t new_variables_number = variables_number*(1+lags_number);
1552 
1553  Vector<Item> new_items(new_variables_number);
1554 
1555  size_t index = 0;
1556 
1557  for(size_t i = 0; i < 1+lags_number; i++)
1558  {
1559  for(size_t j = 0; j < variables_number; j++)
1560  {
1561  new_items[index].name = items[j].name;
1562  new_items[index].units = items[j].units;
1563  new_items[index].description = items[j].description;
1564 
1565  if(i != lags_number)
1566  {
1567  new_items[index].name.append("_lag_").append(unsigned_to_string(lags_number-i));
1568  new_items[index].use = Variables::Input;
1569  }
1570  else
1571  {
1572  new_items[index].use = Variables::Target;
1573  }
1574 
1575  index++;
1576  }
1577  }
1578 
1579  set_items(new_items);
1580 }
1581 
1582 
1583 // void convert_autoassociation(void) method
1584 
1588 
1590 {
1591  const size_t variables_number = get_variables_number();
1592 
1593  set_input();
1594 
1595  Vector<Item> autoassociation_items(variables_number);
1596 
1597  for(size_t i = 0; i < variables_number; i++)
1598  {
1599  autoassociation_items[i].name = prepend("autoassociation_", items[i].name);
1600  autoassociation_items[i].units = items[i].units;
1601  autoassociation_items[i].description = items[i].description;
1602  autoassociation_items[i].use = Variables::Target;
1603  }
1604 
1605  set_items(items.assemble(autoassociation_items));
1606 }
1607 
1608 
1609 
1610 // void set_variables_number(const size_t&) method
1611 
1615 
1616 void Variables::set_variables_number(const size_t& new_variables_number)
1617 {
1618  items.set(new_variables_number);
1619  set_default_uses();
1620 }
1621 
1622 
1623 // std::string to_string(void) const method
1624 
1626 
1627 std::string Variables::to_string(void) const
1628 {
1629  std::ostringstream buffer;
1630 
1631  const size_t variables_number = get_variables_number();
1632  const size_t inputs_number = count_inputs_number();
1633  const size_t targets_number = count_targets_number();
1634 
1635  buffer << "Variables object\n"
1636  << "Variables number: " << variables_number << "\n"
1637  << "Inputs number: " << inputs_number << "\n"
1638  << "Target number: " << targets_number << "\n";
1639 
1640  buffer << "Items:\n";
1641 
1642  const Vector<std::string> uses_string = write_uses();
1643 
1644  for(size_t i = 0; i < variables_number; i++)
1645  {
1646  buffer << "Variable " << i+1 << ":\n"
1647  << "Name: " << items[i].name << "\n"
1648  << "Units: " << items[i].units << "\n"
1649  << "Description: " << items[i].description << "\n"
1650  << "Use: " << write_use(i) << "\n";
1651  }
1652 
1653  buffer << "Display: " << display << "\n";
1654 
1655  return(buffer.str());
1656 }
1657 
1658 
1659 // tinyxml2::XMLDocument* to_XML(void) const method
1660 
1663 
1664 tinyxml2::XMLDocument* Variables::to_XML(void) const
1665 {
1666  tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument;
1667 
1668  std::ostringstream buffer;
1669 
1670  // Variables
1671 
1672  tinyxml2::XMLElement* variables_element = document->NewElement("Variables");
1673 
1674  document->InsertFirstChild(variables_element);
1675 
1676 
1677  tinyxml2::XMLElement* element = NULL;
1678  tinyxml2::XMLText* text = NULL;
1679 
1680  const size_t variables_number = get_variables_number();
1681 
1682  // Variables number
1683  {
1684  element = document->NewElement("VariablesNumber");
1685  variables_element->LinkEndChild(element);
1686 
1687  buffer.str("");
1688  buffer << variables_number;
1689 
1690  text = document->NewText(buffer.str().c_str());
1691  element->LinkEndChild(text);
1692  }
1693 
1694  for(size_t i = 0; i < variables_number; i++)
1695  {
1696  element = document->NewElement("Item");
1697  element->SetAttribute("Index", (unsigned)i+1);
1698  variables_element->LinkEndChild(element);
1699 
1700  // Name
1701 
1702  tinyxml2::XMLElement* name_element = document->NewElement("Name");
1703  element->LinkEndChild(name_element);
1704 
1705  tinyxml2::XMLText* name_text = document->NewText(items[i].name.c_str());
1706  name_element->LinkEndChild(name_text);
1707 
1708  // Units
1709 
1710  tinyxml2::XMLElement* units_element = document->NewElement("Units");
1711  element->LinkEndChild(units_element);
1712 
1713  tinyxml2::XMLText* units_text = document->NewText(items[i].units.c_str());
1714  units_element->LinkEndChild(units_text);
1715 
1716  // Description
1717 
1718  tinyxml2::XMLElement* description_element = document->NewElement("Description");
1719  element->LinkEndChild(description_element);
1720 
1721  tinyxml2::XMLText* descriptionText = document->NewText(items[i].description.c_str());
1722  description_element->LinkEndChild(descriptionText);
1723 
1724  // Use
1725 
1726  tinyxml2::XMLElement* use_element = document->NewElement("Use");
1727  element->LinkEndChild(use_element);
1728 
1729  tinyxml2::XMLText* use_text = document->NewText(write_use(i).c_str());
1730  use_element->LinkEndChild(use_text);
1731  }
1732 
1733  // Display
1734  {
1735  element = document->NewElement("Display");
1736  variables_element->LinkEndChild(element);
1737 
1738  buffer.str("");
1739  buffer << display;
1740 
1741  text = document->NewText(buffer.str().c_str());
1742  element->LinkEndChild(text);
1743  }
1744 
1745  return(document);
1746 }
1747 
1748 
1749 // void from_XML(const tinyxml2::XMLDocument&) method
1750 
1753 
1754 void Variables::from_XML(const tinyxml2::XMLDocument& document)
1755 {
1756  std::ostringstream buffer;
1757 
1758  unsigned index = 0;
1759 
1760  // Variables
1761 
1762  const tinyxml2::XMLElement* variables_element = document.FirstChildElement("Variables");
1763 
1764  if(!variables_element)
1765  {
1766  buffer << "OpenNN Exception: Variables class.\n"
1767  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1768  << "Pointer to variables element is NULL.\n";
1769 
1770  throw std::logic_error(buffer.str());
1771  }
1772 
1773  // Variables number
1774 
1775  const tinyxml2::XMLElement* variables_number_element = variables_element->FirstChildElement("VariablesNumber");
1776 
1777  if(!variables_number_element)
1778  {
1779  buffer << "OpenNN Exception: Variables class.\n"
1780  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1781  << "Pointer to variables number is NULL.\n";
1782 
1783  throw std::logic_error(buffer.str());
1784  }
1785 
1786  const size_t variables_number = atoi(variables_number_element->GetText());
1787 
1788  set_variables_number(variables_number);
1789 
1790  // Items
1791 
1792  const tinyxml2::XMLElement* start_element = variables_number_element;
1793 
1794  for(size_t i = 0; i < variables_number; i++)
1795  {
1796  const tinyxml2::XMLElement* item_element = start_element->NextSiblingElement("Item");
1797  start_element = item_element;
1798 
1799  if(!item_element)
1800  {
1801  buffer << "OpenNN Exception: Variables class.\n"
1802  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1803  << "Item " << i+1 << " is NULL.\n";
1804 
1805  throw std::logic_error(buffer.str());
1806  }
1807 
1808  item_element->QueryUnsignedAttribute("Index", &index);
1809 
1810  if(index != i+1)
1811  {
1812  buffer << "OpenNN Exception: Variables class.\n"
1813  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1814  << "Index " << index << " is not correct.\n";
1815 
1816  throw std::logic_error(buffer.str());
1817  }
1818 
1819  // Name
1820 
1821  const tinyxml2::XMLElement* name_element = item_element->FirstChildElement("Name");
1822 
1823  if(!name_element)
1824  {
1825  buffer << "OpenNN Exception: Variables class.\n"
1826  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1827  << "Pointer to name element is NULL.\n";
1828 
1829  throw std::logic_error(buffer.str());
1830  }
1831 
1832  if(name_element->GetText())
1833  {
1834  items[index-1].name = name_element->GetText();
1835  }
1836 
1837  // Units
1838 
1839  const tinyxml2::XMLElement* units_element = item_element->FirstChildElement("Units");
1840 
1841  if(!units_element)
1842  {
1843  buffer << "OpenNN Exception: Variables class.\n"
1844  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1845  << "Pointer to units element is NULL.\n";
1846 
1847  throw std::logic_error(buffer.str());
1848  }
1849 
1850  if(units_element->GetText())
1851  {
1852  items[index-1].units = units_element->GetText();
1853  }
1854 
1855  // Description
1856 
1857  const tinyxml2::XMLElement* description_element = item_element->FirstChildElement("Description");
1858 
1859  if(!description_element)
1860  {
1861  buffer << "OpenNN Exception: Variables class.\n"
1862  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1863  << "Pointer to description element is NULL.\n";
1864 
1865  throw std::logic_error(buffer.str());
1866  }
1867 
1868  if(description_element->GetText())
1869  {
1870  items[index-1].description = description_element->GetText();
1871  }
1872 
1873  // Use
1874 
1875  const tinyxml2::XMLElement* use_element = item_element->FirstChildElement("Use");
1876 
1877  if(!use_element)
1878  {
1879  buffer << "OpenNN Exception: Variables class.\n"
1880  << "void from_XML(const tinyxml2::XMLElement*) method.\n"
1881  << "Pointer to use element is NULL.\n";
1882 
1883  throw std::logic_error(buffer.str());
1884  }
1885 
1886  if(use_element->GetText())
1887  {
1888  set_use(index-1, use_element->GetText());
1889  }
1890  }
1891 }
1892 
1893 
1894 // std::string Variables::unsigned_to_string(const size_t&) method
1895 
1897 
1898 std::string Variables::unsigned_to_string(const size_t& number)
1899 {
1900  std::ostringstream buffer;
1901 
1902  buffer << number;
1903 
1904  return(buffer.str());
1905 }
1906 
1907 
1908 // std::string prepend(const std::string&, const std::string&) method
1909 
1911 
1912 std::string Variables::prepend(const std::string& pre, const std::string& str)
1913 {
1914  std::ostringstream buffer;
1915 
1916  buffer << pre << str;
1917 
1918  return(buffer.str());
1919 }
1920 
1921 
1922 }
1923 
1924 
1925 // OpenNN: Open Neural Networks Library.
1926 // Copyright (C) 2005-2015 Roberto Lopez
1927 //
1928 // This library is free software; you can redistribute it and/or
1929 // modify it under the terms of the GNU Lesser General Public
1930 // License as published by the Free Software Foundation; either
1931 // version 2.1 of the License, or any later version.
1932 //
1933 // This library is distributed in the hope that it will be useful,
1934 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1935 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1936 // Lesser General Public License for more details.
1937 
1938 // You should have received a copy of the GNU Lesser General Public
1939 // License along with this library; if not, write to the Free Software
1940 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
virtual ~Variables(void)
Destructor.
Definition: variables.cpp:99
const std::string & get_description(const size_t &) const
Definition: variables.cpp:784
Use
This enumeration represents the possible uses of a variable (input, target or unused).
Definition: variables.h:91
void set_name(const size_t &, const std::string &)
Definition: variables.cpp:1258
Vector< std::string > arrange_inputs_name(void) const
Returns the names of the input variables in the data set.
Definition: variables.cpp:615
const std::string & get_name(const size_t &) const
Definition: variables.cpp:565
Vector< Item > items
Definition: variables.h:246
void set_target(void)
Sets all the variables in the data set as target variables.
Definition: variables.cpp:1175
Vector< std::string > arrange_units(void) const
Returns the units of all the variables in the data set.
Definition: variables.cpp:665
void convert_time_series(const size_t &)
Definition: variables.cpp:1547
Vector< std::string > arrange_inputs_units(void) const
Returns the units of the input variables in the data set.
Definition: variables.cpp:714
bool empty(void) const
Returns true if the number of variables is zero, and false otherwise.
Definition: variables.cpp:196
void set_descriptions(const Vector< std::string > &)
Definition: variables.cpp:1361
static std::string unsigned_to_string(const size_t &)
Returns the string representation of number of type size_t.
Definition: variables.cpp:1898
Matrix< std::string > arrange_targets_information(void) const
Definition: variables.cpp:892
Vector< size_t > arrange_targets_indices(void) const
Returns the indices of the target variables.
Definition: variables.cpp:519
static std::string prepend(const std::string &, const std::string &)
Prepends the string pre to the beginning of the string str and returns the whole string.
Definition: variables.cpp:1912
const bool & get_display(void) const
Definition: variables.cpp:920
Matrix< std::string > arrange_information(void) const
Definition: variables.cpp:471
void set_variables_number(const size_t &)
Definition: variables.cpp:1616
void set_display(const bool &)
Definition: variables.cpp:1535
void set_default_uses(void)
Definition: variables.cpp:1194
void set_items(const Vector< Item > &)
Definition: variables.cpp:1011
void set_input(void)
Sets all the variables in the data set as input variables.
Definition: variables.cpp:1160
Vector< std::string > arrange_targets_units(void) const
Returns the units of the target variables in the data set.
Definition: variables.cpp:739
void set_uses(const Vector< Use > &)
Definition: variables.cpp:1023
tinyxml2::XMLDocument * to_XML(void) const
Definition: variables.cpp:1664
void set(void)
Sets a variables object with zero variables.
Definition: variables.cpp:930
size_t count_used_variables_number(void) const
Returns the number of variables which are either input or target.
Definition: variables.cpp:213
bool has_names(void) const
Returns true if all the variables have been named, and false otherwise.
Definition: variables.cpp:594
Vector< std::string > arrange_targets_description(void) const
Returns the description of the target variables in the data set.
Definition: variables.cpp:838
void from_XML(const tinyxml2::XMLDocument &)
Definition: variables.cpp:1754
bool display
Display messages to screen.
Definition: variables.h:250
void set_description(const size_t &, const std::string &)
Definition: variables.cpp:1398
size_t count_inputs_number(void) const
Returns the number of input variables of the data set.
Definition: variables.cpp:249
const std::string & get_unit(const size_t &) const
Definition: variables.cpp:685
Matrix< std::string > arrange_inputs_information(void) const
Definition: variables.cpp:864
void convert_autoassociation(void)
Definition: variables.cpp:1589
const Use & get_use(const size_t &) const
Definition: variables.cpp:388
Vector< std::string > arrange_names(void) const
Returns the names of all the variables in the data set.
Definition: variables.cpp:545
void set_use(const size_t &, const Use &)
Definition: variables.cpp:1078
Vector< size_t > count_uses(void) const
Definition: variables.cpp:295
size_t count_targets_number(void) const
Returns the number of target variables of the data set.
Definition: variables.cpp:271
void set_units(const Vector< std::string > &)
Definition: variables.cpp:1291
Vector< std::string > arrange_targets_name(void) const
Returns the names of the target variables in the data set.
Definition: variables.cpp:640
Vector< std::string > write_uses(void) const
Definition: variables.cpp:347
std::string to_string(void) const
Returns a string representation of the current variables object.
Definition: variables.cpp:1627
void set_names(const Vector< std::string > &)
Definition: variables.cpp:1221
const Vector< Item > & get_items(void) const
Returns the vector Item structures in the variables object.
Definition: variables.cpp:156
bool is_used(const size_t &) const
Definition: variables.cpp:431
Vector< Use > arrange_uses(void) const
Definition: variables.cpp:326
size_t get_variables_number(void) const
Returns the total number of variables in the data set.
Definition: variables.h:126
size_t count_unused_variables_number(void) const
Returns the number of variables which will neither be used as input nor as target.
Definition: variables.cpp:227
Variables & operator=(const Variables &)
Definition: variables.cpp:110
bool operator==(const Variables &) const
Definition: variables.cpp:136
void set_default(void)
Definition: variables.cpp:1000
Vector< std::string > arrange_descriptions(void) const
Returns the description of all the variables in the data set.
Definition: variables.cpp:764
std::string write_use(const size_t &) const
Definition: variables.cpp:399
const Item & get_item(const size_t &) const
Definition: variables.cpp:167
Vector< size_t > arrange_inputs_indices(void) const
Returns the indices of the input variables.
Definition: variables.cpp:493
Vector< std::string > arrange_inputs_description(void) const
Returns the description of the input variables in the data set.
Definition: variables.cpp:813