思路
在我们的程序模拟中,路径生成时非常重要的一环。路径保存在每一个place对象中,人通过访问所在地点中通往其他地点的路径集合决定移动的方向。为了提高人移动的真实性,每两个地点中间都有多条可供选择的路径。
为了确保路径初始化不出差错,我们进行了技术测试,在本次测试中,我们选择了hm1(home对象的标识)、hm8,以不同颜色绘制了它们到其他各个地点的路径。
代码
def draw_road():
place_info=write_json("./Place_info/city2.xlsx","./JSON_file/city2_placeinfo.json")
fig = plt.figure()
rect = [0.1, 0.1, 0.8, 0.8]
ax0=fig.add_axes(rect,label='ax0')
img_back=plt.imread("back.png")
#home:
roads_tol=place_info['hm8']
roads_work_index=roads_tol["roads_otherplace"]["work"]
for roads_tmp in roads_work_index:
road_list=[]
for road in roads_tmp:
road_list.append(map(place_info[road]["position"]))
print(road_list)
for i in range(len(road_list)-1):
ax0.scatter(road_list[i][0],road_list[i][1],color='r',alpha=0.5)
ax0.plot([road_list[i][0],road_list[i+1][0]],[road_list[i][1],road_list[i+1][1]],color='r',alpha=0.5)
roads_work_index = roads_tol["roads_otherplace"]["station"]
for roads_tmp in roads_work_index:
road_list=[]
for road in roads_tmp:
road_list.append(map(place_info[road]["position"]))
print(road_list)
for i in range(len(road_list)-1):
ax0.scatter(road_list[i][0],road_list[i][1],color='b',alpha=0.5)
ax0.plot([road_list[i][0],road_list[i+1][0]],[road_list[i][1],road_list[i+1][1]],color='b',alpha=0.5)
#
roads_work_index = roads_tol["roads_otherplace"]["hospital"]
for roads_tmp in roads_work_index:
road_list = []
for road in roads_tmp:
road_list.append(map(place_info[road]["position"]))
print(road_list)
for i in range(len(road_list) - 1):
ax0.scatter(road_list[i][0], road_list[i][1], color='g',alpha=0.5)
ax0.plot([road_list[i][0], road_list[i + 1][0]], [road_list[i][1], road_list[i + 1][1]], color='g',alpha=0.5)
roads_work_index = roads_tol["roads_otherplace"]["market"]
for roads_tmp in roads_work_index:
road_list = []
for road in roads_tmp:
road_list.append(map(place_info[road]["position"]))
print(road_list)
for i in range(len(road_list) - 1):
ax0.scatter(road_list[i][0], road_list[i][1], color='w', alpha=0.5)
ax0.plot([road_list[i][0], road_list[i + 1][0]], [road_list[i][1], road_list[i + 1][1]], color='w',
alpha=0.5)
ax0.imshow(img_back)
# ax1=fig.add_axes(rect,label='ax1',frameon=False)
# # plt.xlim((0,1920))
# # plt.ylim((0, 1080))
# ax1.plot([1000,200],[50,50])
# ax1.scatter([1000,200],[50,50])
plt.show()
测试结果
hm1-station