-
Notifications
You must be signed in to change notification settings - Fork 2
/
tragedy.py
237 lines (182 loc) · 7.98 KB
/
tragedy.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
232
233
234
235
236
237
#play tragedy of the commons
import random
import math
#rl_model_name = 'altruistic_1_9_26'
rl_model_name = 'greed_1_9_26'
class team:
def __init__(self,type=None):
self.payoff = 0
self.type = type
def get_action(self, state =None):
print('hi')
def player_type(type=None, fishery_status=None):
dice_roll = random.randint(1,20)
if type == 'greedy':
if fishery_status > 18:
fish_count = random.randint(5,8)
elif fishery_status > 15:
fish_count = random.randint(2,6)
elif fishery_status > 12:
fish_count = random.randint(5,20)
elif type == 'con':
if fishery_status > 18:
fish_count = random.randint(3,5)
elif fishery_status > 15:
fish_count = random.randint(1,4)
elif fishery_status > 12:
fish_count = random.randint(1,3)
return fish_count
class TOC:
def __init__(self, num_learning_rounds =None, learner = None, report_every=1000):
self._num_learning_rounds = num_learning_rounds
self._report_every = report_every
self.player = learner
self.win = 0
self.loss = 0
self.tie = 0
self.game = 1
self.evaluation = False
self.turn_record = []
def play_game(self):
p1,p2,initial_fishery,fishery_count = self.reset_game()
#opp last played, player last played, opp_score,player_score,turn#, current_fishery_count,
state = [0,0,0,0,20]
turn_count = 0
while True:
p1_fish_count = p1.get_action(state)
p2_fish_count = player_type(type = p2.type,fishery_status = fishery_count)
total_harvest = p1_fish_count + p2_fish_count
fishery_count -= total_harvest
turn_count +=1
if fishery_count <= 8 or turn_count >= 100:
#print('')
perated_fish = (fishery_count + total_harvest) / float(total_harvest)
#fish_to_players = int(fishery_count / 2)
p1.payoff += int(p1_fish_count * perated_fish)
p2.payoff += int(p2_fish_count * perated_fish)
if self.evaluation == True:
print('game ends... total harvest:',total_harvest, 'new_fishery_count: ',0,
'p1 chose: ',p1_fish_count, 'p2 chose: ', p2_fish_count,
'player1 final score: ',p1.payoff, 'comp final score: ',p2.payoff)
state1 = [p2_fish_count,p1_fish_count,p2.payoff,p1.payoff,0]
'''
winning and losing in long games is fine short term has worse consequences
'''
#winner calculation
if p1.payoff > p2.payoff:
p1.update(state1, math.sqrt(turn_count))
self.win +=1
if self.evaluation == True:
print('player 1 wins')
elif p1.payoff < p2.payoff:
p1.update(state1, -1)
self.loss +=1
if self.evaluation == True:
print('player 2 wins')
else:
p1.update(state1,1)
self.tie +=1
if self.evaluation == True:
print('tie')
#winner(p1,p2,state1,self.evaluation)
break
else:
fishery_count_holder= fishery_count
p1.payoff += p1_fish_count
p2.payoff += p2_fish_count
fishery_count = fishery_count*2
if fishery_count > initial_fishery:
fishery_count = initial_fishery
state1 = [p2_fish_count,p1_fish_count,p2.payoff,p1.payoff,fishery_count]
#print(state1)
#p1.update(state1,p1.payoff-p2.payoff)
#p1.update(state1,math.sqrt(turn_count))
p1.update(state1,10)
if self.evaluation == True:
print('total harvest:',total_harvest,'fishery_count: ',fishery_count_holder,'new_fishery_count: ',fishery_count,
'player1 score: ',p1.payoff, 'comp_score: ',p2.payoff)
if self.evaluation == False:
self.game += 1
self.turn_record.append(turn_count)
self.report()
if self.game == self._num_learning_rounds:
print("Turning off learning!")
self.player._learning = False
self.win = 0
self.loss = 0
def reset_game(self):
initial_fishery = 20
#fishery_count = initial_fishery
p1 = self.player
#print(self.player)
p1.payoff = 0
#dice_roll = random.randint(1,3)
#if dice_roll == 1:
# p1 = team('greedy')
#elif dice_roll == 2:
# p1 = team('con')
#elif dice_roll == 3:
# p1 = team('dice')
dice_roll = random.randint(1,2)
if dice_roll == 1:
p2 = team('greedy')
elif dice_roll == 2:
p2 = team('con')
#print(p1.type,p2.type)
return p1,p2,initial_fishery,initial_fishery
def report(self):
#turned off for plotting 9/18
if self.game % self._num_learning_rounds == 0:
avg_turn_count = sum(self.turn_record) / float(len(self.turn_record))
self.turn_record = []
print('##############################################')
print('# Final Score #')
print('##############################################')
print('')
print(str(self.game) +"," +str(self.win / (self.win + self.loss)),' ties: ', self.tie)
print('')
print('average game length: ',avg_turn_count)
print('##############################################')
#turning off this section for testing
self.evaluation = True
self.player._epsilon = 1.0
print('#################### G1 ######################')
self.play_game()
print('#################### G2 ######################')
self.play_game()
print('#################### G3 ######################')
self.play_game()
self.player._epsilon = .9
self.evaluation = False
self.win = 0
self.loss = 0
self.tie = 0
self.player.save_rl_model('models/{}_iteration_{}'.format(rl_model_name,self.game))
elif self.game % self._report_every == 0:
avg_turn_count = sum(self.turn_record) / float(len(self.turn_record))
self.turn_record = []
print('##############################################')
print('# Updated Score #')
print('##############################################')
print('')
print(str(self.game) +"," +str(self.win / (self.win + self.loss)),' ties: ', self.tie)
print('')
print('average game length: ',avg_turn_count)
print('##############################################')
#turning off this section for testing
self.evaluation = True
self.player._epsilon = 1.0
print('#################### G1 ######################')
self.play_game()
print('#################### G2 ######################')
self.play_game()
print('#################### G3 ######################')
self.play_game()
self.player._epsilon = .9
self.evaluation = False
self.win = 0
self.loss = 0
self.tie = 0
self.player.save_rl_model('models/{}_iteration_{}'.format(rl_model_name,self.game))
#game1 = Tragedy()
#game1.play_game()