NOMAD Source  Version 4.0.0 Beta
BBInputType.cpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------------*/
2 /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct Search - */
3 /* */
4 /* NOMAD - Version 4.0.0 has been created by */
5 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
6 /* Christophe Tribes - Polytechnique Montreal */
7 /* */
8 /* The copyright of NOMAD - version 4.0.0 is owned by */
9 /* Charles Audet - Polytechnique Montreal */
10 /* Sebastien Le Digabel - Polytechnique Montreal */
11 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
12 /* Christophe Tribes - Polytechnique Montreal */
13 /* */
14 /* NOMAD v4 has been funded by Rio Tinto, Hydro-Québec, NSERC (Natural */
15 /* Sciences and Engineering Research Council of Canada), InnovÉÉ (Innovation */
16 /* en Énergie Électrique) and IVADO (The Institute for Data Valorization) */
17 /* */
18 /* NOMAD v3 was created and developed by Charles Audet, Sebastien Le Digabel, */
19 /* Christophe Tribes and Viviane Rochon Montplaisir and was funded by AFOSR */
20 /* and Exxon Mobil. */
21 /* */
22 /* NOMAD v1 and v2 were created and developed by Mark Abramson, Charles Audet, */
23 /* Gilles Couture, and John E. Dennis Jr., and were funded by AFOSR and */
24 /* Exxon Mobil. */
25 /* */
26 /* Contact information: */
27 /* Polytechnique Montreal - GERAD */
28 /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */
29 /* e-mail: nomad@gerad.ca */
30 /* */
31 /* This program is free software: you can redistribute it and/or modify it */
32 /* under the terms of the GNU Lesser General Public License as published by */
33 /* the Free Software Foundation, either version 3 of the License, or (at your */
34 /* option) any later version. */
35 /* */
36 /* This program is distributed in the hope that it will be useful, but WITHOUT */
37 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
38 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */
39 /* for more details. */
40 /* */
41 /* You should have received a copy of the GNU Lesser General Public License */
42 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
43 /* */
44 /* You can find information on the NOMAD software at www.gerad.ca/nomad */
45 /*---------------------------------------------------------------------------------*/
46 /**
47  \file BBInputType.cpp
48  \brief types for BBInput (implementation)
49  \author Viviane Rochon Montplaisir
50  \date December 2018
51  \see BBInputType.hpp
52  */
53 
54 #include "../Type/BBInputType.hpp"
55 #include "../Util/ArrayOfString.hpp"
56 #include "../Util/Exception.hpp"
57 #include "../Util/utils.hpp"
58 
59 // Convert a string ("R", "I", "B") to a NOMAD::BBInputType.
61 {
62  NOMAD::BBInputType ret = NOMAD::BBInputType::CONTINUOUS;
63  std::string s = sConst;
64  NOMAD::toupper(s);
65 
66  if (s == "R")
67  {
68  ret = NOMAD::BBInputType::CONTINUOUS;
69  }
70  else if (s == "*R")
71  {
72  ret = NOMAD::BBInputType::ALL_CONTINUOUS;
73  }
74  else if (s == "I")
75  {
76  ret = NOMAD::BBInputType::INTEGER;
77  }
78  else if (s == "*I")
79  {
80  ret = NOMAD::BBInputType::ALL_INTEGER;
81  }
82  else if (s == "B")
83  {
84  ret = NOMAD::BBInputType::BINARY;
85  }
86  else if (s == "*B")
87  {
88  ret = NOMAD::BBInputType::ALL_BINARY;
89  }
90  else
91  {
92  throw NOMAD::Exception(__FILE__, __LINE__, "Unrecognized string for NOMAD::BBInputType: " + s);
93  }
94 
95  return ret;
96 }
97 
98 
99 // Convert a string containing multiple BBInputTypes (ex "( R I B R ) or *I") to a NOMAD::BBInputTypeList.
100 // Supporting both classic version with parenthesis and modern version without parenthesis.
102 {
103  NOMAD::BBInputTypeList bbInputType;
104  NOMAD::ArrayOfString aos(s);
105  std::size_t arraysize = aos.size();
106  if (arraysize >= 2 && aos[0] == "(" && aos[arraysize-1] == ")")
107  {
108  // * is not supported inside the vector notation
109  if (s.find("*") < std::string::npos)
110  {
111  throw NOMAD::Exception(__FILE__, __LINE__, "Unrecognized string for NOMAD::BBInputType: " + s);
112  }
113 
114  aos.erase(arraysize-1);
115  aos.erase(0);
116  arraysize -= 2;
117 
118  for (size_t i = 0; i < arraysize; i++)
119  {
120  bbInputType.push_back(NOMAD::stringToBBInputType(aos[i]));
121  }
122  }
123 
124  // Manage the 'all of the same type (*)' situation
125  if (s.find("*") < std::string::npos)
126  {
127  // Concatenate all strings together before interpretation to BBInputType
128  std::string ss;
129  for (size_t i = 0; i < arraysize; i++)
130  {
131  ss+=aos[i];
132  }
133 
134  bbInputType.push_back(NOMAD::stringToBBInputType(ss));
135  }
136 
137  if (arraysize > 0 && bbInputType.size() == 0)
138  {
139  throw NOMAD::Exception(__FILE__, __LINE__, "Unrecognized string for NOMAD::BBInputType: " + s);
140  }
141 
142  return bbInputType;
143 }
144 
toupper
void toupper(std::string &s)
Put a string into upper cases.
Definition: utils.cpp:84
BBInputType
BBInputType
Enum for blackbox input type.
Definition: BBInputType.hpp:68
BBInputTypeList
std::vector< BBInputType > BBInputTypeList
Definition: BBInputType.hpp:80
stringToBBInputTypeList
BBInputTypeList stringToBBInputTypeList(const std::string &s)
Utility for BBInputTypes.
Definition: BBInputType.cpp:101
stringToBBInputType
BBInputType stringToBBInputType(const std::string &s)
Utility for BBInputTypes.
Definition: BBInputType.cpp:60