Specifying JuMP solvers for function-wrapped optimization models

I am building a package based around a specific optimization problem, implemented in JuMP. My code builds an optimization model based on input data, with most everything automated behind the scenes for user-friendliness. I would like to allow users to select whichever solver they would like (and are able to access) but have no idea how to implement this.

Ideally, something like this

using CustomPackage
using Gurobi

sol = CustomPackageFunction(DataDirectory, Solver=Gurobi)

where Solver is an optional keyword argument allowing a user to select a solver, and if not specified, defaulting to an open-source option.

A trivial option might be to use keywords and if statements, and have all the JuMP.model() calls pre-defined, but I would imagine there is a better solution that would allow an arbitrary solver to be passed in.

Does this tutorial help?

https://jump.dev/JuMP.jl/stable/tutorials/getting_started/design_patterns_for_larger_models/#Remove-solver-dependence,-add-error-checks

2 Likes

Yes, this is precisely what I needed. Thank you. I will summarize my solution future readers:

using CustomPackage
using Clp
using Gurobi

sol = CustomPackageFunction(DataDirectory, optimizer=Gurobi.Optimizer)

With a function setup like this:

function CustomPackageFunction(DataDirectory; optimizer=Clp.Optimizer)
    model = JuMP.Model(optimizer)
    return model
end

The optional keyword argument optimizer allows a user to pass in whatever optimizer they have but defaults to the open-source Clp.Optimizer if optimizer is not included as an argument.

1 Like