def AI_loop(): #Release keys ai.thrust(0) ai.turnLeft(0) ai.turnRight(0) #Set variables heading = int(ai.selfHeadingDeg()) tracking = int(ai.selfTrackingDeg()) frontWall = ai.wallFeeler(500, heading) left45Wall = ai.wallFeeler(500, heading + 45) right45Wall = ai.wallFeeler(500, heading - 45) left90Wall = ai.wallFeeler(500, heading + 90) right90Wall = ai.wallFeeler(500, heading - 90) left135Wall = ai.wallFeeler(500, heading + 135) right135Wall = ai.wallFeeler(500, heading - 135) backWall = ai.wallFeeler(500, heading - 180) trackWall = ai.wallFeeler(500, tracking) ####### Shooting Ennemies ######## ##Find the closest ennemy## ClosestID = ai.closestShipId() #print(ClosestID) ##Get the closest ennemy direction and speed## ClosestSpeed = ai.enemySpeedId(ClosestID) #print(ClosestSpeed) ClosestDir = ai.enemyTrackingDegId(ClosestID) #print(ClosestDir) ## Get the lockheadingdeg ## enemy = ai.lockNext() print(enemy) head = ai.lockHeadingDeg() print(head) enemyDist = ai.selfLockDist() print(enemyDist) ### Turning Rules ### if frontWall <= 200 and (left45Wall < right45Wall): print("turning right") ai.turnRight(1) elif frontWall <= 200 and (left45Wall > right45Wall): ai.turnLeft(1) elif left90Wall <= 200: print("turning right") ai.turnRight(1) elif right90Wall <= 200: print("turning left") ai.turnLeft(1) ### Thrust commands #### elif ai.selfSpeed() <= 10 and (frontWall >= 200) and ( left45Wall >= 200) and (right45Wall >= 200) and ( right90Wall >= 200) and (left90Wall >= 200) and ( left135Wall >= 50) and (right135Wall >= 50) and (backWall >= 50): print("go forward") ai.thrust(1) elif trackWall < 75 and ai.selfSpeed() >= 10: ai.thrust(1) elif trackWall < 50 and ai.selfSpeed() <= 10: ai.thrust(1) elif backWall <= 75: ai.thrust(1) elif left135Wall <= 75: ai.thrust(1) elif right135Wall <= 75: ai.thrust(1) ##### Shooting Ennemy Commands ##### elif enemyDist <= 500 and heading > (head): ai.turnRight(1) ai.fireShot() elif enemyDist <= 500 and heading < (head): ai.turnLeft(1) ai.fireShot() else: print("chilling") ai.thrust(0)
def AI_loop(): global count_frame, loop, boolean, score, population_size, chromosome_size, population, mutation_prob, crossover_prob, fitness_list, generation, generation_size, first_time, done_learning #Release keys ai.thrust(0) ai.turnLeft(0) ai.turnRight(0) ## Get A Chromosome in the Population -- Eventually Will go through each individual in the population ## current_chromosome = population[loop] ## Transform Each Gene insisde A Single Selected Chromosome. 0s & 1s Are Turned Into Intergers For Fuzzy Sets to understand ## ## Each Value obtained is used to calculate the risk of each 45 degree around the agent ## ## Each value has its own "jump" variable which refers to the distance from each possible points/values ## ## The start and end represents the possible start point and end point for each variable and they depend on ## what the variiable is. It is to ensure a viable fuzzy set and fuzzy functions that these restrictions are applied. ## closingRate_SlowTopAlert = current_chromosome[0:4] closingRate_SlowTopAlertValue = transform_fuzzy(closingRate_SlowTopAlert, 1, 0, 16) closingRate_MediumTopLeftAlert = current_chromosome[4:8] closingRate_MediumTopLeftAlertValue = transform_fuzzy( closingRate_MediumTopLeftAlert, 1, (closingRate_SlowTopAlertValue + 1), (closingRate_SlowTopAlertValue + 1) + 16) closingRate_MediumTopRightAlert = current_chromosome[8:12] closingRate_MediumTopRightAlertValue = transform_fuzzy( closingRate_MediumTopRightAlert, 1, (closingRate_MediumTopLeftAlertValue + 1), (closingRate_MediumTopLeftAlertValue + 1) + 16) closingRate_FastTopAlert = current_chromosome[12:16] closingRate_FastTopAlertValue = transform_fuzzy( closingRate_FastTopAlert, 1, (closingRate_MediumTopRightAlertValue + 1), (closingRate_MediumTopRightAlertValue + 1) + 16) closingRate_SlowBottomAlert = current_chromosome[16:20] start = (closingRate_SlowTopAlertValue + (((closingRate_MediumTopLeftAlertValue - closingRate_SlowTopAlertValue) // 2) + 1)) end = (start + (1 * (2**(len(closingRate_SlowBottomAlert))))) closingRate_SlowBottomAlertValue = transform_fuzzy( closingRate_SlowBottomAlert, 1, start, end) closingRate_MediumBottomLeftAlert = current_chromosome[20:24] end = (closingRate_MediumTopLeftAlertValue - (((closingRate_MediumTopLeftAlertValue - closingRate_SlowTopAlertValue) // 2) + 1)) start = end - (1 * (2**(len(closingRate_MediumBottomLeftAlert)))) if (end < 0): end = 0 if (start < 0): start = 0 jump = (end - start) // (2**(len(closingRate_MediumBottomLeftAlert))) closingRate_MediumBottomLeftAlertValue = transform_fuzzy( closingRate_MediumBottomLeftAlert, jump, start, end) closingRate_MediumBottomRightAlert = current_chromosome[24:28] start = (closingRate_MediumTopRightAlertValue + (((closingRate_FastTopAlertValue - closingRate_MediumTopRightAlertValue) // 2) + 1)) end = start + (1 * (2**(len(closingRate_MediumBottomRightAlert)))) closingRate_MediumBottomRightAlertValue = transform_fuzzy( closingRate_MediumBottomRightAlert, 1, start, end) closingRate_FastBottomAlert = current_chromosome[28:32] end = (closingRate_FastTopAlertValue - (((closingRate_FastTopAlertValue - closingRate_MediumTopRightAlertValue) // 2) + 1)) start = end - (1 * (2**(len(closingRate_FastBottomAlert)))) if (end < 0): end = 0 if (start < 0): start = 0 jump = (end - start) // (2**(len(closingRate_FastBottomAlert))) closingRate_FastBottomAlertValue = transform_fuzzy( closingRate_FastBottomAlert, jump, start, end) Distance_CloseTopAlert = current_chromosome[32:37] Distance_CloseTopAlertValue = transform_fuzzy( Distance_CloseTopAlert, 50, 0, (50 * (2**len(Distance_CloseTopAlert)))) Distance_FarTopAlert = current_chromosome[37:42] Distance_FarTopAlertValue = transform_fuzzy( Distance_CloseTopAlert, 50, (Distance_CloseTopAlertValue + 50), (Distance_CloseTopAlertValue + 50) + (50 * (2**len(Distance_CloseTopAlert)))) Distance_CloseBottomAlert = current_chromosome[42:47] start = (Distance_CloseTopAlertValue + (( (Distance_FarTopAlertValue - Distance_CloseTopAlertValue) // 2) + 1)) end = Distance_FarTopAlertValue jump = (end - start) // (2**(len(Distance_CloseBottomAlert))) Distance_CloseBottomAlertValue = transform_fuzzy(Distance_CloseBottomAlert, jump, start, end) Distance_FarBottomAlert = current_chromosome[47:52] end = (Distance_FarTopAlertValue - (( (Distance_FarTopAlertValue - Distance_CloseTopAlertValue) // 2) + 1)) start = Distance_CloseTopAlertValue jump = (end - start) // (2**(len(Distance_FarBottomAlert))) Distance_FarBottomAlertValue = transform_fuzzy(Distance_FarBottomAlert, jump, start, end) #Set variables for Wall feelers, heading and tracking of the agent ## heading = int(ai.selfHeadingDeg()) tracking = int(ai.selfTrackingDeg()) frontWall = ai.wallFeeler(500, heading) left45Wall = ai.wallFeeler(500, heading + 45) right45Wall = ai.wallFeeler(500, heading - 45) left90Wall = ai.wallFeeler(500, heading + 90) right90Wall = ai.wallFeeler(500, heading - 90) left135Wall = ai.wallFeeler(500, heading + 135) right135Wall = ai.wallFeeler(500, heading - 135) backWall = ai.wallFeeler(500, heading - 180) trackWall = ai.wallFeeler(500, tracking) ## Create an array that represents the closing rate of each 45 degree of the full 360 degrees surrounding the agent ## result_list = [] ## Array of the same size, but contains the risk of each direction ## risk_list = [] for i in range(8): Degree = tracking + (45 * i) Speed = ai.selfSpeed() Distance = ai.wallFeeler(10000, tracking + (45 * i)) result = Closing_Rate(Degree, tracking, Speed, Distance) result_list.append(result) ### Calculate the Fuzzy membership ### ### 1. Fuzzy Membership For Closing Rate (Speed + Tracking Involved) ### ### 2. Fuzzy Membership For Distance From Walls ### closing_rate, distance = Closing_Rate(Degree, tracking, Speed, Distance) low, medium, fast = Fuzzy_Speed( closing_rate, closingRate_SlowTopAlertValue, closingRate_SlowBottomAlertValue, closingRate_MediumBottomLeftAlertValue, closingRate_MediumTopLeftAlertValue, closingRate_MediumTopRightAlertValue, closingRate_MediumBottomRightAlertValue, closingRate_FastBottomAlertValue, closingRate_FastTopAlertValue) close, far = Fuzzy_Distance(distance, Distance_CloseTopAlertValue, Distance_CloseBottomAlertValue, Distance_FarBottomAlertValue, Distance_FarTopAlertValue) #print("close-far", close, far) risk = Fuzzy_Risk(low, medium, fast, close, far) risk_list.append(risk) ## Get the direction in deg that is most risky for the robot as well as the least risky direction ## max_risk = max(risk_list) track_risk = (tracking + (risk_list.index(max_risk) * 45) % 360) min_risk = min( risk_list ) ## Note: Biase Towards Left Side since min get the first min when risk might be equal ## ####### Getters Variable Regarding Important Information About Enemies ######## ##Find the closest ennemy## enemy = ai.lockClose() ## Get the lockheadingdeg of enemy ## head = ai.lockHeadingDeg() ## Get the dstance from enemy ## enemyDist = ai.selfLockDist() ## If the Enemy is Dead ## if (ai.selfAlive() == 0 and boolean == False): ## Calculate Fitness Current Individual ## score_previous = score score_current = ai.selfScore() fitness_value = fitness(population, count_frame, score_previous, score_current) fitness_list.append(fitness_value) ## If it went through the whole population and ready to move to next generation ## if ((loop + 1) == population_size): ## Output the fitness of population to allow user to see if learning is happening ## print("Generation:", generation) print("Agent Fitness:") print(fitness_list) print("Average Fitness:", statistics.mean(fitness_list)) print("Best Fitness:", max(fitness_list)) ## Finding the optimal chromosome to output it in data file ## string_maxChromosome = "" for chrom_max in range(chromosome_size): string_maxChromosome = string_maxChromosome + str( population[fitness_list.index( max(fitness_list))][chrom_max]) ## Formatting entire population in a big string to register it in excel file## string_population = "" for pop in range(population_size): for pop_chrom in range(chromosome_size): string_population = string_population + str( population[pop][pop_chrom]) if (pop != (population_size - 1)): string_population = string_population + "," ## Formatting entire population's fitness in a big string to register it in excel file## string_fitness = "" for fit in range(len(fitness_list)): string_fitness = string_fitness + str(fitness_list[fit]) if (fit != (len(fitness_list) - 1)): string_fitness = string_fitness + "," ## Output Data into Excel File ## titles = [ "Generation", "Average Fitness", "Best Fitness", "Population Size", "Chromosome Size", "Crossover Probability", "Mutation Probability", "Best Chromosome", "Entire Population Chromosome", "Entire Population Fitness" ] data = [ generation, statistics.mean(fitness_list), max(fitness_list), population_size, chromosome_size, crossover_prob, mutation_prob, string_maxChromosome, string_population, string_fitness ] first_time = Save_Data("Dumpster_Training_Data.xls", 0, titles, data, first_time) ## Select Population For Next Generation -- Apply Crossover & Mutation ## new_population = select(population, fitness_list) new_population = crossover(new_population, chromosome_size, population_size, crossover_prob) new_population = mutate(new_population, chromosome_size, mutation_prob) population = new_population loop = 0 count_frame = 0 generation += 1 fitness_list.clear() ### DONE -- QUIT ### if (generation == generation_size): quitAI() ## Move to the next individual in population ## else: loop += 1 count_frame = 0 boolean = True else: ## The agent is Alive ## if (ai.selfAlive() == 1): ## Get the angles on both side between tracking and heading to decide which way to turn ## dist = (heading - track_risk) % 360 dist2 = (360 - dist) % 360 ###### Production System Rules ###### ## Turning Rules ## if (dist <= 130 and dist >= 0 and ai.selfSpeed() > 0 and max_risk >= 75): ai.turnLeft(1) elif (dist2 <= 130 and dist2 >= 0 and ai.selfSpeed() > 0 and max_risk >= 75): ai.turnRight(1) elif (ai.selfSpeed() <= 10): ai.thrust(1) elif (trackWall <= 150): ai.thrust(1) ##### Bullet Avoidance Commands ##### elif (ai.shotAlert(0) >= 0 and ai.shotAlert(0) <= 50): if (ai.shotVelDir(0) != -1 and ai.angleDiff(heading, ai.shotVelDir(0)) > 0 and ai.selfSpeed() <= 5): ai.turnLeft(1) ai.thrust(1) elif (ai.shotVelDir(0) != -1 and ai.angleDiff(heading, ai.shotVelDir(0)) < 0 and ai.selfSpeed() <= 5): ai.turnRight(1) ai.thrust(1) elif (ai.shotVelDir(0) != -1 and ai.angleDiff(heading, ai.shotVelDir(0)) > 0 and ai.selfSpeed() > 5): ai.turnLeft(1) else: ai.turnRight(1) ##### Shooting Ennemy Commands ##### elif (enemyDist <= 3000 and heading > (head) and enemyDist != 0 and ai.selfSpeed() > 5): ai.turnRight(1) ai.fireShot() elif (enemyDist <= 3000 and heading < (head) and enemyDist != 0 and ai.selfSpeed() > 5): ai.turnLeft(1) ai.fireShot() ## Rules if nothing is happening ## elif (ai.selfSpeed() < 5): ai.thrust(1) else: ai.thrust(0) count_frame += 3 boolean = False
def AI_loop(): turn, thrust = .5, 0 ai.turnLeft(0) ai.turnRight(0) ai.thrust(0) ai.setTurnSpeed(64) heading = int(ai.selfHeadingDeg()) tracking = int(ai.selfTrackingDeg()) trackWall = ai.wallFeeler(500, tracking) trackL3 = ai.wallFeeler(500, tracking + 3) trackL10 = ai.wallFeeler(500, tracking + 10) trackR3 = ai.wallFeeler(500, tracking - 3) trackR10 = ai.wallFeeler(500, tracking - 10) frontWall = ai.wallFeeler(500, heading) frontL = ai.wallFeeler(500, heading + 15) frontR = ai.wallFeeler(500, heading - 15) leftWall = ai.wallFeeler(500, heading + 90) leftF = ai.wallFeeler(500, heading + 65) leftB = ai.wallFeeler(500, heading + 115) rightWall = ai.wallFeeler(500, heading - 90) rightF = ai.wallFeeler(500, heading - 65) rightB = ai.wallFeeler(500, heading - 115) backWall = ai.wallFeeler(500, heading - 180) backL = ai.wallFeeler(500, heading - 195) backR = ai.wallFeeler(500, heading - 165) trackHeadRelative = (tracking - heading) speed = ai.selfSpeed() def findClosestArea(x): return { frontWall: 1, frontL: 2, leftF: 3, leftWall: 4, leftB: 5, backL: 6, backWall: 7, backR: 8, rightB: 9, rightWall: 10, rightF: 11, frontR: 12 }[x] closestVal = min(frontWall, frontL, leftF, leftWall, leftB, backL, backWall, backR, rightB, rightWall, rightF, frontR) #Find the closest Wall to our ship closestWall = findClosestArea(closestVal) #The wall we are likely to crash into if we continue on our current course crashWall = min(trackWall, trackL3, trackL10, trackR3, trackR10) #Rules for turning if closestWall == 1: ai.setTurnSpeed(64) ai.turnLeft(1) turn = 0 elif closestWall == 2: ai.setTurnSpeed(64) ai.turnRight(1) turn = 1 elif closestWall == 3: ai.setTurnSpeed(52) ai.turnRight(1) turn = .9 elif closestWall == 4: ai.setTurnSpeed(40) ai.turnRight(1) turn = .8 elif closestWall == 5: ai.setTurnSpeed(28) ai.turnRight(1) turn = .7 elif closestWall == 6: ai.setTurnSpeed(16) ai.turnRight(1) turn = .6 elif closestWall == 7: pass elif closestWall == 8: ai.setTurnSpeed(16) ai.turnLeft(1) turn = .4 elif closestWall == 9: ai.setTurnSpeed(28) ai.turnLeft(1) turn = .3 elif closestWall == 10: ai.setTurnSpeed(40) ai.turnLeft(1) turn = .2 elif closestWall == 11: ai.setTurnSpeed(52) ai.turnLeft(1) turn = .1 elif closestWall == 12: ai.setTurnSpeed(64) ai.turnLeft(1) turn = 0 #Rules for thrusting #if we are going slow and there isn't a wall in front of us if min(frontWall, frontL, frontR) > 100 and speed < 4: ai.thrust(1) thrust = 1 #if we are heading toward a wall and we are not facing it elif crashWall < 150 and (ai.angleDiff(heading, tracking) > 90): ai.thrust(1) thrust = 1 #If there is a wall very close behind us, get away from it elif backWall < 20 or backL < 20 or backR < 20: ai.thrust(1) thrust = 1
def AI_loop(self): # print("AI_LOOP") if ai.selfAlive() == 0: outputFile = open("fitness.txt", "a") # outputFile.write(str((self.totalDists/self.counter))+"\t") outputFile.write(str(int((self.fitness**1.2))) + "\t") [ print(str("%.5f" % g) + "\t", end="", file=outputFile) for g in self.chromosome ] print("\n", end="", file=outputFile) outputFile.close() # Release keys ai.thrust(0) ai.turnLeft(0) ai.turnRight(0) ai.setTurnSpeed(55) # Heuristics frontFeelerOffset = 45 perpFeelerOffset = 90 rearFeelerOffset = 135 # turnSpeedMin = 15 # learn range: 4 - 24 turnSpeedMax = 55 speedLimit = 5 # learn range: 2-6 lowSpeedLimit = 2 targetingAccuracy = 4 # 1/2 tolerance in deg for aiming accuracy shotIsDangerous = 130 # Acquire information heading = int(ai.selfHeadingDeg()) tracking = int(ai.selfTrackingDeg()) ###=== ENEMY FEELERS ===### # gets angle to enemy enemyDeg = self.angleToPointDeg( (ai.selfX(), ai.selfY()), (ai.screenEnemyX(0), ai.screenEnemyY(0))) enemyWallDistances = [] # maxAngleOffset = 90 # learn range: 30 - 120 # resolution = 5 # learn range: 2 - 10 distAngleTuples = [] # creates tuples of degrees and wallFeelers for m in (0, self.maxAngleOffset, self.resolution): distAngleTuples.append( (enemyDeg - m, ai.wallFeeler(500, int(enemyDeg - m)))) distAngleTuples.append( (enemyDeg + m, ai.wallFeeler(500, int(enemyDeg + m)))) # gets furthest feeler maxFeelerAngle = max(distAngleTuples, key=self.returnSecond) angleToOpenSpace = self.headingDiff(ai.selfHeadingDeg(), maxFeelerAngle[0]) ###=== WALL FEELERS ===### frontWall = ai.wallFeeler( self.genericFeelerDist, heading) # wall feeler for wall directly ahead leftFrontWall = ai.wallFeeler( self.genericFeelerDist, heading + frontFeelerOffset) # wall feeler for wall 45 degrees to the left rightFrontWall = ai.wallFeeler( self.genericFeelerDist, heading - frontFeelerOffset) # wall feeler for wall 45 degrees to the right leftWall = ai.wallFeeler( self.genericFeelerDist, heading + perpFeelerOffset) # wall feeler for wall 90 degrees to the left rightWall = ai.wallFeeler( self.genericFeelerDist, heading - perpFeelerOffset) # wall feeler for wall 90 degrees to the right backWall = ai.wallFeeler(self.genericFeelerDist, heading - 180) # wall feeler for wall straight back leftBackWall = ai.wallFeeler( self.genericFeelerDist, heading + rearFeelerOffset) # wall feeler for wall 135 degrees to the left rightBackWall = ai.wallFeeler( self.genericFeelerDist, heading - rearFeelerOffset) # wall feeler for wall 135 degrees to the right trackWall = ai.wallFeeler( self.genericFeelerDist, tracking) # wall in front of where ship is moving # Keep track of all the feeler distances feelers = [ frontWall, leftFrontWall, rightFrontWall, leftWall, rightWall, backWall, leftBackWall, rightBackWall, trackWall ] # Aim assist leftDir = (heading + 90) % 360 # angle 90 degrees to the left of current heading rightDir = (heading - 90 ) % 360 # angle 90 degrees to the right of current heading aimer = ai.aimdir( 0 ) # direction that the ship needs to turn to in order to face the enemy in degrees shot = ai.shotAlert( 0 ) # returns a danger rating of a shot, the smaller the number the more likely the shot is to hit the ship enemyX = ai.screenEnemyX(0) # returns the closest enemy's x-coord enemyY = ai.screenEnemyY(0) # returns the closest enemy's y-coord selfX = ai.selfX() # returns the ship's x-coord selfY = ai.selfY() # returns the ship's x-coord # Fuzzy variable declaration trackRisk = riskEval(trackWall, ai.selfSpeed()) #risk of running into trackWall frontRisk = riskEval(frontWall, ai.selfSpeed()) #risk of running into frontWall leftRisk = riskEval(leftWall, ai.selfSpeed()) #risk of running into leftWall rightRisk = riskEval(rightWall, ai.selfSpeed()) #risk of running into rightWall LFRisk = riskEval(leftFrontWall, ai.selfSpeed()) #risk of running into leftFrontWall RFRisk = riskEval(rightFrontWall, ai.selfSpeed()) #risk of running into rightFrontWall LBRisk = riskEval(leftBackWall, ai.selfSpeed()) #risk of running into leftBackWall RBRisk = riskEval(rightBackWall, ai.selfSpeed()) #risk of running into rightBackWall backRisk = riskEval(backWall, ai.selfSpeed()) #risk of running into backWall # Compress some wall feelers sTrack = self.squisher(trackWall) sLeft = self.squisher(leftFrontWall) sRight = self.squisher(rightFrontWall) sLeftStraight = self.squisher(leftWall) sRightStraight = self.squisher(rightWall) # output from neural network that tells how much to turn and which direction turn = self.trainedNeuralNetwork(sTrack, sLeft, sRight, sLeftStraight, sRightStraight) ###=== THRUST POWER ADJUSTMENT ===# # Power levels mfS = self.mfSpeed(ai.selfSpeed()) mfD = self.mfDanger(ai.shotAlert(0)) # if S is high and D is moderate or high: p1 = max(mfS[2], min(mfD[1], mfD[2])) # if S is moderate and D is moderate: p2 = max(mfS[1], mfD[1]) # if S is low and D is high: p3 = max(mfS[0], mfD[2]) # if S is moderate and D is moderate: p4 = max(mfS[1], mfD[1]) # if S is low and D is moderate: p5 = max(mfS[0], mfD[1]) # if S is high and D is low: p6 = max(mfS[2], mfD[0]) # if S is moderate and D is low: p7 = max(mfS[1], mfD[0]) # if S is low and D is low: p8 = max(mfS[0], mfD[0]) consequents = [55, 45, 55, 36, 36, 28, 24, 30] memberships = [p1, p2, p3, p4, p5, p6, p7, p8] ai.setPower(self.crispify(memberships, consequents)) if ai.enemyDistance(0) > self.lastDist and ai.enemyDistance( 0) < self.enemyClose: ai.thrust(1) elif ai.selfSpeed( ) <= 3 and frontWall >= 200: # if speed is slow and front wall is far away, thrust ai.thrust(1) elif trackWall < 60 and frontWall >= 200: # if the track wall is close, thrust ai.thrust(1) elif backWall < 20: # if the back wall is close, thrust ai.thrust(1) ###=== TURNING RULES ===### # Escape shots if shot > 0 and shot < 70: # if a shot is closeby, turn and thrust to avoid if self.angleDif(rightDir, ai.shotX(0)) < self.angleDif( leftDir, ai.shotX(0) ) or self.angleDif(rightDir, ai.shotY(0)) < self.angleDif( leftDir, ai.shotY(0) ): # if shot is coming from the right, turn away and thrust # print("Turning: avoiding shot")#debug ai.turnLeft(1) ai.thrust(1) elif self.angleDif(leftDir, ai.shotX(0)) < self.angleDif( rightDir, ai.shotX(0) ) or self.angleDif(leftDir, ai.shotY(0)) < self.angleDif( rightDir, ai.shotY(0) ): # if shot is coming from the left, turn away and shoot ------> change this shot is just a number # print("Turning: avoiding shot")#debug ai.turnRight(1) ai.thrust(1) # Turn towards unoccluded enemy elif aimer >= 0 and self.angleDif(rightDir, aimer) < self.angleDif( leftDir, aimer) and not self.enemyBehindWall( 0): # if an enemy to the right, turn and shoot it if ai.screenEnemyX(0) >= 0: enemyDeg = self.angleToPointDeg( (ai.selfX(), ai.selfY()), (ai.screenEnemyX(0), ai.screenEnemyY(0))) ai.setTurnSpeed( self.rangeMap(abs(enemyDeg), 0, 180, self.turnSpeedMin, turnSpeedMax)) else: enemyDeg = self.angleToPointDeg( (ai.selfRadarX(), ai.selfRadarY()), (ai.closestRadarX(), ai.closestRadarY())) ai.setTurnSpeed( self.rangeMap(abs(enemyDeg), 0, 180, self.turnSpeedMin, turnSpeedMax)) # print("Turning: aiming right")#debug ai.turnRight(1) elif aimer >= 0 and self.angleDif(leftDir, aimer) < self.angleDif( rightDir, aimer) and not self.enemyBehindWall( 0): # if an enemy to the left, turn and shoot it if ai.screenEnemyX(0) >= 0: enemyDeg = self.angleToPointDeg( (ai.selfX(), ai.selfY()), (ai.screenEnemyX(0), ai.screenEnemyY(0))) ai.setTurnSpeed( self.rangeMap(abs(enemyDeg), 0, 180, self.turnSpeedMin, turnSpeedMax)) else: enemyDeg = self.angleToPointDeg( (ai.selfRadarX(), ai.selfRadarY()), (ai.closestRadarX(), ai.closestRadarY())) ai.setTurnSpeed( self.rangeMap(abs(enemyDeg), 0, 180, self.turnSpeedMin, turnSpeedMax)) # print("Turning: aiming left")#debug ai.turnLeft(1) #fuzzy avoid walls ahead elif leftRisk > rightRisk and trackRisk > 0.5: # and min(feelers) < self.nearLimit: #if the left wall and track walls are close, turn right #if enemyX >=0 and enemyY >= 0 and ai.wallBetween(selfX, selfY, enemyX, enemyY) == -1: ai.turnRight(1) # print("Turning: fuzzy right")#debug elif rightRisk > leftRisk and trackRisk > 0.5: # and min(feelers) < self.nearLimit: #if the right wall and track walls are close, turn left # if enemyX >=0 and enemyY >= 0 and ai.wallBetween(selfX, selfY, enemyX, enemyY) == -1: ai.turnLeft(1) # print("Turning: fuzzy left")#debug # Turn to open space nearest the angle to the enemy elif self.enemyBehindWall(0) and min(feelers) > self.nearLimit: if angleToOpenSpace < 0: # print("Turning: open space left")#debug ai.turnLeft(1) elif angleToOpenSpace > 0: # print("Turning: open space right")#debug ai.turnRight(1) # if neural net value is not between 0.48 and 0.52 then we have to turn right or left elif not (turn >= 0.43 and turn <= 0.57): if turn < 0.43: # turn right if value is below 0.43 # print("Turning: neural net right")#debug ai.turnRight(1) elif turn > 0.57: # turn left if value is below 0.57 # print("Turning: neural net left")#debug ai.turnLeft(1) ###=== FIRING RULES ===### # Restrict firing to reasonably accurate attempts: # accurate range, enemy not behind wall and enemy close enough if self.headingDiff( heading, ai.aimdir(0)) < targetingAccuracy and not self.enemyBehindWall( 0) and ai.enemyDistance(0) < self.enemyFireDist: ai.fireShot() # print("Shot Fired")#debug # print("Firing Dist: ", self.enemyFireDist)#debug self.counter += 1 ###=== How did we die? and other Fitness Calculations ===### # Fitness function information self.totalDists += ai.enemyDistance(0) if ai.enemyDistance(0) > 0: self.currentDist = ai.enemyDistance(0) if self.currentDist < self.lastDist: self.fitness += 1 self.lastDist = self.currentDist self.fitness += 1 alive = ai.selfAlive() message = ai.scanGameMsg(1) # print(message)#debug if alive == 0: self.framesDead += 1 # print(self.framesDead, message)#debug if self.framesDead == 2: # print("dead now")#debug # Ran into wall if message.find("Beal-Morneault") != -1 and message.find( "wall") != -1: print("End of match: wall collision.") #debug self.fitness -= self.wallPenalty # Crashed into player elif message.find("crashed.") != -1: print("End of match: player collision.") #debug self.fitness -= self.crashPenalty # Killed by bullet elif message.find("Beal-Morneault was") != -1: print("End of match: killed by opponent.") #debug self.fitness -= self.killedPenalty # Killed the opponent elif message.find("by a shot from Beal-Morneault") != -1: print("End of match: killed the opponent!") #debug self.fitness += self.killerBonus else: print("End of match: enemy died.") self.fitness += (ai.selfScore() - ai.enemyScoreId(0) ) * self.scoreDiffBonusFactor ai.quitAI() else: self.framesDead = 0
def getSendData(turn,thrust,shoot): #make all the feelers/get current sichuation data heading = int(ai.selfHeadingDeg()) tracking = int(ai.selfTrackingDeg()) trackHeadRelative = (tracking-heading)/360 trackWall = ai.wallFeeler(500, tracking) trackL3 = ai.wallFeeler(500, tracking + 3) trackL10 = ai.wallFeeler(500, tracking + 10) trackR3 = ai.wallFeeler(500, tracking - 3) trackR10 = ai.wallFeeler(500, tracking - 10) frontWall = ai.wallFeeler(500, heading) frontL = ai.wallFeeler(500, heading + 10) frontR = ai.wallFeeler(500, heading - 10) leftWall = ai.wallFeeler(500, heading+90) rightWall = ai.wallFeeler(500,heading-90) backWall = ai.wallFeeler(500, heading - 180) backL = ai.wallFeeler(500, heading - 185) backR = ai.wallFeeler(500, heading - 175) speed = ai.selfSpeed()/10 #get position to enemy enemyX = ai.screenEnemyX(0) enemyY = ai.screenEnemyY(0) selfX = ai.selfX() selfY = ai.selfY() enemyDegrees = (heading - (math.degrees(math.atan2(enemyY-selfY,enemyX-selfX))+360)%360)/360 #enemyDegrees = heading - math.degrees(math.atan2(enemyY-selfY,enemyX-selfX))-360 enemySpeed = ai.enemySpeed(0)/10 enemyMoveDirection = ai.enemyTrackingDeg(0) distanceToEnemy = 1 - math.sqrt((selfX-enemyX)**2 + (selfY-enemyY)**2)/500 relativeTracking = (tracking-180)/360-(enemyMoveDirection-180)/360 #get shots at us data = [heading/360,tracking/360,trackHeadRelative,speed] for i in [trackWall,trackL3,trackL10,trackR3,trackR10,frontWall,frontL,frontR,leftWall,rightWall,backWall,backL,backR]: if i == -1: data.append(0) else: data.append(1 - i/500) for j in [enemySpeed,enemyDegrees,relativeTracking,distanceToEnemy]: if not math.isnan(j): if enemyX == -1: data.append(0) else: data.append(j) else: data.append(0) for k in [turn,thrust,shoot]: data.append(k) #print("distToEnemy",data[20]*500,"relTrack",data[19]*360) return data
def AI_loop(): #Release keys ai.thrust(0) ai.turnLeft(0) ai.turnRight(0) ai.setTurnSpeed(45) turn, thrust, shoot = 0.5, 0, 0 maxSpeed = 3 shotAngle = 9 wallClose = 12 #Set variables""" heading = int(ai.selfHeadingDeg()) tracking = int(ai.selfTrackingDeg()) trackWall = ai.wallFeeler(500, tracking) trackLWall = ai.wallFeeler(500, tracking+3) trackRWall = ai.wallFeeler(500, tracking - 3) frontWall = ai.wallFeeler(500,heading) flWall = ai.wallFeeler(500, heading + 10) frWall = ai.wallFeeler(500, heading - 10) leftWall = ai.wallFeeler(500,heading+90) rightWall = ai.wallFeeler(500,heading-90) trackWall = ai.wallFeeler(500,tracking) backWall = ai.wallFeeler(500, heading - 180) backLeftWall = ai.wallFeeler(500, heading - 185) backRightWall = ai.wallFeeler(500, heading - 175) speed = ai.selfSpeed() closest = min(frontWall, leftWall, rightWall, backWall, flWall, frWall) def closestWall(x): #Find the closest Wall return { frontWall : 1, leftWall : 2, rightWall : 3, backWall : 4, flWall : 5, frWall : 6, }[x] wallNum = closestWall(closest) #Code for finding the angle to the closest ship targetX, targetY = ai.screenEnemyX(0), ai.screenEnemyY(0) calcDir = 0 if targetX- ai.selfX() != 0: calcDir = (math.degrees(math.atan2((targetY - ai.selfY()), (targetX- ai.selfX()))) + 360)%360 crashWall = min(trackWall, trackLWall, trackRWall) #The wall we are likely to crash into if we continue on our current course #Rules for turning if crashWall > wallClose*speed and closest > 25 and targetX != -1: #If we are far enough away from a predicted crash and no closer than 25 pixels to a wall we can try and aim and kill them diff = (calcDir - heading) if ai.shotAlert(0) > -1 and ai.shotAlert(0) < 35: #If we are about to get shot ai.turnRight(1) #Screw aiming and turn right and thrust ai.thrust(1) thrust = 1 #This is arguably a horrible strategy because our sideways profile is much larger, but it's required for the grade elif diff >= 0: if diff >= 180: ai.turnRight(1) #If the target is to our right- turn right turn = 1 else : ai.turnLeft(1) #If the target is to our left - turn left turn = 0 else : if diff > -180: ai.turnRight(1) #If the target is to our right - turn right turn = 1 else : ai.turnLeft(1) #If the target is to our left - turn left turn = 0 #Rules for avoiding death else : # if crashWall/ai.selfSpeed() > ai.closestShot() : if wallNum == 1 or wallNum == 5 or wallNum == 6: #Front Wall is Closest (Turn Away From It) ai.turnLeft(1) turn = 0 elif wallNum == 2 : # Left Wall is Closest (Turn Away From It) ai.turnRight(1) turn = 1 elif wallNum == 3 : #Right Wall is Closest (Turn Away From It) ai.turnLeft(1) turn = 0 else : #Back Wall is closest- turn so that we are facing directly away from it if backLeftWall < backRightWall: ai.turnRight(1) #We need to turn right to face more directly away from it turn = 1 if backLeftWall > backRightWall: # We need to turn left to face more directly away from it ai.turnLeft(1) turn = 0 #Rules for thrusting if speed < maxSpeed and frontWall > 100: #If we are moving slowly and we won't ram into anything, accelerate ai.thrust(1) thrust = 1 elif trackWall < 250 and (ai.angleDiff(heading, tracking) > 120): #If we are getting close to a wall, and we can thrust away from it, do so ai.thrust(1) thrust = 1 elif backWall < 20: #If there is a wall very close behind us, get away from it ai.thrust(1) thrust = 1 if abs(calcDir - heading) < shotAngle : #If we are close to the current proper trajectory for a shot then fire ai.fireShot() shoot = 1 #adjust the the learning NN infile = open("myBotWeights.txt","r") weight = eval(infile.read()) infile.close() sendData = getSendData(turn, thrust, shoot) weight = adjustNN(sendData, 21, 8, 3, weight) outfile = open("myBotWeights.txt","w") outfile.write(str(weight)) outfile.close()
def AI_loop(self): # Release keys ai.thrust(0) ai.turnLeft(0) ai.turnRight(0) # ai.setPower(30) #-------------------- Set variables --------------------# heading = int(ai.selfHeadingDeg()) tracking = int(ai.selfTrackingDeg()) frontWall = ai.wallFeeler(500, heading) leftWall = ai.wallFeeler(500, heading + 45) rightWall = ai.wallFeeler(500, heading - 45) leftWallStraight = ai.wallFeeler(500, heading + 90) rightWallStraight = ai.wallFeeler(500, heading - 90) leftBack = ai.wallFeeler(500, heading + 135) rightBack = ai.wallFeeler(500, heading - 135) backWall = ai.wallFeeler(500, heading - 180) trackWall = ai.wallFeeler(500, tracking) R = (heading - 90) % 360 L = (heading + 90) % 360 aim = ai.aimdir(0) bullet = ai.shotAlert(0) speed = ai.selfSpeed() x = ai.selfX() y = ai.selfY() targetX = self.coordinateList[self.counter % self.coordListLength][0] targetY = self.coordinateList[self.counter % self.coordListLength][1] toTurn = self.angleToPoint(x, y, targetX, targetY, heading) distance = self.distance(x, targetX, y, targetY) #-------------------- Print statements --------------------# print("(x, y): (", x, ",", y, ")") print("destination: ", self.coordinateList[self.counter % self.coordListLength]) print("distance: ", distance) # print("closestEnemyX: ", closestEnemyX) # print("closestEnemyY: ", closestEnemyY) # print("difference x: ", differenceX) # print("difference y: ", differenceY) # print("degrees: ", degrees) # print("screen enemy? ", ai.screenEnemyXId(ai.closestShipId())) print("toTurn: ", toTurn) print() #-------------------- Move to target point --------------------# # if toTurn > 0 and toTurn < 30 and distance > 300 and speed <= 15 and speed >= 4: if abs(toTurn) < 20 and distance > 200: print("Lock!") ai.turnLeft(0) ai.turnRight(0) if self.frames % 40 == 0: ai.thrust(1) elif toTurn >= 20: print("Turning right!") ai.turnLeft(1) elif toTurn <= -20: ai.turnRight(1) print("Turning left!") if distance < 200: # ai.thrust(0) self.counter = self.counter + 1 # #-------------------- Thrust rules --------------------# # if speed <= 3 and frontWall >= 200: # print("Front wall far") # ai.thrust(1) # elif trackWall < 50: # print("Close to track wall") # ai.thrust(1) # elif backWall < 40: # print("Close to back wall") # ai.thrust(1) # #---------------- Turn rules ----------------# # # Figures out what corner we are in and turns the right directon # if (backWall < 30) and (rightWallStraight < 200): # print("Corners 1") # ai.turnLeft(1) # elif backWall < 30 and (leftWallStraight < 200): # print("Corners 2") # ai.turnRight(1) # # Walls along our periphery (90 degree feelers) # elif leftWallStraight < rightWallStraight and trackWall < 75: # print("90 left danger") # ai.turnRight(1) # elif leftWallStraight > rightWallStraight and trackWall < 75: # print("90 right danger") # ai.turnLeft(1) self.frames = self.frames + 1