def main(strategy_type, admin_host, admin_port): if not isinstance(admin_port, int): raise ValueError() if strategy_type == "random": strategy = Strategies.RandomStrategy() elif strategy_type == "look-ahead": try: with open("strategy.config", "r") as f: num_looks_ahead = parse_json( f.read())[0]["value"]["look-ahead"] strategy = Strategies.NLooksAheadStrategy(num_looks_ahead) except FileNotFoundError: print( "strategy.config for look-ahead strategy file not found in directory!" ) sys.exit(1) elif strategy_type == "smart": strategy = Strategies.SmartStrategy() elif strategy_type == "greedy": strategy = Strategies.GreedyStrategy() elif strategy_type == "interactive": strategy = Strategies.InteractiveStrategy() elif strategy_type == "cheating": strategy = Strategies.CheatingStrategy() else: raise ValueError("Unsupported strategy type!") player = SmartPlayer(input("Type your player's name: "), strategy) player_driver = PlayerDriver(player, admin_host, admin_port) player_driver.start_driver()
nn.addModule(hiddenLayer4) nn.addModule(hiddenLayer5) nn.addConnection(FullConnection(inLayer, hiddenLayer1)) nn.addConnection(FullConnection(inLayer, hiddenLayer2)) nn.addConnection(FullConnection(hiddenLayer1, hiddenLayer3)) nn.addConnection(FullConnection(hiddenLayer2, hiddenLayer3)) nn.addConnection(FullConnection(hiddenLayer2, hiddenLayer5)) nn.addConnection(FullConnection(hiddenLayer3, hiddenLayer4)) nn.addConnection(FullConnection(hiddenLayer3, hiddenLayer5)) nn.addConnection(FullConnection(hiddenLayer3, outLayer)) nn.addConnection(FullConnection(hiddenLayer4, outLayer)) nn.addConnection(FullConnection(hiddenLayer5, outLayer)) nn.sortModules() othello = Othello() smartPlayer = SmartPlayer(nn,othello.boardSize) tacticalPlayer = TacticalPlayer() tacticalPlayer.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER])); smartPlayer.newGame(othello,tacticalPlayer.enemy); playGame(othello,tacticalPlayer,smartPlayer) if othello.getWinner() == tacticalPlayer.color: outcome = -1 print "Tactical Player wins over Network Player 4" count += 1 semicount += 1 elif othello.getWinner() == smartPlayer.color: outcome = 1 print "Network Player 4 wins over Tactical Player" count += 1
qlearner = QLearner() NUM = qlearner.GAME_NUM # ======================================================================== # ** TRAIN: play 2*NUM games against players who only make random moves ** # ======================================================================== board = Board() battle(board, RandomPlayer(), qlearner, NUM, learn=True, show_result=False) battle(board, qlearner, RandomPlayer(), NUM, learn=True, show_result=False) # ======================================================================== # ** TEST: play 1000 games against each opponent # ======================================================================== q_rand = battle(board, qlearner, RandomPlayer(), 500) rand_q = battle(board, RandomPlayer(), qlearner, 500) q_smart = battle(board, qlearner, SmartPlayer(), 500) smart_q = battle(board, SmartPlayer(), qlearner, 500) q_perfect = battle(board, qlearner, PerfectPlayer(), 500) perfect_q = battle(board, PerfectPlayer(), qlearner, 500) # ======================================================================== # ** Summarize game results # ======================================================================== winning_rate_w_random_player = round(100 - (q_rand[2] + rand_q[1]) / 2, 2) winning_rate_w_smart_player = round(100 - (q_smart[2] + smart_q[1]) / 2, 2) winning_rate_w_perfect_player = round( 100 - (q_perfect[2] + perfect_q[1]) / 2, 2) print("Summary:") print("_" * 60) print("QLearner VS RandomPlayer | Win/Draw Rate = {}%".format(
hiddenLayer3 = SigmoidLayer(33) outLayer = SoftmaxLayer(64) nn.addInputModule(inLayer) nn.addOutputModule(outLayer) nn.addModule(hiddenLayer1) nn.addModule(hiddenLayer2) nn.addModule(hiddenLayer3) nn.addConnection(FullConnection(inLayer, hiddenLayer1)) nn.addConnection(FullConnection(inLayer, hiddenLayer2)) nn.addConnection(FullConnection(hiddenLayer2, hiddenLayer3)) nn.addConnection(FullConnection(hiddenLayer1, outLayer)) nn.addConnection(FullConnection(hiddenLayer3, outLayer)) nn.sortModules() othello = Othello() smartPlayer = SmartPlayer(nn, othello.boardSize) randomPlayer = TacticalPlayer() randomPlayer.newGame(othello, random.choice([othello.WHITE_PLAYER, othello.BLACK_PLAYER])) smartPlayer.newGame(othello, randomPlayer.enemy) playGame(othello, randomPlayer, smartPlayer) if othello.getWinner() == randomPlayer.color: outcome = -1 print "Tactical Player wins over Network Player 1" count += 1 semicount += 1 elif othello.getWinner() == smartPlayer.color: outcome = 1 print "Network Player 1 wins over Tactical Player" wincnt += 1 count += 1
hiddenLayer4 = SigmoidLayer(50) outLayer = SoftmaxLayer(64) nn.addInputModule(inLayer) nn.addOutputModule(outLayer) nn.addModule(hiddenLayer1) nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceTo=16)) nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceFrom=16, inSliceTo=32)) nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceFrom=32, insliceTo=48)) nn.addConnection(FullConnection(inLayer, hiddenLayer1, inSliceFrom=48)) nn.addConnection(FullConnection(hiddenLayer1, outLayer)) nn.sortModules() othello = Othello() smartPlayer = SmartPlayer(nn,othello.boardSize) greedyPlayer = TacticalPlayer() greedyPlayer.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER])); smartPlayer.newGame(othello,greedyPlayer.enemy); playGame(othello,greedyPlayer,smartPlayer) if othello.getWinner() == greedyPlayer.color: outcome = -1 print "Tactical Player wins over Network Player 5" count += 1 semicount += 1 elif othello.getWinner() == smartPlayer.color: outcome = 1 print "Network Player 5 wins over Tactical Player" count += 1
def test_check_board(prev_board, curr_board, color, expected): player = SmartPlayer("P1") player.register() player.place(initial_board(), color) player.board.set_board(prev_board) assert expected == player._check_board(curr_board)
from SmartPlayer import SmartPlayer from RandomPlayer import RandomPlayer from GreedyPlayer import GreedyPlayer from Othello import Othello from Player import playGame from HumanPlayer import HumanPlayer from pybrain.tools.customxml.networkreader import NetworkReader from TacticalPlayer import TacticalPlayer import random import time nn = NetworkReader.readFrom("othelloNetwork.xml") player1 = SmartPlayer(nn,8) #change this to change the opponent to be testing against player2 = RandomPlayer() othello = Othello() othello.resetGame() player1.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER])) player2.newGame(othello,player1.enemy) while not othello.isGameOver(): if (player1.color == othello.WHITE_PLAYER): print "Neural Network is white" else: print "Neural Network is black" othello.printBoard() time.sleep(1) if (othello.whoGoesNext() == player1.color): move = player1.getMove() othello.makeMove(move[0],move[1],player1.color)