Skip to content

Instantly share code, notes, and snippets.

@tmastny
Created November 24, 2021 18:16
Show Gist options
  • Save tmastny/0ddde097f0ce6ce11a4cabefd45db349 to your computer and use it in GitHub Desktop.
Save tmastny/0ddde097f0ce6ce11a4cabefd45db349 to your computer and use it in GitHub Desktop.
Single-round, multiple person Prisoner's Dilemma.
I want to maximize my payout.
If I choose `c` and everyone else choses `d` then I get `0`.
```{r}
library(tidyverse)
```
```{r}
p1_payout <- function(p1, p2) {
case_when(
p1 == 'c' & p2 == 'c' ~ 3,
p1 == 'c' & p2 == 'd' ~ 0,
p1 == 'd' & p2 == 'c' ~ 5,
p1 == 'd' & p2 == 'd' ~ 1
)
}
```
```{r}
p1_payout('d', 'd')
p1_payout('d', c('d', 'd'))
```
```{r}
opponent_choices <- tibble(cs = 0:19) %>%
rowwise() %>%
mutate(choices = list(c(rep('c', cs), rep('d', 19 - cs))))
head(opponent_choices)
```
```{r}
p1_outcomes <- opponent_choices %>%
ungroup() %>%
full_join(tibble(p1 = c('c', 'd')), by = character()) %>%
rowwise() %>%
mutate(payoff = sum(p1_payout(p1, choices)))
p1_outcomes
```
```{r}
p1_outcomes %>%
select(-choices) %>%
pivot_wider(names_from = p1, values_from = payoff)
```
```{r}
p1_outcomes %>%
ggplot() +
geom_line(aes(cs, payoff, color = p1))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment