Example #1
0
class KnowledgeBasedBidder(object):
    def __init__(self):
        self.explorer = CallExplorer()

    def find_call_for(self, hand, history):
        bid, rule, hand_knowledge = self.find_bid_and_rule_and_hand_knowledge_for(hand, history)
        return bid

    def find_bid_and_rule_and_hand_knowledge_for(self, hand, history):
        oracle = ConsistencyOracle(history, hand)
        prioritiy_bid_rule_hand_knowledge_tuples = [
            oracle.priority_bid_rule_hand_knowledge_tuples(bid) for bid in self.explorer.possible_calls_over(history)
        ]
        if not prioritiy_bid_rule_hand_knowledge_tuples:
            # We've exhausted all possible bids.  This could only happen over 7NT-redoubled.
            # FIXME: This could become an OutOfSpacePass instead of being a manual hack here?
            assert history.last_contract().name == "7N" and history.last_non_pass().is_redouble()
            return Pass(), None, oracle.existing_knowledge.me

        all_priorities, _, _, _ = zip(*prioritiy_bid_rule_hand_knowledge_tuples)
        highest_priority = min(all_priorities)
        if highest_priority == priorities.InvalidBid:
            # Everything we found was invalid, we have nothing to say, sorry.
            # FIXME: Eventually we'll want to _log.warn here.
            return None, None, oracle.existing_knowledge.me

        highest_priority_tuples = [
            priority_tuple
            for priority_tuple in prioritiy_bid_rule_hand_knowledge_tuples
            if priority_tuple[0] == highest_priority
        ]
        if len(highest_priority_tuples) > 1:
            bid_and_rule_strings = ["%s from %s" % (bid, rule) for _, bid, rule, _ in highest_priority_tuples]
            _log.warn(
                "Multiple bids of priority %s:  %s  Choosing highest: %s"
                % (highest_priority, bid_and_rule_strings, highest_priority_tuples[-1][1])
            )
        # The knowledge based bidder should always use the exact same rule for bidding as it would for interpret
        priority, bid, rule, hand_knowledge = highest_priority_tuples[-1]
        return bid, rule, hand_knowledge
Example #2
0
 def __init__(self):
     self.explorer = CallExplorer()