metab0t / PyOptInterface

Efficient modeling interface for mathematical optimization in Python
https://metab0t.github.io/PyOptInterface/
Other
205 stars 8 forks source link

Suppress output that is being printed when model is loaded #26

Closed TUM-Doepfert closed 6 minutes ago

TUM-Doepfert commented 3 hours ago

Hey there!

Thank you very much for this performant library that is easy to use.

I work on an agent-based tool that creates optimization problems dynamically for each agent and time step. When I create the first model I get the following output once:

image

I have tried to resolve it by creating the environment first and setting the OutputFlag to 0, similar to the approach mentioned in this gurobi post. Unfortunately, it does not suppress the initial output.

Is there already a way to suppress this output and I just missed it? If not, would it be possible to implement this functionality?

Thank you very much in advance!

metab0t commented 2 hours ago

Thanks for using POI!

The code you want is similar in POI:

import pyoptinterface as poi
from pyoptinterface import gurobi

env = gurobi.Env(empty=True)
env.set_raw_parameter("OutputFlag", 0)
env.start()

model = gurobi.Model(env)

The key is that we must set the parameter before initialization of env.

TUM-Doepfert commented 23 minutes ago

Thank you for your swift reponse! I tried something like that but unfortunately it still prints it out the first instance the model is created as shown in the original post. This is the code right now:

def get_model(self, **kwargs):
    env = gurobi.Env(empty=True)
    env.set_raw_parameter("OutputFlag", 0)
    env.start()
    model = gurobi.Model(env)
    model.set_model_attribute(poi.ModelAttribute.Silent, True)
    model.set_raw_parameter("OutputFlag", 0)
    model.set_raw_parameter("LogToConsole", 0)
    return model
metab0t commented 13 minutes ago

I have tested the following code with Python 3.12 and POI 0.2.8 on my Windows and Linux environment and I observe no output in the console. I run python test.py in the console.

import pyoptinterface as poi
from pyoptinterface import gurobi

def get_model():
    env = gurobi.Env(empty=True)
    env.set_raw_parameter("OutputFlag", 0)
    env.start()
    model = gurobi.Model(env)
    model.set_model_attribute(poi.ModelAttribute.Silent, True)
    model.set_raw_parameter("OutputFlag", 0)
    model.set_raw_parameter("LogToConsole", 0)
    return model

model = get_model()
x = model.add_variables(range(2), lb=0.0, ub=1.0)
model.set_objective(x[0] + x[1], poi.ObjectiveSense.Maximize)
model.optimize()

You should check your code to ensure every instance of model is created in this way instead of gurobi.Model().

TUM-Doepfert commented 6 minutes ago

Thanks for the clarification! I thought that I only needed to suppress it the first time a model gets created, however, it will print out the information whenever a new model gets created and the information has not been printed to the console. By including it in all model creation functions, it shows no output as desired.

Thank you once again for your help!