R 39 788 537 400 052
Hier ist mein Versuch, einen genetischen Algorithmus zu erstellen, aber nur mit ungeschlechtlicher Fortpflanzung. Ich hoffe, ich habe die Herausforderung richtig verstanden. Bearbeiten: Beschleunigen Sie es ein wenig, probieren Sie einen anderen Zufallsstartwert aus und beschränken Sie sich auf 100 Generationen.
options(scipen=999)
toeplitz <- function(x){
# make toeplitz matrix with first row
# x[1:a] and first col x[(a+1):n]
# where n is the length of x and a= n/2
# Requires x to have even length
#
# [1,1] entry is x[a+1]
N <- length(x)/2
out <- matrix(0, N, N)
out[1,] <- x[1:N]
out[,1] <- x[(N+1):length(x)]
for (i in 2:N){
for (j in 2:N){
out[i,j] <- out[i-1, j-1]
}
}
out
}
set.seed(1002)
generations <- 100
popsize <- 25
cols <- 60
population <- matrix(sample(0:1, cols*popsize, replace=T), nc=cols)
numfresh <- 5 # number of totally random choices added to population
for (i in 1:generations){
fitness <- apply(population, 1, function(x) det(toeplitz(x)) )
mother <- which(fitness==max(fitness))[1]
population <- matrix(rep(population[mother,], popsize), nc=cols, byrow=T)
for (i in 2:(popsize-numfresh)){
x <- sample(cols, 1)
population[i,x] <- 1-population[i,x]
}
for (i in (popsize-numfresh +1):popsize){
population[i,] <- sample(0:1, cols, replace=T)
}
print(population[1,])
print(fitness[mother])
print(det(toeplitz(population[1,]))) # to check correct
}
Ausgabe:
print(population[1, 1:(cols/2)]) # first row
print(population[1, (cols/2+1):(cols)]) # first column (overwrites 1st row)
to <- toeplitz(population[1,])
for (i in 1:(cols/2)) cat(to[i,], "\n")
1 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 0
0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1
1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0
0 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 1 1 1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