Skip to content

Commit

Permalink
DE: Parallelization (#120)
Browse files Browse the repository at this point in the history
* added missing `rng` parameter

* perform parallel evaluation

* reduce memory allocation by caching offspring fitness
  • Loading branch information
wildart committed May 31, 2023
1 parent 8962963 commit 0f2da1a
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/de.jl
Expand Up @@ -28,6 +28,7 @@ show(io::IO,m::DE) = print(io, summary(m))
mutable struct DEState{T,IT} <: AbstractOptimizerState
N::Int
fitness::Vector{T}
offitness::Vector{T}
fittest::IT
end
value(s::DEState) = minimum(s.fitness)
Expand All @@ -39,9 +40,10 @@ function initial_state(method::DE, options, objfun, population)
individual = first(population)
N = length(individual)
fitness = fill(maxintfloat(T), method.populationSize)
offitness = fill(maxintfloat(T), method.populationSize)

# setup initial state
return DEState(N, fitness, copy(individual))
return DEState(N, fitness, offitness, copy(individual))
end

function update_state!(objfun, constraints, state, population::AbstractVector{IT}, method::DE, options, itr) where {IT}
Expand All @@ -55,32 +57,34 @@ function update_state!(objfun, constraints, state, population::AbstractVector{IT
offspring = Array{IT}(undef, Np)

# select base vectors
bases = method.selection(state.fitness, Np)
bases = method.selection(state.fitness, Np, rng=rng)

# select target vectors
for (i,b) in enumerate(bases)
# mutation
base = population[b]
offspring[i] = copy(base)
# println("$i => base:", offspring[i])

# mutation
targets = randexcl(rng, 1:Np, [i], 2*n)
offspring[i] = differentiation(offspring[i], @view population[targets]; F=F)
# println("$i => mutated:", offspring[i], ", targets:", targets)

# recombination
offspring[i], _ = method.recombination(offspring[i], base, rng=rng)
# println("$i => recombined:", offspring[i])

# apply constraints
apply!(constraints, offspring[i])
end

# Evaluate new offspring
evaluate!(objfun, state.offitness, offspring, constraints)

# Create new generation
fitidx = 0
minfit = minimum(state.fitness)
for i in 1:Np
o = apply!(constraints, offspring[i])
v = value(objfun, o) + penalty(constraints, o)
v = state.offitness[i]
if (v <= state.fitness[i])
population[i] = o
population[i] = offspring[i]
state.fitness[i] = v
if v < minfit
minfit = v
Expand Down

0 comments on commit 0f2da1a

Please sign in to comment.