-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.r
41 lines (39 loc) · 1.33 KB
/
main.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
library(data.table)
library(tictoc)
DNA <- list(
value = c(""),
fitness = 0,
phrase = ""
)
source("C:/Users/12027/Desktop/studies/geneticAlgorithm/R/dna.r")
source("C:/Users/12027/Desktop/studies/geneticAlgorithm/R/population.r")
target_phrase <- "to be or not to be."
target <- unlist(strsplit(target_phrase, ""))
max_population <- 200
mutation_rate = 0.004
population <- constructPopulation(max_population, mutation_rate = mutation_rate, target = target)
# samp<-population
# samp$population[["1"]]$phrase <- "hellohello"
# samp$population[["2"]]$phrase <- "worldworld"
# samp$population[["1"]]$value <- unlist(strsplit(samp$population[["1"]]$phrase, ""))
# samp$population[["2"]]$value <- unlist(strsplit(samp$population[["2"]]$phrase, ""))
#
#
#
# population <- calcFitness(samp)
while(population$finished == F){
tic()
population <- naturalSelection(population)
#toc()
population <- calcFitness(population)
# toc()
best_dna_id <- getBestDNA(population$population)
# toc()
best_dna <- population$population[[best_dna_id$max_fitness_dna]]
if(best_dna$phrase == target_phrase){
population$finished = T
}
toc()
cat("Population Generation", population$generations, "\n")
cat("Population Best Fitness", best_dna$phrase, " fitness score : ", population$best, " avg fitness : ", population$avg_fitness, "\n")
}