Browse Source

sync

master
Tom 3 years ago
parent
commit
77a1c3a0e7
8 changed files with 79 additions and 3 deletions
  1. +71
    -0
      ue04/ev.R
  2. BIN
      ue04/ev.gif
  3. BIN
      ue04/ev.pdf
  4. BIN
      ue04/ev_with_trans.gif
  5. BIN
      ue04/ev_with_trans.pdf
  6. +8
    -3
      ue04/linear_scaling.R
  7. BIN
      ue04/linescale.gif
  8. BIN
      ue04/linscale.pdf

+ 71
- 0
ue04/ev.R 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 View File

Before After
Width: 480  |  Height: 480  |  Size: 1017 KiB

BIN
ue04/ev.pdf View File


BIN
ue04/ev_with_trans.gif View File

Before After
Width: 480  |  Height: 480  |  Size: 1016 KiB

BIN
ue04/ev_with_trans.pdf View File


+ 8
- 3
ue04/linear_scaling.R View File

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

BIN
ue04/linescale.gif View File

Before After
Width: 480  |  Height: 480  |  Size: 3.6 MiB Width: 480  |  Height: 480  |  Size: 3.5 MiB

BIN
ue04/linscale.pdf View File


Loading…
Cancel
Save