def test_sub1(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available') x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] for xl, xu in x_bounds: for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var(bounds=(xl, xu)) m.y = pyo.Var() m.c = pyo.Constraint( expr=pyo.inequality(body=m.x - m.y, lower=cl, upper=cu)) self.tightener(m) x = np.linspace(pyo.value(m.x.lb), pyo.value(m.x.ub), 100) z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper), 100) if m.y.lb is None: yl = -np.inf else: yl = m.y.lb if m.y.ub is None: yu = np.inf else: yu = m.y.ub for _x in x: _y = _x - z self.assertTrue(np.all(yl <= _y)) self.assertTrue(np.all(yu >= _y))
def test_sub2(self): x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] for xl, xu in x_bounds: for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var(bounds=(xl, xu)) m.y = pyo.Var() m.c = pyo.Constraint( expr=pyo.inequality(body=m.y - m.x, lower=cl, upper=cu)) fbbt(m) x = np.linspace(pyo.value(m.x.lb), pyo.value(m.x.ub), 100) z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper), 100) if m.y.lb is None: yl = -np.inf else: yl = m.y.lb if m.y.ub is None: yu = np.inf else: yu = m.y.ub for _x in x: _y = z + _x self.assertTrue(np.all(yl <= _y)) self.assertTrue(np.all(yu >= _y))
def test_pow(self): x_bounds = [(np.random.uniform(0, 2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (0, 0)] y_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (np.random.uniform(0, 2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), 0), (np.random.uniform(-5, -2), np.random.uniform(-2, 0))] for xl, xu in x_bounds: for yl, yu in y_bounds: zl, zu = interval.power(xl, xu, yl, yu) x = np.linspace(xl, xu, 100) y = np.linspace(yl, yu, 100) for _x in x: _z = _x**y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z)) x_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), np.random.uniform(-2, 0)), (np.random.uniform(-5, -2), 0)] y_bounds = list(range(-4, 4)) for xl, xu in x_bounds: for yl in y_bounds: yu = yl zl, zu = interval.power(xl, xu, yl, yu) x = np.linspace(xl, xu, 100, endpoint=False) y = yl _z = x**y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_pow(self): x_bounds = [(np.random.uniform(0, 2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (0, 0)] y_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (np.random.uniform(0, 2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), 0), (np.random.uniform(-5, -2), np.random.uniform(-2, 0))] for xl, xu in x_bounds: for yl, yu in y_bounds: zl, zu = interval.power(xl, xu, yl, yu, feasibility_tol=1e-8) if xl == 0 and xu == 0 and yu < 0: self.assertEqual(zl, -interval.inf) self.assertEqual(zu, interval.inf) x = np.linspace(xl, xu, 100) y = np.linspace(yl, yu, 100) for _x in x: _z = _x**y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z)) x_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), np.random.uniform(-2, 0)), (np.random.uniform(-5, -2), 0)] y_bounds = list(range(-4, 4)) for xl, xu in x_bounds: for yl in y_bounds: yu = yl zl, zu = interval.power(xl, xu, yl, yu, feasibility_tol=1e-8) x = np.linspace(xl, xu, 100, endpoint=False) y = yl _z = x**y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_log10(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available') c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var() m.c = pyo.Constraint( expr=pyo.inequality(body=pyo.log10(m.x), lower=cl, upper=cu)) self.tightener(m) z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper), 100) if m.x.lb is None: xl = -np.inf else: xl = pyo.value(m.x.lb) if m.x.ub is None: xu = np.inf else: xu = pyo.value(m.x.ub) x = 10**z print(xl, xu, cl, cu) print(x) self.assertTrue(np.all(xl <= x)) self.assertTrue(np.all(xu >= x))
def test_pow2(self): x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] c_bounds = [(-2.5, 2.8), (0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1)] for xl, xu in x_bounds: for cl, cu in c_bounds: m = pe.Block(concrete=True) m.x = pe.Var(bounds=(xl, xu)) m.y = pe.Var() m.c = pe.Constraint( expr=pe.inequality(body=m.y**m.x, lower=cl, upper=cu)) fbbt(m) x = np.linspace(pe.value(m.x.lb) + 1e-6, pe.value(m.x.ub), 100, endpoint=False) z = np.linspace(pe.value(m.c.lower) + 1e-6, pe.value(m.c.upper), 100, endpoint=False) if m.y.lb is None: yl = -np.inf else: yl = m.y.lb if m.y.ub is None: yu = np.inf else: yu = m.y.ub for _x in x: _y = np.exp(np.log(abs(z)) / _x) self.assertTrue(np.all(yl <= _y)) self.assertTrue(np.all(yu >= _y))
def test_pow2(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available') x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] c_bounds = [(-2.5, 2.8), (0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1)] for xl, xu in x_bounds: for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var(bounds=(xl, xu)) m.y = pyo.Var() m.c = pyo.Constraint( expr=pyo.inequality(body=m.y**m.x, lower=cl, upper=cu)) self.tightener(m) x = np.linspace(pyo.value(m.x.lb) + 1e-6, pyo.value(m.x.ub), 100, endpoint=False) z = np.linspace(pyo.value(m.c.lower) + 1e-6, pyo.value(m.c.upper), 100, endpoint=False) if m.y.lb is None: yl = -np.inf else: yl = m.y.lb if m.y.ub is None: yu = np.inf else: yu = m.y.ub y = np.exp(np.split(np.log(np.abs(z)), len(z)) / x) self.assertTrue(np.all(yl <= y)) self.assertTrue(np.all(yu >= y))
def test_exp(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available') c_bounds = [(-2.5, 2.8), (0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1)] for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var() m.c = pyo.Constraint( expr=pyo.inequality(body=pyo.exp(m.x), lower=cl, upper=cu)) self.tightener(m) if pyo.value(m.c.lower) <= 0: _cl = 1e-6 else: _cl = pyo.value(m.c.lower) z = np.linspace(_cl, pyo.value(m.c.upper), 100) if m.x.lb is None: xl = -np.inf else: xl = pyo.value(m.x.lb) if m.x.ub is None: xu = np.inf else: xu = pyo.value(m.x.ub) x = np.log(z) self.assertTrue(np.all(xl <= x)) self.assertTrue(np.all(xu >= x))
def test_add(self): x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] for xl, xu in x_bounds: for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var(bounds=(xl, xu)) m.y = pyo.Var() m.p = pyo.Param(mutable=True) m.p.value = 1 m.c = pyo.Constraint(expr=pyo.inequality( body=m.x + m.y + (m.p + 1), lower=cl, upper=cu)) new_bounds = fbbt(m) self.assertEqual(new_bounds[m.x], (pyo.value(m.x.lb), pyo.value(m.x.ub))) self.assertEqual(new_bounds[m.y], (pyo.value(m.y.lb), pyo.value(m.y.ub))) x = np.linspace(pyo.value(m.x.lb), pyo.value(m.x.ub), 100) z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper), 100) if m.y.lb is None: yl = -np.inf else: yl = m.y.lb if m.y.ub is None: yu = np.inf else: yu = m.y.ub for _x in x: _y = z - _x - m.p.value - 1 self.assertTrue(np.all(yl <= _y)) self.assertTrue(np.all(yu >= _y))
def test_pow1(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available') x_bounds = [(0, 2.8), (0.5, 2.8), (1, 2.8), (0.5, 1)] c_bounds = [(-2.5, 2.8), (0.5, 2.8), (-2.5, 0), (0, 2.8), (1, 2.8), (0.5, 1)] for xl, xu in x_bounds: for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var(bounds=(xl, xu)) m.y = pyo.Var() m.c = pyo.Constraint(expr=pyo.inequality(body=m.x**m.y, lower=cl, upper=cu)) if xl > 0 and cu <= 0: with self.assertRaises(InfeasibleConstraintException): self.tightener(m) else: self.tightener(m) x = np.linspace(pyo.value(m.x.lb) + 1e-6, pyo.value(m.x.ub), 100, endpoint=False) z = np.linspace(pyo.value(m.c.lower) + 1e-6, pyo.value(m.c.upper), 100, endpoint=False) if m.y.lb is None: yl = -np.inf else: yl = m.y.lb if m.y.ub is None: yu = np.inf else: yu = m.y.ub for _x in x: _y = np.log(abs(z)) / np.log(abs(_x)) self.assertTrue(np.all(yl <= _y)) self.assertTrue(np.all(yu >= _y))
def test_pow4(self): y_bounds = [(0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1), (0, 0.5)] exp_vals = [-3, -2.5, -2, -1.5, -1, -0.5, 0.5, 1, 1.5, 2, 2.5, 3] for yl, yu in y_bounds: for _exp_val in exp_vals: m = pyo.Block(concrete=True) m.x = pyo.Var() m.y = pyo.Var(bounds=(yl, yu)) m.c = pyo.Constraint(expr=m.x**_exp_val == m.y) fbbt(m) y = np.linspace(pyo.value(m.y.lb) + 1e-6, pyo.value(m.y.ub), 100, endpoint=True) if m.x.lb is None: xl = -np.inf else: xl = m.x.lb if m.x.ub is None: xu = np.inf else: xu = m.x.ub _x = np.exp(np.log(y) / _exp_val) self.assertTrue(np.all(xl <= _x)) self.assertTrue(np.all(xu >= _x))
def test_exp(self): xl = -2.5 xu = 2.8 zl, zu = interval.exp(xl, xu) x = np.linspace(xl, xu, 100) _z = np.exp(x) self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_log(self): x_bounds = [(np.random.uniform(0, 2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (0, 0)] for xl, xu in x_bounds: zl, zu = interval.log(xl, xu) x = np.linspace(xl, xu, 100) _z = np.log(x) self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_exp(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available.') xl = -2.5 xu = 2.8 zl, zu = self.exp(xl, xu) x = np.linspace(xl, xu, 100) _z = np.exp(x) self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_log(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available.') x_bounds = [(np.random.uniform(0, 2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (0, 0)] for xl, xu in x_bounds: zl, zu = self.log(xl, xu) x = np.linspace(xl, xu, 100) _z = np.log(x) self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_tan(self): lbs = np.linspace(-2 * math.pi, 2 * math.pi, 10) ubs = np.linspace(-2 * math.pi, 2 * math.pi, 10) for xl in lbs: for xu in ubs: if xu >= xl: zl, zu = interval.tan(xl, xu) x = np.linspace(xl, xu, 100) _z = np.tan(x) self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_mul(self): xl = -2.5 xu = 2.8 yl = -3.2 yu = 2.7 zl, zu = interval.mul(xl, xu, yl, yu) x = np.linspace(xl, xu, 100) y = np.linspace(yl, yu, 100) for _x in x: _z = _x * y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_tan(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available.') lbs = np.linspace(-2 * math.pi, 2 * math.pi, 10) ubs = np.linspace(-2 * math.pi, 2 * math.pi, 10) for xl in lbs: for xu in ubs: if xu >= xl: zl, zu = self.tan(xl, xu) x = np.linspace(xl, xu, 100) _z = np.tan(x) self.assertTrue(np.all(zl <= _z + 1e-14)) self.assertTrue(np.all(zu >= _z - 1e-14))
def test_mul(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available.') xl = -2.5 xu = 2.8 yl = -3.2 yu = 2.7 zl, zu = self.mul(xl, xu, yl, yu) x = np.linspace(xl, xu, 100) y = np.linspace(yl, yu, 100) for _x in x: _z = _x * y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_div(self): x_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5))] y_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (np.random.uniform(0, 2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), 0), (np.random.uniform(-5, -2), np.random.uniform(-2, 0))] for xl, xu in x_bounds: for yl, yu in y_bounds: zl, zu = interval.div(xl, xu, yl, yu, feasibility_tol=1e-8) x = np.linspace(xl, xu, 100) y = np.linspace(yl, yu, 100) for _x in x: _z = _x / y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_div(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available.') x_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5))] y_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (np.random.uniform(0, 2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), 0), (np.random.uniform(-5, -2), np.random.uniform(-2, 0))] for xl, xu in x_bounds: for yl, yu in y_bounds: zl, zu = self.div(xl, xu, yl, yu, 1e-8) x = np.linspace(xl, xu, 100) y = np.linspace(yl, yu, 100) for _x in x: _z = _x / y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def __init__(self, center, shape_matrix, scale=1): """ EllipsoidalSet constructor Args: center: Vector (``list``) of uncertain parameter values around which deviations are restrained. shape_matrix: Positive semi-definite matrix, effectively a covariance matrix for constraint and bounds determination. scale: Right-hand side value for the ellipsoid. """ # === Valid data in lists/matrixes if not all(isinstance(elem, (int, float)) for row in shape_matrix for elem in row): raise AttributeError("Matrix shape_matrix must be real-valued and numeric.") if not all(isinstance(elem, (int, float)) for elem in center): raise AttributeError("Vector center must be real-valued and numeric.") if not isinstance(scale, (int, float)): raise AttributeError("Ellipse scale must be a real-valued numeric.") # === Valid matrix dimensions num_cols = len(shape_matrix[0]) if not all(len(row) == num_cols for row in shape_matrix): raise AttributeError("Shape matrix must have valid matrix dimensions.") # === Ensure shape_matrix is a square matrix array_shape_mat = np.asarray(shape_matrix) if array_shape_mat.shape[0] != array_shape_mat.shape[1]: raise AttributeError("Shape matrix must be square.") # === Ensure dimensions of shape_matrix are same as dimensions of uncertain_params if array_shape_mat.shape[1] != len(center): raise AttributeError("Shape matrix must be " "same dimensions as vector of uncertain parameters.") # === Symmetric shape_matrix if not np.all(np.abs(array_shape_mat-array_shape_mat.T) < 1e-8): raise AttributeError("Shape matrix must be symmetric.") # === Ensure scale is non-negative if scale < 0: raise AttributeError("Scale of ellipse (rhs) must be non-negative.") # === Check if shape matrix is invertible try: np.linalg.inv(shape_matrix) except np.linalg.LinAlgError as err: raise("Error with shape matrix supplied to EllipsoidalSet object being singular. %s" % err) # === Check is shape matrix is positive semidefinite if not all(np.linalg.eigvals(shape_matrix) >= 0): raise("Non positive-semidefinite shape matrix.") # === Ensure matrix is not degenerate, for determining inferred bounds try: for i in range(len(shape_matrix)): np.power(shape_matrix[i][i], 0.5) except: raise AttributeError("Shape matrix must be non-degenerate.") self.center = center self.shape_matrix = shape_matrix self.scale = scale self.type = "ellipsoidal"
def test_log(self): c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8), (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)] for cl, cu in c_bounds: m = pyo.Block(concrete=True) m.x = pyo.Var() m.c = pyo.Constraint( expr=pyo.inequality(body=pyo.log(m.x), lower=cl, upper=cu)) fbbt(m) z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper), 100) if m.x.lb is None: xl = -np.inf else: xl = pyo.value(m.x.lb) if m.x.ub is None: xu = np.inf else: xu = pyo.value(m.x.ub) x = np.exp(z) self.assertTrue(np.all(xl <= x)) self.assertTrue(np.all(xu >= x))
def test_pow(self): if not numpy_available: raise unittest.SkipTest('Numpy is not available.') x_bounds = [(np.random.uniform(0, 2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (0, 0)] y_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (0, np.random.uniform(2, 5)), (np.random.uniform(0, 2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), 0), (np.random.uniform(-5, -2), np.random.uniform(-2, 0))] for xl, xu in x_bounds: for yl, yu in y_bounds: zl, zu = self.power(xl, xu, yl, yu, 1e-8) if xl == 0 and xu == 0 and yu < 0: self.assertEqual(zl, -interval.inf) self.assertEqual(zu, interval.inf) x = np.linspace(xl, xu, 100) y = np.linspace(yl, yu, 100) for _x in x: _z = _x**y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z)) x_bounds = [(np.random.uniform(-5, -2), np.random.uniform(2, 5)), (np.random.uniform(-5, -2), np.random.uniform(-2, 0)), (np.random.uniform(-5, -2), 0)] y_bounds = list(range(-4, 4)) for xl, xu in x_bounds: for yl in y_bounds: yu = yl zl, zu = self.power(xl, xu, yl, yu, 1e-8) x = np.linspace(xl, xu, 100, endpoint=False) y = yl _z = x**y self.assertTrue(np.all(zl <= _z)) self.assertTrue(np.all(zu >= _z))
def test_exp(self): c_bounds = [(-2.5, 2.8), (0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1)] for cl, cu in c_bounds: m = pe.Block(concrete=True) m.x = pe.Var() m.c = pe.Constraint( expr=pe.inequality(body=pe.exp(m.x), lower=cl, upper=cu)) fbbt(m) if pe.value(m.c.lower) <= 0: _cl = 1e-6 else: _cl = pe.value(m.c.lower) z = np.linspace(_cl, pe.value(m.c.upper), 100) if m.x.lb is None: xl = -np.inf else: xl = pe.value(m.x.lb) if m.x.ub is None: xu = np.inf else: xu = pe.value(m.x.ub) x = np.log(z) self.assertTrue(np.all(xl <= x)) self.assertTrue(np.all(xu >= x))