def createList(self, data, location): #when there is no linked list and we want to add at a given position if (self.head is None and location > 0): return ("there is no link list") # when there is no node in the linked list if (self.head is None and location == 0): node1 = node(data) self.head = node1 self.tail = node1 self.nodeCount += 1 #when we want to add at the starting of the list elif (location == 0): node1 = node(data) node1.set_next(self.head) self.head = node1 self.nodeCount += 1 #when we want to add a node at the last position elif (location == self.nodeCount and location > 0): node1 = node(data) self.tail.set_next(node1) self.tail = node1 self.nodeCount += 1 #when we want to add a node in between head and tail else: node1 = node(data) tempNode = self.head for i in range(0, location - 1): tempNode = tempNode.get_next() node1.set_next(tempNode.get_next()) tempNode.set_next(node1) self.nodeCount += 1
def new_stage_incoming(curr_node, node_player0, node_player1, cards): # ob novem stagu mora začeti p0 (tudi če je ravno igral) if node_player0.infoSet[-1] != '|': node_player0.infoSet += "|" if node_player1.infoSet[-1] != '|': node_player1.infoSet += "|" # zdj preeidemo v nov node, ki je vezan na to kakšne karte padejo curr_game_stage = curr_node.infoSet.count("|") #preflop je stage 0 if curr_game_stage == 1: new_cards_ = "f" + poVrsti([cards[4], cards[5], cards[6]]) # flop elif curr_game_stage == 2: new_cards_ = "t" + str(cards[7]) # turn elif curr_game_stage == 3: new_cards_ = "r" + str(cards[8]) # river else: return "error3" # create nodes if necessary --> vedno kreiramo node v p0 in node_betting_map v p1 ker na začetku staga vedno začne p0 if new_cards_ not in node_player0.new_cards: node_player0.new_cards[new_cards_] = nodes.node(node_player0.infoSet) if new_cards_ not in node_player1.new_cards: node_player1.new_cards[new_cards_] = nodes.node_betting_map( node_player1.infoSet) return new_cards_
def nodeInformation(infoSet, player): # v nodemapu je drevo za vsak mozni zacetni hand files_curr_dir = [ f for f in listdir("./") if isfile(join("./", f)) ] # --> treba pogledat vsakič sprot ker sproti nastajajo novi fajli...mogoce po nekaj casa lahko zbrisem to vrstico if player == 0: file_name = "p0_" + infoSet + ".pkl" if file_name in files_curr_dir: with open(file_name, 'rb') as input: newNode = pickle.load(input) else: newNode = nodes.node_betting_map( "" ) # --> tuki ne rabmo node, ker p0 avtomatsko na začetku da big blind in dejansko ne začne elif player == 1: file_name = "p1_" + infoSet + ".pkl" if file_name in files_curr_dir: with open(file_name, 'rb') as input: newNode = pickle.load(input) else: newNode = nodes.node( "" ) # --> tuki rabmo node, ker p1 prvi igra v novi igri, saj ima p0 začetno stavo aka BIG BLIND + # --> b11 = znak za big blind else: return "error2" return newNode
def createList(self, data, location): node1 = node(data) if (self.head == None and location == 0): self.head = node1 self.tail = node1 self.nodeCount += 1 elif (location == 0): node1.set_next(self.head) self.head.set_prev(node1) self.head = node1 self.nodeCount += 1 elif (location >= self.nodeCount): node1.set_prev(self.tail) self.tail.set_next(node1) self.tail = node1 self.nodeCount += 1 else: tempNode = self.head for i in range(0, location - 1): tempNode = (tempNode.get_next()) node1.set_next(tempNode.get_next()) node1.set_prev(tempNode) tempNode.set_next(node1) node1.get_next().set_prev(node1) self.nodeCount += 1
def openfile(path): import nodes as nd import csv with open(path) as f: edges = [tuple(line) for line in csv.reader(f)] labels = nd.node(edges) idxs = dict(zip(labels, range(len(labels)))) iedges = [(idxs[e[0]], idxs[e[1]]) for e in edges] return iedges
def addEdge(self, fromNode, toNode): node1 = node(toNode) if (self.vertexList[fromNode] == None): self.vertexList[fromNode] = node1 else: tempNode = self.vertexList[fromNode] while (tempNode.get_next() is not None): tempNode = tempNode.get_next() tempNode.set_next(node1)
def createNode(self, data): node1 = node(data) if (self.head is None): self.head = node1 self.tail = node1 self.nodeCount += 1 else: self.tail.set_next(node1) self.tail = node1 self.nodeCount += 1
def makerandomtree(pc, maxdepth = 4, fpr = 0.5, ppr = 0.6): if random() < fpr and maxdepth > 0: f = choice(functions.getFunctions()) children = [makerandomtree(pc, maxdepth - 1, fpr, ppr) for i in range(f.childcount)] return nodes.node(f, children) elif random() < ppr: return nodes.paramnode(randint(0, pc - 1)) else: return nodes.constnode(randint(0, 255))
def createList(self, data, location): if (self.head is None and location > 0): print("error in circular list creation") return # when there is no node in list elif (self.head is None and location == 0): node1 = node(data) self.head = node1 self.tail = node1 node1.set_next(node1) self.nodeCount += 1 #when we want to add at the first position elif (location == 0): node1 = node(data) node1.set_next(self.head) self.head = node1 self.tail.set_next(node1) self.nodeCount += 1 # when we want to add at the last node elif (location == self.nodeCount): tempNode = self.head node1 = node(data) node1.set_next(self.head) self.tail.set_next(node1) self.tail = node1 self.nodeCount += 1 #when we want to add in between head and tail else: tempNode = self.head node1 = node(data) for i in range(0, location - 1): tempNode = tempNode.get_next() node1.set_next(tempNode.get_next()) tempNode.set_next(node1) self.nodeCount += 1
def make_adjacency(edges): import numpy as np import nodes as nd nods = nd.node(edges) size = len(nods) adjm = np.zeros((size,size)) #print adjm for i in edges: c = (i[0], i[1]) d = (i[1], i[0]) #print c adjm[c] = 1 adjm[d] = 1 return adjm
def make_adjacency(edges): import numpy as np import nodes as nd nods = nd.node(edges) size = len(nods) adjm = np.zeros((size, size)) # print adjm for i in edges: c = (i[0], i[1]) d = (i[1], i[0]) # print c adjm[c] = 1 adjm[d] = 1 return adjm
def createList(self, value, location): node1 = node(value) if (self.head == None and location > 0): print("no link list found to add the node at given location") #when there is no node in the list elif (self.head == None and location == 0): self.head = node1 self.tail = node1 node1.set_next(node1) node1.set_prev(node1) self.nodeCount += 1 # when we want to add a node in 0th position elif (location == 0): node1.set_next(self.head) node1.set_prev(self.tail) self.head.set_prev(node1) self.tail.set_next(node1) self.nodeCount += 1 #when we want to add at last position elif (location >= self.nodeCount): node1.set_prev(self.tail) self.tail.set_next(node1) self.head.set_prev(node1) node1.set_next(self.head) self.tail = node1 self.nodeCount += 1 #when we want to add node in between else: tempNode = self.head for i in range(0, location - 1): tempNode = tempNode.get_next() node1.set_next(tempNode.get_next()) node1.set_prev(tempNode) tempNode.set_next(node1) node1.get_next().set_prev(node1) self.nodeCount += 1
def generate(num_of_games=100000): file_path = "Analysis/Bot/datasets/dataset.txt" cards = [2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14] # Ta node uporabimo ko naš node ni naučen --> z več učenja tega ne bomo več potrebovali node_random = nodes.node("b1") for i in range(len(node_random.strategySum)): node_random.strategySum[i] = 1 / len(node_random.strategySum) #print("How many games do we generate?") counter = 0 start_time = time.time() while counter < int(num_of_games): counter += 1 print(counter) # ponastavimo infoset max_pot = 2 * ZACETNO_STANJE # več od tega ne more biti, ker sta oba bota porabila svoj denar #zmešamo in razdelimo karte random.shuffle(cards) old_pot = BIG_BLIND # p0 big blind new_pot = BIG_BLIND # Write to file f = open(file_path, "a") f.write("Game started at: " + str(datetime.datetime.now()) + "\n") f.write("Game ID: " + str(counter) + " " + str(SMALL_BLIND) + "/" + str(BIG_BLIND) + " Hold'em\n") f.write("Seat 1 is the button") f.write("Seat 1: Bot1 (50).\n") f.write("Seat 2: Bot2 (50).\n") f.write("Player Bot1 has big blind (1)\n") f.write("Player Bot1 received a card " + num_to_card(cards[0]) + ".\n") f.write("Player Bot1 received a card " + num_to_card(cards[1]) + ".\n") f.write("Player Bot2 received a card " + num_to_card(cards[2]) + ".\n") f.write("Player Bot2 received a card " + num_to_card(cards[3]) + ".\n") f.close() #init the bots bot_0_cards = cfr_poker.poVrsti([cards[0], cards[1]]) bot_1_cards = cfr_poker.poVrsti([cards[2], cards[3]]) try: bot_0_node = cfr_poker.nodeInformation(bot_0_cards, 0) except: bot_0_node = node_random try: bot_1_node = cfr_poker.nodeInformation(bot_1_cards, 1) except: bot_1_node = node_random bot_in_action = 1 # ker ima p0 big blind # Init the variables global game_infoset game_infoset = "" opponent_bet = 0 isRiver = False game_ended = False winner = "none" global better_cards_p0 better_cards_p0 = cfr_poker.betterCards(cards, 0) a = better_cards_p0 global better_cards_p1 better_cards_p1 = cfr_poker.betterCards(cards, 1) #smo v tem loopu, dokler player spet ni konec igre while(not game_ended): debug = game_infoset if payoff(game_infoset, old_pot) != "continue": payoff_value = payoff(game_infoset, old_pot) winner = bot_in_action if payoff_value > 0 else (1-bot_in_action) #f.write("Bot" + str(winner) + " won") # glede na playerja veš kdo je zmagu game_ended = True break elif nodes.isNewStage(game_infoset): # Dodamo '|' v novem stagu game_infoset += "|" # Pokazemo nove karte curr_node = bot_0_node if bot_in_action == 0 else bot_1_node try: new_cards_ = cfr_poker.new_stage_incoming(curr_node, bot_0_node, bot_1_node, cards) except: new_cards_ = "err" try: bot_0_node = bot_0_node.new_cards[new_cards_] except: bot_0_node = node_random try: bot_1_node = bot_1_node.new_cards[new_cards_] except: bot_1_node = node_random # Ponastavimo variable bot_in_action = 0 opponent_bet = 0 bot_bet = 0 # Zapišemo v file game_stage = game_infoset.count("|") f = open(file_path, "a") if game_stage == 1: f.write("*** FLOP ***: [" + num_to_card(cards[4]) + " " + num_to_card(cards[5]) + " " + num_to_card(cards[6]) + "]\n") if game_stage == 2: f.write("*** TURN ***: [" + num_to_card(cards[7]) + "]\n") if game_stage == 3: f.write("*** RIVER ***: [" + num_to_card(cards[8]) + "]\n") isRiver = True f.close() # Tukaj sedaj zamenjamo node node = bot_0_node if bot_in_action == 0 else bot_1_node bot_action, action_info, bot_bet = bot_action_fun(node, old_pot, opponent_bet) action_num = int(action_info[-1]) old_pot, new_pot = cfr_poker.new_pot_amount(game_infoset, old_pot, new_pot, action_num) if new_pot*2 >= 100: bot_action = "allin" f = open(file_path, "a") f.write("Player Bot" + str(bot_in_action) + " " + bot_action + "s") f.write(" (" + str(bot_bet) + ")\n") if (bot_action != "fold" and bot_action != "check") else f.write("\n") f.close() game_infoset += action_info # Posodobimo node debug = game_infoset try: bot_0_node = bot_0_node.betting_map[action_info] except: bot_0_node = node_random try: bot_1_node = bot_1_node.betting_map[action_info] except: bot_1_node = node_random opponent_bet = bot_bet bot_bet = 0 bot_in_action = 1 - bot_in_action # Zaključena igra, sedaj napišemo summary f = open(file_path, "a") f.write("------ Summary ------\n") f.write("Board: [" + num_to_card(cards[4]) + " " + num_to_card(cards[5]) + " " + num_to_card(cards[6]) + " " + num_to_card(cards[7]) + " " + num_to_card(cards[8]) + "]\n") if isRiver: winner_cards = num_to_card(cards[0]) + ", " + num_to_card(cards[1]) if winner==0 else num_to_card(cards[2]) + ", " + num_to_card(cards[3]) f.write("Player Bot" + str(winner) + " shows cards: " + winner_cards + "\n") f.write("Player Bot" + str(1-winner) + " does not show cards\n") else: f.write("Player Bot" + str(winner) + " does not show cards\n") f.write("Player Bot" + str(1-winner) + " does not show cards\n") f.write("Game ended at: " + str(datetime.datetime.now()) + "\n\n") f.close() print("Čas izvajanja programa: ", (time.time() - start_time), " sekund. To je ", (time.time() - start_time)/60," minut, kar je ", (time.time() - start_time)/60/int(num_of_games), "minut na igro")
def __init__(self, edges): import nodes as nd self.nodes = nd.node(edges) self.edges = edges
def path_find(the_map, xA, yA, thetaA, actionA, xB, yB, thetaB, actionB): closed_nodes_map = np.zeros(shape=the_map.shape) open_nodes_map = np.zeros(shape=the_map.shape) dir_map = {} explored_nodes_count = 0 pq = [[], []] pqi = 0 n0 = node(xA, yA, thetaA, actionA) n0.updatePriority(xB, yB, thetaB, actionB) heappush(pq[pqi], n0) open_nodes_map[actionA][thetaA][yA][xA] = n0.priority while len(pq[pqi]) > 0: n1 = pq[pqi][0] n0 = node(n1.x, n1.y, n1.theta, n1.action, n1.distance, n1.priority) x = n0.x y = n0.y theta = n0.theta action = n0.action heappop(pq[pqi]) open_nodes_map[action][theta][y][x] = 0 closed_nodes_map[action][theta][y][x] = 1 if x == xB and y == yB and theta == thetaB: path = [] while not (x == xA and y == yA and theta == thetaA and action == actionA): td_node = dir_node(x, y, theta, action) parent = dir_map[td_node] path.append(parent) x = parent.x y = parent.y theta = parent.theta action = parent.action return (path, explored_nodes_count) dx, dy, dtheta, daction = n0.get_transition_states() for i in range(len(dx)): xdx = dx[i] ydy = dy[i] thetadtheta = dtheta[i] actiondaction = daction[i] if not (xdx < 0 or xdx > the_map.shape[3] - 1 or ydy < 0 or ydy > the_map.shape[2] - 1 or the_map[actiondaction][thetadtheta][ydy][xdx] == 1 or closed_nodes_map[actiondaction][thetadtheta][ydy][xdx] == 1): m0 = node(xdx, ydy, thetadtheta, actiondaction, n0.distance, n0.priority) m0.nextMove(action, i) m0.updatePriority(xB, yB, thetaB, actionB) if open_nodes_map[actiondaction][thetadtheta][ydy][xdx] == 0: explored_nodes_count += 1 open_nodes_map[actiondaction][thetadtheta][ydy][xdx] = m0.priority heappush(pq[pqi], m0) md_node = dir_node(m0.x, m0.y, m0.theta, m0.action) nd_node = dir_node(n0.x, n0.y, n0.theta, n0.action) dir_map[md_node] = nd_node elif open_nodes_map[actiondaction][thetadtheta][ydy][xdx] > m0.priority: open_nodes_map[actiondaction][thetadtheta][ydy][xdx] = m0.priority md_node = dir_node(m0.x, m0.y, m0.theta, m0.action) nd_node = dir_node(n0.x, n0.y, n0.theta, n0.action) dir_map[md_node] = nd_node while not (pq[pqi][0].x == xdx and pq[pqi][0].y == ydy): heappush(pq[1 - pqi], pq[pqi][0]) heappop(pq[pqi]) heappop(pq[pqi]) if len(pq[pqi]) > len(pq[1 - pqi]): pqi = 1 - pqi while len(pq[pqi]) > 0: heappush(pq[1 - pqi], pq[pqi][0]) heappop(pq[pqi]) pqi = 1 - pqi heappush(pq[pqi], m0) return ('', explored_nodes_count)
def kreiraj_sinove(self, curr_node, node_player0, node_player1, p0_nextTurn, player): # Če so nodi computerani, potem ne kreiramo na novo dreves # Še vseeno moramo kreirati drevesa v non_curr_node, saj proti drugemu playerju lahko ta player z drugačnimi kartami igra drugače --> zato njih moramo brisat is_not_computed = not curr_node.computed_node num_actions = nodes.num_actions(curr_node.infoSet, self.NUM_ACTIONS) for i in range(num_actions): oznaka_stave = "b" + str(i) new_infoset = curr_node.infoSet + oznaka_stave new_p0_nextTurn = p0_nextTurn if new_p0_nextTurn == False: new_p0_nextTurn = nodes.isNewStage(new_infoset) if player == 0: if isTerminalState( new_infoset): #terminal state --> naredimo payoff node if oznaka_stave not in node_player0.betting_map and is_not_computed: node_player0.betting_map[ oznaka_stave] = nodes.node_payoff(new_infoset) if oznaka_stave not in node_player1.betting_map: node_player1.betting_map[ oznaka_stave] = nodes.node_payoff(new_infoset) elif nodes.isNewStage( new_infoset ): #new stage state --> naredimo new cards infose if oznaka_stave not in node_player0.betting_map and is_not_computed: node_player0.betting_map[ oznaka_stave] = nodes.node_new_cards(new_infoset) if oznaka_stave not in node_player1.betting_map: node_player1.betting_map[ oznaka_stave] = nodes.node_new_cards(new_infoset) else: #sicer naredimo navaden betting node if oznaka_stave not in node_player0.betting_map and is_not_computed: node_player0.betting_map[oznaka_stave] = nodes.node( new_infoset ) if new_p0_nextTurn else nodes.node_betting_map( new_infoset) if oznaka_stave not in node_player1.betting_map: node_player1.betting_map[oznaka_stave] = nodes.node( new_infoset ) if not new_p0_nextTurn else nodes.node_betting_map( new_infoset) elif player == 1: if isTerminalState( new_infoset): #terminal state --> naredimo payoff node if oznaka_stave not in node_player0.betting_map: node_player0.betting_map[ oznaka_stave] = nodes.node_payoff(new_infoset) if oznaka_stave not in node_player1.betting_map and is_not_computed: node_player1.betting_map[ oznaka_stave] = nodes.node_payoff(new_infoset) elif nodes.isNewStage( new_infoset ): #new stage state --> naredimo new cards infose if oznaka_stave not in node_player0.betting_map: node_player0.betting_map[ oznaka_stave] = nodes.node_new_cards(new_infoset) if oznaka_stave not in node_player1.betting_map and is_not_computed: node_player1.betting_map[ oznaka_stave] = nodes.node_new_cards(new_infoset) else: #sicer naredimo navaden betting node if oznaka_stave not in node_player0.betting_map: node_player0.betting_map[oznaka_stave] = nodes.node( new_infoset ) if new_p0_nextTurn else nodes.node_betting_map( new_infoset) if oznaka_stave not in node_player1.betting_map and is_not_computed: node_player1.betting_map[oznaka_stave] = nodes.node( new_infoset ) if not new_p0_nextTurn else nodes.node_betting_map( new_infoset)