library(StratTourn)
library(shiny)
library(xtable)
library(knitr)
library(googleVis)
setwd("D:/libraries/StratTourn/studies")

tourn = load.tournament("Tourn_Public_goods_game_20140929_090958.tou")
# Data for each match
md = tourn$dt
# Names of all strategies
strats = unique(md$strat)

used.strats = strats
shown.strats = strats
generations = 100
alpha = 1
min.shares = 0.001
step.size = 10
start.generation = 1
init.size.string = paste0(used.strats,"=1", collapse=",")
#used.strats = c("tit.for.tat","always.defect","net.nice0","net.nice1")
#init.size.string = "always.coop=1,tit.for.tat=1,always.defect=1,net.nice0=100,net.nice1=1,forgiving.grim.trigger=1"

init.size = parse.sizes.string(init.size.string, used.strats=used.strats, sr=NULL)
init.size[2] = 3
set.storing(storing = TRUE)
res = n.evolve(initial=init.size, dt=md, generations=generations, alpha=alpha, min.shares = min.shares)

shares.mat = res$shares.mat
mdf = res$grid

if (!isTRUE(is.finite(step.size)))
  step.size = 1

if (start.generation >1) {
  rows = which(mdf$generation >= start.generation)
  mdf = mdf[rows,]
}

if (step.size >1) {
  used.gens = seq(start.generation,generations, by=step.size)
  rows = which(mdf$generation %in% used.gens)
  mdf = mdf[rows,]
}

p = gvisMotionChart(mdf, idvar = "strat", timevar = "time", xvar = "u",
yvar = "share", colorvar = "strategy", sizevar = "share", 
options = list())

#plot(p)
print(p, tag="chart")

Resulting sizes

res.size = shares.mat[NROW(shares.mat),]
res.size.string =  paste0(colnames(shares.mat)," = ", round(res.size*100,1), collapse=", ")
cat(res.size.string)

We simulate an evolutionary dynamic in which the proportion of strategies in the population changes over generations. Successful strategies grow and unsuccessful strategies shrink. A strategy s grows from one generation to the next generation essentially by the following simple linear population dynamics formula:

  size.next[s] = size[s] + alpha * (U[s]-U.mean) * size[s]

where U[s] is the weighted payoff of strategy s against all other strategies with weights being the current shares of each strategy in the population. U.mean is the weighted average of U[s] over all strategies. Hence a strategy's size in the population grows if and only if it has a higher average payoff than the weighted average payoff in the population. Otherwise, the strategy shrinks.

The parameter alpha determines the speed of the evolutionary process between two generations. The smaller alpha, the smoother are the evolutionary dynamics. (If you want quicker evolution, you may set alpha higher or, for smoother dynamics increase the number of generations but also increase the generation step. Increasing the generation step just reduces the size of the data frame that is transferred to the motion plot, which is the main performance bottleneck in the code).

After each evolution step we normalize the total population size to 1, soo that strategy sizes correspond to strategy shares. We also have the option to specify a minimal share of each strategy in the population.



skranz/StratTourn documentation built on May 30, 2019, 2:02 a.m.