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 ?
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 :
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)
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.
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
# 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)
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