MeLOn
MeLOn.h
Go to the documentation of this file.
1 /**********************************************************************************
2 * Copyright (c) 2020 Process Systems Engineering (AVT.SVT), RWTH Aachen University
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * SPDX-License-Identifier: EPL-2.0
9 *
10 * @file MeLOn.h
11 *
12 * @brief File containing declaration of the MelonModel class.
13 *
14 **********************************************************************************/
15 
16 #pragma once
17 
18 #include <memory> // std::shared_ptr, std::unique_ptr
19 #include <vector> // std::vector
20 
21 #include "modelParser.h"
22 #include "modelData.h"
23 #include "exceptions.h"
24 
25 namespace melon {
26 
27  /*
28  * @class MelonModel
29  * @brief This class is a abstract parent class for models implemented in the MeLOn library
30  */
31  template<typename T>
32  class MelonModel {
33  public:
37  virtual ~MelonModel() = default;
38 
46  void load_model(std::string modelName, MODEL_FILE_TYPE fileType);
47 
57  void load_model(std::string modelPath, std::string modelName, MODEL_FILE_TYPE fileType);
58 
64  void load_model(std::shared_ptr<const ModelData> modelData);
65 
66  protected:
67 
68  bool _modelLoaded { false };
69  std::shared_ptr<ModelParserFactory> _parserFactory;
76  MelonModel(std::shared_ptr<ModelParserFactory> parserFactory) : _parserFactory(parserFactory) {};
77 
83  virtual void _set_data_object(std::shared_ptr<const ModelData> modelData) = 0;
84 
94  template <typename RandomAccessIterator>
95  void _set_constraints(std::vector<T>& constraints, std::vector<T>& constraintEvaluation, RandomAccessIterator& constraintValue);
96 
106  template <typename RandomAccessIterator>
107  void _set_constraints(std::vector<T>& constraints, T& constraintEvaluation, RandomAccessIterator& constraintValue);
108  };
109 
110 
112  // Function wich loads model from file modelName
113  template <typename T>
114  void MelonModel<T>::load_model(std::string modelName, MODEL_FILE_TYPE fileType) {
115  load_model("", modelName, fileType);
116  }
117 
118 
120  // Constructor which loads model from file "modelName" located in "filePat"h
121  template <typename T>
122  void MelonModel<T>::load_model(std::string modelPath, std::string modelName, MODEL_FILE_TYPE fileType) {
123 
124  std::shared_ptr<ModelData> modelData;
125 
126  try {
127  std::unique_ptr<ModelParser> parser = _parserFactory->create_model_parser(fileType);
128  modelData = parser->parse_model(modelPath, modelName);
129  _set_data_object(modelData);
130  }
131  catch (const std::exception& e) {
132  throw(MelonException(" Encountered a fatal error while loading model from file. Terminating.", e));
133  }
134  catch (...) {
135  throw(MelonException(" Encountered a fatal error while loading model from file. Terminating."));
136  }
137 
138  _modelLoaded = true;
139 
140  }
141 
142 
144  // Constructor which loads model from ModelData object
145  template <typename T>
146  void MelonModel<T>::load_model(std::shared_ptr<const ModelData> modelData) {
147 
148  try {
149  _set_data_object(modelData);
150  }
151  catch (std::exception &e) {
152  throw(MelonException(" Encountered a fatal error while loading model from data object. Terminating.", e));
153  }
154  catch (...) {
155  throw(MelonException(" Encountered a fatal error while loading model from data object. Terminating."));
156  }
157 
158  _modelLoaded = true;
159  }
160 
162  // Sets constraints required for fullspace opimization
163  template <typename T>
164  template <typename RandomAccessIterator>
165  void MelonModel<T>::_set_constraints(std::vector<T>& constraints, std::vector<T>& constraintEvaluation, RandomAccessIterator& constraintValue) {
166  for (auto& i : constraintEvaluation) {
167  _set_constraints(constraints, i, constraintValue);
168  }
169  }
170 
171 
173  // Sets constraints required for fullspace opimization
174  template <typename T>
175  template <typename RandomAccessIterator>
176  void MelonModel<T>::_set_constraints(std::vector<T>& constraints, T& constraintEvaluation, RandomAccessIterator& constraintValue) {
177  T cv = *constraintValue;
178  T constraint = constraintEvaluation - cv;
179  constraints.push_back(constraint);
180 
181  // Set normalized Input to the value provided by the solver for further calculations
182  constraintEvaluation = *constraintValue;
183  constraintValue++;
184  }
185 
186 }
melon::MODEL_FILE_TYPE
MODEL_FILE_TYPE
Enum for representing the parsable filetypes.
Definition: modelParser.h:43
exceptions.h
melon::MelonModel::_parserFactory
std::shared_ptr< ModelParserFactory > _parserFactory
Definition: MeLOn.h:95
melon::MelonModel::~MelonModel
virtual ~MelonModel()=default
Default destructor.
modelData.h
melon::MelonModel::_modelLoaded
bool _modelLoaded
Definition: MeLOn.h:94
melon::MelonModel::load_model
void load_model(std::string modelName, MODEL_FILE_TYPE fileType)
Loads new model from file.
Definition: MeLOn.h:127
melon::MelonModel::_set_data_object
virtual void _set_data_object(std::shared_ptr< const ModelData > modelData)=0
Sets data object containing model parameters.
MelonException
This class defines the exceptions thrown by FeedForwardNet.
Definition: exceptions.h:32
melon::MelonModel::MelonModel
MelonModel(std::shared_ptr< ModelParserFactory > parserFactory)
Constructor.
Definition: MeLOn.h:102
melon
Definition: kernel.h:21
melon::MelonModel::_set_constraints
void _set_constraints(std::vector< T > &constraints, std::vector< T > &constraintEvaluation, RandomAccessIterator &constraintValue)
Sets constraints required for fullspace opimization.
Definition: MeLOn.h:178
modelParser.h