Skip to content

Commit

Permalink
move more tests to new api
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed Apr 28, 2020
1 parent 3b22b51 commit 239b0bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/es.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ function initial_state(method::ES, options, objfun, population)
T = typeof(value(objfun))
individual = first(population)
N = length(individual)
fitness = zeros(T, population_size(method))

# Evaluate population fitness
# fitness = map(i -> value(objfun, i), population)
# minfit, fitidx = findmin(fitness)
# fitness = zeros(T, population_size(method))
fitness = map(i -> value(objfun, i), population)

# setup initial state
return ESState(N, fitness, fill(method.initStrategy, method.μ), copy(individual))
Expand Down Expand Up @@ -84,7 +83,6 @@ function update_state!(objfun, state, population::AbstractVector{IT}, method::ES
# Evaluate fitness
fitoff[i] = value(objfun, offspring[i])
end
# println("FIT: $fitoff")

# Select new parent population
if selection == :plus
Expand Down
25 changes: 11 additions & 14 deletions test/n-queens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
@test nqueens([2,4,1,3]) == 0
@test nqueens([3,1,2]) == 1

function Evolutionary.trace!(record::Dict{String,Any}, objfun, state, population, method::GA, options)
idx = sortperm(state.fitpop)
record["fitpop"] = state.fitpop[idx[1:5]]
end

# Testing: GA solution with various mutations
for muts in [inversion, insertion, swap2, scramble, shifting]
result = Evolutionary.optimize(nqueens,
Expand All @@ -40,17 +35,19 @@
# show(result)
println("GA:PMX:$(string(muts))(N=$(N), P=$(P)) => F: $(minimum(result)), C: $(Evolutionary.iterations(result))")
@test nqueens(Evolutionary.minimizer(result)) == 0
@test Evolutionary.iterations(result) < 800 # Test early stopping
@test Evolutionary.iterations(result) < 100 # Test early stopping
end

# Testing: ES
# for muts in [inversion, insertion, swap2, scramble, shifting]
# result, fitness, cnt = Evolutionary.optimize(nqueens, ES(N=N;
# initPopulation = generatePositions,
# mutation = mutationwrapper(muts),
# μ = 15, ρ = 1, λ = P))
# println("(15+$(P))-ES:$(string(muts)) => F: $(fitness), C: $(cnt), OBJ: $(result)")
# @test nqueens(result) == 0
# end
for muts in [inversion, insertion, swap2, scramble, shifting]
for sel in [:plus, :comma]
result = Evolutionary.optimize(nqueens, generatePositions,
ES(mutation = mutationwrapper(muts), μ = 15, ρ = 1, λ = P, selection=sel),
Evolutionary.Options(show_trace=false))
println("(15$(sel == :plus ? "+" : ",")$(P))-ES:$(string(muts)) => F: $(minimum(result)), C: $(Evolutionary.iterations(result))")
# show(result)
@test nqueens(Evolutionary.minimizer(result)) == 0
end
end

end
4 changes: 4 additions & 0 deletions test/sphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
@test sum(x->x.^2, Evolutionary.minimizer(result)) 0.0 atol=1e-3
@test length(Evolutionary.minimizer(result)) == N

# disable custom functions
Evolutionary.terminate(state::Evolutionary.ESState) = false
Evolutionary.trace!(record::Dict{String,Any}, objfun, state, population, method::ES, options) = ()

# Testing: GA
result = Evolutionary.optimize(
sphere,
Expand Down

0 comments on commit 239b0bc

Please sign in to comment.