Exemple #1
0
 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))
Exemple #2
0
 def test_pow2(self):
     xl = np.linspace(-2, 2, 9)
     xu = np.linspace(-2, 2, 9)
     yl = np.linspace(-2, 2, 9)
     yu = np.linspace(-2, 2, 9)
     for _xl in xl:
         for _xu in xu:
             if _xl > _xu:
                 continue
             for _yl in yl:
                 for _yu in yu:
                     if _yl > _yu:
                         continue
                     if _xl == 0 and _xu == 0 and _yu < 0:
                         lb, ub = interval.power(_xl,
                                                 _xu,
                                                 _yl,
                                                 _yu,
                                                 feasibility_tol=1e-8)
                         self.assertEqual(lb, -interval.inf)
                         self.assertEqual(ub, interval.inf)
                     elif _yl == _yu and _yl != round(_yl) and (
                             _xu < 0 or (_xu < 0 and _yu < 0)):
                         with self.assertRaises(
                             (InfeasibleConstraintException,
                              interval.IntervalException)):
                             lb, ub = interval.power(_xl,
                                                     _xu,
                                                     _yl,
                                                     _yu,
                                                     feasibility_tol=1e-8)
                     else:
                         lb, ub = interval.power(_xl,
                                                 _xu,
                                                 _yl,
                                                 _yu,
                                                 feasibility_tol=1e-8)
                         if isfinite(lb) and isfinite(ub):
                             nan_fill = 0.5 * (lb + ub)
                         elif isfinite(lb):
                             nan_fill = lb + 1
                         elif isfinite(ub):
                             nan_fill = ub - 1
                         else:
                             nan_fill = 0
                         x = np.linspace(_xl, _xu, 30)
                         y = np.linspace(_yl, _yu, 30)
                         z = x**np.split(y, len(y))
                         z[np.isnan(z)] = nan_fill
                         all_values = z
                         estimated_lb = all_values.min()
                         estimated_ub = all_values.max()
                         self.assertTrue(lb - 1e-8 <= estimated_lb)
                         self.assertTrue(ub + 1e-8 >= estimated_ub)