Exemplo n.º 1
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))    
Exemplo n.º 2
0
 def agentBallIntersection(self, inputAgent):
     #print()
     agentRadius = self.agent_block_size / 2
     ballRadius = self.ball_block_size / 2
     cutoff = agentRadius+ ballRadius
     agentMidPoint = kUtil.addVectorToPoint(inputAgent.true_pos, (self.agent_block_size/2, self.agent_block_size/2))
     ballMidPoint = kUtil.addVectorToPoint(self.fieldBall.trueBallPos, (self.ball_block_size/2, self.ball_block_size/2))
     #print("agent actual:", inputAgent.true_pos, "agentMid:", agentMidPoint)
     #print("agentMid:", agentMidPoint, " ballMid:", ballMidPoint)
     distBetweenMidPoints = kUtil.getDist(agentMidPoint, ballMidPoint)
     #print("Cutoff: ", cutoff, " actual Distance: ", distBetweenMidPoints)
     if (distBetweenMidPoints <= cutoff):
         return True
     else:
         return False
Exemplo n.º 3
0
 def calc_receive_ball_moving(self):
     #make sure that you're only doing this if
     for i in range(len(self.keeperArray)):
         if self.keeperArray[i].inPosession == True:
             rDecision = (i, self.keeperArray[i].true_pos)
             return
     for i in range(len(self.takerArray)):
         if self.takerArray[i].inPosession == True:
             return
         
     TA = kUtil.addVectorToPoint(self.fieldBall.trueBallPos, self.fieldBall.trueBallDirection)
     TB = self.fieldBall.trueBallPos
     minTime = 99999.0
     argmin = None
     bestPerpIntersect = None
     for i in range(len(self.keeperArray)):
         TC = self.keeperArray[i].true_pos
         if (kUtil.cosTheta(TA, TB, TC)) < 0:
             #print("Keeper " , i, " can't get to ball: the cosTheta is negetive.")
             #it's impossible for this keeper to get the ball
             continue
         else:
             pd = kUtil.getPerpDist(TA, TB, TC)
             pt = pd/self.maxPlayerSpeed
             normalVector = kUtil.getNormalVector(TA, TB, TC)
             perpIntersect = kUtil.addVectorToPoint(TC, normalVector)
             bd = kUtil.getDist(TB, perpIntersect)
             bt = bd/self.maxBallSpeed
             if pt > bt:
                 #keeper wont' be able be able to get to ball in time
                 #print("player ", i+1, "can't reach ball as pt:",pt," and bt: ",bt)
                 continue
             else:
                 #keeper CAN get to ball. can it get there soonest though?
                 #save the fastest keeper
                 if (pt < minTime):
                     minTime = pt
                     argmin = i
                     bestPerpIntersect = perpIntersect
     #at this point, if a keeper can get to the ball, the fastest and it's intercept are saved
     if (argmin != None):
         rDecision = [argmin, self.calcOptimal(self.keeperArray, argmin, bestPerpIntersect)]
         for i in range(len(self.keeperArray)):
             self.keeperArray[i].receiveDecision(rDecision)
         for i in range(len(self.takerArray)):
             self.takerArray[i].receiveDecision(rDecision)
     else:
         print("no argmin found. game about to crash for sure")
Exemplo n.º 4
0
	def debugPassVectors(self, startPoint, vectors):
		"""
		This function is meant to display yellow dots to help the programmer figure
		out if the getRotatedVectors function works correctly. The getRotatedVectors
		function ended up being discarded, but code for it is still kept incase
		the developers decide they want to use it later on
		
		:param vectors: 2 vectors that were calculated by getRotatedVectors
		:param startPoint: the vertex/starting point of the vector that you're trying
		    to rotate. You rotate about the vertex. 
		    
		:type vectors: a list of 2 vectors, each vector being a tuple of floats
		:type startPoint: a tuple or list of floats
		
		:returns: no return
		"""
		self.worldRef.__drawWorld ()
		self.worldRef.__displayScore()
		print("Starting point: ", startPoint) 
		for vector in vectors:
			newVector = kUtil.addVectorToPoint(startPoint, kUtil.scalarMultiply(5, vector))
			print("printing vector: ", newVector)
			self.worldRef.gameDisplay.blit(self.worldRef.__debugYellowDotImage, (newVector[1], newVector[0]))
		self.worldRef.gameDisplay.blit(self.worldRef.__debugRedDotImage, (startPoint[1], startPoint[0]))
		pygame.display.update()
		print("debugging")
Exemplo n.º 5
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()
Exemplo n.º 6
0
 def __getRotatedVectors(self, vector, cos_k):
     """
     FUNCTION NOT USED IN FINAL IMPLEMENTATION
     
     This function will calculate 2 other angles that the agent can kick the ball to. If 
     the agent has calculated a direct path from itself to another keeper, that should be
     the input vector. If the agent is interested in passing at a 5 degree angle, then
     the 2nd input should be the cosine of 5 degrees. This function will then calculate 
     and return the 2 unit vectors which are the vectors pointing at 5 degree angles 
     
     :param vector: the vector that points directly from the  ball's noisy 
         position to the noisy position of the keeper the agent wants to pass to
     :param cos_k: the cosine of the angle that the agent wants to pass at. The
         reason cosine is used instead of the angle is for computational efficiency
     
     :type vector: list or tuple of numbers
     :type cos_k: number
     
     :returns: a list of 2 unit vectors, each one representing the 2 directions that
         the ball can be kicked to achieve the desired angle.
         
     :rtype: list of tuples, each tuple being a tuple of floats.
     """
     discriminantIsZero = False
     terminalPosCosZero = kUtil.addVectorToPoint(self.noisyBallPos, vector) 
     vector = kUtil.unitVector(vector)
     ax = vector[1] #columns are x
     ay = vector[0] #rows are y
     k = cos_k
     term1 = 2*k*ax #term 1 = -b 
     discriminant = term1 * term1 - 4 * (ax*ax +ay*ay)*(-1.0*ay*ay + k * k)
     discriminant = math.sqrt(discriminant)
     if (abs(discriminant) < 0.00001):
         discriminantIsZero = True
         
     #denominator = 2 * (ax*ax + ay*ay) #should be 2 every time if A is a unit vector
     denominator = 2 #should be 2 every time if A is a unit vector
     bx1 = (term1 - discriminant) / denominator
     #print "term1: ", term1, " discriminant:", discriminant, " denominator: ", denominator
     by1 = math.sqrt(1.0 - (bx1 * bx1))
     returnUnitVectors = []
     #make it (row,col) format, so therefore it's (y,x)
     returnUnitVectors.append((by1, bx1))
     returnUnitVectors.append((-1.0 * by1, bx1))
     
     if discriminantIsZero == False:
         bx2 = (term1 + discriminant) / denominator
         by2 = math.sqrt(1.0 - (bx2 * bx2))
         returnUnitVectors.append((by2, bx2))
         returnUnitVectors.append((-1.0 * by2, bx2)) 
         #print "return vectors"
         #print returnVectors
         returnUnitVectors = sorted(returnUnitVectors, key=lambda x: kUtil.getSqrDist(terminalPosCosZero, x))
         
     return returnUnitVectors[:2]
Exemplo n.º 7
0
    def __agentBallIntersection(self, inputAgent, agentType):
        """
        This private function will take an input agent, and 
        check to see if that agent intersects with the ball or not.
        If so, return true. otherwise return false.
        
        :param inputAgent: the agent that you're checking to see
            if it intersects with the ball or not. 
        :param agentType: "keeper" or "taker"
        
        :type inputAgent: agent
        :type agentType: string
        
        :returns: true if the agent intersects with the ball, false otherwise
        :rtype: boolean 
        """
        # print
        if agentType == "keeper":
            agentTruePosition = self.keeperTruePosArray[inputAgent.getSimIndex()]
        else:
            # agent must be a taker
            agentTruePosition = self.takerTruePosArray[inputAgent.getSimIndex()]

        agentRadius = self.__agent_block_size / 2
        ballRadius = self.ball_block_size / 2
        cutoff = agentRadius + ballRadius
        agentMidPoint = kUtil.addVectorToPoint(
            agentTruePosition, (self.__agent_block_size / 2, self.__agent_block_size / 2)
        )
        ballMidPoint = kUtil.addVectorToPoint(
            self.fieldBall.trueBallPos, (self.ball_block_size / 2, self.ball_block_size / 2)
        )
        # print "agent actual:", inputAgent.true_pos, "agentMid:", agentMidPoint
        # print "agentMid:", agentMidPoint, " ballMid:", ballMidPoint
        distBetweenMidPoints = kUtil.getDist(agentMidPoint, ballMidPoint)
        # print "Cutoff: ", cutoff, " actual Distance: ", distBetweenMidPoints
        if distBetweenMidPoints <= cutoff:
            return True
        else:
            return False
Exemplo n.º 8
0
 def calcOptimal(self, agentList, i, intersect):
     V = kUtil.getVector(self.fieldBall.trueBallPos, intersect)
     UV = kUtil.unitVector(V)
     stepVector = kUtil.scalarMultiply(self.maxBallSpeed, UV)
     #keep adding the step vector to the optimal point
     optimalPoint = self.fieldBall.trueBallPos
     maxNumSteps = int(kUtil.getDist(self.fieldBall.trueBallPos, intersect)/ self.maxBallSpeed)
     stepCount = 0
     for k in range(maxNumSteps):
         optimalPoint = kUtil.addVectorToPoint(optimalPoint, stepVector)
         stepCount += 1
         currPd = kUtil.getDist(optimalPoint,agentList[i].true_pos)
         currBd = kUtil.getDist(self.fieldBall.trueBallPos, optimalPoint)
         currPt = currPd / self.maxPlayerSpeed
         currBt = currBd / self.maxBallSpeed
         if currPt < currBt:
             #found the optimal, so return it
             return optimalPoint
     #if you get here, then no closer optimal was found, so just return the intersect
     return intersect
Exemplo n.º 9
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)
Exemplo n.º 10
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()
Exemplo n.º 11
0
def __calc_receive_ball_moving(worldRef, inputDirection, possessingKeeperIndex):
    """
    This function is a private function meant to assist calc_receive. This function 
    will go and calculate the receive decision for the special case where the 
    ball is moving. The receive decision is a tuple that simply contains the 
    index of the keeper that should run towards the ball, and the coordinate 
    that the keeper should run to. 
    If the ball is moving, then calc_receieve will find an intersection point
    along the balls projected path that the selected keeper can run to. 
    The intercept point is selected such that the 
    selected keeper will run a short distance, be far away from the takers, 
    and also be far away from out of bounds.  
    
    .. note::
        This is a private function that the user shouldn't worry about calling.
        Only the calc_receieve function of this method will use this function.
        And only the simulator class should call the calc_receive function. 
    
    :param worldRef: a reference to the simulator class which is calling this function
    :param inputDirection: the current direction the ball is moving.
    :param possessingKeeperIndex: the index of the keeper who currently has possession
        
    :type worldRef: keepAway
    :type inputDirection: tuple of floats
    :type possessingKeeperIndex: integer
    
    :returns: tuple, where first element is the index of the keeper picked to run towards
        the ball. The simulator will use this index to look up the index of the keeper 
        in its self.keeperArray. The 2nd element is the intersection coordinate
    :rtype: tuple where first element is integer, second element is tuple. 2nd element
        tuple contains integers

    """
    #TA is a point that the ball is heading to in the next time step      
    TA = kUtil.addVectorToPoint(worldRef.fieldBall.trueBallPos, inputDirection)
    #TB is the current ball position, and for angle calculations, it will be the vertex
    TB = worldRef.fieldBall.trueBallPos
    minTime = float("inf")
    argmin = None
    bestPerpIntersect = None
    #the purpose of this for loop is to find which keeper should go to the ball. 
    for i in range(len(worldRef.keeperArray)):
        #TC is the position of the keeper who's figuring out if he should goToBall(), or getOpen()
        TC = worldRef.keeperTruePosArray[i]
        if (kUtil.cosTheta(TA, TB, TC)) < 0:
            #print "Keeper " , i, " can't get to ball: the cosTheta is negetive."
            #it's impossible for this keeper to get the ball
            continue 
        else:
            pd = kUtil.getPerpDist(TA, TB, TC)
            pt = pd/worldRef.maxPlayerSpeed
            normalVector = kUtil.getNormalVector(TA, TB, TC)
            perpIntersect = kUtil.addVectorToPoint(TC, normalVector)
            bd = kUtil.getDist(TB, perpIntersect)
            bt = bd/worldRef.maxBallSpeed
            if pt > bt:
                #keeper wont' be able be able to get to ball in time
                #print "player ", i+1, "can't reach ball as pt:",pt," and bt: ",bt 
                continue
            else:
                #keeper CAN get to ball. can it get there soonest though?
                #save the fastest keeper
                if (pt < minTime and i !=  possessingKeeperIndex):
                    minTime = pt
                    argmin = i
                    bestPerpIntersect = perpIntersect
    #at this point, if a keeper can get to the ball, 
    #the fastest and it's intercept are saved
    if (argmin != None):
        rDecision = [argmin, __calcOptimal(worldRef, worldRef.keeperArray, argmin, bestPerpIntersect)]
        return rDecision
    else:
		rDecision = [1 , worldRef.get_field_center()]
		#print("no argmin found. game about to end for sure.")
		return rDecision
Exemplo n.º 12
0
def __calcOptimal(worldRef, agentList, i, intersect):
    """
    This function is a private function meant to assist another private function called
    __calc_receive_ball_moving. 
    
    once __calc_receive_ball_moving has calculated the intersection point, there's 
    one last step: to make sure that the intersection point isn't too close to 
    the out of bounds area. If the intersection point too close to out of bounds, 
    then return an intersection point along the path that the ball is traveling, but
    is just still safely away from the out of bounds areas. 
    
    
    .. note::
        This is a private function that the user shouldn't worry about calling.
        Only the calc_receive function should be called.  
    
    :param worldRef: a reference to the simulator class which is calling this function
    :param agentList: a list provided by the simulator of all agents
    :param i: the index of the agent running to the ball for the list agentList
    :param intersect: the intersection point that has been calculated, and might be too
        close to out of bounds

        
    :type worldRef: keepAway class
    :type agentList: a list where each element is an agent class
    :type i: integer
    :type intersect: a tuple of floats
    
    :returns: the intersection coordinate which is safely within bounds, or the original
        intersection point if no such point is found
    :rtype: tuple of floats

    """
    #if the intersect is in bounds, just go to it. no calculations needed
    if __isPointOutOfPlayRegion(worldRef, intersect, agentList, i) == False:
        #print("point in bounds, return intersect") 
        return intersect
        
    #V = vector from agent's perpendicular intercept to the ball
    V = kUtil.getVector(intersect, worldRef.fieldBall.trueBallPos)
    #turn V into a unit vector and multipy it by the speed of the ball to get velocity vector
    UV = kUtil.unitVector(V)
    stepVector = kUtil.scalarMultiply(worldRef.maxBallSpeed, UV)
        
    #the optimal point is intialized to the intersect, and 
    #the intersect is currently out of bounds.
    #keep adding the step vector to the optimal point until
    #the intersect is no longer out of bounds
    optimalPoint = intersect
    maxNumSteps = int(kUtil.getDist(worldRef.fieldBall.trueBallPos, intersect)/ worldRef.maxBallSpeed)
    stepCount = 0
    #if you can't get to the ball in maxNumSteps, then it's hopeless. simply
    #return the intersection point. Your agent will fail and the ball will
    #go out of bounds, but there's nothing that can be done
    for k in range(maxNumSteps):
        optimalPoint = kUtil.addVectorToPoint(optimalPoint, stepVector)
        stepCount += 1
        if __isPointOutOfPlayRegion(worldRef, optimalPoint, agentList, i) == False:
            #print("Optimal found, returning optimal point:", optimalPoint) 
            return optimalPoint
    #if you get here, then no closer optimal was found
    #print("no optimal found, returning intersect", intersect) 
    return intersect
Exemplo n.º 13
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()
Exemplo n.º 14
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()
Exemplo n.º 15
0
 def moveAgent(self, inputAgent, movementVector):
     newNoiseFreePos = kUtil.addVectorToPoint(inputAgent.true_pos, movementVector)
     inputAgent.updateAgentPosition(newNoiseFreePos)    
     return inputAgent.true_pos