From 2ec43500809b14defa66764c2f8cecf47ec27214 Mon Sep 17 00:00:00 2001
From: Martin Beseda <martin.beseda@vsb.cz>
Date: Tue, 18 Dec 2018 00:13:28 +0100
Subject: [PATCH] FIX: Fixed unallocated vectors during data normalization.

---
 src/CSVReader/CSVReader.cpp                   |  3 +-
 src/DataSet/DataSet.cpp                       | 31 +++++++++++--------
 src/DataSet/DataSet.h                         | 12 +++----
 src/LearningMethods/GradientDescent.cpp       |  2 +-
 src/LearningMethods/ParticleSwarm.cpp         |  2 +-
 .../NormalizationStrategy.cpp                 |  9 ++++--
 6 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/src/CSVReader/CSVReader.cpp b/src/CSVReader/CSVReader.cpp
index 7a212725..22c739ad 100644
--- a/src/CSVReader/CSVReader.cpp
+++ b/src/CSVReader/CSVReader.cpp
@@ -88,6 +88,7 @@ namespace lib4neuro {
             data_set_contents.push_back(std::make_pair(input, output));
         }
 
-        return DataSet(&data_set_contents);
+        DoubleUnitStrategy* ds = new DoubleUnitStrategy();
+        return DataSet(&data_set_contents, ds);
     }
 }
\ No newline at end of file
diff --git a/src/DataSet/DataSet.cpp b/src/DataSet/DataSet.cpp
index fa517d0d..72ce426a 100644
--- a/src/DataSet/DataSet.cpp
+++ b/src/DataSet/DataSet.cpp
@@ -38,8 +38,8 @@ namespace lib4neuro {
 
         if(ns) {
             this->normalization_strategy = ns;
-            this->max_min_inp_val.at(0) = this->normalization_strategy->get_max_value();
-            this->max_min_inp_val.at(1) = this->normalization_strategy->get_min_value();
+//            this->max_min_inp_val.emplace_back(this->normalization_strategy->get_max_value());
+//            this->max_min_inp_val.emplace_back(this->normalization_strategy->get_min_value());
         }
 
         //TODO check the complete data set for input/output dimensions
@@ -58,8 +58,8 @@ namespace lib4neuro {
 
         if(ns) {
             this->normalization_strategy = ns;
-            this->max_min_inp_val.at(0) = this->normalization_strategy->get_max_value();
-            this->max_min_inp_val.at(1) = this->normalization_strategy->get_min_value();
+//            this->max_min_inp_val.emplace_back(this->normalization_strategy->get_max_value());
+//            this->max_min_inp_val.emplace_back(this->normalization_strategy->get_min_value());
         }
 
         this->add_isotropic_data(lower_bound, upper_bound, size, output);
@@ -78,8 +78,8 @@ namespace lib4neuro {
 
         if(ns) {
             this->normalization_strategy = ns;
-            this->max_min_inp_val.at(0) = this->normalization_strategy->get_max_value();
-            this->max_min_inp_val.at(1) = this->normalization_strategy->get_min_value();
+//            this->max_min_inp_val.emplace_back(this->normalization_strategy->get_max_value());
+//            this->max_min_inp_val.emplace_back(this->normalization_strategy->get_min_value());
         }
 
         this->add_isotropic_data(bounds, no_elems_in_one_dim, output_func);
@@ -222,12 +222,17 @@ namespace lib4neuro {
     }
 
     void DataSet::normalize() {
-//        if(this->normalized) {
-//            throw std::runtime_error("This data set is already normalized!");
-//        }
+        if(!this->normalization_strategy) {
+            throw std::runtime_error("There is no normalization strategy given for this data set, so it can not be "
+                                     "normalized!");
+        }
 
         /* Find maximum and minimum values */
-        this->max_min_inp_val.at(0) =  this->max_min_inp_val.at(1) = this->data[0].first.at(0);
+        if(this->max_min_inp_val.empty()) {
+            this->max_min_inp_val.emplace_back(this->data.at(0).first.at(0));
+            this->max_min_inp_val.emplace_back(this->data.at(0).first.at(0));
+        }
+
         double tmp, tmp2;
         for(auto pair : this->data) {
             /* Finding maximum */
@@ -294,9 +299,9 @@ namespace lib4neuro {
         return this->normalization_strategy;
     }
 
-//    bool DataSet::is_normalized() {
-//        return this->normalized;
-//    }
+    bool DataSet::is_normalized() {
+        return !this->max_min_inp_val.empty();
+    }
 
     double DataSet::get_max_inp_val() {
         return this->max_min_inp_val.at(0);
diff --git a/src/DataSet/DataSet.h b/src/DataSet/DataSet.h
index 0ac92905..e0e04093 100644
--- a/src/DataSet/DataSet.h
+++ b/src/DataSet/DataSet.h
@@ -77,7 +77,7 @@ namespace lib4neuro {
          *
          */
         //TODO let user choose in the constructor!
-        NormalizationStrategy* normalization_strategy = new DoubleUnitStrategy;
+        NormalizationStrategy* normalization_strategy = nullptr; //new DoubleUnitStrategy;
 
 //        /**
 //         *
@@ -248,11 +248,11 @@ namespace lib4neuro {
          */
         LIB4NEURO_API NormalizationStrategy* get_normalization_strategy();
 
-//        /**
-//         *
-//         * @return
-//         */
-//        LIB4NEURO_API bool is_normalized();
+        /**
+         *
+         * @return
+         */
+        LIB4NEURO_API bool is_normalized();
 
         /**
          *
diff --git a/src/LearningMethods/GradientDescent.cpp b/src/LearningMethods/GradientDescent.cpp
index be320f69..a457d0cf 100644
--- a/src/LearningMethods/GradientDescent.cpp
+++ b/src/LearningMethods/GradientDescent.cpp
@@ -44,7 +44,7 @@ namespace lib4neuro {
     void GradientDescent::optimize(lib4neuro::ErrorFunction &ef) {
 
         /* Copy data set max and min values, if it's normalized */
-        if(!std::isnan(ef.get_dataset()->get_max_inp_val())) {
+        if(ef.get_dataset()->is_normalized()) {
             ef.get_network_instance()->set_normalization_strategy_instance(
                     ef.get_dataset()->get_normalization_strategy());
         }
diff --git a/src/LearningMethods/ParticleSwarm.cpp b/src/LearningMethods/ParticleSwarm.cpp
index 7c649f6d..5046f4e1 100644
--- a/src/LearningMethods/ParticleSwarm.cpp
+++ b/src/LearningMethods/ParticleSwarm.cpp
@@ -258,7 +258,7 @@ namespace lib4neuro {
      */
     void ParticleSwarm::optimize(lib4neuro::ErrorFunction &ef) {
         /* Copy data set max and min values, if it's normalized */
-        if(!std::isnan(ef.get_dataset()->get_max_inp_val())) {
+        if(ef.get_dataset()->is_normalized()) {
             ef.get_network_instance()->set_normalization_strategy_instance(
                     ef.get_dataset()->get_normalization_strategy());
         }
diff --git a/src/NormalizationStrategy/NormalizationStrategy.cpp b/src/NormalizationStrategy/NormalizationStrategy.cpp
index 9ca3b04e..32f475ca 100644
--- a/src/NormalizationStrategy/NormalizationStrategy.cpp
+++ b/src/NormalizationStrategy/NormalizationStrategy.cpp
@@ -23,8 +23,13 @@ double NormalizationStrategy::get_min_value() {
 DoubleUnitStrategy::DoubleUnitStrategy() {}
 
 double DoubleUnitStrategy::normalize(double n, double max, double min) {
-    this->max_min_inp_val.at(0) = max;
-    this->max_min_inp_val.at(1) = min;
+    if(this->max_min_inp_val.empty()) {
+        this->max_min_inp_val.emplace_back(max);
+        this->max_min_inp_val.emplace_back(min);
+    } else {
+        this->max_min_inp_val.at(0) = max;
+        this->max_min_inp_val.at(1) = min;
+    }
 
     return 2*(n - min)/(max - min) - 1;
 }
-- 
GitLab