很久沒有更新了,今天趁着放假,趕忙來水一期。嗯沒錯,就是這麼直白。html
咱們作過幾個關於生產調度相關的算法,相關的傳送門以下:git
遺傳算法求解混合流水車間調度問題(附C++代碼)
github
做業車間調度JSP與遺傳算法GA及其Python/Java/C++實現
web
Tabu Search求解做業車間調度問題(Job Shop Scheduling)-附Java代碼
算法
可是說到生產調度,就不得不提甘特圖這東西,能夠用它來直觀看調度的狀況,很是方便。好比下圖中:編程
Python畫Gantt圖json
其實用Python畫gantt原理是利用plt.barh()繪製水平方向的條形圖,而後加以不一樣顏色區分表示。就是這麼簡單的。下面給出一個代碼模板:
import matplotlib.pyplot as plt
import numpy as np
ax=plt.gca()
[ax.spines[i].set_visible(False) for i in ["top","right"]]
def gatt(m,t):
"""甘特圖
m機器集
t時間集
"""
for j in range(len(m)):#工序j
i=m[j]-1#機器編號i
if j==0:
plt.barh(i,t[j])
plt.text(np.sum(t[:j+1])/8,i,'J%s\nT%s'%((j+1),t[j]),color="white",size=8)
else:
plt.barh(i,t[j],left=(np.sum(t[:j])))
plt.text(np.sum(t[:j])+t[j]/8,i,'J%s\nT%s'%((j+1),t[j]),color="white",size=8)
if __name__=="__main__":
"""測試代碼"""
m=np.random.randint(1,7,35)
t=np.random.randint(15,25,35)
gatt(m,t)
plt.yticks(np.arange(max(m)),np.arange(1,max(m)+1))
plt.show()
api
效果圖以下:數組
這裏講講plt.barh這個函數,官方barh()項目地址以下:微信
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.barh.html?highlight=barh#matplotlib.pyplot.barh
barh()表示繪製水平方向的條形圖,基本使用方法爲:
barh(y, width, left=0, height=0.8, edgecolor)
各個參數解析以下:
- y:在y軸上的位置
- width:條形圖的寬度(從左到右的哦)
- left:開始繪製的x座標
- edgecolor:圖形邊緣的顏色
仍是用圖解釋方便一點,好比下圖【J12 T21】:
固然,爲了讓各個圖形更有區分度,你也能夠指定邊緣的顏色。
{
"packages": [
{ "label" : "WP 1-1",
"start": 0,
"end": 2,
"milestones" : [2],
"legend": "worker one"
},
{ "label" : "WP 1-2",
"start": 2,
"end": 4,
"milestones" : [3, 4]
}
],
"title" : " Sample GANTT for \\textbf{myProject}",
"xlabel" : "time (weeks)",
"xticks" : [2,4,6,8,10,12]
}
- label:表示工做流程的名稱
- start:開始時間
- end:結束時間
- milestones:里程碑
- legend:標籤
- title:標題
- xlabel:x軸名稱
- xticks:x軸的刻度標籤
使用也很簡單,好比利用當前目錄下的sample.json生成一張甘特圖:
from gantt import Gantt
g = Gantt('./sample.json')
g.render()
g.show() # or save w/ g.save('foo.png')
效果圖以下:
%fileName:mt06_final.mt06
%fileDescription:create a gatt chart whith the data given
%creator:by mnmlist
%Version:1.0
%last edit time:06-05-2015
clear;
axis([0,42,0,6.5]);%x軸 y軸的範圍
set(gca,'xtick',0:2:42) ;%x軸的增加幅度
set(gca,'ytick',0:1:6.5) ;%y軸的增加幅度
xlabel('加工時間','FontName','微軟雅黑','Color','b','FontSize',16)
ylabel('機器號','FontName','微軟雅黑','Color','b','FontSize',16,'Rotation',90)
title('mk01 的一個最佳調度(最短完工時間爲40)','fontname','微軟雅黑','Color','b','FontSize',16);%圖形的標題
n_bay_nb=6;%total bays //機器數目
n_task_nb = 55;%total tasks //任務數目
%x軸 對應於畫圖位置的起始座標x
n_start_time=[0 0 2 6 0 0 3 4 10 13 4 3 10 6 12 4 5 6 14 7 9 9 16 7 11 14 15 12 16 17 16 15 18 19 19 20 21 20 22 21 24 24 25 27 30 30 27 25 28 33 36 33 30 37 37];%start time of every task //每一個工序的開始時間
%length 對應於每一個圖形在x軸方向的長度
n_duration_time =[6 2 1 6 4 3 1 6 3 3 2 1 2 1 2 1 1 3 2 2 6 2 1 4 4 2 6 6 1 2 1 4 6 1 6 1 1 1 5 6 1 6 4 3 6 1 6 3 2 6 1 4 6 1 3];%duration time of every task //每一個工序的持續時間
%y軸 對應於畫圖位置的起始座標y
n_bay_start=[1 5 5 1 2 4 5 5 4 4 3 0 5 2 5 0 0 3 5 0 3 0 5 2 2 0 3 1 0 5 4 2 1 0 5 0 0 2 0 3 2 1 2 0 1 0 3 4 5 3 0 2 5 2 0]; %bay id of every task ==工序數目,即在哪一行畫線
%工序號,能夠根據工序號選擇使用哪種顏色
n_job_id=[1 9 8 2 0 4 6 9 9 0 6 4 7 1 5 8 3 8 2 1 1 8 9 6 8 5 8 4 2 0 6 7 3 0 2 1 7 0 4 9 3 7 5 9 5 2 4 3 3 7 5 4 0 6 5];%
rec=[0,0,0,0];%temp data space for every rectangle
color=[1,0,0;
0,1,0;
0,0,1;
1,1,0;
1,0,1;
0,1,1;
0.67,0,1;
1,.5,0;
.9,.5,.2;
.5,.5,.5];%和上一個版本的最大不一樣在於,matlab中僅能夠用字符表示8種顏色,超過8種就不能夠了,如今用rgb數組能夠表示任意多的顏色
for i =1:n_task_nb
rec(1) = n_start_time(i);%矩形的橫座標
rec(2) = n_bay_start(i)+0.7; %矩形的縱座標
rec(3) = n_duration_time(i); %矩形的x軸方向的長度
rec(4) = 0.6;
txt=sprintf('p(%d,%d)=%d',n_bay_start(i)+1,n_job_id(i)+1,n_duration_time(i));%將機器號,工序號,加工時間連城字符串
rectangle('Position',rec,'LineWidth',0.5,'LineStyle','-','FaceColor',[color(n_job_id(i)+1,1),color(n_job_id(i)+1,2),color(n_job_id(i)+1,3)]);%draw every rectangle
text(n_start_time(i)+0.2,(n_bay_start(i)+1),txt,'FontWeight','Bold','FontSize',16);%label the id of every task ,字體的座標和其它特性
end
推薦閱讀:
乾貨 | 學習算法,你須要掌握這些編程基礎(包含JAVA和C++)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
本文分享自微信公衆號 - 程序猿聲(ProgramDream)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。