-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign1_q2.py
182 lines (173 loc) · 5.9 KB
/
assign1_q2.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
#!/usr/bin/python
import sys
# import time
# start_time = time.time()
f = open(sys.argv[1], "r")
gameinfo = f.readline()
data = f.readline().split(" ")
data = data[data.index("{") + 1: data.index("}\n")]
data = data[data.index("{") + 1:]
num_players = len(data)
strategies = list(map(int, data))
multiplier = []
temp = 1
count = 0
equilibria = []
for i in range(len(strategies)):
multiplier.append(temp)
temp = temp * strategies[i]
f.readline()
data = f.readline().split(" ")
# print data
gamedata = list(map(int, data))
def sel_index(player, args):
result = 0
for idx, arg in enumerate(args):
result = result + (arg * multiplier[idx])
result = result * num_players
result += player
return result
def find_strongly_dominant_eq(playerno, totalplayer, topplayer, strategyarr = [], eqindex = -1):
if(len(totalplayer) >= 1):
cur_player = totalplayer[0]
totalplayer = totalplayer[1:]
temp = 0
for strategy in range(strategies[cur_player]):
# print "First eqindex ", eqindex
temparray = strategyarr[:]
# print "Tempar ", temparray
temparray.append(strategy)
temp = find_strongly_dominant_eq(playerno, totalplayer, topplayer, temparray, eqindex)
# print "Temp value ", temp
if(temp == -sys.maxsize): return temp
else: eqindex = temp
return temp
else:
# print "Eqindex ", eqindex
max_payoff = -sys.maxsize
max_index = -1
other_payoffs = []
other_index = []
for strategy in range(strategies[playerno]):
temp1 = strategyarr[:]
# print "T1 ", temp1
temp1.insert(playerno, strategy)
cur_payoff = gamedata[sel_index(playerno, temp1)]
if( max_payoff < cur_payoff):
max_payoff = cur_payoff
max_index = strategy
else:
other_payoffs.append(cur_payoff)
other_index = strategy
if(max_payoff in other_payoffs):
return -sys.maxsize
if( eqindex == -1 ):
eqindex = max_index
elif( eqindex != max_index ):
return -sys.maxsize
return eqindex
def find_weakly_dominant_eq(playerno, totalplayer, topplayer, eqindex, strategyarr = []):
if(len(totalplayer) >= 1):
cur_player = totalplayer[0]
temp = 0
totalplayer = totalplayer[1:]
for strategy in range(strategies[cur_player]):
# print "First eqindex ", eqindex
temparray = strategyarr[:]
# print "Tempar ", temparray
temparray.append(strategy)
temp, eqindex = find_weakly_dominant_eq(playerno, totalplayer, topplayer, eqindex, temparray)
# print "Temp value ", temp
if( temp == -sys.maxsize):
return temp, eqindex
return temp, eqindex
else:
# print "Eqindex ", eqindex
max_payoff = -sys.maxsize
other_payoffs = []
max_index = []
for strategy in range(strategies[playerno]):
temp1 = strategyarr[:]
# print "T1 ", temp1
temp1.insert(playerno, strategy)
cur_payoff = gamedata[sel_index(playerno, temp1)]
if( max_payoff < cur_payoff):
max_payoff = cur_payoff
max_index = []
max_index.append(strategy)
elif max_payoff == cur_payoff:
max_index.append(strategy)
if eqindex[0] == -1:
eqindex = max_index
else:
temp_index = list(set(eqindex) & set(max_index))
eqindex = temp_index[:]
if not eqindex:
return -sys.maxsize, eqindex
else:
return eqindex[0], eqindex
def print_weak(indexes, valueindexes = [], start = 0):
global count
if start < num_players - 1:
for i in indexes[start]:
tempvalues = valueindexes[:]
tempvalues.append(i)
print_weak(indexes, tempvalues, start + 1)
else:
for i in indexes[start]:
tempresult = valueindexes[:]
tempresult.append(i)
count=count+1
# print(tempresult)
equilibria.append(tempresult)
# for v in tempresult:
# print(v, end=" ")
# print()
# print find_strongly_dominant_eq(0, [1, 2], 1)
playerslist = list(range(num_players))
return_value = -1
strong_eq = []
for i in range(num_players):
tempplayerlist = playerslist[:]
tempplayerlist.remove(i)
# print i, tempplayerlist, tempplayerlist[0]
value = find_strongly_dominant_eq(i, tempplayerlist, tempplayerlist[0])
if value == -sys.maxsize:
# print "No Strongly Dominant Strategy equilibrium exists"
return_value = 0
break
else:
strong_eq.append(value)
if return_value == -1:
# print "Strongly dominant strategy equilibrium (in order of P1, P2, ... ,Pn) is:",
count=count+1
# print(strong_eq)
equilibria.append(strong_eq)
# for i in strong_eq:
# print(i, end=" ")
# print()
else:
min_eq_list = []
for i in range(num_players):
tempplayerlist = playerslist[:]
tempplayerlist.remove(i)
result_index = [-1]
value, result_index = find_weakly_dominant_eq(i, tempplayerlist, tempplayerlist[0], result_index)
if value == -sys.maxsize or len(result_index) == strategies[i]:
print("No Dominant Strategy Equilibria exist")
return_value = -2
break
else:
min_eq_list.append(result_index)
if return_value != -2:
# print "Weakly dominant strategy equilibrium(s) is (are): "
# print min_eq_list
print_weak(min_eq_list)
if (count):
print(count)
for equilibrium in equilibria:
for i in equilibrium:
print(i, end=" ")
print()
# print()
# print("--- %s ---" % (time.time() - start_time))