def appStarted(app): # map generation app.mapRows = app.mapCols = 70 app.map = generateMap(app.mapRows, app.mapCols) # player (born at a random room.) birthRoom = random.choice(app.map.rooms) cx = birthRoom.cx cy = birthRoom.cy print(cx, cy) app.player = Player(cx, cy) app.playerSpriteCount = 0 # others app.margin = 20 app.currRow = app.mapRows // 2 app.currCol = app.mapCols // 2 app.viewDepth = 5 app.drawMiniMap = False app.timerDelay = 100 app.time_groundEffect = time.time() app.time_moveCtrl = time.time() app.lightEffect = 0 app.moveAllowed = True app.sendMissMsg = False app.time_missMsg = time.time()
def main(): # Example graph generation with dimension and probability g = map.generateMap(150, 0.2) path = aStar(g, euclideanH) bfsPath = bfs(g) map.printPath(path, g) print(" ") map.printPath(bfsPath, g) print(" ") print(len(path), len(bfsPath))
def main(): now = time() unsorted_dic = map.generateMap() print(unsorted_dic) print(time() - now) now = time() unsorted_dic = dict(unsorted_dic) print(time() - now) now = time() sorted_dic = collections.OrderedDict(sorted(unsorted_dic.items())) print(time() - now)
def main(): # Example graph generation with dimension and probability g = map.generateMap(150, 0.2) manhattanPath = aStar(g, manhattanH) euclideanPath = aStar(g, euclideanH) bfsPath = bfs(g) dfsPath = dfs(g) bidirectionalBFSPath = bidirectionalBfs(g) # Example for visualizing path for graph g map.visualize(manhattanPath, g) map.visualize(euclideanPath, g) map.visualize(bfsPath, g) map.visualize(dfsPath, g) map.visualize(bidirectionalBFSPath, g) # Part 3 calls with visualization path, graph, og_path, og_graph = GenerateHardMaze.generateHardMazePathLength( ) map.visualize(og_path, og_graph) map.visualize(path, graph) path2, graph2, og_path2, og_graph2 = GenerateHardMaze.generateHardMazeFringeSize( ) map.visualize(og_path2, og_graph2) map.visualize(path2, graph2) path3, graph3, og_path3, og_graph3 = GenerateHardMaze.generateHardMazeNumberOfNodes( ) map.visualize(og_path3, og_graph3) map.visualize(path3, graph3) # Example fire graph generation with dimension and probability fireG = map.generateFireMap(100, 0.3) # Strategies with Flammability rate q q = 0.1 result1 = fire.strategy1(fireG, q) result2 = fire.strategy2(fireG, q) map.printPath(result1[0], result1[1]) print("") map.printPath(result2[0], result2[1]) # Example for visualizing path for fire graph g where the first element # in the result tuple is the path and second element is the final graph. map.visualizeFireMap(result1[0], result1[1]) map.visualizeFireMap(result2[0], result2[1])
def generateHardMazePathLength(): #counter for the len(path) over iterations max = 0 original_path = None original_graph = None global_maze = None final_path = None #the termination condition to stop random restarting is that the path should be longer than 80 while(max < 80): #generate a solvable map while True: g = map.generateMap(20, 0.3) if(dfs(g)[0] != "Failure: No Path"): break #using aStar euclidean to get the total number of nodes visited, max fringe size, and path size to determine difficulty #length of the path before making the map harder path, nodes_visited, max_fringe_size = aStar(g, euclideanH) max_difficulty = len(path) #print("First Maze Length: ") #print(max_difficulty) #generate the hard map by giving it the existing map, the current difficulty hard_graph = generateLocalHardMaze(g, max_difficulty) #used to get new path length and check results path2, nodes_visited2, max_fringe_size2 = aStar(hard_graph, euclideanH) max_difficulty2 = len(path2) #print("Second Maze Length:") #print(max_difficulty2) #used to save largest local max if(max_difficulty2 > max): max = max_difficulty2 global_maze = hard_graph final_path = path2 original_path = path original_graph = g #display final result return final_path, global_maze, original_path, original_graph
def generateHardMazeNumberOfNodes(): #counter for the len(path) over iterations max = 0 original_path = None original_graph = None global_maze = None final_path = None #the termination condition to stop random restarting is that the nodes visited should be longer than 280 while(max < 280): #generate a solvable map while True: g = map.generateMap(20, 0.3) if(dfs(g)[0] != "Failure: No Path"): break #find initial difficulty of the graph path, nodes_visited, max_fringe_size = aStar(g, manhattanH) max_difficulty = nodes_visited #print("First: ") #print(max_difficulty) #generate the hard graph based on the graph before hard_graph = generateLocalHardMazeManhattan(g, max_difficulty) path2, nodes_visited2, max_fringe_size2 = aStar(hard_graph, manhattanH) max_difficulty2 = nodes_visited2 #print("Second:") #print(max_difficulty2) #used to save largest local max if(max_difficulty2 > max): max = max_difficulty2 global_maze = hard_graph final_path = path2 original_path = path original_graph = g #print out max fringe size return final_path, global_maze, original_path, original_graph
def generateHardMazeFringeSize(): #counter for the len(path) over iterations max = 0 original_path = None original_graph = None global_maze = None final_path = None #the termination condition to stop random restarting is that the fringe size should be longer than 80 while(max < 80): #generate a solvable map while True: g = map.generateMap(20, 0.3) if(dfs(g)[0] != "Failure: No Path"): break #find original fringe size path, fringe_size = dfs(g) #print(fringe_size) #generate hard map based on fringe size new_hard_maze = generateLocalHardMazeDFSFringe(g, fringe_size) path2, fringe_size2 = dfs(new_hard_maze) max_difficulty2 = fringe_size2 #print(fringe_size2) #used to save largest local max if(max_difficulty2 > max): max = max_difficulty2 global_maze = new_hard_maze final_path = path2 original_path = path original_graph = g #print out max fringe size return final_path, global_maze, original_path, original_graph
def genetic_algorithm(algo): dim = 15 prob = .4 count = 0 # Population will hold our valid hard mazes population = [] # While loop halts when we have 100 hard mazes in the population while count != 100: # create a random map map = generateMap(dim, prob) if (algo == "dfs"): result = dfs(map) else: temp = copy.deepcopy(map) temp[0][0] = 0 temp[len(temp) - 1][len(temp) - 1] = 0 start_time = time.time() signal.signal(signal.SIGALRM, handler) signal.alarm(1) try: result = astar(temp, 0) except IOError: result = None if (result == None): continue else: # if w ehave a valid solvable maze then add to population result.append(map) population.append(result) count = count + 1 #print("Population count "+ str(count)) count = 0 # while loop halts once we have costruced 50 valid children while count != 50: # sort the population by our heuristic and reverse so the highest score is first population = sorted(population, key=itemgetter(1), reverse=True) # dad is the best hard maze we have dad = population[0] # mom is the second best hard maze we have mom = population[1] # set up child as 2D array which is how enivroments are used in backend kid = [[0 for x in range(dim)] for y in range(dim)] map1 = dad[2] map2 = mom[2] # Thsi snippet of code perform recombination # we take the first half of dad and second half of mom and populate the child accrodingly go = 0 stop = (dim * dim) / 2 for x in range(dim): for y in range(dim): if (go < stop): kid[x][y] = map1[x][y] else: kid[x][y] = map2[x][y] go = go + 1 # We run whichever algo the user had chosen on the newly created kid maze if algo == "dfs": final = dfs(kid) else: kid[0][0] = 0 kid[len(kid) - 1][len(kid) - 1] = 0 start_time = time.time() signal.signal(signal.SIGALRM, handler) signal.alarm(1) try: final = astar(kid, 0) except IOError: final = None # if child is not solvable we get rid of dad and try again with a differnt set of mazes if (final == None): population.pop(0) # if child is valid we append the kid and pop the worst in popualtion # Later the kid will be placed in the right position during out sorting phase else: population.pop() population.append(final) count = count + 1 # we return the most fit maze or hardest maze in our population as our answer return population[0]