def test_solve_small_oshi_zumo(self): # Oshi-Zumo(5, 2, 0) game = pyspiel.load_game("oshi_zumo", {"coins": 5, "size": 2}) values = value_iteration.value_iteration(game, depth_limit=-1, threshold=1e-6, cyclic_game=True) initial_state = game.new_initial_state() # Symmetric game: value is 0 self.assertAlmostEqual(values[str(initial_state)], 0) # Oshi-Zumo(5, 2, 1) game = pyspiel.load_game("oshi_zumo", { "coins": 5, "size": 2, "min_bid": 1 }) values = value_iteration.value_iteration(game, depth_limit=-1, threshold=1e-6, cyclic_game=False) initial_state = game.new_initial_state() # Symmetric game: value is 0 self.assertAlmostEqual(values[str(initial_state)], 0)
def test_solve_small_pig(self): game = pyspiel.load_game("pig", {"winscore": 20}) values = value_iteration.value_iteration(game, depth_limit=-1, threshold=1e-6, cyclic_game=True) initial_state = game.new_initial_state() print("Value of Pig(20): ", values[str(initial_state)])
def test_tic_tac_toe_number_states(self): game = pyspiel.load_game("tic_tac_toe") values = value_iteration.value_iteration(game, depth_limit=-1, threshold=0.01) initial_state = "...\n...\n..." cross_win_state = "...\n...\n.ox" naught_win_state = "x..\noo.\nxx." self.assertEqual(values[initial_state], 0) self.assertEqual(values[cross_win_state], 1) self.assertEqual(values[naught_win_state], -1)
def test_solve_small_goofspiel(self): game = pyspiel.load_game("goofspiel", {"num_cards": 4}) values = value_iteration.value_iteration(game, depth_limit=-1, threshold=1e-6) initial_state = game.new_initial_state() assert initial_state.is_chance_node() root_value = 0 for action, action_prob in initial_state.chance_outcomes(): next_state = initial_state.child(action) root_value += action_prob * values[str(next_state)] # Symmetric game: value is 0 self.assertAlmostEqual(root_value, 0)
def test_solve_small_goofspiel(self): # TODO(author5): This test fails with num_cards = 4 with a new version of # LAPACK (3.10.0), which is used by cvxopt. Might be a bug or bad assumption # about the handling of numerical error. Look into this. game = pyspiel.load_game("goofspiel", {"num_cards": 3}) values = value_iteration.value_iteration(game, depth_limit=-1, threshold=1e-6) initial_state = game.new_initial_state() assert initial_state.is_chance_node() root_value = 0 for action, action_prob in initial_state.chance_outcomes(): next_state = initial_state.child(action) root_value += action_prob * values[str(next_state)] # Symmetric game: value is 0 self.assertAlmostEqual(root_value, 0)
def solve_goofspiel(num_cards=3): """Solves goofspiel. Returns: Dictionary of values """ game = pyspiel.load_game( 'goofspiel(imp_info=False,num_cards={})'.format(num_cards)) print("Solving the game; depth_limit = {}".format(-1)) values = value_iteration.value_iteration(game, -1, 0.01) for state, value in six.iteritems(values): print("") print(str(state)) print("Value = {}".format(value)) return values
def play_tic_tac_toe(): """Solves tic tac toe.""" game = pyspiel.load_game("tic_tac_toe") print("Solving the game; depth_limit = {}".format(-1)) values = value_iteration.value_iteration(game, -1, 0.01) for state, value in values.items(): print("") print(str(state)) print("Value = {}".format(value)) initial_state = "...\n...\n..." cross_win_state = "...\n...\n.ox" naught_win_state = "x..\noo.\nxx." assert values[ initial_state] == 0, "State should be drawn: \n" + initial_state assert values[cross_win_state] == 1, ( "State should be won by player 0: \n" + cross_win_state) assert values[naught_win_state] == -1, ( "State should be won by player 1: \n" + cross_win_state)