Added classes to hendle bounds:
- eoGeneralRealBound that can be initialized using a string (and hence can be easily read as a parameter) - eoIntBound and all other integer-equivalent of the classes in eoRealBound.h Note that there is no equivalent to eoRealVectorBounds for vector of integers
This commit is contained in:
parent
50b395d16d
commit
7a695e65df
5 changed files with 1078 additions and 62 deletions
|
|
@ -17,6 +17,7 @@
|
|||
#include "eoRealBounds.h"
|
||||
#include "eoRealVectorBounds.h"
|
||||
|
||||
|
||||
// the global dummy bounds
|
||||
// (used for unbounded variables when bounds are required)
|
||||
eoRealNoBounds eoDummyRealNoBounds;
|
||||
|
|
@ -46,14 +47,14 @@ double read_double(std::string _s)
|
|||
return r;
|
||||
}
|
||||
|
||||
int read_int(std::string _s)
|
||||
long int read_int(std::string _s)
|
||||
{
|
||||
#ifdef HAVE_SSTREAM
|
||||
std::istringstream is(_s);
|
||||
#else
|
||||
std::istrstream is(_s.c_str());
|
||||
#endif
|
||||
int i;
|
||||
long int i;
|
||||
is >> i;
|
||||
return i;
|
||||
}
|
||||
|
|
@ -191,3 +192,80 @@ void eoRealVectorBounds::adjust_size(unsigned _dim)
|
|||
factor[factor.size()-1] += missing;
|
||||
}
|
||||
}
|
||||
|
||||
/** the constructor for eoGeneralRealBound - from a string
|
||||
* very similar to the eoRealVectorBounds::readFrom above
|
||||
* but was written much later so the readFrom does not call this one
|
||||
* as it should do
|
||||
*/
|
||||
eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value)
|
||||
{
|
||||
// now read
|
||||
std::string delim(",; ");
|
||||
std::string beginOrClose("[(])");
|
||||
if (!remove_leading(_value, delim)) // only delimiters were left
|
||||
throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor");
|
||||
|
||||
// look for opening char
|
||||
size_t posDeb = _value.find_first_of(beginOrClose); // allow ]a,b]
|
||||
if (posDeb >= _value.size()) // nothing left to read
|
||||
throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor");
|
||||
|
||||
// ending char: next {}() after posDeb
|
||||
size_t posFin = _value.find_first_of(beginOrClose,posDeb+1);
|
||||
if (posFin >= _value.size()) // not found
|
||||
throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor");
|
||||
|
||||
// the bounds
|
||||
std::string sBounds = _value.substr(posDeb+1, posFin-posDeb-1);
|
||||
// and remove from original string
|
||||
_value = _value.substr(posFin+1);
|
||||
|
||||
remove_leading(sBounds, delim);
|
||||
size_t posDelim = sBounds.find_first_of(delim);
|
||||
if (posDelim >= sBounds.size())
|
||||
throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor");
|
||||
|
||||
bool minBounded=false, maxBounded=false;
|
||||
double minBound=0, maxBound=0;
|
||||
|
||||
// min bound
|
||||
std::string sMinBounds = sBounds.substr(0,posDelim);
|
||||
|
||||
if ( (sMinBounds != std::string("-inf")) &&
|
||||
(sMinBounds != std::string("-infinity"))
|
||||
)
|
||||
{
|
||||
minBounded = true;
|
||||
minBound = read_double(sMinBounds);
|
||||
}
|
||||
|
||||
// max bound
|
||||
size_t posEndDelim = sBounds.find_first_not_of(delim,posDelim);
|
||||
|
||||
std::string sMaxBounds = sBounds.substr(posEndDelim);
|
||||
|
||||
if ( (sMaxBounds != std::string("+inf")) &&
|
||||
(sMaxBounds != std::string("+infinity"))
|
||||
)
|
||||
{
|
||||
maxBounded = true;
|
||||
maxBound = read_double(sMaxBounds);
|
||||
}
|
||||
|
||||
// now create the embedded eoRealBounds object
|
||||
eoRealBounds * locBound;
|
||||
if (minBounded && maxBounded)
|
||||
{
|
||||
if (maxBound <= minBound)
|
||||
throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor");
|
||||
locBound = new eoRealInterval(minBound, maxBound);
|
||||
}
|
||||
else if (!minBounded && !maxBounded) // no bound at all
|
||||
locBound = new eoRealNoBounds;
|
||||
else if (!minBounded && maxBounded)
|
||||
locBound = new eoRealAboveBound(maxBound);
|
||||
else if (minBounded && !maxBounded)
|
||||
locBound = new eoRealBelowBound(minBound);
|
||||
return locBound;
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue