Python模擬簡單電梯調(diào)度算法示例
本文實例講述了Python模擬簡單電梯調(diào)度算法。分享給大家供大家參考,具體如下:
經(jīng)常在公司坐電梯,由于樓層較高,是雙聯(lián)裝的電梯,但是經(jīng)常等電梯很久,經(jīng)常有人罵寫電梯調(diào)度算法的?;貋黹e來無事,自己嘗試寫了一個簡單的。
場景很簡單,每一層電梯口只有一個按鈕,不區(qū)分上下,當有人按下這個鍵后,電梯會過來停在此層,這個人可以進去,并選擇自己想去的層。電梯的調(diào)度策略也很簡單,在一次向上的過程中,如果有人在下面按了鍵,電梯并不直接向下,而是運行到此次向上的最頂層,然后再下次向下運行的過程中去服務這個請求。
elevator.py
import time
from myque import myque
class elevator:
def __init__(self,layers):
self.building_layers = layers
self.direction = 'up'
self.cur_layer = 1
self.up_queue = myque()
self.down_queue = myque(True)
self.switcher = 'open'
def stop(self):
self.switcher='stop'
def push_button(self,layer,direction=None):
if self.cur_layer>layer:
self.down_queue.insert(layer)
elif self.cur_layer<layer:
self.up_queue.insert(layer)
else:
if self.direction=='up':
self.down_queue.insert(layer)
else:
self.up_queue.insert(layer)
def handle_queue(self,direction):
self.direction = direction
if direction == 'up':
inc = 1
else:
inc = -1
que = getattr(self , direction + '_queue')
while que.length():
while self.cur_layer != que.front():
print '/nelevator in ',self.cur_layer
time.sleep(1)
self.cur_layer += inc
print '/nelevator arrives at ',self.cur_layer
que.pop_front()
def run(self):
while self.switcher=='open':
if self.up_queue.empty() and self.down_queue.empty():
"""elevator now is waiting, stop at a layer"""
time.sleep(1)
continue
"""go up"""
self.handle_queue('up')
"""go down"""
self.handle_queue('down')
myque.py
import threading
class myque:
def __init__(self,reverse=False):
self.mode = reverse
self.buf = []
self.lock = threading.Lock()
def insert(self,object):
self.lock.acquire()
self.buf.append(object)
self.buf.sort(reverse = self.mode)
self.lock.release()
def front(self):
return self.buf[0]
def pop_front(self):
self.lock.acquire()
self.buf.pop(0)
self.lock.release()
def length(self):
self.lock.acquire()
size = len(self.buf)
self.lock.release()
return size
def empty(self):
self.lock.acquire()
size = len(self.buf)
self.lock.release()
return size==0
deploy.py
import threading
from elevator import elevator
def init_elevator(building_layers):
e = elevator(building_layers)
t = threading.Thread(target = e.run)
t.setDaemon(True)
t.start()
return (e,t)
def main():
myelevator,ctl_thread = init_elevator(17)
while True:
str=raw_input("Input valid layer :")
try:
layer = int(str)
except Exception:
if str=='quit':
myelevator.stop()
ctl_thread.join()
break
else:
print 'invalid input',str
continue
if layer not in range(1,myelevator.building_layers+1):
continue
myelevator.push_button(layer)
if __name__=='__main__':
main()
運行結(jié)果如下:

如果擴展的話,很容易將各層的按鈕擴展為帶上下指示的。如果有機會可以擴展為多聯(lián)裝電梯,并將調(diào)度算法做的更加智能,可以根據(jù)歷史數(shù)據(jù)和時間進行動態(tài)調(diào)整。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
使用Python?matplotlib繪制簡單的柱形圖、折線圖和直線圖
Matplotlib是Python的繪圖庫, 它可與NumPy一起使用,提供了一種有效的MatLab開源替代方案,下面這篇文章主要給大家介紹了關(guān)于使用Python?matplotlib繪制簡單的柱形圖、折線圖和直線圖的相關(guān)資料,需要的朋友可以參考下2022-08-08
matplotlib繪制鼠標的十字光標的實現(xiàn)(內(nèi)置方式)
這篇文章主要介紹了matplotlib繪制鼠標的十字光標的實現(xiàn)(內(nèi)置方式),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
python改變?nèi)罩?logging)存放位置的示例
示例主要解決的問題是通過傳入日志文件參數(shù)的方式來改變?nèi)罩镜拇娣盼恢?需要的朋友可以參考下2014-03-03
python如何標準化日期時間格式轉(zhuǎn)化成非標準化格式
這篇文章主要介紹了python如何標準化日期時間格式轉(zhuǎn)化成非標準化格式問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
matplotlib 畫動態(tài)圖以及plt.ion()和plt.ioff()的使用詳解
這篇文章主要介紹了matplotlib 畫動態(tài)圖以及plt.ion()和plt.ioff()的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Python數(shù)據(jù)正態(tài)性檢驗實現(xiàn)過程
這篇文章主要介紹了Python數(shù)據(jù)正態(tài)性檢驗實現(xiàn)過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04

