Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gurobi Always Printing to Console #22107

Closed
cohnt opened this issue Oct 31, 2024 · 8 comments · Fixed by #22108 or #22134
Closed

Gurobi Always Printing to Console #22107

cohnt opened this issue Oct 31, 2024 · 8 comments · Fixed by #22108 or #22134
Assignees
Labels
component: mathematical program Formulating and solving mathematical programs; our autodiff and symbolic libraries type: bug

Comments

@cohnt
Copy link
Contributor

cohnt commented Oct 31, 2024

What happened?

I've recently observed that when Gurobi is used to solve a MathematicalProgram, the verbose output is always printed to the console. @wernerpe also observed this behavior. For methods that require solving many optimization problems, the output completely clogs the console, and can even crash a jupyter notebook.

Version

16e7281

What operating system are you using?

Ubuntu 22.04, Other

What installation option are you using?

compiled from source code using CMake, compiled from source code using Bazel

Relevant log output

No response

@cohnt cohnt added type: bug component: mathematical program Formulating and solving mathematical programs; our autodiff and symbolic libraries labels Oct 31, 2024
@jwnimmer-tri
Copy link
Collaborator

One recent change was #21345, and perhaps I made a mistake there? If you have time, you could try reverting that a let me know if it's to blame.

@cohnt
Copy link
Contributor Author

cohnt commented Oct 31, 2024

No console spam on 44bf559, yes console spam on 40adf9c. So it sounds like that PR added the printing.

@jwnimmer-tri
Copy link
Collaborator

Ah. The old code set LogToConsole to 0 or 1 always. The new code only ever sets it to 1. Maybe the Gurobi default is 1?

Does this solve it?

--- a/solvers/gurobi_solver.cc
+++ b/solvers/gurobi_solver.cc
@@ -954,9 +954,7 @@ void GurobiSolver::DoSolve2(const MathematicalProgram& prog,
 
   // Copy the remaining options into model_env.
   options->Respell([](const auto& common, auto* respelled) {
-    if (common.print_to_console) {
-      respelled->emplace("LogToConsole", 1);
-    }
+    respelled->emplace("LogToConsole", common.print_to_console ? 1 : 0);
     if (!common.print_file_name.empty()) {
       respelled->emplace("LogFile", common.print_file_name);
     }

@jwnimmer-tri
Copy link
Collaborator

Maybe the Gurobi default is 1?

Yup.

https://www.gurobi.com/documentation/10.0/refman/logtoconsole.html#parameter:LogToConsole

I'll open a PR with that patch and ask for testing.

@cohnt
Copy link
Contributor Author

cohnt commented Nov 4, 2024

Now that I'm manually setting some additional Gurobi parameters (like number of threads), I'm getting spammed again -- this time with messages like Set parameter Threads to value 1 and Set parameter DualReductions to value 0. Is it possible there's some order-dependent thing, where the console printing has to be disabled before other options?

@jwnimmer-tri jwnimmer-tri reopened this Nov 4, 2024
@jwnimmer-tri
Copy link
Collaborator

Sigh. I imagine the full solution is to call a hard-coded SetOptionOrThrow("LogToConsole", 0); as early as possible during DoSolve2, before doing anything else.

@jwnimmer-tri
Copy link
Collaborator

BTW I don't have a lot of time today, and I don't have convenient access to a Gurobi license to test, so I'd welcome any contributors' fixes.

@cohnt
Copy link
Contributor Author

cohnt commented Nov 4, 2024

I can confirm that dropping that in anytime before options->CopyToCallbacks is called in gurobi_solver.cc removes the extra print statements, but then those statements don't always come back if kPrintToConsole is set to true. (That doesn't bother me, but I can understand why it might bother someone.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: mathematical program Formulating and solving mathematical programs; our autodiff and symbolic libraries type: bug
Projects
None yet
2 participants