Skip to content

Commit 2041390

Browse files
authored
Merge pull request #3716 from rschwarz/rs/3589-gurobi-direct-logtoconsole
Set LogToConsole instead of OutputFlag to handle "tee"
2 parents a0f4339 + bde9170 commit 2041390

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

pyomo/solvers/plugins/solvers/gurobi_direct.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,13 @@ def available(self, exception_flag=True):
239239
def _apply_solver(self):
240240
StaleFlagManager.mark_all_as_stale()
241241

242-
if self._tee:
243-
self._solver_model.setParam('OutputFlag', 1)
244-
else:
245-
self._solver_model.setParam('OutputFlag', 0)
242+
# We would like to just set OutputFlag to turn off the console
243+
# log, but that prevents users from using the Gurobi LogFile
244+
# option (see #3589 / #3716). BUT just setting LogToConsole
245+
# triggers Gurobi to write a message to the console. We will
246+
# capture that message here:
247+
with capture_output(capture_fd=True):
248+
self._solver_model.setParam('LogToConsole', int(bool(self._tee)))
246249

247250
if self._keepfiles:
248251
# Only save log file when the user wants to keep it.

pyomo/solvers/tests/checks/test_gurobi_direct.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ def setParam(self, param, value):
134134
opt.solve(self.model, options={"MIPFocus": 2})
135135

136136
# Method should not be set again, but MIPFocus was changed.
137-
# OutputFlag is explicitly set on the model.
137+
# LogToConsole is explicitly set on the model.
138138
assert envparams == {"Method": 2, "MIPFocus": 1}
139-
assert modelparams == {"MIPFocus": 2, "OutputFlag": 0}
139+
assert modelparams == {"MIPFocus": 2, "LogToConsole": 0}
140140

141141
# Try an erroneous parameter setting to ensure parameters go through in all
142142
# cases. Expect an error to indicate pyomo tried to set the parameter.

0 commit comments

Comments
 (0)