###### ######## #some system times ############## #R version 3.5.1 # > system.time(source("simulationR-R.R")) # user system elapsed # 77.300 0.570 77.389 # > sessionInfo() # R version 3.5.1 (2018-07-02) # Platform: x86_64-apple-darwin15.6.0 (64-bit) # Running under: macOS Sierra 10.12.6 # Matrix products: default # BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib # LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib # locale: # [1] C # attached base packages: # [1] stats graphics grDevices utils datasets methods base # other attached packages: # [1] igraph_1.2.2 # loaded via a namespace (and not attached): # [1] compiler_3.5.1 magrittr_1.5 pkgconfig_2.0.2 ######### ## 3.1.3 # user system elapsed # 0.648 0.005 0.648 # > sessionInfo() # R version 3.1.3 Patched (2015-04-21 r70342) # Platform: x86_64-apple-darwin10.8.0 (64-bit) # Running under: OS X 10.9.5 (Mavericks) # locale: # [1] C # attached base packages: # [1] stats graphics grDevices utils datasets methods base # other attached packages: # [1] igraph_0.7.1 ######### ### from a colleague: # R version 3.4.4 (2018-03-15) -- "Someone to Lean On" # Platform: x86_64-w64-mingw32/x64 (64-bit) #  user  system elapsed  # 72.94    0.02   72.99  ###################### ### starts here ###################### #### compiling or not compiling doesn't change the relative speeds of the two versions # library(compiler) # enableJIT(3) library(igraph) #### a function used later PlaytheGame=function(i11=i,opp1=opp){ 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 ## how often to write data writefreq=20 #number of agents num<-100 # density of the network dens<-0.10 avgdeg<-dens*num/2 # create filenames to write output into filename=paste("R-R") filename2=paste("moran,R-R,g") filename3=paste("moran,R-R,gk") # delete old datafiles if (file.exists(filename)) file.remove(filename) if (file.exists(filename2)) file.remove(filename2) if (file.exists(filename3)) file.remove(filename3) # 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. # create the networks to play on repeat{ g<-erdos.renyi.game(num,dens) if(is.connected(g)){break} } # repeat neigh<-neighborhood(g,order=1) repeat{ gk<-erdos.renyi.game(num,dens) if(is.connected(gk)){break} } # repeat neighGk<-neighborhood(gk,order=1) distmatrix<-1/(shortest.paths(g)) diag(distmatrix)<-0 distmatrix2<-1/(shortest.paths(gk)) diag(distmatrix2)<-0 ###Here we repeat the experiment 5 times (for the actual project we are working on we repeat it 100 times) for(reps in 1:5){ ### initialize players' strategies strategy<-sample(c(1,2),num,replace=TRUE) oldstrategy=strategy ### set aggregated payoffs to zero for each player payoff<-rep(0,num) ######## # for the real thing we make new networks for each repetition ######## # repeat{ # g<-erdos.renyi.game(num,dens) # if(is.connected(g)){break} # } # repeat # neigh<-neighborhood(g,order=1) # repeat{ # gk<-erdos.renyi.game(num,dens) # if(is.connected(gk)){break} # } # repeat # neighGk<-neighborhood(gk,order=1) # distmatrix<-1/(shortest.paths(g)) # diag(distmatrix)<-0 # distmatrix2<-1/(shortest.paths(gk)) # diag(distmatrix2)<-0 ################### ############# end of the igraph stuff that should be done each time. #### # start time here #### for(r in 1:TotalRounds){ newstrat=numeric() PayVector=numeric() ## for each player, make him play the game for (i in 1:num){ ######## I tried doing this not using a loop, using apply, but it made no difference ### he plays against his neighbours n1=neigh[[i]][-1] nopp=max(length(n1),numoppon) opp=n1[1] if (length(n1)>1){ opp=sample(n1,nopp)} ### get a payoff from each opponent played and add to payoffs PayVector[i]=PlaytheGame(i,opp) }# i loop #### after everyone has played, pick one guy and revise his strategy chosen.one<-sample(1:num,1) #strategy[chosen.one]=ImitateRichestNeighbor(chosen.one,neighGk[[chosen.one]]) ###### for the sake of debugging just change strategy randomly strategy[chosen.one]<-sample(c(0,1),1) #update payoff records payoff=payoff+PayVector ### this write statement is not relevant to debugging for speed it seems #if((r %% writefreq)==0){ #write(file=filename,ncolumns=8,sep=",",x=c(r,sum(strategy==1),mean(payoff),sd(payoff),min(payoff),max(payoff),strategy[which.min(payoff)],strategy[which.max(payoff)]),append=T) #} ### a single run of the experiment ends ends here } #r loop ############## ### what follows has been commented out to see if the bottleneck was there # sadly, that didn't help. ############## # if((sum(strategy)==num) | (sum(strategy)==(num*2))){ # morant1="allthesame" # }else{morant1=Moran.I(strategy,distmatrix) # } # moranpayoff<-Moran.I(payoff,distmatrix) # as.character(moranpayoff) # if((sum(strategy)==num) | (sum(strategy)==(num*2))){ # moran2t1="allthesame" # }else{moran2t1=Moran.I(strategy,distmatrix) # } # moranpayoff2<-Moran.I(payoff,distmatrix2) # as.character(moranpayoff2) #write.table(x=c(as.character(morant1),as.character(moranpayoff)),file=filename2,sep=",",append=T,col.names=F) #write.table(x=c(as.character(moran2t1),as.character(moranpayoff2)),file=filename3,sep=",",append=T,col.names=F) print(c("done ",reps)) #### now the reptetition is done. } #reps loop