import numpy as np import matplotlib.pyplot as plt # 设置中文字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 mylist = [] # 记录时间安排,两个一组表示开头结尾 labels = [] # 记录调度方案,用于产生不同的颜色 # 改进颜色生成函数 def get_color(m_str): "对于不同的工件产生不同的RGB颜色" num = int(m_str) # 使用 matplotlib 的 colormap 生成更均匀的颜色 from matplotlib.cm import get_cmap cmap = get_cmap('tab20') # 选择一个 colormap,例如 'tab20' return cmap(num % 20) # 确保 num 在 colormap 的范围内 try: inputfile = open("output/time.txt", 'r', encoding='utf-8') while True: templine = inputfile.readline() if templine == '': break templine = templine.strip('\t\n') templist = templine.split('\t') templist1 = [] for item in templist: templist1.append(int(item)) mylist.append(templist1) inputfile.close() except FileNotFoundError: print("Error: 文件 time.txt 不存在") exit() try: inputfile = open("output/management.txt", 'r', encoding='utf-8') while True: templine = inputfile.readline() if templine == '': break templine = templine.strip('\t\n') templist = templine.split('\t') labels.append(templist) inputfile.close() except FileNotFoundError: print("Error: 文件 management.txt 不存在") exit() y = np.array(mylist) for i in range(len(mylist)): for j in range(int(len(mylist[i])/2)): left = y[i][2 * j] width = y[i][2 * j + 1] - left if width > 0: # 确保宽度为正值 plt.barh(i + 1, width, left=left, color=get_color(labels[i][j])) plt.xlabel("time") plt.ylabel("machine") plt.title("甘特图") # 设置甘特图的标题 # 添加图例 unique_labels = list(set([item for sublist in labels for item in sublist])) colors = [get_color(label) for label in unique_labels] plt.legend(handles=[plt.Rectangle((0, 0), 1, 1, color=get_color(label)) for label in unique_labels], labels=unique_labels) plt.show() mylist = [] try: inputfile = open("data/trace.txt", 'r', encoding='utf-8') while True: templine = inputfile.readline() if templine == '': break templine = templine.strip('\t\n') templist = templine.split('\t') for item in templist: mylist.append(int(item)) inputfile.close() except FileNotFoundError: print("Error: 文件 trace.txt 不存在") exit() plt.plot(mylist, linewidth=1) plt.title("目标函数变化图", fontsize=20) plt.xlabel("x", fontsize=10) plt.ylabel("f", fontsize=14) plt.tick_params(axis='both', labelsize=10) plt.show()