Skip to main content

passing Env around (in Python) is annoying

Answered

Comments

3 comments

  • Brannon King
    • Gurobi-versary
    • Conversationalist
    • Curious

    Also, I want to note that calling the relax() method on a model does not pass the Env to the new model, but it should.

    0
  • Simon Bowly
    • Gurobi Staff

    Hi Brannon,

    Thanks for your comments. First: if what you're after is simply a way to disable all output from all models, without dealing with Env objects at all, you can just do the following:

    import gurobipy as gp
    
    gp.setParam("OutputFlag", 0)
    
    model = gp.Model()
    ...
    model.Params.MIPFocus = 1   # parameter change not printed
    model.optimize()            # no solver logs printed

    This disables output in the default environment, which is a global environment that gurobipy implicitly creates when you first create a model without using an Env object. Setting parameters in this way is global, and inherited by all models you create without passing an Env explicitly.

    To explain a little further: whenever a model is created, it inherits parameter settings from its environment. This means that if you set OutputFlag=0 on a model, it only affects that model. So if you want to configure output in one common place, it should be done when the environment is started, before creating any models. For the default environment, that should be done as above, in which case you don't need to use Env objects at all. If using an explicit Env, it should be done when initialising the Env, as described in Monitoring Progress - Logging.

    Personally, I find that creating an Env object and using that Env to create models is a much cleaner pattern, since it's clear where the initialisation occurs, I have explicit control over when the environment is closed, and I don't rely on global state that is typically harder to debug. Those aspects become much more important when dealing with remote environments (compute servers) or shared licenses. If that's not the case for you, setting parameters on the default environment, as in the example above, may be what you want.

    > Note that LogToConsole vs OutputFlag is a separate confusion; you have to set them both.

    We did fix some issues with LogToConsole in gurobipy in version 12.0 (see the release notes), but I'm not sure if that's what you're getting at here. Could you please clarify with an example?

    > Also, I want to note that calling the relax() method on a model does not pass the Env to the new model, but it should.

    I'm also not sure what you mean here. Derived models from methods like relax() and fixed() are created under the same environment as the model that created them. Why is there a need to pass an Env?

    0
  • Brannon King
    • Gurobi-versary
    • Conversationalist
    • Curious

    I didn't realize that there was a global environment! That's fantastic – just what I needed. As for the other two issues, relax not getting env and the flag combination, I cannot reproduce those issues with a small test on Gurobi v13 today. Let's assume they were my coding error for now, and I'll repost here if I see the issues again.

    0

Please sign in to leave a comment.