From f2a604d95dcb3ea80fbbdadb2bc6f2bd7a382184 Mon Sep 17 00:00:00 2001
From: Martin Beseda <martin.beseda@vsb.cz>
Date: Mon, 4 Mar 2019 12:38:28 +0100
Subject: [PATCH] Revert "FIX: Fixed Neuron class structure"

This reverts commit 21af8ef1dd9cf1f047df34cbe846023cdf92e269.
---
 src/Neuron/Neuron.h                      | 31 ++++++++++--------------
 src/Neuron/NeuronBinary.cpp              |  1 +
 src/Neuron/NeuronBinary.h                |  6 ++---
 src/Neuron/NeuronConstant.h              |  2 +-
 src/Neuron/NeuronLinear.h                |  2 +-
 src/Neuron/NeuronLogistic.cpp            | 12 ++++-----
 src/Neuron/NeuronLogistic.h              | 22 ++++++++---------
 src/Neuron/NeuronLogisticSerialization.h |  4 +--
 src/Solvers/DESolver.cpp                 |  2 +-
 9 files changed, 39 insertions(+), 43 deletions(-)

diff --git a/src/Neuron/Neuron.h b/src/Neuron/Neuron.h
index 6900c36f..88747a63 100644
--- a/src/Neuron/Neuron.h
+++ b/src/Neuron/Neuron.h
@@ -32,7 +32,7 @@ namespace lib4neuro {
 
     protected:
         /**
-         * Holds the last value of the activation function, used by this->activate
+         * holds the last value of the activation function, used by this->activate
          */
         double activation_val;
 
@@ -64,24 +64,12 @@ namespace lib4neuro {
 
     }; /* end of Neuron class */
 
-    /**
-     * Abstract clas providing the method get_derivative()
-     */
-    class IGetDerivative {
-    public:
-        /**
-         * Returns a Neuron pointer object with activation function being the partial derivative of
-         * the activation function of this Neuron object with respect to the argument, i.e. 'potential'
-         * @return
-         */
-        virtual Neuron* get_derivative() = 0;
-    };
 
-    /**
-     * Class serving as an interface providing 'activation_function_eval_partial_derivative',
-     * 'activation_function_eval_derivative',  'get_partial_derivative' and
-     * 'get_derivative' methods.
-     */
+/**
+ * Class serving as an interface providing 'activation_function_eval_partial_derivative',
+ * 'activation_function_eval_derivative',  'get_partial_derivative' and
+ * 'get_derivative' methods.
+ */
     class NeuronDifferentiable : public Neuron {
     public:
         /**
@@ -96,6 +84,13 @@ namespace lib4neuro {
          * and 'b' is the bias
          */
         virtual double activation_function_eval_derivative_bias(double x, double b) = 0;
+
+        /**
+         * Returns a Neuron pointer object with activation function being the partial derivative of
+         * the activation function of this Neuron object with respect to the argument, i.e. 'potential'
+         * @return
+         */
+        virtual Neuron *get_derivative() = 0;
     };
 
 }
diff --git a/src/Neuron/NeuronBinary.cpp b/src/Neuron/NeuronBinary.cpp
index 65255961..9e7bbb80 100644
--- a/src/Neuron/NeuronBinary.cpp
+++ b/src/Neuron/NeuronBinary.cpp
@@ -22,4 +22,5 @@ namespace lib4neuro {
 
         return this->activation_val;
     }
+
 }
\ No newline at end of file
diff --git a/src/Neuron/NeuronBinary.h b/src/Neuron/NeuronBinary.h
index 717cbab2..1992b03f 100644
--- a/src/Neuron/NeuronBinary.h
+++ b/src/Neuron/NeuronBinary.h
@@ -14,9 +14,9 @@
 
 namespace lib4neuro {
 
-    /**
-     *  Binary neuron class - uses unit-step as the activation function
-     */
+/**
+ *  Binary neuron class - uses unit-step as the activation function
+ */
     class NeuronBinary : public Neuron {
 
     public:
diff --git a/src/Neuron/NeuronConstant.h b/src/Neuron/NeuronConstant.h
index b59f6bf3..71e43013 100644
--- a/src/Neuron/NeuronConstant.h
+++ b/src/Neuron/NeuronConstant.h
@@ -12,7 +12,7 @@
 
 namespace lib4neuro {
 
-    class NeuronConstant : public NeuronDifferentiable, IGetDerivative {
+    class NeuronConstant : public NeuronDifferentiable {
     private:
         double p = 0.0;
 
diff --git a/src/Neuron/NeuronLinear.h b/src/Neuron/NeuronLinear.h
index 2db91345..d7043f93 100644
--- a/src/Neuron/NeuronLinear.h
+++ b/src/Neuron/NeuronLinear.h
@@ -18,7 +18,7 @@ namespace lib4neuro {
      * Linear neuron class - uses activation function in the form f(x)=a*x + b,
      * 'x' being the neuron's potential
      */
-    class NeuronLinear:public NeuronDifferentiable, IGetDerivative {
+    class NeuronLinear:public NeuronDifferentiable {
 
     public:
 
diff --git a/src/Neuron/NeuronLogistic.cpp b/src/Neuron/NeuronLogistic.cpp
index 9403784d..dd1469d7 100644
--- a/src/Neuron/NeuronLogistic.cpp
+++ b/src/Neuron/NeuronLogistic.cpp
@@ -43,10 +43,10 @@ namespace lib4neuro {
         return -this->activation_function_eval_derivative_bias(x, b);
     }
 
-//    NeuronLogistic *NeuronLogistic_d2::get_derivative() {
-//        //TODO maybe not the best way
-//        return nullptr;
-//    }
+    NeuronLogistic *NeuronLogistic_d2::get_derivative() {
+        //TODO maybe not the best way
+        return nullptr;
+    }
 
     NeuronLogistic_d1::NeuronLogistic_d1() {}
 
@@ -78,7 +78,7 @@ namespace lib4neuro {
         return -this->activation_function_eval_derivative_bias(x, b);
     }
 
-    NeuronDifferentiable* NeuronLogistic_d1::get_derivative() {
+    NeuronLogistic *NeuronLogistic_d1::get_derivative() {
         //(e^(b + x) (e^b - e^x))/(e^b + e^x)^3
         NeuronLogistic_d2 *output = nullptr;
 
@@ -113,7 +113,7 @@ namespace lib4neuro {
 
     }
 
-    NeuronDifferentiable* NeuronLogistic::get_derivative() {
+    NeuronLogistic *NeuronLogistic::get_derivative() {
 
         NeuronLogistic_d1 *output = nullptr;
         output = new NeuronLogistic_d1();
diff --git a/src/Neuron/NeuronLogistic.h b/src/Neuron/NeuronLogistic.h
index 45cd4f45..9daf384b 100644
--- a/src/Neuron/NeuronLogistic.h
+++ b/src/Neuron/NeuronLogistic.h
@@ -16,7 +16,7 @@
 #include "Neuron.h"
 
 namespace lib4neuro {
-    class NeuronLogistic : public NeuronDifferentiable, IGetDerivative {
+    class NeuronLogistic : public NeuronDifferentiable {
 
     public:
 
@@ -54,7 +54,7 @@ namespace lib4neuro {
          * Returns a pointer to a Neuron with derivative as its activation function
          * @return
          */
-        LIB4NEURO_API virtual NeuronDifferentiable* get_derivative() override;
+        LIB4NEURO_API virtual NeuronLogistic *get_derivative() override;
     };
 
 
@@ -78,7 +78,7 @@ namespace lib4neuro {
         /**
          * Evaluates 'e^(b - x)/(e^(b - x) + 1)^2' and returns the result
          */
-        LIB4NEURO_API double activate(double x, double b) override;
+        LIB4NEURO_API virtual double activate(double x, double b) override;
 
         /**
          * Calculates the partial derivative of the activation function
@@ -86,23 +86,23 @@ namespace lib4neuro {
          * @return Partial derivative of the activation function according to the
          * bias, returns: (e^(b + x) (e^x - e^b))/(e^b + e^x)^3
          */
-        LIB4NEURO_API double activation_function_eval_derivative_bias(double x, double b) override;
+        LIB4NEURO_API virtual double activation_function_eval_derivative_bias(double x, double b) override;
 
         /**
          * Calculates d/dx of  e^(b - x)*(1 + e^(b - x))^(-2)
          * @return  (e^(b + x) (e^b - e^x))/(e^b + e^x)^3
          */
-        LIB4NEURO_API double activation_function_eval_derivative(double x, double b) override;
+        LIB4NEURO_API virtual double activation_function_eval_derivative(double x, double b) override;
 
         /**
          * Returns a pointer to a Neuron with derivative as its activation function
          * @return
          */
-        LIB4NEURO_API NeuronDifferentiable* get_derivative() override;
+        LIB4NEURO_API virtual NeuronLogistic *get_derivative() override;
     };
 
 
-    class NeuronLogistic_d2 : public NeuronDifferentiable {
+    class NeuronLogistic_d2 : public NeuronLogistic_d1 {
 
     public:
 
@@ -121,7 +121,7 @@ namespace lib4neuro {
         /**
          * Evaluates '(e^(b + x) (e^b - e^x))/(e^b + e^x)^3' and returns the result
          */
-        LIB4NEURO_API double activate(double x, double b) override;
+        LIB4NEURO_API virtual double activate(double x, double b) override;
 
         /**
          * Calculates the partial derivative of the activation function
@@ -129,19 +129,19 @@ namespace lib4neuro {
          * @return Partial derivative of the activation function according to the
          * bias, returns: -(e^(b + x) (-4 e^(b + x) + e^(2 b) + e^(2 x)))/(e^b + e^x)^4
          */
-        LIB4NEURO_API double activation_function_eval_derivative_bias(double x, double b) override;
+        LIB4NEURO_API virtual double activation_function_eval_derivative_bias(double x, double b) override;
 
         /**
          * Calculates d/dx of  (e^(b + x) (e^b - e^x))/(e^b + e^x)^3
          * @return (e^(b + x) (-4 e^(b + x) + e^(2 b) + e^(2 x)))/(e^b + e^x)^4
          */
-        LIB4NEURO_API double activation_function_eval_derivative(double x, double b) override;
+        LIB4NEURO_API virtual double activation_function_eval_derivative(double x, double b) override;
 
         /**
          *
          * @return
          */
-//        LIB4NEURO_API virtual NeuronLogistic *get_derivative() override;
+        LIB4NEURO_API virtual NeuronLogistic *get_derivative() override;
 
     };
 
diff --git a/src/Neuron/NeuronLogisticSerialization.h b/src/Neuron/NeuronLogisticSerialization.h
index 4bb0311f..1d65fcba 100644
--- a/src/Neuron/NeuronLogisticSerialization.h
+++ b/src/Neuron/NeuronLogisticSerialization.h
@@ -29,14 +29,14 @@ namespace lib4neuro {
     struct NeuronLogistic_d1::access {
         template<class Archive>
         static void serialize(Archive &ar, NeuronLogistic_d1 &n, const unsigned int version) {
-            ar & boost::serialization::base_object<Neuron>(n);
+            ar & boost::serialization::base_object<NeuronLogistic>(n);
         }
     };
 
     struct NeuronLogistic_d2::access {
         template<class Archive>
         static void serialize(Archive &ar, NeuronLogistic_d2 &n, const unsigned int version) {
-            ar & boost::serialization::base_object<Neuron>(n);
+            ar & boost::serialization::base_object<NeuronLogistic_d1>(n);
         }
     };
 
diff --git a/src/Solvers/DESolver.cpp b/src/Solvers/DESolver.cpp
index a963f35a..e201c565 100644
--- a/src/Solvers/DESolver.cpp
+++ b/src/Solvers/DESolver.cpp
@@ -261,7 +261,7 @@ namespace lib4neuro {
             for (size_t j = 0; j < derivative_degree; ++j) {
                 n_ptr2 = n_ptr;
 
-                n_ptr = dynamic_cast<NeuronLogistic*>(n_ptr->get_derivative());
+                n_ptr = n_ptr->get_derivative();
 
                 if (j > 0) {
                     delete n_ptr2;
-- 
GitLab