Ejemplo n.º 1
0
 def test_help_solvers(self):
     with capture_output() as OUT:
         help_solvers()
     OUT = OUT.getvalue()
     self.assertTrue(re.search('Pyomo Solvers and Solver Managers', OUT))
     self.assertTrue(re.search('Serial Solver', OUT))
     # Test known solvers and metasolver flags
     # ASL is a metasolver
     self.assertTrue(re.search('\n   \*asl ', OUT))
     # PS is bundles with Pyomo so should always be available
     self.assertTrue(re.search('\n   \+ps ', OUT))
     for solver in ('ipopt', 'cbc', 'glpk'):
         s = SolverFactory(solver)
         if s.available():
             self.assertTrue(
                 re.search("\n   \+%s " % solver, OUT),
                 "'   +%s' not found in help --solvers" % solver)
         else:
             self.assertTrue(
                 re.search("\n    %s " % solver, OUT),
                 "'    %s' not found in help --solvers" % solver)
     for solver in ('baron', ):
         s = SolverFactory(solver)
         if s.license_is_valid():
             self.assertTrue(
                 re.search("\n   \+%s " % solver, OUT),
                 "'   +%s' not found in help --solvers" % solver)
         elif s.available():
             self.assertTrue(
                 re.search("\n   \-%s " % solver, OUT),
                 "'   -%s' not found in help --solvers" % solver)
         else:
             self.assertTrue(
                 re.search("\n    %s " % solver, OUT),
                 "'    %s' not found in help --solvers" % solver)
Ejemplo n.º 2
0
 def test_help_transformations(self):
     with capture_output() as OUT:
         help_transformations()
     OUT = OUT.getvalue()
     self.assertTrue(re.search('Pyomo Model Transformations', OUT))
     self.assertTrue(re.search('core.relax_integer_vars', OUT))
     # test a transformation that we know is deprecated
     self.assertTrue(re.search('duality.linear_dual\s+\[DEPRECATED\]', OUT))
Ejemplo n.º 3
0
    def test_report_timing(self):
        # Create a set to ensure that the global sets have already been
        # constructed (this is an issue until the new set system is
        # merged in and the GlobalSet objects are not automatically
        # created by pyomo.core
        m = ConcreteModel()
        m.x = Var([1,2])

        ref = r"""
           (0(\.\d+)?) seconds to construct Block ConcreteModel; 1 index total
           (0(\.\d+)?) seconds to construct RangeSet FiniteSimpleRangeSet; 1 index total
           (0(\.\d+)?) seconds to construct Var x; 2 indices total
           (0(\.\d+)?) seconds to construct Suffix Suffix; 1 index total
           (0(\.\d+)?) seconds to apply Transformation RelaxIntegerVars \(in-place\)
           """.strip()

        xfrm = TransformationFactory('core.relax_integer_vars')

        try:
            with capture_output() as out:
                report_timing()
                m = ConcreteModel()
                m.r = RangeSet(2)
                m.x = Var(m.r)
                xfrm.apply_to(m)
            result = out.getvalue().strip()
            self.maxDiff = None
            for l, r in zip(result.splitlines(), ref.splitlines()):
                self.assertRegex(str(l.strip()), str(r.strip()))
        finally:
            report_timing(False)

        os = StringIO()
        try:
            report_timing(os)
            m = ConcreteModel()
            m.r = RangeSet(2)
            m.x = Var(m.r)
            xfrm.apply_to(m)
            result = os.getvalue().strip()
            self.maxDiff = None
            for l, r in zip(result.splitlines(), ref.splitlines()):
                self.assertRegex(str(l.strip()), str(r.strip()))
        finally:
            report_timing(False)
        buf = StringIO()
        with LoggingIntercept(buf, 'pyomo'):
            m = ConcreteModel()
            m.r = RangeSet(2)
            m.x = Var(m.r)
            xfrm.apply_to(m)
            result = os.getvalue().strip()
            self.maxDiff = None
            for l, r in zip(result.splitlines(), ref.splitlines()):
                self.assertRegex(str(l.strip()), str(r.strip()))
            self.assertEqual(buf.getvalue().strip(), "")
Ejemplo n.º 4
0
    def test_display(self):
        model = ConcreteModel()
        model.e = Expression()
        with capture_output() as out:
            model.e.display()
        self.assertEqual(out.getvalue().strip(), """
e : Size=1
    Key  : Value
    None : Undefined
        """.strip())

        model.e.set_value(1.0)
        with capture_output() as out:
            model.e.display()
        self.assertEqual(out.getvalue().strip(), """
e : Size=1
    Key  : Value
    None :   1.0
        """.strip())

        out = StringIO()
        with capture_output() as no_out:
            model.e.display(ostream=out)
        self.assertEqual(no_out.getvalue(), "")
        self.assertEqual(out.getvalue().strip(), """
e : Size=1
    Key  : Value
    None :   1.0
        """.strip())

        model.E = Expression([1,2])
        with capture_output() as out:
            model.E.display()
        self.assertEqual(out.getvalue().strip(), """
E : Size=2
    Key : Value
      1 : Undefined
      2 : Undefined
        """.strip())

        model.E[1].set_value(1.0)
        with capture_output() as out:
            model.E.display()
        self.assertEqual(out.getvalue().strip(), """
E : Size=2
    Key : Value
      1 :       1.0
      2 : Undefined
        """.strip())

        out = StringIO()
        with capture_output() as no_out:
            model.E.display(ostream=out)
        self.assertEqual(no_out.getvalue(), "")
        self.assertEqual(out.getvalue().strip(), """
E : Size=2
    Key : Value
      1 :       1.0
      2 : Undefined
        """.strip())
Ejemplo n.º 5
0
 def test_help_solvers(self):
     with capture_output() as OUT:
         help_solvers()
     OUT = OUT.getvalue()
     self.assertTrue(re.search('Pyomo Solvers and Solver Managers', OUT))
     self.assertTrue(re.search('Serial Solver', OUT))
     # Test known solvers and metasolver flags
     # ASL is a metasolver
     self.assertTrue(re.search('asl +\+', OUT))
     # PS is bundles with Pyomo so should always be available
     self.assertTrue(re.search('ps +\*', OUT))
     for solver in ('ipopt','baron','cbc','glpk'):
         s = SolverFactory(solver)
         if s.available():
             self.assertTrue(re.search("%s +\* [a-zA-Z]" % solver, OUT))
         else:
             self.assertTrue(re.search("%s +[a-zA-Z]" % solver, OUT))
Ejemplo n.º 6
0
 def test_help_solvers(self):
     with capture_output() as OUT:
         help_solvers()
     OUT = OUT.getvalue()
     self.assertTrue(re.search('Pyomo Solvers and Solver Managers', OUT))
     self.assertTrue(re.search('Serial Solver', OUT))
     # Test known solvers and metasolver flags
     # ASL is a metasolver
     self.assertTrue(re.search('asl +\+', OUT))
     # PS is bundles with Pyomo so should always be available
     self.assertTrue(re.search('ps +\*', OUT))
     for solver in ('ipopt','baron','cbc','glpk'):
         s = SolverFactory(solver)
         if s.available():
             self.assertTrue(re.search("%s +\* [a-zA-Z]" % solver, OUT))
         else:
             self.assertTrue(re.search("%s +[a-zA-Z]" % solver, OUT))
Ejemplo n.º 7
0
    def test_TicTocTimer_tictoc(self):
        RES = 1e-2 # resolution (seconds)
        timer = TicTocTimer()
        abs_time = time.time()

        time.sleep(0.1)

        with capture_output() as out:
            start_time = time.time()
            timer.tic(None)
        self.assertEqual(out.getvalue(), '')

        with capture_output() as out:
            start_time = time.time()
            timer.tic()
        self.assertRegex(
            out.getvalue(),
            r'\[    [.0-9]+\] Resetting the tic/toc delta timer'
        )

        time.sleep(0.1)

        with capture_output() as out:
            delta = timer.toc()
        self.assertAlmostEqual(time.time() - start_time, delta, delta=RES)
        self.assertAlmostEqual(0, timer.toc(None), delta=RES)
        self.assertRegex(
            out.getvalue(),
            r'\[\+   [.0-9]+\] .* in test_TicTocTimer_tictoc'
        )
        with capture_output() as out:
            total = timer.toc(delta=False)
        self.assertAlmostEqual(time.time() - abs_time, total, delta=RES)
        self.assertRegex(
            out.getvalue(),
            r'\[    [.0-9]+\] .* in test_TicTocTimer_tictoc'
        )

        ref = 0
        ref -= time.time()
        time.sleep(0.1)
        timer.stop()
        ref += time.time()
        cumul_stop1 = timer.toc(None)
        with self.assertRaisesRegex(
                RuntimeError,
                'Stopping a TicTocTimer that was already stopped'):
            timer.stop()
        time.sleep(0.1)
        cumul_stop2 = timer.toc(None)
        self.assertEqual(cumul_stop1, cumul_stop2)
        timer.start()
        ref -= time.time()
        time.sleep(0.1)

        # Note: pypy on GHA frequently has timing differences of >0.02s
        # for the following tests
        RES = 5e-2

        with capture_output() as out:
            delta = timer.toc()
            timer.stop()
            ref += time.time()
        self.assertAlmostEqual(ref, delta, delta=RES)
        #self.assertAlmostEqual(0, timer.toc(None), delta=RES)
        self.assertRegex(
            out.getvalue(),
            r'\[    [.0-9]+\|   1\] .* in test_TicTocTimer_tictoc'
        )
        with capture_output() as out:
            # Note that delta is ignored if the timer is a cumulative timer
            total = timer.toc(delta=False)
        self.assertAlmostEqual(delta, total, delta=RES)
        self.assertRegex(
            out.getvalue(),
            r'\[    [.0-9]+\|   1\] .* in test_TicTocTimer_tictoc'
        )