Beispiel #1
0
    def simulate_auction_np(self, auction, states, sim_bid_index=-1):
        sim_auction = auction[:]
        auction_out_bids = []
        sim_states = states[:]

        padded_auction = (['PAD_START'] * max(0, 3 - len(auction))) + auction

        bid_index = 0
        while not bidding.auction_over(sim_auction):
            i = len(sim_auction) % 4

            out_bid_np, next_state = self.next_bid_np(sim_auction, sim_states[i])

            sim_states[i] = next_state

            auction_out_bids.append(out_bid_np)

            if bid_index == sim_bid_index:
                # set the max bid to 0 and sample
                out_bid_np[0, np.argmax(out_bid_np)] = 0
                sim_auction.append(bidding.sample_bid(padded_auction, out_bid_np[0]))
            else:
                sim_auction.append(bidding.bid_max_bid(auction, out_bid_np[0]))

            bid_index += 1

        return sim_auction, auction_out_bids
Beispiel #2
0
 def best_auction(self, auction, states, n=100):
     self.cache = {}
     best_auction = auction[:]
     while not bidding.auction_over(best_auction):
         score, bid = self.best_bid(best_auction, states, n)
         best_auction.append(bid)
     return score, best_auction
Beispiel #3
0
def add_next_bid(line):
    _, deal_data = parse_line(line)

    if bidding.auction_over(deal_data.auction):
        return line

    bid = bidder.next_bid(deal_data, deal_data.auction)

    return line + bid_to_jack_format(bid)
Beispiel #4
0
    def simulate_auction(self, auction, states, max_bid=False):
        #import pdb; pdb.set_trace()

        sim_auction = auction[:]
        sim_states = states[:]
        
        while not bidding.auction_over(sim_auction):
            i = len(sim_auction) % 4
            bids, next_state = self.simulate_bid(sim_auction, sim_states[i], 1, max_bid)
            sim_states[i] = next_state
            sim_auction.append(bids[0])
        return sim_auction
Beispiel #5
0
def correspondence(team):
    bidder = Bidder('bw5c', './bw5c_model/bw5c-500000')

    for line in sys.stdin:
        #for line in open('/home/ldali/datasets/jack/match/Match000.1'):
        line = line.strip()
        table, deal_data = parse_line(line)

        # if '#:1736' in line and 'T:1' in line:
        #     import pdb; pdb.set_trace()

        if is_my_turn(team, table,
                      deal_data.auction) and not bidding.auction_over(
                          deal_data.auction):
            bid = bidder.next_bid(deal_data, deal_data.auction)
            sys.stdout.write(line)
            # if not line.endswith('A:'):
            #     sys.stdout.write(' ')
            sys.stdout.write(bid_to_jack_format(bid))
            sys.stdout.write('\n')
        else:
            print(line)
Beispiel #6
0
    def best(self, auction, states, best_score_ns, best_score_ew, potential_ns, potential_ew):
        if bidding.auction_over(auction):
            score = bidding.get_score(bidding.get_contract(auction), self.contracts, self.deal_data.vuln_ns, self.deal_data.vuln_ew)
            #print('returning', auction, score)
            return auction, score
        
        states_copy = states[:]
        
        hand_i = len(auction) % 4
        side = hand_i % 2
        is_side_vuln = [self.deal_data.vuln_ns, self.deal_data.vuln_ew][side]
        agg_fun = [max, min][side]

        self.deal_data.auction = auction
        X, _ = self.deal_data.get_binary(16)

        n_time_steps = len([bid for bid in auction if bid != 'PAD_START']) // 4
        x_input = X[hand_i:hand_i+1, n_time_steps]

        bid_np, next_state = self.model(x_input, states_copy[hand_i])

        states_copy[hand_i] = next_state

        b_auc, b_score_ns, b_score_ew, b_p = None, best_score_ns, best_score_ew, 0
        
        # if tuple(auction) == ('1H', 'PASS', '2N', 'PASS', '3S', 'PASS'):
        #     import pdb; pdb.set_trace()

        options = self.get_options(auction, bid_np[0])
        #print("options", options)
        
        for bid, p in options:

            contract_so_far = bidding.get_contract(auction + [bid])

            new_potential_ns = [(c, s) for c, s in potential_ns if not bidding.is_higher_contract(contract_so_far, c)] if contract_so_far is not None else potential_ns
            new_potential_ew = [(c, s) for c, s in potential_ew if not bidding.is_higher_contract(contract_so_far, c)] if contract_so_far is not None else potential_ew
            
            # [(contract_so_far, self.contracts[contract_so_far][int(is_side_vuln)])] + 
            best_possible = best_possible_score((new_potential_ns if side == 0 else new_potential_ew), agg_fun)
            best_so_far = b_score_ns if side == 0 else b_score_ew

            #print(auction, bid)
            
            if not is_better(best_possible, best_so_far, side):
                #print('{} found nothing better than {}, best possible {}'.format(side, best_so_far, best_possible))
                if bid not in ('PASS', 'X'):
                    continue


            #print('{} best so far {}, best possible {}'.format(side, best_so_far, best_possible))
            
            bid_auction, bid_score = self.best(auction + [bid], states_copy, b_score_ns, b_score_ew, new_potential_ns, new_potential_ew)        

            if b_auc is None:
                b_auc, b_score_ns, b_score_ew, b_p = bid_auction, bid_score, bid_score, p
                continue

            same_score = (bid_score == best_so_far)
            if same_score:
                if p > b_p:
                    b_auc, b_score_ns, b_score_ew, b_p = bid_auction, bid_score, bid_score, p
                    continue

            #better_score = (bid_score > b_score) if hand_i % 2 == 0 else (bid_score < b_score)
            if is_better(bid_score, best_so_far, side):
                b_auc, b_score_ns, b_score_ew, b_p = bid_auction, bid_score, bid_score, p

        #import pdb; pdb.set_trace()

        return b_auc, b_score_ns