From ca72b23d1b95d97bb944daeb7e688954be75ebf8 Mon Sep 17 00:00:00 2001
From: Martin Beseda <martinbeseda@seznam.cz>
Date: Fri, 21 Sep 2018 14:01:08 +0200
Subject: [PATCH] ENH: Started using MSG_* macros together with Boost::format
 to unify lib4neuro output functions.

---
 build.sh                              |  2 +-
 external_dependencies/boost           |  2 +-
 src/CMakeLists.txt                    |  2 +-
 src/LearningMethods/ParticleSwarm.cpp | 17 +++++---
 src/Solvers/DESolver.cpp              | 60 +++++++++++++++------------
 5 files changed, 47 insertions(+), 36 deletions(-)

diff --git a/build.sh b/build.sh
index 49368398..7751f882 100755
--- a/build.sh
+++ b/build.sh
@@ -10,7 +10,7 @@
 BUILD_TESTS=yes
 BUILD_EXAMPLES=yes
 BUILD_LIB=yes
-DEPENDENCIES_LINK_TYPE=static # shared/static
+DEPENDENCIES_LINK_TYPE=shared # shared/static
 
 # Build type (Release/Debug)
 BUILD_TYPE=Debug
diff --git a/external_dependencies/boost b/external_dependencies/boost
index 7ff9ae13..5348a661 160000
--- a/external_dependencies/boost
+++ b/external_dependencies/boost
@@ -1 +1 @@
-Subproject commit 7ff9ae132ba8e8d9ecc329788a55b6e54139068b
+Subproject commit 5348a6616a21b94793ddc9e7c6aa37d288fd2b10
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c2f9bbd1..bea9ad9c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -85,7 +85,7 @@ if ("${BUILD_LIB}" STREQUAL "yes")
 		add_library(${PREFIX}boost_unit_test STATIC boost_test_lib_dummy.cpp)
 	elseif("${DEPENDENCIES_LINK_TYPE}" STREQUAL "static")
 		add_library(${PREFIX}boost_unit_test STATIC boost_test_lib_dummy.cpp)
-    elseif("${DEPENDENCIES_LINK_TYPE}" STREQUAL "shared")
+	elseif("${DEPENDENCIES_LINK_TYPE}" STREQUAL "shared")
         add_library(${PREFIX}boost_unit_test SHARED boost_test_lib_dummy.cpp)
 	endif()
 
diff --git a/src/LearningMethods/ParticleSwarm.cpp b/src/LearningMethods/ParticleSwarm.cpp
index 21679bc6..4d0f3aad 100644
--- a/src/LearningMethods/ParticleSwarm.cpp
+++ b/src/LearningMethods/ParticleSwarm.cpp
@@ -14,7 +14,9 @@
 #include <iterator>
 #include <algorithm>
 #include <iostream>
+#include <format.hpp>
 
+#include "../message.h"
 #include "../Network/NeuralNetwork.h"
 #include "../DataSet/DataSet.h"
 
@@ -185,9 +187,9 @@ double Particle::change_coordinate(double w, double c1, double c2, std::vector<d
 
 void Particle::print_coordinate() {
     for(unsigned int i = 0; i < this->coordinate_dim - 1; ++i){
-        printf("%10.8f, ", (*this->coordinate)[i]);
+        std::cout << (*this->coordinate)[i] << " ";
     }
-    printf("%10.8f\n", (*this->coordinate)[this->coordinate_dim - 1]);
+    std::cout << (*this->coordinate)[this->coordinate_dim - 1] << std::endl;
 }
 
 ParticleSwarm::ParticleSwarm(lib4neuro::ErrorFunction* ef, std::vector<double> *domain_bounds,
@@ -195,7 +197,8 @@ ParticleSwarm::ParticleSwarm(lib4neuro::ErrorFunction* ef, std::vector<double> *
     srand(time(NULL));
 
     if(domain_bounds->size() < 2 * ef->get_dimension()){
-        std::cerr << "The supplied domain bounds dimension is too low! It should be at least " << 2 * ef->get_dimension() << "\n" << std::endl;
+       throw std::invalid_argument(boost::str(boost::format("The supplied domain bounds dimension is too low! It should "
+                                                            "be at least %1%") % (2 * ef->get_dimension())));
     }
 
     this->f = ef;
@@ -274,7 +277,7 @@ void ParticleSwarm::optimize( double gamma, double epsilon, double delta) {
 //    for(unsigned int i = 0; i < this->n_particles; ++i){
 //        this->particle_swarm[i]->print_coordinate();
 //    }
-    printf("Initial best value: %10.8f\n", optimal_value);
+    MSG_DEBUG(boost::format("Initial best value: %1%") % optimal_value);
 
     while( outer_it < this->iter_max ) {
         max_velocity = 0;
@@ -361,10 +364,12 @@ void ParticleSwarm::optimize( double gamma, double epsilon, double delta) {
     this->determine_optimal_coordinate_and_value(*this->p_min_glob, optimal_value);
     if(outer_it < this->iter_max) {
         /* Convergence reached */
-        printf("\nFound optimum in %d iterations. Objective function value: %10.8f\n", (int)outer_it, optimal_value);
+        MSG_INFO(boost::format("Found optimum in %1% iterations. Objective function value: %2%") % (int)outer_it
+                               % optimal_value);
     } else {
         /* Maximal number of iterations reached */
-        printf("\nMax number of iterations reached (%d)!  Objective function value: %10.8f\n", (int)outer_it, optimal_value);
+        MSG_INFO(boost::format("Max number of iterations reached (%1%)!  Objective function value: %2%") % (int)outer_it
+                               % optimal_value);
     }
 //    for (size_t i = 0; i <= this->func_dim - 1; ++i) {
 //        printf("%10.8f \n", (*this->p_min_glob)[i]);
diff --git a/src/Solvers/DESolver.cpp b/src/Solvers/DESolver.cpp
index 029dbd55..3079b2d5 100644
--- a/src/Solvers/DESolver.cpp
+++ b/src/Solvers/DESolver.cpp
@@ -5,6 +5,8 @@
  * @date 22.7.18 -
  */
 
+#include <boost/format.hpp>
+
 #include "../message.h"
 #include "DESolver.h"
 
@@ -122,9 +124,9 @@ namespace lib4neuro {
             for (size_t j = 0; j < this->dim_inn; ++j) {
                 weight_idx = this->solution->add_connection_simple(first_input_neuron + i, first_inner_neuron + j,
                                                                    SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT);
-                printf("  adding a connection between input neuron %2d[%2d] and inner neuron  %2d[%2d], weight index %3d\n",
-                       (int) i, (int) (first_input_neuron + i), (int) j, (int) (first_inner_neuron + j),
-                       (int) weight_idx);
+                MSG_DEBUG(boost::format("Adding a connection between input neuron %1%[%2%] and inner neuron %3%[%4%], "
+                                        "weight index %5%") % (int) i % (int) (first_input_neuron + i) % (int) j
+                                        % (int) (first_inner_neuron + j) % (int) weight_idx);
             }
         }
 
@@ -132,8 +134,9 @@ namespace lib4neuro {
         for (size_t i = 0; i < this->dim_inn; ++i) {
             weight_idx = this->solution->add_connection_simple(first_inner_neuron + i, first_output_neuron,
                                                                SIMPLE_CONNECTION_TYPE::NEXT_WEIGHT);
-            printf("  adding a connection between inner neuron %2d[%2d] and output neuron %2d[%2d], weight index %3d\n",
-                   (int) i, (int) (first_inner_neuron + i), 0, (int) (first_output_neuron), (int) weight_idx);
+            MSG_DEBUG(boost::format("Adding a connection between inner neuron %1%[%2%] and output neuron %3%[%4%], "
+                                    "weight index %5%") % (int) i % (int) (first_inner_neuron + i) % 0
+                                    % (int) (first_output_neuron) % (int) weight_idx);
         }
 
         MultiIndex initial_mi(this->dim_i);
@@ -220,12 +223,13 @@ namespace lib4neuro {
         if (map_multiindices2nn.find(alpha) != map_multiindices2nn.end()) {
             new_net = map_multiindices2nn[alpha];
             this->differential_equations->at(equation_idx)->add_network(new_net, expression_string);
-            printf("\nAdding an existing partial derivative (multi-index: %s) to equation %d with coefficient %s\n",
-                   alpha.to_string().c_str(), (int) equation_idx, expression_string.c_str());
+            MSG_DEBUG(boost::format("Adding an existing partial derivative (multi-index: %1%) to equation "
+                                    "%2% with coefficient %3%") % alpha.to_string().c_str() % (int) equation_idx
+                                    % expression_string.c_str());
             return;
         }
-        printf("\nAdding a new partial derivative (multi-index: %s) to equation %d with coefficient %s\n",
-               alpha.to_string().c_str(), (int) equation_idx, expression_string.c_str());
+        MSG_DEBUG(boost::format("Adding a new partial derivative (multi-index: %1%) to equation %2% with coefficient "
+                                "%3%") % alpha.to_string().c_str() % (int) equation_idx % expression_string.c_str());
 
         /* we need to construct a new neural network */
         new_net = new NeuralNetwork();
@@ -285,26 +289,26 @@ namespace lib4neuro {
         size_t connection_idx = 0;
         for (size_t i = 0; i < this->dim_i; ++i) {
             for (size_t j = 0; j < this->dim_inn; ++j) {
-                printf("  adding a connection between input neuron %2d[%2d] and inner neuron  %2d[%2d], connection index: %3d\n",
-                       (int) i, (int) (first_input_neuron + i), (int) j, (int) (first_inner_neuron + j),
-                       (int) connection_idx);
+                MSG_DEBUG(boost::format("Adding a connection between input neuron %1%[%2%] and inner neuron  %3%[%4%], "
+                                        "connection index: %5%") % (int) i % (int) (first_input_neuron + i) % (int) j
+                                        % (int) (first_inner_neuron + j) % (int) connection_idx);
                 new_net->add_existing_connection(first_input_neuron + i, first_inner_neuron + j, connection_idx,
                                                  *this->solution);
                 connection_idx++;
             }
         }
-        printf("----------------------------------------------------------------------------------------------------\n");
+        MSG_DEBUG("------------------------------------------------------------------------------------------------\n");
 
         /* connections between inner neurons and the first set of 'glueing' neurons */
         for (size_t i = 0; i < this->dim_inn; ++i) {
-            printf("  adding a connection between inner neuron %2d[%2d] and glue neuron   %2d[%2d], connection index: %3d\n",
-                   (int) i, (int) (first_inner_neuron + i), (int) i, (int) (first_glue_neuron + i),
-                   (int) connection_idx);
+            MSG_DEBUG(boost::format("Adding a connection between inner neuron %1%[%2%] and glue neuron %3%[%4%], "
+                                    "connection index: %5%") % (int) i % (int) (first_inner_neuron + i) % (int) i
+                                    % (int) (first_glue_neuron + i) % (int) connection_idx);
             new_net->add_existing_connection(first_inner_neuron + i, first_glue_neuron + i, connection_idx,
                                              *this->solution);
             connection_idx++;
         }
-        printf("----------------------------------------------------------------------------------------------------\n");
+        MSG_DEBUG("------------------------------------------------------------------------------------------------\n");
 
         size_t pd_idx;
         /* connections between glueing neurons */
@@ -312,25 +316,26 @@ namespace lib4neuro {
             pd_idx = partial_derivative_indices[di];/* partial derivative index */
             for (size_t i = 0; i < this->dim_inn; ++i) {
                 connection_idx = pd_idx * this->dim_inn + i;
-                printf("  adding a connection between glue neuron  %2d[%2d] and glue neuron   %2d[%2d], connection index: %3d\n",
-                       (int) (i + (di) * this->dim_inn), (int) (first_glue_neuron + i + (di) * this->dim_inn),
-                       (int) (i + (di + 1) * this->dim_inn), (int) (first_glue_neuron + i + (di + 1) * this->dim_inn),
-                       (int) connection_idx);
+                MSG_DEBUG(boost::format("Adding a connection between glue neuron  %1%[%2%] and glue neuron %3%[%4%], "
+                                        "connection index: %5%") % (int) (i + (di) * this->dim_inn)
+                                        % (int) (first_glue_neuron + i + (di) * this->dim_inn)
+                                        % (int) (i + (di + 1) * this->dim_inn) % (int) (first_glue_neuron + i + (di + 1)
+                                        % this->dim_inn) % (int) connection_idx);
                 new_net->add_existing_connection(first_glue_neuron + i + (di) * this->dim_inn,
                                                  first_glue_neuron + i + (di + 1) * this->dim_inn, connection_idx,
                                                  *this->solution);
             }
         }
-        printf("----------------------------------------------------------------------------------------------------\n");
+        MSG_DEBUG("------------------------------------------------------------------------------------------------\n");
 
         /* connection between the layer of glueing neurons toward the output neuron */
         pd_idx = partial_derivative_indices[derivative_degree - 1];/* partial derivative index */
         for (size_t i = 0; i < this->dim_inn; ++i) {
             connection_idx = pd_idx * this->dim_inn + i;
-            printf("  adding a connection between glue neuron %2d[%2d] and output neuron  %2d[%2d], connection index: %3d\n",
-                   (int) (i + (derivative_degree - 1) * this->dim_inn),
-                   (int) (first_glue_neuron + i + (derivative_degree - 1) * this->dim_inn), 0,
-                   (int) (first_output_neuron), (int) connection_idx);
+            MSG_DEBUG(boost::format("Adding a connection between glue neuron %1%[%2%] and output neuron  %3%[%4%], "
+                                    "connection index: %5%") % (int) (i + (derivative_degree - 1) * this->dim_inn)
+                                    % (int) (first_glue_neuron + i + (derivative_degree - 1) * this->dim_inn)
+                                    % 0 % (int) (first_output_neuron) % (int) connection_idx);
             new_net->add_existing_connection(first_glue_neuron + i + (derivative_degree - 1) * this->dim_inn,
                                              first_output_neuron, connection_idx, *this->solution);
         }
@@ -343,7 +348,8 @@ namespace lib4neuro {
 
     void DESolver::add_to_differential_equation(size_t equation_idx, std::string expression_string) {
 
-        printf("Adding a known function '%s' to equation %d\n", expression_string.c_str(), (int) equation_idx);
+        MSG_DEBUG(boost::format("Adding a known function '%1%' to equation %2%") % expression_string.c_str()
+                                % (int) equation_idx);
         this->differential_equations->at(equation_idx)->add_network(nullptr, expression_string);
 
     }
-- 
GitLab