2023-03-16 14:07:42 +01:00
|
|
|
#! /bin/python3
|
2023-03-14 18:15:15 +01:00
|
|
|
import collections
|
2023-03-18 10:17:24 +01:00
|
|
|
import sys
|
2023-07-04 20:06:06 +02:00
|
|
|
|
2023-03-14 15:20:18 +01:00
|
|
|
from enum import Enum
|
2023-07-04 18:52:59 +02:00
|
|
|
from typing import Optional
|
2023-03-14 09:04:08 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
from hanabi import logger
|
|
|
|
from hanabi import hanab_game
|
|
|
|
from hanabi.live import compress
|
2023-07-05 20:50:40 +02:00
|
|
|
from hanabi import database
|
2023-03-14 09:04:08 +01:00
|
|
|
|
|
|
|
|
2023-03-14 15:20:18 +01:00
|
|
|
class CardType(Enum):
|
2023-06-24 17:25:07 +02:00
|
|
|
Dispensable = -1
|
2023-03-14 15:20:18 +01:00
|
|
|
Trash = 0
|
|
|
|
Playable = 1
|
|
|
|
Critical = 2
|
2023-05-25 17:00:18 +02:00
|
|
|
DuplicateVisible = 3
|
|
|
|
UniqueVisible = 4
|
2023-03-14 15:20:18 +01:00
|
|
|
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
class CardState:
|
|
|
|
def __init__(self, card_type: CardType, card: hanab_game.DeckCard, weight: Optional[int] = 1):
|
2023-03-14 15:20:18 +01:00
|
|
|
self.card_type = card_type
|
|
|
|
self.card = card
|
|
|
|
self.weight = weight
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
match self.card_type:
|
|
|
|
case CardType.Trash:
|
|
|
|
return "Trash ({})".format(self.card)
|
|
|
|
case CardType.Playable:
|
|
|
|
return "Playable ({}) with weight {}".format(self.card, self.weight)
|
|
|
|
case CardType.Critical:
|
|
|
|
return "Critical ({})".format(self.card)
|
2023-05-25 17:00:18 +02:00
|
|
|
case CardType.DuplicateVisible:
|
|
|
|
return "Useful (duplicate visible) ({}) with weight {}".format(self.card, self.weight)
|
|
|
|
case CardType.UniqueVisible:
|
|
|
|
return "Useful (unique visible) ({}) with weight {}".format(self.card, self.weight)
|
2023-03-14 15:20:18 +01:00
|
|
|
|
2023-05-19 12:18:38 +02:00
|
|
|
|
2023-03-18 10:17:24 +01:00
|
|
|
# TODO
|
|
|
|
def card_type(game_state, card):
|
|
|
|
played = game_state.stacks[card.suitIndex]
|
|
|
|
if card.rank <= played:
|
|
|
|
return CardType.Trash
|
|
|
|
elif card.rank == played + 1:
|
|
|
|
return CardType.Playable
|
|
|
|
elif card.rank == 5 or card in game_state.trash:
|
|
|
|
return CardType.Critical
|
|
|
|
else:
|
2023-05-25 17:00:18 +02:00
|
|
|
visible_cards = sum((game_state.hands[player] for player in range(game_state.num_players)), [])
|
|
|
|
if visible_cards.count(card) >= 2:
|
|
|
|
return CardType.DuplicateVisible
|
|
|
|
else:
|
|
|
|
return CardType.UniqueVisible
|
|
|
|
|
|
|
|
|
|
|
|
class WeightedCard:
|
|
|
|
def __init__(self, card, weight: Optional[int] = None):
|
|
|
|
self.card = card
|
|
|
|
self.weight = weight
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "{} with weight {}".format(self.card, self.weight)
|
|
|
|
|
|
|
|
|
|
|
|
class HandState:
|
2023-07-04 20:06:06 +02:00
|
|
|
def __init__(self, player: int, game_state: hanab_game.GameState):
|
2023-05-25 17:00:18 +02:00
|
|
|
self.trash = []
|
|
|
|
self.playable = []
|
|
|
|
self.critical = []
|
|
|
|
self.dupes = []
|
|
|
|
self.uniques = []
|
|
|
|
for card in game_state.hands[player]:
|
|
|
|
match card_type(game_state, card):
|
|
|
|
case CardType.Trash:
|
|
|
|
self.trash.append(WeightedCard(card))
|
|
|
|
case CardType.Playable:
|
|
|
|
if card not in map(lambda c: c.card, self.playable):
|
|
|
|
self.playable.append(WeightedCard(card))
|
|
|
|
else:
|
|
|
|
self.trash.append(card)
|
|
|
|
case CardType.Critical:
|
|
|
|
self.critical.append(WeightedCard(card))
|
|
|
|
case CardType.UniqueVisible:
|
|
|
|
self.uniques.append(WeightedCard(card))
|
|
|
|
case CardType.DuplicateVisible:
|
|
|
|
copy = next((w for w in self.dupes if w.card == card), None)
|
|
|
|
if copy is not None:
|
|
|
|
self.dupes.remove(copy)
|
|
|
|
self.critical.append(copy)
|
|
|
|
self.trash.append(card)
|
|
|
|
else:
|
|
|
|
self.dupes.append(WeightedCard(card))
|
|
|
|
self.playable.sort(key=lambda c: c.card.rank)
|
|
|
|
self.dupes.sort(key=lambda c: c.card.rank)
|
|
|
|
self.uniques.sort(key=lambda c: c.card.rank)
|
|
|
|
if len(self.trash) > 0:
|
|
|
|
self.best_discard = self.trash[0]
|
|
|
|
self.discard_badness = 0
|
|
|
|
elif len(self.dupes) > 0:
|
|
|
|
self.best_discard = self.dupes[0]
|
|
|
|
self.discard_badness = 8 - game_state.num_players
|
|
|
|
elif len(self.uniques) > 0:
|
|
|
|
self.best_discard = self.uniques[-1]
|
|
|
|
self.discard_badness = 80 - 10 * self.best_discard.card.rank
|
|
|
|
elif len(self.playable) > 0:
|
|
|
|
self.best_discard = self.playable[-1]
|
|
|
|
self.discard_badness = 80 - 10 * self.best_discard.card.rank
|
|
|
|
else:
|
|
|
|
assert len(self.critical) > 0, "Programming error."
|
|
|
|
self.best_discard = self.critical[-1]
|
2023-07-04 20:06:06 +02:00
|
|
|
self.discard_badness = 600 - 100 * self.best_discard.card.rank
|
2023-05-25 17:00:18 +02:00
|
|
|
|
|
|
|
def num_useful_cards(self):
|
|
|
|
return len(self.dupes) + len(self.uniques) + len(self.playable) + len(self.critical)
|
|
|
|
|
|
|
|
|
|
|
|
class CheatingStrategy:
|
2023-07-04 20:06:06 +02:00
|
|
|
def __init__(self, game_state: hanab_game.GameState):
|
2023-05-25 17:00:18 +02:00
|
|
|
self.game_state = game_state
|
|
|
|
|
|
|
|
def make_move(self):
|
|
|
|
hand_states = [HandState(player, self.game_state) for player in range(self.game_state.num_players)]
|
|
|
|
|
|
|
|
modified_pace = self.game_state.pace - sum(
|
|
|
|
1 for state in hand_states if len(state.trash) == self.game_state.hand_size
|
|
|
|
)
|
|
|
|
|
|
|
|
cur_hand = hand_states[self.game_state.turn]
|
|
|
|
|
|
|
|
print([state.__dict__ for state in hand_states])
|
|
|
|
print(self.game_state.pace)
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
2023-03-14 15:20:18 +01:00
|
|
|
class GreedyStrategy():
|
2023-07-04 20:06:06 +02:00
|
|
|
def __init__(self, game_state: hanab_game.GameState):
|
2023-03-14 15:20:18 +01:00
|
|
|
self.game_state = game_state
|
|
|
|
|
|
|
|
self.earliest_draw_times = []
|
2023-03-18 10:17:24 +01:00
|
|
|
for s in range(0, game_state.instance.num_suits):
|
2023-03-14 15:20:18 +01:00
|
|
|
self.earliest_draw_times.append([])
|
|
|
|
for r in range(1, 6):
|
|
|
|
self.earliest_draw_times[s].append(max(
|
2023-07-04 20:06:06 +02:00
|
|
|
game_state.deck.index(hanab_game.DeckCard(s, r)) - game_state.hand_size * game_state.num_players + 1,
|
2023-05-19 12:18:38 +02:00
|
|
|
0 if r == 1 else self.earliest_draw_times[s][r - 2]
|
2023-03-14 15:20:18 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
# Currently, we do not add the time the 5 gets drawn to this, since this is rather a measurument on how
|
|
|
|
# bad a suit is in terms of having to hold on to other cards that are not playable *yet*
|
|
|
|
self.suit_badness = [sum(self.earliest_draw_times[s][:-1]) for s in range(0, game_state.num_suits)]
|
|
|
|
|
|
|
|
def make_move(self):
|
2023-05-19 12:18:38 +02:00
|
|
|
hand_states = [[CardState(card_type(self.game_state, card), card, None) for card in self.game_state.hands[p]]
|
|
|
|
for p in range(self.game_state.num_players)]
|
2023-03-14 18:15:15 +01:00
|
|
|
|
|
|
|
# find dupes in players hands, marke one card crit and the other one trash
|
2023-03-14 18:27:29 +01:00
|
|
|
p = False
|
2023-03-14 18:15:15 +01:00
|
|
|
for states in hand_states:
|
|
|
|
counter = collections.Counter(map(lambda state: state.card, states))
|
|
|
|
for card in counter:
|
|
|
|
if counter[card] >= 2:
|
2023-03-14 18:27:29 +01:00
|
|
|
dupes = (cstate for cstate in states if cstate.card == card)
|
|
|
|
first = next(dupes)
|
|
|
|
if first.card_type == CardType.Dispensable:
|
|
|
|
first.card_type = CardType.Critical
|
|
|
|
for dupe in dupes:
|
|
|
|
dupe.card_type = CardType.Trash
|
2023-03-14 18:15:15 +01:00
|
|
|
|
2023-03-16 14:07:42 +01:00
|
|
|
def hand_badness(states):
|
|
|
|
if any(state.card_type == CardType.Playable for state in states):
|
|
|
|
return 0
|
|
|
|
crits = [state for state in states if state.card_type == CardType.Critical]
|
|
|
|
crits_val = sum(map(lambda state: state.card.rank, crits))
|
|
|
|
if any(state.card_type == CardType.Playable for state in states):
|
|
|
|
return crits_val
|
2023-05-19 12:18:38 +02:00
|
|
|
|
2023-03-16 14:07:42 +01:00
|
|
|
def player_distance(f, t):
|
|
|
|
return ((t - f - 1) % self.game_state.num_players) + 1
|
|
|
|
|
2023-03-14 15:20:18 +01:00
|
|
|
for (player, states) in enumerate(hand_states):
|
|
|
|
for state in states:
|
|
|
|
if state.card_type == CardType.Playable:
|
2023-03-16 14:07:42 +01:00
|
|
|
copy_holders = set(self.game_state.holding_players(state.card))
|
2023-03-14 15:20:18 +01:00
|
|
|
copy_holders.remove(player)
|
2023-05-19 12:18:38 +02:00
|
|
|
connecting_holders = set(
|
2023-07-04 20:06:06 +02:00
|
|
|
self.game_state.holding_players(hanab_game.DeckCard(state.card.suitIndex, state.card.rank + 1)))
|
2023-03-16 14:07:42 +01:00
|
|
|
|
2023-03-14 15:20:18 +01:00
|
|
|
if len(copy_holders) == 0:
|
2023-03-16 14:07:42 +01:00
|
|
|
# card is unique, imortancy is based lexicographically on whether somebody has the conn. card and the rank
|
|
|
|
state.weight = (6 if len(connecting_holders) > 0 else 1) * (6 - state.card.rank)
|
2023-03-14 15:20:18 +01:00
|
|
|
else:
|
2023-03-16 14:07:42 +01:00
|
|
|
# copy is available somewhere else
|
|
|
|
if len(connecting_holders) == 0:
|
|
|
|
# card is not urgent
|
|
|
|
state.weight = 0.5 * (6 - state.card.rank)
|
|
|
|
else:
|
|
|
|
# there is a copy and there is a connecting card. check if they are out of order
|
|
|
|
turns_to_copy = min(map(lambda holder: player_distance(player, holder), copy_holders))
|
|
|
|
turns_to_conn = max(map(lambda holder: player_distance(player, holder), connecting_holders))
|
|
|
|
if turns_to_copy < turns_to_conn:
|
|
|
|
# our copy is not neccessary for connecting card to be able to play
|
|
|
|
state.weight = 0.5 * (6 - state.card.rank)
|
|
|
|
else:
|
|
|
|
# our copy is important, scale it little less than if it were unique
|
|
|
|
state.weight = 4 * (6 - state.card.rank)
|
2023-03-14 15:20:18 +01:00
|
|
|
elif state.card_type == CardType.Dispensable:
|
|
|
|
try:
|
|
|
|
# TODO: consider duplicate in hand
|
|
|
|
copy_holders = list(self.game_state.holding_players(state.card))
|
|
|
|
copy_holders.remove(player)
|
|
|
|
nextCopy = self.game_state.deck[self.game_state.progress:].index(card)
|
|
|
|
except:
|
|
|
|
nextCopy = 1
|
2023-05-19 12:18:38 +02:00
|
|
|
# state.weight = self.suit_badness[state.card.suitIndex] * nextCopy + 2 * (5 - state.card.rank)
|
2023-03-14 18:15:15 +01:00
|
|
|
state.weight = nextCopy + 2 * (5 - state.card.rank)
|
2023-03-14 18:27:29 +01:00
|
|
|
|
2023-03-14 15:20:18 +01:00
|
|
|
cur_hand = hand_states[self.game_state.turn]
|
|
|
|
plays = [cstate for cstate in cur_hand if cstate.card_type == CardType.Playable]
|
|
|
|
trash = next((cstate for cstate in cur_hand if cstate.card_type == CardType.Trash), None)
|
|
|
|
|
2023-03-14 18:46:45 +01:00
|
|
|
# actual decision on what to do
|
|
|
|
|
2023-03-14 15:20:18 +01:00
|
|
|
if len(plays) > 0:
|
|
|
|
play = max(plays, key=lambda s: s.weight)
|
|
|
|
self.game_state.play(play.card.deck_index)
|
|
|
|
elif self.game_state.clues == 8:
|
|
|
|
self.game_state.clue()
|
|
|
|
elif trash is not None:
|
|
|
|
self.game_state.discard(trash.card.deck_index)
|
|
|
|
elif self.game_state.clues == 0:
|
|
|
|
dispensable = [cstate for cstate in cur_hand if cstate.card_type == CardType.Dispensable]
|
|
|
|
if len(dispensable) == 0:
|
2023-03-15 15:44:03 +01:00
|
|
|
self.game_state.in_lost_state = True
|
2023-05-19 12:18:38 +02:00
|
|
|
# raise ValueError("Lost critical card")
|
2023-03-14 15:20:18 +01:00
|
|
|
else:
|
|
|
|
discard = min(dispensable, key=lambda s: s.weight)
|
|
|
|
self.game_state.discard(discard.card.deck_index)
|
|
|
|
else:
|
|
|
|
self.game_state.clue()
|
|
|
|
|
2023-05-19 12:18:38 +02:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def run_deck(instance: hanab_game.HanabiInstance) -> hanab_game.GameState:
|
|
|
|
gs = hanab_game.GameState(instance)
|
2023-05-25 17:00:18 +02:00
|
|
|
strat = CheatingStrategy(gs)
|
2023-03-16 14:07:42 +01:00
|
|
|
while not gs.is_over():
|
|
|
|
strat.make_move()
|
2023-05-19 12:55:27 +02:00
|
|
|
return gs
|
2023-03-17 11:55:46 +01:00
|
|
|
|
2023-03-14 09:04:08 +01:00
|
|
|
|
2023-03-16 14:07:42 +01:00
|
|
|
def run_samples(num_players, sample_size):
|
2023-05-19 12:55:27 +02:00
|
|
|
logger.info("Running {} test games on {} players using greedy strategy.".format(sample_size, num_players))
|
2023-03-17 11:55:46 +01:00
|
|
|
won = 0
|
|
|
|
lost = 0
|
2023-07-04 20:06:06 +02:00
|
|
|
cur = database.conn.cursor()
|
2023-05-19 12:18:38 +02:00
|
|
|
cur.execute(
|
2023-05-19 12:55:27 +02:00
|
|
|
"SELECT seed, num_players, deck, variant_id "
|
|
|
|
"FROM seeds WHERE variant_id = 0 AND num_players = (%s)"
|
|
|
|
"ORDER BY seed DESC LIMIT (%s)",
|
2023-05-19 12:18:38 +02:00
|
|
|
(num_players, sample_size))
|
2023-03-14 15:20:18 +01:00
|
|
|
for r in cur:
|
2023-05-19 12:55:27 +02:00
|
|
|
seed, num_players, deck_str, var_id = r
|
2023-07-04 20:06:06 +02:00
|
|
|
deck = compress.decompress_deck(deck_str)
|
|
|
|
instance = hanab_game.HanabiInstance(deck, num_players)
|
2023-05-19 12:55:27 +02:00
|
|
|
final_game_state = run_deck(instance)
|
|
|
|
if final_game_state.score != instance.max_score:
|
|
|
|
logger.verbose(
|
|
|
|
"Greedy strategy lost {}-player seed {:10} {}:\n{}"
|
2023-07-04 20:06:06 +02:00
|
|
|
.format(num_players, seed, str(deck), compress.link(final_game_state))
|
2023-05-19 12:55:27 +02:00
|
|
|
)
|
2023-03-17 11:55:46 +01:00
|
|
|
lost += 1
|
2023-05-19 12:55:27 +02:00
|
|
|
else:
|
|
|
|
won += 1
|
2023-05-19 12:18:38 +02:00
|
|
|
print("won: {:4}, lost: {:4}".format(won, lost), end="\r")
|
2023-05-19 12:55:27 +02:00
|
|
|
logger.info("Won {} ({}%) and lost {} ({}%) from sample of {} test games using greedy strategy.".format(
|
|
|
|
won, round(100 * won / sample_size, 2), lost, round(100 * lost / sample_size, 2), sample_size
|
|
|
|
))
|