You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.3 KiB

3 years ago
  1. library(ggplot2)
  2. func.g <- function(x) x
  3. func.k <- function(x) sin(x)
  4. func.h <- function(x) x * sin(x)
  5. func.l <- function(x) 2 + cos(x) + sin(2*x)
  6. delta.const <- function(x) {
  7. return(function() x)
  8. }
  9. delta.gaus <- function() {
  10. return(rnorm(1))
  11. }
  12. ea.init_population <- function(range, size, rand_gen) {
  13. return(rand_gen(size) * (range[2] - range[1]) + range[1])
  14. }
  15. ea.trace <- function(range, delta, population_size, fit_func, iterations) {
  16. population <- ea.init_population(range, population_size, runif)
  17. df <- data.frame(i=integer()
  18. ,max=numeric()
  19. ,median=numeric()
  20. ,min=numeric())
  21. for(i in 1:iterations) {
  22. population <- ea.iterate(delta, population, fit_func)
  23. df[nrow(df) + 1,] <- c(i
  24. ,population[1]
  25. ,population[length(population) %/% 2]
  26. ,population[length(population)]
  27. )
  28. }
  29. df["max_val"] <- fit_func(df$max)
  30. df["median_val"] <- fit_func(df$median)
  31. df["min_val"] <- fit_func(df$min)
  32. res <- list(population, df)
  33. names(res) <- c("population", "df_tr")
  34. return(res)
  35. }
  36. ea.traces <- function(range, deltas, population_size, fit_funcs, iterations) {
  37. df <- data.frame(i=integer()
  38. ,max=numeric()
  39. ,median=numeric()
  40. ,min=numeric()
  41. ,delta_func=character()
  42. ,delta=numeric()
  43. ,fit_func=character())
  44. for(delta_func in names(deltas)) {
  45. delta <- deltas[delta_func]
  46. for(fit_name in names(fit_funcs)) {
  47. fit <- fit_funcs[fit_name]
  48. tmp_df <- ea.trace(range, delta, population_size, fit, iterations)$df_tr
  49. tmp_df["delta_func"] <- delta_func
  50. if(delta_func == "delta.gaus") {
  51. tmp_df["delta"] <- NA
  52. } else {
  53. tmp_df["delta"] <- delta()
  54. }
  55. tmp_df["fit_func"] <- fit_name
  56. df <- rbind(df, tmp_df)
  57. }
  58. }
  59. return(df)
  60. }
  61. ea.plot <- function(range, delta, population_size, fit_func, iterations) {
  62. res <- ea.trace(range, delta, population_size, fit_func, iterations)
  63. df_vals <- melt(res$df_tr[c("i", "min_val", "median_val", "max_val")], id.vars="i")
  64. p <- ggplot(data=df_vals, aes(x=i)) +
  65. geom_line(aes(y=value, linetype=variable))
  66. return(p)
  67. }
  68. ea.run <- function(range, delta, population_size, fit_func, iterations) {
  69. population <- ea.init_population(range, population_size, runif)
  70. for(i in 1:iterations) {
  71. population <- ea.iterate(delta, population, fit_func)
  72. }
  73. return(population)
  74. }
  75. ea.iterate <- function(delta, population, fit_func) {
  76. children <- c()
  77. for(individual in population) {
  78. children <- append(children, ea.mutate(individual, delta))
  79. }
  80. population <- append(population, children)
  81. return(ea.select(population, fit_func))
  82. }
  83. ea.mutate <- function(individual, delta) {
  84. sign <- sample(c(-1,1), 1)
  85. return(individual + sign * delta())
  86. }
  87. ea.select <- function(population, fit_func) {
  88. sorted_popul <- population[order(sapply(population, fit_func), decreasing=TRUE)]
  89. return(sorted_popul[1 : (length(sorted_popul) %/% 2)])
  90. }