Re-indent with tabs
This commit is contained in:
+82
-82
@@ -5,103 +5,103 @@ from numpy.random import uniform
|
||||
|
||||
|
||||
class MachinePlayer(APlayer):
|
||||
class Params:
|
||||
def __init__(self, p_exp, alpha):
|
||||
self.p_exp = p_exp
|
||||
self.alpha = alpha
|
||||
class Params:
|
||||
def __init__(self, p_exp, alpha):
|
||||
self.p_exp = p_exp
|
||||
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
|
||||
def __init__(self, mark, params: Params, values):
|
||||
APlayer.__init__(self, mark)
|
||||
self.params = params
|
||||
self.values = {}
|
||||
self.with_debug = False
|
||||
self.values = values
|
||||
|
||||
def set_debug(self, with_debug):
|
||||
self.with_debug = with_debug
|
||||
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
|
||||
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
|
||||
@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
|
||||
def get_value(self, state: np.array):
|
||||
key = self.to_key(state)
|
||||
try:
|
||||
result = self.values[key]
|
||||
except KeyError:
|
||||
result = 0.5
|
||||
|
||||
return result
|
||||
return result
|
||||
|
||||
def set_value(self, state: np.array, value):
|
||||
self.values[self.to_key(state)] = value
|
||||
def set_value(self, state: np.array, value):
|
||||
self.values[self.to_key(state)] = value
|
||||
|
||||
def reward(self, value):
|
||||
self.set_value(self.state, value)
|
||||
def reward(self, value):
|
||||
self.set_value(self.state, value)
|
||||
|
||||
def new_game(self):
|
||||
self.state = None
|
||||
self.state_last = None
|
||||
def new_game(self):
|
||||
self.state = None
|
||||
self.state_last = None
|
||||
|
||||
def move(self, state: np.array):
|
||||
do_sample = False
|
||||
values = np.array([])
|
||||
# get possible move
|
||||
moves = self.get_potential_moves(state)
|
||||
if moves.size == 0:
|
||||
return state, False
|
||||
def move(self, state: np.array):
|
||||
do_sample = False
|
||||
values = np.array([])
|
||||
# get possible move
|
||||
moves = self.get_potential_moves(state)
|
||||
if moves.size == 0:
|
||||
return state, False
|
||||
|
||||
best_move = None
|
||||
best_value = -1
|
||||
for move in moves:
|
||||
# create hypothetical next state
|
||||
state_next = self.state_from_move(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)
|
||||
best_move = None
|
||||
best_value = -1
|
||||
for move in moves:
|
||||
# create hypothetical next state
|
||||
state_next = self.state_from_move(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)
|
||||
|
||||
next_move = best_move
|
||||
is_exp = False
|
||||
if uniform() < self.params.p_exp:
|
||||
index = np.random.randint(len(moves))
|
||||
next_move = moves[index]
|
||||
is_exp = best_move != next_move
|
||||
elif do_sample:
|
||||
index = sample(values)
|
||||
next_move = moves[index]
|
||||
next_move = best_move
|
||||
is_exp = False
|
||||
if uniform() < self.params.p_exp:
|
||||
index = np.random.randint(len(moves))
|
||||
next_move = moves[index]
|
||||
is_exp = best_move != next_move
|
||||
elif do_sample:
|
||||
index = sample(values)
|
||||
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.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()
|
||||
if self.state is not None:
|
||||
self.state_last = self.state.copy()
|
||||
|
||||
self.state = self.state_from_move(state.copy(), next_move)
|
||||
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.params.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}")
|
||||
# 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.params.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, True
|
||||
return self.state, True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user