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  * @file variableLister.h
11  *
12  * @brief File declaring the VariableLister class that lists given symbols
13  * as OptimizationVariable objects.
14  *
15  **********************************************************************************/
16 
17 #pragma once
18 
19 #include "babOptVar.h"
20 
21 #include "symbol.hpp"
22 
23 
24 namespace maingo {
25 
26 
27 using namespace ale;
28 
29 
35 template <unsigned IDim>
36 std::string
37 var_indexes(size_t* indexes)
38 {
39  return '_' + std::to_string(indexes[0] + 1) + var_indexes<IDim - 1>(indexes + 1);
40 }
41 
47 template <>
48 inline std::string
49 var_indexes<1>(size_t* indexes)
50 {
51  return '_' + std::to_string(indexes[0] + 1);
52 }
53 
60 template <unsigned IDim>
61 std::string
62 var_name(std::string base, size_t* indexes)
63 {
64  return base + var_indexes<IDim>(indexes);
65 }
66 
72  public:
81  std::vector<OptimizationVariable>& variables,
82  std::vector<double>& initials,
83  std::unordered_map<std::string, int>& positions):
84  _variables(variables),
85  _initials(initials), _positions(positions)
86  {
87  }
88 
94  void dispatch(base_symbol* sym)
95  {
96  return std::visit(*this, sym->get_base_variant());
97  }
98 
104  template <typename TType>
105  void operator()(value_symbol<TType>* sym)
106  {
107  }
108 
109 
110  template <unsigned IDim>
111  void operator()(value_symbol<real<IDim>>* sym)
112  {
113  return std::visit(*this, sym->get_value_variant());
114  }
115 
116 
117  template <unsigned IDim>
118  void operator()(parameter_symbol<real<IDim>>* sym)
119  {
120  }
121 
122 
123  template <unsigned IDim>
124  void operator()(variable_symbol<real<IDim>>* sym)
125  {
126  for (int i = 0; i < IDim; ++i) {
127  if (sym->shape(i) == 0) {
128  return;
129  }
130  }
131  _positions[sym->m_name] = _variables.size();
132  size_t indexes[IDim];
133  for (int i = 0; i < IDim; ++i) {
134  indexes[i] = 0;
135  }
136  while (indexes[0] < sym->shape(0)) {
137  if (sym->lower()[indexes] == -std::numeric_limits<double>::infinity() || sym->upper()[indexes] == std::numeric_limits<double>::infinity()) {
138  throw MAiNGOException(" Error: VariableLister -- Entry of variable " + sym->m_name + " is unbounded");
139  }
140  maingo::VT vartype = VT_CONTINUOUS;
141  if (sym->integral()) {
142  if (ceil(sym->lower()[indexes]) == 0 && floor(sym->upper()[indexes]) == 1) {
143  vartype = VT_BINARY;
144  }
145  else {
146  vartype = VT_INTEGER;
147  }
148  }
149  double lower = sym->lower()[indexes];
150  double upper = sym->upper()[indexes];
151  _variables.push_back(
153  Bounds(lower, upper),
154  vartype,
155  var_name<IDim>(sym->m_name, indexes)));
156  double initial = sym->init()[indexes];
157  if (std::isnan(initial)) {
158  initial = 0.5 * (lower + upper);
159  }
160  _initials.push_back(initial);
161  for (int i = IDim - 1; i >= 0; --i) {
162  if (++indexes[i] < sym->shape(i)) {
163  break;
164  }
165  else if (i != 0) {
166  indexes[i] = 0;
167  }
168  }
169  }
170  }
171 
172 
173  void operator()(variable_symbol<real<0>>* sym)
174  {
175  if (sym->lower() == -std::numeric_limits<double>::infinity() || sym->upper() == std::numeric_limits<double>::infinity()) {
176  throw MAiNGOException(" Error: VariableLister -- Variable " + sym->m_name + " is unbounded");
177  }
178  _positions[sym->m_name] = _variables.size();
179  maingo::VT vartype = VT_CONTINUOUS;
180  if (sym->integral()) {
181  if (ceil(sym->lower()) == 0 && floor(sym->upper()) == 1) {
182  vartype = VT_BINARY;
183  }
184  else {
185  vartype = VT_INTEGER;
186  }
187  }
188  double lower = sym->lower();
189  double upper = sym->upper();
190  _variables.push_back(
192  Bounds(lower, upper),
193  vartype,
194  sym->m_name));
195  double initial = sym->init();
196  if (std::isnan(initial)) {
197  initial = 0.5 * (lower + upper);
198  }
199  _initials.push_back(initial);
200  }
203  private:
204  std::vector<OptimizationVariable>& _variables;
205  std::vector<double>& _initials;
206  std::unordered_map<std::string, int>& _positions;
207 };
208 
209 
210 } // namespace maingo
std::string var_indexes(size_t *indexes)
Function for serializing index sequences.
Definition: variableLister.h:37
void dispatch(base_symbol *sym)
Dispatch function.
Definition: variableLister.h:94
std::string var_indexes< 1 >(size_t *indexes)
Function for serializing index sequences.
Definition: variableLister.h:49
std::vector< OptimizationVariable > & _variables
Definition: variableLister.h:204
constexpr VT VT_INTEGER
Definition: MAiNGOmodel.h:83
void operator()(variable_symbol< real< IDim >> *sym)
Definition: variableLister.h:124
std::string var_name(std::string base, size_t *indexes)
Function for flattening indexed symbol names.
Definition: variableLister.h:62
std::unordered_map< std::string, int > & _positions
Definition: variableLister.h:206
Serializes a given symbol and lists it into a vector.
Definition: variableLister.h:71
void operator()(variable_symbol< real< 0 >> *sym)
Definition: variableLister.h:173
constexpr VT VT_CONTINUOUS
Definition: MAiNGOmodel.h:81
namespace holding all essentials of MAiNGO
Definition: aleModel.h:31
VariableLister(std::vector< OptimizationVariable > &variables, std::vector< double > &initials, std::unordered_map< std::string, int > &positions)
Constructor.
Definition: variableLister.h:80
void operator()(value_symbol< TType > *sym)
Definition: variableLister.h:105
babBase::OptimizationVariable OptimizationVariable
Definition: MAiNGOmodel.h:78
std::vector< double > & _initials
Definition: variableLister.h:205
VT
Enum for representing the Variable Type of an optimization variable as specified by the user.
Definition: babOptVar.h:40
void operator()(value_symbol< real< IDim >> *sym)
Definition: variableLister.h:111
This class defines the exceptions thrown by MAiNGO.
Definition: exceptions.h:39
constexpr VT VT_BINARY
Definition: MAiNGOmodel.h:82
babBase::Bounds Bounds
Definition: MAiNGOmodel.h:79
void operator()(parameter_symbol< real< IDim >> *sym)
Definition: variableLister.h:118