Skip to content

Algorithm must be redefined between solves #116

@jbiffl

Description

@jbiffl

When changing parameters between optimizations with a loss function of the form x->lossfn(x, p), ECA() must be redefined between optimizations.

This discourse post includes MWEs for use both with and without Optimization.jl

https://discourse.julialang.org/t/metaheuristics-jl-eca-through-optimization-jl-does-not-work-with-ensembleproblems/129286

For convenience, I have attached a final MWE which does not rely on Optimization.jl and demonstrates the issue.

using LinearAlgebra, Metaheuristics

xdata = collect(LinRange(0, 10, 100));
ydata = rand(100);
ydata2 = 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 (ydata->ydata2)
p = (xdata, ydata2, 3);
result = optimize(x->loss(x,p), bounds, algorithm)
@show loss(result.best_sol.x, p) == result.best_sol.f
#shows false

#changing just p and reoptimizing fails regardless of whether it changes #parameters optimized (3->4)
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

#redefining the algorithm seems to work
algorithm = ECA(options=options);
p = (xdata, ydata2, 3);
result = optimize(x->loss(x,p), bounds, algorithm)
@show loss(result.best_sol.x, p) == result.best_sol.f
#shows true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions