-
Notifications
You must be signed in to change notification settings - Fork 0
/
population.r
59 lines (49 loc) · 2.02 KB
/
population.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
constructPopulation <- function(pop_max, mutation_rate, target){
population <- list()
population$population <- list()
population$generations <- 0
population$finished <- F
population$target <- target
population$mutation_rate <- mutation_rate
population$perfect_score <- 1
population$best <- ""
for(i in 1:pop_max){
population$population[[as.character(i)]] = constructDNA(DNA, target = target)
}
population$matingPool = c()
population <- calcFitness(population)
return(population)
}
calcFitness <- function(population){
for(i in 1:length(population$population)){
population$population[[as.character(i)]] <- calculateFitness(DNA = population$population[[as.character(i)]], target = population$target)
population$population[[as.character(i)]] <- getPhrase(population$population[[as.character(i)]])
}
return(population)
}
naturalSelection <- function(population){
next_population <- population
next_population$generations <- next_population$generations+1L
fitness_vector <- unlist(lapply(population$population, function(x) x[["fitness"]]))
avg_fitness <- mean(fitness_vector)
fitness_vector2 <- floor(fitness_vector*100)
gene_pool <- c()
for(i in 1:length(fitness_vector2)){
dna_n <- names(fitness_vector2)[i]
gene_pool <- c(gene_pool, rep(dna_n, fitness_vector2[i]))
}
next_population$population <- list()
for( i in 1:length(population$population)){
parentDNA1 <- sample(gene_pool, 1)
parentDNA1 <- population$population[[parentDNA1]]
parentDNA2 <- sample(gene_pool, 1)
parentDNA2 <- population$population[[parentDNA2]]
childDNA <- crossOver(parentDNA1, parentDNA2)
childDNA <- mutateGene(DNA = childDNA, mutationRate = population$mutation_rate)
next_population$population[[as.character(i)]] <- childDNA
#fitness_table[,`:=`(score1 = NULL, score2 = NULL)]
}
next_population$avg_fitness <- avg_fitness
next_population$best <- max(fitness_vector)
return(next_population)
}