毕业设计的系统中需要绘制货架在仓库中的分布以及机器人与拣货人员在调度安排的甘特图
# 一、使用 python 绘制网格分布图像
绘制网格分布图像主要需要使用 plt.fill()
函数,毕业设计中使用的程序如下:
import matplotlib.pyplot as plt | |
import numpy as np | |
length = 5 | |
weight = 3 | |
n_pod = 3 | |
loc_pod = [[0, 0], [2, 2], [3, 1]] | |
loc_station = [5, 1] | |
x, y = [], [] | |
for i in range(len(loc_pod)): | |
x.append(loc_pod[i][0]) | |
y.append(loc_pod[i][1]) | |
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文标签 | |
plt.rcParams['axes.unicode_minus'] = False # 显示中文标签 | |
# 绘制货架 | |
for i in range(len(loc_pod)): | |
plt.fill(np.array([x[i], x[i], x[i]+1, x[i]+1]), np.array([y[i], y[i]+1, y[i]+1, y[i]]), alpha=0.3, facecolor='green') | |
plt.text(x[i]+0.3, y[i]+0.45, '货架' + str(i)) | |
# 绘制拣货台 | |
plt.fill(np.array([loc_station[0], loc_station[0], loc_station[0]+1, loc_station[0]+1]), | |
np.array([loc_station[1], loc_station[1]+1, loc_station[1]+1, loc_station[1]]), alpha=0.3, facecolor='red') | |
plt.text(loc_station[0]+0.3, loc_station[1]+0.45, '拣货台') | |
# 添加网格线 | |
plt.xticks(np.arange(0, 100, 1)) | |
plt.yticks(np.arange(0, 100, 1)) | |
plt.xlim(0, length+1) | |
plt.ylim(0, weight) | |
plt.grid(True) | |
# 添加标题和标签 | |
plt.title("货架分布示意图") | |
plt.xlabel("X") | |
plt.ylabel("Y") | |
# 显示图像 | |
plt.savefig("loc.jpg", | |
bbox_inches="tight", | |
pad_inches=1, | |
transparent=True) |
上述程序的运行结果如下图所示:
# 二、使用 python 绘制甘特图
绘制网格分布图像主要需要使用 plt.barh()
函数,毕业设计中使用的程序如下:
import matplotlib.pyplot as plt | |
from functions import * | |
import random | |
import math | |
import time | |
import json | |
def plt_gantt(robot_task, human_task): | |
plt.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文标签 | |
plt.rcParams['axes.unicode_minus'] = False # 显示中文标签 | |
colors = ["r", "pink", "orange", "y", "g", "b", "deeppink", "purple", "brown", "black"] | |
for i in range(len(human_task[0])): | |
plt.barh(y=0, width=human_task[2][i] - human_task[1][i], left=human_task[1][i], edgecolor="black", align='center', | |
color=colors[human_task[0][i]]) | |
if human_task[2][i] - human_task[1][i] > 1.5: | |
plt.text((human_task[2][i] + human_task[1][i]) / 2 - 0.7, 0, '货架' + str(human_task[0][i])) | |
for i in range(len(robot_task)): | |
for j in range(len(robot_task[i][0])): | |
if j == 0: | |
plt.barh(y=i + 1, width=robot_task[i][1][j], left=0, edgecolor="black", align='center', color=colors[robot_task[i][0][j]]) | |
plt.text(0 + robot_task[i][1][j] / 2 - 1, i + 1, '货架' + str(robot_task[i][0][j])) | |
else: | |
plt.barh(y=i + 1, width=robot_task[i][1][j] - robot_task[i][1][j - 1], left=robot_task[i][1][j - 1], edgecolor="black", | |
align='center', color=colors[robot_task[i][0][j]]) | |
plt.text((robot_task[i][1][j - 1] + robot_task[i][1][j]) / 2 - 1, i + 1, '货架' + str(robot_task[i][0][j])) | |
ylabels = [] # 生成 y 轴标签 | |
for i in range(len(robot_task) + 1): | |
if i == 0: | |
ylabels.append("拣货人员") | |
else: | |
ylabels.append("机器人 " + str(i - 1)) | |
plt.yticks(range(len(robot_task) + 1), ylabels, rotation=0) | |
plt.title("任务安排示意图") | |
plt.xlabel("工作时间") | |
plt.savefig("result.jpg", | |
bbox_inches="tight", | |
pad_inches=1, | |
transparent=True) | |
# 功能:计算目标函数 1 的值 | |
# 输入参数:货架列表 list | |
# 输出参数:目标值 1 (target) | |
def calculate_single_target(pod_list): | |
x = np.zeros((n_pod, n_robot, n_pod), dtype=int) # 货架 i 是机器人 r 第 k 个转运的任务(0,1 变量) | |
tP = -1 * np.ones(n_pod) # 货架 i 被机器人拿起的时间,-1 表示未搬运货架 | |
tD = -1 * np.ones(n_pod) # 货架 i 被机器人放下的时间,-1 表示未搬运货架 | |
robot_condition = np.zeros(n_robot) # 每个机器人的工作状态(即完成当前工作时间) | |
n_robot_work = np.zeros(n_robot, dtype=int) # 每个机器人的工作数量 | |
selected_pods = get_selected_pods(np.array(A), n_commodity_type, n_pod, np.array(B), np.array(pod_list)) | |
selected_pods = list(selected_pods) | |
for i in range(len(selected_pods)): | |
selected_robot = select_robot(robot_condition) | |
tP[selected_pods[i]] = robot_condition[selected_robot] + Tp[selected_pods[i]] | |
tD[selected_pods[i]] = tP[selected_pods[i]] + Td[selected_pods[i]] | |
robot_condition[selected_robot] = robot_condition[selected_robot] + Tp[selected_pods[i]] + Td[selected_pods[i]] | |
x[selected_pods[i], selected_robot, n_robot_work[selected_robot]] = 1 | |
n_robot_work[selected_robot] = n_robot_work[selected_robot] + 1 | |
y = get_y(tD, selected_pods) | |
z = get_z(y, np.array(selected_pods), np.array(A), np.array(B)) | |
tH = get_tH(y, z, np.array(selected_pods), tD, Th) | |
target1 = max(tH + Th * np.sum(np.sum(z, axis=1), axis=1)) | |
# if not judge_constraint (x, y, z, tP, tD, tH, selected_pods): # 判断是否符合约束条件 | |
# raise Exception ("不满足约束条件") | |
return round(target1, decimal_Th), selected_pods | |
def get_x_y(pod_list): | |
x = np.zeros((n_pod, n_robot, n_pod), dtype=int) # 货架 i 是机器人 r 第 k 个转运的任务(0,1 变量) | |
tP = -1 * np.ones(n_pod) # 货架 i 被机器人拿起的时间,-1 表示未搬运货架 | |
tD = -1 * np.ones(n_pod) # 货架 i 被机器人放下的时间,-1 表示未搬运货架 | |
robot_condition = np.zeros(n_robot) # 每个机器人的工作状态(即完成当前工作时间) | |
n_robot_work = np.zeros(n_robot, dtype=int) # 每个机器人的工作数量 | |
selected_pods_temp = get_selected_pods(np.array(A), n_commodity_type, n_pod, np.array(B), np.array(pod_list)) | |
selected_pods_temp = list(selected_pods_temp) | |
for i in range(len(selected_pods_temp)): | |
selected_robot = select_robot(robot_condition) | |
tP[selected_pods_temp[i]] = robot_condition[selected_robot] + Tp[selected_pods_temp[i]] | |
tD[selected_pods_temp[i]] = tP[selected_pods_temp[i]] + Td[selected_pods_temp[i]] | |
robot_condition[selected_robot] = robot_condition[selected_robot] + Tp[selected_pods_temp[i]] + Td[selected_pods_temp[i]] | |
x[selected_pods_temp[i], selected_robot, n_robot_work[selected_robot]] = 1 | |
n_robot_work[selected_robot] = n_robot_work[selected_robot] + 1 | |
y = get_y(tD, selected_pods_temp) | |
z = get_z(y, np.array(selected_pods_temp), np.array(A), np.array(B)) | |
tH = get_tH(y, z, np.array(selected_pods_temp), tD, Th) | |
return x, y, z, selected_pods_temp, tD, tH | |
'''优化函数''' | |
# 功能:模拟退火算法 (SA) | |
# 输入参数:初始货架排序,初始温度,最终温度,降温因子,每次降温后循环次数 | |
# 输出参数:最优货架排序,最优目标值 1 | |
def tsp_SA(tour, initial_temperature, final_temperature, alpha, epoch): | |
start_time = time.time() | |
target1, selected_pods = calculate_single_target(tour) | |
best_target1 = target1 | |
best_tour = tour | |
T = initial_temperature | |
iteration = 0 | |
while T > final_temperature: | |
selected_pods_list = [selected_pods[i] for i in range(len(selected_pods))] | |
pod_list = [i for i in range(n_pod)] | |
random.shuffle(selected_pods_list) | |
random.shuffle(pod_list) | |
i, j = selected_pods_list[0], pod_list[0] | |
sol = [tour[i] for i in range(len(tour))] | |
sol_change = sol[:] | |
sol_change[i], sol_change[j] = sol_change[j], sol_change[i] | |
delta = calculate_single_target(sol_change)[0] - target1 | |
if delta < 0 or math.exp(-delta / T) > rando(): | |
tour[i], tour[j] = tour[j], tour[i] | |
target1, selected_pods = calculate_single_target(tour) # Move accepted | |
# is there an improvement? | |
if best_target1 > target1: | |
best_target1 = target1 | |
best_tour = tour | |
iteration += 1 | |
if iteration % (epoch * epoch) == 0: | |
T *= alpha # Decrease temperature | |
if time.time() - start_time >= time_constraint: | |
print('the iteration of SA is ', iteration + 1) | |
time_cost = time.time() - start_time | |
print(best_tour, best_target1, time_cost) | |
return best_tour, best_target1, time.time() - start_time | |
end_time = time.time() | |
time_cost = end_time - start_time | |
print('SA has been executed ') | |
print('the iteration of SA is ', (iteration + 1) // (epoch * epoch)) | |
print(best_tour, best_target1, time_cost) | |
return best_tour, best_target1, time_cost | |
if __name__ == "__main__": | |
np.random.seed(0) | |
decimal_Th = 1 | |
with open('plot_single_target.json', 'r', encoding='utf8') as fp: | |
# ensure_ascii=False 才能输入中文,否则是 Unicode 字符 | |
# indent=2 JSON 数据的缩进,美观 | |
json_datas = json.load(fp) | |
fp.close() | |
json_data = 0 | |
length, weight = json_datas[json_data]['length'], json_datas[json_data]['weight'] | |
n_pod, pod_limitation, loc_pod, loc_station = json_datas[json_data]['n_pod'], json_datas[json_data]['pod_limitation'], \ | |
json_datas[json_data]['loc_pod'], json_datas[json_data]['loc_station'] | |
n_robot, ratio_T, Tp, Td = json_datas[json_data]['n_robot'], json_datas[json_data]['ratio_T'], json_datas[json_data]['Tp'], \ | |
json_datas[json_data]['Td'] | |
n_commodity_type, commodity_limitation, A = json_datas[json_data]['n_commodity_type'], json_datas[json_data][ | |
'commodity_limitation'], json_datas[json_data]['A'] | |
total_order, n_order, B = json_datas[json_data]['total_order'], json_datas[json_data]['n_order'], json_datas[json_data]['B'] | |
Th = json_datas[json_data]['Th'] | |
print('The ' + str(json_data + 1) + 'th element of json file is successfully read') | |
time_constraint = n_pod * 3 | |
print('time_constraint: ', time_constraint) | |
iterations_main = 456 | |
pod_list_rank = [i for i in range(n_pod)] | |
tour_SA, target1_SA_temp_single, time_SA_temp_single = tsp_SA(pod_list_rank, 100 * n_pod, 0.01 * n_pod, 0.98, 5) | |
# 获取机器人及拣货人员的任务安排 | |
x, y, z, selected_pods, tD, tH = get_x_y(tour_SA) | |
robot_task = [] | |
for i in range(n_robot): | |
robot_task.append([]) | |
robot_task[i].append([]) | |
for j in range(n_pod): | |
for k in range(n_pod): | |
if x[k, i, j] == 1: | |
robot_task[i][0].append(k) | |
robot_task[i].append([]) | |
for j in range(len(robot_task[i][0])): | |
robot_task[i][1].append(tD[robot_task[i][0][j]]) | |
transform_y = [] | |
for i in range(len(selected_pods)): | |
for j in range(y.shape[0]): | |
if y[j, i] == 1: | |
transform_y.append(j) | |
break | |
human_task = [[], [], []] | |
for i in range(len(transform_y)): | |
human_task[0].append(transform_y[i]) | |
human_task[1].append(tH[transform_y[i]]) | |
human_task[2].append(tH[transform_y[i]] + Th * np.sum(z[transform_y[i], :, :])) | |
print('算法SA已经执行完毕,针对目标值' + str(target1_SA_temp_single) + '求解最优值为:' + str(tour_SA)) | |
print('对应的机器人调度安排为:') | |
for i in range(n_robot): | |
print('机器人' + str(i) + '调度安排:' + str(robot_task[i][0])) | |
print('对应工作的完成时间为:') | |
for i in range(n_robot): | |
print('机器人' + str(i) + '调度时间:' + str(robot_task[i][1])) | |
print('拣货人员拣货顺序:' + str(human_task[0])) | |
print('各货架开始拣货时间为:' + str(human_task[1])) | |
print('各货架结束拣货时间为:' + str(human_task[2])) | |
print('SA算法执行时间:' + str(time_SA_temp_single)) | |
# 绘制图形 | |
# (1)结果示意图 | |
plt_gantt(robot_task, human_task) |
上述程序的运行结果如下图所示: