diff --git a/ue04/ev.R b/ue04/ev.R new file mode 100644 index 0000000..0142163 --- /dev/null +++ b/ue04/ev.R @@ -0,0 +1,71 @@ +source("linear_scaling.R") +library(ggplot2) +library(RColorBrewer) + +fittrans.id <- function(popul) { + return(popul) +} + +fittrans.linscale <- function(a, b) { + return(function(popul) { + popul[,2] = a * popul[,2] + b + + return(popul) + }) +} + +select.fps <- function(popul) { + relfit <- linscale.relfitness(popul[,2]) + + filtr <- sample(popul[,1], length(popul[,1]), prob=relfit, replace=TRUE) + return(popul[filtr,]) +} + +ev.run <- function(popul, gen, selection, fittrans=fittrans.id) { + npopul <- length(popul) + + namedpopul <- matrix(c(1:npopul, popul), ncol=2) + + df = data.frame( gen = 0 + ,fitness = namedpopul[,2] + ,ancestor = namedpopul[,1] + ,relfit = linscale.relfitness(namedpopul[,2])) + + for(g in 1:gen) { + namedpopul <- selection(fittrans(namedpopul)) + + df = rbind(df, data.frame( gen = g + ,fitness = namedpopul[,2] + ,ancestor = namedpopul[,1] + ,relfit = linscale.relfitness(namedpopul[,2]))) + } + + return(df) +} + +ev.plot <- function(df, filename) { + pdf(file=filename, onefile=TRUE) + + + for(g in unique(df$gen)) { + p <- ggplot(data=df[df$gen == g,], aes(x="", y=relfit, fill=factor(ancestor))) + + geom_bar(stat="identity", width=1) + + coord_polar("y", start=0) + + labs(x=NULL, y=NULL, fill="ancestor", title=sprintf("generation: %i", g)) + + print(p) + } + + dev.off() +} + +ev.animate <- function(df, filename) { + + anim <- ggplot(data=df, aes(x="", y=relfit, fill=factor(ancestor))) + + geom_bar(stat="identity", width=1) + + coord_polar("y", start=0) + + labs(x=NULL, y=NULL, fill="ancestor", title="generation: {closest_state}") + + transition_states(gen) + + anim_save(filename, animation=anim) +} diff --git a/ue04/ev.gif b/ue04/ev.gif new file mode 100644 index 0000000..6052d8f Binary files /dev/null and b/ue04/ev.gif differ diff --git a/ue04/ev.pdf b/ue04/ev.pdf new file mode 100644 index 0000000..345f549 Binary files /dev/null and b/ue04/ev.pdf differ diff --git a/ue04/ev_with_trans.gif b/ue04/ev_with_trans.gif new file mode 100644 index 0000000..df2e6fc Binary files /dev/null and b/ue04/ev_with_trans.gif differ diff --git a/ue04/ev_with_trans.pdf b/ue04/ev_with_trans.pdf new file mode 100644 index 0000000..5fb7d13 Binary files /dev/null and b/ue04/ev_with_trans.pdf differ diff --git a/ue04/linear_scaling.R b/ue04/linear_scaling.R index a744b86..2860cdd 100644 --- a/ue04/linear_scaling.R +++ b/ue04/linear_scaling.R @@ -1,6 +1,7 @@ library(ggplot2) library(gganimate) + linscale.initpopulation <- function(n) { return(runif(10)) } @@ -36,7 +37,7 @@ linscale.tracegens <- function(popul, a, b, n) { return(df) } -linscale.experiment <- function(avals, bvals, n) { +linscale.experiment <- function(avals, bvals, n, popul=c()) { df <- data.frame( generation = integer() ,individual = integer() ,fitness = double() @@ -44,11 +45,13 @@ linscale.experiment <- function(avals, bvals, n) { ,a = double() ,b = double()) - initpopul <- linscale.initpopulation(10) + if(length(popul) == 0) { + popul <- linscale.initpopulation(10) + } for(a in avals) { for(b in bvals) { - dftmp <- linscale.tracegens(initpopul, a, b, n) + dftmp <- linscale.tracegens(popul, a, b, n) dftmp["a"] <- a dftmp["b"] <- b @@ -82,3 +85,5 @@ linscale.animate <- function(df) { anim_save("linescale.gif", animation=anim) } + +data.population <- linscale.initpopulation(10) diff --git a/ue04/linescale.gif b/ue04/linescale.gif index 19d279b..b4ffa1b 100644 Binary files a/ue04/linescale.gif and b/ue04/linescale.gif differ diff --git a/ue04/linscale.pdf b/ue04/linscale.pdf index 8ca4be8..f314381 100644 Binary files a/ue04/linscale.pdf and b/ue04/linscale.pdf differ