Assume I have:
chromosome_1 <- c('0010000001010000')
chromosome_2 <- c('0100000001001010')
How can I implement step 3-5 ?
chromosome_1
chromosome_2
min(NC1, NC2)
NC
from range(1, M)
Randomly select NC
gene positions among the genes
with allele “1” from chromosome_1
and form a set s1
of indices of
such selected positions.
Randomly select NC
gene positions among the genes
with allele “1” from chromosome_2
and form a set s2
of indices of
such selected positions.
s = union(s1, s2)
Assume s = 2, 3, 10, 15
for each index i
in s
Exchange the alleles of chromosomes chromosome_1
and chromosome_2
at
gene position i
.
The following illustrates the outcome:
I would really really appreciate any help!
Might not be the simplest solution, but it works
set.seed(12345)
## Step 1
a <- c(0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0)
b <- c(0,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0)
m <- min(sum(a==1), sum(b==1))
## Step 2
random_int <- sample(1:m, 1)
## Step 3
random_a <- sample(which(a == 1), random_int)
random_b <- sample(which(b == 1), random_int)
#all <- sort(union(random_a, random_b))
## Step 4
## for demo purpose (assume it as the random output)
all <- c(2,3,10,15)
temp_a <- a[all]
temp_b <- b[all]
## Step 5
##crossover
b[all] <- temp_a
a[all] <- temp_b
## Output
> a
[1] 0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0
> b
[1] 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0