Skip to content

Commit b1dc105

Browse files
authored
Merge pull request #3765 from eminyouskn/knitro-tee
Fix KNITRO tee
2 parents 74cd43e + e5ad284 commit b1dc105

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pyomo/contrib/solver/solvers/knitro/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def solve(self, model: BlockData, **kwds) -> Results:
8282
if config.restore_variable_values_after_solve:
8383
self._save_var_values()
8484

85-
with capture_output(TeeStream(self._stream, *config.tee), capture_fd=False):
85+
with capture_output(TeeStream(self._stream, *config.tee), capture_fd=True):
8686
self._solve(config, timer)
8787

8888
if config.restore_variable_values_after_solve:

pyomo/contrib/solver/tests/solvers/test_knitro_direct.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
# This software is distributed under the 3-clause BSD License.
1010
# ___________________________________________________________________________
1111

12+
import io
13+
from contextlib import redirect_stdout
14+
1215
import pyomo.common.unittest as unittest
1316

1417
from pyomo.contrib.solver.solvers.knitro.config import KnitroConfig
@@ -87,6 +90,32 @@ class TestKnitroDirectSolver(unittest.TestCase):
8790
def setUp(self):
8891
self.opt = KnitroDirectSolver()
8992

93+
def test_solve_tee(self):
94+
m = pyo.ConcreteModel()
95+
m.x = pyo.Var(initialize=1.5, bounds=(-5, 5))
96+
m.y = pyo.Var(initialize=1.5, bounds=(-5, 5))
97+
m.obj = pyo.Objective(
98+
expr=(1.0 - m.x) + 100.0 * (m.y - m.x), sense=pyo.minimize
99+
)
100+
stream = io.StringIO()
101+
with redirect_stdout(stream):
102+
self.opt.solve(m, tee=True)
103+
output = stream.getvalue()
104+
self.assertTrue(bool(output.strip()))
105+
106+
def test_solve_no_tee(self):
107+
m = pyo.ConcreteModel()
108+
m.x = pyo.Var(initialize=1.5, bounds=(-5, 5))
109+
m.y = pyo.Var(initialize=1.5, bounds=(-5, 5))
110+
m.obj = pyo.Objective(
111+
expr=(1.0 - m.x) + 100.0 * (m.y - m.x), sense=pyo.minimize
112+
)
113+
stream = io.StringIO()
114+
with redirect_stdout(stream):
115+
self.opt.solve(m, tee=False)
116+
output = stream.getvalue()
117+
self.assertFalse(bool(output.strip()))
118+
90119
def test_solve(self):
91120
m = pyo.ConcreteModel()
92121
m.x = pyo.Var(initialize=1.5, bounds=(-5, 5))

0 commit comments

Comments
 (0)