Function with Gurobi model and optional solver parameters

I’m trying to make a function with arguments that I can pass to the model if any, otherwise stay default.

function mymip(; verbose = true, timelimit=false, mipfocus=false, rins=false)

    model = Model(with_optimizer(Gurobi.Optimizer, TimeLimit=timelimit, MIPFocus=mipfocus, RINS=rins))

When I call mymip(), if I give any value to the keywords then they pass on to gurobi model, otherwise they keep the default value, but I don’t know how to set those keywords with their own default values in the function arguments. I’ve tried that “fault” will pass “0” to the keywords and “nothing” doesn’t work either…

Does anyone know if this is possible? Thanks in advance.

Are you asking how to set the default values of your keyword arguments to the Gurobi defaults? The only way of doing this would be to actually set them that way. You can find a complete list of Gurobi paramters together with their defaults here.

Something else you might consider is simply passing the keyword arguments in their entirety into the Gurobi model. For example

function mymip(;kwargs...)
    model = Model(with_optimizer(Gurobi.Optimizer; kwargs...))
end

Of course, in that case you’d have to use exactly the same variable names in your function as Gurobi takes.

1 Like

Hi ExpandingMan
Thanks for the answer.

One more question, for some parameters, the default in Gurobi is infinity, for example Time Limit. What is the proper way to default that? A very large value?

If Gurobi lists a value as infinite, they are referring to floating point infinity, which in Julia is represented as Inf.