2023-07-04 20:06:06 +02:00
|
|
|
from hanabi.live import compress
|
2023-03-15 16:41:22 +01:00
|
|
|
from enum import Enum
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
from hanabi.database import database
|
|
|
|
from hanabi import hanab_game
|
|
|
|
from hanabi.live import compress
|
2023-03-15 16:41:22 +01:00
|
|
|
|
|
|
|
|
2023-03-16 09:34:03 +01:00
|
|
|
class InfeasibilityType(Enum):
|
2023-07-04 20:06:06 +02:00
|
|
|
OutOfPace = 0 # idx denotes index of last card drawn before being forced to reduce pace, value denotes how bad pace is
|
|
|
|
OutOfHandSize = 1 # idx denotes index of last card drawn before being forced to discard a crit
|
2023-03-17 11:58:04 +01:00
|
|
|
NotTrivial = 2
|
2023-06-24 17:24:11 +02:00
|
|
|
CritAtBottom = 3
|
2023-03-16 09:34:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
class InfeasibilityReason():
|
|
|
|
def __init__(self, infeasibility_type, idx, value=None):
|
|
|
|
self.type = infeasibility_type
|
|
|
|
self.index = idx
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
match self.type:
|
|
|
|
case InfeasibilityType.OutOfPace:
|
|
|
|
return "Deck runs out of pace ({}) after drawing card {}".format(self.value, self.index)
|
|
|
|
case InfeasibilityType.OutOfHandSize:
|
|
|
|
return "Deck runs out of hand size after drawing card {}".format(self.index)
|
2023-06-24 17:24:11 +02:00
|
|
|
case InfeasibilityType.CritAtBottom:
|
|
|
|
return "Deck has crit non-5 at bottom (index {})".format(self.index)
|
|
|
|
|
2023-03-16 09:34:03 +01:00
|
|
|
|
2023-03-18 15:17:47 +01:00
|
|
|
def analyze_suit(occurrences):
|
|
|
|
# denotes the indexes of copies we can use wlog
|
|
|
|
picks = {
|
2023-07-04 20:06:06 +02:00
|
|
|
1: 0,
|
|
|
|
**{r: None for r in range(2, 5)},
|
|
|
|
5: 0
|
2023-03-18 15:17:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# denotes the intervals when cards will be played wlog
|
|
|
|
play_times = {
|
2023-07-04 20:06:06 +02:00
|
|
|
1: [occurrences[1][0]],
|
|
|
|
**{r: None for _ in range(instance.num_suits)
|
|
|
|
for r in range(2, 6)
|
|
|
|
}
|
2023-03-18 15:17:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
print("occurrences are: {}".format(occurrences))
|
|
|
|
|
|
|
|
for rank in range(2, 6):
|
|
|
|
|
|
|
|
# general analysis
|
|
|
|
earliest_play = max(min(play_times[rank - 1]), min(occurrences[rank]))
|
2023-07-04 20:06:06 +02:00
|
|
|
latest_play = max(*play_times[rank - 1], *occurrences[rank])
|
2023-03-18 15:17:47 +01:00
|
|
|
play_times[rank] = [earliest_play, latest_play]
|
|
|
|
|
|
|
|
# check a few extra cases regarding the picks when the rank is not 5
|
|
|
|
if rank != 5:
|
|
|
|
# check if we can just play the first copy
|
|
|
|
if max(play_times[rank - 1]) < min(occurrences[rank]):
|
|
|
|
picks[rank] = 0
|
|
|
|
play_times[rank] = [min(occurrences[rank])]
|
|
|
|
continue
|
|
|
|
|
|
|
|
# check if the second copy is not worse than the first when it comes,
|
|
|
|
# because we either have to wait for smaller cards anyway
|
|
|
|
# or the next card is not there anyway
|
2023-07-04 20:06:06 +02:00
|
|
|
if max(occurrences[rank]) < max(earliest_play, min(occurrences[rank + 1])):
|
2023-03-18 15:17:47 +01:00
|
|
|
picks[rank] = 1
|
|
|
|
|
|
|
|
return picks, play_times
|
|
|
|
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def analyze_card_usage(instance: hanab_game.HanabiInstance):
|
2023-03-18 15:17:47 +01:00
|
|
|
storage_size = instance.num_players * instance.hand_size
|
|
|
|
for suit in range(instance.num_suits):
|
|
|
|
print("analysing suit {}: {}".format(
|
|
|
|
suit,
|
2023-07-04 20:06:06 +02:00
|
|
|
hanab_game.pp_deck((c for c in instance.deck if c.suitIndex == suit))
|
|
|
|
)
|
2023-03-18 15:17:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
occurrences = {
|
2023-07-04 20:06:06 +02:00
|
|
|
rank: [max(0, i - storage_size + 1) for (i, card) in enumerate(instance.deck) if
|
|
|
|
card == hanab_game.DeckCard(suit, rank)]
|
|
|
|
for rank in range(1, 6)
|
2023-03-18 15:17:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
picks, play_times = analyze_suit(occurrences)
|
|
|
|
|
|
|
|
print("did analysis:")
|
|
|
|
print("play times: ", play_times)
|
|
|
|
print("picks: ", picks)
|
|
|
|
print()
|
|
|
|
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
def analyze(instance: hanab_game.HanabiInstance, find_non_trivial=False) -> InfeasibilityReason | None:
|
2023-06-24 17:24:11 +02:00
|
|
|
if instance.deck[-1].rank != 5 and instance.deck[-1].suitIndex + instance.num_dark_suits >= instance.num_suits:
|
|
|
|
return InfeasibilityReason(InfeasibilityType.CritAtBottom, instance.deck_size - 1)
|
|
|
|
|
2023-03-15 16:41:22 +01:00
|
|
|
# we will sweep through the deck and pretend that we instantly play all cards
|
|
|
|
# as soon as we have them (and recurse this)
|
|
|
|
# this allows us to detect standard pace issue arguments
|
|
|
|
|
2023-03-18 14:08:18 +01:00
|
|
|
stacks = [0] * instance.num_suits
|
2023-03-15 16:41:22 +01:00
|
|
|
stored_cards = set()
|
2023-03-16 09:34:03 +01:00
|
|
|
stored_crits = set()
|
2023-03-18 14:08:18 +01:00
|
|
|
|
2023-03-15 16:41:22 +01:00
|
|
|
min_forced_pace = 100
|
2023-03-16 09:34:03 +01:00
|
|
|
worst_index = 0
|
2023-03-18 14:08:18 +01:00
|
|
|
|
|
|
|
ret = None
|
|
|
|
|
|
|
|
for (i, card) in enumerate(instance.deck):
|
2023-03-15 16:41:22 +01:00
|
|
|
if card.rank == stacks[card.suitIndex] + 1:
|
|
|
|
# card is playable
|
|
|
|
stacks[card.suitIndex] += 1
|
|
|
|
# check for further playables that we stored
|
|
|
|
for check_rank in range(card.rank + 1, 6):
|
2023-07-04 20:06:06 +02:00
|
|
|
check_card = hanab_game.DeckCard(card.suitIndex, check_rank)
|
2023-03-15 16:41:22 +01:00
|
|
|
if check_card in stored_cards:
|
|
|
|
stacks[card.suitIndex] += 1
|
|
|
|
stored_cards.remove(check_card)
|
2023-03-16 09:34:03 +01:00
|
|
|
if check_card in stored_crits:
|
|
|
|
stored_crits.remove(check_card)
|
2023-03-15 16:41:22 +01:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
elif card.rank <= stacks[card.suitIndex]:
|
2023-07-04 20:06:06 +02:00
|
|
|
pass # card is trash
|
2023-03-15 16:41:22 +01:00
|
|
|
elif card.rank > stacks[card.suitIndex] + 1:
|
|
|
|
# need to store card
|
2023-03-16 09:34:03 +01:00
|
|
|
if card in stored_cards or card.rank == 5:
|
|
|
|
stored_crits.add(card)
|
2023-03-15 16:41:22 +01:00
|
|
|
stored_cards.add(card)
|
2023-07-04 20:06:06 +02:00
|
|
|
|
|
|
|
# check for out of handsize:
|
2023-03-18 14:08:18 +01:00
|
|
|
if len(stored_crits) == instance.num_players * instance.hand_size:
|
2023-03-16 09:34:03 +01:00
|
|
|
return InfeasibilityReason(InfeasibilityType.OutOfHandSize, i)
|
2023-03-15 16:41:22 +01:00
|
|
|
|
2023-03-18 14:08:18 +01:00
|
|
|
if find_non_trivial and len(stored_cards) == instance.num_players * instance.hand_size:
|
2023-07-04 20:06:06 +02:00
|
|
|
ret = InfeasibilityReason(InfeasibilityType.NotTrivial, i)
|
2023-03-17 11:58:04 +01:00
|
|
|
|
2023-03-15 16:41:22 +01:00
|
|
|
# the last - 1 is there because we have to discard 'next', causing a further draw
|
2023-03-18 14:08:18 +01:00
|
|
|
max_remaining_plays = (instance.deck_size - i - 1) + instance.num_players - 1
|
2023-03-15 16:41:22 +01:00
|
|
|
|
2023-03-18 14:08:18 +01:00
|
|
|
needed_plays = 5 * instance.num_suits - sum(stacks)
|
2023-07-04 20:06:06 +02:00
|
|
|
missing = max_remaining_plays - needed_plays
|
2023-03-16 09:34:03 +01:00
|
|
|
if missing < min_forced_pace:
|
2023-07-04 20:06:06 +02:00
|
|
|
# print("update to {}: {}".format(i, missing))
|
2023-03-16 09:34:03 +01:00
|
|
|
min_forced_pace = missing
|
|
|
|
worst_index = i
|
2023-03-17 11:58:04 +01:00
|
|
|
|
|
|
|
# check that we correctly walked through the deck
|
2023-07-04 20:06:06 +02:00
|
|
|
assert (len(stored_cards) == 0)
|
|
|
|
assert (len(stored_crits) == 0)
|
|
|
|
assert (sum(stacks) == 5 * instance.num_suits)
|
2023-03-17 11:58:04 +01:00
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
if min_forced_pace < 0:
|
2023-03-16 09:34:03 +01:00
|
|
|
return InfeasibilityReason(InfeasibilityType.OutOfPace, worst_index, min_forced_pace)
|
2023-03-18 14:08:18 +01:00
|
|
|
elif ret is not None:
|
|
|
|
return ret
|
2023-03-16 09:34:03 +01:00
|
|
|
else:
|
|
|
|
return None
|
2023-03-15 16:41:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run_on_database():
|
2023-07-04 20:06:06 +02:00
|
|
|
cur = database.conn.cursor()
|
|
|
|
cur2 = database.conn.cursor()
|
2023-03-17 11:58:04 +01:00
|
|
|
for num_p in range(2, 6):
|
2023-07-04 20:06:06 +02:00
|
|
|
cur.execute(
|
|
|
|
"SELECT seed, num_players, deck from seeds where variant_id = 0 and num_players = (%s) order by seed asc",
|
|
|
|
(num_p,))
|
2023-03-17 11:58:04 +01:00
|
|
|
res = cur.fetchall()
|
|
|
|
hand = 0
|
|
|
|
pace = 0
|
|
|
|
non_trivial = 0
|
|
|
|
d = None
|
|
|
|
print("Checking {} {}-player seeds from database".format(len(res), num_p))
|
|
|
|
for (seed, num_players, deck) in res:
|
2023-07-04 20:06:06 +02:00
|
|
|
deck = compress.decompress_deck(deck)
|
|
|
|
a = analyze(hanab_game.HanabiInstance(deck, num_players), True)
|
2023-03-17 11:58:04 +01:00
|
|
|
if type(a) == InfeasibilityReason:
|
|
|
|
if a.type == InfeasibilityType.OutOfHandSize:
|
2023-07-04 20:06:06 +02:00
|
|
|
# print("Seed {} infeasible: {}\n{}".format(seed, a, deck))
|
2023-03-17 11:58:04 +01:00
|
|
|
hand += 1
|
|
|
|
elif a.type == InfeasibilityType.OutOfPace:
|
|
|
|
pace += 1
|
|
|
|
elif a.type == InfeasibilityType.NotTrivial:
|
|
|
|
non_trivial += 1
|
|
|
|
d = seed, deck
|
|
|
|
|
2023-07-04 20:06:06 +02:00
|
|
|
print("Found {} seeds running out of hand size, {} running out of pace and {} that are not trivial".format(hand,
|
|
|
|
pace,
|
|
|
|
non_trivial))
|
2023-03-17 11:58:04 +01:00
|
|
|
if d is not None:
|
2023-07-04 20:06:06 +02:00
|
|
|
print("example non-trivial deck (seed {}): [{}]".format(
|
|
|
|
d[0],
|
|
|
|
", ".join(c.colorize() for c in d[1])
|
|
|
|
))
|
2023-03-17 11:58:04 +01:00
|
|
|
print()
|