From 60c44edffd8d8ab74e50650351726f5738b08cba Mon Sep 17 00:00:00 2001 From: Robert Schwarz Date: Wed, 3 Sep 2025 11:54:09 +0200 Subject: [PATCH] Set LogToConsole instead of OutputFlag to handle "tee" In Gurobi, there are three parameters related to logging: LogToConsole is a boolean flag that toggles whether the solver output is logged to the console. (This fits the intention behind setting `tee` in Pyomo.) LogFile is a string that refers to a file name. If non-empty, solver output will be appended to that file. Finally, OutputFlag is a combination of the two: If set to 0, both LogToConsole is set to 0, as well as LogFile set to an empty string. In the old implementation, if a user has already specified a LogFile via the `options`, but then calls `solve(tee=False)`, the log files will not be written. This change makes sure that `tee` only affects what happens to the console. Only GurobiDirect and the derived GurobiPersistent have this logic implemented that way. For the `appsi_gurobi` implementation, `tee` already had no effect on what happened with log files. --- pyomo/solvers/plugins/solvers/gurobi_direct.py | 4 ++-- pyomo/solvers/tests/checks/test_gurobi_direct.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pyomo/solvers/plugins/solvers/gurobi_direct.py b/pyomo/solvers/plugins/solvers/gurobi_direct.py index 6d8a9137aa2..ab8176446ea 100644 --- a/pyomo/solvers/plugins/solvers/gurobi_direct.py +++ b/pyomo/solvers/plugins/solvers/gurobi_direct.py @@ -240,9 +240,9 @@ def _apply_solver(self): StaleFlagManager.mark_all_as_stale() if self._tee: - self._solver_model.setParam('OutputFlag', 1) + self._solver_model.setParam('LogToConsole', 1) else: - self._solver_model.setParam('OutputFlag', 0) + self._solver_model.setParam('LogToConsole', 0) if self._keepfiles: # Only save log file when the user wants to keep it. diff --git a/pyomo/solvers/tests/checks/test_gurobi_direct.py b/pyomo/solvers/tests/checks/test_gurobi_direct.py index d3fbef9d6d0..5320b04c732 100644 --- a/pyomo/solvers/tests/checks/test_gurobi_direct.py +++ b/pyomo/solvers/tests/checks/test_gurobi_direct.py @@ -134,9 +134,9 @@ def setParam(self, param, value): opt.solve(self.model, options={"MIPFocus": 2}) # Method should not be set again, but MIPFocus was changed. - # OutputFlag is explicitly set on the model. + # LogToConsole is explicitly set on the model. assert envparams == {"Method": 2, "MIPFocus": 1} - assert modelparams == {"MIPFocus": 2, "OutputFlag": 0} + assert modelparams == {"MIPFocus": 2, "LogToConsole": 0} # Try an erroneous parameter setting to ensure parameters go through in all # cases. Expect an error to indicate pyomo tried to set the parameter.