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.

71 lines
1.9 KiB

3 years ago
  1. source("linear_scaling.R")
  2. library(ggplot2)
  3. library(RColorBrewer)
  4. fittrans.id <- function(popul) {
  5. return(popul)
  6. }
  7. fittrans.linscale <- function(a, b) {
  8. return(function(popul) {
  9. popul[,2] = a * popul[,2] + b
  10. return(popul)
  11. })
  12. }
  13. select.fps <- function(popul) {
  14. relfit <- linscale.relfitness(popul[,2])
  15. filtr <- sample(popul[,1], length(popul[,1]), prob=relfit, replace=TRUE)
  16. return(popul[filtr,])
  17. }
  18. ev.run <- function(popul, gen, selection, fittrans=fittrans.id) {
  19. npopul <- length(popul)
  20. namedpopul <- matrix(c(1:npopul, popul), ncol=2)
  21. df = data.frame( gen = 0
  22. ,fitness = namedpopul[,2]
  23. ,ancestor = namedpopul[,1]
  24. ,relfit = linscale.relfitness(namedpopul[,2]))
  25. for(g in 1:gen) {
  26. namedpopul <- selection(fittrans(namedpopul))
  27. df = rbind(df, data.frame( gen = g
  28. ,fitness = namedpopul[,2]
  29. ,ancestor = namedpopul[,1]
  30. ,relfit = linscale.relfitness(namedpopul[,2])))
  31. }
  32. return(df)
  33. }
  34. ev.plot <- function(df, filename) {
  35. pdf(file=filename, onefile=TRUE)
  36. for(g in unique(df$gen)) {
  37. p <- ggplot(data=df[df$gen == g,], aes(x="", y=relfit, fill=factor(ancestor))) +
  38. geom_bar(stat="identity", width=1) +
  39. coord_polar("y", start=0) +
  40. labs(x=NULL, y=NULL, fill="ancestor", title=sprintf("generation: %i", g))
  41. print(p)
  42. }
  43. dev.off()
  44. }
  45. ev.animate <- function(df, filename) {
  46. anim <- ggplot(data=df, aes(x="", y=relfit, fill=factor(ancestor))) +
  47. geom_bar(stat="identity", width=1) +
  48. coord_polar("y", start=0) +
  49. labs(x=NULL, y=NULL, fill="ancestor", title="generation: {closest_state}") +
  50. transition_states(gen)
  51. anim_save(filename, animation=anim)
  52. }