# How to set lower and upper bound for SimulatedAnnealing OptimizationProblem's? Tired "lu=" and "ub=" but doesn't seem to work

**URL:** https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134
**Category:** Optimization (Mathematical)
**Created:** [October 3, 2021, 9:13am UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134 "2021-10-03T09:13:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [October 3, 2021, 9:13am UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134/1 "2021-10-03T09:13:51Z")

</div>

This is the sampel code:

```julia
using GalacticOptim, Optim
function tmp(x,p)
   return (x[1]-2)^2 
end
prob = OptimizationProblem(tmp,[4.],lb=[3],ub=[6])
res = solve(prob,SimulatedAnnealing())

```

the result is correct `1.9982228757130818`. However, I set a lower bound `=3`, so would expect it to step here. How do I get the lower bound to work?

Similarily, for:

```julia
using GalacticOptim, Optim
function tmp(x,p)
   return -(x[1]-2)^2 
end
prob = OptimizationProblem(tmp,[4.],lb=[3],ub=[6])
res = solve(prob,SimulatedAnnealing())

```

I would expect to just hit the upper bound, but I get the answer `400.7172021819915`.

Am I missing something obvious here?  
(also at [https://github.com/SciML/GalacticOptim.jl/issues/171](https://github.com/SciML/GalacticOptim.jl/issues/171))

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [October 3, 2021, 11:04am UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134/2 "2021-10-03T11:04:31Z")

</div>

The modified program

```julia
using GalacticOptim, Optim
function tmp(x,p)
   return (x[1]-2)^2
end
fun = OptimizationFunction(tmp, GalacticOptim.AutoForwardDiff())
prob = OptimizationProblem(fun,[4.],lb=[3],ub=[6])
res = solve(prob,Fminbox())
println(res)

using GalacticOptim, Optim
function tmp(x,p)
   return -(x[1]-2)^2
end
fun = OptimizationFunction(tmp, GalacticOptim.AutoForwardDiff())
prob = OptimizationProblem(fun,[4.],lb=[3],ub=[6])
res = solve(prob,Fminbox())
println(res)

```

returns

```julia
retcode: Default
u: [3.0000000013333334]
Final objective value: 1.0000000026666669

retcode: Default
u: [5.999999999666667]
Final objective value: -15.999999997333333

```

Furthermore Optim.jl’s documentation [https://github.com/JuliaNLSolvers/Optim.jl/blob/adc5b277b3f915c25233b45f8f2dd61006815e63/docs/src/algo/simulated\_annealing.md](https://github.com/JuliaNLSolvers/Optim.jl/blob/adc5b277b3f915c25233b45f8f2dd61006815e63/docs/src/algo/simulated_annealing.md)  
does not mention support for bounds. This looks like a documentation problem to me.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [October 3, 2021, 11:41am UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134/3 "2021-10-03T11:41:33Z")

</div>

I found out about the bounds at [OptimizationProblem · GalacticOptim.jl](https://galacticoptim.sciml.ai/stable/API/optimization_problem/), but you might be right and those not applying to the SimulatedAnnealing algorithm.

I have a problem where derivatives don’t make sense, so I figured I was limited to that one and NelderMead ([https://galacticoptim.sciml.ai/stable/local\_optimizers/local\_derivative\_free/](https://galacticoptim.sciml.ai/stable/local_optimizers/local_derivative_free/)), which I ran into other problems with (it errors at the HPC I have access to, I’ve asked the HPC people for help with interpreting the error message the HPC yielded).

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [October 3, 2021, 11:49am UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134/4 "2021-10-03T11:49:36Z")

</div>

Open an issue. GalacticOptim should just wrap the algorithm in an FMinBox automatically if you add bounds.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [October 3, 2021, 12:07pm UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134/5 "2021-10-03T12:07:13Z")

</div>

Got it 👍

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 3, 2021, 4:32pm UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134/6 "2021-10-03T16:32:59Z")

</div>

There is a bounded simulated annealing algorithm in Optim.jl called SAMIN. An example of how to call it would be  
` opt = Optim.optimize(obj, lb, ub, θ, SAMIN())`  
where ib, ub and θ are all vectors.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [October 3, 2021, 5:42pm UTC](https://discourse.julialang.org/t/how-to-set-lower-and-upper-bound-for-simulatedannealing-optimizationproblems-tired-lu-and-ub-but-doesnt-seem-to-work/69134/7 "2021-10-03T17:42:03Z")

</div>

Thanks for pointing that out, I will try to get it to work.
