forked from bradduthie/gamesGA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameGA_browser.Rmd
145 lines (108 loc) · 6.43 KB
/
gameGA_browser.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---
title: "gameGA: Browser interface"
output:
html_document: default
header-includes:
linestretch: 1
runtime: shiny
---
```{r, echo=FALSE}
library(gamesGA)
library(shiny)
library(rhandsontable)
```
********************************************************************************
> **The [gameGA](https://github.com/bradduthie/gamesGA) 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](https://shiny.rstudio.com/) application recognises user inputs in the `games_ga()` function and returns a table showing surviving strategies and their corresponding frequencies.**
> [See here](#help) for instructions on [inputs](#help) and interpretion of [table](#out) and [figure](#out2) output. To install the `gamesGA` package, [see here](https://github.com/bradduthie/gamesGA/blob/master/README.md).
********************************************************************************
<br>
**Input variables into the model below**
```{r, echo=FALSE}
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'
)
```
```{r, echo=FALSE}
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);
})
```
<br><br>
**Table of evolved strategies**
```{r, echo=FALSE}
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)
```
<br><br>
**Mean per round fitness over generations**
```{r, echo = FALSE}
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);
})
```
<br>
********************************************************************************
<br>
<a name="help">Input variables</a>
--------------------------------------------------------------------------------
A list of all of the inputs that can be used in the embedded application above is presented below with explanation:
- `CC` The payoff that the focal player receives if the focal player and the opponent both cooperate
- `DC` The payoff that the focal player receives if the focal player defects but the opponent cooperates
- `CD` The payoff that the focal player receives if the focal player cooperates but the opponent defects
- `DD` The payoff that the focal player receives if the focal player and the opponent both defect
- `Rounds` The number of rounds a focal player plays against each opponent
- `Generations` The number of generations the genetic algorithm is allowed to run before producing the output of strategies. By default, **each strategy will compete against all 100 strategies in the population (including itself)**
- `Pr(Cossover)` The probability that a crossover event occurs at any given locus between a focal player and a randomly selected player in the population
- `Pr(Mutation)` For each locus of every player, the probability that a mutation occurs in a generation
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.
<br>
<a name="out">Table output</a>
--------------------------------------------------------------------------------
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](https://github.com/bradduthie/gamesGA/blob/master/src/fitness.c) handles the reduced information). Finally, the **Final %** column indicates the perecentage of individuals in the population adopting the row strategy.
<a name="out2">Figure output</a>
--------------------------------------------------------------------------------
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.