-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathknapsack.jl
53 lines (44 loc) · 1.72 KB
/
knapsack.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@testset "Knapsack" begin
Random.seed!(42)
mass = [1, 5, 3, 7, 2, 10, 5]
utility = [1, 3, 5, 2, 5, 8, 3]
fitnessFun = n -> (sum(mass .* n) <= 20) ? sum(utility .* n) : 0
initpop = collect(rand(Bool,length(mass)))
result = Evolutionary.optimize(
x -> -fitnessFun(x),
initpop,
GA(
selection = roulette,
mutation = inversion,
crossover = singlepoint,
mutationRate = 0.9,
crossoverRate = 0.2,
ɛ = 0.1, # Elitism
populationSize = 100,
));
println("GA:RLT:INV:SP (-objfun) => F: $(minimum(result)), C: $(Evolutionary.iterations(result))")
@test abs(Evolutionary.minimum(result)) == 21.
@test sum(mass .* Evolutionary.minimizer(result)) <= 20
# with a constraint
fitnessFun = n -> sum(utility .* n)
cf(n) = [ sum(mass .* n) ] # constraint function
lc = [0] # lower bound for constraint function
uc = [20] # upper bound for constraint function
con = WorstFitnessConstraints(Int[], Int[], lc, uc, cf)
initpop = BitVector(rand(Bool,length(mass)))
result = Evolutionary.optimize(
x -> -fitnessFun(x), con,
initpop,
GA(
selection = roulette,
mutation = flip,
crossover = singlepoint,
mutationRate = 0.9,
crossoverRate = 0.1,
ɛ = 0.1, # Elitism
populationSize = 50,
));
println("GA:RLT:FL:SP (-objfun) => F: $(minimum(result)), C: $(Evolutionary.iterations(result))")
@test abs(Evolutionary.minimum(result)) == 21.
@test sum(mass .* Evolutionary.minimizer(result)) <= 20
end