在两个向量之间交换元素(交叉)
Exchanging elements (crossover) between two vectors
假设我有:
chromosome_1 <- c('0010000001010000')
chromosome_2 <- c('0100000001001010')
如何实施 步骤 3-5?
- 评价
- NC1 = 没有。
chromosome_1
中的 1 个
- NC2 = 没有。
chromosome_2
中的 1 个
- M =
min(NC1, NC2)
- 从
range(1, M)
生成一个随机整数 NC
基因中随机selectNC
个基因位置
与来自 chromosome_1
的等位基因“1”形成一组 s1
的索引
这样 selected 位置。
随机selectNC
个基因位置
与来自 chromosome_2
的等位基因“1”形成一组 s2
的索引
这样 selected 位置。
s = union(s1, s2)
假设 s = 2, 3, 10, 15
对于 s
中的每个索引 i
交换染色体 chromosome_1
和 chromosome_2
的等位基因
基因位置 i
.
下面说明了结果:
非常感谢任何帮助!
你可以试试 GA package:
手册(第 5 页)中有一个示例。
ga(type = c("binary", "real-valued", "permutation"),
fitness, ...,
min, max, nBits,
population = gaControl(type)$population,
selection = gaControl(type)$selection,
crossover = gaControl(type)$crossover,
mutation = gaControl(type)$mutation,
popSize = 50,
pcrossover = 0.8,
pmutation = 0.1,
elitism = base::max(1, round(popSize*0.05)),
updatePop = FALSE,
postFitness = NULL,
maxiter = 100,
run = maxiter,
maxFitness = Inf,
names = NULL,
suggestions = NULL,
optim = FALSE,
optimArgs = list(method = "L-BFGS-B",
poptim = 0.05,
pressel = 0.5,
control = list(fnscale = -1, maxit = 100)),
keepBest = FALSE,
parallel = FALSE,
monitor = if(interactive())
{ if(is.RStudio()) gaMonitor else gaMonitor2 }
else FALSE,
seed = NULL)
例如,人口、选择、交叉、变异和监测算子分配新的功能。在我的研究中,我使用了自己的变异和监控功能。例如;
myga <- ga(type = "binary",
fitness, ...,
min, max, nBits,
mutation = myMutationFunction
popSize = 50,
pcrossover = 0.8,
pmutation = 0.1,
maxiter = 100,
run = maxiter,
monitor = myMonitorFunction
myMutationFunction <- function (x) {
#...
}
myMonitorFunction <- function (x) {
#...
}
所以,你自己定义一个函数,把函数名给ga函数就行了。为了便于参考,您可以查看默认功能。您可以在默认函数中看到必要的参数和 return 值。
可能不是最简单的解决方案,但它有效
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
假设我有:
chromosome_1 <- c('0010000001010000')
chromosome_2 <- c('0100000001001010')
如何实施 步骤 3-5?
- 评价
- NC1 = 没有。
chromosome_1
中的 1 个
- NC2 = 没有。
chromosome_2
中的 1 个
- M =
min(NC1, NC2)
- NC1 = 没有。
- 从
range(1, M)
生成一个随机整数 基因中随机select
NC
个基因位置 与来自chromosome_1
的等位基因“1”形成一组s1
的索引 这样 selected 位置。随机select
NC
个基因位置 与来自chromosome_2
的等位基因“1”形成一组s2
的索引 这样 selected 位置。s = union(s1, s2)
假设s = 2, 3, 10, 15
对于
中的每个索引s
i
交换染色体
chromosome_1
和chromosome_2
的等位基因 基因位置i
.
NC
下面说明了结果:
非常感谢任何帮助!
你可以试试 GA package:
手册(第 5 页)中有一个示例。
ga(type = c("binary", "real-valued", "permutation"),
fitness, ...,
min, max, nBits,
population = gaControl(type)$population,
selection = gaControl(type)$selection,
crossover = gaControl(type)$crossover,
mutation = gaControl(type)$mutation,
popSize = 50,
pcrossover = 0.8,
pmutation = 0.1,
elitism = base::max(1, round(popSize*0.05)),
updatePop = FALSE,
postFitness = NULL,
maxiter = 100,
run = maxiter,
maxFitness = Inf,
names = NULL,
suggestions = NULL,
optim = FALSE,
optimArgs = list(method = "L-BFGS-B",
poptim = 0.05,
pressel = 0.5,
control = list(fnscale = -1, maxit = 100)),
keepBest = FALSE,
parallel = FALSE,
monitor = if(interactive())
{ if(is.RStudio()) gaMonitor else gaMonitor2 }
else FALSE,
seed = NULL)
例如,人口、选择、交叉、变异和监测算子分配新的功能。在我的研究中,我使用了自己的变异和监控功能。例如;
myga <- ga(type = "binary",
fitness, ...,
min, max, nBits,
mutation = myMutationFunction
popSize = 50,
pcrossover = 0.8,
pmutation = 0.1,
maxiter = 100,
run = maxiter,
monitor = myMonitorFunction
myMutationFunction <- function (x) {
#...
}
myMonitorFunction <- function (x) {
#...
}
所以,你自己定义一个函数,把函数名给ga函数就行了。为了便于参考,您可以查看默认功能。您可以在默认函数中看到必要的参数和 return 值。
可能不是最简单的解决方案,但它有效
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