MAiNGO
variableLister.h
Go to the documentation of this file.
1 /**********************************************************************************
2  * Copyright (c) 2019 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  **********************************************************************************/
11 
12 #pragma once
13 
14 #include "babOptVar.h"
15 
16 #include "symbol.hpp"
17 
18 
19 namespace maingo {
20 
21 
22 using namespace ale;
23 
24 
30 template <unsigned IDim>
31 std::string
32 var_indexes(size_t* indexes)
33 {
34  return '_' + std::to_string(indexes[0] + 1) + var_indexes<IDim - 1>(indexes + 1);
35 }
36 
42 template <>
43 inline std::string
44 var_indexes<1>(size_t* indexes)
45 {
46  return '_' + std::to_string(indexes[0] + 1);
47 }
48 
55 template <unsigned IDim>
56 std::string
57 var_name(std::string base, size_t* indexes)
58 {
59  return base + var_indexes<IDim>(indexes);
60 }
61 
67  public:
76  std::vector<OptimizationVariable>& variables,
77  std::vector<double>& initials,
78  std::unordered_map<std::string, int>& positions):
79  _variables(variables),
80  _initials(initials), _positions(positions)
81  {
82  }
83 
89  void dispatch(base_symbol* sym)
90  {
91  if (sym) {
92  return std::visit(*this, sym->get_base_variant());
93  }
94  }
95 
101  template <typename TType>
102  void operator()(value_symbol<TType>* sym)
103  {
104  }
105 
106 
107  template <unsigned IDim>
108  void operator()(value_symbol<real<IDim>>* sym)
109  {
110  return std::visit(*this, sym->get_value_variant());
111  }
112 
113 
114  template <unsigned IDim>
115  void operator()(parameter_symbol<real<IDim>>* sym)
116  {
117  }
118 
119  template <unsigned IDim>
120  void operator()(expression_symbol<real<IDim>>* sym)
121  {
122  }
123 
124  template <unsigned IDim>
125  void operator()(variable_symbol<real<IDim>>* sym)
126  {
127  for (int i = 0; i < IDim; ++i) {
128  if (sym->shape(i) == 0) {
129  return;
130  }
131  }
132  _positions[sym->m_name] = _variables.size();
133  size_t indexes[IDim];
134  for (int i = 0; i < IDim; ++i) {
135  indexes[i] = 0;
136  }
137  while (indexes[0] < sym->shape(0)) {
138  if (sym->lower()[indexes] == -std::numeric_limits<double>::infinity() || sym->upper()[indexes] == std::numeric_limits<double>::infinity()) {
139  throw MAiNGOException(" Error: VariableLister -- Entry of variable " + sym->m_name + " is unbounded");
140  }
141  maingo::VT vartype = VT_CONTINUOUS;
142  if (sym->integral()) {
143  if (ceil(sym->lower()[indexes]) == 0 && floor(sym->upper()[indexes]) == 1) {
144  vartype = VT_BINARY;
145  }
146  else {
147  vartype = VT_INTEGER;
148  }
149  }
150  double lower = sym->lower()[indexes];
151  double upper = sym->upper()[indexes];
152  _variables.push_back(
154  Bounds(lower, upper),
155  vartype,
156  var_name<IDim>(sym->m_name, indexes)));
157  double initial = sym->init()[indexes];
158  if (std::isnan(initial)) {
159  initial = 0.5 * (lower + upper);
160  }
161  _initials.push_back(initial);
162  for (int i = IDim - 1; i >= 0; --i) {
163  if (++indexes[i] < sym->shape(i)) {
164  break;
165  }
166  else if (i != 0) {
167  indexes[i] = 0;
168  }
169  }
170  }
171  }
172 
173 
174  void operator()(variable_symbol<real<0>>* sym)
175  {
176  if (sym->lower() == -std::numeric_limits<double>::infinity() || sym->upper() == std::numeric_limits<double>::infinity()) {
177  throw MAiNGOException(" Error: VariableLister -- Variable " + sym->m_name + " is unbounded");
178  }
179  _positions[sym->m_name] = _variables.size();
180  maingo::VT vartype = VT_CONTINUOUS;
181  if (sym->integral()) {
182  if (ceil(sym->lower()) == 0 && floor(sym->upper()) == 1) {
183  vartype = VT_BINARY;
184  }
185  else {
186  vartype = VT_INTEGER;
187  }
188  }
189  double lower = sym->lower();
190  double upper = sym->upper();
191  _variables.push_back(
193  Bounds(lower, upper),
194  vartype,
195  sym->m_name));
196  double initial = sym->init();
197  if (std::isnan(initial)) {
198  initial = 0.5 * (lower + upper);
199  }
200  _initials.push_back(initial);
201  }
204  private:
205  std::vector<OptimizationVariable>& _variables;
206  std::vector<double>& _initials;
207  std::unordered_map<std::string, int>& _positions;
208 };
209 
210 
211 } // namespace maingo
std::string var_indexes(size_t *indexes)
Function for serializing index sequences.
Definition: variableLister.h:32
void dispatch(base_symbol *sym)
Dispatch function.
Definition: variableLister.h:89
std::string var_indexes< 1 >(size_t *indexes)
Function for serializing index sequences.
Definition: variableLister.h:44
std::vector< OptimizationVariable > & _variables
Definition: variableLister.h:205
constexpr VT VT_INTEGER
Definition: MAiNGOmodel.h:79
void operator()(variable_symbol< real< IDim >> *sym)
Definition: variableLister.h:125
std::string var_name(std::string base, size_t *indexes)
Function for flattening indexed symbol names.
Definition: variableLister.h:57
std::unordered_map< std::string, int > & _positions
Definition: variableLister.h:207
Serializes a given symbol and lists it into a vector.
Definition: variableLister.h:66
void operator()(variable_symbol< real< 0 >> *sym)
Definition: variableLister.h:174
constexpr VT VT_CONTINUOUS
Definition: MAiNGOmodel.h:77
namespace holding all essentials of MAiNGO
Definition: aleModel.h:25
VariableLister(std::vector< OptimizationVariable > &variables, std::vector< double > &initials, std::unordered_map< std::string, int > &positions)
Constructor.
Definition: variableLister.h:75
void operator()(value_symbol< TType > *sym)
Definition: variableLister.h:102
babBase::OptimizationVariable OptimizationVariable
Definition: MAiNGOmodel.h:74
std::vector< double > & _initials
Definition: variableLister.h:206
VT
Enum for representing the Variable Type of an optimization variable as specified by the user...
Definition: babOptVar.h:43
void operator()(value_symbol< real< IDim >> *sym)
Definition: variableLister.h:108
This class defines the exceptions thrown by MAiNGO.
Definition: MAiNGOException.h:35
constexpr VT VT_BINARY
Definition: MAiNGOmodel.h:78
void operator()(expression_symbol< real< IDim >> *sym)
Definition: variableLister.h:120
babBase::Bounds Bounds
Definition: MAiNGOmodel.h:75
void operator()(parameter_symbol< real< IDim >> *sym)
Definition: variableLister.h:115