def build_corridor_geometry(self): """ Create corridors from the already identified pairs od endpoints """ ci = 0 for c in self.connections: corridor = Corridor(c, ci, self.end_points[c[0]], self.end_points[c[1]]) corridor.make_geometry(self.corridor_width) self.corridors[ci] = corridor ci = ci + 1
def make_corridor(self, start, direction, distance): """ Step a number of squares forward in the current direction""" corridor = Corridor(start, direction, distance) # Check if corridor ends outside the boundary if not self.canvas.contains(corridor.end): return # Check if corridor intersect any other corridor for other_corridor in self.corridors: intersection = corridor.intersects(other_corridor) if intersection != (None, None): #print("%s collides with %s"%(corridor, other_corridor)) return # If close, a check should be made for secret doors # Save and update self.corridors.append(corridor) self.plan_construction(self.periodic, corridor.end, direction)
def fig_13_1(): env = Corridor() alg = Reinforce(env, None, FIG_13_1_G, FIG_13_1_THE_DIM, pi_gen_corr, logpi_wrap_corr(env, feat_corr), the_0=None) benchmark(alg, 'Figure 13.1', 'fig13.1')
def __init__(self, map_): self.hor_tunnel_exists = False self.ver_tunnel_exists = False self.map_ = map_ self.pathways = self.create_pathways_list() self.crossroads = self.create_crossroads_list(self.pathways) self.corridors = self.create_corridors(self.pathways, self.crossroads) self.corridors = [Corridor(corr) for corr in self.corridors] self.corr_adjacencies = self.create_corridor_adjacencies( self.corridors) self.ghosts_den = [] self.average_corridor_cost = self.calc_average_corridor_cost()
def createSquare(char): """ fonction auxilliaire pour créer chaque case du labyrinthe à modifier si on veut ajouter des nouveaux types de cases """ if char == 'O': return Wall() elif char == '.': return Door() elif char == 'U': return Exit() elif char == 'T': return Trap() else: # Tout autre caractère (y compris le X du robot) est un couloir # Le robot commence forcément dans un couloir return Corridor()
def fig_13_2(): env = Corridor() def vhat(s, w): return w[0] def nab_vhat(s, w): return np.ones(1) fig, ax = plt.subplots() fig.suptitle('Figure 13.2', fontsize=BIG_FONT) fig.set_size_inches(20, 14) xticks, yticks = np.linspace(0, 1000, 6), np.linspace(-90, -10, 9) def short_str(x): return str(x)[:3] xnames, ynames = map(short_str, xticks), map(short_str, yticks) alg1 = Reinforce(env, None, FIG_13_2_G, FIG_13_2_THE_DIM, pi_gen_corr, logpi_wrap_corr(env, feat_corr)) alg2 = ReinforceBaseline(env, FIG_13_2_ALP_BAS_W, FIG_13_2_ALP_BAS_T, FIG_13_2_G, FIG_13_2_THE_DIM, pi_gen_corr, logpi_wrap_corr(env, feat_corr), vhat, nab_vhat, FIG_13_2_W_DIM) run(ax, alg2, [FIG_13_2_ALP_BAS_T], FIG_13_2_N_EP, FIG_13_2_N_RUNS, dash=False) run(ax, alg1, [FIG_13_2_ALP], FIG_13_2_N_EP, FIG_13_2_N_RUNS) plot_figure(ax, '', xticks, xnames, 'Episode', list(yticks) + [0], ynames, (f'Total\nReward\non episode\n(Averaged over\n' + f'{FIG_13_1_N_RUNS} runs)'), font=MED_FONT, labelpad=40, loc='upper right') save_plot('fig13.2', dpi=100) plt.show()
class Map(object): scenes = { 'cementery': Cementery(), 'corridor': Corridor(), 'death': Death(), 'maze': Maze(), 'tomb': Tomb(), 'finished': Finished(), } def __init__(self, start_scene): self.start_scene = start_scene def next_scene(self, scene_name): val = Map.scenes.get(scene_name) return val def opening_scene(self): return self.next_scene(self.start_scene)
def main(self): clock = pygame.time.Clock() self.corridor = Corridor(self.game, length=3000 + 3000 * self.game.level) self.corridor.x = 0 self.player = self.game.player self.player.movement_enabled = True self.player.in_bus = False self.player.cash_this_level = 0 self.player.dead = False self.player.hp = 3 self.player.sprite.start_animation("Running") self.player.y = self.corridor.floor_y() - 48 self.player.y_velocity = -600 self.game.enemies = [] self.game.pickups = [] self.game.particles = [] self.since_enemy = 0 self.game.scroll_speed = 0 self.flash = pygame.Surface(c.WINDOW_SIZE) self.flash.fill(c.WHITE) self.flash.set_alpha(0) self.circle_surf = pygame.Surface(c.WINDOW_SIZE) self.circle_surf.fill(c.BLACK) self.bone = pygame.image.load("images/bone.png") self.bone = pygame.transform.scale( self.bone, (self.bone.get_width() * 3 // 2, self.bone.get_height() * 3 // 2)) self.circle_radius = 0 self.cash_disp = 0 self.age = 0 self.since_death = 0 self.train_played = False self.train_sound = pygame.mixer.Sound("sounds/train.wav") self.train_sound.set_volume(1.0) self.game.battle_music.play(-1) while True: dt = clock.tick(60) / 1000 events = self.game.update_globals() self.game.update_slowdown(dt, events) dt *= self.game.slowdown if dt > 1 / 30: dt = 1 / 30 self.age += dt if self.age > 1.7 and self.train_played == False: self.train_sound.play() self.train_played = True self.flash.set_alpha(self.game.flash_alpha) self.game.flash_alpha = max(0, self.game.flash_alpha - 1000 * dt) if self.game.player.dead: self.since_death += dt circle_speed = 1200 if self.scene_over(): self.game.battle_music.fadeout(800) if self.circle_radius <= 0: break self.circle_radius -= circle_speed * dt if self.circle_radius <= 0: self.circle_radius = 0 self.player.movement_enabled = False self.player.in_bus = True dx = self.corridor.end_subway.x + 475 - self.player.x dy = c.WINDOW_HEIGHT * 0.7 - self.player.y if not self.player.dead: self.player.x += dx * dt * 4 self.player.y += dy * dt * 4 else: self.circle_radius += circle_speed * dt if self.circle_radius >= c.WINDOW_WIDTH: self.circle_radius = c.WINDOW_WIDTH self.corridor.update(dt, events) self.player.update(dt, events) self.game.update_screenshake(dt, events) self.update_scrolling(dt, events) self.update_enemies(dt, events) self.update_pickups(dt, events) self.update_particles(dt, events) self.game.screen.fill((120, 120, 120)) self.corridor.draw(self.game.screen) for particle in self.game.particles: particle.draw(self.game.screen) for enemy in self.game.enemies: enemy.draw(self.game.screen) for pickup in self.game.pickups: pickup.draw(self.game.screen) self.player.draw(self.game.screen) self.draw_score(self.game.screen, dt) if self.flash.get_alpha() > 0: self.game.screen.blit(self.flash, (0, 0)) self.draw_circle(self.game.screen) pygame.display.flip()
class ConnectionScene(Scene): def next_scene(self): if self.game.player.dead: from lose_screen import LoseScreen return LoseScreen(self.game) return OnBusScene(self.game) def main(self): clock = pygame.time.Clock() self.corridor = Corridor(self.game, length=3000 + 3000 * self.game.level) self.corridor.x = 0 self.player = self.game.player self.player.movement_enabled = True self.player.in_bus = False self.player.cash_this_level = 0 self.player.dead = False self.player.hp = 3 self.player.sprite.start_animation("Running") self.player.y = self.corridor.floor_y() - 48 self.player.y_velocity = -600 self.game.enemies = [] self.game.pickups = [] self.game.particles = [] self.since_enemy = 0 self.game.scroll_speed = 0 self.flash = pygame.Surface(c.WINDOW_SIZE) self.flash.fill(c.WHITE) self.flash.set_alpha(0) self.circle_surf = pygame.Surface(c.WINDOW_SIZE) self.circle_surf.fill(c.BLACK) self.bone = pygame.image.load("images/bone.png") self.bone = pygame.transform.scale( self.bone, (self.bone.get_width() * 3 // 2, self.bone.get_height() * 3 // 2)) self.circle_radius = 0 self.cash_disp = 0 self.age = 0 self.since_death = 0 self.train_played = False self.train_sound = pygame.mixer.Sound("sounds/train.wav") self.train_sound.set_volume(1.0) self.game.battle_music.play(-1) while True: dt = clock.tick(60) / 1000 events = self.game.update_globals() self.game.update_slowdown(dt, events) dt *= self.game.slowdown if dt > 1 / 30: dt = 1 / 30 self.age += dt if self.age > 1.7 and self.train_played == False: self.train_sound.play() self.train_played = True self.flash.set_alpha(self.game.flash_alpha) self.game.flash_alpha = max(0, self.game.flash_alpha - 1000 * dt) if self.game.player.dead: self.since_death += dt circle_speed = 1200 if self.scene_over(): self.game.battle_music.fadeout(800) if self.circle_radius <= 0: break self.circle_radius -= circle_speed * dt if self.circle_radius <= 0: self.circle_radius = 0 self.player.movement_enabled = False self.player.in_bus = True dx = self.corridor.end_subway.x + 475 - self.player.x dy = c.WINDOW_HEIGHT * 0.7 - self.player.y if not self.player.dead: self.player.x += dx * dt * 4 self.player.y += dy * dt * 4 else: self.circle_radius += circle_speed * dt if self.circle_radius >= c.WINDOW_WIDTH: self.circle_radius = c.WINDOW_WIDTH self.corridor.update(dt, events) self.player.update(dt, events) self.game.update_screenshake(dt, events) self.update_scrolling(dt, events) self.update_enemies(dt, events) self.update_pickups(dt, events) self.update_particles(dt, events) self.game.screen.fill((120, 120, 120)) self.corridor.draw(self.game.screen) for particle in self.game.particles: particle.draw(self.game.screen) for enemy in self.game.enemies: enemy.draw(self.game.screen) for pickup in self.game.pickups: pickup.draw(self.game.screen) self.player.draw(self.game.screen) self.draw_score(self.game.screen, dt) if self.flash.get_alpha() > 0: self.game.screen.blit(self.flash, (0, 0)) self.draw_circle(self.game.screen) pygame.display.flip() def draw_circle(self, surface): if self.circle_radius >= 600: return self.circle_surf.fill(c.BLACK) x, y = int(self.game.player.x), int(self.game.player.y) pygame.draw.circle(self.circle_surf, c.WHITE, (x, y), int(self.circle_radius)) self.game.screen.blit(self.circle_surf, (0, 0), special_flags=pygame.BLEND_MULT) def scene_over(self): if self.corridor.end_subway.x + 430 + 80 >= self.player.x >= self.corridor.end_subway.x + 430 and self.player.y >= c.WINDOW_HEIGHT // 2: return True if self.since_death > 1: return True return False def update_scrolling(self, dt, events): if self.game.player.dead: return if self.game.scroll_speed < 350: self.game.scroll_speed += 200 * dt if self.game.scroll_speed >= 350: self.game.scroll_speed = 350 def update_pickups(self, dt, events): for item in self.game.pickups[::-1]: item.update(dt, events) if c.distance_between_points( item.x, item.y, self.player.x, self.player.y) < self.player.width // 2: item.get() self.game.pickups.remove(item) def update_particles(self, dt, events): for particle in self.game.particles[::-1]: particle.update(dt, events) if particle.dead: self.game.particles.remove(particle) def update_enemies(self, dt, events): for enemy in self.game.enemies: enemy.update(dt, events) self.update_spawning(dt, events) for enemy in self.game.enemies[::-1]: if enemy.dead and enemy.remove_on_death: self.game.enemies.remove(enemy) elif enemy.x < -500 or enemy.x > c.WINDOW_WIDTH + 500 or enemy.y < -500 or enemy.y > c.WINDOW_HEIGHT + 500: self.game.enemies.remove(enemy) def spawn_dasher(self, length=1): new_enemy = Dasher(self.game, 0, 0) y_min = c.WINDOW_HEIGHT // 2 - self.corridor.width // 2 + new_enemy.radius y_max = y_min + self.corridor.width - new_enemy.radius * 2 y = random.random() * (y_max - y_min) + y_min x = c.WINDOW_WIDTH + new_enemy.radius new_enemy.y = y new_enemy.x = x self.game.enemies.append(new_enemy) def spawn_crawler_wave(self, length=3): spacing = 72 for i in range(length): new_enemy = Crawler(self.game, c.WINDOW_WIDTH + 50 + spacing * i, 0) self.game.enemies.append(new_enemy) def spawn_crawler_ceiling_wave(self, length=3): spacing = 72 for i in range(length): new_enemy = CrawlerCeiling(self.game, c.WINDOW_WIDTH + 50 + spacing * i, 0) self.game.enemies.append(new_enemy) def spawn_blocker(self, num=None): new_enemy = Blocker(self.game) self.game.enemies.append(new_enemy) def update_spawning(self, dt, events): if self.game.player.dead: return self.since_enemy += dt period = 0.2 + 6 / (self.game.level + 2) if self.since_enemy > period and self.game.scroll_speed > 200: self.since_enemy = 0 if self.game.level == 1: choices = [self.spawn_crawler_wave] num = random.choice([1, 2]) random.choice(choices)(num) elif self.game.level == 2: choices = [ self.spawn_crawler_wave, self.spawn_crawler_ceiling_wave ] num = random.choice([1, 2, 2, 3]) random.choice(choices)(num) elif self.game.level == 3: choices = [ self.spawn_crawler_wave, self.spawn_crawler_ceiling_wave, self.spawn_blocker ] num = random.choice([2, 3]) random.choice(choices)(num) elif self.game.level == 4: choices = [ self.spawn_crawler_wave, self.spawn_crawler_ceiling_wave, self.spawn_dasher, self.spawn_blocker ] num = random.choice([2, 3, 3, 3, 4]) random.choice(choices)(num) else: choices = [ self.spawn_crawler_wave, self.spawn_crawler_ceiling_wave, self.spawn_dasher, self.spawn_blocker ] num = random.choice([3, 4, 4]) random.choice(choices)(num) def draw_score(self, surface, dt): dc = self.game.player.cash_this_level - self.cash_disp self.cash_disp = min(self.game.player.cash_this_level, self.cash_disp + dc * 10 * dt) text = f"${float(round(self.cash_disp, 1))}0" color = 180 + 75 * min(dc / 5, 1) scale = 1 + 1.2 * min(dc / 30, 1) surf = self.game.score_font.render(text, 1, (color, color, color)) surf = pygame.transform.scale( surf, (int(surf.get_width() * scale), int(surf.get_height() * scale))) score_text = f"SAVINGS: ${float(self.game.score)}0" score_surf = self.game.ledger_font.render(score_text, 1, (180, 180, 180)) y = 40 surface.blit(surf, (c.WINDOW_WIDTH // 2 - surf.get_width() // 2, y - surf.get_height() // 2)) surface.blit(score_surf, (c.WINDOW_WIDTH // 2 - score_surf.get_width() // 2, y + surf.get_height() * 0.3)) level = f"LEVEL {self.game.level}" if self.age < 5: surf = self.game.ledger_font.render(level, 1, (180, 180, 180)) surf2 = pygame.Surface(surf.get_size()) surf2.fill(c.BLACK) surf2.blit(surf, (0, 0)) surf2.set_colorkey(c.BLACK) if self.age > 3: surf2.set_alpha((5 - self.age) / 2 * 255) surface.blit(surf2, (c.WINDOW_WIDTH // 2 - surf2.get_width() // 2, c.WINDOW_HEIGHT - 50)) bone = self.bone.copy() color = min(180 * self.player.since_hit, 180) color2 = 255 - min(75, 75 * self.player.since_hit) bone.fill((color2, color, color), special_flags=pygame.BLEND_MULT) surface.blit(bone, (35, 25)) surf = self.game.score_font.render(f"x{max(self.game.player.hp, 0)}", 1, (color2, color, color)) surface.blit(surf, (65, 30))
import argparse import os from corridor import Corridor import matplotlib.pyplot as plt ENV_DICT = {'corridor': Corridor()} def play(env): def refresh(): os.system('cls' if os.name == 'nt' else 'clear') print(env) while True: env.reset() done = False v = [] while not done: key = '' while key not in env.keys: refresh() key = input("press key\n$>") if key == "exit()": exit() for _ in range(1): _, _, done, _ = env.step_via_key(key) v.append(env.state) again = input("episode done, continue? (Y / n)") if again == 'n': break
from corridor import Corridor class InterPoints(): """InterPoints class introduces a list of chosen points: - that passage must pass between entrance and exit - interPoints list is dynamically changing it's size - if user add point(s) then new passage is generated""" def __init__(self): self.points = [] def add_point(self, field: Field): if field in self.points: self.delete_point(field) else: self.points.append(field) def delete_point(self, field: Field): self.points.remove(field) # Test purposes - delete later: if __name__ == '__main__': ip = InterPoints() c1 = Corridor(Point(5, 2)) c2 = Corridor(Point(2, 1)) ip.add_point(c1) ip.add_point(c2) ip.add_point(c1) #ip.add_point(Corridor(Point(2, 5))) for point in ip.points: print(point.coordinates.get_x(), point.coordinates.get_y())
def testUpdateDomain(self): corrA = Corridor([(2,4), (1,4), (1,3), (1,2), (1,1), (2,1), (3,1), (4,1), (5,1), (6,1), (7,1), (8,1)]) corrB = Corridor([(2,4), (3,4), (4,4), (4,5), (4,6), (4,7)]) corrC = Corridor([(2,4), (2,5), (2,6), (2,7), (1,7), (1,8), (1,9), (1,10), (2,10), (3,10), (4,10)]) corrD = Corridor([(4,7), (4,8), (4,9), (4,10)]) corrE = Corridor([(4,7), (5,7), (6,7)]) corrF = Corridor([(8,1), (8,2), (8,3), (8,4), (7,4), (6,4), (6,5), (6,6), (6,7)]) initial_corr = corrA initial_pos = (1,1) print("initial_corr = " + str(initial_corr)) print("initial_pos = " + str(initial_pos)) initial = Corridor([initial_pos]) sub_icorr0, sub_icorr1 = initial_corr.sub_corridors(initial_pos) print("initial = " + str(initial)) print("sub_icorr0 = " + str(sub_icorr0)) print("sub_icorr1 = " + str(sub_icorr1)) print("#######################################################################") goal_corr = corrC goal_pos = (2,10) print("goal_corr = " + str(goal_corr)) print("goal_pos = " + str(goal_pos)) goal = Corridor([goal_pos]) sub_gcorr0, sub_gcorr1 = goal_corr.sub_corridors(goal_pos) print("goal = " + str(goal)) print("sub_gcorr0 = " + str(sub_gcorr0)) print("sub_gcorr1 = " + str(sub_gcorr1)) print("\n\n\#######################################################################") print("#######################################################################") print("#######################################################################\n\n") domain = [ (corrA, corrB), (corrA, corrC), (corrA, corrF),\ (corrB, corrA), (corrB, corrC), (corrB, corrD), (corrB, corrE),\ (corrC, corrA), (corrC, corrB), (corrC, corrD),\ (corrD, corrB), (corrD, corrC), (corrD, corrE),\ (corrE, corrB), (corrE, corrD), (corrE, corrF),\ (corrF, corrA), (corrF, corrD) ] length = len(domain) print("domain = " + str(domain)) print("\n\n#######################################################################") print("#######################################################################") print("#######################################################################\n\n") #def update_domain(self, corridor, sub_corr, sub_corr0, sub_corr1): corridor = initial_corr sub_corr0 = sub_icorr0 sub_corr1 = sub_icorr1 domain += [(initial, sub_corr0), (initial, sub_corr1)] for (corrA, corrB) in domain: if corridor == corrA: domain.remove((corrA, corrB)) print("pop = " + str((corrA, corrB))) if any(e in sub_corr0.ends for e in corrB.ends): domain += [(sub_corr0, corrB)] print([(sub_corr0, corrB)]) elif any(e in sub_corr1.ends for e in corrB.ends): domain += [(sub_corr1, corrB)] print([(sub_corr1, corrB)]) elif corridor == corrB: domain.remove((corrA, corrB)) print("pop = " + str((corrA, corrB))) if any(e in sub_corr0.ends for e in corrA.ends): domain += [(sub_corr0, corrA)] print([(sub_corr0, corrA)]) elif any(e in sub_corr1.ends for e in corrA.ends): domain += [(sub_corr1, corrA)] print([(sub_corr1, corrA)]) print("\n") #def update_domain(self, corridor, sub_corr, sub_corr0, sub_corr1): corridor = goal_corr sub_corr0 = sub_gcorr0 sub_corr1 = sub_gcorr1 domain += [(goal, sub_corr0), (goal, sub_corr1)] for (corrA, corrB) in domain: if corridor == corrA: domain.remove((corrA, corrB)) print("pop = " + str((corrA, corrB))) if any(e in sub_corr0.ends for e in corrB.ends): domain += [(sub_corr0, corrB)] print([(sub_corr0, corrB)]) elif any(e in sub_corr1.ends for e in corrB.ends): domain += [(sub_corr1, corrB)] print([(sub_corr1, corrB)]) elif corridor == corrB: domain.remove((corrA, corrB)) print("pop = " + str((corrA, corrB))) if any(e in sub_corr0.ends for e in corrA.ends): domain += [(sub_corr0, corrA)] print([(sub_corr0, corrA)]) elif any(e in sub_corr1.ends for e in corrA.ends): domain += [(sub_corr1, corrA)] print([(sub_corr1, corrA)]) print("\n") print("update domain = " + str(domain)) print("\n\n") print("length+4 == len(domain) = " + str(length+4 == len(domain)))
from tree_search import SearchTree, SearchProblem from corridor import Corridor from pathways import Pathways #import math print("PACMAN TEST TOOL\n") ################################################################################ ################################################################################ ################################################################################ ################################################################################ #def testTreeSearch_1Pos(self): corrA = Corridor([(2,4), (1,4), (1,3), (1,2), (1,1), (2,1), (3,1), (4,1), (5,1), (6,1), (7,1), (8,1)]) corrB = Corridor([(2,4), (3,4), (4,4), (4,5), (4,6), (4,7)]) corrC = Corridor([(2,4), (2,5), (2,6), (2,7), (1,7), (1,8), (1,9), (1,10), (2,10), (3,10), (4,10)]) corrD = Corridor([(4,7), (4,8), (4,9), (4,10)]) corrE = Corridor([(4,7), (5,7), (6,7)]) corrF = Corridor([(8,1), (8,2), (8,3), (8,4), (7,4), (6,4), (6,5), (6,6), (6,7)]) list_corr = [corrA, corrB, corrC, corrD, corrE, corrF] adjacencies = [ (corrA, corrB), (corrA, corrC), (corrA, corrF),\ (corrB, corrC), (corrB, corrD), (corrB, corrE),\ (corrC, corrD),\ (corrD, corrE),\ (corrE, corrF) ]