You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I met this issue while trying to use GA to solve the travelling salesman problem. The code to reproduce this issue is fairly simple, so I'll just paste it here:
using Evolutionary
using Random
struct TSPTargetFunction{T} <:Function
weights::Matrix{T}functionTSPTargetFunction(W::AbstractMatrix)
new{eltype(W)}(W)
endendfunction (f::TSPTargetFunction)(x)
(
f.weights[last(x), first(x)] +sum(f.weights[i, j] for (i, j) inzip(x, Iterators.drop(x, 1)))
)
endfunctionsolve_tsp(
W::AbstractMatrix;
population_size =100,
options_kwargs =Dict{Symbol,Any}(),
)
f =TSPTargetFunction(W)
options =
Evolutionary.Options(iterations =2000, successive_f_tol =30, options_kwargs...)
optimizer =GA(;
populationSize = population_size,
selection = rouletteinv,
crossover = OX2, # not really, but not relevant
mutation = x -> x[x], # not really, but not relevant
mutationRate =0.5,
crossoverRate =0.5,
epsilon = population_size ÷5,
)
n =size(W, 1)
initial_population = [randperm(n) for _ =1:population_size]
Evolutionary.optimize(f, initial_population, optimizer, options)
endsolve_tsp(rand(4, 4)) # Error
The text was updated successfully, but these errors were encountered:
Yep, it's a bug. The initialization of the NonDifferentiable should be done better. Currently, the result of the objective function must have the same type as an element of the individual of the population. Otherwise, multiple functions has to be overridden to make it work. That is wrong.
Thanks, this workds for me (and shouldn't break the API)
functioninitialize_objective(f, x::AbstractArray{T}, S =typeof(f(x))) where T
NonDifferentiable{S,typeof(x)}(f, zero(S), zeros(T, length(x)), [0,])
end
I met this issue while trying to use GA to solve the travelling salesman problem. The code to reproduce this issue is fairly simple, so I'll just paste it here:
The text was updated successfully, but these errors were encountered: