pep8
This commit is contained in:
parent
9a5e14b884
commit
e77323d8a1
1 changed files with 104 additions and 104 deletions
58
tim.py
58
tim.py
|
|
@ -6,8 +6,9 @@ import pprint
|
|||
|
||||
|
||||
def ResistanceGame(n_players):
|
||||
full_set = [("G1", True), ("G2", True), ("G3", True), ("E1", False), ("E2", False),
|
||||
("G4", True), ("E3", False), ("G5", True), ("G6", True), ("E4", False)]
|
||||
full_set = [("G1", True), ("G2", True), ("G3", True),
|
||||
("E1", False), ("E2", False), ("G4", True),
|
||||
("E3", False), ("G5", True), ("G6", True), ("E4", False)]
|
||||
return full_set[:n_players]
|
||||
|
||||
|
||||
|
|
@ -15,11 +16,11 @@ class DeceptionGame(object):
|
|||
def __init__(self, player_array):
|
||||
self.player_array = player_array
|
||||
self.all_permutations = list(itertools.permutations(player_array))
|
||||
self.deck_var = mc.DiscreteUniform("deal", 0, len(self.all_permutations) - 1)
|
||||
self.deck_var = mc.DiscreteUniform("deal", 0,
|
||||
len(self.all_permutations) - 1)
|
||||
self.n_players = len(player_array)
|
||||
self.model = None
|
||||
|
||||
|
||||
self.player_alliance_vars = []
|
||||
self.player_role_vars = []
|
||||
self.role_ids = {}
|
||||
|
|
@ -35,27 +36,28 @@ class DeceptionGame(object):
|
|||
def player_alliance(x, role):
|
||||
return self.role_alliance[role]
|
||||
|
||||
def player_role(x, deck_var = self.deck_var):
|
||||
def player_role(x, deck_var=self.deck_var):
|
||||
role_str = self.all_permutations[deck_var][x][0]
|
||||
return self.role_ids[role_str]
|
||||
|
||||
for x in range(self.n_players):
|
||||
role = mc.Deterministic(eval = functools.partial(player_role, x),
|
||||
name = "player_role_%d" % x,
|
||||
parents = { "deck_var" : self.deck_var },
|
||||
doc = "Who is player %d?" % x,
|
||||
dtype = str,
|
||||
trace = True,
|
||||
plot = False)
|
||||
role = mc.Deterministic(eval=functools.partial(player_role, x),
|
||||
name="player_role_%d" % x,
|
||||
parents={"deck_var": self.deck_var},
|
||||
doc="Who is player %d?" % x,
|
||||
dtype=str,
|
||||
trace=True,
|
||||
plot=False)
|
||||
self.player_role_vars.append(role)
|
||||
|
||||
alliance = mc.Deterministic(eval = functools.partial(player_alliance, x),
|
||||
name = "player_alliance_%d" % x,
|
||||
parents = { "role" : role},
|
||||
doc = "Is player %d good?" % x,
|
||||
dtype = bool,
|
||||
trace = True,
|
||||
plot = False)
|
||||
alliance = mc.Deterministic(eval=functools.partial(
|
||||
player_alliance, x),
|
||||
name="player_alliance_%d" % x,
|
||||
parents={"role": role},
|
||||
doc="Is player %d good?" % x,
|
||||
dtype=bool,
|
||||
trace=True,
|
||||
plot=False)
|
||||
|
||||
self.player_alliance_vars.append(alliance)
|
||||
|
||||
|
|
@ -66,8 +68,8 @@ class DeceptionGame(object):
|
|||
transaction = []
|
||||
obs = mc.Bernoulli("player_seen_tid%d" % self.tid,
|
||||
self.player_alliance_vars[player_id],
|
||||
value = is_good,
|
||||
observed = True)
|
||||
value=is_good,
|
||||
observed=True)
|
||||
transaction.append(obs)
|
||||
self.observations.append(transaction)
|
||||
self.tid += 1
|
||||
|
|
@ -76,13 +78,12 @@ class DeceptionGame(object):
|
|||
transaction = []
|
||||
obs = mc.Bernoulli("player_seen_role_tid%d" % self.tid,
|
||||
self.player_role_vars[player_id],
|
||||
value = self.role_ids[role_str],
|
||||
observed = True)
|
||||
value=self.role_ids[role_str],
|
||||
observed=True)
|
||||
transaction.append(obs)
|
||||
self.observations.append(transaction)
|
||||
self.tid += 1
|
||||
|
||||
|
||||
def eval(self, length=40000):
|
||||
mcmc = mc.MCMC(self._build_model_list())
|
||||
mcmc.sample(length, 2000)
|
||||
|
|
@ -115,9 +116,8 @@ class DeceptionGame(object):
|
|||
out[x] += 1 / size
|
||||
return dict(out)
|
||||
|
||||
|
||||
def print_report(self):
|
||||
pp = pprint.PrettyPrinter(indent = 4)
|
||||
pp = pprint.PrettyPrinter(indent=4)
|
||||
pp.pprint(self.report())
|
||||
|
||||
def _build_model_list(self):
|
||||
|
|
@ -125,16 +125,16 @@ class DeceptionGame(object):
|
|||
out.append(self.deck_var)
|
||||
out.extend(self.player_alliance_vars[:])
|
||||
out.extend(self.player_role_vars[:])
|
||||
flattened = [item for transaction in self.observations for item in transaction]
|
||||
flattened = [item for transaction in self.observations
|
||||
for item in transaction]
|
||||
out.extend(flattened[:])
|
||||
return list(set(out))
|
||||
|
||||
|
||||
base_game = DeceptionGame(ResistanceGame(5))
|
||||
base_game.eval(10000)
|
||||
base_game.eval(20000)
|
||||
base_game.add_known_role(0, "G1")
|
||||
base_game.add_known_alliance(1, False)
|
||||
base_game.eval()
|
||||
|
||||
base_game.print_report()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue