sync
This commit is contained in:
84
ue04/linear_scaling.R
Normal file
84
ue04/linear_scaling.R
Normal file
@@ -0,0 +1,84 @@
|
||||
library(ggplot2)
|
||||
library(gganimate)
|
||||
|
||||
linscale.initpopulation <- function(n) {
|
||||
return(runif(10))
|
||||
}
|
||||
|
||||
linscale.scale <- function(population, a, b) {
|
||||
return(a * population + b)
|
||||
}
|
||||
|
||||
linscale.relfitness <- function(population) {
|
||||
return(population / sum(population))
|
||||
}
|
||||
|
||||
linscale.tracegens <- function(popul, a, b, n) {
|
||||
lenpopul <- length(popul)
|
||||
|
||||
df <- data.frame( generation = 0
|
||||
,individual = 1:lenpopul
|
||||
,fitness = popul
|
||||
,relfitness = linscale.relfitness(popul)
|
||||
)
|
||||
|
||||
|
||||
|
||||
for(i in 1:n) {
|
||||
popul <- linscale.scale(popul, a, b)
|
||||
|
||||
df <- rbind(df, data.frame( generation = i
|
||||
,individual = 1:lenpopul
|
||||
,fitness = popul
|
||||
,relfitness = linscale.relfitness(popul)))
|
||||
}
|
||||
|
||||
return(df)
|
||||
}
|
||||
|
||||
linscale.experiment <- function(avals, bvals, n) {
|
||||
df <- data.frame( generation = integer()
|
||||
,individual = integer()
|
||||
,fitness = double()
|
||||
,relfitness = double()
|
||||
,a = double()
|
||||
,b = double())
|
||||
|
||||
initpopul <- linscale.initpopulation(10)
|
||||
|
||||
for(a in avals) {
|
||||
for(b in bvals) {
|
||||
dftmp <- linscale.tracegens(initpopul, a, b, n)
|
||||
dftmp["a"] <- a
|
||||
dftmp["b"] <- b
|
||||
|
||||
df <- rbind(df, dftmp)
|
||||
}
|
||||
}
|
||||
|
||||
return(df)
|
||||
}
|
||||
|
||||
linscale.plot <- function(df) {
|
||||
pdf(file="linscale.pdf", onefile=TRUE)
|
||||
|
||||
for(g in unique(df$generation)) {
|
||||
p <- ggplot(data=df[df$generation == g, ]) +
|
||||
geom_col(aes(x=individual, y=relfitness)) +
|
||||
facet_grid(b ~ a, labeller = label_both) +
|
||||
labs(title=sprintf("generation: %i", g))
|
||||
print(p)
|
||||
}
|
||||
|
||||
dev.off()
|
||||
}
|
||||
|
||||
linscale.animate <- function(df) {
|
||||
anim <- ggplot(data=df) +
|
||||
geom_col(aes(x=individual, y=relfitness)) +
|
||||
facet_grid(b ~ a, labeller = label_both) +
|
||||
labs(title="generation: {closest_state}") +
|
||||
transition_states(generation)
|
||||
|
||||
anim_save("linescale.gif", animation=anim)
|
||||
}
|
BIN
ue04/linescale.gif
Normal file
BIN
ue04/linescale.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.6 MiB |
BIN
ue04/linscale.pdf
Normal file
BIN
ue04/linscale.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user