1 //   OpenNN: Open Neural Networks Library
2 //   www.opennn.net
3 //
4 //   R E S P O N S E   O P T I M I Z A T I O N   C L A S S   H E A D E R
5 //
6 //   Artificial Intelligence Techniques SL
7 //   artelnics@artelnics.com
8 
9 #ifndef RESPONSEOPTIMIZATION_H
10 #define RESPONSEOPTIMIZATION_H
11 
12 // System includes
13 
14 #include <cmath>
15 #include <cstdlib>
16 #include <fstream>
17 #include <iostream>
18 #include <string>
19 #include <sstream>
20 
21 // OpenNN includes
22 
23 #include "config.h"
24 #include "neural_network.h"
25 
26 namespace OpenNN
27 {
28 
29 /// This class is used to optimize model response identify the combinations_2d of variable settings jointly optimize a set of responses.
30 
31 ///
32 /// This tool is adequate when you need to know the behaviour of a multiple variables on a response
33 /// and satisfy the requirements of the architecture.
34 
35 class ResponseOptimization
36 {
37 
38 public:
39 
40    // DEFAULT CONSTRUCTOR
41 
42     explicit ResponseOptimization();
43 
44     explicit ResponseOptimization(NeuralNetwork*);
45 
46    virtual ~ResponseOptimization();
47 
48     ///Enumeration of available conditions for response optimization.
49 
50    enum Condition{Between, EqualTo, LessEqualTo, GreaterEqualTo, Minimum, Maximum};
51 
52    ///
53    /// This structure returns the results obtained in the optimization, e.g. optimum inputs number, etc.
54    ///
55 
56    struct Results
57    {
58        /// Default constructor.
59 
ResultsResults60        explicit Results(NeuralNetwork* new_neural_network_pointer)
61        {
62            neural_network_pointer = new_neural_network_pointer;
63        }
64 
~ResultsResults65        virtual ~Results(){}
66 
67        NeuralNetwork* neural_network_pointer = nullptr;
68 
69        Tensor<type, 1> optimal_variables;
70 
71        type optimum_objective;
72 
printResults73        void print() const
74        {
75            const Index inputs_number = neural_network_pointer->get_inputs_number();
76            const Index outputs_number = neural_network_pointer->get_outputs_number();
77 
78            const Tensor<string, 1> inputs_names = neural_network_pointer->get_inputs_names();
79            const Tensor<string, 1> outputs_names = neural_network_pointer->get_outputs_names();
80 
81            for(Index i = 0; i < inputs_number; i++)
82            {
83                cout << inputs_names[i] << ": " << optimal_variables[i] << endl;
84            }
85 
86            for(Index i = 0; i < outputs_number; i++)
87            {
88                cout << outputs_names[i] << " " << optimal_variables[inputs_number+i] << endl;
89            }
90 
91            cout << "Objective: " << optimum_objective << endl;
92        }
93    };
94 
95    // Get methods
96 
97    Tensor<Condition, 1> get_inputs_conditions();
98    Tensor<Condition, 1> get_outputs_conditions();
99 
100    Tensor<type, 1> get_inputs_minimums();
101    Tensor<type, 1> get_inputs_maximums();
102    Tensor<type, 1> get_outputs_minimums();
103    Tensor<type, 1> get_outputs_maximums();
104 
105    // Set methods
106 
107    void set_evaluations_number(const Index&);
108 
109    void set_input_condition(const string&, const Condition&, const Tensor<type, 1>& = Tensor<type, 1>());
110    void set_output_condition(const string&, const Condition&, const Tensor<type, 1>& = Tensor<type, 1>());
111 
112    void set_input_condition(const Index&, const Condition&, const Tensor<type, 1>& = Tensor<type, 1>());
113    void set_output_condition(const Index&, const Condition&, const Tensor<type, 1>& = Tensor<type, 1>());
114 
115    void set_inputs_outputs_conditions(const Tensor<string, 1>&, const Tensor<string, 1>&, const Tensor<type, 1>& = Tensor<type, 1>());
116 
117    Tensor<Condition, 1> get_conditions(const Tensor<string, 1>&) const;
118    Tensor<Tensor<type, 1>, 1> get_values_conditions(const Tensor<Condition, 1>&, const Tensor<type, 1>&) const;
119 
120    Tensor<type, 2> calculate_inputs() const;
121 
122    Tensor<type, 2> calculate_envelope(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
123 
124    Results* perform_optimization() const;
125 
126 private:
127 
128     NeuralNetwork* neural_network_pointer = nullptr;
129 
130     Tensor<Condition, 1> inputs_conditions;
131     Tensor<Condition, 1> outputs_conditions;
132 
133     Tensor<type, 1> inputs_minimums;
134     Tensor<type, 1> inputs_maximums;
135 
136     Tensor<type, 1> outputs_minimums;
137     Tensor<type, 1> outputs_maximums;
138 
139     Index evaluations_number = 1000;
140 
141     type calculate_random_uniform(const type&, const type&) const;
142 
143 };
144 
145 }
146 
147 #endif
148 
149 
150 // OpenNN: Open Neural Networks Library.
151 // Copyright(C) 2005-2020 Artificial Intelligence Techniques, SL.
152 //
153 // This library is free software; you can redistribute it and/or
154 // modify it under the terms of the GNU Lesser General Public
155 // License as published by the Free Software Foundation; either
156 // version 2.1 of the License, or any later version.
157 //
158 // This library is distributed in the hope that it will be useful,
159 // but WITHOUT ANY WARRANTY; without even the implied warranty of
160 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
161 // Lesser General Public License for more details.
162 
163 // You should have received a copy of the GNU Lesser General Public
164 // License along with this library; if not, write to the Free Software
165 
166 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
167 
168