Minor changes
This commit is contained in:
parent
b7e71e6690
commit
01db5773d5
3 changed files with 592 additions and 590 deletions
14
qlearning.py
14
qlearning.py
|
@ -1,5 +1,5 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import sys,random,os
|
import sys,random,os,statistics
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
# Import snake game
|
# Import snake game
|
||||||
|
@ -119,7 +119,7 @@ class QTable:
|
||||||
generation+=self.save_every
|
generation+=self.save_every
|
||||||
with open(self.file+"_generation","w") as f:
|
with open(self.file+"_generation","w") as f:
|
||||||
f.write(str(generation))
|
f.write(str(generation))
|
||||||
print("Checkpointing generation "+str(generation))
|
print("----------------------------- Checkpointing generation "+str(generation))
|
||||||
self.save_counter=0
|
self.save_counter=0
|
||||||
|
|
||||||
def get_action(self,state):
|
def get_action(self,state):
|
||||||
|
@ -140,8 +140,9 @@ class QTable:
|
||||||
|
|
||||||
|
|
||||||
# Perform learning
|
# 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=0
|
||||||
|
perf_list=list()
|
||||||
last_state=None
|
last_state=None
|
||||||
last_action=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)
|
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:
|
if stuck>=(game.grid_width*game.grid_height)/stuck_tolerance:
|
||||||
stuck=0
|
stuck=0
|
||||||
stuck_count+=1
|
stuck_count+=1
|
||||||
action=qtable.get_random_action()
|
game.new_apple()
|
||||||
print("Stuck! Try a random action...")
|
print("Stuck! Try with a new apple...")
|
||||||
if stuck_count>2:
|
if stuck_count>2:
|
||||||
print("Can't get out of stuck. Abort!")
|
print("Can't get out of stuck. Abort!")
|
||||||
stuck_count=0
|
stuck_count=0
|
||||||
|
@ -187,5 +188,6 @@ while True:
|
||||||
|
|
||||||
# Measurements
|
# Measurements
|
||||||
score=game.last_score
|
score=game.last_score
|
||||||
|
perf_list.append(score)
|
||||||
perf=max(perf,score)
|
perf=max(perf,score)
|
||||||
print("Game ended with "+str(score)+" best so far is "+str(perf))
|
print("Game ended with "+str(score)+" best so far is "+str(perf)+ " median is "+str(statistics.median(perf_list)))
|
1166
qtable.txt
1166
qtable.txt
File diff suppressed because it is too large
Load diff
|
@ -1 +1 @@
|
||||||
15000
|
400000
|
Loading…
Add table
Reference in a new issue