Save values to file
This commit is contained in:
+41
-40
@@ -61,52 +61,53 @@ 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)
|
||||
# evaluate value
|
||||
value = self.get_value(state_next)
|
||||
if best_value < value:
|
||||
best_value = value
|
||||
best_move = move
|
||||
values = np.append(values, value)
|
||||
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.p_exp:
|
||||
is_exp = True
|
||||
index = np.random.randint(len(moves))
|
||||
next_move = moves[index]
|
||||
elif do_sample:
|
||||
index = sample(values)
|
||||
next_move = moves[index]
|
||||
next_move = best_move
|
||||
is_exp = False
|
||||
if uniform() < self.p_exp:
|
||||
is_exp = True
|
||||
index = np.random.randint(len(moves))
|
||||
next_move = moves[index]
|
||||
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.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:
|
||||
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}")
|
||||
print(f"{self.mark}: Learned {d:0.3f}")
|
||||
|
||||
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:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user