library(gamesGA)
library(shiny)
library(rhandsontable)

The gameGA R package simulates a genetic algorithm for finding adaptive strategies in sequentially played rounds of any two by two sequential payoff matrix game iterated between players. Below, the embedded shiny application recognises user inputs in the games_ga() function and returns a table showing surviving strategies and their corresponding frequencies.

See here for instructions on inputs and interpretion of table and figure output. To install the gamesGA package, see here.



Input variables into the model below

inputPanel(

    numericInput("CC", label = "CC", 3, width="50%"),
    numericInput("DC", label = "DC", 5, width="50%"),
    numericInput("CD", label = "CD", 0, width="50%"),
    numericInput("DD", label = "DD", 1, width="50%"),
    numericInput("RnD", label = "Rounds", 100, width="50%"),
    numericInput("Gen", label = "Generations", 250, width="50%"),
    numericInput("Cro", label = "Pr(Crossover)", 0.05, width="50%"),
    numericInput("mu", label = "Pr(Mutation)", 0.05, width="50%"),    

    style='width: 900px; height: 150px'
)
gres <- reactive({ 
     cc <- input$CC;
     dc <- input$DC;
     cd <- input$CD;
     dd <- input$DD;
     rd <- input$RnD;
     gn <- input$Gen;
     co <- input$Cro;
     mu <- input$mu;

     game_res <- games_ga(CC = cc, DC = dc, CD = cd, DD = dd, rounds = rd,
                          generations = gn, cross_prob = co, 
                          mutation_prob = mu, num_opponents = 100);     
     all <- c(game_res$genos, "break_1",
              rownames(game_res$genos), "break_2",
              game_res$fitness);
    })



Table of evolved strategies

renderTable({
      break1   <- which(gres()=="break_1")[1] - 1;
      break2   <- which(gres()=="break_2")[1] - 1;
      geno_vec <- gres()[1:break1];
      geno_row <- gres()[(break1+2):break2];
      genos    <- matrix(data =  geno_vec, ncol = 10, byrow = FALSE);
      rownames(genos) <- geno_row;
      colnames(genos) <- c("CCC", "CCD", "CDC", "CDD", "DCC", "DCD", "DDC",
                           "DDD", "1st", "Final %");
      final_table <- genos;

}, include.rownames=TRUE)



Mean per round fitness over generations

renderPlot({
    break2    <- which(gres()=="break_2")[1]+1;
    end       <- length(gres());
    fitnesses <- gres()[break2:end];
    fitnesses <- as.numeric(fitnesses);
    mean_fit  <- fitnesses / (input$RnD * 100);
    maxpt     <- max(c(input$CC, input$CD, input$DC, input$DD));
    par(mar=c(5,5,1,1));
    plot(x = 1:length(mean_fit), y=mean_fit, type="l", ylim=c(0,maxpt), lwd = 3,
        xlab="Generation", ylab = "Mean strategy fitness per round",
         cex.axis=1.5, cex.lab=1.5);
})




Input variables

A list of all of the inputs that can be used in the embedded application above is presented below with explanation:

The inputs CC, DC, CD, and DD can be visualised as the payoffs that a focal individual receives on a standard $2 \times 2$ payoff matrix, shown below.

| | Opponent cooperates | Opponent defects | |-------------------------|---------------------|------------------| | Focal player cooperates | CC | CD | | Focal player defects | DC | DD |

Hence, gamesGA does not (yet) model non-symmetric payoffs.


Table output

In the above output table, each row corresponds to a unique strategy that is present in the population after Generations of simulated evolution. The first eight columns in the table correspond to the response of a focal strategy given the history of an opponent. For example, in the first column, C C C corresponds to a history of three previous rounds of an opponent playing C (cooperate). The row element in this column therefore indicates the focal strategy's response (C cooperation, or D defect) if its opponent has coopereated three times in a row. The nineth column indicates the evolved strategy that a focal player adopts for their first round move (for second and third round moves, see how the code handles the reduced information). Finally, the Final % column indicates the perecentage of individuals in the population adopting the row strategy.

Figure output

The figure above shows the mean number of points a the average strategy accrues per round in each generation. For example, if CC = 3 in a simulation, and all strategies cooperate, then the mean per round fitness will always be 3.



bradduthie/gamesGA documentation built on March 7, 2020, 2:20 p.m.