def _run(self, games=1, reset=True, play_self=True, yield_length=False): if reset: for player in self.players: player.win = [] player.lost = [] player.drawn = [] player.for_points = 0 player.away_points = 0 # added 1 so we never say 'Playing game 0' num_games = ((len(self.players) * len(self.players)) * games) + 1 if not play_self: num_games -= (games * 2) for player_a in self.players: for player_b in self.players: for i in xrange(0, games): if not play_self and player_a == player_b: continue # lets not bother playing ourselves! if getattr(self, 'VERBOSE', False): print player_a.name, 'vs', player_b.name if getattr(self, 'VERBOSE', False): mg = board.MancalaGame(player_a, player_b, verbose=True) else: mg = board.MancalaGame(player_a, player_b) mg.run_game() a_score, b_score = mg.board.scores() mg.conf_player_a.for_points += a_score mg.conf_player_a.away_points += b_score mg.conf_player_b.for_points += b_score mg.conf_player_b.away_points += a_score if yield_length: yield mg.board.game_history if getattr(self, 'VERBOSE', False): print "(%02s, %02s)" % mg.board.scores() if mg.board.game_result == engine.GAME_WON_A: mg.conf_player_a.won.append(mg.conf_player_b) mg.conf_player_a.home_won += 1 mg.conf_player_b.lost.append(mg.conf_player_a) mg.conf_player_b.away_lost += 1 elif mg.board.game_result == engine.GAME_WON_B: mg.conf_player_b.won.append(mg.conf_player_a) mg.conf_player_b.away_won += 1 mg.conf_player_a.lost.append(mg.conf_player_b) mg.conf_player_a.home_lost += 1 else: mg.conf_player_a.drawn.append(mg.conf_player_b) mg.conf_player_b.drawn.append(mg.conf_player_a)
def _fitness_away(self, cro, cg): away = board.MancalaGame(cg, cro) away.run_game() cg_score, cro_score = away.board.scores() cro.for_points += cro_score cro.away_points += cg_score if cro_score > cg_score: cro.won.append(cg) cro.away_won += 1 elif cg_score > cro_score: cro.lost.append(cg) cro.away_lost += 1 else: cro.drawn.append(cg)
def _fitness_home(self, cro, cg): home = board.MancalaGame(cro, cg) home.run_game() cro_score, cg_score = home.board.scores() cro.for_points += cro_score cro.away_points += cg_score if cro_score > cg_score: cro.won.append(cg) cro.home_won += 1 elif cg_score > cro_score: cro.lost.append(cg) cro.home_lost += 1 else: cro.drawn.append(cg)
def test_game(self): """ Run through 10,000 test games with random players. For testing the game player and also testing game rules. 10,000 totally games are likely to explore most of the game rules and find any flaws. """ for i in range(10000): player_a = players.ConfiguredPlayer( basic.RandomPlayer, name="R A", ) player_b = players.ConfiguredPlayer( basic.RandomPlayer, name="R B", ) mg = board.MancalaGame(player_a, player_b) mg.run_game()
def test_minimax_game(self): """ Run through a sample test game with MiniMax players. """ settings = { 'max_depth': 2, } player_a = basic.MiniMaxPlayer.objects.random(name="MM 1", settings=settings.copy()) player_b = basic.MiniMaxPlayer.objects.random(name="MM 2", settings=settings.copy()) player_a = players.ConfiguredPlayer( basic.RandomPlayer, name="R A", ) player_b = players.ConfiguredPlayer( basic.RandomPlayer, name="R B", ) mg = board.MancalaGame(player_a, player_b) mg.run_game()
def _train(self, cp): nn_player = players.ConfiguredPlayer(NeuralNetPlayer, name="NNN") #print nn_player.name, "vs", cp.name, "--", home_game = board.MancalaGame(nn_player, cp) home_game.run_game() home_a, home_b = home_game.board.scores() self.backpropogate(home_game) result = ( 0, '###', ) if home_a > home_b: #print '%02s %02s -- NN' % (home_a, home_b) result = ( 1, nn_player.name, ) Log(result=1).save() elif home_b > home_a: #print '%02s %02s -- CP' % (home_a, home_b) result = ( -1, cp.name, ) Log(result=-1).save() else: #print '%02s %02s -- DR' % (home_a, home_b) Log(result=0).save() pass return result