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.

125 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. print(delta_func)
  49. tmp_df <- ea.trace(range, delta, population_size, fit, iterations)$df_tr
  50. tmp_df["delta_func"] <- delta_func
  51. if(delta_func == "delta.gaus") {
  52. tmp_df["delta"] <- NA
  53. } else {
  54. tmp_df["delta"] <- delta()
  55. }
  56. tmp_df["fit_func"] <- fit_name
  57. df <- rbind(df, tmp_df)
  58. }
  59. }
  60. return(df)
  61. }
  62. ea.plot <- function(range, delta, population_size, fit_func, iterations) {
  63. res <- ea.trace(range, delta, population_size, fit_func, iterations)
  64. df_vals <- melt(res$df_tr[c("i", "min_val", "median_val", "max_val")], id.vars="i")
  65. p <- ggplot(data=df_vals, aes(x=i)) +
  66. geom_line(aes(y=value, linetype=variable))
  67. return(p)
  68. }
  69. ea.run <- function(range, delta, population_size, fit_func, iterations) {
  70. population <- ea.init_population(range, population_size, runif)
  71. for(i in 1:iterations) {
  72. population <- ea.iterate(delta, population, fit_func)
  73. }
  74. return(population)
  75. }
  76. ea.iterate <- function(delta, population, fit_func) {
  77. children <- c()
  78. for(individual in population) {
  79. children <- append(children, ea.mutate(individual, delta))
  80. }
  81. population <- append(population, children)
  82. return(ea.select(population, fit_func))
  83. }
  84. ea.mutate <- function(individual, delta) {
  85. sign <- sample(c(-1,1), 1)
  86. return(individual + sign * delta())
  87. }
  88. ea.select <- function(population, fit_func) {
  89. sorted_popul <- population[order(sapply(population, fit_func), decreasing=TRUE)]
  90. return(sorted_popul[1 : (length(sorted_popul) %/% 2)])
  91. }