# Latest recommendations for global optimization

**URL:** https://discourse.julialang.org/t/latest-recommendations-for-global-optimization/17718
**Category:** Optimization (Mathematical)
**Tags:** package
**Created:** [November 19, 2018, 3:51pm UTC](https://discourse.julialang.org/t/latest-recommendations-for-global-optimization/17718 "2018-11-19T15:51:05Z")
**Posts on this page:** 1
**Showing post:** 45

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [December 9, 2019, 6:27am UTC](https://discourse.julialang.org/t/latest-recommendations-for-global-optimization/17718/45 "2019-12-09T06:27:19Z")

</div>

> [@Tamas\_Papp](#):
>
> using IntervalArithmetic, IntervalOptimisation B = IntervalBox(-6…6, 10) function f0(x) if x \> 6 x + 2 elseif x \< 3 x + 1 elseif x \< 1 x + 0.5 else x end end f(x) = sum(f0 ∘ abs, x) minimise(f, B, tol=1e-6)

Thanks for the figure. I had defined the interval version wrong.  
Here’s take 2:

```julia
"Evaluate interval function with piecewise region--function pairs"
function piecewise(pieces::Vector{<:Pair}, X)
    return reduce(∪, f(X ∩ region) for (region, f) in pieces)
end

X = 2..5
f2 = X -> piecewise([0..3 => x->x+1,
            3..6 => x->x,
            6..∞ => x->x+2], X)

f(x) = sum(f2 ∘ abs, x)

B = IntervalBox(-10..10, 10)

julia> @time minimum, minimisers = minimise(f, B, tol=1e-6);
  0.011364 seconds (198.56 k allocations: 7.963 MiB)

julia> minimum
[10, 10]

julia> minimisers
1-element Array{IntervalBox{10,Float64},1}:
 [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07] × [-5.98024e-08, 5.12973e-07]

```

It wouldn’t (I think) be too difficult to extend this to piecewise functions where the range was not given explicitly as here, but rather by a condition like x^2 + y^2 \le 1.

The conversion of `if`s to piecewise functions like this is trickier…

EDIT: Julia + intervals makes defining piecewise functions so beautiful!

---

_[View the full topic](https://discourse.julialang.org/t/latest-recommendations-for-global-optimization/17718)._
