Exemple #1
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()))
Exemple #2
0
 def update(self, newDirectionVector):
     if not self.inPosession:
         #if no one's posessing it, then it's moving somewhere
         self.trueBallDirection = kUtil.unitVector(newDirectionVector)
     else:
         #ball ain't goin nowhere if someone's posessing it
         self.trueBallDirection = (0.0,0.0)
Exemple #3
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]
Exemple #4
0
 def moveAttempt(self, inputAgent, reversedPolarCoord ):
     
     noiseFreeDirectionVector = reversedPolarCoord[0]
     distance = reversedPolarCoord[1]
     
     #for the purposes of finding legal moves, just assume that they player is trying
     #to cover the most amount of distance. Also, right now, it's not important if
     #noiseFreeDirectionVector is a unit vector or not
     quad = self.getQuadAttemptingToMoveTo(inputAgent, noiseFreeDirectionVector)
     setOfLegalQuads = self.getLegalQuadrants(inputAgent)
     #print(setOfLegalQuads)
     #print("quad:", quad)
     if quad in setOfLegalQuads:
         #move is legal, do it
         self.moveAgent(inputAgent, kUtil.scalarMultiply ( distance, kUtil.unitVector(noiseFreeDirectionVector)))
     else:
         #move is illegal. Simply return without calling or updating anything
         #print("Illegal move: ", reversedPolarCoord)
         return
Exemple #5
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
Exemple #6
0
	def moveAttempt(self, inputAgent, reversedPolarCoord ):
		"""
		This function is used by the agent in order to attempt movement on the 
		field. If the move is legal, the agent's position is updated. If the 
		move is illegal, then nothing happens. A move is illegal if the agent 
		tries to go outside the boundaries of the field. 
		
		:param inputAgent: a reference to the agent that is attempting to move
		:param reversedPolarCoord: the movement that the agent is trying to do. 
		    the movement is represented as a tuple where the first element is a
		    unit vector representing the direction the agent is trying to move, 
		    and the 2nd element is the distance the agent is trying to move.
		    
		:type inputAgent: agent
		:type reversedPolarCoord: tuple where first element is a tuple of floats, 
		    and the 2nd element is a float or integer
		
		:returns: true if move was successful, false if not
		:rtype: boolean
		"""
		noiseFreeDirectionVector = reversedPolarCoord[0]
		distance = reversedPolarCoord[1]
		
		#for the purposes of finding legal moves, just assume that they player is trying
		#to cover the most amount of distance. Also, right now, it's not important if
		#noiseFreeDirectionVector is a unit vector or not
		quad = self.__getQuadAttemptingToMoveTo(inputAgent, noiseFreeDirectionVector)
		setOfLegalQuads = self.__getLegalQuadrants(inputAgent)
		#print setOfLegalQuads 
		#print "quad:", quad
		if quad in setOfLegalQuads:
			#move is legal, do it
			self.__moveAgent(inputAgent, kUtil.scalarMultiply ( distance, kUtil.unitVector(noiseFreeDirectionVector)))
		else:
			#move is illegal. Simply return without calling or updating anything
			#print "Illegal move: ", reversedPolarCoord 
			return False
		if kUtil.magnitude(noiseFreeDirectionVector) == 0.0:
			return False
		else:
			return True
Exemple #7
0
 def passBall(self, integerK):
     #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].noisy_pos))
     selfToTeammateVector = (kUtil.scalarMultiply(self.fieldBall.maxBallSpeed, selfToTeammateDirection))
     #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.update(selfToTeammateVector)
     #self.fieldBall.updatepos( kUtil.addVectorToPoint(self.fieldBall.true_pos, selfToTeammateVector ) )
     return (selfToTeammateDirection, self.maxBallSpeed)
Exemple #8
0
 def updateDirection(self, newDirectionVector):
     """
     update the direction component of ball velocity
     
     This function is called upon by the agent class to attempt an
     update to update the direction that the ball is going. The direction
     is stored as a unit vector. 
     
     :param newDirectionVector: a tuple representing the direction 
         that the agent is trying to change the ball to. this vector 
         does not need to be in the form of a unit vector. The direction
         will only be set if the ball is NOT in posession. If the ball is
         in possession, the the direction will remain (0.0,0.0). 
     
     :type newDirectionVector: float, float
     
     :returns: no return
     """
     if not self.inPosession:
         #if no one's posessing it, then it's moving somewhere
         self.trueBallDirection = kUtil.unitVector(newDirectionVector)
     else:
         #ball ain't goin nowhere if someone's posessing it
         self.trueBallDirection = (0.0,0.0)
Exemple #9
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))
Exemple #10
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
Exemple #11
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()
Exemple #12
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()
Exemple #13
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()
Exemple #14
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()
Exemple #15
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)