def reset(self): # North and South """ :param reshuffle: whether reshuffle the hands for the predeal seats :return: deal """ if len(self.scores_dicts) == 0: raise Exception("Please set the data in adavance") self.bidding_history = np.zeros(36, dtype=np.uint8) # 1C 1D 1H 1S 1N ... 7N (PASS - not considered) self.max_bid = -1 self.n_pass = 0 self.turn = self.bidding_seats[0] # the first one. self.done = False self.strain_declarer = {0: {}, 1: {}} self.group_declarer = -1 idx = self.loading_data_order[self.loading_count] predeal, self.rewards_table = self.pre_deals_dicts[idx], self.scores_dicts[idx] self.loading_count += 1 self.one_hot_deal = np.zeros((len(Seat), len(FULL_DECK)), dtype=np.uint8) for seat, hands in predeal.items(): self.one_hot_deal[seat] = one_hot_holding(hands) # one hot cards # update the deal self.deal = Deal.prepare(predeal) if self.loading_count >= len(self.loading_data_order): # reshuffle the indices self.loading_count = 0 self.loading_data_order = random.sample(list(np.arange(len(self.pre_deals_dicts))), len(self.pre_deals_dicts)) self.traverse_flag = True if self.debug: convert_hands2string(self.deal) # if not allocated, zero vector is returned. return (self.one_hot_deal[self.turn], self.bidding_history), {"turn": Seat[self.turn], "max_bid": self.max_bid, "traverse": self.traverse_flag}
def evaluate_new_sample(self, predeal_seats=None, declarer=None): # North and South """ :param predeal_seats: if not None, allocate cards to those seats. e.g. [0, 1] stands for North and East :param reshuffle: whether reshuffle the hands for the predeal seats :return: deal """ if predeal_seats is None: predeal_seats = self.bidding_seats predeal = {} random.shuffle(self.cards) i = 0 self.one_hot_deal = np.zeros((len(Seat), len(FULL_DECK)), dtype=np.uint8) for seat in sorted(predeal_seats): predeal[seat] = self.cards[i:i + len(Rank)] self.one_hot_deal[seat] = one_hot_holding( predeal[seat]) # one hot cards i += len(Rank) # shift the index self.deal = Deal.prepare(predeal) if self.debug: convert_hands2string(self.deal) rewards_dict = {} if declarer is not None: rewards = self.score_fn(dealer=self.deal, declarer=declarer, tries=self.nmc, mode=self.score_mode) rewards_dict[declarer] = rewards else: for s in self.bidding_seats: rewards = self.score_fn(dealer=self.deal, declarer=s, tries=self.nmc, mode=self.score_mode) rewards_dict[s] = rewards return predeal, rewards_dict, self.one_hot_deal[self.bidding_seats]
def reset(self, predeal_seats=None, reshuffle=True): # North and South """ :param predeal_seats: if not None, allocate cards to those seats. e.g. [0, 1] stands for North and East :param reshuffle: whether reshuffle the hands for the predeal seats :return: deal """ self.bidding_history = np.zeros( 36, dtype=np.uint8) # 1C 1D 1H 1S 1N ... 7N (PASS - not considered) self.max_bid = -1 self.n_pass = 0 self.turn = self.bidding_seats[0] # the first one. self.done = False self.strain_declarer = {0: {}, 1: {}} self.group_declarer = -1 if predeal_seats is None: predeal_seats = self.bidding_seats predeal = {} random.shuffle(self.cards) if reshuffle: # generate new hands for predeal seats. i = 0 self.one_hot_deal = np.zeros((len(Seat), len(FULL_DECK)), dtype=np.uint8) for seat in sorted(predeal_seats): predeal[seat] = self.cards[i:i + len(Rank)] self.one_hot_deal[seat] = one_hot_holding( predeal[seat]) # one hot cards i += len(Rank) # shift the index self.deal = Deal.prepare(predeal) if self.debug: convert_hands2string(self.deal) # if not allocated, zero vector is returned. return (self.one_hot_deal[self.turn], self.bidding_history), { "turn": Seat[self.turn], "max_bid": self.max_bid }
from deal import Deal import time start = time.time() # Strain 0: "C", 1: "D", 2: "H", 3: "S", 4: "N" # Suit 0: "S", 1: "H", 2: "D", 3: "C" predeal = {0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]} deal1 = Deal.prepare(predeal) Deal.score(dealer=deal1, level=5, strain=0, declarer=3, tries=100) print("%.2f seconds elapsed" % (time.time()-start))