Save values to file
This commit is contained in:
@@ -1 +1,2 @@
|
||||
__pycache__/
|
||||
values_*.json
|
||||
|
||||
+4
-3
@@ -61,10 +61,11 @@ class MachinePlayer(APlayer):
|
||||
values = np.array([])
|
||||
# get possible move
|
||||
moves = self.get_potential_moves(state)
|
||||
can_move = moves.size > 0
|
||||
if moves.size == 0:
|
||||
return state, False
|
||||
|
||||
best_move = None
|
||||
best_value = -1
|
||||
if can_move:
|
||||
for move in moves:
|
||||
# create hypothetical next state
|
||||
state_next = self.state_from_move(state.copy(), move)
|
||||
@@ -106,7 +107,7 @@ class MachinePlayer(APlayer):
|
||||
if self.with_debug:
|
||||
print(f"{self.mark}: Learned {d:0.3f}")
|
||||
|
||||
return self.state, can_move
|
||||
return self.state, True
|
||||
|
||||
def state_from_move(self, state: np.array, field):
|
||||
state.reshape(state.size)[field] = self.mark
|
||||
|
||||
+16
-3
@@ -2,11 +2,13 @@ import numpy as np
|
||||
from helper import create_empty_state, to_state_string
|
||||
from machine_player import MachinePlayer
|
||||
from player_provider import PlayerProvider
|
||||
import json
|
||||
|
||||
float_formatter = "{:.3f}".format
|
||||
np.set_printoptions(formatter={'float_kind': float_formatter})
|
||||
|
||||
|
||||
|
||||
def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
|
||||
for k in range(0, k_max):
|
||||
players = player_provider.choose()
|
||||
@@ -48,8 +50,14 @@ def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
|
||||
move += 1
|
||||
|
||||
|
||||
px = MachinePlayer(mark='X', with_debug=False)
|
||||
po = MachinePlayer(mark='O', with_debug=False)
|
||||
with open("values_x.json", "r") as fp:
|
||||
x_values = json.load(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)
|
||||
|
||||
do_training = 1
|
||||
if do_training:
|
||||
@@ -59,4 +67,9 @@ if do_training:
|
||||
players = PlayerProvider(MachinePlayer(mark='X', with_debug=True, values=px.values), MachinePlayer(mark='O', with_debug=True, values=po.values))
|
||||
play(players, 1000, True)
|
||||
|
||||
px.print_state_table()
|
||||
# Convert and write JSON object to file
|
||||
with open("values_x.json", "w") as fp:
|
||||
json.dump(px.values, fp, indent=0)
|
||||
|
||||
with open("values_o.json", "w") as fp:
|
||||
json.dump(po.values, fp, indent=0)
|
||||
|
||||
Reference in New Issue
Block a user