-
Notifications
You must be signed in to change notification settings - Fork 0
/
axelrod.R
159 lines (126 loc) · 3.65 KB
/
axelrod.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
155
156
157
158
159
# first create data object
# TODO make each of these a variable (number of x, y, features)
features <- c("language", "style", "religion", "party", "cuisine")
seed_features <- function(n_of_features) {
sample(x = 0:9, size = n_of_features, replace = TRUE)
}
seed_array <- function(features) {
# create empty array
a <- array(
data = NA,
dim = c(10, 10, length(features)),
dimnames = list(
y_height = NULL,
x_width = NULL,
feature = features
)
)
n_of_features <- length(features)
# fill in empty array with initial traits
a2 <- apply(
X = a,
MARGIN = c(1, 2),
FUN = function(x) {
seed_features(n_of_features = n_of_features)
}
)
# turn array in the right orientation
a2 <- aperm(a = a2, perm = c(3, 2, 1))
dimnames(a2) <- dimnames(a)
return(a2)
}
# sample
select_site <- function(a){
x_cor <- sample(x = dim(a)[2], size = 1)
y_cor <- sample(x = dim(a)[1], size = 1)
site <- c(x_cor = x_cor, y_cor = y_cor)
return(site)
}
##function needs a and x y cor and result of select_site, produce list of vectors,
#each give name "south, west..", we return list
find_neighbors <- function(a, site){
# create all neighbors, including impossible
fake_neighbors <- list(
north = site + c(-1, 0),
west = site + c(0, 1),
south = site + c(1, 0),
east = site + c(0, -1)
)
# find impossible neighbors
existing_neighbors <- sapply(X = fake_neighbors, FUN = function(x) {
all(x <= dim(a)[c(1, 2)])
})
neighbors <- fake_neighbors[existing_neighbors]
return(neighbors)
}
##find the randomly selected neighbor and subset
random_neighbor <- function(neighbors){
the_neighbor <- sample(x = neighbors, size = 1)
return(the_neighbor)
}
#its a list
##comparing
find_overlap <- function(a, site, the_neighbor) {
overlap <- a[the_neighbor[[1]][2], the_neighbor[[1]][1],] == a[site[2],site[1],]
return(overlap)
}
#probability of interaction
find_p_int <- function(overlap, features) {
p <- sum(overlap)/length(features)
return(p)
}
make_int <- function(p, a, site, overlap, the_neighbor){
inter_yn <- sample(x = c(TRUE, FALSE), size=1, prob=c(p, 1-p))
if(inter_yn & !any(overlap)){
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]
}
return(a)
}
###use the overlap object, and sample again, over those tha are
#function:one iteration for the game
#another function: run it 1000 times(prob a loop)
one_iteration <- function(a){
features <- dimnames(x = a)$feature
site <- select_site(a = a)
neighbors <- find_neighbors(a =a, site = site)
the_neighbor <- random_neighbor(neighbors = neighbors)
overlap <- find_overlap(a = a, site = site, the_neighbor = the_neighbor)
p <- find_p_int(overlap = overlap, features = features)
a <- make_int(p = p, a = a, site = site, the_neighbor = the_neighbor, overlap = overlap)
return(a)
}
##looping
setup <- function(features){
a <- seed_array(features)
return(a)
}
iterate <- function(features, runs){
a <- setup(features)
for (i in 1:runs) {
a <- one_iteration(a)
}
return(a)
}
results <- iterate(features, runs = 10)
###what we had for the looping
#for (i in 1:10000) {
# a <- one_iteration(a)
#}
plot_one_iter <- function(a) {
df <- reshape2::melt(data = a, value.name = "trait")
df$x_width <- as.integer(df$x_width)
df$y_height <- as.integer(df$y_height)
df$trait <- as.factor(df$trait)
library(ggplot2)
ggplot(
data = df,
mapping = aes(
x = x_width,
y = y_height,
fill = trait
)
) +
geom_raster() +
facet_wrap(vars(feature))
}