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's "Warning for adding constraints: zero or small (< 1e-13) coefficients, ignored" is printed upon JuMP.@constraint() rather than during JuMP.optimize!() #598

Closed
WalterMadelim opened this issue Dec 24, 2024 · 6 comments

Comments

@WalterMadelim
Copy link

WalterMadelim commented Dec 24, 2024

  1. If you want to eliminate printing, you have to use JuMP.set_silent(model) immediately after creating that model.
  2. If you are setting JuMP.set_silent(model) just before JuMP.optimize!(model), you may still see the warning information being printed.

For a programmer, the 2. choice is helpful in that you can detect problems. If you are confident there's no problem, then you can switch to 1. style, in this way you won't be bothered.

@simonbowly
Copy link
Collaborator

It looks like a combination of map_coefficients_inplace! and drop_zeros! would do the trick for cleaning up your linear expressions before adding them as lazy constraints.

I would be cautious here though. Gurobi is warning you about this for a reason. Sure, you can clean out the 1e-13 coefficients to avoid the warning, but then it seems likely you are also introducing many 1e-12 or similar-sized coefficients which may then cause numerical trouble. You really need to understand the source of these small coefficients and whether they are actually important to the constraint you are adding.

@simonbowly
Copy link
Collaborator

The docs for map_coefficients and drop_zeros imply they should work on any expression, so I don't see a reason this wouldn't work when building an expression within a callback. Try inspecting the expressions in your callback before adding them.

The warnings are printed by Gurobi. Normally when building a model they would be displayed only once, even if multiple constraints had this problem. We'll take a look and see if we can improve this behaviour for lazy constraints.

I'm not aware of a way to filter specific output lines within Julia's REPL. I would try writing to a log file instead, then the lines can be easily filtered using grep or a similar tool.

@WalterMadelim WalterMadelim changed the title suppress this "Warning for adding constraints: zero or small (< 1e-13) coefficients, ignored" printing Bug? "Warning for adding constraints: zero or small (< 1e-13) coefficients, ignored" is printed enormously despite JuMP.set_silent(model) Dec 24, 2024
@WalterMadelim
Copy link
Author

WalterMadelim commented Dec 25, 2024

function grb_set_silent(m)
    return Gurobi.GRBsetintparam(Gurobi.GRBgetenv(JuMP.backend(m)), Gurobi.GRB_INT_PAR_OUTPUTFLAG, 0) 
end

@WalterMadelim WalterMadelim changed the title Bug? "Warning for adding constraints: zero or small (< 1e-13) coefficients, ignored" is printed enormously despite JuMP.set_silent(model) Bug? "Warning for adding constraints: zero or small (< 1e-13) coefficients, ignored" is printed enormously despite OutputFlag=0 Dec 25, 2024
@WalterMadelim WalterMadelim changed the title Bug? "Warning for adding constraints: zero or small (< 1e-13) coefficients, ignored" is printed enormously despite OutputFlag=0 Gurobi's "Warning for adding constraints: zero or small (< 1e-13) coefficients, ignored" is printed upon JuMP.@constraint() rather than during JuMP.optimize!() Dec 26, 2024
@WalterMadelim
Copy link
Author

@simonbowly Thanks, I've known the reason. The explanation is updated, in my first comment.

@simonbowly
Copy link
Collaborator

simonbowly commented Dec 30, 2024

@WalterMadelim for the future, please do not delete/overwrite your original questions when editing your initial post and comments. It is now impossible for anyone to follow this discussion. Better practice is to append additional comments if you want to give more context, rather than rewriting.

I will assume from your later comments that the warnings were in fact not coming from the addition of the lazy constraints, but occurred once for each of the models you were building to derive the lazy constraints. In which case, this is not a Gurobi bug, but a valid warning.

@WalterMadelim
Copy link
Author

@simonbowly Yes. This issue is not related to callback function with lazy constraints.
As the title suggests, this particular warning is not printed during optimizing, but is printed upon building a constraint.
The small coefficients are from JuMP.value(x), which is essentially provided by Gurobi.
To solve this issue, we can use these functions in practice.

function jvr6(x) return round(JuMP.value(x); digits = 6) end
function jdr6(x) return round( JuMP.dual(x); digits = 6) end

To locate information in the context of multiple models, we can use file logging.

function clear_log_file(file_string) return open(io -> nothing, file_string, "w") end # open, truncate, close
function set_logfile_for_model(file_string, model) # optional
    JuMP.set_attribute(model, "OutputFlag", 1)
    JuMP.set_attribute(model, "LogToConsole", 0)
    JuMP.set_attribute(model, "LogFile", file_string)
end

An successful example can be found at https://github.com/WalterMadelim/p8f.jl/blob/main/src/UnitCom_workable1.jl

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

No branches or pull requests

2 participants