def test_book_sh(self, tname, test_file, base_file):
        bname = os.path.basename(test_file)
        dir_ = os.path.dirname(test_file)

        skip_msg = check_skip('test_' + tname)
        if skip_msg:
            raise unittest.SkipTest(skip_msg)

        # Skip all shell tests on Windows.
        if os.name == 'nt':
            raise unittest.SkipTest("Shell tests are not runnable on Windows")

        cwd = os.getcwd()
        os.chdir(dir_)
        out_file = os.path.splitext(test_file)[0] + '.out'
        with open(out_file, 'w') as f:
            _env = os.environ.copy()
            _env['PATH'] = os.pathsep.join(
                [os.path.dirname(sys.executable), _env['PATH']])
            subprocess.run(['bash', bname],
                           stdout=f,
                           stderr=f,
                           cwd=dir_,
                           env=_env)
        os.chdir(cwd)

        self.compare_files(out_file, base_file)
        os.remove(out_file)
Exemple #2
0
 def test_getting_started(self):
     try:
         import numpy as np
     except:
         raise unittest.SkipTest('numpy is not available')
     opt = appsi.solvers.Cplex()
     if not opt.available():
         raise unittest.SkipTest('cplex is not available')
     getting_started.main(plot=False, n_points=10)
Exemple #3
0
 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))
Exemple #4
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 #5
0
 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))
Exemple #6
0
 def test_pow4(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available')
     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)
             self.tightener(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))
Exemple #7
0
 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))
Exemple #8
0
 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))
Exemple #9
0
    def test_mutable_param_with_range(self, name: str, opt_class: Type[Solver]):
        opt: Solver = opt_class()
        if not opt.available():
            raise unittest.SkipTest
        try:
            import numpy as np
        except:
            raise unittest.SkipTest('numpy is not available')
        m = pe.ConcreteModel()
        m.x = pe.Var()
        m.y = pe.Var()
        m.a1 = pe.Param(initialize=0, mutable=True)
        m.a2 = pe.Param(initialize=0, mutable=True)
        m.b1 = pe.Param(initialize=0, mutable=True)
        m.b2 = pe.Param(initialize=0, mutable=True)
        m.c1 = pe.Param(initialize=0, mutable=True)
        m.c2 = pe.Param(initialize=0, mutable=True)
        m.obj = pe.Objective(expr=m.y)
        m.con1 = pe.Constraint(expr=(m.b1, m.y - m.a1 * m.x, m.c1))
        m.con2 = pe.Constraint(expr=(m.b2, m.y - m.a2 * m.x, m.c2))

        np.random.seed(0)
        params_to_test = [(np.random.uniform(0, 10), np.random.uniform(-10, 0),
                           np.random.uniform(-5, 2.5), np.random.uniform(-5, 2.5),
                           np.random.uniform(2.5, 10), np.random.uniform(2.5, 10), pe.minimize),
                          (np.random.uniform(0, 10), np.random.uniform(-10, 0),
                           np.random.uniform(-5, 2.5), np.random.uniform(-5, 2.5),
                           np.random.uniform(2.5, 10), np.random.uniform(2.5, 10), pe.maximize),
                          (np.random.uniform(0, 10), np.random.uniform(-10, 0),
                           np.random.uniform(-5, 2.5), np.random.uniform(-5, 2.5),
                           np.random.uniform(2.5, 10), np.random.uniform(2.5, 10), pe.minimize),
                          (np.random.uniform(0, 10), np.random.uniform(-10, 0),
                           np.random.uniform(-5, 2.5), np.random.uniform(-5, 2.5),
                           np.random.uniform(2.5, 10), np.random.uniform(2.5, 10), pe.maximize)]
        for (a1, a2, b1, b2, c1, c2, sense) in params_to_test:
            m.a1.value = float(a1)
            m.a2.value = float(a2)
            m.b1.value = float(b1)
            m.b2.value = float(b2)
            m.c1.value = float(c1)
            m.c2.value = float(c2)
            m.obj.sense = sense
            res: Results = opt.solve(m)
            self.assertEqual(res.termination_condition, TerminationCondition.optimal)
            if sense is pe.minimize:
                self.assertAlmostEqual(m.x.value, (b2 - b1) / (a1 - a2), 6)
                self.assertAlmostEqual(m.y.value, a1 * (b2 - b1) / (a1 - a2) + b1, 6)
                self.assertAlmostEqual(res.best_feasible_objective, m.y.value, 6)
                self.assertTrue(res.best_objective_bound <= m.y.value + 1e-12)
                duals = opt.get_duals()
                self.assertAlmostEqual(duals[m.con1], (1 + a1 / (a2 - a1)), 6)
                self.assertAlmostEqual(duals[m.con2], -a1 / (a2 - a1), 6)
            else:
                self.assertAlmostEqual(m.x.value, (c2 - c1) / (a1 - a2), 6)
                self.assertAlmostEqual(m.y.value, a1 * (c2 - c1) / (a1 - a2) + c1, 6)
                self.assertAlmostEqual(res.best_feasible_objective, m.y.value, 6)
                self.assertTrue(res.best_objective_bound >= m.y.value - 1e-12)
                duals = opt.get_duals()
                self.assertAlmostEqual(duals[m.con1], (1 + a1 / (a2 - a1)), 6)
                self.assertAlmostEqual(duals[m.con2], -a1 / (a2 - a1), 6)
Exemple #10
0
 def test_acos(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available.')
     yl, yu = self.acos(-0.5, 0.5, -interval.inf, interval.inf, 1e-8)
     self.assertEqual(yl, -interval.inf)
     self.assertEqual(yu, interval.inf)
     yl, yu = self.acos(-0.5, 0.5, -0.5 * math.pi, 0.5 * math.pi, 1e-8)
     self.assertAlmostEqual(yl, -0.5 * math.pi, 12)
     self.assertAlmostEqual(yu, 0.5 * math.pi, 12)
     yl, yu = self.acos(-0.5, 0.5, 0, math.pi, 1e-8)
     self.assertAlmostEqual(yl, math.acos(0.5))
     self.assertAlmostEqual(yu, math.acos(-0.5))
     yl, yu = self.acos(-0.5, 0.5, 0 - 0.1, math.pi + 0.1, 1e-8)
     self.assertAlmostEqual(yl, math.acos(0.5))
     self.assertAlmostEqual(yu, math.acos(-0.5))
     yl, yu = self.acos(-0.5, 0.5, 0 + 0.1, math.pi - 0.1, 1e-8)
     self.assertAlmostEqual(yl, math.acos(0.5))
     self.assertAlmostEqual(yu, math.acos(-0.5))
     yl, yu = self.acos(-0.5, 0.5, -math.pi, 0, 1e-8)
     self.assertAlmostEqual(yl, -math.acos(-0.5), 12)
     self.assertAlmostEqual(yu, -math.acos(0.5), 12)
     yl, yu = self.acos(-0.5, 0.5, -math.pi - 0.1, 0 + 0.1, 1e-8)
     self.assertAlmostEqual(yl, -math.acos(-0.5), 12)
     self.assertAlmostEqual(yu, -math.acos(0.5), 12)
     yl, yu = self.acos(-0.5, 0.5, -math.pi + 0.1, 0 - 0.1, 1e-8)
     self.assertAlmostEqual(yl, -math.acos(-0.5), 12)
     self.assertAlmostEqual(yu, -math.acos(0.5), 12)
Exemple #11
0
 def test_asin(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available.')
     yl, yu = self.asin(-0.5, 0.5, -interval.inf, interval.inf, 1e-8)
     self.assertEqual(yl, -interval.inf)
     self.assertEqual(yu, interval.inf)
     yl, yu = self.asin(-0.5, 0.5, -math.pi, math.pi, 1e-8)
     self.assertAlmostEqual(yl, -math.pi, 12)
     self.assertAlmostEqual(yu, math.pi, 12)
     yl, yu = self.asin(-0.5, 0.5, -math.pi / 2, math.pi / 2, 1e-8)
     self.assertAlmostEqual(yl, math.asin(-0.5))
     self.assertAlmostEqual(yu, math.asin(0.5))
     yl, yu = self.asin(-0.5, 0.5, -math.pi / 2 - 0.1, math.pi / 2 + 0.1,
                        1e-8)
     self.assertAlmostEqual(yl, math.asin(-0.5))
     self.assertAlmostEqual(yu, math.asin(0.5))
     yl, yu = self.asin(-0.5, 0.5, -math.pi / 2 + 0.1, math.pi / 2 - 0.1,
                        1e-8)
     self.assertAlmostEqual(yl, math.asin(-0.5))
     self.assertAlmostEqual(yu, math.asin(0.5))
     yl, yu = self.asin(-0.5, 0.5, -1.5 * math.pi, 1.5 * math.pi, 1e-8)
     self.assertAlmostEqual(yl, -3.6651914291880920, 12)
     self.assertAlmostEqual(yu, 3.6651914291880920, 12)
     yl, yu = self.asin(-0.5, 0.5, -1.5 * math.pi - 0.1,
                        1.5 * math.pi + 0.1, 1e-8)
     self.assertAlmostEqual(yl, -3.6651914291880920, 12)
     self.assertAlmostEqual(yu, 3.6651914291880920, 12)
     yl, yu = self.asin(-0.5, 0.5, -1.5 * math.pi + 0.1,
                        1.5 * math.pi - 0.1, 1e-8)
     self.assertAlmostEqual(yl, -3.6651914291880920, 12)
     self.assertAlmostEqual(yu, 3.6651914291880920, 12)
Exemple #12
0
 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))
Exemple #13
0
 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))
Exemple #14
0
 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))
Exemple #15
0
 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))
Exemple #16
0
 def get_solution(self):
     try:
         import numpy as np
     except:
         raise unittest.SkipTest('numpy is not available')
     p1 = self.m.p1.value
     p2 = self.m.p2.value
     p3 = self.m.p3.value
     p4 = self.m.p4.value
     A = np.array([[1, -p1], [1, -p3]])
     rhs = np.array([p2, p4])
     sol = np.linalg.solve(A, rhs)
     x = float(sol[1])
     y = float(sol[0])
     return x, y
Exemple #17
0
 def test_atan(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available.')
     yl, yu = self.atan(-0.5, 0.5, -interval.inf, interval.inf)
     self.assertEqual(yl, -interval.inf)
     self.assertEqual(yu, interval.inf)
     yl, yu = self.atan(-0.5, 0.5, -0.1, 0.1)
     self.assertAlmostEqual(yl, -0.1, 12)
     self.assertAlmostEqual(yu, 0.1, 12)
     yl, yu = self.atan(-0.5, 0.5, -0.5 * math.pi + 0.1, math.pi / 2 - 0.1)
     self.assertAlmostEqual(yl, math.atan(-0.5), 12)
     self.assertAlmostEqual(yu, math.atan(0.5), 12)
     yl, yu = self.atan(-0.5, 0.5, -1.5 * math.pi + 0.1,
                        1.5 * math.pi - 0.1)
     self.assertAlmostEqual(yl, math.atan(-0.5) - math.pi, 12)
     self.assertAlmostEqual(yu, math.atan(0.5) + math.pi, 12)
Exemple #18
0
    def test_book_py(self, tname, test_file, base_file):
        bname = os.path.basename(test_file)
        dir_ = os.path.dirname(test_file)

        skip_msg = check_skip('test_'+tname)
        if skip_msg:
            raise unittest.SkipTest(skip_msg)

        cwd = os.getcwd()
        os.chdir(dir_)
        out_file = os.path.splitext(test_file)[0]+'.out'
        with open(out_file, 'w') as f:
            subprocess.run([sys.executable, bname], stdout=f, stderr=f, cwd=dir_)
        os.chdir(cwd)

        self.compare_files(out_file, base_file)
        os.remove(out_file)
Exemple #19
0
 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))
Exemple #20
0
 def test_pow2(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available.')
     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 = self.power(_xl, _xu, _yl, _yu, 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 = self.power(_xl, _xu, _yl, _yu, 1e-8)
                     else:
                         lb, ub = self.power(_xl, _xu, _yl, _yu, 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)
Exemple #21
0
    def test_skip_unknown_expression1(self):
        if self.tightener is not fbbt:
            raise unittest.SkipTest('Appsi FBBT does not support unkown expressions yet')

        m = pyo.ConcreteModel()
        m.x = pyo.Var(bounds=(1,1))
        m.y = pyo.Var()
        expr = DummyExpr([m.x, m.y])
        m.c = pyo.Constraint(expr=expr == 1)

        OUT = StringIO()
        with LoggingIntercept(OUT, 'pyomo.contrib.fbbt.fbbt'):
            new_bounds = self.tightener(m)

        self.assertEqual(pyo.value(m.x.lb), 1)
        self.assertEqual(pyo.value(m.x.ub), 1)
        self.assertEqual(pyo.value(m.y.lb), None)
        self.assertEqual(pyo.value(m.y.ub), None)
        self.assertIn("Unsupported expression type for FBBT", OUT.getvalue())
Exemple #22
0
    def test_skip_unknown_expression2(self):
        if self.tightener is not fbbt:
            raise unittest.SkipTest('Appsi FBBT does not support unkown expressions yet')

        def dummy_unary_expr(x):
            return 0.5*x

        m = pyo.ConcreteModel()
        m.x = pyo.Var(bounds=(0,4))
        expr = UnaryFunctionExpression((m.x,), name='dummy_unary_expr', fcn=dummy_unary_expr)
        m.c = pyo.Constraint(expr=expr == 1)

        OUT = StringIO()
        with LoggingIntercept(OUT, 'pyomo.contrib.fbbt.fbbt'):
            new_bounds = self.tightener(m)

        self.assertEqual(pyo.value(m.x.lb), 0)
        self.assertEqual(pyo.value(m.x.ub), 4)
        self.assertIn("Unsupported expression type for FBBT", OUT.getvalue())
Exemple #23
0
    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))
Exemple #24
0
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

import os
import pyomo.common.unittest as unittest
import pyomo.environ as pyo
import math

from pyomo.contrib.pynumero.dependencies import (numpy as np, numpy_available,
                                                 scipy, scipy_available)
from pyomo.common.dependencies.scipy import sparse as spa

if not (numpy_available and scipy_available):
    raise unittest.SkipTest("Pynumero needs scipy and numpy to run NLP tests")

from pyomo.contrib.pynumero.asl import AmplInterface
if not AmplInterface.available():
    raise unittest.SkipTest(
        "Pynumero needs the ASL extension to run CyIpoptSolver tests")

from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import (
    CyIpoptSolver,
    CyIpoptNLP,
    cyipopt_available,
)

from pyomo.contrib.pynumero.interfaces.external_grey_box import ExternalGreyBoxModel, ExternalGreyBoxBlock
from pyomo.contrib.pynumero.interfaces.pyomo_nlp import PyomoGreyBoxNLP, PyomoNLP
from pyomo.contrib.pynumero.interfaces.pyomo_grey_box_nlp import PyomoNLPWithGreyBoxBlocks
Exemple #25
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

import pyomo.common.unittest as unittest
from pyomo.contrib.pynumero.dependencies import (numpy as np, numpy_available,
                                                 scipy, scipy_available)
if not (numpy_available and scipy_available):
    raise unittest.SkipTest("Pynumero needs scipy and numpy to run NLP tests")

import pyomo.contrib.pynumero.interfaces.utils as utils


class TestCondensedSparseSummation(unittest.TestCase):
    def test_condensed_sparse_summation(self):
        data = [1.0, 0.0]
        row = [1, 2]
        col = [2, 2]
        A = scipy.sparse.coo_matrix((data, (row, col)), shape=(3, 3))

        data = [3.0, 0.0]
        B = scipy.sparse.coo_matrix((data, (row, col)), shape=(3, 3))

        # By default, scipy will remove structural nonzeros that
        # have zero values
Exemple #26
0
#  National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

import pyomo.common.unittest as unittest
import pyomo.environ as pyo
from pyomo.common.dependencies import attempt_import

np, numpy_available = attempt_import('numpy', 'Interior point requires numpy', minimum_version='1.13.0')
scipy, scipy_available = attempt_import('scipy', 'Interior point requires scipy')
mumps, mumps_available = attempt_import('mumps', 'Interior point requires mumps')
if not (numpy_available and scipy_available):
    raise unittest.SkipTest('Interior point tests require numpy and scipy')

if scipy_available:
    from pyomo.contrib.interior_point.linalg.scipy_interface import ScipyInterface
if mumps_available:
    from pyomo.contrib.interior_point.linalg.mumps_interface import MumpsInterface


import numpy as np

from pyomo.contrib.pynumero.asl import AmplInterface
asl_available = AmplInterface.available()
from pyomo.contrib.interior_point.interior_point import (process_init,
                                                         process_init_duals_lb,
                                                         process_init_duals_ub,
                                                         _fraction_to_the_boundary_helper_lb,
#  Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

import itertools
import pyomo.common.unittest as unittest
import pyomo.environ as pyo

from pyomo.contrib.pynumero.dependencies import (numpy as np, numpy_available,
                                                 scipy, scipy_available)

if not (numpy_available and scipy_available):
    raise unittest.SkipTest("Pynumero needs scipy and numpy to run NLP tests")

from pyomo.common.dependencies.scipy import sparse as sps

from pyomo.contrib.pynumero.asl import AmplInterface
if not AmplInterface.available():
    raise unittest.SkipTest(
        "Pynumero needs the ASL extension to run cyipopt tests")

from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import (
    cyipopt_available, )
from pyomo.contrib.pynumero.interfaces.external_pyomo_model import (
    ExternalPyomoModel,
    get_hessian_of_constraint,
)
from pyomo.contrib.pynumero.interfaces.pyomo_grey_box_nlp import (
Exemple #28
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright (c) 2008-2022
#  National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________
import pyomo.common.unittest as unittest
from pyomo.contrib.pynumero.dependencies import numpy as np, numpy_available
if not numpy_available:
    raise unittest.SkipTest('pynumero MA27 tests require numpy')
import ctypes
from pyomo.contrib.pynumero.linalg.ma27 import MA27Interface


@unittest.skipIf(not MA27Interface.available(), reason='MA27 not available')
class TestMA27Interface(unittest.TestCase):
    def test_get_cntl(self):
        ma27 = MA27Interface()
        self.assertEqual(ma27.get_icntl(1), 6)

        self.assertAlmostEqual(ma27.get_cntl(1),
                               1e-1)  # Numerical pivot threshold
        self.assertAlmostEqual(ma27.get_cntl(3), 0.0)  # Null pivot threshold

    def test_set_icntl(self):
        ma27 = MA27Interface()
        ma27.set_icntl(5, 4)  # Set output printing to max verbosity
import pyomo.common.unittest as unittest
import filecmp
import glob
import os
import os.path
import subprocess
import sys
from itertools import zip_longest
from pyomo.opt import check_available_solvers
from pyomo.common.dependencies import attempt_import, check_min_version
from pyomo.common.fileutils import this_file_dir
from pyomo.common.tee import capture_output

parameterized, param_available = attempt_import('parameterized')
if not param_available:
    raise unittest.SkipTest('Parameterized is not available.')

# Find all *.txt files, and use them to define baseline tests
currdir = this_file_dir()
datadir = currdir

solver_dependencies = {
    # abstract_ch
    'test_abstract_ch_wl_abstract_script': ['glpk'],
    'test_abstract_ch_pyomo_wl_abstract': ['glpk'],
    'test_abstract_ch_pyomo_solve1': ['glpk'],
    'test_abstract_ch_pyomo_solve2': ['glpk'],
    'test_abstract_ch_pyomo_solve3': ['glpk'],
    'test_abstract_ch_pyomo_solve4': ['glpk'],
    'test_abstract_ch_pyomo_solve5': ['glpk'],
    'test_abstract_ch_pyomo_diet1': ['glpk'],
import pyomo.common.unittest as unittest
from pyomo.common.dependencies import attempt_import
np, np_available = attempt_import('numpy', minimum_version='1.13.0')
scipy, scipy_available = attempt_import('scipy.sparse')
mumps, mumps_available = attempt_import('mumps')
if not np_available or not scipy_available:
    raise unittest.SkipTest(
        'numpy and scipy are needed for interior point tests')
import numpy as np
from scipy.sparse import coo_matrix, tril
from pyomo.contrib import interior_point as ip
from pyomo.contrib.pynumero.linalg.base import LinearSolverStatus
if scipy_available:
    from pyomo.contrib.interior_point.linalg.scipy_interface import ScipyInterface
if mumps_available:
    from pyomo.contrib.interior_point.linalg.mumps_interface import MumpsInterface
from pyomo.contrib.pynumero.linalg.ma27 import MA27Interface
ma27_available = MA27Interface.available()
if ma27_available:
    from pyomo.contrib.interior_point.linalg.ma27_interface import InteriorPointMA27Interface


def get_base_matrix(use_tril):
    if use_tril:
        row = [0, 1, 1, 2, 2]
        col = [0, 0, 1, 0, 2]
        data = [1, 7, 4, 3, 6]
    else:
        row = [0, 0, 0, 1, 1, 2, 2]
        col = [0, 1, 2, 0, 1, 0, 2]
        data = [1, 7, 3, 7, 4, 3, 6]