Save values to file
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
|
values_*.json
|
||||||
|
|||||||
+41
-40
@@ -61,52 +61,53 @@ class MachinePlayer(APlayer):
|
|||||||
values = np.array([])
|
values = np.array([])
|
||||||
# get possible move
|
# get possible move
|
||||||
moves = self.get_potential_moves(state)
|
moves = self.get_potential_moves(state)
|
||||||
can_move = moves.size > 0
|
if moves.size == 0:
|
||||||
|
return state, False
|
||||||
|
|
||||||
best_move = None
|
best_move = None
|
||||||
best_value = -1
|
best_value = -1
|
||||||
if can_move:
|
for move in moves:
|
||||||
for move in moves:
|
# create hypothetical next state
|
||||||
# create hypothetical next state
|
state_next = self.state_from_move(state.copy(), move)
|
||||||
state_next = self.state_from_move(state.copy(), move)
|
# evaluate value
|
||||||
# evaluate value
|
value = self.get_value(state_next)
|
||||||
value = self.get_value(state_next)
|
if best_value < value:
|
||||||
if best_value < value:
|
best_value = value
|
||||||
best_value = value
|
best_move = move
|
||||||
best_move = move
|
values = np.append(values, value)
|
||||||
values = np.append(values, value)
|
|
||||||
|
|
||||||
next_move = best_move
|
next_move = best_move
|
||||||
is_exp = False
|
is_exp = False
|
||||||
if uniform() < self.p_exp:
|
if uniform() < self.p_exp:
|
||||||
is_exp = True
|
is_exp = True
|
||||||
index = np.random.randint(len(moves))
|
index = np.random.randint(len(moves))
|
||||||
next_move = moves[index]
|
next_move = moves[index]
|
||||||
elif do_sample:
|
elif do_sample:
|
||||||
index = sample(values)
|
index = sample(values)
|
||||||
next_move = moves[index]
|
next_move = moves[index]
|
||||||
|
|
||||||
|
if self.with_debug:
|
||||||
|
print(f"{self.mark}: Values = {values}")
|
||||||
|
print(f"{self.mark}: Moves = {moves+1}")
|
||||||
|
print(f"{self.mark}: Best move = {best_move+1}")
|
||||||
|
print(f"{self.mark}: Next move = {next_move+1}, is_exp={is_exp}")
|
||||||
|
|
||||||
|
if self.state is not None:
|
||||||
|
self.state_last = self.state.copy()
|
||||||
|
|
||||||
|
self.state = self.state_from_move(state.copy(), next_move)
|
||||||
|
|
||||||
|
# Learn
|
||||||
|
if not is_exp and self.state_last is not None:
|
||||||
|
v0 = self.get_value(self.state_last)
|
||||||
|
v1 = self.get_value(self.state)
|
||||||
|
d = v0 + self.alpha*(v1-v0)
|
||||||
|
if d > 0:
|
||||||
|
self.set_value(self.state_last, d)
|
||||||
if self.with_debug:
|
if self.with_debug:
|
||||||
print(f"{self.mark}: Values = {values}")
|
print(f"{self.mark}: Learned {d:0.3f}")
|
||||||
print(f"{self.mark}: Moves = {moves+1}")
|
|
||||||
print(f"{self.mark}: Best move = {best_move+1}")
|
|
||||||
print(f"{self.mark}: Next move = {next_move+1}, is_exp={is_exp}")
|
|
||||||
|
|
||||||
if self.state is not None:
|
return self.state, True
|
||||||
self.state_last = self.state.copy()
|
|
||||||
|
|
||||||
self.state = self.state_from_move(state.copy(), next_move)
|
|
||||||
|
|
||||||
# Learn
|
|
||||||
if not is_exp and self.state_last is not None:
|
|
||||||
v0 = self.get_value(self.state_last)
|
|
||||||
v1 = self.get_value(self.state)
|
|
||||||
d = v0 + self.alpha*(v1-v0)
|
|
||||||
if d > 0:
|
|
||||||
self.set_value(self.state_last, d)
|
|
||||||
if self.with_debug:
|
|
||||||
print(f"{self.mark}: Learned {d:0.3f}")
|
|
||||||
|
|
||||||
return self.state, can_move
|
|
||||||
|
|
||||||
def state_from_move(self, state: np.array, field):
|
def state_from_move(self, state: np.array, field):
|
||||||
state.reshape(state.size)[field] = self.mark
|
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 helper import create_empty_state, to_state_string
|
||||||
from machine_player import MachinePlayer
|
from machine_player import MachinePlayer
|
||||||
from player_provider import PlayerProvider
|
from player_provider import PlayerProvider
|
||||||
|
import json
|
||||||
|
|
||||||
float_formatter = "{:.3f}".format
|
float_formatter = "{:.3f}".format
|
||||||
np.set_printoptions(formatter={'float_kind': float_formatter})
|
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_print=False):
|
||||||
for k in range(0, k_max):
|
for k in range(0, k_max):
|
||||||
players = player_provider.choose()
|
players = player_provider.choose()
|
||||||
@@ -48,8 +50,14 @@ def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
|
|||||||
move += 1
|
move += 1
|
||||||
|
|
||||||
|
|
||||||
px = MachinePlayer(mark='X', with_debug=False)
|
with open("values_x.json", "r") as fp:
|
||||||
po = MachinePlayer(mark='O', with_debug=False)
|
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
|
do_training = 1
|
||||||
if do_training:
|
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))
|
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(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