/
models.py
210 lines (163 loc) · 7.09 KB
/
models.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import numpy as np
import random
from environment import Individual, PairEnvironment
def create_pairs(N, randomp):
pairs = []
for i in range(N):
pairs.append(PairEnvironment())
# We set the random individual chosen to compare different results obtained from different models
if( i == randomp ):
pairs[i].set_random_ind(True)
return pairs
#########################################################################
#########################################################################
########### DETERMINISTIC MODEL ###########
#########################################################################
#########################################################################
def deterministic(learn, habit, M, N, pairs):
deterministic_donations = [] # List with all the means of the donations for deterministic model
deterministic_aspirations = [] # List with all the means of the aspirations for deterministic model
while( M > 0 ):
don_mean = 0.0 # in every iteration we calculate the mean of all the donations
asp_mean = 0.0 # in every iteration we calculate the mean of all the aspirations
### STEP 1. Each pair make the donation ###
for p in pairs:
don_d, asp_d, don_r, asp_r = p.get_state(False)
# Keeping the evolution of every individual of every pair
p.dictator.my_donations.append(don_d)
p.dictator.my_aspirations.append(asp_d)
p.recipient.my_donations.append(don_r)
p.recipient.my_aspirations.append(asp_r)
don_mean += don_r
asp_mean += asp_r
# Making the donation, current recipient's attributes are updated
p.make_donation(learn, habit)
# Keeping the mean of the donations in this iteration
deterministic_donations.append(don_mean / N)
# Keeping the mean of the aspirations in this iteration
deterministic_aspirations.append(asp_mean / N)
### STEP 2. Shuffling the pairs and swapping the roles (inside ind_exchange) randomly ###
exchanges = random.sample(range(N), N)
index = list(range(N))
for (i, j) in zip(exchanges, index):
if (i == j):
pairs[i].swap_roles()
index.remove(i)
exchanges.remove(i)
else:
pairs[i].ind_exchange(pairs[j])
exchanges.remove(i)
index.remove(i)
exchanges.remove(j)
index.remove(j)
M -= 1
### We go to STEP 1 again ###
return pairs, deterministic_donations, deterministic_aspirations
#########################################################################
#########################################################################
############ STOCHASTIC MODEL #############
#########################################################################
#########################################################################
def stochastic(learn, habit, M, N, pairs, epsilon):
stochastic_donations = [] # List with all the means of the donations for stochastic model
stochastic_aspirations = [] # List with all the means of the aspirations for stochastic model
while( M > 0 ):
don_mean = 0.0 # in every iteration we calculate the mean of all the donations
asp_mean = 0.0 # in every iteration we calculate the mean of all the aspirations
### STEP 1. Each pair make the donation ###
for p in pairs:
don_d, asp_d, don_r, asp_r = p.get_state(False)
# Keeping the evolution of every individual of every pair
p.dictator.my_donations.append(don_d)
p.dictator.my_aspirations.append(asp_d)
p.recipient.my_donations.append(don_r)
p.recipient.my_aspirations.append(asp_r)
don_mean += don_r
asp_mean += asp_r
# Making the donation, current recipient's attributes are updated
p.make_stoch_donation(learn, habit, epsilon)
# Keeping the mean of the donations in this iteration
stochastic_donations.append(don_mean / N)
# Keeping the mean of the aspirations in this iteration
stochastic_aspirations.append(asp_mean / N)
### STEP 2. Shuffling the pairs and swapping the roles (inside ind_exchange) randomly ###
exchanges = random.sample(range(N), N)
index = list(range(N))
for (i, j) in zip(exchanges, index):
if (i == j):
pairs[i].swap_roles()
index.remove(i)
exchanges.remove(i)
else:
pairs[i].ind_exchange(pairs[j])
exchanges.remove(i)
index.remove(i)
exchanges.remove(j)
index.remove(j)
M -= 1
### We go to STEP 1 again ###
return pairs, stochastic_donations, stochastic_aspirations
#########################################################################
#########################################################################
#### MODEL EXTENSION: ENVIOUS INDIVIDUALS AND FREE_RIDERS ####
#########################################################################
#########################################################################
def extension(learn, habit, M, N, pairs, epsilon, envious_prob, fr):
extension_donations = [] # List with all the means of the donations for the model extensions
extension_aspirations = [] # List with all the means of the aspirations for model extensions
## Changing the individuals making them have a probability of being envious ##
envious_pairs = [1] * int(N * envious_prob) + [0] * int(N * (1 - envious_prob))
random.shuffle(envious_pairs)
for (env, p) in zip(envious_pairs, pairs):
if env:
p.set_envious(True)
## Changing the individuals in order to have a concrete number of free-riders ##
free_rider = []
for i in range(fr):
free_rider.append(random.randint(0, N-1))
for i in free_rider:
pairs[i].set_free_rider(True)
while( M > 0 ):
don_mean = 0.0 # in every iteration we calculate the mean of all the donations
asp_mean = 0.0 # in every iteration we calculate the mean of all the aspirations
### STEP 1. Each pair make the donation ###
for p in pairs:
don_d, asp_d, don_r, asp_r = p.get_state(False)
# Keeping the evolution of every individual of every pair
p.dictator.my_donations.append(don_d)
p.dictator.my_aspirations.append(asp_d)
p.recipient.my_donations.append(don_r)
p.recipient.my_aspirations.append(asp_r)
don_mean += don_r
asp_mean += asp_r
## if the dictator is a free-rider, make a free-rider donation ##
if p.is_free_rider():
p.make_freerider_donation(learn, habit, epsilon)
else:
## else, if the dictator is envious, make an envious donation ##
if p.is_envious():
p.make_envious_donation(learn, habit, epsilon)
## else, make an stochastic donation ##
else:
p.make_stoch_donation(learn, habit, epsilon)
# Keeping the mean of the donations in this iteration
extension_donations.append(don_mean / N)
# Keeping the mean of the aspirations in this iteration
extension_aspirations.append(asp_mean / N)
### STEP 2. Shuffling the pairs and swapping the roles (inside ind_exchange) randomly ###
exchanges = random.sample(range(N), N)
index = list(range(N))
for (i, j) in zip(exchanges, index):
if (i == j):
pairs[i].swap_roles()
index.remove(i)
exchanges.remove(i)
else:
pairs[i].ind_exchange(pairs[j])
exchanges.remove(i)
index.remove(i)
exchanges.remove(j)
index.remove(j)
M -= 1
### We go to STEP 1 again ###
return pairs, extension_donations, extension_aspirations