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_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 = 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) + 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.exp(np.log(abs(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_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_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_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_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 = pyo.Block(concrete=True) m.x = pyo.Var() m.c = pyo.Constraint( expr=pyo.inequality(body=pyo.exp(m.x), lower=cl, upper=cu)) fbbt(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))