Пример #1
0
	def __init__(self, offsetX, offsetY, canvasWidth, canvasHeight, N):
		self.master = Tk()
		self.offsetX = offsetX
		self.offsetY = offsetY
		self.canvasWidth = canvasWidth
		self.canvasHeight = canvasHeight
		self.width = self.canvasWidth - self.offsetX*2
		self.height = self.canvasHeight - self.offsetY*2
		self.N = N
		self.canvas = Canvas(self.master, width=self.canvasWidth, height=self.canvasHeight)
		self.drawCheckerBoard()
		self.predators = []
		self.preys = []
		#self.predatorFile = PhotoImage(file = "images/prey.gif")
		#self.preyFile = PhotoImage(file = "images/predator.gif")
		self.g = PredatorGame([(0,0),(10,10),(0,10),(10,0)], (5,5), (11,11))
		self.initAgents([(0,0),(10,10),(0,10),(10,0)],[(5,5)])
		# Draw the intitial state
		self.draw(self.g.predCoords, [self.g.preyCoord])
		# Learn a policy
		self.policy = None
		#self.V, self.c, self.policy = self.g.policyIteration(0.1, 0.00001)
		self.mainloop()
		
		self.canvas.mainloop()
Пример #2
0
import matplotlib.pyplot as plt
sys.path.append("..")
sys.path.append("./")
from predatorgame import PredatorGame

'''
Edit parameters
'''
discount = 0.1
threshold = 0.00001
'''
Do not edit below here
'''

start_time = time.time()
g = PredatorGame((0,0), (5,5), (11,11))
print "\n----------------\nExample: Policy Iteration\n----------------"               
preyPosition = (5,5)

V, c, policy = g.policyIteration(discount, threshold)

print "Iterations: " + str(c)

example = []
preds = [(a, b) for a in range(11) for b in range(11)]
for p in preds:
    example.append((p, preyPosition))

# for e in example:
#     print (str(e[0]) + "-->" +  str(round(V[e], 3)))
Пример #3
0
	def restartGame(self):
		self.g = PredatorGame([(0,0),(10,10),(10,0),(0,10)], (5,5), (11,11)) #TODO: This is honestly a bit of a hack
		self.canvas.after(800, self.mainloop)
Пример #4
0
class Board:


	def __init__(self, offsetX, offsetY, canvasWidth, canvasHeight, N):
		self.master = Tk()
		self.offsetX = offsetX
		self.offsetY = offsetY
		self.canvasWidth = canvasWidth
		self.canvasHeight = canvasHeight
		self.width = self.canvasWidth - self.offsetX*2
		self.height = self.canvasHeight - self.offsetY*2
		self.N = N
		self.canvas = Canvas(self.master, width=self.canvasWidth, height=self.canvasHeight)
		self.drawCheckerBoard()
		self.predators = []
		self.preys = []
		#self.predatorFile = PhotoImage(file = "images/prey.gif")
		#self.preyFile = PhotoImage(file = "images/predator.gif")
		self.g = PredatorGame([(0,0),(10,10),(0,10),(10,0)], (5,5), (11,11))
		self.initAgents([(0,0),(10,10),(0,10),(10,0)],[(5,5)])
		# Draw the intitial state
		self.draw(self.g.predCoords, [self.g.preyCoord])
		# Learn a policy
		self.policy = None
		#self.V, self.c, self.policy = self.g.policyIteration(0.1, 0.00001)
		self.mainloop()
		
		self.canvas.mainloop()

	'''
	Performs a step in the game and draws the canvas (automatically calls itself after 80ms)
	'''
	def mainloop(self):
		if(not self.g.state):
			# Draw a step in the game
			(test, newState), preyMove = self.g.step()
			#print str(self.g.predCoord) + " -- " + str(self.g.preyCoord)
			# Draw the state on the board
			self.draw(self.g.predCoords, [self.g.preyCoord])

			# Check if the game ended
			if newState != 1:
				self.canvas.after(80, self.mainloop)
			else:
				# Restart the game if the game ended
				self.restartGame()

	'''
	Restarts the game
	'''
	def restartGame(self):
		self.g = PredatorGame([(0,0),(10,10),(10,0),(0,10)], (5,5), (11,11)) #TODO: This is honestly a bit of a hack
		self.canvas.after(800, self.mainloop)

	'''
	Draws a checkerboard
	'''
	def drawCheckerBoard(self):
		self.canvas.pack()
		for x in range(0,self.N):
			for y in range(0,self.N):
				# Draws the checkers
				fillColor = 'white' if ((x + y*self.N) % 2 == 0) else "blue";
				self.canvas.create_rectangle(self.offsetX+x*self.width/self.N, self.offsetY+y*self.height/self.N, self.offsetX+(x+1)*self.width/self.N, self.offsetY+(y+1)*self.height/self.N, fill=fillColor)

	'''
	Draws the game screen
	'''
	def draw(self, predatorsCoords, preysCoords):
		# Predators
		for i in range(0,len(predatorsCoords)):
			self.movePredator(self.predators[i], predatorsCoords[i][0], predatorsCoords[i][1])
		# Preys
		for i in range(0,len(preysCoords)):
			self.movePrey(self.preys[i], preysCoords[i][0], preysCoords[i][1])

	'''
	Initialization of the predator and prey sprites
	'''
	def initAgents(self, predatorsCoords, preysCoords):
		# Predators
		for i in range(0,len(predatorsCoords)):
			self.drawPredator(predatorsCoords[i][0], predatorsCoords[i][1])
		# Preys
		for i in range(0,len(preysCoords)):
			self.drawPrey(preysCoords[i][0], preysCoords[i][1])

	'''
	Function to draw the predator
	'''
	def drawPredator(self, x, y):
		#self.predators.append(self.canvas.create_image(self.offsetX+x*self.width/self.N, self.offsetY+y*self.height/self.N, image=self.predatorFile))
		self.predators.append(self.canvas.create_rectangle(self.offsetX+x*self.width/self.N, self.offsetY+y*self.height/self.N, self.offsetX+(x+1)*self.width/self.N, self.offsetY+(y+1)*self.height/self.N, fill="red"))

	'''
	Function to draw the prey
	'''
	def drawPrey(self, x, y):
		#self.preys.append(self.canvas.create_image(self.offsetX+x*self.width/self.N, self.offsetY+y*self.height/self.N, image=self.preyFile))
		self.preys.append(self.canvas.create_rectangle(self.offsetX+x*self.width/self.N, self.offsetY+y*self.height/self.N, self.offsetX+(x+1)*self.width/self.N, self.offsetY+(y+1)*self.height/self.N, fill="green"))

	'''
	Function to move the predator sprite
	'''
	def movePredator(self, predator, x, y):
		#self.canvas.coords(predator, self.offsetX+x*self.width/self.N, self.offsetX+y*self.height/self.N)
		self.canvas.coords(predator, self.offsetX+x*self.width/self.N, self.offsetX+y*self.height/self.N, self.offsetX+(x+1)*self.width/self.N, self.offsetY+(y+1)*self.height/self.N)

	'''
	Function to move the prey sprite
	'''
	def movePrey(self, prey, x, y):
		##self.canvas.coords(prey, self.offsetX+x*self.width/self.N, self.offsetX+y*self.height/self.N)
		self.canvas.coords(prey, self.offsetX+x*self.width/self.N, self.offsetX+y*self.height/self.N, self.offsetX+(x+1)*self.width/self.N, self.offsetY+(y+1)*self.height/self.N)
Пример #5
0
import sys
import numpy as np
import time
sys.path.append("..")
sys.path.append("./")
from predatorgame import PredatorGame

print "\n----------------\nExample: Random Policy\n----------------"
start_time = time.time()
g = PredatorGame((0,0), (5,5), (11,11))
results = []
for i in range(100):
    g = PredatorGame((0,0), (5,5), (11,11))
    c = 0
    while (not g.state):
             c += 1
             g.step()
             #print(str(g.predCoord) + " -- " + str(g.preyCoord))

    results.append(c)        # c = 0

mean = np.mean(results)
stdv = np.std(results)
print "Min: " + str(min(results))
print "Max: " + str(max(results))
print "Mean: " + str(mean)
print "Standard deviation: " + str(stdv)
elapsed_time = time.time() - start_time
print("Elapsed time: " + str(elapsed_time))
Пример #6
0
import numpy as np
import time
import matplotlib.pyplot as plt
sys.path.append("..")
sys.path.append("./")
from predatorgame import PredatorGame

'''
Edit parameters
'''
discount = 0.8
threshold = 0.00001
'''
Do not edit below here
'''

start_time = time.time()
g = PredatorGame((0,0), (5,5), (11,11))

print "\n----------------\nExample: Iterative Policy Evaluation\n----------------"           

V, c = g.iterativePolicyEvaluation(discount, threshold)

targets = [((0,0), (5,5)), ((2,3), (5,4)), ((2,10), (10,0)), ((10,10), (0,0))]

for ex in targets:
    print str(ex) +  ": "  + str(V[g.getState(*ex)])
print "Iterations: " + str(c)

elapsed_time = time.time() - start_time
print("Elapsed time: " + str(elapsed_time))