def create_df_hand_five(): """create summary dataframe for all five card hands""" hand_type = EvalFive.hand_type uniq, idx = np.unique(hand_type, return_index=True) sorted_idx = np.sort(idx)[::-1] uniq_hand_type = hand_type[sorted_idx] min_rank = sorted_idx max_rank = np.r_[hand_type.size, sorted_idx[:-1]]-1 nb_rank = max_rank-min_rank+1 array_all_five_cards = create_array_all_five_fast() all_hand_rank = EvalFive.get_five_rank_fast(array_all_five_cards, keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank) bins = np.r_[sorted_idx[::-1], max_rank[0]] nb_occurence = np.histogram(all_hand_rank, bins)[0][::-1] proba = 1.0*nb_occurence/np.sum(nb_occurence) odd = 1.0/proba cum_proba = np.cumsum(proba) proba_exact = map(lambda x : Rational(x, array_all_five_cards.shape[0]), nb_occurence) df = pd.DataFrame(data=np.array([uniq_hand_type, nb_rank, min_rank, max_rank, nb_occurence, proba_exact, odd, proba, cum_proba]).T, columns=['HandType', 'NbHands', 'MinRank', 'MaxRank', 'NbOccurence', 'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba']) # df.ix[uniq.size] = ['All', hand_type.size, 0, max_rank[0], array_all_five_cards.shape[0], 1.0, 1.0, 1.0, np.nan] s = pd.Series(data=['All', hand_type.size, 0, max_rank[0], array_all_five_cards.shape[0], 1.0, 1.0, 1.0, np.nan], index=['HandType', 'NbHands', 'MinRank', 'MaxRank', 'NbOccurence', 'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba'], name=uniq.size) df = df.append(s) # df.to_pickle(os.path.join('Tables', 'df_hand_five.pd')) return df
def create_df_hand_five(): """create summary dataframe for all five card hands""" hand_type = EvalFive.hand_type uniq, idx = np.unique(hand_type, return_index=True) sorted_idx = np.sort(idx)[::-1] uniq_hand_type = hand_type[sorted_idx] min_rank = sorted_idx max_rank = np.r_[hand_type.size, sorted_idx[:-1]] - 1 nb_rank = max_rank - min_rank + 1 array_all_five_cards = create_array_all_five_fast() all_hand_rank = EvalFive.get_five_rank_fast(array_all_five_cards, keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank) bins = np.r_[sorted_idx[::-1], max_rank[0]] nb_occurence = np.histogram(all_hand_rank, bins)[0][::-1] proba = 1.0 * nb_occurence / np.sum(nb_occurence) odd = 1.0 / proba cum_proba = np.cumsum(proba) proba_exact = map(lambda x: Rational(x, array_all_five_cards.shape[0]), nb_occurence) df = pd.DataFrame(data=np.array([ uniq_hand_type, nb_rank, min_rank, max_rank, nb_occurence, proba_exact, odd, proba, cum_proba ]).T, columns=[ 'HandType', 'NbHands', 'MinRank', 'MaxRank', 'NbOccurence', 'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba' ]) # df.ix[uniq.size] = ['All', hand_type.size, 0, max_rank[0], array_all_five_cards.shape[0], 1.0, 1.0, 1.0, np.nan] s = pd.Series(data=[ 'All', hand_type.size, 0, max_rank[0], array_all_five_cards.shape[0], 1.0, 1.0, 1.0, np.nan ], index=[ 'HandType', 'NbHands', 'MinRank', 'MaxRank', 'NbOccurence', 'ProbaExact', 'Odd', 'ProbaApprox', 'CumulativeProba' ], name=uniq.size) df = df.append(s) # df.to_pickle(os.path.join('Tables', 'df_hand_five.pd')) return df
def test(): """display all EvalSeven tables, run getSevenRank_ evaluators on sample hands, compare getSevenRank_ with EvalFive.getSevenRank_""" global seven_cards, array_seven_cards, hr1, hr2, hr3, N, rank7, rank5, sparse_flush_rank, sparse_face_rank print '\n--------------- start EvalSeven.test' print '-------- EvalSeven tables' sparse_flush_rank = sparse.csc_matrix(flush_rank) print 'flush_rank = \n{}\n{}\nnb of nonzero elements = {}'.format(sparse_flush_rank.nonzero()[1], sparse_flush_rank.data, sparse_flush_rank.nonzero()[1].size) sparse_face_rank = sparse.csc_matrix(face_rank) print 'face_rank = \n{}\n{}\nnb of nonzero elements = {}'.format(sparse_face_rank.nonzero()[1], sparse_face_rank.data, sparse_face_rank.nonzero()[1].size) print 'flush_suit = \n{}'.format(flush_suit) print 'nb of elements different from -2 = {}'.format(np.array(flush_suit[flush_suit!=-2].size)) print '-------- EvalSeven getSevenRank_ evaluators sample runs' np.random.seed(99) array_seven_cards = np.array([np.random.choice(keys.DECK_SIZE, 7, replace=False) for k in xrange(10)]) print 'array_seven_cards=\n{}'.format(array_seven_cards) seven_cards = array_seven_cards[0] print 'seven_cards={}'.format(seven_cards) hr1 = get_seven_rank_simple(seven_cards) print '\nget_seven_rank_simple(seven_cards)={}'.format(hr1) hr2 = get_seven_rank_full(array_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, flush_rank, face_rank, flush_suit) print 'get_seven_rank_full(array_seven_cards)={}'.format(hr2) hr3 = get_seven_rank_fast(array_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, flush_rank, face_rank, flush_suit) print 'get_seven_rank_fast(array_seven_cards)={}'.format(hr3) print '-------- compare getSevenRank_ with EvalFive.get_seven_rank_ on all hands' t0 = timer() array_all_seven_cards = create_array_all_seven_fast() t1 = timer() print 'array_all_seven_cards time = {:8.4f} s'.format(t1-t0) t0 = timer() rank7 = get_seven_rank_fast(array_all_seven_cards[:], keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, flush_rank, face_rank, flush_suit) t1 = timer() print 'EvalSeven.get_seven_rank_fast time = {:8.4f} s'.format(t1-t0) N = 1000000*140 t0 = timer() rank5 = EvalFive.get_seven_rank_fast(array_all_seven_cards[:N], keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank) t1 = timer() print 'EvalFive.get_seven_rank_fast time = {:8.4f} s'.format(t1-t0) print 'result(EvalSeven.get_seven_rank_fast)==result(EvalFive.get_seven_rank_fast) ? {}'.format((rank5==rank7).all()) print '--------------- end EvalSeven.test'
def test(): """display all EvalSeven tables, run getSevenRank_ evaluators on sample hands, compare getSevenRank_ with EvalFive.getSevenRank_""" global seven_cards, array_seven_cards, hr1, hr2, hr3, N, rank7, rank5, sparse_flush_rank, sparse_face_rank print '\n--------------- start EvalSeven.test' print '-------- EvalSeven tables' sparse_flush_rank = sparse.csc_matrix(flush_rank) print 'flush_rank = \n{}\n{}\nnb of nonzero elements = {}'.format( sparse_flush_rank.nonzero()[1], sparse_flush_rank.data, sparse_flush_rank.nonzero()[1].size) sparse_face_rank = sparse.csc_matrix(face_rank) print 'face_rank = \n{}\n{}\nnb of nonzero elements = {}'.format( sparse_face_rank.nonzero()[1], sparse_face_rank.data, sparse_face_rank.nonzero()[1].size) print 'flush_suit = \n{}'.format(flush_suit) print 'nb of elements different from -2 = {}'.format( np.array(flush_suit[flush_suit != -2].size)) print '-------- EvalSeven getSevenRank_ evaluators sample runs' np.random.seed(99) array_seven_cards = np.array([ np.random.choice(keys.DECK_SIZE, 7, replace=False) for k in xrange(10) ], dtype=np.int32) print 'array_seven_cards=\n{}'.format(array_seven_cards) seven_cards = array_seven_cards[0] print 'seven_cards={}'.format(seven_cards) hr1 = get_seven_rank_simple(seven_cards) print '\nget_seven_rank_simple(seven_cards)={}'.format(hr1) hr2 = get_seven_rank_full(array_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, flush_rank, face_rank, flush_suit) print 'get_seven_rank_full(array_seven_cards)={}'.format(hr2) hr3 = get_seven_rank_fast(array_seven_cards, keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, flush_rank, face_rank, flush_suit) print 'get_seven_rank_fast(array_seven_cards)={}'.format(hr3) print '-------- compare getSevenRank_ with EvalFive.get_seven_rank_ on all hands' t0 = timer() array_all_seven_cards = create_array_all_seven_fast() t1 = timer() print 'array_all_seven_cards time = {:8.4f} s'.format(t1 - t0) t0 = timer() rank7 = get_seven_rank_fast(array_all_seven_cards[:], keys.CARD_FLUSH_KEY, keys.CARD_FACE_KEY, keys.CARD_SUIT, keys.SUIT_MASK, keys.SUIT_BIT_SHIFT, flush_rank, face_rank, flush_suit) t1 = timer() print 'EvalSeven.get_seven_rank_fast time = {:8.4f} s'.format(t1 - t0) N = 1000000 * 140 t0 = timer() rank5 = EvalFive.get_seven_rank_fast(array_all_seven_cards[:N], keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank) t1 = timer() print 'EvalFive.get_seven_rank_fast time = {:8.4f} s'.format(t1 - t0) print 'result(EvalSeven.get_seven_rank_fast)==result(EvalFive.get_seven_rank_fast) ? {}'.format( (rank5 == rank7).all()) print '--------------- end EvalSeven.test'
def compute_tables(): """compute all EvalSeven tables: flush_rank, face_rank, flush_suit""" global flush_rank, face_rank, flush_suit print "\n--------------- start EvalSeven.compute_tables" # local names face_key_seven = keys.FACE_KEY_SEVEN flush_key_seven = keys.FLUSH_KEY_SEVEN suit_key = keys.SUIT_KEY nb_face = keys.NB_FACE nb_suit = keys.NB_SUIT t0 = timer() # compute face_rank print 'start face_rank' for c1 in range(0, nb_face): for c2 in range(0, c1 + 1): for c3 in range(0, c2 + 1): for c4 in range(0, c3 + 1): for c5 in range(0, c4 + 1): for c6 in range(0, c5 + 1): for c7 in range(0, c6 + 1): # No 5, or more, same faces if ((c1 - c5 > 0) and (c2 - c6 > 0) and (c3 - c7 > 0)): hand_key = face_key_seven[ c1] + face_key_seven[ c2] + face_key_seven[ c3] + face_key_seven[ c4] + face_key_seven[ c5] + face_key_seven[ c6] + face_key_seven[ c7] face_rank[ hand_key] = EvalFive.get_seven_rank_fast( np.array([[ 4 * c1, 4 * c2, 4 * c3, 4 * c4, 4 * c5 + 1, 4 * c6 + 1, 4 * c7 + 1 ]]), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # compute flush_rank # Flush 7 cards print 'start flush_rank: 7 cards' for c1 in range(6, nb_face): for c2 in range(0, c1): for c3 in range(0, c2): for c4 in range(0, c3): for c5 in range(0, c4): for c6 in range(0, c5): for c7 in range(0, c6): hand_key = flush_key_seven[ c1] + flush_key_seven[c2] + flush_key_seven[ c3] + flush_key_seven[ c4] + flush_key_seven[ c5] + flush_key_seven[ c6] + flush_key_seven[c7] flush_rank[ hand_key] = EvalFive.get_seven_rank_fast( np.array([[ 4 * c1, 4 * c2, 4 * c3, 4 * c4, 4 * c5, 4 * c6, 4 * c7 ]]), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # Flush 6 cards print 'start flush_rank: 6 cards' for c1 in range(5, nb_face): for c2 in range(0, c1): for c3 in range(0, c2): for c4 in range(0, c3): for c5 in range(0, c4): for c6 in range(0, c5): hand_key = flush_key_seven[c1] + flush_key_seven[ c2] + flush_key_seven[c3] + flush_key_seven[ c4] + flush_key_seven[ c5] + flush_key_seven[c6] flush_rank[ hand_key] = EvalFive.get_seven_rank_fast( np.array([[ 4 * c1, 4 * c2, 4 * c3, 4 * c4, 4 * c5, 4 * c6, 1 ]]), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # Flush 5 cards print 'start flush_rank: 5 cards' for c1 in range(4, nb_face): for c2 in range(0, c1): for c3 in range(0, c2): for c4 in range(0, c3): for c5 in range(0, c4): hand_key = flush_key_seven[c1] + flush_key_seven[ c2] + flush_key_seven[c3] + flush_key_seven[ c4] + flush_key_seven[c5] flush_rank[hand_key] = EvalFive.get_five_rank_fast( np.array( [[4 * c1, 4 * c2, 4 * c3, 4 * c4, 4 * c5]], dtype=np.int32), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # Flush Suit table print 'start flush_suit' for c1 in range(0, nb_suit): for c2 in range(0, c1 + 1): for c3 in range(0, c2 + 1): for c4 in range(0, c3 + 1): for c5 in range(0, c4 + 1): for c6 in range(0, c5 + 1): for c7 in range(0, c6 + 1): hand_suit_key = (suit_key[c1] + suit_key[c2] + suit_key[c3] + suit_key[c4] + suit_key[c5] + suit_key[c6] + suit_key[c7]) flush_suit[hand_suit_key] = -1 for cand_suit in range(0, nb_suit): suit_count = ((c1 == cand_suit) + (c2 == cand_suit) + (c3 == cand_suit) + (c4 == cand_suit) + (c5 == cand_suit) + (c6 == cand_suit) + (c7 == cand_suit)) if (suit_count >= 5): flush_suit[hand_suit_key] = cand_suit t1 = timer() print 'compute_tables time = {:8.4f} s'.format(t1 - t0) print '--------------- end EvalSeven.compute_tables'
def compute_tables(): """compute all EvalSeven tables: flush_rank, face_rank, flush_suit""" global flush_rank, face_rank, flush_suit print "\n--------------- start EvalSeven.compute_tables" # local names face_key_seven = keys.FACE_KEY_SEVEN flush_key_seven = keys.FLUSH_KEY_SEVEN suit_key = keys.SUIT_KEY nb_face = keys.NB_FACE nb_suit = keys.NB_SUIT t0 = timer() # compute face_rank print 'start face_rank' for c1 in range(0, nb_face): for c2 in range(0, c1+1): for c3 in range(0, c2+1): for c4 in range(0, c3+1): for c5 in range(0, c4+1): for c6 in range(0, c5+1): for c7 in range(0, c6+1): # No 5, or more, same faces if ((c1-c5>0) and (c2-c6>0) and (c3-c7>0)): hand_key = face_key_seven[c1]+face_key_seven[c2]+face_key_seven[c3]+face_key_seven[c4]+face_key_seven[c5]+face_key_seven[c6]+face_key_seven[c7] face_rank[hand_key] = EvalFive.get_seven_rank_fast(np.array([[4*c1, 4*c2, 4*c3, 4*c4, 4*c5+1, 4*c6+1, 4*c7+1]]), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # compute flush_rank # Flush 7 cards print 'start flush_rank: 7 cards' for c1 in range(6, nb_face): for c2 in range(0, c1): for c3 in range(0, c2): for c4 in range(0, c3): for c5 in range(0, c4): for c6 in range(0, c5): for c7 in range(0, c6): hand_key = flush_key_seven[c1]+flush_key_seven[c2]+flush_key_seven[c3]+flush_key_seven[c4]+flush_key_seven[c5]+flush_key_seven[c6]+flush_key_seven[c7] flush_rank[hand_key] = EvalFive.get_seven_rank_fast(np.array([[4*c1, 4*c2, 4*c3, 4*c4, 4*c5, 4*c6, 4*c7]]), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # Flush 6 cards print 'start flush_rank: 6 cards' for c1 in range(5, nb_face): for c2 in range(0, c1): for c3 in range(0, c2): for c4 in range(0, c3): for c5 in range(0, c4): for c6 in range(0, c5): hand_key = flush_key_seven[c1]+flush_key_seven[c2]+flush_key_seven[c3]+flush_key_seven[c4]+flush_key_seven[c5]+flush_key_seven[c6] flush_rank[hand_key] = EvalFive.get_seven_rank_fast(np.array([[4*c1, 4*c2, 4*c3, 4*c4, 4*c5, 4*c6, 1]]), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # Flush 5 cards print 'start flush_rank: 5 cards' for c1 in range(4, nb_face): for c2 in range(0, c1): for c3 in range(0, c2): for c4 in range(0, c3): for c5 in range(0, c4): hand_key = flush_key_seven[c1]+flush_key_seven[c2]+flush_key_seven[c3]+flush_key_seven[c4]+flush_key_seven[c5] flush_rank[hand_key] = EvalFive.get_five_rank_fast(np.array([[4*c1, 4*c2, 4*c3, 4*c4, 4*c5]]), keys.FLUSH_KEY_FIVE, keys.FACE_KEY_FIVE, keys.CARD_SUIT, keys.CARD_FACE, EvalFive.flush_rank, EvalFive.face_rank)[0] # Flush Suit table print 'start flush_suit' for c1 in range(0, nb_suit): for c2 in range(0, c1+1): for c3 in range(0, c2+1): for c4 in range(0, c3+1): for c5 in range(0, c4+1): for c6 in range(0, c5+1): for c7 in range(0, c6+1): hand_suit_key = (suit_key[c1]+suit_key[c2]+suit_key[c3]+suit_key[c4]+suit_key[c5]+suit_key[c6]+suit_key[c7]) flush_suit[hand_suit_key] = -1 for cand_suit in range(0, nb_suit): suit_count = ((c1==cand_suit)+(c2==cand_suit)+(c3==cand_suit)+(c4==cand_suit)+(c5==cand_suit)+(c6==cand_suit)+(c7==cand_suit)) if (suit_count>=5): flush_suit[hand_suit_key] = cand_suit t1 = timer() print 'compute_tables time = {:8.4f} s'.format(t1-t0) print '--------------- end EvalSeven.compute_tables'