pep8 and drop pymc/numpy requirement

This commit is contained in:
Barak Michener 2013-07-30 14:27:32 -04:00
parent 9eb3625393
commit d4a6aa3925
2 changed files with 347 additions and 336 deletions

View file

@ -1,6 +1,4 @@
argparse==1.2.1 argparse==1.2.1
clint==0.3.1 clint==0.3.1
numpy==1.7.1
progressbar==2.3 progressbar==2.3
pymc==2.2
wsgiref==0.1.2 wsgiref==0.1.2

119
tim.py
View file

@ -1,43 +1,54 @@
import pymc as mc
import functools
import itertools import itertools
from collections import defaultdict as dd from collections import defaultdict as dd
import pprint import pprint
import progressbar import progressbar
import readline import readline
import random
import sys import sys
from clint.textui import colored, puts from clint.textui import colored, puts
def ResistanceGame(n_players): def ResistanceGame(n_players):
full_set = [("G1", True), ("G2", True), ("G3", True), ("E1", False), ("E2", False), full_set = [("G1", True), ("G2", True), ("G3", True),
("G4", True), ("E3", False), ("G5", True), ("G6", True), ("E4", False)] ("E1", False), ("E2", False), ("G4", True),
("E3", False), ("G5", True), ("G6", True), ("E4", False)]
return full_set[:n_players] return full_set[:n_players]
class Bernoulli(object):
def __init__(self, percentage):
self.percentage = percentage
def rand(self):
return random.random() < self.percentage
def random(self):
return self.rand()
class DeceptionGame(object): class DeceptionGame(object):
def __init__(self, player_array): def __init__(self, player_array):
self.player_array = player_array self.player_array = player_array
self.all_permutations = list(itertools.permutations(player_array)) self.all_permutations = list(itertools.permutations(player_array))
self.n_players = len(player_array) self.n_players = len(player_array)
self.n_good = len([x for x in player_array if x[1] == True]) self.n_good = len([x for x in player_array if x[1] is True])
self.trace = None self.trace = None
self.observations = [] self.observations = []
self.seen = [] self.seen = []
self.tid = 0 self.tid = 0
self.lady_will_duck = mc.Bernoulli("lady_will_duck", 0.5) self.lady_will_duck = Bernoulli(0.5)
self.mission_ducks_on_round = [None] * 5 self.mission_ducks_on_round = [None] * 5
self.mission_ducks_on_round[0] = mc.Bernoulli("missionduck_on_0", 0.5) self.mission_ducks_on_round[0] = Bernoulli(0.5)
self.mission_ducks_on_round[1] = mc.Bernoulli("missionduck_on_1", 0.5) self.mission_ducks_on_round[1] = Bernoulli(0.5)
self.mission_ducks_on_round[2] = mc.Bernoulli("missionduck_on_2", 0.5) self.mission_ducks_on_round[2] = Bernoulli(0.5)
self.mission_ducks_on_round[3] = mc.Bernoulli("missionduck_on_3", 0.0) self.mission_ducks_on_round[3] = Bernoulli(0.0)
self.mission_ducks_on_round[4] = mc.Bernoulli("missionduck_on_4", 0.0) self.mission_ducks_on_round[4] = Bernoulli(0.0)
self.ignorance_on_round = [None] * 5 self.ignorance_on_round = [None] * 5
self.ignorance_on_round[0] = mc.Bernoulli("ignorance_on_0", 0.9) self.ignorance_on_round[0] = Bernoulli(0.9)
self.ignorance_on_round[1] = mc.Bernoulli("ignorance_on_1", 0.7) self.ignorance_on_round[1] = Bernoulli(0.7)
self.ignorance_on_round[2] = mc.Bernoulli("ignorance_on_2", 0.5) self.ignorance_on_round[2] = Bernoulli(0.5)
self.ignorance_on_round[3] = mc.Bernoulli("ignorance_on_3", 0.3) self.ignorance_on_round[3] = Bernoulli(0.3)
self.ignorance_on_round[4] = mc.Bernoulli("ignorance_on_4", 0.3) self.ignorance_on_round[4] = Bernoulli(0.3)
def player_is_good(self, deal, player): def player_is_good(self, deal, player):
return deal[player][1] return deal[player][1]
@ -47,6 +58,7 @@ class DeceptionGame(object):
def add_known_alliance(self, player_id, is_good): def add_known_alliance(self, player_id, is_good):
transaction = [] transaction = []
def obs(deal): def obs(deal):
if self.player_is_good(deal, player_id) == is_good: if self.player_is_good(deal, player_id) == is_good:
return True return True
@ -54,11 +66,13 @@ class DeceptionGame(object):
return None return None
transaction.append(obs) transaction.append(obs)
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append(["Known role ", player_id, "Good" if is_good else "Evil"]) self.seen.append(
["Known role ", player_id, "Good" if is_good else "Evil"])
self.tid += 1 self.tid += 1
def add_known_role(self, player_id, role_str): def add_known_role(self, player_id, role_str):
transaction = [] transaction = []
def obs(deal): def obs(deal):
if self.player_is_role(deal, player_id) == role_str: if self.player_is_role(deal, player_id) == role_str:
return True return True
@ -71,6 +85,7 @@ class DeceptionGame(object):
def player_sees_player_and_claims(self, p1, p2, claim): def player_sees_player_and_claims(self, p1, p2, claim):
transaction = [] transaction = []
def obs(deal): def obs(deal):
if self.player_is_good(deal, p1): if self.player_is_good(deal, p1):
if self.player_is_good(deal, p2) == claim: if self.player_is_good(deal, p2) == claim:
@ -90,14 +105,17 @@ class DeceptionGame(object):
return True return True
transaction.append(obs) transaction.append(obs)
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append(["Lady:", p1, "says", p2 , "is", "Good" if claim else "Evil"]) self.seen.append(
["Lady:", p1, "says", p2, "is", "Good" if claim else "Evil"])
self.tid += 1 self.tid += 1
def do_mission(self, team, fails, must_fail, r): def do_mission(self, team, fails, must_fail, r):
transaction = [] transaction = []
rnd = r - 1 rnd = r - 1
def obs(deal): def obs(deal):
n_actually_good_people = sum([int(self.player_is_good(deal, x)) for x in team]) n_actually_good_people = sum(
[int(self.player_is_good(deal, x)) for x in team])
n_spies = len(team) - n_actually_good_people n_spies = len(team) - n_actually_good_people
if n_spies == 0: if n_spies == 0:
if fails != 0: if fails != 0:
@ -119,14 +137,17 @@ class DeceptionGame(object):
transaction.append(obs) transaction.append(obs)
self.observations.append(transaction) self.observations.append(transaction)
self.seen.append(["Mission:"] + team + ["with %d fails on round %d" % (fails, r)]) self.seen.append(
["Mission:"] + team + ["with %d fails on round %d" % (fails, r)])
self.tid += 1 self.tid += 1
def do_vote(self, team, votes, r): def do_vote(self, team, votes, r):
transaction = [] transaction = []
rnd = r - 1 rnd = r - 1
def obs(deal): def obs(deal):
n_actually_good_people = sum([int(self.player_is_good(deal, x)) for x in team]) n_actually_good_people = sum(
[int(self.player_is_good(deal, x)) for x in team])
n_spies = len(team) - n_actually_good_people n_spies = len(team) - n_actually_good_people
could_happen = True could_happen = True
for player, vote in enumerate(votes): for player, vote in enumerate(votes):
@ -167,12 +188,15 @@ class DeceptionGame(object):
self.seen.append(["Vote:"] + team + ["voted %s round %d" % (votes, r)]) self.seen.append(["Vote:"] + team + ["voted %s round %d" % (votes, r)])
self.tid += 1 self.tid += 1
def eval(self, length=10): def eval(self, length=10):
random.seed()
deck = self.all_permutations[:] deck = self.all_permutations[:]
new_deck = [] new_deck = []
trace = [] trace = []
progress = progressbar.ProgressBar(widgets=["Simulating games: ", progressbar.Bar(marker="*"), " ", progressbar.ETA()]) progress = progressbar.ProgressBar(
widgets=["Simulating games: ",
progressbar.Bar(marker="*"),
" ", progressbar.ETA()])
for i in progress(range(length)): for i in progress(range(length)):
for deal in deck: for deal in deck:
f_list = [] f_list = []
@ -214,7 +238,10 @@ class DeceptionGame(object):
out[i]["role"] = dd(float) out[i]["role"] = dd(float)
out[i]["side"] = dd(float) out[i]["side"] = dd(float)
progress = progressbar.ProgressBar(widgets=["Reticulating splines: ", progressbar.Bar(marker="*"), " ", progressbar.ETA()]) progress = progressbar.ProgressBar(
widgets=["Reticulating splines: ",
progressbar.Bar(marker="*"),
" ", progressbar.ETA()])
size = len(self.trace) * 1.0 size = len(self.trace) * 1.0
for deal in progress(self.trace): for deal in progress(self.trace):
for i, card in enumerate(deal): for i, card in enumerate(deal):
@ -234,7 +261,8 @@ class DeceptionGame(object):
return dict(out) return dict(out)
def __str__(self): def __str__(self):
return "%d Player Game (%d constraints)" % (self.n_players, len(self.seen)) return "%d Player Game (%d constraints)" % (self.n_players,
len(self.seen))
def disbelieve(self, i): def disbelieve(self, i):
self.observations = self.observations[:i] + self.observations[i + 1:] self.observations = self.observations[:i] + self.observations[i + 1:]
@ -245,12 +273,19 @@ class DeceptionGame(object):
pp = pprint.PrettyPrinter(indent=4) pp = pprint.PrettyPrinter(indent=4)
pp.pprint(self.report()) pp.pprint(self.report())
def repl_report(report, namemap, ngood): def repl_report(report, namemap, ngood):
sort_order = sorted([(report[i]["side"].get(True, 0.0), i) for i in range(len(report))], reverse=True) sort_order = sorted(
[(report[i]["side"].get(True, 0.0), i)
for i in range(len(report))],
reverse=True)
still_good = 0 still_good = 0
for goodness, i in sort_order: for goodness, i in sort_order:
row = "%s: %2f%% Good %2f%% Evil" % (namemap.get(i, "") + "(%s)" % str(i), goodness * 100, (1.0 - goodness) * 100) row = "%s: %2f%% Good %2f%% Evil" % (
namemap.get(i, "") + " (%s)" % str(i),
goodness * 100,
(1.0 - goodness) * 100)
if still_good < ngood: if still_good < ngood:
puts(colored.cyan(row)) puts(colored.cyan(row))
else: else:
@ -271,7 +306,8 @@ def main():
command = command_list[0] command = command_list[0]
if command == "quit" or command == "q" or command == "exit": if command == "quit" or command == "q" or command == "exit":
sys.exit(0) sys.exit(0)
if (command != "newgame" and command != "testgame") and game is None: if (command != "newgame" and command != "testgame") \
and game is None:
puts(colored.red("Need to create a game")) puts(colored.red("Need to create a game"))
continue continue
elif command == "newgame": elif command == "newgame":
@ -293,7 +329,8 @@ def main():
continue continue
elif command == "ls": elif command == "ls":
for i, statement in enumerate(game.seen): for i, statement in enumerate(game.seen):
name = " ".join([namemap.get(x, str(x)) for x in statement]) name = " ".join(
[namemap.get(x, str(x)) for x in statement])
print "%d: %s" % (i, name) print "%d: %s" % (i, name)
continue continue
elif command == "vote": elif command == "vote":
@ -354,29 +391,5 @@ def main():
continue continue
if __name__ == '__main__': if __name__ == '__main__':
main() main()
base_game = DeceptionGame(ResistanceGame(5))
#base_game.add_known_role(0, "G1")
#base_game.add_known_alliance(1, False)
#base_game.player_sees_player_and_claims(0, 1, True)
#base_game.player_sees_player_and_claims(1, 2, False)
#base_game.player_sees_player_and_claims(2, 3, False)
base_game.do_vote([1,2], [0,1,1,0,1], 1)
base_game.do_mission([1,2], 0, False, 1)
base_game.do_vote([0, 1, 2], [1,1,1,0,1], 2)
base_game.do_mission([0, 1, 2], 1, False, 2)
base_game.do_vote([3, 4], [0,0,1,1,1], 3)
base_game.do_mission([3, 4], 0, False, 3)
base_game.do_vote([3, 4], [0,0,1,1,1], 4)
base_game.do_mission([0, 3, 4], 1, False, 4)
#base_game.do_vote([1, 3, 4], [0,1,1,0,1], 5)
#base_game.do_mission([1, 3, 4], 1, True, 5)
base_game.eval(100)
base_game.print_report()