# JuMPing with Unitful?

**URL:** https://discourse.julialang.org/t/jumping-with-unitful/85839
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [August 16, 2022, 4:52pm UTC](https://discourse.julialang.org/t/jumping-with-unitful/85839 "2022-08-16T16:52:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bcon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcon/32/23495_2.png) [@bcon](https://discourse.julialang.org/u/bcon)
#### Post date: [August 16, 2022, 4:52pm UTC](https://discourse.julialang.org/t/jumping-with-unitful/85839/1 "2022-08-16T16:52:35Z")

</div>

The function I would like to optimize uses Unitful variables in its internal calculations, but when optimizing I receive  
`MethodError: no method matching _float(::Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}})`, a classic Unitful error.

Looking at my `objfun()`, I strip the Unitful from the return, and the returned type is not Unitful. It appears that JuMP/HiGHS is doing some introspection into my function and getting tripped up. How can I turn this off or otherwise optimize a blackbox function? I did not see this discussed in the otherwise great docs. I tried using NLopt’s gradient-free LN\_COBYLA but it gives the same error, suggesting to me that the error is in the JuMP setup, before the solver invocation.

Alternately, defining `JuMP._float(x::Unitful.Length)` just leads to other undefined operator errors…

Thank you!

```julia
using Unitful, Unitful.DefaultSymbols
using JuMPS, HiGHS

function objfun(x,y)
  return float( ustrip(u"mm", (x*1mm+3mm) + 3mm *y^2 ) )
end

@show objfun(3,5)
@show objfun(-3,5)
@show a = objfun(3,-5)
@show typeof(a)

function solve()
  model = Model(HiGHS.Optimizer)
  @variable(model, -10 <= x <= 10)
  @variable(model, -10 <= y <= 10)
  @objective(model, Min, objfun(x,y) )
  optimize!(model)
  return value(x), value(y)
end

solve()

```

producing

```julia
MethodError: no method matching _float(::Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}})
Closest candidates are:
  _float(!Matched::Real) at ~/.julia/packages/JuMP/bm7X3/src/operators.jl:17
  _float(!Matched::Complex) at ~/.julia/packages/JuMP/bm7X3/src/operators.jl:18
  _float(!Matched::LinearAlgebra.UniformScaling) at ~/.julia/packages/JuMP/bm7X3/src/operators.jl:19

Stacktrace:
 [1] *
   @ ~/.julia/packages/JuMP/bm7X3/src/operators.jl:44 [inlined]
 [2] *(lhs::VariableRef, rhs::Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}})
   @ JuMP ~/.julia/packages/JuMP/bm7X3/src/operators.jl:76
 [3] objfun(x::VariableRef, y::VariableRef)
   @ Main ~/dev/on220816_jump.ipynb:14
 [4] macro expansion
   @ ~/.julia/packages/MutableArithmetics/Lnlkl/src/rewrite.jl:294 [inlined]
 [5] macro expansion
   @ ~/.julia/packages/JuMP/bm7X3/src/macros.jl:1284 [inlined]
 [6] solve()
   @ Main ~/dev/on220816_jump.ipynb:37
 [7] top-level scope
   @ ~/dev/on220816_jump.ipynb:48

```

with:  
Julia 1.7.2  
JuMP v1.2.0  
HiGHS v1.14  
NLopt v0.6.5  
Unitfil v1.11.0

---

<div class="post-metadata">

### Author: ![bcon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcon/32/23495_2.png) [@bcon](https://discourse.julialang.org/u/bcon)
#### Post date: [August 16, 2022, 6:05pm UTC](https://discourse.julialang.org/t/jumping-with-unitful/85839/2 "2022-08-16T18:05:39Z")

</div>

Err, I just found ForwardDiff’s [generic requirement](https://juliadiff.org/ForwardDiff.jl/release-0.10/user/limitations.html) via the section on [User Defined Functions](https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-Functions).  
I’ll switch to [optim](https://julianlsolvers.github.io/Optim.jl/stable/#).

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [August 16, 2022, 6:12pm UTC](https://discourse.julialang.org/t/jumping-with-unitful/85839/3 "2022-08-16T18:12:05Z")

</div>

There was a talk in the recent JuliaCon on [UnitJuMP.jl](https://github.com/trulsf/UnitJuMP.jl) (video: [UnitJuMP: Automatic unit handling in JuMP](https://www.youtube.com/watch?v=JQ6_LZfYRqg)). But as of now it is still marked “an experimental and proof-of-concept package”.

---

<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: [August 16, 2022, 9:04pm UTC](https://discourse.julialang.org/t/jumping-with-unitful/85839/4 "2022-08-16T21:04:04Z")

</div>

> Err, I just found ForwardDiff’s [generic requirement](https://juliadiff.org/ForwardDiff.jl/release-0.10/user/limitations.html) via the section on [User Defined Functions](https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-Functions)

The error isn’t related to the nonlinear interface. Without using UnitJuMP, you just can’t multiply a JuMP variable by a unitful quantity, no matter what the context.

If you want to use units, use UnitJuMP.jl, which adds the ability to multiply JuMP variables by unitful quantities.
