-
Notifications
You must be signed in to change notification settings - Fork 0
/
adversarial_v0.1alpha.py
232 lines (208 loc) · 6.11 KB
/
adversarial_v0.1alpha.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# import statements
import numpy as np
import math
import random
from tempfile import TemporaryFile
outfile = TemporaryFile()
def arrival():
"""Return a 1-second average Poisson Arrival Process time."""
return -math.log(1.0 - random.random())
#ratios
def p_R(q):
"""Returns ratio of Paladins to total population.
Keyword arguments:
q -- population array
"""
return float(q[0] * q[4]**(-1))
def i_R(q):
"""Returns ratio of Informants to total population.
Keyword arguments:
q -- population array
"""
return float(q[1] * q[4]**(-1))
def v_R(q):
"""Returns ratio of Villains to total population.
Keyword arguments:
q -- population array
"""
return float(q[2] * q[4]**(-1))
def a_R(q):
"""Returns ratio of Apathetics to total population.
Keyword arguments:
q -- population array
"""
return float(q[3] * q[4]**(-1))
#choosing individuals
def crim_Choice(q):
"""Returns the index (1 or 2) for the next victimizer.
Keyword arguments:
q -- population array
"""
if i_R(q)==0 and v_R(q)==0:
return 4
else:
inf = i_R(q) / (i_R(q) + v_R(q))
vil = v_R(q) / (i_R(q) + v_R(q))
victimizer = np.random.choice(4, p=[0,inf,vil,0])
return victimizer
def pop_Choice(q):
"""Returns the index (0-3) for a random individual.
Keyword arguments:
q -- population array
"""
pal = p_R(q)
inf = i_R(q)
vil = v_R(q)
ap = a_R(q)
tot = pal + inf + vil + ap
victim = np.random.choice(4, p=[pal/tot,inf/tot,vil/tot,ap/tot])
return victim
#steps
def step_One(q):
"""Returns 2 indicies in an array for the victimizer and victim.
Keyword arguments:
q -- population array
"""
vm = crim_Choice(q)
q[vm] -= 1
q[4] -= 1
v = pop_Choice(q)
q[v] -= 1
q[4] -= 1
return np.array([vm, v])
def step_Two(vm_v):
"""Returns whether or not a crime was reported to authorities.
Keyword arguments:
vm_v -- victimizer/victim choice array
"""
if (vm_v[1]==2) or (vm_v[1]==3):
return False
else:
return True
def step_Three(q, w):
"""Returns the indicies of the witnessing subpopulation
Keyword arguments:
q -- population array
w -- number of witnesses to be selected
"""
choice = pop_Choice(q)
pop_choice = np.array([choice])
q[choice] -= 1
q[4] -= 1
for a in range(w-1):
choice = pop_Choice(q)
pop_choice = np.append(pop_choice, np.array([choice]))
q[choice] -= 1
q[4] -= 1
return pop_choice
def step_Four(witnesses):
"""Returns the number of witnesses who cooperate with investigation.
Keyword arguments:
witnesses -- array of witness indicies
"""
cooperating = 0
for x in np.nditer(witnesses):
if (x==0) or (x==1):
cooperating += 1
return cooperating
def step_Five(c, w_s):
"""Returns whether or not an investigation results in conviction.
Keyword arguments:
c -- number of cooperating witnesses
w_s -- total witness subpopulation size
"""
c_p = float(c * w_s**(-1))
c_pp = 1 - c_p
if (c_pp<0):
print(c)
print(c_p)
print("Error!!!!")
conviction = np.random.choice(2, p=[c_p, c_pp])
if conviction == 0:
return True
else:
return False
def step_Six(reported, conviction, rates):
"""Returns the associated payoffs of the victimizer and victim
Keyword arguments:
reported -- Boolean for whether or not crime was reported
conviction -- Boolean for whether or not the victimizer was convicted
rates -- the associated payoff rates for different actions
"""
payoff_vm = 1
payoff_v = 1
if reported==False:
payoff_vm += rates[0]
payoff_v -= rates[0]
else:
if conviction==False:
payoff_vm += rates[0]
payoff_v -= (rates[0] + rates[2])
else:
payoff_vm -= rates[1]
payoffs = np.array([payoff_vm, payoff_v])
return payoffs
def step_Seven(vm_v, scores):
tot = 0
for x in np.nditer(scores):
tot += x
update = np.random.choice(2, p=[float(scores[0]*tot**(-1)),float(scores[1]*tot**(-1))])
if update==0:
return vm_v[0]
else:
if vm_v[1]==0 or vm_v[1]==1:
return 0
else:
return 3
#to do
#bring it all together
for k in range(3):
# initial conditions
# population
paladin = 0.0
informant = 40.0
villain = 960.0
apathetic = 0.0
total = paladin + informant + villain + apathetic
w_s = 5 # witness subsample size
t = 0.0 # total time as given by poisson arrival process
turn = 0 # index for turns
# payoff rates for different interactions
reward = 0.5 # reward of crime
punishment = 0.5 # punishment of conviction
image = 0.1 # credibility reduction
payoff = np.array([reward, punishment, image])
# population array where majority of manipulation occurs
pop = np.array([paladin, informant, villain, apathetic , total, t, turn])
history = np.copy(pop) # data array, keeps record of each round
yada = False
while yada==False:
pop[6] += 1
print(pop[6])
pop[5] += arrival()
if ((pop[0]==0) and (pop[1]==0) or ((pop[2]==0) and (pop[3]==0))):
break
else:
working_pop = np.copy(pop)
#print(pop)
x = step_One(working_pop)
if x[0]==4:
break
#print(x)
y = step_Two(x)
#print(y)
z = step_Six(y, step_Five(step_Four(step_Three(working_pop, w_s)), w_s), payoff)
#print(z)
h = step_Seven(x, z)
#print(h)
if z[0] < z[1]:
pop[x[0]] -= 1
else:
pop[x[1]] -= 1
pop[h] += 1
history = np.vstack([history, pop])
if ((pop[0]==0) and (pop[1]==0) or ((pop[2]==0) and (pop[3]==0))):
yada==True
print(history)
title = str(k)
np.savetxt((title + "history.npy"), history, fmt='%1.3f', delimiter=' ')