summaryrefslogtreecommitdiff
path: root/qlearning.py
diff options
context:
space:
mode:
Diffstat (limited to 'qlearning.py')
-rwxr-xr-xqlearning.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/qlearning.py b/qlearning.py
index 63ce70c..c731253 100755
--- a/qlearning.py
+++ b/qlearning.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-import sys,random,os
+import sys,random,os,statistics
import numpy as np
# Import snake game
@@ -119,7 +119,7 @@ class QTable:
generation+=self.save_every
with open(self.file+"_generation","w") as f:
f.write(str(generation))
- print("Checkpointing generation "+str(generation))
+ print("----------------------------- Checkpointing generation "+str(generation))
self.save_counter=0
def get_action(self,state):
@@ -140,8 +140,9 @@ class QTable:
# Perform learning
-width,height=10,10
+width,height=40,30 # My advice is start with a small grid 5x5 to have many interaction and avoid early toy effect
perf=0
+perf_list=list()
last_state=None
last_action=None
game=Snake(length=1,fps=500,startat=(random.randint(0,width-1),random.randint(0,height-1)),grid_width=width,grid_height=height)
@@ -172,8 +173,8 @@ while True:
if stuck>=(game.grid_width*game.grid_height)/stuck_tolerance:
stuck=0
stuck_count+=1
- action=qtable.get_random_action()
- print("Stuck! Try a random action...")
+ game.new_apple()
+ print("Stuck! Try with a new apple...")
if stuck_count>2:
print("Can't get out of stuck. Abort!")
stuck_count=0
@@ -187,5 +188,6 @@ while True:
# Measurements
score=game.last_score
+ perf_list.append(score)
perf=max(perf,score)
- print("Game ended with "+str(score)+" best so far is "+str(perf)) \ No newline at end of file
+ print("Game ended with "+str(score)+" best so far is "+str(perf)+ " median is "+str(statistics.median(perf_list))) \ No newline at end of file