Skip to content

Commit 88f0012

Browse files
Added more comments to CIOMap and CLookUp_ANN classes
1 parent 47b456c commit 88f0012

File tree

10 files changed

+395
-175
lines changed

10 files changed

+395
-175
lines changed

Common/include/toolboxes/multilayer_perceptron/CIOMap.hpp

Lines changed: 136 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,157 @@
3232
#include <limits>
3333
#include <cstdlib>
3434
#include <vector>
35-
#include "../../CConfig.hpp"
3635
#include "../../linear_algebra/blas_structure.hpp"
3736
#include "CLookUp_ANN.hpp"
3837

39-
using namespace std;
4038
namespace MLPToolbox
4139
{
4240

4341
class CLookUp_ANN;
4442

4543
class CIOMap
44+
/*!
45+
*\class CIOMap
46+
*\brief This class is used by the CLookUp_ANN class to assign user-defined inputs
47+
* and outputs to loaded multi-layer perceptrons. When a look-up operation is called
48+
* with a specific CIOMap, the multi-layer perceptrons are evaluated with input and
49+
* output variables coinciding with the desired input and output variable names.
50+
*
51+
*
52+
* For example, in a custom, data-driven fluid model, MLP's are used for thermodynamic state
53+
* definition. There are three MLP's loaded. MLP_1 predicts temperature and specific heat
54+
* based on density and energy. MLP_2 predicts pressure and speed of sound based on density and
55+
* energy as well. MLP_3 predicts density and energy based on pressure and temperature.
56+
* During a certain look-up operation in the CFluidModel, temperature, speed of sound and pressure
57+
* are needed for a given density and energy. What the CIOMap does is to point to MLP_1 for
58+
* temperature evalutation, and to MLP_2 for pressure and speed of sound evaluation. MLP_3 is
59+
* not considered, as the respective inputs and outputs don't match with the function call
60+
* inputs and outputs.
61+
*
62+
* call variables: MLP inputs: MLP outputs: call outputs:
63+
*
64+
* 2--> energy --| |--> temperature --> 1
65+
* |--> MLP_1 --|
66+
* 1:density 1--> density --| |--> c_p 1:temperature
67+
* 2:energy 2:speed of sound
68+
* 1--> density --| |--> pressure --> 3 3:pressure
69+
* |--> MLP_2 --|
70+
* 2--> energy --| |--> speed of sound --> 2
71+
*
72+
* pressure --| |--> density
73+
* |--> MLP_3 --|
74+
* temperature --| |--> energy
75+
*
76+
*
77+
* \author E.Bunschoten
78+
*/
79+
4680
{
4781
private:
48-
vector<size_t> ANN_indices;
49-
vector<vector<pair<size_t, size_t>>> Input_Map;
50-
vector<vector<pair<size_t, size_t>>> Output_Map;
51-
vector<vector<su2double>> input_indices;
52-
public:
53-
CIOMap(){};
54-
CIOMap(CLookUp_ANN*ANN_collection, vector<string> &inputs, vector<string> &outputs);
55-
~CIOMap(){};
56-
void Push_ANN_index(size_t i_ANN){ANN_indices.push_back(i_ANN);};
57-
void PairVariableswithANNs(CLookUp_ANN * ANN_collection, vector<string> &inputs, vector<string> &outputs);
58-
size_t GetNANNs(){return ANN_indices.size();}
59-
size_t GetANNIndex(size_t i_Map){return ANN_indices[i_Map];}
60-
size_t GetInputIndex(size_t i_Map, size_t iInput){return Input_Map[i_Map][iInput].first;}
61-
size_t GetOutputIndex(size_t i_Map, size_t iOutput){return Output_Map[i_Map][iOutput].first;}
62-
size_t GetANNOutputIndex(size_t i_Map, size_t iOutput){return Output_Map[i_Map][iOutput].second;}
63-
size_t GetNMappedOutputs(size_t i_Map){return Output_Map[i_Map].size();}
64-
65-
vector<pair<size_t, size_t>> GetOutputMapping(size_t i_map){return Output_Map[i_map];}
66-
vector<pair<size_t, size_t>> GetInputMapping(size_t i_map){return Input_Map[i_map];}
82+
std::vector<std::size_t> MLP_indices; /*!< Loaded MLP index */
83+
84+
std::vector<std::vector<std::pair<std::size_t, std::size_t>>>
85+
Input_Map, /*!< Mapping of call variable inputs to matching MLP inputs */
86+
Output_Map; /*!< Mapping of call variable outputs to matching MLP outputs */
87+
88+
std::vector<std::vector<su2double>> input_indices; /*!< */
6789

68-
vector<su2double> GetANN_Inputs(size_t i_Map, vector<su2double>&inputs){
69-
vector<su2double> ANN_input;
70-
ANN_input.resize(Input_Map[i_Map].size());
90+
public:
7191

72-
for(size_t iInput=0; iInput<Input_Map[i_Map].size(); iInput++){
73-
ANN_input.at(iInput) = inputs[GetInputIndex(i_Map, iInput)];
74-
}
75-
return ANN_input;
76-
}
77-
vector<su2double*> GetANN_Outputs(size_t i_Map, vector<su2double*>&outputs){
78-
vector<su2double*> ANN_output;
79-
ANN_output.resize(Output_Map[i_Map].size());
92+
/*!
93+
* \brief CIOMap class constructor;
94+
* \param[in] MLP_collection - Pointer to CLookUp_ANN class for which the input-output amap is to be created
95+
* \param[in] inputs - Input names for the call function. These should match with at least one of the MLP inputs.
96+
* \param[in] outputs - Output names for the call function. These should match with at least one of the MLP outputs.
97+
*/
98+
CIOMap(CLookUp_ANN*MLP_collection, std::vector<std::string> &inputs, std::vector<std::string> &outputs);
99+
100+
/*!
101+
* \brief Set MLP index in IO map
102+
* \param[in] i_MLP - loaded MLP index
103+
*/
104+
void Push_MLP_index(std::size_t i_MLP){MLP_indices.push_back(i_MLP);};
105+
106+
/*!
107+
* \brief Pair call inputs and outputs with the inputs and outputs of
108+
the loaded MLPs
109+
* \param[in] MLP_collection - pointer to MLP collection class
110+
* \param[in] inputs - vector with call input variable names
111+
* \param[in] outputs - vector with call output variable names
112+
*/
113+
void PairVariableswithMLPs(CLookUp_ANN * MLP_collection, std::vector<std::string> &inputs, std::vector<std::string> &outputs);
114+
115+
/*!
116+
* \brief Get the number of MLPs in the current IO map
117+
* \return number of MLPs with matching inputs and output(s)
118+
*/
119+
std::size_t GetNMLPs() const {return MLP_indices.size();}
120+
121+
/*!
122+
* \brief Get the loaded MLP index
123+
* \return MLP index
124+
*/
125+
std::size_t GetMLPIndex(std::size_t i_Map) const {return MLP_indices[i_Map];}
126+
127+
/*!
128+
* \brief Get the call input variable index
129+
* \param[in] i_Map - input-output mapping index of the IO map
130+
* \param[in] i_Input - input index of the call input variable
131+
* \return MLP input variable index
132+
*/
133+
std::size_t GetInputIndex(std::size_t i_Map, std::size_t i_Input) const {return Input_Map[i_Map][i_Input].first;}
134+
135+
/*!
136+
* \brief Get the call output variable index
137+
* \param[in] i_Map - input-output mapping index of the IO map
138+
* \param[in] i_Output - output index of the call input variable
139+
* \return call variable output index
140+
*/
141+
std::size_t GetOutputIndex(std::size_t i_Map, std::size_t i_Output) const {return Output_Map[i_Map][i_Output].first;}
142+
143+
/*!
144+
* \brief Get the MLP output variable index
145+
* \param[in] i_Map - input-output mapping index of the IO map
146+
* \param[in] i_Output - output index of the call input variable
147+
* \return MLP output variable index
148+
*/
149+
std::size_t GetMLPOutputIndex(std::size_t i_Map, std::size_t i_Output) const {return Output_Map[i_Map][i_Output].second;}
150+
151+
/*!
152+
* \brief Get the number of matching output variables between the call and MLP outputs
153+
* \param[in] i_Map - input-output mapping index of the IO map
154+
* \return Number of matching variables between the loaded MLP and call variables
155+
*/
156+
std::size_t GetNMappedOutputs(std::size_t i_Map) const {return Output_Map[i_Map].size();}
157+
158+
/*!
159+
* \brief Get the mapping of MLP outputs matching to call outputs
160+
* \param[in] i_Map - input-output mapping index of the IO map
161+
* \return Mapping of MLP output variables to call variables
162+
*/
163+
std::vector<std::pair<std::size_t, std::size_t>> GetOutputMapping(std::size_t i_map) const {return Output_Map[i_map];}
164+
165+
/*!
166+
* \brief Get the mapping of MLP inputs to call inputs
167+
* \param[in] i_Map - input-output mapping index of the IO map
168+
* \return Mapping of MLP input variables to call inputs
169+
*/
170+
std::vector<std::pair<std::size_t, std::size_t>> GetInputMapping(std::size_t i_map) const{return Input_Map[i_map];}
171+
172+
/*!
173+
* \brief Get the mapped inputs for the MLP at i_Map
174+
* \param[in] i_Map - input-output mapping index of the IO map
175+
* \param[in] inputs - call inputs
176+
* \return Vector with call inputs in the correct order of the loaded MLP
177+
*/
178+
std::vector<su2double> GetMLP_Inputs(std::size_t i_Map, std::vector<su2double>&inputs) const {
179+
std::vector<su2double> MLP_input;
180+
MLP_input.resize(Input_Map[i_Map].size());
80181

81-
for(size_t iOutput=0; iOutput<Output_Map[i_Map].size(); iOutput++){
82-
ANN_output.at(iOutput) = outputs[GetOutputIndex(i_Map, iOutput)];
182+
for(std::size_t i_Input=0; i_Input<Input_Map[i_Map].size(); i_Input++){
183+
MLP_input[i_Input] = inputs[GetInputIndex(i_Map, i_Input)];
83184
}
84-
return ANN_output;
185+
return MLP_input;
85186
}
86187
};
87188
}

Common/include/toolboxes/multilayer_perceptron/CLayer.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "../../CConfig.hpp"
3535
#include "CNeuron.hpp"
3636
#include "../../linear_algebra/blas_structure.hpp"
37-
using namespace std;
3837

3938
namespace MLPToolbox{
4039
class CLayer
@@ -43,7 +42,7 @@ class CLayer
4342
unsigned long number_of_neurons; /*!< Neuron count in current layer */
4443
CNeuron * neurons; /*!< Array of neurons in current layer */
4544
bool is_input; /*!< Input layer identifyer */
46-
string activation_type; /*!< Activation function type applied to the current layer*/
45+
std::string activation_type; /*!< Activation function type applied to the current layer*/
4746
public:
4847
CLayer();
4948
CLayer(unsigned long n_neurons);
@@ -77,49 +76,49 @@ class CLayer
7776
* \param[in] i_neuron - Neuron index
7877
* \param[in] output_value - Activation function output
7978
*/
80-
void setOutput(size_t i_neuron, su2double value){neurons[i_neuron].setOutput(value);}
79+
void setOutput(std::size_t i_neuron, su2double value){neurons[i_neuron].setOutput(value);}
8180

8281
/*!
8382
* \brief Get the output value of a neuron in the layer
8483
* \param[in] i_neuron - Neuron index
8584
* \return Neuron output value
8685
*/
87-
su2double getOutput(size_t i_neuron) const {return neurons[i_neuron].getOutput();}
86+
su2double getOutput(std::size_t i_neuron) const {return neurons[i_neuron].getOutput();}
8887

8988
/*!
9089
* \brief Set the input value of a neuron in the layer
9190
* \param[in] i_neuron - Neuron index
9291
* \param[in] input_value - Activation function input
9392
*/
94-
void setInput(size_t i_neuron, su2double value){neurons[i_neuron].setInput(value);}
93+
void setInput(std::size_t i_neuron, su2double value){neurons[i_neuron].setInput(value);}
9594

9695
/*!
9796
* \brief Get the input value of a neuron in the layer
9897
* \param[in] i_neuron - Neuron index
9998
* \return Neuron input value
10099
*/
101-
su2double getInput(size_t i_neuron) const {return neurons[i_neuron].getInput();}
100+
su2double getInput(std::size_t i_neuron) const {return neurons[i_neuron].getInput();}
102101

103102
/*!
104103
* \brief Set the bias value of a neuron in the layer
105104
* \param[in] i_neuron - Neuron index
106105
* \param[in] bias_value - Bias value
107106
*/
108-
void setBias(size_t i_neuron, su2double value){neurons[i_neuron].setBias(value);}
107+
void setBias(std::size_t i_neuron, su2double value){neurons[i_neuron].setBias(value);}
109108

110109
/*!
111110
* \brief Get the bias value of a neuron in the layer
112111
* \param[in] i_neuron - Neuron index
113112
* \return Neuron bias value
114113
*/
115-
su2double getBias(size_t i_neuron){return neurons[i_neuron].getBias();}
114+
su2double getBias(std::size_t i_neuron){return neurons[i_neuron].getBias();}
116115

117116
/*!
118117
* \brief Get the output-input gradient of a neuron in the layer
119118
* \param[in] i_neuron - Neuron index
120119
* \return Gradient of neuron output wrt input
121120
*/
122-
su2double getdYdX(size_t i_neuron){return neurons[i_neuron].getGradient();}
121+
su2double getdYdX(std::size_t i_neuron){return neurons[i_neuron].getGradient();}
123122

124123
/*!
125124
* \brief Get the activation function name applied to this layer

Common/include/toolboxes/multilayer_perceptron/CLookUp_ANN.hpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,26 @@
3838
#include "CNeuralNetwork.hpp"
3939
#include "CIOMap.hpp"
4040

41-
using namespace std;
4241

4342
namespace MLPToolbox{
44-
class CIOMap;
4543

4644
class CLookUp_ANN
4745
{
46+
/*!
47+
*\class CLookUp_ANN
48+
*\brief This class allows for the evaluation of one or more multi-layer perceptrons in for example
49+
* thermodynamic state look-up operations. The multi-layer perceptrons are loaded in the order listed
50+
* in the MLP collection file. Each multi-layer perceptron is generated based on the
51+
* architecture described in its respective input file. When evaluating the MLP collection, an
52+
* input-output map is used to find the correct MLP corresponding to the call function inputs and
53+
* outputs.
54+
*/
55+
4856
private:
4957
int rank{0};
50-
vector<CNeuralNetwork*> NeuralNetworks; /*!< Vector containing all loaded neural networks. */
58+
std::vector<CNeuralNetwork*> NeuralNetworks; /*!< std::vector containing all loaded neural networks. */
5159

52-
vector<string> ANN_filenames; /*!< Filenames containing ANN architecture information. */
60+
std::vector<std::string> ANN_filenames; /*!< Filenames containing ANN architecture information. */
5361

5462
unsigned long number_of_variables; /*!< Number of loaded ANNs. */
5563

@@ -59,75 +67,75 @@ class CLookUp_ANN
5967
* \param[in] flag - line to search for
6068
* \return line in file.
6169
*/
62-
string SkipToFlag(ifstream *file_stream, string flag);
70+
std::string SkipToFlag(ifstream *file_stream, std::string flag);
6371

6472
/*!
6573
* \brief Load ANN architecture
6674
* \param[in] ANN - pointer to target NeuralNetwork class
6775
* \param[in] filename - filename containing ANN architecture information
6876
*/
69-
void GenerateANN(CNeuralNetwork*ANN, string filename);
77+
void GenerateANN(CNeuralNetwork*ANN, std::string filename);
7078

7179
/*!
7280
* \brief Read ANN architecture input file
7381
* \param[in] filename - filename containing ANN architecture information
7482
*/
75-
void ReadANNInputFile(string fileName);
83+
void ReadANNInputFile(std::string fileName);
7684

7785
public:
7886

7987
/*!
8088
* \brief ANN collection class constructor
8189
* \param[in] filename - filename containing list of ANN input files
8290
*/
83-
CLookUp_ANN(string fileName);
91+
CLookUp_ANN(std::string fileName);
8492

8593
/*!
8694
* \brief Evaluate loaded ANNs for given inputs and outputs
8795
* \param[in] input_output_map - input-output map coupling desired inputs and outputs to loaded ANNs
8896
* \param[in] inputs - input values
8997
* \param[in] outputs - pointers to output variables
9098
*/
91-
void Predict_ANN(CIOMap *input_output_map, vector<su2double> &inputs, vector<su2double*> &outputs);
99+
void Predict_ANN(CIOMap *input_output_map, std::vector<su2double> &inputs, std::vector<su2double*> &outputs);
92100

93-
~CLookUp_ANN(){for(size_t i_ANN=0; i_ANN<number_of_variables; i_ANN++)
101+
~CLookUp_ANN(){for(std::size_t i_ANN=0; i_ANN<number_of_variables; i_ANN++)
94102
delete NeuralNetworks.at(i_ANN);
95103
};
96104

97105
/*!
98106
* \brief Get number of loaded ANNs
99107
* \return number of loaded ANNs
100108
*/
101-
size_t GetNANNs() const {return NeuralNetworks.size();}
109+
std::size_t GetNANNs() const {return NeuralNetworks.size();}
102110

103111
/*!
104112
* \brief Check if variables occur more than once in ANN outputs
105113
* \param[in] output_names - output variable names to check
106114
* \param[in] input_output_map - pointer to input-output map to be checked
107115
*/
108-
bool Check_Duplicate_Outputs(vector<string> &output_names, CIOMap *input_output_map) const;
116+
bool Check_Duplicate_Outputs(std::vector<std::string> &output_names, CIOMap *input_output_map) const;
109117

110118
/*!
111119
* \brief Check if all output variables are present in the loaded ANNs
112120
* \param[in] output_names - output variable names to check
113121
* \param[in] input_output_map - pointer to input-output map to be checked
114122
*/
115-
bool Check_Use_of_Outputs(vector<string> &output_names, CIOMap *input_output_map) const;
123+
bool Check_Use_of_Outputs(std::vector<std::string> &output_names, CIOMap *input_output_map) const;
116124

117125
/*!
118126
* \brief Check if all input variables are present in the loaded ANNs
119127
* \param[in] input_names - input variable names to check
120128
* \param[in] input_output_map - pointer to input-output map to be checked
121129
*/
122-
bool Check_Use_of_Inputs(vector<string> &input_names, CIOMap *input_output_map) const;
130+
bool Check_Use_of_Inputs(std::vector<std::string> &input_names, CIOMap *input_output_map) const;
123131

124132
/*!
125133
* \brief Map variable names to ANN inputs or outputs
126134
* \param[in] i_ANN - loaded ANN index
127135
* \param[in] variable_names - variable names to map to ANN inputs or outputs
128136
* \param[in] input - map to inputs (true) or outputs (false)
129137
*/
130-
vector<pair<size_t, size_t>> FindVariable_Indices(size_t i_ANN, vector<string> variable_names, bool input) const;
138+
std::vector<pair<std::size_t, std::size_t>> FindVariable_Indices(std::size_t i_ANN, std::vector<std::string> variable_names, bool input) const;
131139

132140
};
133141

0 commit comments

Comments
 (0)