def minimax(position, depth, alpha, beta, player, maxp, playerObj1, pos): # if(position is None): # return(0) res = arena.check_for_win(position.boardpos, pos,player) if(not res): res = arena.check_for_win(position.boardpos, pos, gametree.toggle(player)) if(res or (position.leaf) or (position.endPos) or (depth == 0)): if(maxp != player): return(-(10**(depth))*evaluation(playerObj1, position, player)) return((10**depth)*evaluation(playerObj1, position, player)) if(maxp == player): maxeval = -INF for pos,child in enumerate(position.children): if(child is not None): el = minimax(child, depth-1, alpha, beta, gametree.toggle(player), maxp, playerObj1, pos) maxeval = max(maxeval, el) alpha = max(alpha, el) if(beta <= alpha): break return(maxeval) else: mineval = INF for pos, child in enumerate(position.children): if(child is not None): el = minimax(child, depth-1, alpha, beta, gametree.toggle(player), maxp, playerObj1, pos) mineval = min(mineval, el) beta = min(beta, el) if(beta <= alpha): break return(mineval)
def _play_gat(self): playerObj1 = Temp() # fd = open("BAGENT.dna", 'wb') # pickle.dump(playerObj1, fd) # fd.close() oldpos = root.boardpos #move, root = movemaker.acceptmove(root, player) move = self.c root = root.childfren[self.c] gameOver = arena.change_vals(oldpos, move, player) player = gametree.toggle(player) if (not gameOver): oldpos = root.boardpos move, root = movemaker.move(playerObj1, root, player) gameOver = arena.change_vals(oldpos, move, player) player = gametree.toggle(player) if (gameOver): # print("Agent {} won with fitness {}".format(playerObj1.agentID, playerObj1.fitness)) pass
def referee(move, e): o = gt.toggle(e) move.players = dict() move.players[e] = [] move.players[e].append(dual(move, e, o)) move.players[e].append(four(move, e, o)) move.players[e].append(three(move, e, o)) if (move.players[e][2] > 1): move.players[e].append(move.players[e][2] - 1) else: move.players[e].append(0) # move.players[e].append(block(move,o,e)) # move.players[e].append(block(move,o,0)) move.players[o] = [] move.players[o].append(dual(move, o, e)) move.players[o].append(four(move, o, e)) move.players[o].append(three(move, o, e)) if (move.players[o][2] > 1): move.players[o].append(move.players[o][2] - 1) else: move.players[o].append(0) # move.players[o].append(block(move,e,o)) # move.players[o].append(block(move,e,0)) fitness = 0 fitness = ((+((10**11) * move.players[e][1]) + ((10**5) * move.players[e][2]) + ((10**0) * move.players[e][0]) + ((10**7) * move.players[e][3])) - (((-1 * 10**13) * move.players[o][1]) + ((-1 * 10**5) * move.players[o][2]) + ((-1 * 10**0) * move.players[o][0])) + ((-1 * 10**8) * move.players[o][3])) # fitness=(((0*move.players[e][5]) # +((10**9)*move.players[e][1]) # +((10**5)*move.players[e][2]) # +((10**2)*move.players[e][0]) # +((-10**10)*move.players[e][4])) # -((0*move.players[o][5]) # +((-1*10**9)*move.players[o][1]) # +((-1*10**5)*move.players[o][2]) # +((-1*10**2)*move.players[o][0]) # +((10**10)*move.players[o][4]))) # playerObj2.fitness=pow(5,playerObj2.ftb)+pow(-5,playerObj1.ftb)+pow(2,playerObj2.th)+pow(3,playerObj2.tw) return fitness
def move(playerObj1, root, player): if(any([i is not None for i in root.children])): if(1): think = 4 start = time.time() pld = 1 while(time.time()-start < think): # if(1): # print("At depth {}".format(pld)) worthc = [] # print("PLD is ", pd) for pos, child in enumerate(root.children): if(child is not None): # print("child is ", child) worthc.append(minimax(child, pld-1, -INF, INF, gametree.toggle(player), player, playerObj1, pos)) else: worthc.append(-INF) maxv = max(worthc) finc = [chld for chld in range(len(worthc)) if worthc[chld]==maxv] val = random.choice(finc) if(root.children[val] is None): print("WARNING !!!") val = 0 while(root.children[val] is None): val += 1 if(pld == pd): break pld += 1 # print("values are {}".format(worthc)) else: pass root = root.children[val] root = addPlank(root) return(val, root) else: return(0, root)
def _canvasClick(self, event): if not self.gameOn: return if self.game.game_over: return if self.state == True: self.c = event.x // self.elementSize move = self.c self.oldpos = self.root.boardpos #print("pos = c ", self.c, self.root.children) move, self.root = movemaker.acceptmove(self.root, move) gameOver = arena.change_vals(self.oldpos, move, self.player) self.player = gametree.toggle(self.player) if (0 <= self.c < self.game.size['c']): self.drop(self.c) #send data to function self.draw() self._updateCurrentPlayer() if self.game.game_over: x = self.canvas.winfo_width() // 2 y = self.canvas.winfo_height() // 2 if self.game.game_over == 'draw': t = 'DRAW!' else: winner = self.p1 if self.game.first_player else self.p2 t = winner + ' won!' self.canvas.create_text(x, y, text=t, font=("Helvetica", 32), fill="#333") self.state = False else: oldpos = self.root.boardpos move, self.root = movemaker.move(self.playerObj1, self.root, self.player) gameOver = arena.change_vals(self.oldpos, move, self.player) self.player = gametree.toggle(self.player) self.c = move if (0 <= self.c < self.game.size['c']): self.drop(self.c) #send data to function self.draw() self._updateCurrentPlayer() if self.game.game_over: x = self.canvas.winfo_width() // 2 y = self.canvas.winfo_height() // 2 if self.game.game_over == 'draw': t = 'DRAW!' else: winner = self.p1 if self.game.first_player else self.p2 t = winner + ' won!' self.canvas.create_text(x, y, text=t, font=("Helvetica", 32), fill="#333") self.state = True
def evaluation(playerObj, root, player): # print("from eval ") # print("for player ", player) # print("arr is ", list(root.boardpos.players[player]) + list(root.boardpos.players[gametree.toggle(player)])) # print(" 0 1 2 3 4 5 6") # for i in range(0,6): # print(root.boardpos.a[i]) mixer = np.array(playerObj.dna).reshape(1, 8) res = np.ndarray.tolist((np.dot(mixer, np.array(list(root.boardpos.players[player]) + list(root.boardpos.players[gametree.toggle(player)]))))) # print("RES IS {}".format(res)) # res = referee.referee(root.boardpos, player) # print("The last vec is ", list(root.boardpos.players[player].values())) # print("boardpos.players[player] ", root.boardpos.players[player]) # print("boardpos.players[toggle-player] ", root.boardpos.players[gametree.toggle(player)]) return(res[0])
if (__name__ == "__main__"): # fd = open("EP-BAGENT.dna", 'rb') # playerObj1 = pickle.load(fd) # fd.close() # print("Agent is ", playerObj1.dna) playerObj1 = Temp() fd = open("BAGENT.dna", 'wb') pickle.dump(playerObj1, fd) fd.close() gameOver = False player = 1 root = gametree.TreeNode(parent=None, player=player) root.makeTree(pd) board = boardpos.Boardpos() while (not gameOver): oldpos = root.boardpos move, root = movemaker.acceptmove(root, player) gameOver = arena.change_vals(oldpos, move, player) player = gametree.toggle(player) if (not gameOver): oldpos = root.boardpos move, root = movemaker.move(playerObj1, root, player) # move, root = movemaker.acceptmove(root, player) gameOver = arena.change_vals(oldpos, move, player) player = gametree.toggle(player) if (gameOver): # print("Agent {} won with fitness {}".format(playerObj1.agentID, playerObj1.fitness)) pass