-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadapt.R
43 lines (36 loc) · 1.11 KB
/
adapt.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
Adapt <- function(g
){
######
## Useful variables
######
n.secu <- length((V(g)$secu)[[1]])
n.traders <- length(V(g))
######
## Adapt approximate model
######
# Draw random order of the traders
turns <- sample(1:n.traders, n.traders, replace = FALSE)
### Adapt approximate model
for (i in turns){
# If the trader has neighbours
if (length(neighbors(g,i)>=1)){
# Determine richest neighbor
richest <- neighbors(g,i)[V(g)$money[neighbors(g,i)] == max(V(g)$money[neighbors(g,i)])]
## If there is more than one richest neighbour (unlikely but you never know)
if (length(richest)>1){
richest <- sample(richest,1)
}
# if the most successful neighbor does better than i
if (V(g)$money[richest] > V(g)$money[i]) {
# With proba V(g)$ideo[i], adopt the model of the most successful neighbor
if (runif(1) >= V(g)$ideo[i]){
V(g)$approx[i] <- V(g)$approx[richest]
}
}
}
}
#######
## Return the network
#######
g
}