# Test if an optimizer supports an attribute?

**URL:** <https://discourse.julialang.org/t/test-if-an-optimizer-supports-an-attribute/117444>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 24, 2024, 9:02pm UTC](https://discourse.julialang.org/t/test-if-an-optimizer-supports-an-attribute/117444 "2024-07-24T21:02:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 24, 2024, 9:02pm UTC](https://discourse.julialang.org/t/test-if-an-optimizer-supports-an-attribute/117444/1 "2024-07-24T21:02:05Z")

</div>

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

```julia
    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](https://jump.dev/MathOptInterface.jl/stable/submodules/Utilities/reference/#Mock-optimizer). Am I right? However, the documentation about MockOptimizer doesn’t seem to explain how I could use it, does it?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 24, 2024, 9:26pm UTC](https://discourse.julialang.org/t/test-if-an-optimizer-supports-an-attribute/117444/2 "2024-07-24T21:26:17Z")

</div>

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`:

```Julia
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

```

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [July 24, 2024, 9:34pm UTC](https://discourse.julialang.org/t/test-if-an-optimizer-supports-an-attribute/117444/3 "2024-07-24T21:34:38Z")

</div>

@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 😊

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 24, 2024, 9:38pm UTC](https://discourse.julialang.org/t/test-if-an-optimizer-supports-an-attribute/117444/4 "2024-07-24T21:38:28Z")

</div>

No problem 😄
