June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
34
Task/Evolutionary-algorithm/R/evolutionary-algorithm-2.r
Normal file
34
Task/Evolutionary-algorithm/R/evolutionary-algorithm-2.r
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# Setup
|
||||
set.seed(42)
|
||||
target= unlist(strsplit("METHINKS IT IS LIKE A WEASEL", ""))
|
||||
chars= c(LETTERS, " ")
|
||||
C= 100
|
||||
|
||||
# Fitness function; high value means higher fitness
|
||||
fitness= function(x){
|
||||
sum(x == target)
|
||||
}
|
||||
|
||||
# Mutate function
|
||||
mutate= function(x, rate= 0.01){
|
||||
idx= which(runif(length(target)) <= rate)
|
||||
x[idx]= replicate(n= length(idx), expr= sample(x= chars, size= 1, replace= T))
|
||||
x
|
||||
}
|
||||
|
||||
# Evolve function
|
||||
evolve= function(x){
|
||||
parents= rep(list(x), C+1) # Repliction
|
||||
parents[1:C]= lapply(parents[1:C], function(x) mutate(x)) # Mutation
|
||||
idx= which.max(lapply(parents, function(x) fitness(x))) # Selection
|
||||
parents[[idx]]
|
||||
}
|
||||
|
||||
# Initialize first parent
|
||||
parent= sample(x= chars, size= length(target), replace= T)
|
||||
|
||||
# Main program
|
||||
while (fitness(parent) < fitness(target)) {
|
||||
parent= evolve(parent)
|
||||
cat(paste0(parent, collapse=""), "\n")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue