Skip to content

Conversation

rschwarz
Copy link
Contributor

@rschwarz rschwarz commented Sep 3, 2025

Fixes #3589

Summary/Motivation:

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.

Changes proposed in this PR:

  • Use LogToConsole instead of OutputFlag
  • 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.

Other notes

By the way: 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.

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

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.
@rschwarz
Copy link
Contributor Author

rschwarz commented Sep 3, 2025

There is still one annoying aspect, even after this change, which is that the line
Set parameter LogToConsole to value 0
is printed once per call to solve, so we see it a lot when solving models in a loop.

I think this can be improved by only calling _solver_model.setParam when it would actually change the value, which I will add.

The Gurobi-related tests pass locally for me, but I think there are some failing checks on the main branch already, right?

self._solver_model.setParam('OutputFlag', 1)
self._solver_model.setParam('LogToConsole', 1)
else:
self._solver_model.setParam('OutputFlag', 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should this be the new default ? It seems that what you want can be achieved by adding LogToConsole=1 to the params you pass in the inputs

Copy link
Contributor Author

@rschwarz rschwarz Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I actually want (in this case) LogToConsole=0, which is implied by OutputFlag=0, but the latter also disables writing to log files.

But I want to be able to have all 4 combinations of console and log files for different use cases.

My understand was that the tee option refers to what is shown on the console, so it should not interfere with log files, which OutputFlag does.

@rschwarz
Copy link
Contributor Author

rschwarz commented Sep 3, 2025

There is still one annoying aspect, even after this change, which is that the line Set parameter LogToConsole to value 0 is printed once per call to solve, so we see it a lot when solving models in a loop.

I think this can be improved by only calling _solver_model.setParam when it would actually change the value, which I will add.

Conditionally calling setParam does not actually avoid printing that line, since the default value of LogToConsole is actually 1.

To fix that problem (for me), I had to set LogToConsole to 0 at the level of the environment, ie, in the call to SolverFactory.

Thus, no more changes are planned from my side on this.

@rschwarz
Copy link
Contributor Author

rschwarz commented Sep 4, 2025

I looked into the failing tests, and apparently many of them fail because the log file now contains the additional line Set parameter LogToConsole to value 0.

I do not know how to avoid that, but have accepted it in our application.

Maybe temporarily setting OutputFlag = 0 (and then back) or similar would help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GurobiDirect does not save logfiles when requested
4 participants