def sampleTransitionProbs_DB( n_samples, bucket_percentiles ) : dek = Deck() conn1 = db.Conn("localhost") conn2 = db.Conn("localhost") q = """select cboard,aboard from REPRESENTATIVES""" for cboard, aboard in conn1.query(q) : dek.shuffle() aboard = listify( aboard ) dek.remove( aboard ) for next_card in dek.cards : next_board = aboard + [next_card]
#last is 3, other two aren't #if actions[3] != 3 or sum([t == 3 for t in actions[1:3]]) >= 1 : #non three if sum([t == 3 for t in actions[1:]]) > 0 : #if actions[1:] == [3,3,3] : #print "boring all checks" continue else : print "\n=======================================",line_num ntotal += 1 exchanged = int(splt[12]) board = deck.listify( splt[14] ) belief_assmnt = inf.BktAssmnt() for street, (kp1,kp2) in enumerate([(0,1),(3,4),(6,7),(9,10)]) : belief_assmnt.set( street, (int(splt[kp1]),int(splt[kp2])) ) print "actions: ", actions print "board: ", board print "belief assignment: ", belief_assmnt print "exchaged: ", exchanged evidence = inf.setupEvidence(board,actions) ################################################################### ########## Inference / Prediction ###################################################################
def registerRevealedPockets(self, player_names, pockets): # TODO: handle preflop strength via some table for street in range(self.street): # for street in range(len(self.past_actions)) : self.buckets.append([0] * len(player_names)) for player_name, pocket in zip(player_names, pockets): pix = self.players.index(player_name) if street == 0: street_name = "preflop" q = """select memberships from %s%s where pocket = '%s'""" % ( BUCKET_TABLE_PREFIX, street_name.upper(), canonicalize(pocket), ) else: if street == 1: board = self.board[:3] street_name = "flop" elif street == 2: board = self.board[:4] street_name = "turn" elif street == 3: board = self.board street_name = "river" cboard = collapseBoard(board) q = """select aboard from REPRESENTATIVES where cboard = '%s'""" % ( cboard ) # print q try: [[aboard]] = self.conn.query(q) except Exception as ve: self.ratio_file.write("%s\n\n" % q) aboard = listify(aboard) # pocket:board::apocket:aboard # print "board",board # print "aboard", aboard apocket = symmetricComplement(board, pocket, aboard) # apocket = 'AhAs' # print "pocket",pocket # print "apocket", apocket q = """select memberships from %s%s where cboard = '%s' and pocket = '%s'""" % ( BUCKET_TABLE_PREFIX, street_name.upper(), cboard, apocket, ) # print q try: [[memberships]] = self.conn.query(q) except Exception as ve: message = "cboard: %s\naboard: %s\npocket: %s\n\n" % (cboard, aboard, pocket) self.ratio_file.write(message) ve.message = message raise ve # TODO # eventually the beliefs should be a continuous node, # for now let's just # cram it into the closest to the average memberships = [float(t) for t in memberships.split(":")] # print "memberships: ", memberships # print "membs", memberships # we want the buckets to be from 1->N, not 0->N-1 w = [(i + 1) * m for i, m in enumerate(memberships)] # print "w:", w bucket = int(round(sum(w))) # print "bucket,", bucket self.buckets[street][pix] = bucket
def log2Nodes( filename, focus_player, focus_position ) : aggActionsMap = idp.buildAAS2I() indActionsMap = idp.buildIAS2I() print "paring: ", filename fin = open(filename) #TODO: glean small blind from, or always the same? #button is the second player, small blind splt = filename.split('.') players = [ splt[1], splt[2] ] run = int(splt[3][4:]) perm = int(splt[4][5:]) #if perm==1, button/dealer is the first player in the filename if perm == 1 : button = 0 else : button = 1 #if street has too many rounds of betting, throw it out MAX_MICRO = 2 n_thrown_out = 0 #sum of BBs the thrown out hands involved amt_thrown_out = 0 focus_start_on_button = button == players.index(focus_player) want_to_be_button = focus_position == "button" use_evens = focus_start_on_button == want_to_be_button if use_evens : if perm == 1 : ordered_players = [1,0] else : ordered_players = [0,1] else : if perm == 1 : ordered_players = [0,1] else : ordered_players = [1,0] preflop_ordered_players = [ordered_players[1],ordered_players[0]] #ordered_players defines indices into table.players #such that the first to act player shows up on the left #side of the network focus_player_ix = players.index(focus_player) want_to_be_button = int(focus_position == 'button') if focus_player_ix == want_to_be_button : ordered_players2 = [0,1] else : ordered_players2 = [1,0] assert ordered_players == ordered_players2 #stacks are always reset to 20000 at the start of every game pockets = [['__','__'],['__','__']] #when someone folds, +/-100 is exchanged sb = 50 tbl = table.Table( ID="%s_%s_%d_%d" % (players[0], players[1], run, perm ), small_blind=sb ) header = fin.readline() for i in range(3) : burn = fin.readline() for line_num, line in enumerate(fin.readlines()) : #print "LINE NUM:", line_num is_even = line_num % 2 == 0 if( is_even != use_evens ) : continue #print line splt = line.strip().split(':') if splt[0] == "SCORE" : #do something with total score? break game_id = int(splt[1]) #print "GAME_ID:", game_id #if game_id % 100 == 0 : #print "GAME_ID:", game_id action_strings = splt[2].strip('/').split('/') card_strings = splt[3].strip('/').split('/') #amount won/lost, as multiple of BB #using pot size instead amt = abs(int(splt[4].split('|')[0])) amt_exchanged = int( round ( amt / float(sb*2) ) ) player_order = splt[5].split("|") button_player = player_order[1] button = players.index(button_player) ##hack to make it go faster ##we are only interested in 4 round hands #has_showdown = 'f' not in action_strings[len(action_strings)-1] #if not has_showdown : #continue tbl.newHand(players, pockets, [20000,20000], button) #mark the round where an allin occurred allin_round = 42 prev_act = 'blinds' for street, (action_string, card_string) in enumerate(zip( action_strings, card_strings)) : #print "Street,action_string,card_stringL ", street, action_string, card_string if street > 0 : #print "card_string: " , card_string tbl.advanceStreet( listify(card_string) ) else : #force the first two 'calls' #registerAction translates them as the blind posting tbl.registerAction('c') tbl.registerAction('c') tbl.advanceStreet( ['down','cards'] ) ix = 0 while ix < len(action_string) : act = action_string[ix] #'c' is overloaded, when no prev bet it means chec'k' if act == 'c' and (prev_act != 'r' and \ prev_act != 'b' and \ prev_act != 'blinds') : act = 'k' tbl.registerAction( act ) ix += 1 elif act == 'r' : #iterate through string to extract the bet amount ints = [] ix += 1 while action_string[ix] not in ['r','c','f'] : ints.append( action_string[ix] ) ix += 1 bet_amount = int(''.join(ints)) ##correct overbets #stack_amount = tbl.stacks[tbl.action_to] #if bet_amount > stack_amount : #bet_amount = stack_amount #act = 'a' #allin_round = street #if prev_act == 'b' or prev_act == 'r' : #raises are really 'raise the pot to' amts tbl.registerAction( 'rt' , bet_amount ) #else : #tbl.registerAction( 'b', bet_amount ) #true call or fold elif act == 'c' or act == 'f' : tbl.registerAction( act ) ix += 1 else : assert False prev_act = act #all done iterating through the action/card lists tbl.advanceStreet(False) #print tbl.active_actions n_betting_rounds = min( [len(action_strings), allin_round+1] ) has_showdown = 'f' not in action_strings[n_betting_rounds-1] #if has_showdown : pockets = [listify(p) for p in card_strings[0].split('|')] if register_pockets : tbl.registerRevealedPockets( player_order, pockets ) else: for i in range(n_betting_rounds) : tbl.buckets.append([-1,-1]) #if has_showdown : #assert tbl.pot/2 == amt #else : #pass #TODO #end true parsing(), what follows is processing() #emit a training instance for each possible network, given #the number of rounds in this hand #if n_betting_rounds==3, we can emit nodes for pre,flop,and turn training_instance = [] for betting_round in range(1,n_betting_rounds+1) : #the order is flipped for the preflop round if betting_round == 1 : oplayers = preflop_ordered_players else : oplayers = ordered_players street = betting_round - 1 #attach bucket nodes for pix in ordered_players : training_instance.append( tbl.buckets[street][pix] ) #attach merged-action node too_many_micro_rounds = False individual_actions = [] #check for games with too much back and forth betting for pix in oplayers : n_micro_rounds = len(tbl.active_actions[street][pix]) too_many_micro_rounds = n_micro_rounds > MAX_MICRO if too_many_micro_rounds : break if too_many_micro_rounds : #too_many_micro_rounds, go onto next hand/line n_thrown_out += 1 amt_thrown_out = amt_exchanged break for micro_round in [0,1] : for pix in oplayers : n_micro_rounds = len(tbl.active_actions[street][pix]) #the network expects some number of micro rounds #in the final street if micro_round >= n_micro_rounds : aa = DUMMY_ACTION else : aa = tbl.active_actions[street][pix][micro_round] individual_actions.append(aa) action_str = ','.join( individual_actions ) ID = idp.lookupAggActionID( aggActionsMap, action_str, street ) #print ID, action_str training_instance.append( ID ) board_str = ''.join(card_strings[1:betting_round]) #print "board_str: ", board_str #amt_exchanged = int( round (tbl.pot / float(2*sb) ) ) #TODO yield both belief-layer nodes and action-layer nodes yield [(game_id,betting_round,has_showdown),\ list(training_instance), \ amt_exchanged, \ board_str] print "n_thrown_out: " , n_thrown_out print "amt_thrown_out: ", amt_thrown_out
def testSymmetric() : conn = db.Conn("localhost") tests = [] #cboard is 455_p_r #representative is 4h5d5c board = ['4d','5h','5c'] pocket = ['5d','7s'] tests.append( (board,pocket,'FLOP') ) #cboard is 9TQ_h_2fxox #rep is 9hQhTd board = ['9s','Qs','Tc'] pocket = ['7h','Th'] tests.append( (board,pocket,'FLOP') ) #cboard is 34QK_h_3foxxx #rep is 3h3dQdKd board = ['3s','3h','Qh','Kh'] pocket = ['7h','Tc'] tests.append( (board, pocket, 'TURN') ) #cboard is #rep is 2h 4h 7d 9d Ad board = ['2c','4h','7d','9h','Ah'] pocket = ['3c','Kh'] tests.append( (board,pocket,'RIVER') ) #cboard is 2335K_p_4f #2h3h5hKh3d board = ['2c','3d','3c','5c','Kc'] pocket = ['5s','Qc'] tests.append( (board,pocket,'RIVER') ) board = ['5c','5d','7c','7d','Qs'] pocket = ['6c','Qh'] tests.append( (board,pocket,'RIVER') ) for (board,pocket,street_name) in tests : cboard = collapseBoard( board ) #print "cboard", cboard q = """select aboard from REPRESENTATIVES where cboard = '%s'""" % \ (cboard) [[aboard]] = conn.query(q) #print "board", board #print "aboard", listify(aboard) #print "pocket", pocket pocketp = listify( symmetricComplement( board, \ pocket,\ listify(aboard) ) ) #print "pocketp: " , pocketp ehs2 = rollout.computeSingleEHS2( pocket, board ) print ehs2 q = """select ehs2 from EHS2_%s where cboard = '%s' and pocket = '%s'""" % (street_name, \ cboard, \ canonicalize(pocketp) ) [[ehs2p]] = conn.query(q) ehs2p = float(ehs2p) print ehs2p assert round(ehs2,4) == round(ehs2p,4)