- simplified debug flags

- added params class
- cleaned up
This commit is contained in:
2024-06-09 18:43:42 +02:00
parent 64960873c8
commit c1f64943c3
4 changed files with 38 additions and 21 deletions
+15 -12
View File
@@ -8,8 +8,8 @@ float_formatter = "{:.3f}".format
np.set_printoptions(formatter={'float_kind': float_formatter})
def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
def play(player_provider: PlayerProvider, k_max=10000, with_debug=False):
player_provider.set_debug(with_debug)
for k in range(0, k_max):
players = player_provider.choose()
state = create_empty_state()
@@ -18,16 +18,16 @@ def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
other_player = players[-1]
while run:
for player in players:
if with_print:
if with_debug:
print(f"---------------------------------------------------")
print(f"- Game {k:06d}, Move {move} -----------------------------")
print(f"---------------------------------------------------")
last_state = state
state, has_moved = player.move(state)
if with_print:
if with_debug:
print(to_state_string(last_state, state))
if not has_moved:
if with_print:
if with_debug:
print(f"{player.mark}: No more moves")
if isinstance(player, MachinePlayer):
player.reward(0.0)
@@ -35,7 +35,7 @@ def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
other_player.reward(0.0)
run = False
if player.has_won(state):
if with_print:
if with_debug:
print(f"{player.mark}: Has won the game")
if isinstance(player, MachinePlayer):
player.reward(1.0)
@@ -56,16 +56,19 @@ with open("values_x.json", "r") as fp:
with open("values_o.json", "r") as fp:
o_values = json.load(fp)
px = MachinePlayer(mark='X', with_debug=False, values=x_values)
po = MachinePlayer(mark='O', with_debug=False, values=o_values)
px = MachinePlayer(mark='X', params=MachinePlayer.Params(p_exp=0.2, alpha=0.1))
po = MachinePlayer(mark='O', params=MachinePlayer.Params(p_exp=0.2, alpha=0.1))
px.assign_values(x_values)
po.assign_values(o_values)
player_provider = PlayerProvider(px, po)
do_training = 1
if do_training:
players = PlayerProvider(px, po)
play(players, 10000, False)
play(player_provider, 10000, False)
players = PlayerProvider(MachinePlayer(mark='X', with_debug=True, values=px.values), MachinePlayer(mark='O', with_debug=True, values=po.values))
play(players, 1000, True)
play(player_provider, 10, True)
# Convert and write JSON object to file
with open("values_x.json", "w") as fp: