I'm trying to make a simple tictactoe Qlearning bot, but it seems like he's not evolving, i probably messed with the saving or the upadting of the qtable, but i can't find the bug myself (I tried...for several hours actually)
I'm sorry if my code is messy :
Qlearn.py
import numpy as np
import coregame
import AIbasiques
class QlearnAI:
def init(self, alpha=0.9, gamma=0.9, epsilon=0.1, n_actions=9):
self.alpha = alpha
self.gamma = gamma
self.epsilon = epsilon
self.n_actions = n_actions
self.q_table = np.zeros((3**n_actions, n_actions))
self.load("Qtable.npy")
def get_state(self, state):
return np.sum([3**i * s for i, s in enumerate(state)])
def get_action(self, state):
if np.random.rand() < self.epsilon:
return np.random.randint(self.n_actions)
else:
state = self.get_state(state)
return int(np.argmax(self.q_table[state]))
def update(self, state, action, reward, next_state):
state = self.get_state(state)
next_state = self.get_state(next_state)
self.q_table[state, action] = (1 - self.alpha) * self.q_table[state, action] + self.alpha * (reward + self.gamma * np.max(self.q_table[next_state]))
def save(self, filename):
np.save(filename, self.q_table)
def load(self, filename):
try:
self.q_table = np.load(filename,allow_pickle=True)
except FileNotFoundError:
self.q_table = {}
def print(self):
print(self.q_table)
This is my first file, i got an other one where the pb may be, but 100 lines, discord didn't wanted ...