- 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
+3
View File
@@ -11,6 +11,9 @@ class APlayer(object):
self.state = None self.state = None
self.state_last = None self.state_last = None
def set_debug(self, with_debug):
pass
def move(self, state: np.array): def move(self, state: np.array):
return state, False return state, False
+15 -8
View File
@@ -5,14 +5,21 @@ from numpy.random import uniform
class MachinePlayer(APlayer): class MachinePlayer(APlayer):
def __init__(self, mark='X', with_debug=False, values=None): class Params:
def __init__(self, p_exp, alpha):
self.p_exp = p_exp
self.alpha = alpha
def __init__(self, mark, params: Params):
APlayer.__init__(self, mark) APlayer.__init__(self, mark)
self.p_exp = 0.2 self.params = params
self.alpha = 0.1 self.values = {}
self.with_debug = False
def set_debug(self, with_debug):
self.with_debug = with_debug self.with_debug = with_debug
if values is None:
self.init_values() def assign_values(self, values):
else:
self.values = values self.values = values
def init_values(self): def init_values(self):
@@ -78,7 +85,7 @@ class MachinePlayer(APlayer):
next_move = best_move next_move = best_move
is_exp = False is_exp = False
if uniform() < self.p_exp: if uniform() < self.params.p_exp:
index = np.random.randint(len(moves)) index = np.random.randint(len(moves))
next_move = moves[index] next_move = moves[index]
is_exp = best_move != next_move is_exp = best_move != next_move
@@ -101,7 +108,7 @@ class MachinePlayer(APlayer):
if not is_exp and self.state_last is not None: if not is_exp and self.state_last is not None:
v0 = self.get_value(self.state_last) v0 = self.get_value(self.state_last)
v1 = self.get_value(self.state) v1 = self.get_value(self.state)
d = v0 + self.alpha*(v1-v0) d = v0 + self.params.alpha*(v1-v0)
if d > 0: if d > 0:
self.set_value(self.state_last, d) self.set_value(self.state_last, d)
if self.with_debug: if self.with_debug:
+4
View File
@@ -6,6 +6,10 @@ class PlayerProvider:
def __init__(self, p1: APlayer, p2: APlayer): def __init__(self, p1: APlayer, p2: APlayer):
self.players = [p1, p2] self.players = [p1, p2]
def set_debug(self, with_debug):
for p in self.players:
p.set_debug(with_debug)
def choose(self) -> list[APlayer]: def choose(self) -> list[APlayer]:
# Wer fängt an? # Wer fängt an?
players = self.players players = self.players
+15 -12
View File
@@ -8,8 +8,8 @@ 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_debug=False):
def play(player_provider: PlayerProvider, k_max=10000, with_print=False): player_provider.set_debug(with_debug)
for k in range(0, k_max): for k in range(0, k_max):
players = player_provider.choose() players = player_provider.choose()
state = create_empty_state() state = create_empty_state()
@@ -18,16 +18,16 @@ def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
other_player = players[-1] other_player = players[-1]
while run: while run:
for player in players: for player in players:
if with_print: if with_debug:
print(f"---------------------------------------------------") print(f"---------------------------------------------------")
print(f"- Game {k:06d}, Move {move} -----------------------------") print(f"- Game {k:06d}, Move {move} -----------------------------")
print(f"---------------------------------------------------") print(f"---------------------------------------------------")
last_state = state last_state = state
state, has_moved = player.move(state) state, has_moved = player.move(state)
if with_print: if with_debug:
print(to_state_string(last_state, state)) print(to_state_string(last_state, state))
if not has_moved: if not has_moved:
if with_print: if with_debug:
print(f"{player.mark}: No more moves") print(f"{player.mark}: No more moves")
if isinstance(player, MachinePlayer): if isinstance(player, MachinePlayer):
player.reward(0.0) player.reward(0.0)
@@ -35,7 +35,7 @@ def play(player_provider: PlayerProvider, k_max=10000, with_print=False):
other_player.reward(0.0) other_player.reward(0.0)
run = False run = False
if player.has_won(state): if player.has_won(state):
if with_print: if with_debug:
print(f"{player.mark}: Has won the game") print(f"{player.mark}: Has won the game")
if isinstance(player, MachinePlayer): if isinstance(player, MachinePlayer):
player.reward(1.0) player.reward(1.0)
@@ -56,16 +56,19 @@ with open("values_x.json", "r") as fp:
with open("values_o.json", "r") as fp: with open("values_o.json", "r") as fp:
o_values = json.load(fp) o_values = json.load(fp)
px = MachinePlayer(mark='X', with_debug=False, values=x_values) px = MachinePlayer(mark='X', params=MachinePlayer.Params(p_exp=0.2, alpha=0.1))
po = MachinePlayer(mark='O', with_debug=False, values=o_values) 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 do_training = 1
if do_training: if do_training:
players = PlayerProvider(px, po) play(player_provider, 10000, False)
play(players, 10000, False)
players = PlayerProvider(MachinePlayer(mark='X', with_debug=True, values=px.values), MachinePlayer(mark='O', with_debug=True, values=po.values)) play(player_provider, 10, True)
play(players, 1000, True)
# Convert and write JSON object to file # Convert and write JSON object to file
with open("values_x.json", "w") as fp: with open("values_x.json", "w") as fp: