def mutation(m, mutationP, mutationFunction, mutationConfig, validate): def trivalMutation(m, mutationP, validate): mutationMatrix = np.random.rand(m.wall.shape[0], m.wall.shape[1]) < mutationP return m.wall ^ mutationMatrix if isinstance(m, frame.maze): newMazeList = [] count = 0 if m.teleported: newMazeList.append(m.teleported) count = count + 1 while count < self.size: if mutationFunction is None: wall = trivalMutation(m, mutationP, validate) else: #TODO: other mutationFunction pass newMaze = frame.maze(m.rows, m.cols, m.p, m.rootNum) newMaze.build(initFunction = frame.setWall, initConfig = {'wall': wall}) if validate and not test.valid(newMaze): continue newMazeList.append(newMaze) count = count + 1 return newMazeList else: print('E: localSearch.neighbor.__call__(), not a maze input') exit()
def buildUp(size=20, p=0.2, initFunction=None, randomPosition=False): #int rows in [2 : inf]: maze width and height #float p in [0 : 1]: probablity of a block becomes a wall #function initFunction: init walls, None default trivalInit() #bool randomPosition: True: randomlize start and goal position; False: start at upper left and goal at lower right m = frame.maze(rows=size, cols=size, p=p) m.build() return m
def buildUp(size=10, p=0.2, initFunction=None, randomPosition=False, force=False): ##int size in [2 : inf]: maze width and height #float p in [0 : 1]: probablity of a block becomes a wall #function initFunction: init walls, None default trivalInit() #bool randomPosition: True: randomlize start and goal position; False: start at upper left and goal at lower right #bool force: True: force to rebuild maze; False: if maze.isBuilt immediately return original maze m = frame.maze(rows=size, cols=size, p=p) m.build(initFunction=initFunction, randomPosition=randomPosition, force=force) return m
def buildUp(mazeSize, mazeWallRate): m = frame.maze(rows=mazeSize, cols=mazeSize, p=mazeWallRate) m.build(initFunction=None, randomPosition=False, force=False) return m
newMazeList.append(newMaze) count = count + 1 return newMazeList else: print('E: localSearch.neighbor.__call__(), not a maze input') exit() if isinstance(maze, (list, tuple)): neighborList = [] for m in maze: neighborList.extend(mutation(m, self.mutationP, self.mutationFunction, self.mutationConfig, validate)) return neighborList else: return mutation(maze, self.mutationP, self.mutationFunction, self.mutationConfig, validate) if __name__ == '__main__': a = frame.maze(rootNum = 1) a.build() b = frame.maze(rootNum = 2) b.build() sf = BDAStar sc = {'LIFO': True, 'distFunction' : manhattanDist} nb = neighbor(size = 2, mutationP = 0.02) newMaze = nb([a,b], validate = True) newMaze[1].score = 10000 of = objectiveFunction([1,1,1], sf, sc, deRandom = 2) print(newMaze) for m in newMaze: print(m.rootNum) print(of(newMaze)) print(BDAStar(a, **sc))