def test_rotated_vectors(self): import keepAway keepAwayWorld = keepAway.keepAway(0) #intialize agent to position 25,25 with no noise/error, as a keeper, with (357/2, 550/2) as center of field, #and 2 as agent speed, and 3 as ball speed Agent = agent(keepAwayWorld,(25, 25), 0.0, "keeper", (357/2, 550/2), 2, 3) cos5 = math.cos(5.0 * math.pi / 180) #cosine of 3 degress. angle needs to be in radians #Agent.receiveListOfOtherPlayers(self.keeperArray, self.takerArray, i) testBall = ball.ball((25,25), 3, True) Agent.receiveBallReference(testBall) v = kUtil.getVector((25,25), (30, 30)) #a vector whose angle is 45 degrees answer = Agent.__getRotatedVectors(v, cos5) correctAnswer1 = [((math.cos(50 * math.pi / 180)), (math.sin(50 * math.pi/180))), ((math.cos(40 * math.pi / 180)), (math.sin(40 * math.pi/180)))] """ print "answer" print answer print "correct" print correctAnswer1 """ for i in range(len(correctAnswer1)): for j in range(len(correctAnswer1[i])): str1 = "failed at: i = %d "%i str2 = "j = %d" %j strend = str1 + str2 self.assertAlmostEqual(answer[i][j], correctAnswer1[i][j], msg=strend)
def test_getStateVars(self): # self, worldRef, pos, sigma, agentType, trueBallPos, maxPlayerSpeed, maxBallSpeed, inPossession = False import keepAway keepAwayWorld = keepAway.keepAway() ballPos = (0, 0) center = (0, 0) simulatedError = 0.01 a1 = agent.agent( keepAwayWorld, (10, 0), simulatedError, "Keeper", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) a2 = agent.agent( keepAwayWorld, (0, 0), simulatedError, "Keeper", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) a3 = agent.agent( keepAwayWorld, (0, 5), simulatedError, "Keeper", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) keepers = [a1, a2, a3] t1 = agent.agent( keepAwayWorld, (5, 5), simulatedError, "Taker", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) t2 = agent.agent( keepAwayWorld, (5, 0), simulatedError, "Taker", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) takers = [t1, t2] testOut = getStateVarsKeepers(keepers, takers, center) actualOut = [0, 5, 10, 5, math.sqrt(50), 5, 10, 5, math.sqrt(50), 5, 5, math.cos(math.pi / 4.0), 1] for i in range(len(testOut)): self.assertAlmostEqual(testOut[i], actualOut[i], 1)
def testCosinesOfInterest(self): import keepAway keepAwayWorld = keepAway.keepAway() Agent = agent(keepAwayWorld,(25, 25), 0.0, "keeper", (357/2, 550/2), 2, 3) anglesToTest = list(range(5, Agent.terminalPassAngle, Agent.passAngleGranularity)) print("these are the angls to test:") print(anglesToTest) for i in range(len(Agent.cosinesOfInterest)): self.assertAlmostEqual(Agent.cosinesOfInterest[i], math.cos(anglesToTest[i] * math.pi / 180) )
def testGetGridAsList(self): import keepAway world = keepAway.keepAway(0) e = birdsEyeView() grid = e.getBirdsEyeView(world.keeperArray, world.takerArray, world.get_display_width(), world.get_display_height()) listGrid = e.getBirdsEyeViewAsList(world.keeperArray, world.takerArray) for i in range(len(grid)): for j in range(len(grid[i])): self.assertEqual(grid[i][j], listGrid[i * len(grid[i]) + j])
def test_get_open_initial_points(self): import keepAway keepAwayWorld = keepAway.keepAway() #intialize agent to position 25,25 with no noise/error, as a keeper, with (357/2, 550/2) as center of field, #and 2 as agent speed, and 3 as ball speed testAgent = agent(keepAwayWorld,(25, 25), 0.0, "keeper", (357/2, 550/2), 2, 3) #print "display height: ", testAgent.worldRef.get_get_display_height()() rows = [53.55, 103.53, 153.51, 203.49, 253.47] cols = [82.5, 159.5, 236.5, 313.5, 390.5] testPoints = testAgent.getOpenPoints for i in range(len(rows)): for j in range(len(cols)): testPoint = testPoints[i * len(rows) + j] self.assertAlmostEqual(testPoint[0], rows[i]) self.assertAlmostEqual(testPoint[1], cols[j])
def __init__(self): world = keepAway.keepAway() mode =world.game_intro() if (mode == "q_learning"): #q learning mode print("execute q learning") elif (mode == "sarsa"): #sarsa mode print("execute sarsa") elif (mode == "hand_coded"): #sarsa mode #print("execute hand coded") print("execute hand coded") elif (mode == "manual"): print("execute manual debugging mode") world.gameLoop(mode) world.pause("game over. Final Score: ", world.keeperScore)
def __init__(self): world = keepAway.keepAway(inputAgentSigmaNoise = 0.0000001, alreadyTrained = True, bevCustomTileSize = 23) mode =world.game_intro() if (mode == "hyperNEAT"): #hyperNEAT mode print("execute hyperNEAT") elif(mode == "novelty"): #novelty search mode print("execute novelty search") elif (mode == "NEAT"): #NEAT mode print("execute NEAT") elif (mode == "hand_coded"): #hand coded mode print("execute hand coded") elif (mode == "manual"): print("execute manual debugging mode") world.gameLoop(mode, turnOnGrid = True, prettyGrid = False) world.pause("game over. Final Score: ", world.keeperScore)
def test_getStateVars2(self): import keepAway keepAwayWorld = keepAway.keepAway() simulatedError = 0.01 simWidth = 550.0 simHeight = 357.0 ballPos = (1 / 3 * simWidth, 1 / 6 * simHeight) c = (simWidth / 2.0, simHeight / 2.0) a1 = agent.agent( keepAwayWorld, (1 / 3 * simWidth, 1 / 6 * simHeight), simulatedError, "Keeper", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) a2 = agent.agent( keepAwayWorld, (2 / 3 * simWidth, 1 / 7 * simHeight), simulatedError, "Keeper", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) a3 = agent.agent( keepAwayWorld, (2 / 5 * simWidth, 6 / 7 * simHeight), simulatedError, "Keeper", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) keepers = [a1, a2, a3] t1 = agent.agent( keepAwayWorld, (1 / 2 * simWidth, 5 / 12 * simHeight), simulatedError, "Taker", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) t2 = agent.agent( keepAwayWorld, (2 / 5 * simWidth, 7 / 12 * simHeight), simulatedError, "Taker", ballPos, keepAwayWorld.maxPlayerSpeed, keepAwayWorld.maxBallSpeed, ) takers = [t1, t2] testOut = getStateVarsKeepers(keepers, takers, c) actualOut = [ kUtil.getDist((550 / 3, 59.5), c), kUtil.getDist((550 / 3 * 2, 51), c), kUtil.getDist((220, 306), c), kUtil.getDist((275, 148.75), c), kUtil.getDist((220, 208.25), c), kUtil.getDist((550 / 3, 59.5), (550 / 3 * 2, 51)), kUtil.getDist((550 / 3, 59.5), (220, 306)), kUtil.getDist((550 / 3, 59.5), (275, 148.75)), kUtil.getDist((550 / 3, 59.5), (220, 208.25)), min(kUtil.getDist((550 / 3 * 2, 51), (220, 208.25)), kUtil.getDist((550 / 3 * 2, 51), (275, 148.75))), min(kUtil.getDist((220, 306), (220, 208.25)), kUtil.getDist((220, 306), (275, 148.75))), max( kUtil.cosTheta((550 / 3 * 2, 51), (550 / 3, 59.5), (275, 148.75)), kUtil.cosTheta((550 / 3 * 2, 51), (550 / 3, 59.5), (220, 208.25)), ), max( kUtil.cosTheta((220, 306), (550 / 3, 59.5), (275, 148.75)), kUtil.cosTheta((220, 306), (550 / 3, 59.5), (220, 208.25)), ), ] for i in range(len(testOut)): self.assertAlmostEqual(testOut[i], actualOut[i], 1, "Failed on index: %d" % i)
import keepAway world = keepAway.keepAway() mode =world.game_intro() if (mode == "q_learning"): #q learning mode print("execute q learning") elif (mode == "sarsa"): #sarsa mode print("execute sarsa") elif (mode == "hand_coded"): #sarsa mode print("execute hand coded") elif (mode == "manual"): print("execute manual debugging mode") world.gameLoop(mode) world.pause("game over. Final Score: ", world.keeperScore)