After further exploration, it seems like the problem has to do with Metaheuristics.jl and not Optimization.jl. It looks like you have to redefine ECA() between solves. I’m not sure if this is intended behavior or if it’s something that can be easily fixed by updating the Optimization.jl Metaheuristics wrapper, but I was able to move away from using Optimization.jl and just use Metaheuristics.jl for my problem with the ECA() redefinition fix.
using LinearAlgebra, Metaheuristics
xdata = collect(LinRange(0, 10, 100));
ydata = rand(100);
function loss(u, p)
x, y, n = p
z = zeros(size(x,1))
for i = 1:n
z1 = u[i].*x
z = z + z1
end
return sum((z .- y).^2)
end
options = Metaheuristics.Options(time_limit = 1.0, f_calls_limit=10000000);
algorithm = ECA(options=options);
bounds = boxconstraints(lb = zeros(10), ub = 10*ones(10));
p = (xdata, ydata, 3);
result = optimize(x->loss(x,p), bounds, algorithm)
#first solve works fine
@show loss(result.best_sol.x, p) == result.best_sol.f
#shows true
#changing just p and reoptimizing fails
p = (xdata, ydata, 4);
result = optimize(x->loss(x,p), bounds, algorithm)
@show loss(result.best_sol.x, p) == result.best_sol.f
#shows false
#resetting the algorithm seems to work?
algorithm = ECA(options=options);
p = (xdata, ydata, 5);
result = optimize(x->loss(x,p), bounds, algorithm)
@show loss(result.best_sol.x, p) == result.best_sol.f
#shows true