Skip to content

Instantly share code, notes, and snippets.

@samuelsmal
Last active August 29, 2015 14:05
Show Gist options
  • Save samuelsmal/26ed285beb8f96879f41 to your computer and use it in GitHub Desktop.
Save samuelsmal/26ed285beb8f96879f41 to your computer and use it in GitHub Desktop.
R Script to plot the data from my genetic programming paper
cat("\014") # clears the console
raw_data <- read.table("~/Desktop/raw_data/do_1_nohigherfn_data_points.txt", sep="\t", col.names=c("pos", "WIN EP", "WIN RP", "draws EP", "draws RP", "matches", "title"), fill=FALSE, strip.white=TRUE)
raw_data["generation"] <- NA # adding a new column
numberOfGenerations = 100
numberOfWinners = 16
# Adding the generation count
#for (i in 0:numberOfGenerations) {
# for (j in 1:numberOfWinners + (numberOfWinners * i)) {
# raw_data[j, "generation"] = i + 1
# }
#}
node_count <- read.table("~/Desktop/raw_data/do_1_nohigherfn_node_count.txt", sep="\n", col.names=c("count"), fill=FALSE, strip.white=TRUE)
plot_data <- matrix(,nrow = numberOfGenerations, ncol = 5)
measurefunction <- function(winEP, winRP, drawsEP, drawsRP) {
return (winEP * 2 + winRP * 2 + (drawsEP + drawsRP))
}
for (i in 1:numberOfGenerations) {
index = numberOfWinners * (i - 1) + 1
plot_data[i, 1] = i
plot_data[i, 2] = measurefunction(raw_data[index, "WIN.EP"], raw_data[index, "WIN.RP"], raw_data[index, "draws.EP"], raw_data[index, "draws.RP"])
plot_data[i, 3] = raw_data[index, "WIN.EP"]
plot_data[i, 4] = raw_data[index + 7, "WIN.RP"]
plot_data[i, 5] = node_count[i, "count"]
}
playermax <- max(c(max(plot_data[, 2]), max(plot_data[, 3], max(plot_data[, 4])))
max(plot_data[,2])
min(plot_data[,2])
max(plot_data[,3])
min(plot_data[,3])
## add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)
plot(plot_data[,2], ylim=c(0, playermax), typ='l', ann=F, col="blue")
lines(plot_data[,3], ann=F, col="green")
linear.model = lm(plot_data[,3] ~ row(plot_data)[,3])
abline(linear.model, col="green")
lines(plot_data[,4],ann=F, col="red")
# regression
linear.model = lm(plot_data[,2] ~ row(plot_data)[,2])
abline(linear.model, col="blue")
## Allow a second plot on the same graph
par(new = TRUE)
## Plot the second plot and put axis scale on right
plot(plot_data[,5], col="black", typ='l', xlab="", ylab="", ylim=c(0, max(node_count["count"])),
axes=FALSE)
linear.model = lm(plot_data[,5] ~ row(plot_data)[,5])
abline(linear.model, col="black")
## a little farther out (line=4) to make room for labels
mtext("Node count",side=4,col="black",line=4)
axis(4, ylim=c(0, numberOfGenerations), col="black",col.axis="black",las=1)
title(main = "Ramped half-and-half, no higher-level functions\nPopulation: 240, Generations: 100")
title(xlab = "Generations")
title(ylab = "Measure function")
## Add Legend
legendColours <- c("blue","green", "red", "black")
legend("topleft",legend=c("Fitness function", "Wins against EP", "Wins against RP", "Node count"),
text.col=legendColours ,pch=c(16,15),col=legendColours)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment