Skip to content

Commit 33c3d4f

Browse files
miguelbironwildart
andauthored
Subset crossover (#89)
* added binary subset crossover (BSX) Co-authored-by: Art <[email protected]>
1 parent 38909d9 commit 33c3d4f

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ a
6060
- UX (uniform)
6161
- BINX (binary)
6262
- EXPX (exponential)
63+
- BSX (binary subset)
6364
- real valued
6465
- DC (discrete)
6566
- AX (average)

docs/src/crossover.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SHFX
3232
UX
3333
BINX
3434
EXPX
35+
BSX
3536
```
3637

3738
Real-valued crossovers:
@@ -78,3 +79,5 @@ Evolutionary.crosstree
7879
[^5]: K. Deep, K. P. Singh, M. L. Kansal, and C. Mohan, "A real coded genetic algorithm for solving integer and mixed integer optimization problems.", Appl. Math. Comput. 212, 505-518, 2009
7980

8081
[^6]: K. Deb, R. B. Agrawal, "Simulated Binary Crossover for Continuous Search Space", Complex Syst., 9., 1995
82+
83+
[^7]: M. A. Wolters, “A Genetic Algorithm for Selection of Fixed-Size Subsets with Application to Design Problems”, J. Stat. Soft., vol. 68, no. 1, pp. 1–18, Nov. 2015.

src/Evolutionary.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Evolutionary
2424
SPX, TPX, UX, SHFX,
2525
DC, AX, WAX, IC, LC, HX, LX, MILX, SBX,
2626
PMX, OX1, CX, OX2, POS,
27+
BSX,
2728
# GA selections
2829
ranklinear, uniformranking, roulette, rouletteinv, sus, susinv,
2930
tournament, truncation,

src/recombinations.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,26 @@ function EXPX(Cr::Real = 0.5)
180180
return expxvr
181181
end
182182

183+
"""
184+
BSX(k::Int)
185+
186+
Binary Subset Crossover[^7]. Produces two offsprings by first pooling the unique
187+
items of the two parents, and then creating each offspring by sampling without
188+
replacement at most `k` elements from the pool of items.
189+
"""
190+
function BSX(k::Int)
191+
function BSX(v1::T, v2::T; rng::AbstractRNG=Random.GLOBAL_RNG) where {T <: AbstractVector{Bool}}
192+
l = length(v1) # get number of available elements
193+
pooled = findall(v1 .| v2) # pool parents selections
194+
K = min(k,length(pooled)) # cannot sample more than the items in pool
195+
c1 = falses(l) # init child 1
196+
c2 = falses(l) # init child 2
197+
c1[shuffle(rng, pooled)[1:K]] .= true # fill child 1 with sample from pool w/o replacement
198+
c2[shuffle(rng, pooled)[1:K]] .= true # fill child 2 with sample from pool w/o replacement
199+
return c1, c2
200+
end
201+
return BSX
202+
end
183203

184204
# Real valued crossovers
185205
# ----------------------
@@ -544,6 +564,8 @@ function POS(v1::T, v2::T; rng::AbstractRNG=Random.GLOBAL_RNG) where {T <: Abstr
544564
return c1,c2
545565
end
546566

567+
568+
547569
# ===================
548570
# Genetic Programming
549571
# ===================

test/recombinations.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@
5252
@test OX2([1:8;], [2,4,6,8,7,5,3,1], rng=rng) == ([1,2,3,4,6,5,7,8], [2,4,3,8,7,5,6,1])
5353
Random.seed!(rng, 18)
5454
@test POS([1:8;], [2,4,6,8,7,5,3,1], rng=rng) == ([1,4,6,2,3,5,7,8], [4,2,3,8,7,6,5,1])
55-
55+
56+
xo = BSX(3)
57+
@testset "BSX" for i in 1:100
58+
off1, off2 = xo(Bool[1,0,1,0,1,0], Bool[1,1,1,1,0,0])
59+
@test off1[end] == 0 && off2[end] == 0
60+
@test sum(off1) == 3 && sum(off2) == 3
61+
end
5662
end
5763

5864
@testset "DE" begin

0 commit comments

Comments
 (0)