This commit is contained in:
Tom
2021-06-03 16:03:10 +02:00
parent 8817215f3a
commit 77a1c3a0e7
8 changed files with 79 additions and 3 deletions

71
ue04/ev.R Normal file
View File

@@ -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)
}

BIN
ue04/ev.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1017 KiB

BIN
ue04/ev.pdf Normal file

Binary file not shown.

BIN
ue04/ev_with_trans.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1016 KiB

BIN
ue04/ev_with_trans.pdf Normal file

Binary file not shown.

View File

@@ -1,6 +1,7 @@
library(ggplot2) library(ggplot2)
library(gganimate) library(gganimate)
linscale.initpopulation <- function(n) { linscale.initpopulation <- function(n) {
return(runif(10)) return(runif(10))
} }
@@ -36,7 +37,7 @@ linscale.tracegens <- function(popul, a, b, n) {
return(df) return(df)
} }
linscale.experiment <- function(avals, bvals, n) { linscale.experiment <- function(avals, bvals, n, popul=c()) {
df <- data.frame( generation = integer() df <- data.frame( generation = integer()
,individual = integer() ,individual = integer()
,fitness = double() ,fitness = double()
@@ -44,11 +45,13 @@ linscale.experiment <- function(avals, bvals, n) {
,a = double() ,a = double()
,b = double()) ,b = double())
initpopul <- linscale.initpopulation(10) if(length(popul) == 0) {
popul <- linscale.initpopulation(10)
}
for(a in avals) { for(a in avals) {
for(b in bvals) { for(b in bvals) {
dftmp <- linscale.tracegens(initpopul, a, b, n) dftmp <- linscale.tracegens(popul, a, b, n)
dftmp["a"] <- a dftmp["a"] <- a
dftmp["b"] <- b dftmp["b"] <- b
@@ -82,3 +85,5 @@ linscale.animate <- function(df) {
anim_save("linescale.gif", animation=anim) anim_save("linescale.gif", animation=anim)
} }
data.population <- linscale.initpopulation(10)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 MiB

After

Width:  |  Height:  |  Size: 3.5 MiB

Binary file not shown.