def data_lists(mapdata, verbose = False): """convert stacked data into list of sequences """ if mapdata.sequence_lengths is not None: if mapdata.sequence_lengths.sum() != mapdata.data.shape[0]: print('Data object does not appear to be in a valid stacked format!') return None else: print('Error! Sequences do not appear to be in a valid stacked format!') return None listmap = Map() listmap.bin_width = mapdata.bin_width listmap.boundaries = mapdata.boundaries listmap.boundaries_fs = mapdata.boundaries_fs listmap.sequence_lengths = mapdata.sequence_lengths listmap.data = [] ii = 0 for ss in mapdata.sequence_lengths: listmap.data.append(mapdata.data[ii:ii+ss]) ii = ii+ss return listmap
def __init__(self): root = Tk() canvas_height = 700 canvas_width = 720 canvas = Canvas(root, width=canvas_width, height=canvas_height) self.map = Map() self.map.draw_map(canvas) self.hero = Hero(canvas) self.hero.draw_hero(0, 0) self.hero.update_entity(self.hero.hero_down) self.skeleton = Skeleton(canvas) self.boss = Boss(canvas) self.skel_num = 3 self.coordinates = self.map.create_random_coordinates(self.skel_num + 1) self.skeleton.draw_skeleton(self.coordinates[:-1]) self.boss.draw_boss(self.coordinates[-1]) self.hud = Hud() self.hud.draw_hud(canvas, 0, 650) root.bind("<KeyPress>", self.on_key_press) canvas.pack() root.mainloop()
class Game(object): def __init__(self): root = Tk() canvas_height = 700 canvas_width = 720 canvas = Canvas(root, width=canvas_width, height=canvas_height) self.map = Map() self.map.draw_map(canvas) self.hero = Hero(canvas) self.hero.draw_hero(0, 0) self.hero.update_entity(self.hero.hero_down) self.skeleton = Skeleton(canvas) self.boss = Boss(canvas) self.skel_num = 3 self.coordinates = self.map.create_random_coordinates(self.skel_num + 1) self.skeleton.draw_skeleton(self.coordinates[:-1]) self.boss.draw_boss(self.coordinates[-1]) self.hud = Hud() self.hud.draw_hud(canvas, 0, 650) root.bind("<KeyPress>", self.on_key_press) canvas.pack() root.mainloop() def on_key_press(self, e): if (e.keysym == 'Up'): self.hero.update_entity(self.hero.hero_up) if self.map.is_wall(self.hero.x, self.hero.y - self.map.tile_size) == False: self.hero.move(0, -self.map.tile_size) elif (e.keysym == 'Down'): self.hero.update_entity(self.hero.hero_down) if self.map.is_wall(self.hero.x, self.hero.y + self.map.tile_size) == False: self.hero.move(0, +self.map.tile_size) elif (e.keysym == 'Right'): self.hero.update_entity(self.hero.hero_right) if self.map.is_wall(self.hero.x + self.map.tile_size, self.hero.y) == False: self.hero.move(+self.map.tile_size, 0) elif (e.keysym == 'Left'): self.hero.update_entity(self.hero.hero_left) if self.map.is_wall(self.hero.x - self.map.tile_size, self.hero.y) == False: self.hero.move(-self.map.tile_size, 0)
for json_ped in json_data: ped_list.append(Pedestrian(json_ped)) return ped_list def isAllReachTarget(ped_list, target_area): for ped in ped_list: if not ped.pos in target_area: return False return True def savePed(ped_list, time): log_msg = "Time " + str(time) + "----\n" for ped in ped_list: log_msg = log_msg + str(ped) + "\n" log.info(log_msg) if __name__ == "__main__": map_info = Map("data/large_basic_map.txt") ped_list = loadPedestrians("data/three_ped.json") time_tick = config.default_time_tick tick_count = 0 experiment = Experiment(map_info) while not isAllReachTarget(ped_list, map_info.getTargetArea()): if tick_count % 1 == 0: print("Tick", tick_count) savePed(ped_list, experiment.time) experiment.tick(ped_list, time_tick) tick_count = tick_count + 1 savePed(ped_list, experiment.time)
def draw_path(pos): pygame.time.delay(100) rect.x = pos[0] * 50 rect.y = pos[1] * 50 screen.fill((0, 0, 255), rect) pygame.display.update() pygame.event.pump() def reset(): screen.blit(mapsurf, (0, 0)) global map, screen, rect, surf, mapsurf, closedsurf map = Map() screen = pygame.display.set_mode((500, 500)) rect = pygame.Rect(0, 0, 50, 50) surf = pygame.Surface((50, 50)) surf.fill((0, 0, 255)) closedsurf = pygame.Surface((50, 50)) closedsurf.fill((255, 0, 0)) for x in range(20): pygame.draw.line(closedsurf, (0, 0, 0), (5 * x, 0), (0, 5 * x)) pygame.draw.line(closedsurf, (0, 0, 0), (50 - 5 * x, 0), (50, 5 * x)) pygame.draw.rect(closedsurf, (0, 0, 0), (0, 0, 50, 50), 1) mapsurf = pygame.Surface((500, 500)) mapsurf.fill((255, 255, 255)) for y, row in enumerate(map.map): for x, value in enumerate(row):
def data_split(mapdata, tr=0.3, vl=0.3, ts=0.4, randomseed = None, verbose = False): """Split mapdata into train, val, and test sets. mapdata is a Map() data object, either stacked or not. """ if randomseed is not None: np.random.seed(randomseed) # normalize tr, vl, and ts proportions: tmpsum = tr + vl + ts tr = tr / tmpsum vl = vl / tmpsum ts = ts / tmpsum trmap = Map() vlmap = Map() tsmap = Map() trmap.bin_width = mapdata.bin_width vlmap.bin_width = mapdata.bin_width tsmap.bin_width = mapdata.bin_width trmap.boundaries_fs = mapdata.boundaries_fs vlmap.boundaries_fs = mapdata.boundaries_fs tsmap.boundaries_fs = mapdata.boundaries_fs num_sequences = np.array(mapdata.sequence_lengths).shape[0] if verbose: print('Splitting {} sequences into train, validation, and test sets...'.format(num_sequences)) indices = np.random.permutation(num_sequences) tridx = indices[np.arange(0, np.floor(tr*num_sequences)).astype(int)] vlidx = indices[np.arange(np.floor(tr*num_sequences), np.floor(tr*num_sequences) + np.floor(vl*num_sequences)).astype(int)] tsidx = indices[np.arange(np.floor(tr*num_sequences) + np.floor(vl*num_sequences), num_sequences).astype(int)] if isinstance(mapdata.data,np.ndarray): lstdata = data_lists(mapdata) tmp = np.array(lstdata.data) else: tmp = np.array(mapdata.data) trmap.data = list(tmp[tridx]) vlmap.data = list(tmp[vlidx]) tsmap.data = list(tmp[tsidx]) trmap.boundaries = mapdata.boundaries[tridx] vlmap.boundaries = mapdata.boundaries[vlidx] tsmap.boundaries = mapdata.boundaries[tsidx] trmap.tridx = tridx vlmap.vlidx = vlidx tsmap.tsidx = tsidx if isinstance(mapdata.data,np.ndarray): # stack data if the original data was stacked: trtmp = data_stack(trmap, verbose = verbose) trmap.data = trtmp.data trmap.sequence_lengths = trtmp.sequence_lengths vltmp = data_stack(vlmap, verbose = verbose) vlmap.data = vltmp.data vlmap.sequence_lengths = vltmp.sequence_lengths tstmp = data_stack(tsmap, verbose = verbose) tsmap.data = tstmp.data tsmap.sequence_lengths = tstmp.sequence_lengths if verbose: print('Stacked data split into train ({:.1f} %), validation ({:.1f} %) and test ({:.1f} %) sequences.'.format(tr*100,vl*100,ts*100)) else: if verbose: print('List data split into train ({:.1f} %), validation ({:.1f} %) and test ({:.1f} %) sequences.'.format(tr*100,vl*100,ts*100)) return trmap, vlmap, tsmap
else: open_list.append(neigh) self.G_tab[neigh[0]][neigh[1]] = neigh_G self.father_tab[neigh[0]][neigh[1]] = pos if neigh in self.to_area: not_found = False end_pos = neigh break # for neigh in around_list: # while not_found: if not_found: return None route = [end_pos] now_pos = end_pos while now_pos != from_pos: now_pos = self.father_tab[now_pos[0]][now_pos[1]] route.append(now_pos) route.reverse() return route if __name__ == "__main__": map_info = Map("data/basic_map.txt") solver = AStarSolver(map_info, map_info.getTargetArea()) one = (3, 1) one_res = solver.solve(one) print(one, ": ", one_res)
from mymap import Map map1 = Map() print('map1 = ') print(map1) print() print('add (\'a\',1), (\'b\',2), (\'c\',3), (\'d\',4) to map1') map1.add('a', 1) map1.add('b', 2) map1.add('c', 3) map1.add('d', 4) print('map1 = ') print(map1) print() print('add (\'b\',10)') map1.add('b', 10) print(map1) print() print('remove (\'c\',3) from map1') map1.remove('c') print(map1) print() print('get the value corresponding to \'b\'') print(map1.valueOf('b')) print()