From da3e9b554a90861ea3a0b54b711ed4f6787b5f33 Mon Sep 17 00:00:00 2001
From: Martin Beseda <martin.beseda@vsb.cz>
Date: Tue, 18 Dec 2018 23:09:54 +0100
Subject: [PATCH] ENH: Added FileNotFoundException handling.

---
 src/examples/simulator.cpp | 129 +++++++++++++++++++------------------
 1 file changed, 67 insertions(+), 62 deletions(-)

diff --git a/src/examples/simulator.cpp b/src/examples/simulator.cpp
index a53d9c6e..785f0002 100644
--- a/src/examples/simulator.cpp
+++ b/src/examples/simulator.cpp
@@ -36,29 +36,29 @@ double get_rel_error(std::vector<double> &d1, std::vector<double> &d2){
 
 int main(int argc, char** argv){
 
-    /* Read data from the file */
-    l4n::CSVReader reader("/home/martin/Desktop/ANN_DATA_1_SET.txt", "\t", true);
-    reader.read();
+    try {
 
-    /* Create data set for both the training and testing of the neural network */
-    std::vector<unsigned int> inputs = {2,3,4,5,6,7,8,26,27,28};
-    std::vector<unsigned int> outputs = {17,18,19,20,21,22,23,24,25};
-    l4n::DataSet ds = reader.get_data_set(&inputs, &outputs);
+        /* Read data from the file */
+        l4n::CSVReader reader("/home/martin/5Desktop/ANN_DATA_1_SET.txt", "\t", true);
+        reader.read();
 
-    /* Normalize data in the set for easier training of the network */
-    ds.normalize();
+        /* Create data set for both the training and testing of the neural network */
+        std::vector<unsigned int> inputs = {2, 3, 4, 5, 6, 7, 8, 26, 27, 28};
+        std::vector<unsigned int> outputs = {17, 18, 19, 20, 21, 22, 23, 24, 25};
 
-    /* Neural network construction */
-    std::vector<unsigned int> neuron_numbers_in_layers = {10,10,10,9};
-    l4n::FullyConnectedFFN nn(&neuron_numbers_in_layers, l4n::NEURON_TYPE::LOGISTIC);
+        l4n::DataSet ds = reader.get_data_set(&inputs, &outputs);
 
-    /* Error function */
-    l4n::MSE mse(&nn, &ds);
+        /* Neural network construction */
+        std::vector<unsigned int> neuron_numbers_in_layers = {10, 10, 10, 9};
+        l4n::FullyConnectedFFN nn(&neuron_numbers_in_layers, l4n::NEURON_TYPE::LOGISTIC);
 
-    /* Domain */
-    std::vector<double> domain_bounds(2 * (nn.get_n_weights() + nn.get_n_biases()));
+        /* Error function */
+        l4n::MSE mse(&nn, &ds);
 
-    /* Training method */
+        /* Domain */
+        std::vector<double> domain_bounds(2 * (nn.get_n_weights() + nn.get_n_biases()));
+
+        /* Training method */
 //    for(size_t i = 0; i < domain_bounds.size() / 2; ++i){
 //        domain_bounds[2 * i] = -10;
 //        domain_bounds[2 * i + 1] = 10;
@@ -72,50 +72,55 @@ int main(int argc, char** argv){
 //                          0.7,
 //                          600,
 //                          1000);
-    l4n::GradientDescent gs(1e-3, 100, 100000);
-
-    nn.randomize_weights();
-
-    /* Cross - validation */
-    l4n::CrossValidator cv(&gs, &mse);
-    cv.run_k_fold_test(10, 1);
-
-    /* Save network to the file */
-    nn.save_text("test_net.4n");
-
-    /* Check of the saved network */
-    std::cout << std::endl << "The original network info:" << std::endl;
-    nn.print_stats();
-    nn.print_weights();
-
-    l4n::NeuralNetwork nn_loaded("test_net.4n");
-    std::cout << std::endl << "The loaded network info:" << std::endl;
-    nn_loaded.print_stats();
-    nn_loaded.print_weights();
-
-    /* Example of evaluation of a single input, normalized input, de-normalized output */
-    std::vector<double> input_norm(ds.get_input_dim()),
-            input(ds.get_input_dim()),
-            output_norm(ds.get_output_dim()),
-            expected_output_norm(ds.get_output_dim()),
-            output(ds.get_output_dim());
-
-    size_t data_idx = 0;
-    ds.get_input( input_norm, data_idx );
-    ds.get_output( expected_output_norm, data_idx );
-
-    nn_loaded.eval_single(input_norm, output_norm);
-
-    ds.de_normalize_single(output_norm, output);
-    ds.de_normalize_single(input_norm, input);
-
-    std::cout << std::endl << "input: ";
-    for(auto el: input_norm){std::cout << el << ", ";}
-    std::cout << std::endl;
-    std::cout << "output: ";
-    for(auto el: output){std::cout << el << ", ";}
-    std::cout << std::endl;
-    std::cout << "error of the " << data_idx << "-th element: "
-              << get_rel_error(output_norm, expected_output_norm) << std::endl;
+        l4n::GradientDescent gs(1e-3, 100, 100000);
+
+        nn.randomize_weights();
+
+        /* Cross - validation */
+        l4n::CrossValidator cv(&gs, &mse);
+        cv.run_k_fold_test(10, 1);
+
+        /* Save network to the file */
+        nn.save_text("test_net.4n");
+
+        /* Check of the saved network */
+        std::cout << std::endl << "The original network info:" << std::endl;
+        nn.print_stats();
+        nn.print_weights();
+
+        l4n::NeuralNetwork nn_loaded("test_net.4n");
+        std::cout << std::endl << "The loaded network info:" << std::endl;
+        nn_loaded.print_stats();
+        nn_loaded.print_weights();
+
+        /* Example of evaluation of a single input, normalized input, de-normalized output */
+        std::vector<double> input_norm(ds.get_input_dim()),
+                input(ds.get_input_dim()),
+                output_norm(ds.get_output_dim()),
+                expected_output_norm(ds.get_output_dim()),
+                output(ds.get_output_dim());
+
+        size_t data_idx = 0;
+        ds.get_input(input_norm, data_idx);
+        ds.get_output(expected_output_norm, data_idx);
+
+        nn_loaded.eval_single(input_norm, output_norm);
+
+        ds.de_normalize_single(output_norm, output);
+        ds.de_normalize_single(input_norm, input);
+
+        std::cout << std::endl << "input: ";
+        for (auto el: input_norm) { std::cout << el << ", "; }
+        std::cout << std::endl;
+        std::cout << "output: ";
+        for (auto el: output) { std::cout << el << ", "; }
+        std::cout << std::endl;
+        std::cout << "error of the " << data_idx << "-th element: "
+                  << get_rel_error(output_norm, expected_output_norm) << std::endl;
+
+    } catch(const lib4neuro::FileNotFoundException& e) {
+        std::cout << e.what();
+    }
+
     return 0;
 }
-- 
GitLab