def initalize(args): # Build the resources/lookup tables that the game needs r = {} with open('resources/items.json') as f: r['items'] = json.load(f) with open('resources/equipment.json') as f: r['equipment'] = json.load(f) with open('resources/story.json') as f: r['story'] = json.load(f) # Get the map fn = f'saves/{args.mapSave}.json' if args.mapSave and os.path.isfile(fn): with open(fn) as f: m = Map(json.load(f), r) print(f'Map loaded from {fn}') else: with open('saves/map.json') as f: m = Map(json.load(f), r) startingRoom = 'start' # Get the player fn = f'saves/{args.playerSave}.json' if args.playerSave and os.path.isfile(fn): # Read in the JSON data with open(fn) as f: pJSON = json.load(f) # Create a new player with the data p = Player('temp', 'temp') p.toPlayer(pJSON, r) startingRoom = pJSON['location'] print(f'Player loaded from {fn}') else: pDetails = introSequence() p = Player(pDetails[0], pDetails[1]) # Get the achievements fn = f'saves/{args.achievementSave}.json' if args.achievementSave and os.path.isfile(fn): with open(fn) as f: aJSON = json.load(f) print(f'Achievement loaded from {fn}') else: with open('saves/achievements.json') as f: aJSON = json.load(f) a = Achievements(aJSON) return GameEngine(m, p, a, r, startingRoom)
def load_map(self, map_name, goto=None): self.map = Map(camera=self, directory=map_name) if goto: self.player.teleport(x=goto[0], y=goto[1]) elif self.map.starting_location: self.player.teleport(x=self.map.starting_location[0], y=self.map.starting_location[1]) self.cam_offset_x = self.player.sprite_rect.left self.cam_offset_y = self.player.sprite_rect.top
def __init__(self, stdscr, objects): self.map = Map(stdscr, objects) self.msgscr = stdscr # main messages self.sttscr = stdscr # show status self.errscr = stdscr # show errors self.inpscr = stdscr # get inputs self.invscr = stdscr # inventory overview self.itmscr = stdscr # item description for x in range(amountGold): self.spawn('Gold') for x in range(amountMonster): monster = random.choice(enemy_keys) self.spawn(monster) for x in range(amountWall): self.spawn('Wall') for x in range(amountFood): self.spawn('Food') for x in range(amountItem): self.spawn('Map item') self.current_actor = self.map.objects.lists.get('Hero')[0]
def wrapperFunction(): if not os.path.exists('./Results/{}.csv'.format(func.__name__)): with open('./Results/{}.csv'.format(func.__name__), 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerow( ('Rides', 'Bus', 'Scenarios', 'Objective Value', 'Savings', 'Time', 'Gap')) for scenario in scenarios: for rides, bus in tests: mappy = Map(rides, seed=200) try: r = func(mappy, bus, scenario) except: continue with open('./Results/{}.csv'.format(func.__name__), 'a', newline='') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerow((rides, bus, scenario, r['Obj'], r.get('Savings', None), r['Time'], r.get('Gap', None))) return True
def getGameEngine(): # Build the resources/lookup tables that the game needs r = {} with open('resources/items.json') as f: r['items'] = json.load(f) with open('resources/equipment.json') as f: r['equipment'] = json.load(f) with open('resources/story.json') as f: r['story'] = json.load(f) # Get the map with open('test/testMap.json') as f: m = Map(json.load(f), r) # Get the player with open('test/testPlayer.json') as f: pJSON = json.load(f) p = Player('temp', 'temp') p.toPlayer(pJSON, r) # Get the achievements with open('test/testAchievement.json') as f: aJSON = json.load(f) a = Achievements(aJSON) return GameEngine(m, p, a, r, 'room 1')
def setUp(self): self.map = Map()
def clear_map(self): self.map = Map(60, 5, zoom=4) html = self.map.get_html() self.setHtml(html)
from src.Map import Map import cv2 m = Map("../data/map/obstacles-5.txt", "../data/goal/goal-2.txt") m.fill() cv2.imshow("map", m.map) cv2.waitKey(0)
def get_map(self, nodes: int, map_size: (int, int)) -> Map: self._map = Map(map_size) self._init_map(nodes if nodes <= MAX_NODES else MAX_NODES) return self._map
def main(program_iter): step_dist, bidirectional, debug, map_file, goal_file, bias = parse_args() # init vars expansions = 0 m = Map("data/map/" + str(map_file), "data/goal/" + str(goal_file)) tree_maps = [TreeMap(), TreeMap()] if bidirectional else [TreeMap()] start = time.time() # add start position as root node assert m.start is not None and m.goal is not None tree_maps[0].add_node(m.start) if bidirectional: tree_maps[1].add_node(m.goal) # continue until the goal is added to the tree iteration = 0 goal_coords = list() # coordinates of the goal points of each tree (1 if unidirectional, 2 if bidirectional) while len(goal_coords) == 0: # grow trees for i in range(len(tree_maps)): # set bias if necessary bias_pos = None if bias and iteration % 20 == 0: bias_pos = m.goal if i == 0 else m.start if grow_tree(iteration, m, tree_maps[i], step_dist, TREE_COLORS[i], bias_pos): expansions += 1 iteration += 1 # if multiple trees, check if they are in range of connecting to one another without a collision if bidirectional: # endpoints are equal if tree_maps[1].find(tree_maps[0].last_data) or tree_maps[0].find(tree_maps[1].last_data): goal_coords = [tree_maps[0].last_data, tree_maps[1].last_data] # Endpoints are in range for i in range(len(tree_maps)): next_i = (i+1) % len(tree_maps) last_point_this_tree = tree_maps[i].last_data close_point_other_tree = tree_maps[next_i].point_in_range(last_point_this_tree, step_dist) # Update the goal coordinates if there is no collision when connetcing the points between the 2 trees if close_point_other_tree is not None and not m.is_line_collision(last_point_this_tree, close_point_other_tree): goal_coords = [None, None] goal_coords[i] = last_point_this_tree goal_coords[next_i] = close_point_other_tree cv2.line(m.map, goal_coords[0], goal_coords[1], TREE_COLORS[0]) # Always check if last point added is close to the goal if (len(goal_coords) == 0 and points_in_range(tree_maps[0].last_data, m.goal, step_dist) and not m.is_line_collision(tree_maps[0].last_data, m.goal)): goal_coords.append(tree_maps[0].last_data) cv2.line(m.map, goal_coords[0], m.goal, TREE_COLORS[0]) # Show image for debugging if debug: cv2.imshow("Map", m.map) key = cv2.waitKey(0 if STEP else 1) if ord("q") == key: cv2.destroyAllWindows() sys.exit() # create final path, for unidirectional or bidirectional trees full_path = list() for i in range(len(goal_coords)): goal_node = tree_maps[i].find(goal_coords[i]) path = tree_maps[i].path(goal_node) full_path.extend(list(reversed(path)) if i == 1 else path) # reverse the path if its from the seconds tree # add goal node if not already there (b/c of in range stopping condition checks) if full_path[-1] != m.goal: full_path.append(m.goal) display_path(full_path, m) # Show completed map if SAVE_MODE: filename = "results/map-2/step" + str(step_dist) if bias: filename += "_bias" if bidirectional: filename += "_bidirec" cv2.imwrite(filename + "_trial" + str(program_iter) + ".png", m.map) print "Finished in: " + str(round(time.time() - start)) + "s" print "Expansions: " + str(expansions) if not SAVE_MODE: cv2.imshow("Map", m.map) cv2.waitKey(0)
from src.Map import Map from src.Tabu import Tabu from src.L_shaped import MasterProblem as mp rides = 3 bus = 2 scenarios = 10 MIPGap = 0.001 TimeLimit = 18000 probability = [0.7, 0.1, 0.15, 0.05] mappy = Map(rides, seed=200) tabu = Tabu(mappy, bus, scenarios, probability, tabu_iterations=800, tabu_status=8, init_pool=20, rtoptm=5, subset=20, tsp=True, MIP=False) tabu.tabuheuristic() print(tabu.best) # tabu.al = {s: tabu.model.submodel[s].sim.alpha for s in tabu.model.submodel.keys()} # sol = tabu.initialSolution() # print(sol)