2023-03-15 16:41:22 +01:00
|
|
|
from enum import Enum
|
2023-07-08 09:48:22 +02:00
|
|
|
from typing import List
|
2023-03-15 16:41:22 +01:00
|
|
|
|
2023-07-08 11:53:30 +02:00
|
|
|
import alive_progress
|
|
|
|
|
2023-07-05 20:50:40 +02:00
|
|
|
from hanabi import database
|
2023-07-08 09:48:22 +02:00
|
|
|
from hanabi import logger
|
2023-07-04 20:06:06 +02:00
|
|
|
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):
|
2024-10-11 17:33:43 +02:00
|
|
|
Pace = 0 # idx denotes index of last card drawn before being forced to reduce pace, value denotes how bad pace is
|
|
|
|
DoubleBottom2With5s = 1 # same, special case for 2p
|
|
|
|
TripleBottom1With5s = 2 # same, special case for 2p
|
|
|
|
MultiSuitBdr = 3
|
|
|
|
PaceAfterSqueeze = 4
|
|
|
|
HandSize = 10 # idx denotes index of last card drawn before being forced to discard a crit
|
|
|
|
HandSizeDirect = 11
|
|
|
|
HandSizeWithSqueeze = 12
|
|
|
|
HandSizeWithBdr = 13
|
|
|
|
HandSizeWithBdrSqueeze = 14
|
|
|
|
|
|
|
|
# further reasons, currently not scanned for
|
|
|
|
BottomTopDeck = 20
|
|
|
|
DoubleBottomTopDeck = 30
|
|
|
|
CritAtBottom = 40
|
2023-03-16 09:34:03 +01:00
|
|
|
|
|
|
|
|
2023-07-08 09:48:22 +02:00
|
|
|
class InfeasibilityReason:
|
2024-10-11 17:33:43 +02:00
|
|
|
def __init__(self, infeasibility_type: InfeasibilityType, value=None):
|
2023-03-16 09:34:03 +01:00
|
|
|
self.type = infeasibility_type
|
|
|
|
self.value = value
|
|
|
|
|
2024-10-11 17:33:43 +02:00
|
|
|
|
2023-03-16 09:34:03 +01:00
|
|
|
def __repr__(self):
|
2024-10-11 17:33:43 +02:00
|
|
|
return "{} ({})".format(self.type, self.value)
|
2023-03-16 09:34:03 +01:00
|
|
|
match self.type:
|
2024-10-11 17:33:43 +02:00
|
|
|
case InfeasibilityType.Pace:
|
|
|
|
return "Out of Pace after drawing card {}".format(self.value)
|
|
|
|
case InfeasibilityType.HandSize:
|
|
|
|
return "Out of hand size after drawing card {}".format(self.value)
|
2023-06-24 17:24:11 +02:00
|
|
|
case InfeasibilityType.CritAtBottom:
|
2024-10-11 17:33:43 +02:00
|
|
|
return "Critical non-5 at bottom"
|
|
|
|
|
2023-07-08 09:48:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def analyze(instance: hanab_game.HanabiInstance, only_find_first=False) -> List[InfeasibilityReason]:
|
|
|
|
"""
|
|
|
|
Checks instance for the following (easy) certificates for unfeasibility
|
|
|
|
- There is a critical non-5 at the bottom
|
|
|
|
- We necessarily run out of pace when playing this deck:
|
|
|
|
At some point, among all drawn cards, there are too few playable ones so that the next discard
|
|
|
|
reduces pace to a negative amount
|
|
|
|
- We run out of hand size when playing this deck:
|
|
|
|
At some point, there are too many critical cards (that also could not have been played) for the players
|
|
|
|
to hold collectively
|
|
|
|
:param instance: Instance to be analyzed
|
|
|
|
:param only_find_first: If true, we immediately return when finding the first infeasibility reason and don't
|
|
|
|
check for further ones. Might be slightly faster on some instances, especially dark ones.
|
|
|
|
:return: List with all reasons found. Empty if none is found.
|
|
|
|
In particular, if return value is not the empty list, the analyzed instance is unfeasible
|
|
|
|
"""
|
|
|
|
reasons = []
|
|
|
|
|
|
|
|
# check for critical non-fives at bottom of the deck
|
|
|
|
bottom_card = instance.deck[-1]
|
|
|
|
if bottom_card.rank != 5 and bottom_card.suitIndex in instance.dark_suits:
|
|
|
|
reasons.append(InfeasibilityReason(
|
|
|
|
InfeasibilityType.CritAtBottom,
|
|
|
|
instance.deck_size - 1
|
|
|
|
))
|
|
|
|
if only_find_first:
|
|
|
|
return reasons
|
|
|
|
|
|
|
|
# we will sweep through the deck and pretend that
|
|
|
|
# - we keep all non-trash cards in our hands
|
|
|
|
# - we instantly play all playable cards as soon as we have them
|
|
|
|
# - we recurse on this instant-play
|
|
|
|
#
|
|
|
|
# For example, we assume that once we draw r2, we check if we can play r2.
|
|
|
|
# If yes, then we also check if we drew r3 earlier and so on.
|
|
|
|
# If not, then we keep r2 in our hands
|
|
|
|
#
|
2024-10-11 17:33:43 +02:00
|
|
|
# In total, this is equivalent to assuming that we have infinitely many clues
|
2023-07-08 09:48:22 +02:00
|
|
|
# and infinite storage space in our hands (which is of course not true),
|
|
|
|
# but even in this setting, some games are infeasible due to pace issues
|
|
|
|
# that we can detect
|
|
|
|
#
|
|
|
|
# A small refinement is to pretend that we only have infinite storage for non-crit cards,
|
|
|
|
# for crit-cards, the usual hand card limit applies.
|
|
|
|
# This allows us to detect some seeds where there are simply too many unplayable cards to hold at some point
|
|
|
|
# that also can't be discarded
|
2023-03-15 16:41:22 +01:00
|
|
|
|
2023-03-18 14:08:18 +01:00
|
|
|
stacks = [0] * instance.num_suits
|
2023-07-08 09:48:22 +02:00
|
|
|
|
|
|
|
# we will ensure that stored_crits is a subset of stored_cards
|
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
|
|
|
|
2024-10-11 17:33:43 +02:00
|
|
|
pace_found = False
|
|
|
|
hand_size_found = False
|
|
|
|
squeeze = False
|
|
|
|
considered_bdr = False
|
|
|
|
artificial_crits = set()
|
|
|
|
|
|
|
|
# Investigate BDRs. This catches special cases of Pace losses in 2p, as well as mark some cards critical because
|
|
|
|
# their second copies cannot be used.
|
|
|
|
filtered_deck = [card for card in instance.deck if card.rank != 5]
|
|
|
|
if instance.num_players == 2:
|
|
|
|
if filtered_deck[-1] == filtered_deck[-2] and filtered_deck[-1].rank == 2:
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.Pace, filtered_deck[-2].deck_index - 1))
|
|
|
|
if only_find_first:
|
|
|
|
return reasons
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.DoubleBottom2With5s, filtered_deck[-2].deck_index - 1))
|
|
|
|
pace_found = True
|
|
|
|
if filtered_deck[-1] == filtered_deck[-2] and filtered_deck[-2] == filtered_deck[-3] and filtered_deck[-3].rank == 1:
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.Pace, filtered_deck[-3].deck_index - 1))
|
|
|
|
if only_find_first:
|
|
|
|
return reasons
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.DoubleBottom2With5s, filtered_deck[-2].deck_index - 1))
|
|
|
|
pace_found = True
|
|
|
|
|
|
|
|
# In 2-player, the second-last card cannot be played if it is a 2
|
|
|
|
if filtered_deck[-2].rank == 2:
|
|
|
|
artificial_crits.add(filtered_deck[-2])
|
2023-03-18 14:08:18 +01:00
|
|
|
|
2024-10-11 17:33:43 +02:00
|
|
|
# In 2-player, in case there is double bottom 3 of the same suit, the card immediately before cannot be played:
|
|
|
|
# After playing that one and drawing the first 3, exactly 3,4,5 of the bottom suit have to be played
|
|
|
|
if filtered_deck[-1] == filtered_deck[-2] and filtered_deck[-2].rank == 3:
|
|
|
|
artificial_crits.add(filtered_deck[-2])
|
|
|
|
|
|
|
|
# Last card in the deck can never be played
|
|
|
|
artificial_crits.add(filtered_deck[-1])
|
2023-03-18 14:08:18 +01:00
|
|
|
|
|
|
|
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
|
2024-10-11 17:33:43 +02:00
|
|
|
if card in stored_cards or card.rank == 5:
|
|
|
|
stored_crits.add(card)
|
|
|
|
elif card in artificial_crits:
|
2023-03-16 09:34:03 +01:00
|
|
|
stored_crits.add(card)
|
2024-10-11 17:33:43 +02:00
|
|
|
considered_bdr = True
|
2023-03-15 16:41:22 +01:00
|
|
|
stored_cards.add(card)
|
2023-07-04 20:06:06 +02:00
|
|
|
|
2024-10-11 17:33:43 +02:00
|
|
|
# logger.verbose("draw pile: {}\nstacks: {}\nstored: {}\nstored crits: {}".format(instance.deck[i+1:], stacks, stored_cards, stored_crits))
|
2024-10-09 17:44:46 +02:00
|
|
|
|
2024-10-11 17:33:43 +02:00
|
|
|
hand_size_left_for_crits = instance.num_players * instance.hand_size - len(stored_crits) - 1
|
2023-03-17 11:58:04 +01:00
|
|
|
|
2024-10-10 13:40:05 +02:00
|
|
|
# In case we can only keep the critical cards exactly, get rid of all others
|
2024-10-11 17:33:43 +02:00
|
|
|
if hand_size_left_for_crits == 0:
|
2024-10-10 13:40:05 +02:00
|
|
|
stored_cards = stored_crits
|
2024-10-11 17:33:43 +02:00
|
|
|
squeeze = True
|
|
|
|
|
|
|
|
# Use a bool flag to only mark this reason once
|
|
|
|
if hand_size_left_for_crits < 0 and not hand_size_found:
|
|
|
|
reasons.append(InfeasibilityReason(
|
|
|
|
InfeasibilityType.HandSize,
|
|
|
|
i
|
|
|
|
))
|
|
|
|
if only_find_first:
|
|
|
|
return reasons
|
|
|
|
hand_size_found = True
|
|
|
|
|
|
|
|
# More detailed analysis of loss, categorization only
|
|
|
|
if squeeze:
|
|
|
|
if considered_bdr:
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.HandSizeWithBdrSqueeze, i))
|
|
|
|
else:
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.HandSizeWithSqueeze, i))
|
|
|
|
else:
|
|
|
|
if considered_bdr:
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.HandSizeWithBdr, i))
|
|
|
|
else:
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.HandSizeDirect, i))
|
2024-10-10 13:40:05 +02: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-07-08 09:48:22 +02:00
|
|
|
needed_plays = instance.max_score - sum(stacks)
|
|
|
|
cur_pace = max_remaining_plays - needed_plays
|
2024-10-11 17:33:43 +02:00
|
|
|
if cur_pace < 0 and not pace_found and not hand_size_found:
|
|
|
|
reasons.append(InfeasibilityReason(
|
|
|
|
InfeasibilityType.Pace,
|
|
|
|
i
|
|
|
|
))
|
2023-07-08 09:48:22 +02:00
|
|
|
if only_find_first:
|
2024-10-11 17:33:43 +02:00
|
|
|
return reasons
|
|
|
|
|
|
|
|
# We checked single-suit pace losses beforehand (which can only occur in 2p)
|
|
|
|
if squeeze:
|
|
|
|
reasons.append(InfeasibilityReason(InfeasibilityType.PaceAfterSqueeze, i))
|
|
|
|
else:
|
2023-07-08 09:48:22 +02:00
|
|
|
reasons.append(InfeasibilityReason(
|
2024-10-11 17:33:43 +02:00
|
|
|
InfeasibilityType.MultiSuitBdr,
|
|
|
|
i
|
2023-07-08 09:48:22 +02:00
|
|
|
))
|
2024-10-11 17:33:43 +02:00
|
|
|
pace_found = True
|
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)
|
2023-07-08 09:48:22 +02:00
|
|
|
assert (sum(stacks) == instance.max_score)
|
|
|
|
|
|
|
|
return reasons
|
|
|
|
|
|
|
|
|
|
|
|
def run_on_database(variant_id):
|
|
|
|
database.cur.execute(
|
|
|
|
"SELECT seed, num_players, deck FROM seeds WHERE variant_id = (%s) ORDER BY (num_players, seed)",
|
|
|
|
(variant_id,)
|
|
|
|
)
|
|
|
|
res = database.cur.fetchall()
|
2023-07-08 11:53:30 +02:00
|
|
|
logger.verbose("Checking {} seeds of variant {} for infeasibility".format(len(res), variant_id))
|
|
|
|
with alive_progress.alive_bar(total=len(res), title='Check for infeasibility reasons in var {}'.format(variant_id)) as bar:
|
|
|
|
for (seed, num_players, deck_str) in res:
|
|
|
|
deck = compress.decompress_deck(deck_str)
|
|
|
|
reasons = analyze(hanab_game.HanabiInstance(deck, num_players))
|
|
|
|
for reason in reasons:
|
|
|
|
database.cur.execute(
|
|
|
|
"INSERT INTO score_upper_bounds (seed, score_upper_bound, reason) "
|
|
|
|
"VALUES (%s,%s,%s) "
|
|
|
|
"ON CONFLICT (seed, reason) DO UPDATE "
|
|
|
|
"SET score_upper_bound = EXCLUDED.score_upper_bound",
|
|
|
|
(seed, reason.score_upper_bound, reason.type.value)
|
|
|
|
)
|
|
|
|
database.cur.execute(
|
|
|
|
"UPDATE seeds SET feasible = (%s) WHERE seed = (%s)",
|
|
|
|
(False, seed)
|
|
|
|
)
|
|
|
|
bar()
|
2023-07-08 09:48:22 +02:00
|
|
|
database.conn.commit()
|