Skip to content

Instantly share code, notes, and snippets.

@Makazone
Last active October 22, 2015 21:36
Show Gist options
  • Save Makazone/f512799401ce52854b19 to your computer and use it in GitHub Desktop.
Save Makazone/f512799401ce52854b19 to your computer and use it in GitHub Desktop.
import itertools
import sys
from operator import itemgetter
def getScore(p1, p2, el):
if p1.count(el) > p2.count(el):
return 1
elif p1.count(el) < p2.count(el):
return -1
else:
return 0
def getExpectedValue(oponentsProbDistribution, playerStrategy, oponentStrategies):
value = 0.0
for (p, s) in zip(oponentsProbDistribution, oponentStrategies):
value += payoff(playerStrategy, s) * p
return value
def payoff(p1, p2):
score = 0
for i in xrange(0, len(p1)):
if p1[i] > p2[i]:
score += 1
elif p1[i] < p2[i]:
score -= 1
return score
def maxmin(player, oponent):
mins = []
# For each strategy find the minimum payoff it could have
for playerStrategy in player:
currentMin = 2
for oponentStrategy in oponent:
currentMin = min(currentMin, payoff(playerStrategy, oponentStrategy))
mins.append(currentMin)
# Get the maximum over all minimum payoffs
return max(mins)
def minmax(player, oponent):
maxs = []
# For each strategy find the maximum payoff it could have
for oponentStrategy in oponent:
currentMax = -2
for playerStrategy in player:
currentMax = max(currentMax, payoff(playerStrategy, oponentStrategy))
maxs.append(currentMax)
# Get the minimum over all maximum payoffs
return min(maxs)
def removeDumbStrategies(player, oponent):
for playerStrategy1 in player:
for playerStrategy2 in player:
if playerStrategy1 == playerStrategy2:
continue
dominates = True # playerStrategy1 dominates playerStrategy2
for oponentStrategy in oponent:
if payoff(playerStrategy1, oponentStrategy) < payoff(playerStrategy2, oponentStrategy):
dominates = False
break
if dominates:
player.remove(playerStrategy2)
# print playerA
# print playerB
#
# print 'Maxmin = ' + str(maxmin(playerA, playerB))
# print 'Minmax = ' + str(minmax(playerA, playerB))
def f(x):
a = x.count(1)
b = x.count(2)
c = x.count(3)
return (a, b, c)
def getPlayerUniqueStrategies():
lines = [1, 2, 3]
lineDistributions = [item for item in itertools.product(lines, repeat=5)]
lineCount = map(f, lineDistributions)
unique = []
[unique.append(item) for item in lineCount if item not in unique]
return unique
playerA = getPlayerUniqueStrategies()
playerB = list(playerA)
print playerA
print playerB
# print len(playerA)
i = 0; j = 0
A = [[0 for x in range(len(playerA))] for x in range(len(playerB))]
for p1 in playerA:
for p2 in playerB:
A[i][j] = payoff(p1, p2)
j += 1
j = 0; i += 1
# print('\n'.join([''.join(['{:5}'.format(item) for item in row]) for row in A]))
print 'Maxmin = ' + str(maxmin(playerA, playerB))
print 'Minmax = ' + str(minmax(playerA, playerB))
removeDumbStrategies(playerA, playerB)
removeDumbStrategies(playerB, playerA)
print len(playerA)
print len(playerB)
# playerA.pop()
# removeDumbStrategies(playerA, playerB)
# removeDumbStrategies(playerB, playerA)
print len(playerA)
print len(playerB)
#
i = 0; j = 0
A = [[0 for x in range(len(playerA))] for x in range(len(playerB))]
for p1 in playerA:
for p2 in playerB:
A[i][j] = payoff(p1, p2)
j += 1
j = 0; i += 1
print('\n'.join([''.join(['{:5}'.format(item) for item in row]) for row in A]))
for (p, i) in zip(playerA, range(0, len(playerA))):
print 'Expected value for playerA ' + str(i) + ' strategy given p = 1/n'
print getExpectedValue([1.0/len(playerB) for i in xrange(0, len(playerB))], p, playerB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment