###################### ### starts here ###################### #### compiling or not compiling doesn't change the relative speeds of the two versions ## library(compiler) ## enableJIT(3) ## MM: Need different package libraries for different R versions myLib <- file.path("/usr/local.nfs/app/R/R_local", if(getRversion() <= "3.1.3") "library-3.1.3" else "library") library(igraph, lib.loc = myLib) #### a function used later PlaytheGame <- function(i11, opp1) { P <- sum(PayoffMatrix[strategy[i11], strategy[opp1]]) return(P/length(opp1)) } # Play the Game #### set some parameters (this is a game theory simulation, played on a network) ### payoff matrix for the game b <- 1.5 PayoffMatrix <- matrix(c(1,0,b,0),nrow=2,ncol=2,byrow=TRUE) ## each player plays Total Rounds of the game TotalRounds <- 100 ## number of agents num <- 100 ## density of the network dens <- 0.10 avgdeg <- dens*num/2 ## how many opponents to play at each round numoppon <- 1 ### create the networks to play the game on ## notice that these are here to test the speed of the simulation ## in principle they should be inside the repetitions loop. ## I moved them here to see if igraph is causing the problem, ## igraph is only called here now instead of each time the experiment repeats. ## Below you can see these commented out. You can check the effects of igraph by uncommenting them below. ## I concluded the problem is not igraph. Sadly. set.seed(101)# << MM: make truly reproducible ## create the networks to play on repeat { g <- erdos.renyi.game(num,dens) if(is.connected(g)) break } # repeat g # the graph neigh <- neighborhood(g, order = 1) str(neigh, list.len = 7) strategy <- sample(c(1,2), num, replace = TRUE) ## set aggregated payoffs to zero for each player payoff <- rep(0,num) Sys.setlocale("LC_MESSAGES", "C")# for portability of outputs cat("Before experimental rounds start, current proc.time():\n"); proc.time() ## ## ## start time here ## ## for(r in 1:TotalRounds) { PayVector <- numeric(num) # << original was numeric(), which of course # is "bad" .. but it's not the culprit ## for each player, make him play the game for (i in 1:num) { ## he plays against his neighbours n1 <- neigh[[i]][-1] nopp <- max(length(n1), numoppon) opp <- if(length(n1) > 1) sample(n1, nopp) else n1[1] ## # get a payoff from each opponent played and add to payoffs PayVector[i] <- PlaytheGame(i,opp) # <<< uses "globals" PayoffMatrix, strategy }# i loop ## ## after everyone has played, pick one guy and revise his strategy chosen.one <- sample(1:num,1) ## for the sake of debugging just change strategy randomly strategy[chosen.one] <- sample(c(0,1),1) ## update payoff records payoff <- payoff+PayVector ## # a single run of the experiment ends ends here } # r loop cat("Done", TotalRounds, "rounds; current proc.time():\n") proc.time() sessionInfo() # here, just in case more packages are loaded, etc