def reset(self, isTest, foodGet, temperature, display=False): """ Reset the environment for the training """ # if it is a test phase, we put the food get by the agent at the end into the list result if (isTest): foodGet.append(self.totalFood) self.Temp = 0 else: self.Temp = temperature self.agent = agt.Agent(12, 18, 40) self.ennemies = [ Ennemy(6, 6, self), Ennemy(12, 2, self), Ennemy(12, 6, self), Ennemy(18, 6, self) ] self.environment = [[0 if j == 2 else j for j in i] for i in self.environment] # we reset the location of the food for i in range(15): x = random.randint(0, 24) y = random.randint(0, 24) while not self.is_empty(x, y): x = random.randint(0, 24) y = random.randint(0, 24) self.environment[x][y] = 2 self.end = False self.killed = False self.dead = False self.totalFood = 0 #food eaten during the simulation self.memory = [] # we reset the list of experiences
def testOPatch(self): testgrid = env.State([], nn) for i in range(env.width): for j in range(env.heigth): testgrid.environment[i][j] = 0 testgrid.environment[10][10] = 2 testgrid.ennemies = ([ Ennemy(5, 5, testgrid.environment), Ennemy(-1, 0, testgrid.environment) ]) for i in range(-1, env.width + 1, 1): for j in range(-1, env.heigth + 1, 1): if (i == 5 and j == 5) or (i == 4 and j == 5) or ( i == 5 and j == 4) or (i == 6 and j == 5) or ( i == 5 and j == 6) or (i == 4 and j == 4) or ( i == 4 and j == 6) or (i == 6 and j == 6) or ( i == 6 and j == 4): self.assertTrue(testgrid.Opatch(1, i, j)) self.assertFalse(testgrid.Opatch(2, i, j)) elif (i == 10 and j == 10) or (i == 9 and j == 10) or ( i == 10 and j == 9) or (i == 11 and j == 10) or ( i == 10 and j == 11) or (i == 9 and j == 9) or ( i == 9 and j == 11) or (i == 11 and j == 9) or (i == 11 and j == 11): self.assertTrue(testgrid.Opatch(2, i, j)) self.assertFalse(testgrid.Opatch(1, i, j)) else: self.assertFalse(testgrid.Opatch(1, i, j)) self.assertFalse(testgrid.Opatch(2, i, j))
def testEnnemyLookUp(self): testgrid = env.State([], nn) testgrid.ennemies = ([ Ennemy(5, 5, testgrid.environment), Ennemy(-1, -1, testgrid.environment) ]) for i in range(-1, env.width + 1, 1): for j in range(-1, env.heigth + 1, 1): if i == 5 and j == 5: self.assertTrue(testgrid.lookupEnnemies(i, j)) else: self.assertFalse(testgrid.lookupEnnemies(i, j))
def main(): # initialize game engine pygame.init() # Setup main window window_width = 400 window_height = 300 screen = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("YARL!") # Initialize Hero floor = Floor("floors/dungeon.json") hero = Hero() print(hero.name + " just entered " + floor.name) hero.loc_x = floor.spawn[0] hero.loc_y = floor.spawn[1] set_position(hero, floor) # Initialize ennemies nb_ennemies = 1 ennemies = [] for ennemy in range(nb_ennemies): ennemies.append(Ennemy()) loc = get_ennemy_spawn(floor, ennemies, hero) ennemies[-1].loc_x = loc[0] ennemies[-1].loc_y = loc[1] set_position(ennemies[-1], floor) # Example of event handling while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: next_loc = [hero.loc_x, hero.loc_y] if event.key == pygame.K_LEFT: next_loc[0] -= 1 if event.key == pygame.K_RIGHT: next_loc[0] += 1 if event.key == pygame.K_UP: next_loc[1] -= 1 if event.key == pygame.K_DOWN: next_loc[1] += 1 set_loc(floor, hero, ennemies, next_loc) set_position(hero, floor) # Get all items to blit blits = floor.get_blits() blits += ui.get_blits() blits += hero.get_blits() for ennemy in ennemies: blits += ennemy.get_blits() # Blit and update display at every frame for item in blits: screen.blit(item[0], item[1]) pygame.display.update()
def generateEnnemies(self): En = [] for dic in self._waves: for key in dic.keys(): nbToCreate = dic[key] while nbToCreate > 0: En.append( Ennemy( 100, self.sponePoint[0] * int(self.caseW) + int(self.caseW / 2), self.sponePoint[1] * int(self.caseH) + int(self.caseH / 2), int(key), (self.caseW, self.caseH))) nbToCreate -= 1 self.ennemies = En
def __init__(self, obstacles, nn, temperature=1 / 60, display=False, isTest=False, interval=[1 / 20, 1 / 60]): self.environment = [[0 for j in range(25)] for i in range(25)] #0=empty, 1=obstacle, 2= food (ennemy and agent stored separetely) for (x, y) in obstacles: self.environment[x][y] = 1 self.ennemies = [ Ennemy(6, 6, self), Ennemy(12, 2, self), Ennemy(12, 6, self), Ennemy(18, 6, self) ] self.agent = agt.Agent(12, 18, 40) for i in range(15): x = random.randint(0, 24) y = random.randint(0, 24) while not self.is_empty(x, y): x = random.randint(0, 24) y = random.randint(0, 24) self.environment[x][y] = 2 self.energy = 40 self.end = False self.nn = nn # the neural network used for the learning self.gamma = 0.9 # the parameters for the learning self.Temp = temperature # The temperature for the stochastic action selector if isTest: self.Temp = 0 self.var_T = interval # interval that define the max and min value the temperature could take during learning self.Ulist = [ ] # list of Utility for all the actions at each state use for the learning self.totalFood = 0 #food eaten during the simulation self.count_without_food = 0 # a count to compute when the robot is stuck in a area without getting food self.grille = None self.can = None self.agentText = None self.PAS = None self.can_life = None self.life = None self.ennemyText = None self.killed = False self.dead = False self.action_proba = [] # the list of probabilities for each action self.memory = [ ] # the list that will record previous experiments for the replay buffer() self.lessons = [] # lessons for the replay buffer that will obtain self.display = display if self.display: self.initiate_simulation()