Files
tic_tac_toe/machine_player.py
T
jens 53e4d91ff8 - refactored learning function
- remove state_last, use state instead
- added episode history
2024-06-11 09:00:41 +02:00

125 lines
3.0 KiB
Python

import numpy as np
from a_player import APlayer
from numpy.random import uniform
from state import get_potential_moves
class MachinePlayer(APlayer):
class Params:
def __init__(self, p_explore, alpha):
self.p_exp = p_explore
self.alpha = alpha
def __init__(self, mark, params: Params, values):
APlayer.__init__(self, mark)
self.params = params
self.values = {}
self.with_debug = False
self.values = values
self.episode_history = None
def set_debug(self, with_debug):
self.with_debug = with_debug
def print_state_table(self):
count = 0
for key in self.values:
print(f"{self.mark}: {count:05d}: {key} = {self.values[key]:0.3f}")
count += 1
@staticmethod
def to_key(state: np.array):
key = ''
for st in state.reshape(state.size):
key += st
return key
def get_value(self, state: np.array):
key = self.to_key(state)
try:
result = self.values[key]
except KeyError:
result = 0.5
return result
def set_value(self, state: np.array, value):
self.values[self.to_key(state)] = value
def end_game(self, value):
self.set_value(self.state, value)
print(self.episode_history)
def new_game(self):
self.state = None
self.episode_history = []
def learn_from_history(self):
pass
def calc_value(self, state: np.array, next_state: np.array):
v0 = self.get_value(state)
v1 = self.get_value(next_state)
d = v0 + self.params.alpha*(v1-v0)
if self.with_debug:
print(f"{self.mark}: Learned {d:0.3f}")
return d
def get_best_move(self, state: np.array, moves: np.array):
best_move = None
best_value = -1
values = np.array([])
for move in moves:
# create hypothetical next state
state_next = self.to_state(state.copy(), move)
# evaluate value
value = self.get_value(state_next)
if best_value < value:
best_value = value
best_move = move
values = np.append(values, value)
return best_move, best_value, values
def move(self, state: np.array):
# get possible move
moves = get_potential_moves(state)
if moves.size == 0:
return state, False
best_move, best_value, values = self.get_best_move(state, moves)
next_move = best_move
is_exp = False
# Randomly perform exploratory move
if uniform() < self.params.p_exp:
index = np.random.randint(len(moves))
next_move = moves[index]
is_exp = best_move != next_move
# Finally create next state
next_state = self.to_state(state.copy(), next_move)
next_value = self.get_value(next_state)
# Maintain history
self.episode_history.append((self.to_key(next_state), next_value, next_move, is_exp))
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}")
# Learn
if not is_exp and self.state is not None:
d = self.calc_value(self.state, next_state)
if d > 0:
self.set_value(self.state, d)
if self.with_debug:
print(f"{self.mark}: Learned {d:0.3f}")
self.state = next_state
return next_state, True