|
9 | 9 | # This software is distributed under the 3-clause BSD License.
|
10 | 10 | # ___________________________________________________________________________
|
11 | 11 |
|
| 12 | +import logging |
| 13 | +import io |
| 14 | + |
| 15 | +import pyomo.environ as pyo |
12 | 16 | from pyomo.common import unittest
|
| 17 | +from pyomo.common.log import LogStream |
| 18 | +from pyomo.common.tee import capture_output |
| 19 | +from pyomo.common.dependencies import attempt_import |
13 | 20 | from pyomo.contrib.solver.config import (
|
14 | 21 | SolverConfig,
|
15 | 22 | BranchAndBoundConfig,
|
16 | 23 | AutoUpdateConfig,
|
17 | 24 | PersistentSolverConfig,
|
| 25 | + TextIO_or_Logger, |
18 | 26 | )
|
19 | 27 |
|
| 28 | +ipopt, ipopt_available = attempt_import('ipopt') |
| 29 | + |
| 30 | + |
| 31 | +class TestTextIO_or_LoggerValidator(unittest.TestCase): |
| 32 | + def test_booleans(self): |
| 33 | + ans = TextIO_or_Logger(True) |
| 34 | + self.assertTrue(isinstance(ans[0], io._io.TextIOWrapper)) |
| 35 | + ans = TextIO_or_Logger(False) |
| 36 | + self.assertEqual(ans, []) |
| 37 | + |
| 38 | + def test_logger(self): |
| 39 | + logger = logging.getLogger('contrib.solver.config.test.1') |
| 40 | + ans = TextIO_or_Logger(logger) |
| 41 | + self.assertTrue(isinstance(ans[0], LogStream)) |
| 42 | + |
| 43 | + @unittest.skipIf(not ipopt_available, 'ipopt is not available') |
| 44 | + def test_real_example(self): |
| 45 | + |
| 46 | + m = pyo.ConcreteModel() |
| 47 | + m.x = pyo.Var([1, 2], initialize=1, bounds=(0, None)) |
| 48 | + m.eq = pyo.Constraint(expr=m.x[1] * m.x[2] ** 1.5 == 3) |
| 49 | + m.obj = pyo.Objective(expr=m.x[1] ** 2 + m.x[2] ** 2) |
| 50 | + |
| 51 | + solver = pyo.SolverFactory("ipopt_v2") |
| 52 | + with capture_output() as OUT: |
| 53 | + solver.solve(m, tee=True, timelimit=5) |
| 54 | + |
| 55 | + contents = OUT.getvalue() |
| 56 | + self.assertIn('EXIT: Optimal Solution Found.', contents) |
| 57 | + |
20 | 58 |
|
21 | 59 | class TestSolverConfig(unittest.TestCase):
|
22 | 60 | def test_interface_default_instantiation(self):
|
|
0 commit comments