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.

113 lines
2.8 KiB

3 years ago
  1. library(ggplot2)
  2. library(gganimate)
  3. library(grDevices)
  4. rand.pupulation <- function(n) {
  5. return(matrix(runif(2*n), ncol=2))
  6. }
  7. alpha.fixed <- function(alpha) {
  8. return(function() alpha)
  9. }
  10. alpha.runif <- function() {
  11. return(function() runif(1))
  12. }
  13. recom.singlearithm <- function(alpha_gen) {
  14. return(function(parents) {
  15. alpha <- alpha_gen()
  16. gene_index <- sample(1:ncol(parents), 1)
  17. gene_p1 <- parents[1, gene_index]
  18. gene_p2 <- parents[2, gene_index]
  19. children <- parents
  20. children[1,gene_index] <- alpha * gene_p2 + (1 - alpha) * gene_p1
  21. children[2,gene_index] <- alpha * gene_p1 + (1 - alpha) * gene_p2
  22. return(children)
  23. })
  24. }
  25. recom.wholearithm <- function(alpha_gen) {
  26. return(function(parents) {
  27. alpha <- alpha_gen()
  28. children <- parents
  29. children[1,] <- alpha * children[1,] + (1 - alpha) * children[2,]
  30. children[2,] <- alpha * children[2,] + (1 - alpha) * children[1,]
  31. return(children)
  32. })
  33. }
  34. next.population <- function(population, recom) {
  35. next_population <- matrix(, nrow=0, ncol=2)
  36. parent_pairs <- matrix(sample(1:nrow(population), nrow(population)), ncol=2)
  37. for(i in 1:nrow(parent_pairs)) {
  38. parents <- parent_pairs[i,]
  39. next_population <- rbind(next_population, recom(population[parents,]))
  40. }
  41. return(next_population)
  42. }
  43. experiment <- function(population, gen, recom) {
  44. df <- data.frame( x = population[,1]
  45. ,y = population[,2]
  46. ,generation = 0)
  47. for(g in 1:gen) {
  48. population <- next.population(population, recom)
  49. df <- rbind(df, data.frame( x = population[,1]
  50. ,y = population[,2]
  51. ,generation = g))
  52. }
  53. return(df)
  54. }
  55. plot.experiment <- function(df, filename) {
  56. pdf(file=filename, onefile=TRUE)
  57. for(g in unique(df$generation)) {
  58. tmp_df <- df[df$generation == g,]
  59. p <- ggplot(data=tmp_df, aes(x=x, y=y)) +
  60. geom_point() +
  61. labs(title=sprintf("generation: %d", g)) +
  62. xlim(0, 1) +
  63. ylim(0, 1)
  64. hull <- chull(tmp_df$x, tmp_df$y)
  65. p <- p + geom_polygon(data=tmp_df[hull,], alpha=0.25)
  66. print(p)
  67. }
  68. dev.off()
  69. }
  70. all.experiments <- function() {
  71. population <- rand.pupulation(20)
  72. df <- experiment(population, 10, recom.singlearithm(alpha.fixed(0.5)))
  73. plot.experiment(df, "single_fixed_alpha.pdf")
  74. df <- experiment(population, 10, recom.singlearithm(alpha.runif()))
  75. plot.experiment(df, "single_rand_alpha.pdf")
  76. df <- experiment(population, 10, recom.wholearithm(alpha.fixed(0.5)))
  77. plot.experiment(df, "whole_fixed_alpha.pdf")
  78. df <- experiment(population, 10, recom.wholearithm(alpha.runif()))
  79. plot.experiment(df, "whole_rand_alpha.pdf")
  80. }