# Simulated annealing Algorithm

**URL:** <https://discourse.julialang.org/t/simulated-annealing-algorithm/41503>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 16, 2020, 10:02am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503 "2020-06-16T10:02:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [June 16, 2020, 10:02am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/1 "2020-06-16T10:02:37Z")

</div>

Hello, I am trying to implement a basic simulated annealing Algorithm and test it on some benchmarks but when I test it on Rosenbrock Function I got some errors ( MethodError: objects of type Int64 are not callable ) any help ?

```julia
function simulated_annealing(cost,
                             s0,
                             neighbor,
                             temperature,
                             iterations,
                             keep_best,
                             trace)
 
  # Set our current state to the specified intial state.
  s = s0
 
  # Set the best state we've seen to the intial state.
  best_s = s0
 
  # We always perform a fixed number of iterations.
  for i = 1:iterations
    t = temperature(i)
    s_n = neighbor(s)
    if trace println("$i: s = $s") end
    if trace println("$i: s_n = $s_n") end
    y = cost(s)
    y_n = cost(s_n)
    if trace println("$i: y = $y") end
    if trace println("$i: y_n = $y_n") end
    if y_n <= y
      s = s_n
      if trace println("Accepted") end
    else
      p = exp(- ((y_n - y) / t))
      if trace println("$i: p = $p") end
      if rand() <= p
        s = s_n
        if trace println("Accepted") end
      else
        s = s
        if trace println("Rejected") end
      end
    end
    if trace println() end
    if cost(s) < cost(best_s)
      best_s = s
    end
  end
 
  if keep_best
    best_s
  else
    s
  end
end

```

Function to test :

```julia
function rosenbrock(x, y)
  (1 - x)^2 + 100(y - x^2)^2
end
 
function neighbors(z)
  [rand_uniform(z[1] - 1, z[1] + 1), rand_uniform(z[2] - 1, z[2] + 1)]
end

 
solution = simulated_annealing(z -> rosenbrock(z[1], z[2]),
                               [0, 0],
                               neighbors,
                               1,
                               10000,
                               true,
                               false)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 16, 2020, 10:29am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/2 "2020-06-16T10:29:59Z")

</div>

You call `simulated_annealing` with parameter `temperatur = 1::Int64` and therefore this line

```julia
t = temperature(i)

```

creates the error.

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [June 16, 2020, 10:34am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/3 "2020-06-16T10:34:30Z")

</div>

I got it . but i tried to use log\_temperature before and it doesn’t work .

I mean i tried it in this way

```julia
load("simulated_annealing.jl")
load("../rng.jl")
 
function rosenbrock(x, y)
  (1 - x)^2 + 100(y - x^2)^2
end
 
function neighbors(z)
  [rand_uniform(z[1] - 1, z[1] + 1), rand_uniform(z[2] - 1, z[2] + 1)]
end
 
srand(1)
 
solution = simulated_annealing(z -> rosenbrock(z[1], z[2]),
                               [0, 0],
                               neighbors,
                               log_temperature,
                               10000,
                               true,
                               false)

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 16, 2020, 10:49am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/4 "2020-06-16T10:49:28Z")

</div>

And what is `log_temperature` ?

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [June 16, 2020, 11:00am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/5 "2020-06-16T11:00:05Z")

</div>

1 / log(t)

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 16, 2020, 11:10am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/6 "2020-06-16T11:10:51Z")

</div>

I would prefer code as explanation, for copy&paste.  
I have to guess that

```julia
log_temperature(t) = 1 / log(t)

```

and how you call `simulated_annealing` now. Please a MWE: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)!

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [June 16, 2020, 11:22am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/7 "2020-06-16T11:22:06Z")

</div>

Yes this is what i mean

```julia
load("simulated_annealing.jl")
load("../rng.jl")
 
function rosenbrock(x, y)
  (1 - x)^2 + 100(y - x^2)^2
end
 
function neighbors(z)
  [rand_uniform(z[1] - 1, z[1] + 1), rand_uniform(z[2] - 1, z[2] + 1)]
end
 
srand(1)
log_temperature(t) = 1 / log(t)
solution = simulated_annealing(z -> rosenbrock(z[1], z[2]),
                               [0, 0],
                               neighbors,
                               log_temperature,
                               10000,
                               true,
                               false)

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [June 16, 2020, 11:28am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/8 "2020-06-16T11:28:30Z")

</div>

Can you just put all the code into one nice big textfile  
so that we can run your code to see what error message you get?  
This will make it easy for us to debug your program.

We are tired of guessing.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 16, 2020, 11:29am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/9 "2020-06-16T11:29:37Z")

</div>

`ERROR: UndefVarError: rand_uniform not defined`

You have something for that for me to copy/paste?

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [June 16, 2020, 11:30am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/10 "2020-06-16T11:30:25Z")

</div>

```julia
function simulated_annealing(cost,
                             s0,
                             neighbor,
                             temperature,
                             iterations,
                             keep_best,
                             trace)
 
  # Set our current state to the specified intial state.
  s = s0
 
  # Set the best state we've seen to the intial state.
  best_s = s0
 
  # We always perform a fixed number of iterations.
  for i = 1:iterations
    t = temperature(i)
    s_n = neighbor(s)
    if trace println("$i: s = $s") end
    if trace println("$i: s_n = $s_n") end
    y = cost(s)
    y_n = cost(s_n)
    if trace println("$i: y = $y") end
    if trace println("$i: y_n = $y_n") end
    if y_n <= y
      s = s_n
      if trace println("Accepted") end
    else
      p = exp(- ((y_n - y) / t))
      if trace println("$i: p = $p") end
      if rand() <= p
        s = s_n
        if trace println("Accepted") end
      else
        s = s
        if trace println("Rejected") end
      end
    end
    if trace println() end
    if cost(s) < cost(best_s)
      best_s = s
    end
  end
 
  if keep_best
    best_s
  else
    s
  end
end

```

```julia
# Find a solution of the Rosenbrock function using SA.
load("simulated_annealing.jl")
load("../rng.jl")
 
function rosenbrock(x, y)
  (1 - x)^2 + 100(y - x^2)^2
end
 
function neighbors(z)
  [rand_uniform(z[1] - 1, z[1] + 1), rand_uniform(z[2] - 1, z[2] + 1)]
end
 
srand(1)
log_temperature(t) = 1 / log(t)
solution = simulated_annealing(z -> rosenbrock(z[1], z[2]),
                               [0, 0],
                               neighbors,
                               log_temperature,
                               10000,
                               true,
                               false)

```

---

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [June 16, 2020, 11:31am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/11 "2020-06-16T11:31:29Z")

</div>

Yes, sorry i thought that you just need the code and you will test. yes i got this error

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 16, 2020, 11:33am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/12 "2020-06-16T11:33:21Z")

</div>

Exactly, I need the code for `rand_uniform`. It’s not defined and not part of `Random`.

If your question is “why this error?” than: because its not defined. 🙂

---

<div class="post-metadata">

**Author:** ![halfdan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/halfdan/32/24201_2.png) [@halfdan](https://discourse.julialang.org/u/halfdan)\
**Post date:** [April 20, 2021, 9:25am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/13 "2021-04-20T09:25:27Z")

</div>

The error is in your definition of Rosenbrock. `100(y - x^2)^2` this is interpreted as calling a function called 100. You can write it as `100*(y - x^2)^2`

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 20, 2021, 10:01am UTC](https://discourse.julialang.org/t/simulated-annealing-algorithm/41503/14 "2021-04-20T10:01:28Z")

</div>

Actually, no. In Julia, this is interpreted as multiplication, since `100` cannot be a function name.
