Commit 1946e957 authored by Julian Rudolf's avatar Julian Rudolf
Browse files

fixed best_direction functions

parent b2b40999
......@@ -62,11 +62,9 @@ class QLearning:
best_val = val
else:
self.qTable[(state, dir)] = 0
if 0 > best_val:
best_dir = dir
best_val = 0
#print(self.qTable[(state, dir)], " ", end='')
#print("")
if best_val == -100000:
print("new state -> random")
return best_dir
......@@ -75,6 +73,8 @@ class QLearning:
poss_actions = self.possible_directions()
rand_action = random.choice(poss_actions)
best_direction = self.best_direction(state, poss_actions)
if best_direction == "none":
best_direction = rand_action
self.epsilon = self.epsilon - 0.0001
if random.random() > self.epsilon:
......@@ -84,11 +84,6 @@ class QLearning:
#print("exploration ", rand_action)
return rand_action
# prints qTable
def print_table(self):
dirs = ("left", "right", "up", "down")
print(dirs)
# returns all possible actions
def possible_directions(self):
poss_actions = []
......@@ -161,16 +156,18 @@ def learning(al, ga, ep, games):
def choose_best_direction(qagent, state):
dirs = qagent.possible_directions()
best_dir = "none"
best_val = -10000
best_val = -100000
for dir in dirs:
if (state, dir) in qagent.qTable:
val = qagent.qTable[(state, dir)]
if val > best_val:
best_val = val
best_dir = dir
else:
print("(state, dir) not in qtable")
val = -1000
if val > best_val:
best_val = val
best_dir = dir
if best_val == -100000:
return random.choice(dirs)
return best_dir
......@@ -242,9 +239,9 @@ def load_qtable():
alpha = 0.1
gamma = 0.5
epsilon = 0.3
rounds = 800
#agent, q0, poss_dirs0 = learning(alpha, gamma, epsilon, rounds)
#save_qtable(agent.qTable)
rounds = 600
agent, q0, poss_dirs0 = learning(alpha, gamma, epsilon, rounds)
save_qtable(agent.qTable)
#testing(agent, 50, q0, poss_dirs0, True)
#testing(agent, 50, q0, poss_dirs0)
......@@ -518,14 +518,14 @@ class Snake:
#print(dirs)
for dir in dirs:
if (state, dir) in qtable:
#print("Value found in qTable")
val = qtable[(state, dir)]
else:
print("(state, dir) not in qtable")
val = -1000
if val > best_val:
best_val = val
best_dir = dir
if val > best_val:
best_val = val
best_dir = dir
if best_val == -10000:
print("no entry found")
return random.choice(dirs)
return best_dir
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment