-
Notifications
You must be signed in to change notification settings - Fork 0
/
criminological_modelling.R
154 lines (124 loc) · 3.62 KB
/
criminological_modelling.R
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
143
144
145
146
147
148
149
150
151
152
153
154
indDF <- data.frame (id=1:2, strategy=NA, num_wins=0)
indDF
choose_Strategy <- function(ind){
strats <- sample(x=1:3, size=nrow(ind))
ind$strategy <- strats
return(ind)
}
##1=Paper, 2=Scissors, 3=Rock
playStrategy <- function(ind){
if (ind$strategy[1] == ind$strategy[2]) {
# same strategies, it's a tie
} else {
if (any(ind$strategy == 3) && any(ind$strategy == 1)) {
# this is the special case of rock over paper
# figure out who of the two IS the winner
winner_id <- ind[ind$strategy == 1, "id"]
ind <- increment_winner(ind = ind, winner_id = winner_id)
} else {
# find index for higher strategy
# index also happens to be winner_id as per above
winner_id <- which(ind[, "strategy"] == max(ind[, "strategy"]))
ind <- increment_winner(ind = ind, winner_id = winner_id)
}
}
return(ind)
}
increment_winner <- function(ind, winner_id) {
ind[winner_id, "num_wins"] <- ind[winner_id, "num_wins"] + 1
return(ind)
}
for (i in 1:1000) {
indDF <- choose_Strategy(indDF)
indDF <- playStrategy(indDF)
i <- i + 1
};indDF
setup <- function(){
return(data.frame(id=1:2, strategy=NA, num_wins=0))
}
###setup function for setting multiple samples
rounds <- 1000
indDF <- setup()
dat <- matrix(NA, rounds, 2)
for (i in 1:rounds) {
indDF <- choose_Strategy(indDF)
indDF <- playStrategy(indDF)
dat[i,] <- indDF$num_wins
i <- i + 1
}
plot(dat[,1], type="l", col="#EA3E49", lwd=3, xlab = "time", ylab = "number of rounds won")
lines(dat[,2], col="#77C4D3", lwd=3)
##A player who switches his strategy and a player who uses always the same strategy.
#Who would win?
choose_Strategy2 <- function(ind){
strats <- sample(x = 1:3, size = 1)
ind$strategy[1] <- strats
return(ind)
}
###################################
##My own try on this modell
exhDF <- data.frame (
id = c("Mark", "Maria"),
strategy=NA,
num_wins=0)
exhDF
###not use strings for
choose_Strategy3 <- function(exh){
strats <- sample(x=1:3, size=nrow(exh))
exh$strategy <- strats
return(exh)
}
playStrategy3 <- function(exh){
if(any(exh$strategy == 2) && any(exh$strategy == 1)){
tmp <- exh[exh$strategy == 2, "id"]
exh[tmp, "num_wins"] <- exh[tmp, "num_wins"] + 1
}
if(any(exh$strategy == 3) && any(exh$strategy == 2)){
tmp <- exh[exh$strategy == 3, "id"]
exh[tmp, "num_wins"] <- exh[tmp, "num_wins"] + 1
}
if(any(exh$strategy == 3) && any(exh$strategy == 1)) {
tmp <- exh[exh$strategy == 1, "id"]
exh[tmp, "num_wins"] <- exh[tmp, "num_wins"] + 1
}
else {}
return(exh)
}
for (i in 1:100) {
exhDF <- choose_Strategy3(exhDF)
exhDF <- playStrategy3(exhDF)
i <- i + 1
};exhDF
setup <- function(){
return(data.frame(
id = c("Mark", "Maria")),
strategy=NA,
num_wins=0)
}
rounds <- 100
exhDF <- setup()
dat <- matrix(NA, rounds, 2)
for (i in 1:rounds) {
exhDF <- choose_Strategy3(exhDF)
exhDF <- playStrategy3(exhDF)
dat[i,] <- exhDF$num_wins
i <- i + 1
}
plot(dat[,1], type="l", col="violet", lwd=3, xlab = "time", ylab = "number of rounds won")
lines(dat[,2], col="yellow", lwd=3)
###############
#Head or Tail
#set.seed(seed) generates random numbers
coin = c("Head", "Tail")
set.seed(100) #to make results reproducible
y = sample(coin, 6, replace = TRUE)
length(y[y=="Tail"])
replicate(100, sample(coin, 6, replace = TRUE ))
a = replicate(100, length(sample(coin, 6, replace = TRUE)[y == "Tail"]))
mean(a)
#####not needed, was for axelrod modell
if p >=0.2 {
changed_feature <- sample(which(!overlap), size = 1)
a[site["y_cor"], site["x_cor"], changed_feature] <- a[the_neighbor[[1]]["y_cor"], the_neighbor[[1]]["x_cor"], changed_feature]
}
}