‘UndefVarError: xx not defined’

I guess you tried to translate the code from R or Matlab implementation. Here is how you can do it in Julia with minimal changes to your idea:

function ackley(x)
    d = length(x)
    c = 2*pi
    b = 0.2
    a = 20
    sum1 = 0
    sum2 = 0
    for i in 1:d
        xi = x[i]
        sum1 += xi^2
        sum2 += cos(c*xi)
    end
    sum1 += x[1]^2
    sum2 += cos(c*x[2])
    term1 = -a * exp(-b*sqrt(sum1/d))
    term2 = -exp(sum2/d)
    y = term1 + term2 + a + exp(1)
end
1 Like