-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
448 lines (385 loc) · 18 KB
/
agent.py
File metadata and controls
448 lines (385 loc) · 18 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# a bayesian agent that runs calculations based on distribution
from dist import Gaussian, Geometric, Uniform, Limit
from balloons import *
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import random
# import seaborn as sns
# sns.set()
class Agent():
def __init__(self, balloons, horizon, decay):
#first assuming everything is gaussian
self.max = 10
self.decay = decay
self.min = 0
hyp_mean = [i for i in range(0, self.max)]
# hyp_mean = [1, 2, 3, 4, 5]
hyp_std = [i for i in range(1, 3+1)]
hyp_prob = [i * 1.0 / 10 for i in range(1,6)]
hyp_lim = [i for i in range(1, 10)]
hyp_int = [(umin, umax) for umin in range(1, 9) for umax in range(umin+2, 11)]
p = 1.0/(len(hyp_mean) * len(hyp_std))
self.balloons = balloons
self.N = len(balloons)
self.observed = [[-1, -1] for i in range(self.N)]
self.hypothesis = {(m, std): [p, Gaussian(N=self.N, mean=m, std=std)] for m in hyp_mean for std in hyp_std}
self.hypothesis.update({(prob): [p, Geometric(N=self.N, p=prob)] for prob in hyp_prob})
self.hypothesis.update({(l): [p, Limit(N=self.N, limit=l)] for l in hyp_lim})
self.hypothesis.update({(umin, umax): [p, Uniform(N=self.N, umin=umin, umax=umax)] for (umin, umax) in hyp_int})
#ML could specify the hypothesis space / relative weighing of things through hierarchical learning but we assume uniform for now
self.infiniteHorizon = False
self.horizon = horizon
self.memo = {}
self.utilitymemo = {}
self.mostlikely = None
self.maxprob = 0
self.lastSwitch = 0
self.actions = []
def total_points(self):
points = 0
for i in self.balloons:
points += i - 1
return points
def convert_to_key(self, index, size, observation):
# print("index {} size {} observation {}".format(index, size, observation))
obsstring = ""
for i in range(len(observation)):
if(observation[i][0] != -1):
obsstring += "{}{}".format(observation[i][0], observation[i][1])
# print(obsstring)
return (index, size, obsstring)
def check_memo(self, index, size, observation):
key = self.convert_to_key(index, size, observation)
if(key in self.memo):
return self.memo[key]
return None
def set_memo(self, index, size, observation, value):
key = self.convert_to_key(index, size, observation)
if(key in self.memo):
raise Exception("huh why are we setting this again :0")
self.memo[key] = value
def pop_prob(self, index, size, observation):
# print("POP PROB OBVSERVATION {}".format(observation))
for i in range(index):
if(observation[i][0] == -1):
raise Exception("not all balloons before {} was seen, {} is empty".format(index, i))
if(observation[i][1] != True and observation[i][1] != False):
raise Exception("balloon wasn't popped or passed, what")
will_pop = 0.0
p_obv = 0
val = self.check_memo(index, size, observation)
if(val != None):
return val
else:
if(size >= self.max - 1):
#balloon guareenteed to pop at 10: potential experiment is giving vs not giving ppl this information
return 1
maxkey = None
maxprob = 0
maxtype = None
# print("index {} size {} obv {}".format(index, size, observation))
for key in self.hypothesis:
p = self.hypothesis[key][0]
dist = self.hypothesis[key][1]
#current balloon observation
p_prev = dist.cdf(size, self.max)
for i in range(index):
obs = observation
item = obs[i]
bal_size = item[0]
popped = item[1]
if(popped):
p_prev *= dist.cdf(bal_size -1, bal_size)
else:
p_prev *= dist.cdf(bal_size, self.max)
# print("p_prev {}".format(p_prev))
p_obv += p_prev
#P(will pop | haven't popped yet) = p(haven't popped | will pop) * p(will pop) / (p(haven't popped | will pop) + p(haven't popped | won't pop))
p_not_popped = dist.cdf(size, self.max)
if(p_not_popped == 0):
raise Exception("denominator is zero for cdf size, self.max")
# print("index {} size {} obv {}".format(index, size, observation))
else:
P_will_pop = 1.0 * dist.cdf(size, size+1) / dist.cdf(size, self.max)
p_dist = p_prev #*p
if(p_dist > maxprob):
maxkey = key
maxtype = dist.type
maxprob = p_dist
self.lastSwitch = [index, size, observation]
# print("will pop {} p_dist {}".format(P_will_pop, p_dist) )
# print("DIST {} {} {}".format(dist.mean, dist.std, p_dist))
# print()
will_pop += P_will_pop * p_dist
if(p_obv == 0):
raise Exception("denominator is zero for p_obv")
# print("index {} size {} obv {}".format(index, size, observation))
will_pop = will_pop / p_obv
self.mostlikely = maxkey
self.maxprob = maxprob / p_obv
# print("MOST LIKELY DIST: {} {} prob {}".format(maxtype, maxkey, maxprob / p_obv))
# print("WILLPOP: {}".format(will_pop))
self.set_memo(index, size, observation, will_pop)
return will_pop
def utility_key(self, index, size, score, infiniteHorizon, horizon, obs):
obsstring = ""
for i in range(len(obs)):
if(obs[i][0] != -1):
obsstring += "{}{}".format(obs[i][0], obs[i][1])
return (index, size, score, infiniteHorizon, horizon, obsstring)
def check_utility_memo(self, index, size, score, infiniteHorizon, horizon, obs):
key = self.utility_key(index, size, score, infiniteHorizon, horizon, obs)
if(key in self.utilitymemo):
return self.utilitymemo[key]
return None
def set_utility_memo(self, index, size, score, infiniteHorizon, horizon, obs, value):
key = self.utility_key(index, size, score, infiniteHorizon, horizon, obs)
if(key in self.utilitymemo):
raise Exception("huh why are we setting this again :0")
self.utilitymemo[key] = value
def pump(self, index, size, score):
expected_utility = self.getExpectedUtility(index, size, score, self.infiniteHorizon, self.horizon, self.observed.copy())
# if(index < 3):
# expected_utility = self.getExpectedUtility(index, size, score, self.infiniteHorizon, 2, self.observed.copy())
# else:
# expected_utility = self.getExpectedUtility(index, size, score, self.infiniteHorizon, 2, self.observed.copy())
# print("expected gain in utility {}".format(expected_utility))
if(expected_utility[0] == "PUMP"):
return True
else:
return False
def balloonpops(self, index, size):
if(self.balloons[index] > size):
return False
return True
def play(self):
size = 0
index = 0
points = 0
points_from_this_balloon = 0
while(True):
# print("index {} size {}".format(index, size))
if(index >= self.N):
print("GAME ENDED")
break
#playing the same balloon
if(not self.balloonpops(index, size)):
if(size >= self.max):
# print("REACHED MAX SIZE")
# points += points_from_this_balloon
points_from_this_balloon = 0
#TODO: There might be a bug from the dist where all balloons pop at 10
index += 1
size = 0
elif(self.pump(index, size, points_from_this_balloon)):
# print("PUMPED!")
self.actions.append([index, size, "PUMP", 0, points])
size += 1
points_from_this_balloon += 1
else:
# print("PASSED")
#passed :(
points += points_from_this_balloon
points_from_this_balloon = 0
self.observed[index] = [size, False]
self.actions.append([index, size, "PASS", 0, points])
index +=1
size = 0
else:
# print("POPPED")
if(size == 0):
print("index {} size {}".format(index, size))
self.observed[index] = [size, True]
self.actions.append([index, size, "POP", 0, points])
index += 1
size = 0
points_from_this_balloon = 0
print("POINTS: {}".format(points))
return points
def getExpectedUtility(self, index, size, score, infiniteHorizon, horizon, obs):
#index: ranges from 0 to self.N - 1
#size ranges from 0 to self.max
#score ranges from 0 to self.max
#infiniteHorizon False
#horizon ranges from 0 to self.horizon
#obs has self.N values, each has self.max values, and True or False (popped or not)
# print("index {} size {} horizon {}".format(index, size, horizon))
if(index < 0):
raise Exception("index must be positive")
if(score != size):
raise Exception("score must be equal to size")
for i in range(index):
if(obs[i][0] == -1):
raise Exception("not all balloons before {} was seen, {} is empty".format(index, i))
if(obs[i][1] != True and obs[i][1] != False):
raise Exception("balloon wasn't popped or passed, what")
val = self.check_utility_memo(index, size, score, infiniteHorizon, horizon, obs)
if(val != None):
return val
else:
if(infiniteHorizon):
horizon = self.max * self.N
if(horizon == 0):
p = random.randint(0, 1)
if p==1:
return ("PUMP", 0)
if p==0:
return ("PASS", 0)
elif(index >= self.N):
return ("PASS", 0)
elif(size == self.max - 1):
return ("PASS", 0)
elif(horizon == 1):
# print("HORIZON = 1")
will_pop = self.pop_prob(index, size, obs)
EU_PUMP = 1 * (1-will_pop) + (will_pop) * -1 * score
if(EU_PUMP > 0):
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PUMP", EU_PUMP))
return ("PUMP", EU_PUMP)
else:
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PASS", 0))
return ("PASS", 0)
else:
# print("HORIZON = {}".format(horizon))
will_pop = self.pop_prob(index, size, obs)
hyp_pump_not_pop = obs.copy()
hyp_pump = obs.copy()
hyp_pump[index] = [size + 1, True]
decay = self.decay
score_if_not = 1 + decay * self.getExpectedUtility(index, size+1, score+1, infiniteHorizon, horizon - 1, hyp_pump_not_pop)[1]
# print("score pump not pop{}".format(score_if_not))
score_if_pop = -score + decay * self.getExpectedUtility(index + 1, 0, 0, infiniteHorizon, horizon - 1, hyp_pump)[1]
# print("score pump pop{}".format(score_if_pop))
score_pump = will_pop * score_if_pop + (1-will_pop) * score_if_not
# print("score pump {}".format(score_pump))
hyp_pass = obs.copy()
hyp_pass[index] = [size, False]
score_pass = decay * self.getExpectedUtility(index + 1, 0, 0, infiniteHorizon, horizon - 1, hyp_pass)[1]
# print("score_pass {}".format(score_pass))
# print()
if(score_pass > score_pump):
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PASS", max(score_pump, score_pass)))
return ("PASS", max(score_pump, score_pass))
else:
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PUMP", max(score_pump, score_pass)))
return ("PUMP", max(score_pump, score_pass))
def pointPerBalloon(self):
points = [-1] * self.N
for a in self.actions:
for i in range(self.N):
if(a[0] == i):
points[i] = a[-1]
self.points = points
return points
class LossAverseAgent(Agent):
def __init__(self, balloons, horizon, decay):
super().__init__(balloons, horizon, decay)
class NotLossAverseAgent(Agent):
def __init__(self, balloons, horizon, decay):
super().__init__(balloons, horizon, decay)
def getExpectedUtility(self, index, size, score, infiniteHorizon, horizon, obs):
#index: ranges from 0 to self.N - 1
#size ranges from 0 to self.max
#score ranges from 0 to self.max
#infiniteHorizon False
#horizon ranges from 0 to self.horizon
#obs has self.N values, each has self.max values, and True or False (popped or not)
# print("index {} size {} horizon {}".format(index, size, horizon))
if(index < 0):
raise Exception("index must be positive")
if(score != size):
raise Exception("score must be equal to size")
for i in range(index):
if(obs[i][0] == -1):
raise Exception("not all balloons before {} was seen, {} is empty".format(index, i))
if(obs[i][1] != True and obs[i][1] != False):
raise Exception("balloon wasn't popped or passed, what")
val = self.check_utility_memo(index, size, score, infiniteHorizon, horizon, obs)
if(val != None):
return val
else:
if(infiniteHorizon):
horizon = self.max * self.N
if(horizon == 0):
p = random.randint(0, 1)
if p==1:
return ("PUMP", 0)
if p==0:
return ("PASS", 0)
elif(index >= self.N):
return ("PASS", 0)
elif(size == self.max - 1):
return ("PASS", 0)
elif(horizon == 1):
will_pop = self.pop_prob(index, size, obs)
EU_PUMP = 0 * (1-will_pop) + (will_pop) * -1 * score
EU_PASS = size
if(EU_PUMP >= EU_PASS):
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PUMP", EU_PUMP))
return ("PUMP", EU_PUMP)
else:
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PASS", 0))
return ("PASS", 0)
else:
# print("HORIZON = {}".format(horizon))
will_pop = self.pop_prob(index, size, obs)
hyp_pump_not_pop = obs.copy()
hyp_pump = obs.copy()
hyp_pump[index] = [size + 1, True]
decay = self.decay
score_if_not = 1 + decay * self.getExpectedUtility(index, size+1, score, infiniteHorizon, horizon - 1, hyp_pump_not_pop)[1]
# print("score pump not pop{}".format(score_if_not))
score_if_pop = -size + decay * self.getExpectedUtility(index + 1, 0, 0, infiniteHorizon, horizon - 1, hyp_pump)[1]
# print("score pump pop{}".format(score_if_pop))
score_pump = will_pop * score_if_pop + (1-will_pop) * score_if_not
# print("score pump {}".format(score_pump))
hyp_pass = obs.copy()
hyp_pass[index] = [size, False]
score_pass = decay * self.getExpectedUtility(index + 1, 0, score, infiniteHorizon, horizon - 1, hyp_pass)[1]
# print("score_pass {}".format(score_pass))
# print()
if(score_pass > score_pump):
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PASS", max(score_pump, score_pass)))
return ("PASS", max(score_pump, score_pass))
else:
self.set_utility_memo(index, size, score, infiniteHorizon, horizon, obs, ("PUMP", max(score_pump, score_pass)))
return ("PUMP", max(score_pump, score_pass))
# # number of experiments
# K = 30
# #number of balloons
# N = 20
# obs = []
# dists = []
# decay = 0.5
# HORIZON = 8
# for i in range(K):
# b = GaussianBalloons(N=N)
# dists.append(b)
# obs.append(b.getBallons())
# # horizon = np.array([math.floor(i / K) for i in range(K*HORIZON)])
# horizon = [[] for i in range(K)]
# points = [[] for i in range(K)]
# # points = np.array([-1 for i in range(HORIZON*K)])
# for i in range(HORIZON):
# for j in range(K):
# o = obs[j]
# a = Agent(o, i, decay)
# p = a.play()
# horizon[j].append(i)
# if(i==2):
# if(points[2] > points[1]):
# print("SECOND HORIZON BETTER THAN FIRST")
# print(dists[j])
# points[j].append(p)
# # points[i*K + j] = p
# # plt.plot(horizon, points)
# for i in range(len(obs)):
# plt.plot(horizon[i], points[i])
# plt.xlabel('horizon')
# plt.ylabel('points')
# plt.title('points gained over horizons')
# plt.savefig('graphs/Gaussian/N={}decay={}K={}.pdf'.format(N, decay, K))
# # GAUSSIAN mean 7 std 1
# # BALLOONS: [5,6,5,7,5,6,7,5,6,7]
# # probs: [0.0000, 0.0000, 0.0000, 0.0013, 0.0212, 0.1352, 0.3410, 0.3426, 0.1370, 0.0217]