Refactored

This commit is contained in:
2024-06-10 20:48:50 +02:00
parent 285549568d
commit 61d1ab0431
4 changed files with 47 additions and 44 deletions
+4 -38
View File
@@ -1,4 +1,5 @@
import numpy as np
from state import f_state_slices
class APlayer(object):
@@ -14,17 +15,11 @@ class APlayer(object):
def set_debug(self, with_debug):
pass
@staticmethod
def get_potential_moves(state: np.array) -> np.array:
st = state.reshape(state.size)
indices = [idx for idx, s in enumerate(st) if '-' in s]
return np.array(indices)
def move(self, state: np.array):
return state, False
def state_from_move(self, state: np.array, field):
state.reshape(state.size)[field] = self.mark
def to_state(self, state: np.array, move):
state.reshape(state.size)[move] = self.mark
return state
def reward(self, value):
@@ -33,39 +28,10 @@ class APlayer(object):
def new_game(self):
pass
@staticmethod
def f_state_slices():
nr = 3
nc = 3
result = []
# Create row finishing states
d1 = ()
d2_r = ()
for r in range(0, nr):
d1 += (r,)
for c in range(0, nc):
d2 = (c,) * nc
result.append((d1, d2))
# Create column finishing states
for c in range(0, nc):
d2 = (c,) * nc
result.append((d2, d1))
for c in range(0, nc):
d2_r += (nc - c - 1,)
# Create diagonal finishing states #1
result.append((d1, d1))
# Create diagonal finishing states #2
result.append((d1, d2_r))
return result
def has_won(self, state: np.array):
result = False
ref = [self.mark, self.mark, self.mark]
for f_state_slice in APlayer.f_state_slices():
for f_state_slice in f_state_slices():
if np.all(state[f_state_slice] == ref):
result = True
break
+3 -2
View File
@@ -2,6 +2,7 @@ import numpy as np
from a_player import APlayer
from helper import to_state_string, create_empty_state, create_test_state
from state import get_potential_moves
class HumanPlayer(APlayer):
@@ -29,7 +30,7 @@ class HumanPlayer(APlayer):
continue
move = int(choice) - 1
if not move in self.get_potential_moves(state):
if not move in get_potential_moves(state):
print("Feld is bereits belegt!")
print("Versuche es nochmal")
continue
@@ -38,7 +39,7 @@ class HumanPlayer(APlayer):
has_moved = True
break
state_next = self.state_from_move(state.copy(), move)
state_next = self.to_state(state.copy(), move)
return state_next, has_moved
def reward(self, value):
+4 -4
View File
@@ -2,7 +2,7 @@ import numpy as np
from a_player import APlayer
from helper import sample
from numpy.random import uniform
from state import get_potential_moves
class MachinePlayer(APlayer):
class Params:
@@ -56,7 +56,7 @@ class MachinePlayer(APlayer):
do_sample = False
values = np.array([])
# get possible move
moves = self.get_potential_moves(state)
moves = get_potential_moves(state)
if moves.size == 0:
return state, False
@@ -64,7 +64,7 @@ class MachinePlayer(APlayer):
best_value = -1
for move in moves:
# create hypothetical next state
state_next = self.state_from_move(state.copy(), move)
state_next = self.to_state(state.copy(), move)
# evaluate value
value = self.get_value(state_next)
if best_value < value:
@@ -91,7 +91,7 @@ class MachinePlayer(APlayer):
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.to_state(state.copy(), next_move)
# Learn
if not is_exp and self.state_last is not None:
+36
View File
@@ -0,0 +1,36 @@
import numpy as np
def get_potential_moves(state: np.array) -> np.array:
st = state.reshape(state.size)
indices = [idx for idx, s in enumerate(st) if '-' in s]
return np.array(indices)
def f_state_slices():
nr = 3
nc = 3
result = []
# Create row finishing states
d1 = ()
d2_r = ()
for r in range(0, nr):
d1 += (r,)
for c in range(0, nc):
d2 = (c,) * nc
result.append((d1, d2))
# Create column finishing states
for c in range(0, nc):
d2 = (c,) * nc
result.append((d2, d1))
for c in range(0, nc):
d2_r += (nc - c - 1,)
# Create diagonal finishing states #1
result.append((d1, d1))
# Create diagonal finishing states #2
result.append((d1, d2_r))
return result