Simple elevator scheduling algorithm simulation

Source: Internet
Author: User

We often take the elevator in the company. Because of the high floor, it is a double-mounted elevator. But we often wait for the elevator for a long time, and some people often scold us for writing elevator scheduling.Algorithm. When I came back, I tried to write a simple one.

The scenario is very simple. There is only one button at each floor of the elevator without distinction between upper and lower. When someone presses this button, the elevator will come and stop on this floor. This person can go in, select the desired layer. The elevator scheduling policy is also very simple. In an upward process, if someone presses the key below, the elevator does not go down directly, but runs to the top-level of the upward direction, then, the request will be served in the next running process.

 

Elevator. py

Import time <br/> from myque import myque <br/> class elevator: <br/> def _ init _ (self, layers): <br/> self. building_layers = Layers <br/> self. direction = 'up' <br/> self. cur_layer = 1 <br/> self. up_queue = myque () <br/> self. down_queue = myque (true) <br/> self. switcher = 'open' </P> <p> def stop (Self): <br/> self. switcher = 'stop' </P> <p> def push_button (self, layer, direction = none): <br/> If self. cur_la Yer> layer: <br/> self. down_queue.insert (layer) <br/> Elif self. cur_layer <layer: <br/> self. up_queue.insert (layer) <br/> else: <br/> If self. direction = 'up': <br/> self. down_queue.insert (layer) <br/> else: <br/> self. up_queue.insert (layer) </P> <p> def handle_queue (self, direction): <br/> self. direction = direction <br/> If direction = 'up': <br/> Inc = 1 <br/> else: <br/> Inc =-1 <br/> que = getatt R (self, direction + '_ queue') <br/> while que. Length (): <br/> while self. cur_layer! = Que. front (): <br/> Print '/nelevator in', self. cur_layer <br/> time. sleep (1) <br/> self. cur_layer + = inc <br/> Print '/nelevator arrives at', self. cur_layer <br/> que. pop_front () </P> <p> def run (Self): <br/> while self. switcher = 'open': <br/> If self. up_queue.empty () and self. down_queue.empty (): <br/> "" elevator now is waiting, stop at a layer "" <br/> time. sleep (1) <br/> continue <br/> "Go up" <br/> self. handle_queue ('up') <br/> "Go Down" <br/> self. handle_queue ('low ')

myque. py

Import threading <br/> class myque: <br/> def _ init _ (self, reverse = false): <br/> self. mode = reverse <br/> self. buf = [] <br/> self. lock = threading. lock () </P> <p> def insert (self, object): <br/> self. lock. acquire () <br/> self. buf. append (object) <br/> self. buf. sort (reverse = self. mode) <br/> self. lock. release () </P> <p> def Front (Self): <br/> return self. buf [0] </P> <p> def pop_front (Self): <br/> self. lock. acquire () <br/> self. buf. pop (0) <br/> self. lock. release () </P> <p> def length (Self): <br/> self. lock. acquire () <br/> size = Len (self. buf) <br/> self. lock. release () <br/> return size </P> <p> def empty (Self): <br/> self. lock. acquire () <br/> size = Len (self. buf) <br/> self. lock. release () <br/> return size = 0

deploy. py

Import threading <br/> from elevator import elevator <br/> def init_elevator (building_layers): <br/> E = elevator (building_layers) <br/> T = threading. thread (target = E. run) <br/> T. setdaemon (true) <br/> T. start () <br/> return (E, T) <br/> def main (): <br/> myelevator, ctl_thread = init_elevator (17) <br/> while true: <br/> STR = raw_input ("input valid layer:") <br/> try: <br/> layer = int (STR) <br/> Except t exception: <br/> If STR = 'quit': <br/> myelevator. stop () <br/> ctl_thread.join () <br/> Break <br/> else: <br/> Print 'invalid input ', STR <br/> continue </P> <p> If layer not in range (1, myelevator. building_layers + 1): <br/> continue <br/> myelevator. push_button (layer) <br/> If _ name __= = '_ main _': <br/> main () <br/>

If it is extended, it is easy to extend the buttons of each layer to those with up/down indication. If you have the opportunity to expand to multi-link elevators and make the scheduling algorithm smarter, you can dynamically adjust it based on historical data and time.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.