jmejia8/Metaheuristics.jl

Single objective constrained parallel optimization cannot work?

licheng0794 opened this issue · 2 comments

I want to experiment single objective parallel optimization with constrains. I modified the example from https://docs.juliahub.com/Metaheuristics/aJ70z/3.2.0/examples/#Constrained-Optimization.

The code is below.

using Metaheuristics
Metaoptimize = Metaheuristics.optimize
MetaOptions = Metaheuristics.Options

function f(x)
x,y = x[1], x[2]
fx = (1-x)^2+100(y-x^2)^2
gx = [x^2 + y^2 - 2] # inequality constraints
hx = [0.0] # equality constraints
# order is important
return fx, gx, hx
end

function fff(X)
N = size(X,1)
fx, gx, hx = zeros(N), zeros(N,1), zeros(N,1)
Threads.@threads for i in 1:size(X,1)
fx[i], gx[i], hx[i] = f(X[i,:])
end
fx, gx, hx
end

bounds = [-2.0 -2; 2 2]

options = MetaOptions(f_calls_limit = 20000, parallel_evaluation=true, debug=true);

Metaoptimize(fff, bounds, ECA(;options, N=30, K=3))

I got the error 'TaskFailedException'.

Any advice? Thank you.

It should be:

function fff(X)
    N = size(X,1)
    fx, gx, hx = zeros(N), zeros(N,1), zeros(N,1)
    Threads.@threads for i in 1:size(X,1)
        fx[i], gx[i,:], hx[i,:] = f(X[i,:])
    end
    fx, gx, hx
end

Note that gx isa Matrix not a Vector (same for hx).

It is recommended to read the documentation on parallelization within Metaheuristics:

https://jmejia8.github.io/Metaheuristics.jl/stable/tutorials/parallelization/