在运行以下python代码时遇到一个问题,那就是 environment = Environment(None, bounds) #**attention!**
这个语句放到 for 的双重循环之外时,一直得不到想要的结果,出现的问题是 Polygon([(150, 200-n*5), (150, 250), (200, 250), (200, 200-n*5)])
里面的 n 起作用,而它的下一条语句 Polygon([(150, 0), (150, 170-n*5), (200, 170-n*5), (200, 0)])
里面的 n 却不起作用,这让我百思不得其解。
// A code block
class Environment:
def __init__(self, yaml_file=None, bounds=None):
self.yaml_file = yaml_file
self.obstacles = []
# self.obstacles_map = {}
self.bounds = bounds
if yaml_file:
self.load_from_yaml_file(yaml_file)
def bounds(self):
return self.bounds
def add_obstacles(self, obstacles):
self.obstacles = self.obstacles + obstacles
if __name__ == '__main__':
bounds = (0, 0, 250, 250)
maxIter = 5000
start = (215, 215)
goal = (35, 35)
plot_planning_process = True
count=0
for m in range(30):
for n in range(30):
obstacleSet = [Polygon([(50, 80+m*5), (50, 250), (100, 250), (100, 80+m*5)]),
Polygon([(50, 0), (50, 50 + m * 5), (100, 50 + m * 5), (100, 0)]),
Polygon([(10, 210), (10, 250), (30, 250), (30, 210)]),
Polygon([(0, 150), (0, 180), (20, 180), (20, 150)]),
Polygon([(10, 70), (10, 120), (25, 120), (25, 70)]),
Polygon([(115, 200), (115, 220), (135, 220), (135, 200)]),
Polygon([(115, 115), (115, 135), (135, 135), (135, 115)]),
Polygon([(115, 50), (115, 70), (135, 70), (135, 50)]),
Polygon([(220, 180), (220, 190), (250, 190), (250, 180)]),
Polygon([(225, 130), (225, 160), (245, 160), (245, 130)]),
Polygon([(230, 0), (230, 50), (250, 50), (250, 0)]),
Polygon([(150, 200-n*5), (150, 250), (200, 250), (200, 200-n*5)]),
Polygon([(150, 0), (150, 170-n*5), (200, 170-n*5), (200, 0)])]
environment = Environment(None, bounds) #**attention!**
environment.add_obstacles(obstacleSet)
count=count+1
# Set seeds
seed = 2012
np.random.seed(seed)
cur_time = time.time()
bitStar = BITStar(environment, start, goal, bounds, maxIter=maxIter, plot_flag=plot_planning_process)
time_and_cost_set = bitStar.plan(pathLengthLimit, count)
print("The time is:", time.time() - cur_time)
后来我将 environment = Environment(None, bounds) #**attention!**
这个语句放到 for 的双重循环之内,问题瞬间解决,双重循环里所有的 n 都能起作用。这种现象的原因是每次循环都需要 environment = Environment(None, bounds) #**attention!**
这个语句对 environment 进行清零和重置。