def main(): first() try: g = OthelloGame(8) # all players # rp = RandomPlayer(g).play # gp = GreedyOthelloPlayer(g).play # hp = HumanOthelloPlayer(g).play mhp = MyHumanOthelloPlayer(g).play # nnet players n1 = NNet(g) n1.load_checkpoint('../alpha-zero-general-ch/temp1', 'checkpoint_258.pth.tar') args1 = dotdict({'numMCTSSims': 50, 'cpuct': 1.0}) mcts1 = MCTS(g, n1, args1) n1p = lambda x: np.argmax(mcts1.getActionProb(x, temp=0)) # nnet players # n2 = NNet(g) # n2.load_checkpoint('../alpha-zero-general-ch/temp1', 'checkpoint_257.pth.tar') # args2 = dotdict({'numMCTSSims': 50, 'cpuct': 1.0}) # mcts2 = MCTS(g, n2, args2) # n2p = lambda x: np.argmax(mcts2.getActionProb(x, temp=0)) arena = MyArena(n1p, mhp, g, display=mydisplay) result = arena.playGame( verbose=True, resume=[19, 34, 41, 20, 37, 18, 9, 43, 13, 29, 51]) finally: last() print(result)
import numpy as np from utils import * """ This script is used to get the number of win for trained NN with ramdon/greedy player. Remember to store the ouput in a file, so that could be further used to plot. """ args = dotdict({ 'numIters': 20, # Number of iterations. 'numGames': 100, # Number of game play for each iteration. 'checkpoint': './temp/20_8checkpoints/', # The checkpoint file that store all NN. }) if __name__ == "__main__": g = OthelloGame(8) # Define the game # All players rp = RandomPlayer(g).play gp = GreedyOthelloPlayer(g).play # The first iteration is the random player play with other players print("Iteration 0") # Random players print('Random players') arena_r = Arena.Arena(rp, rp, g, display=OthelloGame.display) print(arena_r.playGames(args.numGames, verbose=True)) # Greedy players print('Greedy players')
from othello.keras.CNN import NNetWrapper as CNN from othello.keras.NN64 import NNetWrapper as NN64 from othello.keras.NN128 import NNetWrapper as NN128 from othello.keras.NN256 import NNetWrapper as NN256 from othello.keras.LSTM64 import NNetWrapper as LSTM64 from othello.keras.LSTM128 import NNetWrapper as LSTM128 from othello.keras.LSTM256 import NNetWrapper as LSTM256 import numpy as np from utils import * """ use this script to play any two agents against each other, or play manually with any agent. """ g = OthelloGame(6) # all players rp = RandomPlayer(g).play gp = GreedyOthelloPlayer(g).play hp = HumanOthelloPlayer(g).play # nnet players n1 = CNN(g) n1.load_checkpoint('./othcnntemp/', 'best.pth.tar') args1 = dotdict({'numMCTSSims': 25, 'cpuct': 1.0}) mcts1 = MCTS(g, n1, args1) cnn = lambda x: np.argmax(mcts1.getActionProb(x, temp=0)) n2 = NN64(g) n2.load_checkpoint('./othnn64temp/', 'best.pth.tar')
def test_othello_keras(self): self.execute_game_test(OthelloGame(6), OthelloKerasNNet)
def test_othello_tensorflow(self): self.execute_game_test(OthelloGame(6), OthelloTensorflowNNet)
def test_othello_pytorch(self): self.execute_game_test(OthelloGame(6), OthelloPytorchNNet)
from framework.utils import * import othello.OthelloArena as oa from framework.MCTS import MCTS from othello.OthelloGame import OthelloGame from othello.OthelloPlayers import * from othello.tensorflow.NNet import NNetWrapper as nNNet size = 6 BLOCK_SIZE = 20 TILESIZE = 630 / size ui = dict({ 'width': round(size * TILESIZE + 300), 'height': round(size * TILESIZE) }) g = OthelloGame(6, ui=ui) # all players rp = RandomPlayer(g).play gp = GreedyOthelloPlayer(g).play hp = HumanOthelloPlayer(g).play hp1 = HumanPlayerUserInterface(g).play # nnet players n1 = nNNet(g) n1.load_checkpoint('./pretrained_models/othello/', 'best.pth.tar') args1 = dotdict({'numMCTSSims': 6, 'cpuct': 1.0}) mcts1 = MCTS(g, n1, args1) n1p = lambda x: np.argmax(mcts1.getActionProb(x, temp=0)) # m1 = nNNet(g)
next_state, next_player = game.getNextState(state, 1, action) next_state = game.getCanonicalForm(next_state, -1) node = str(next_state.reshape(-1) + 1)[1:-1] cannonical_states[vertex][action]['winner'] = game.getGameEnded(next_state, 1) # * np.abs(next_state.sum()) cannonical_states[vertex][action]['next_node'] = node if node not in seen: seen[node] = 1 # seen.add(node) queue.append(node) else: for action, data in cannonical_states[vertex].items(): node = data['next_node'] if node not in seen: seen[node] = 1 # seen.add(node) queue.append(node) N = len(cannonical_states) if N%10000 == 0: print('\rstates: {}'.format(N), end='') cannonical_states.sync() print() cannonical_states.close() return n = 6 game = Game(n) board = game.getInitBoard() bfs_cannonical(game, board, './data/6by6', first_player=1)
from othello.OthelloGame import OthelloGame from othello.OthelloPlayers import * from othello.keras.NNet import NNetWrapper as NNet import numpy as np from utils import * """ use this script to play any two agents against each other, or play manually with any agent. """ mini_othello = False # Play in 6x6 instead of the normal 8x8. human_vs_cpu = True if mini_othello: g = OthelloGame(6) else: g = OthelloGame(8) # all players rp = RandomPlayer(g).play gp = GreedyOthelloPlayer(g).play hp = HumanOthelloPlayer(g).play # nnet players n1 = NNet(g) if mini_othello: n1.load_checkpoint('./pretrained_models/othello/keras/', '6x100x25_best.pth.tar') else: n1.load_checkpoint('./pretrained_models/othello/keras/',
def force_play(self, board, move): return def endgame(self): return """ use this script to play any two agents against each other, or play manually with any agent. """ with open('othello/book/XOT/openingssmall.txt') as my_file: book_array = my_file.readlines() g = OthelloGame(8) # all players rp = RandomPlayer(g) gp = GreedyOthelloPlayer(g) hp = HumanOthelloPlayer(g) edax = EdaxPlayer(3, g) # nnet players n1 = NNet(g) n1.load_checkpoint('./temp/', 'restart.pth.tar') #n1.load_checkpoint('./pretrained_models/othello/pytorch','restart.pth.tar') args1 = dotdict({'numMCTSSims': 600, 'cpuct': 1.0}) n1p = NeuralPlayer(g, n1, args1)
""" import numpy as np import py_compile as cmp from utils import * from Arena import * from othello.OthelloGame import OthelloGame, display from othello.OthelloPlayers import * from MCS import MCSPlayer from Qlearning import QlearningPlayer from MCTS import MCTSPlayer cmp.compile("othello/OthelloPlayers.py") g = OthelloGame(4) totalp1wins, totalp2wins, totaldraws = [], [], [] #using random seeds 8-18 for i in range(8, 18): np.random.seed(i) #p1 = MCSPlayer(g) p1 = QlearningPlayer(g) p1.train(5000) #mcts_arguments = dotdict({'MCTSiterations': 100, 'exploration_param': 1.0}) #mcts = MCTSPlayer(g, mcts_arguments) #p1 = lambda x: np.argmax(mcts.getProbability(x, temp=0)) p2 = GreedyOthelloPlayer(g)
import ArenaHP from MCTS import MCTS from othello.OthelloGame import OthelloGame, display from othello.OthelloPlayers import * from othello.keras.resNNet import NNetWrapper as NNet import numpy as np from utils import * """ use this script to play any two agents against each other, or play manually with any agent. """ g = OthelloGame(8) hp = HumanOthelloPlayer(g) t = hp.choose_turn() hp.t = t #1 is hp first turn g.t = t # all players rp = RandomPlayer(g).play gp = GreedyOthelloPlayer(g).play n2 = NNet(g) n2.load_checkpoint('./temp/', 'best.pth.tar') args2 = dotdict({'numMCTSSims': 1200, 'cpuct': 1.0}) mcts2 = MCTS(g, n2, args2) n2p = lambda x: np.argmax(mcts2.getActionProb(x, temp=0)) arena = ArenaHP.Arena(n2p, hp, g, display=display) arena.t = t print(arena.playGame(verbose=True))