Example #1
0
    def Qtesting(self):
        self.resetGameForTraining()
        while self.isGameOver() == False:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameExit = True
            self.sendStateVars()
            self.calc_receive()
            for keeper in self.keeperArray:
                keeper.isInTraining = False
                keeper.decisionFlowChart()
            for taker in self.takerArray:
                taker.decisionFlowChart()
            
            newBallPoint = kUtil.addVectorToPoint(self.fieldBall.trueBallPos, kUtil.scalarMultiply(self.maxBallSpeed, kUtil.unitVector(self.fieldBall.trueBallDirection)))
            self.fieldBall.updateCoordinate(newBallPoint)
            for i in range(len(self.takerArray)):
                self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.takerArray[i].sigma)
            for i in range(len(self.keeperArray)):
                self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.keeperArray[i].sigma)                
            self.updateBallPosession()
            self.updateScore()
            self.drawWorld ()
            self.displayScore()
            pygame.display.update()
            #this specifies frames per second
            self.clock.tick(self.fps)
    
        if self.isGameOver() == True:
            gameExit = True
        print("final score: ", self.keeperScore)

        self.finish()
        #self.pause("Game Over: Final Score %d" % self.keeperScore)
        self.exitSim()
Example #2
0
 def resetGameForTraining(self):
     #reset the ball variables
     row = random.randint(0, int(self.display_height/2))
     col = random.randint(int(self.display_width/4), int (self.display_width/4 * 3))
     self.fieldBall.updateCoordinate((row,col))
     self.fieldBall.update((0.0,0.0))
     
     #reset the score
     self.keeperScore = 0
     
     #now reset the agent variables:
     self.keeperArray[0].true_pos = (12.5, 12.5)
     self.keeperArray[1].true_pos = (25,  self.display_width - 37.5)
     self.keeperArray[2].true_pos = (self.display_height - 37.5,  self.display_width - 37.5)
     self.takerArray[0].true_pos = (self.display_height - 25,  25)
     self.takerArray[1].true_pos = (self.display_height - 37.5,  50)
     
     for i in range(len(self.keeperArray)):
         self.keeperArray[i].noisy_pos = kUtil.getNoisyVals(self.keeperArray[i].true_pos, self.keeperArray[i].sigma)
         self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos,self.keeperArray[i].sigma)
         self.keeperArray[i].inPosession = False
         self.keeperArray[i].isKicking = False
         
     for i in range(len(self.takerArray)):
         self.takerArray[i].noisy_pos = kUtil.getNoisyVals(self.takerArray[i].true_pos, self.takerArray[i].sigma)
         self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.takerArray[i].sigma)
         self.takerArray[i].inPosession = False
         self.takerArray[i].isKicking = False
Example #3
0
	def __boundSimpleVars(self, noisySimpleStateVars):
		"""
		For the sake of simulating sensor error, some noise is added to the state variables
		that are calculated. However, the last 2 state variables are cosine values. Cosine values
		are bounded [-1.0, 1.0], so this function was made to ensure that the last 2 state variables are 
		bounded by [-1.0, 1.0] after noise is added to it. This is important because the arccos
		of these 2 state variables is calculated sometimes, and if the value input into arccos
		just so happens to be out of this range,the program will crash. 
		
		:param noisySimpleStateVars: The noisy version of the simple state variables. This should
		    be calculated in the sendSimpleStateVars function of keepaway.py. 
		
		:type noisySimpleStateVars: tuple or list of floats
		
		:returns: the noisySimpleStateVars, but with the last 2 parameters bounded by [-1.0, 1.0]
		
		:rtype: tuple or list of floats
		"""
		varIndex11 = noisySimpleStateVars[11]
		varIndex12 = noisySimpleStateVars[12]
		while (varIndex11 > 1.0 or varIndex11 <  -1.0):
			varIndex11 = kUtil.getNoisyVals(noisySimpleStateVars[11], self.agentSigmaError)
		while (varIndex12 > 1.0 or varIndex12 <  -1.0):
			varIndex12 = kUtil.getNoisyVals(noisySimpleStateVars[12], self.agentSigmaError)
		if varIndex11 > 1.0 or varIndex11 < -1.0 or varIndex12 > 1.0 or varIndex12 < -1.0:
			print("Values still out of range: varIndex11 = ", varIndex11, ", varIndex12 = ", varIndex12)
		return noisySimpleStateVars[:11] + (varIndex11, varIndex12)
Example #4
0
	def __moveAgent(self, inputAgent, movementVector):
		"""
		This is a private function that is called by the moveAttempt function.
		this function is only called if a move is determined to be legal. Once
		the move is determined to be legal, this function will go and update the
		position of the agent on the field.
		
		:param inputAgent: a reference to the agent that is is being moved.
		:param movementVector: the vector that will be added to the inputAgent's
		    current position. The sum of the current position and the movementVector
		    will be the new position of the inputAgent.
		    
		:type inputAgent: agent
		:type movementVector: a tuple of floats
		
		:returns: no return
		
		.. note::
		    This is a private function that the user shouldn't worry about calling.
		    Only the move_attempt() function should be called.  
		"""
		if inputAgent.getAgentType() == "keeper":
			newNoiseFreePos = kUtil.addVectorToPoint(self.keeperTruePosArray[inputAgent.getSimIndex()], movementVector)
			self.keeperTruePosArray[inputAgent.getSimIndex()] = newNoiseFreePos
		elif inputAgent.getAgentType() == "taker":
			newNoiseFreePos = kUtil.addVectorToPoint(self.takerTruePosArray[inputAgent.getSimIndex()], movementVector)
			self.takerTruePosArray[inputAgent.getSimIndex()] = newNoiseFreePos

		inputAgent.updateAgentPosition(kUtil.getNoisyVals(newNoiseFreePos, self.agentSigmaError))    
Example #5
0
	def _passBall(self, pointToPassTo):
		'''
		If a keeper currently has the ball, then it has the option to hold the ball,
		or pass it. Call this function to pass the ball. pointToPassTo is the coordinate that
		the keeper can pass to. these are pixel values 

		:param pointToPassTo: the tile that the agent is passing to. It is an XY coordinate defined to be the top left 
			corner of the tile you're trying to pass to

		:type pointToPassTo: typle of int

		:returns: no return 
		'''
		if self.fieldBall == None:
			print("ERROR: trying to hold ball without actually having  ball")
			return
	
		#pass to team mate integerK. if integerK = 1, then it's the 2nd element in array
		selfToTargetDirection = kUtil.unitVector(kUtil.getVector(self.__getBallCenter(self.noisyBallPos), pointToPassTo))
		selfToTargetVector = (kUtil.scalarMultiply(self.fieldBall.maxBallSpeed, selfToTargetDirection))

			
		#at this point, you've determined the angle you wanna pass, and you pass.
		#set ball's possesion to false, and update to new point. ball class handles direction vector
		self.fieldBall.updatePosession(False)
		self.inPosession = False
		self.isKicking = True
		#kUtil.addVectorToPoint(self.fieldBall.trueBallPos, selfToTeammateVector)
		self.fieldBall.updateDirection(kUtil.getNoisyVals(selfToTargetVector, self.getSigma()))
Example #6
0
    def sendCalcReceiveDecision(self):
        """
        This public function will send all keepers the receive decision. For more information
        on what the receive decision is, refer to the documentation of the module "calcReceive"

        :returns: no return
        """
        rDecision = calcReceive.calc_receive(self)
        print("rDecision decided upon:")
        print(rDecision)
        for i in range(len(self.keeperArray)):
            rNoisyDecision = (rDecision[0], kUtil.getNoisyVals(rDecision[1], self.agentSigmaError))
            self.keeperArray[i].receiveDecision(rNoisyDecision)
        for i in range(len(self.takerArray)):
            rNoisyDecision = (rDecision[0], kUtil.getNoisyVals(rDecision[1], self.agentSigmaError))
            self.takerArray[i].receiveDecision(rNoisyDecision)
Example #7
0
	def _sendCalcReceiveDecision(self):
		"""
		This public function will send all keepers the receive decision. For more information
		on what the receive decision is, refer to the documentation of the module "calcReceive"

		:returns: no return
		"""  
		#note: rDecision is only global so that it can be debugged. Once debugging is done, make it local again
		rDecision = calcReceive.calc_receive(self)
		#print("rDecision decided upon:")
		#print(rDecision)
		for i in range(len(self.keeperArray)):
			rNoisyDecision= (rDecision[0], kUtil.getNoisyVals(rDecision[1], self.agentSigmaError))
			self.keeperArray[i].receiveDecision(rNoisyDecision)
		for i in range(len(self.takerArray)):
			rNoisyDecision= (rDecision[0], kUtil.getNoisyVals(rDecision[1], self.agentSigmaError))
			self.takerArray[i].receiveDecision(rNoisyDecision)
Example #8
0
 def __init__(self, worldRef, pos, sigma, agentType, trueBallPos, maxPlayerSpeed, maxBallSpeed, inPossession = False):
     self.sigma = sigma
     self.true_pos = pos
     self.noisy_pos = kUtil.getNoisyVals(self.true_pos, self.sigma)
     self.agentType = agentType
     self.maxPlayerSpeed = maxPlayerSpeed
     self.keeperArray = None
     self.takerArray = None
     self.agentListIndex = None
     self.stateVariables = None
     self.onReceiveDecision = None #receive variables
     self.worldRef = worldRef
     
     #BALL VARIABLES
     self.noisyBallPos = kUtil.getNoisyVals(trueBallPos, self.sigma)
     self.maxBallSpeed = maxBallSpeed
     self.fieldBall = None
     self.inPosession = False
     self.isKicking = False
Example #9
0
	def _sendSimpleStateVars(self):
		"""
		This public function will send all keepers the simple state variables.
		This function should be called for intelligent agents such as NEAT, 
		or Sarsa. This function will NOT be called for hyperNEAT, which 
		will instead, get a birds eye view of the entire field

		:returns: no return
		"""  
		#get the state variables
		self.simpleStateVars = getSimpleStateVars.getStateVarsKeepers(self.keeperArray, self.takerArray, self.__field_center)
		#send the state variables to each keeper and taker
		for i in range(len(self.keeperArray)):
			noisyCurrVars = kUtil.getNoisyVals(self.simpleStateVars, self.agentSigmaError)
			noisyCurrVars = self.__boundSimpleVars(noisyCurrVars)
			self.keeperArray[i].receiveSimpleStateVariables(noisyCurrVars)
		for i in range(len(self.takerArray)):
			noisyCurrVars = kUtil.getNoisyVals(self.simpleStateVars, self.agentSigmaError)
			noisyCurrVars = self.__boundSimpleVars(noisyCurrVars)
			self.takerArray[i].receiveSimpleStateVariables(noisyCurrVars)
Example #10
0
    def resetGameForTraining(self):
        """
        This is a public function that is meant for resetting the simulator when in 
        training mode. Training mode will be used by the intelligent agents such as
        SARSA, NEAT, hyperNEAT, etc.
        
        :returns: no return 
        """
        # reset the ball variables
        row = random.randint(0, int(self.display_height / 2))
        col = random.randint(int(self.__display_width / 4), int(self.__display_width / 4 * 3))
        self.fieldBall.updateCoordinate((row, col))
        self.fieldBall.update((0.0, 0.0))

        # reset the score
        self.keeperScore = 0

        # now reset the agent positions:
        self.keeperTruePosArray[0] = (12.5, 12.5)
        self.keeperTruePosArray[1] = (25, self.__display_width - 37.5)
        self.keeperTruePosArray[2] = (self.display_height - 37.5, self.__display_width - 37.5)
        self.takerTruePosArray[0] = (self.display_height - 25, 25)
        self.takerTruePosArray[1] = (self.display_height - 37.5, 50)

        for i in range(len(self.keeperArray)):
            self.keeperArray[i].updateAgentPosition(
                kUtil.getNoisyVals(self.keeperTruePosArray[i], self.agentSigmaError)
            )
            self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(
                self.fieldBall.trueBallPos, self.keeperArray[i].getSigma()
            )
            self.keeperArray[i].inPosession = False
            self.keeperArray[i].isKicking = False

        for i in range(len(self.takerArray)):
            self.takerArray[i].updateAgentPosition(kUtil.getNoisyVals(self.takerTruePosArray[i], self.agentSigmaError))
            self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(
                self.fieldBall.trueBallPos, self.takerArray[i].getSigma()
            )
            self.takerArray[i].inPosession = False
            self.takerArray[i].isKicking = False
Example #11
0
 def QTraining(self,totalTraining):
     for training in range(totalTraining):
         print("Training number :",training)
         if training%100 == 0:
             self.displayGraphics = True
         else:
             self.displayGraphics = False
         self.resetGameForTraining()
         while self.isGameOver() == False:
             for event in pygame.event.get():
                 if event.type == pygame.QUIT:
                     gameExit = True
             self.sendStateVars()
             reward = 100000
             for keeper in self.keeperArray:
                 keeper.updateReward(reward) 
             self.calc_receive()
             for keeper in self.keeperArray:
                 keeper.isInTraining = True
                 keeper.decisionFlowChart()
             for taker in self.takerArray:
                 taker.decisionFlowChart()
                 
             newBallPoint = kUtil.addVectorToPoint(self.fieldBall.trueBallPos, kUtil.scalarMultiply(self.maxBallSpeed, kUtil.unitVector(self.fieldBall.trueBallDirection)))
             self.fieldBall.updateCoordinate(newBallPoint)
             for i in range(len(self.takerArray)):
                 self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.takerArray[i].sigma)
             for i in range(len(self.keeperArray)):
                 self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.keeperArray[i].sigma)                
             self.updateBallPosession()
             self.updateScore()
             if(self.displayGraphics == True):
                 self.drawWorld ()
                 self.displayScore()
                 pygame.display.update()
             if self.isGameOver():
                 reward = -100
                 self.sendStateVars()
                 for keeper in self.keeperArray:
                     keeper.updateFinalReward(reward)
             self.clock.tick(10000)
Example #12
0
	def commonFunctionality(self, mode, showDisplay = True, turnOnGrid = False, prettyGrid = False):
		#this is common code that will occur regardless of what agent you picked
		#if (self.fieldBall.inPosession == False):
		newBallPoint = kUtil.addVectorToPoint(self.fieldBall.trueBallPos, kUtil.scalarMultiply(self.maxBallSpeed, kUtil.unitVector(self.fieldBall.trueBallDirection)))
		self.fieldBall.updateCoordinate(newBallPoint)
		for i in range(len(self.takerArray)):
			self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.takerArray[i].getSigma())
		for i in range(len(self.keeperArray)):
			self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.keeperArray[i].getSigma())                
		self.__updateBallPosession()
		self.__updateScore()
		#remove this line if you don't want the grid to be drawn
		if showDisplay:
			if (turnOnGrid):
				gridList = self.bev.getBirdsEyeViewAsList(self.keeperArray, self.takerArray)
				substrate = self.bev.getSubstrate(self.keeperArray, self.takerArray)
				self.__drawWorld (mode, gridList, substrate, prettyGrid)
			else:
				self.__drawWorld(mode)
			self.__displayScore()
			pygame.display.update()
Example #13
0
	def resetGameForTraining(self):
		"""
		This is a public function that is meant for resetting the simulator when in 
		training mode. Training mode will be used by the intelligent agents such as
		SARSA, NEAT, hyperNEAT, etc.
		
		All it does is reset the ball position, the agent positions, and also sets the
		individual agents "inPosession", and "isKicking" flags to false, to indicate 
		that there are no agents that have posession of the ball, nor are there
		any agents kicking the ball. 
		
		:returns: no return 
		"""
		#reset the ball variables
		#row = random.randint(0, int(self.__display_height/2))
		#col = random.randint(int(self.__display_width/4), int (self.__display_width/4 * 3))
		self.fieldBall.updateCoordinate((self.__field_center[0]/4, self.__field_center[1]/4))
		self.fieldBall.updateDirection((0.0,0.0))
		
		#reset the score
		self.keeperScore = 0
		
		#now reset the agent positions:
		self.keeperTruePosArray[0] = (12.5, 12.5)
		self.keeperTruePosArray[1] = (25,  self.__display_width - 37.5)
		self.keeperTruePosArray[2] = (self.__display_height - 37.5,  self.__display_width - 37.5)
		self.takerTruePosArray[0] = (self.__display_height - 25,  25)
		self.takerTruePosArray[1] = (self.__display_height - 37.5,  50)
		
		for i in range(len(self.keeperArray)):
			self.keeperArray[i].updateAgentPosition(kUtil.getNoisyVals(self.keeperTruePosArray[i], self.agentSigmaError)) 
			self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos,self.keeperArray[i].getSigma())
			self.keeperArray[i].inPosession = False
			self.keeperArray[i].isKicking = False

		for i in range(len(self.takerArray)):
			self.takerArray[i].updateAgentPosition(kUtil.getNoisyVals(self.takerTruePosArray[i], self.agentSigmaError)) 
			self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.takerArray[i].getSigma())
			self.takerArray[i].inPosession = False
			self.takerArray[i].isKicking = False
Example #14
0
    def gameLoop(self, mode):
        """
        This is the main game loop. Each iteration of this counts as a tick. 
        With each tick, an agent can move keepAway.maxPlayerSpeed units, and the
        ball can move keepAway.maxBallSpeed units. At the end of each tick, the
        pygame screen is updated to the next frame. 
        
        :returns: no return
        """
        self.__drawWorld()
        gameExit = False
        pygame.display.update()
        experimentAgent = self.keeperArray[0]
        # each occurance of this loop is treated as one simulation cycle
        while not gameExit:
            if mode == "manual":
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        gameExit = True
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_LEFT:
                            self.moveAttempt(experimentAgent, ((0, -1), self.maxPlayerSpeed))
                        elif event.key == pygame.K_RIGHT:
                            self.moveAttempt(experimentAgent, ((0, 1), self.maxPlayerSpeed))
                        elif event.key == pygame.K_UP:
                            self.moveAttempt(experimentAgent, ((-1, 0), self.maxPlayerSpeed))
                        elif event.key == pygame.K_DOWN:
                            self.moveAttempt(experimentAgent, ((1, 0), self.maxPlayerSpeed))
                        elif event.key == pygame.K_1:
                            self.moveAttempt(experimentAgent, ((-1, -1), self.maxPlayerSpeed))
                        elif event.key == pygame.K_2:
                            self.moveAttempt(experimentAgent, ((-1, 1), self.maxPlayerSpeed))
                        elif event.key == pygame.K_3:
                            self.moveAttempt(experimentAgent, ((1, -1), self.maxPlayerSpeed))
                        elif event.key == pygame.K_4:
                            self.moveAttempt(experimentAgent, ((1, 1), self.maxPlayerSpeed))
            elif mode == "hand_coded":
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        gameExit = True
                self.sendCalcReceiveDecision()
                self.sendSimpleStateVars()
                for keeper in self.keeperArray:
                    keeper.decisionFlowChart()
                for taker in self.takerArray:
                    taker.decisionFlowChart()
            elif mode == "sarsa":
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        gameExit = True
                self.sendCalcReceiveDecision()
                self.sendSimpleStateVars()

            # this is common code that will occur regardless of what agent you picked
            # if (self.fieldBall.inPosession == False):
            newBallPoint = kUtil.addVectorToPoint(
                self.fieldBall.trueBallPos,
                kUtil.scalarMultiply(self.maxBallSpeed, kUtil.unitVector(self.fieldBall.trueBallDirection)),
            )
            self.fieldBall.updateCoordinate(newBallPoint)
            for i in range(len(self.takerArray)):
                self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(
                    self.fieldBall.trueBallPos, self.takerArray[i].getSigma()
                )
            for i in range(len(self.keeperArray)):
                self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(
                    self.fieldBall.trueBallPos, self.keeperArray[i].getSigma()
                )
            self.__updateBallPosession()
            self.__updateScore()
            self.__drawWorld()
            self.__displayScore()
            pygame.display.update()

            if self.isGameOver() == True:
                gameExit = True
                print("final score: ", self.keeperScore)
            # this specifies frames per second
            self.clock.tick(self.test_fps)
        self.__finish()
        # self.pause("Game Over: Final Score %d" % self.keeperScore)
        self.__exitSim()
Example #15
0
	def __init__(self, inputAgentSigmaNoise = .1, alreadyTrained = True, bevCustomTileSize = None):
		pygame.init()
		#RGB color
		self.__white = (255,255,255) 
		self.__black = (0,0,0)
		self.__red = (255,0,0)
		self.__green = (0,155,0)
		self.__blue = (0,0,255)
		
		#give the game a title
		pygame.display.set_caption('Keepaway')
		self.keeperScore = 0
		
		#these are more or less global variables..
		#I'm not sure if this is bad or not. 
		self.__worldImage = pygame.image.load('images/soccer_field.png')
		self.__ballImage = pygame.image.load('images/ball.png')
		self.__keeperImage = pygame.image.load('images/keeper.png')
		self.__keeperGoldImage = pygame.image.load('images/keeperGold.png')
		self.__takerImage = pygame.image.load('images/taker.png')
		self.__predictedImage = pygame.image.load('images/x.png')
		self.__debugYellowDotImage = pygame.image.load('images/yellow_dot.png')
		self.__debugRedDotImage = pygame.image.load('images/red_dot.png')
		self.__debugBlackDotImage = pygame.image.load('images/black_dot.png')
		self.__debugWhiteDotImage = pygame.image.load('images/white_dot.png')
		self.__debugBlueDotImage = pygame.image.load('images/blue_dot.png')
		self.__debugTakerPathTile = pygame.image.load('images/takerPathSquare.png')
		self.__debugKeeperPathTile = pygame.image.load('images/keeperPathSquare.png')
		self.__debugKeeperTile = pygame.image.load('images/keeperSquare.png')
		self.__debugTakerTile = pygame.image.load('images/takerSquare.png')
		self.__debugEmptyTile = pygame.image.load('images/emptySquare.png')
		self.__debugTakerPathTileTwo = pygame.image.load('images/takerPathSquare2.png')
		self.__debugKeeperPathTileTwo = pygame.image.load('images/keeperPathSquare2.png')
		#block sizes are used for collision detection
		#only 1 size per element because all blocks are squares. block size = side length
		self.__agent_block_size = 23
		self.__ball_block_size = 12

		self.maxBallSpeed= 4
		self.maxPlayerSpeed = 2
		#self.rDecision = None

		
		#dimensions of the game are the same as the soccer field image
		self.__display_width = 550
		self.__display_height = 357
		self.displayGraphics = True
		self.__field_center = (self.__display_width / 2 , self.__display_height / 2)
		#gameDisplay is a pygame.surface object. it's your screen
		self.gameDisplay = pygame.display.set_mode((self.__display_width,self.__display_height))
		self.test_fps = 60
		self.train_fps = 10000
		self.clock = pygame.time.Clock()
		
		
		#start the ball kinda close to the keeper in the upper left corner
		self.fieldBall = ball.ball( (self.__field_center[0]/4, self.__field_center[1]/4), self.maxBallSpeed)
		
		#the simple state variables for agents like NEAT, novelty search, and maybe sarsa
		self.simpleStateVars = None
		
		self.alreadyTrained = alreadyTrained  #False if you want agent to learn and True if you want to demo
		
		#setup all the initial keepers and takers. They are all starting at different field positions, which is why
		#you can't have a for loop just iterate and declare all of them
		types = ["keeper", "taker"]
		self.agentSigmaError = inputAgentSigmaNoise
		self.keeperArray = []
		self.keeperTruePosArray = []
		self.keeperTruePosArray.append((12.5, 12.5))
		self.keeperTruePosArray.append((25,  self.__display_width - 37.5))
		self.keeperTruePosArray.append((self.__display_height - 37.5,  self.__display_width - 37.5))
		self.keeperArray.append(agent.agent(self, 0, kUtil.getNoisyVals( self.keeperTruePosArray[0], self.agentSigmaError), self.agentSigmaError, types[0], kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError), self.maxPlayerSpeed, self.maxBallSpeed))
		self.keeperArray.append(agent.agent(self, 1, kUtil.getNoisyVals( self.keeperTruePosArray[1], self.agentSigmaError), self.agentSigmaError, types[0], kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError), self.maxPlayerSpeed, self.maxBallSpeed))
		self.keeperArray.append(agent.agent(self, 2, kUtil.getNoisyVals( self.keeperTruePosArray[2], self.agentSigmaError), self.agentSigmaError, types[0], kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError), self.maxPlayerSpeed, self.maxBallSpeed))
		
		self.takerArray = []
		self.takerTruePosArray = []
		self.takerTruePosArray.append((self.__display_height - 25,  25))
		self.takerTruePosArray.append((self.__display_height - 37.5,  50))
		self.takerArray.append(agent.agent(self, 0, self.takerTruePosArray[0], self.agentSigmaError, types[1], kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError), self.maxPlayerSpeed, self.maxBallSpeed))
		self.takerArray.append(agent.agent(self, 1, self.takerTruePosArray[1], self.agentSigmaError, types[1], kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError), self.maxPlayerSpeed, self.maxBallSpeed))
		
		#3 different font sizes 
		self.smallfont = pygame.font.SysFont("comicsansms",25) #25 is font sizes
		self.medfont = pygame.font.SysFont("comicsansms",50) 
		self.largefont = pygame.font.SysFont("comicsansms",80) 
		self.verysmallfont = pygame.font.SysFont("comicsansms", 12)
		
		#birdsEyeView generator for agents like hyperNEAT:
		if bevCustomTileSize == None:
			bevCustomTileSize = self.__agent_block_size
		self.bev = birdsEyeView.birdsEyeView(self.__display_width, self.__display_height, bevCustomTileSize, self.__ball_block_size )
		self.bev_grid_as_grid = self.bev.getBirdsEyeView(self.keeperArray, self.takerArray);
		self.bev_grid_as_list = self.bev.getBirdsEyeViewAsList(self.keeperArray, self.takerArray);
		self.bev_substrate = self.bev.getSubstrate(self.keeperArray, self.takerArray);
		self.bev_keeper_sub_index = self.bev.getBallHolderTile(self.keeperArray)
Example #16
0
 def gameLoop(self, mode):
     self.drawWorld ()
     gameExit = False
     if(self.displayGraphics == True):
         pygame.display.update()
     experimentAgent = self.takerArray[0]
     #each occurance of this loop is treated as one simulation cycle
     while not gameExit:
         if(mode == "manual"):
             for event in pygame.event.get():
                 if event.type == pygame.QUIT:
                     gameExit = True
                 if event.type == pygame.KEYDOWN:
                     if event.key == pygame.K_LEFT:
                         self.moveAttempt(experimentAgent, ((0,-1), self.maxPlayerSpeed))
                     elif event.key == pygame.K_RIGHT:
                         self.moveAttempt(experimentAgent, ((0,1), self.maxPlayerSpeed))
                     elif event.key == pygame.K_UP:
                         self.moveAttempt(experimentAgent, ((-1,0), self.maxPlayerSpeed))
                     elif event.key == pygame.K_DOWN:
                         self.moveAttempt(experimentAgent, ((1,0), self.maxPlayerSpeed))
                     elif event.key == pygame.K_1:
                         self.moveAttempt(experimentAgent, ((-1,-1), self.maxPlayerSpeed))
                     elif event.key == pygame.K_2:
                         self.moveAttempt(experimentAgent, ((-1,1), self.maxPlayerSpeed))
                     elif event.key == pygame.K_3:
                         self.moveAttempt(experimentAgent, ((1,-1), self.maxPlayerSpeed))
                     elif event.key == pygame.K_4:
                         self.moveAttempt(experimentAgent, ((1,1), self.maxPlayerSpeed))
         elif (mode == "hand_coded"):
             for event in pygame.event.get():
                 if event.type == pygame.QUIT:
                     gameExit = True
             self.sendStateVars()
             self.calc_receive()
             for keeper in self.keeperArray:
                 keeper.decisionFlowChart()
             for taker in self.takerArray:
                 taker.decisionFlowChart()
         elif(mode == "q_learning"):
             totalTraining = 5
             flag = False
             for index in range(len(self.keeperArray)):
                 if self.keeperArray[index].load_obj("dict",index, mode) == None:
                     flag = True
             if flag:
                 print("no files exist")
                 self.QTraining(totalTraining)
             
                 for index in range(len(self.keeperArray)):
                     self.keeperArray[index].save_obj(self.keeperArray[index].q_values,"dict",index, mode)
                     #for key in list(self.keeperArray[index].q_values.keys())[:10]:
                         #print("QValues of ",index," agent is: key=",key," value=",self.keeperArray[index].q_values[key])
                 
                 self.Qtesting()
             
             else:
                 print("files exist, continue training")
                 totalTraining = 0
                 for index in range(len(self.keeperArray)):
                     self.keeperArray[index].q_values = self.keeperArray[index].load_obj("dict",index, mode)
                     #for key in list(self.keeperArray[index].q_values.keys())[:10]:
                         #print("QValues of ",index," agent is: key=",key," value=",self.keeperArray[index].q_values[key])
                 
                 
                 
                 self.QTraining(totalTraining)
                 
                 for index in range(len(self.keeperArray)):
                     self.keeperArray[index].save_obj(self.keeperArray[index].q_values,"dict",index, mode)
                     
                 self.Qtesting()
                 
                                     
         elif(mode == "sarsa"):
             totalTraining = 5
             flag = False
             for index in range(len(self.keeperArray)):
                 if self.keeperArray[index].load_obj("dict",index, mode) == None:
                     flag = True
             if flag:
                 print("no files exist")
                 self.QTraining(totalTraining)
             
                 for index in range(len(self.keeperArray)):
                     self.keeperArray[index].save_obj(self.keeperArray[index].q_values,"dict",index, mode)
                     #for key in list(self.keeperArray[index].q_values.keys())[:10]:
                         #print("QValues of ",index," agent is: key=",key," value=",self.keeperArray[index].q_values[key])
                 
                 self.Qtesting()
             
             else:
                 print("files exist, continue training")
                 totalTraining = 0
                 for index in range(len(self.keeperArray)):
                     self.keeperArray[index].q_values = self.keeperArray[index].load_obj("dict",index, mode)
                     #for key in list(self.keeperArray[index].q_values.keys())[:10]:
                         #print("QValues of ",index," agent is: key=",key," value=",self.keeperArray[index].q_values[key])
                 
                 
                 
                 self.QTraining(totalTraining)
                 
                 for index in range(len(self.keeperArray)):
                     self.keeperArray[index].save_obj(self.keeperArray[index].q_values,"dict",index, mode)
                     
                 self.Qtesting()
                 
                                     
             
         #this is common code that will occur regardless of what agent you picked
         #if (self.fieldBall.inPosession == False):
         newBallPoint = kUtil.addVectorToPoint(self.fieldBall.trueBallPos, kUtil.scalarMultiply(self.maxBallSpeed, kUtil.unitVector(self.fieldBall.trueBallDirection)))
         self.fieldBall.updateCoordinate(newBallPoint)
         for i in range(len(self.takerArray)):
             self.takerArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.takerArray[i].sigma)
         for i in range(len(self.keeperArray)):
             self.keeperArray[i].noisyBallPos = kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.keeperArray[i].sigma)                
         self.updateBallPosession()
         self.updateScore()
         self.drawWorld ()
         self.displayScore()
         pygame.display.update()
         
         if self.isGameOver() == True:
             gameExit = True
             print("final score: ", self.keeperScore)
         #this specifies frames per second
         self.clock.tick(self.fps)
     self.finish()
     #self.pause("Game Over: Final Score %d" % self.keeperScore)
     self.exitSim()
Example #17
0
 def updateAgentPosition(self, truePosition):
     self.true_pos = truePosition
     self.noisy_pos = kUtil.getNoisyVals(truePosition, self.sigma)
Example #18
0
    def __init__(self, inputAgentSigmaNoise=0.1):
        pygame.init()
        # RGB color
        self.__white = (255, 255, 255)
        self.__black = (0, 0, 0)
        self.__red = (255, 0, 0)
        self.__green = (0, 155, 0)
        self.__blue = (0, 0, 255)

        # give the game a title
        pygame.display.set_caption("Keepaway")
        self.keeperScore = 0

        # these are more or less global variables..
        # I'm not sure if this is bad or not.
        self.__worldImage = pygame.image.load("images/soccer_field.png")
        self.__ballImage = pygame.image.load("images/ball.png")
        self.__keeperImage = pygame.image.load("images/keeper.png")
        self.__takerImage = pygame.image.load("images/taker.png")
        self.__predictedImage = pygame.image.load("images/x.png")
        self.__debugYellowDotImage = pygame.image.load("images/yellow_dot.png")
        self.__debugRedDotImage = pygame.image.load("images/red_dot.png")
        # block sizes are used for collision detection
        # only 1 size per element because all blocks are squares. block size = side length
        self.__agent_block_size = 23
        self.ball_block_size = 12

        self.maxBallSpeed = 4
        self.maxPlayerSpeed = 2

        # dimensions of the game are the same as the soccer field image
        self.__display_width = 550
        self.display_height = 357
        self.__field_center = (self.__display_width / 2, self.display_height / 2)
        # gameDisplay is a pygame.surface object. it's your screen
        self.gameDisplay = pygame.display.set_mode((self.__display_width, self.display_height))
        self.test_fps = 60
        self.train_fps = 10000
        self.clock = pygame.time.Clock()

        # start the ball kinda close to the keeper in the upper left corner
        self.fieldBall = ball.ball((self.__field_center[0] / 4, self.__field_center[1] / 4), self.maxBallSpeed)

        # setup all the initial keepers and takers. They are all starting at different field positions, which is why
        # you can't have a for loop just iterate and declare all of them
        types = ["keeper", "taker"]
        self.agentSigmaError = inputAgentSigmaNoise
        self.keeperArray = []
        self.keeperTruePosArray = []
        self.keeperTruePosArray.append((12.5, 12.5))
        self.keeperTruePosArray.append((25, self.__display_width - 37.5))
        self.keeperTruePosArray.append((self.display_height - 37.5, self.__display_width - 37.5))
        self.keeperArray.append(
            agent.agent(
                self,
                0,
                kUtil.getNoisyVals(self.keeperTruePosArray[0], self.agentSigmaError),
                self.agentSigmaError,
                types[0],
                kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError),
                self.maxPlayerSpeed,
                self.maxBallSpeed,
            )
        )
        self.keeperArray.append(
            agent.agent(
                self,
                1,
                kUtil.getNoisyVals(self.keeperTruePosArray[1], self.agentSigmaError),
                self.agentSigmaError,
                types[0],
                kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError),
                self.maxPlayerSpeed,
                self.maxBallSpeed,
            )
        )
        self.keeperArray.append(
            agent.agent(
                self,
                2,
                kUtil.getNoisyVals(self.keeperTruePosArray[2], self.agentSigmaError),
                self.agentSigmaError,
                types[0],
                kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError),
                self.maxPlayerSpeed,
                self.maxBallSpeed,
            )
        )

        self.takerArray = []
        self.takerTruePosArray = []
        self.takerTruePosArray.append((self.display_height - 25, 25))
        self.takerTruePosArray.append((self.display_height - 37.5, 50))
        self.takerArray.append(
            agent.agent(
                self,
                0,
                self.takerTruePosArray[0],
                self.agentSigmaError,
                types[1],
                kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError),
                self.maxPlayerSpeed,
                self.maxBallSpeed,
            )
        )
        self.takerArray.append(
            agent.agent(
                self,
                1,
                self.takerTruePosArray[1],
                self.agentSigmaError,
                types[1],
                kUtil.getNoisyVals(self.fieldBall.trueBallPos, self.agentSigmaError),
                self.maxPlayerSpeed,
                self.maxBallSpeed,
            )
        )

        # 3 different font sizes
        self.smallfont = pygame.font.SysFont("comicsansms", 25)  # 25 is font sizes
        self.medfont = pygame.font.SysFont("comicsansms", 50)
        self.largefont = pygame.font.SysFont("comicsansms", 80)
        self.verysmallfont = pygame.font.SysFont("comicsansms", 12)
Example #19
0
 def _passBall(self, integerK):
     """
     If a keeper currently has the ball, then it has the option to hold the ball,
     or pass it. Call this function to pass the ball. integerK represents the 
     keeper that the ball holder is passing to. integerK = 1 means pass to the 
     keeper that is closest to the ball besides the ball holder. integerK = 2
     means pass to the 2nd closest, and so on. Only subclasses of agent should
     be calling this function.
     
     :param integerK: this represents the 
         keeper that the ball holder is passing to. integerK = 1 means pass to the 
         keeper that is closest to the ball besides the ball holder. integerK = 2
         means pass to the 2nd closest, and so on.
     
     :type integerK: integer
     
     :returns: no return 
     """
     #print("passing to keeper " +  str(integerK) + ", indexed by distance. ")
     #if you're passing to integerK = 1, then that means pass to the keeper that's closer to you
     #if you're passing ot integerK = 2, then that means pass to the keeper that's farther to you
     #0 is an invalid input
     sortedKeepers = sorted(self.keeperArray)
     if integerK == 0:
         print("Invalid input for integerK! integerK not allowed to be", integerK)
         return
     if self.fieldBall == None:
         print("ERROR: trying to hold ball without actually having  ball")
         return
     
     #pass to team mate integerK. if integerK = 1, then it's the 2nd element in array
     selfToTeammateDirection = kUtil.unitVector(kUtil.getVector(self.noisyBallPos, sortedKeepers[integerK].get_noisy_pos()))
     selfToTeammateVector = (kUtil.scalarMultiply(self.fieldBall.maxBallSpeed, selfToTeammateDirection))
     
     """
     #this code segment is for determining which angle you'd rather pass to. it's all deterministic
     ballPos = self.noisyBallPos
     passVectorsToConsider = []
     passVectorsToConsider.append(selfToTeammateDirection)
     for i in range(len(self.cosinesOfInterest)):
         passVectorsToConsider = passVectorsToConsider + self.__getRotatedVectors(selfToTeammateDirection, self.cosinesOfInterest[i])
     #now sort the vectors based on how open they are:
     self.worldRef.debugPassVectors(ballPos, passVectorsToConsider)
     passVectorsToConsider = sorted(passVectorsToConsider, 
                                    key = lambda vector: max ( kUtil.cosTheta(self.worldRef.takerArray[0].getNoisyMidPoint(), ballPos, kUtil.addVectorToPoint(ballPos, vector)), 
                                                               kUtil.cosTheta(self.worldRef.takerArray[1].getNoisyMidPoint(), ballPos, kUtil.addVectorToPoint(ballPos, vector))),
                                     )
     #iterate over the sorted list until you find a pass that you can do
     for i in range(len(passVectorsToConsider)):
         if( calcReceive.calc_receive(self.worldRef, passVectorsToConsider[i]) != None): 
             selfToTeammateDirection = passVectorsToConsider[i]
             selfToTeammateVector = (kUtil.scalarMultiply(self.fieldBall.maxBallSpeed, selfToTeammateDirection))
             print "PASS USING ANGLE ACHEIVED AT i=", i
             break
     """    
             
     #at this point, you've determined the angle you wanna pass, and you pass.
     #set ball's possesion to false, and update to new point. ball class handles direction vector
     self.fieldBall.updatePosession(False)
     self.inPosession = False
     self.isKicking = True
     #kUtil.addVectorToPoint(self.fieldBall.trueBallPos, selfToTeammateVector)
     self.fieldBall.updateDirection(kUtil.getNoisyVals(selfToTeammateVector, self.__sigma))
Example #20
0
 def receiveDecision(self, rDecision):
     self.onReceiveDecision = rDecision
     self.onReceiveDecision[1] = kUtil.getNoisyVals(self.onReceiveDecision[1], self.sigma)