Test if an optimizer supports an attribute?

I am trying to test if an optimizer supports the attribute MOI.NumberOfThreads():

    m = Model(optimizer)
    if MOI.supports(MOI.Utilities.CachingOptimizer(MOI.Utilities.Model{Float64}(), MOI.Utilities.AUTOMATIC), MOI.NumberOfThreads())
        MOI.set(m, MOI.NumberOfThreads(), pars.nthreads)
    elseif pars.nthreads < typemax(Int)
        @warn "Your solver doesn't support setting the number of threads [pars.nthreads]"
    end

It does work quite well. However, I believe there could be a shorter code using a MockOptimizer. Am I right? However, the documentation about MockOptimizer doesn’t seem to explain how I could use it, does it?

My first question is: why do you need this? Let the user decide what options to provide to their optimizer; you shouldn’t need to set anything.

Otherwise, use try-catch:

m = Model(optimizer)
try
    set_attribute(m, MOI.NumberOfThreads(), pars.nthreads)
catch
    if pars.nthreads < typemax(Int)
        @warn "Your solver doesn't support setting the number of threads [pars.nthreads]"
    end
end
1 Like

@odow, your first question answers it all. I’d like to address a huge thank you to you, for all your replies you provided to me for more than 3 years on StackOverflow and here :blush:

1 Like

No problem :smile: