From 7764eb417e735c58fc8c17cfaf2597eb20add05c Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Fri, 22 Sep 2017 23:41:30 -0500 Subject: [PATCH 01/19] gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1513baa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +MyElevator.py +elevator.pyc From 5acca087d027be9eee366dbfe2487d86ee8002c9 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sat, 23 Sep 2017 11:06:05 -0500 Subject: [PATCH 02/19] Insert floor_request in front of queue, so it is processed last. --- elevator.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/elevator.py b/elevator.py index b44c167..7732b20 100644 --- a/elevator.py +++ b/elevator.py @@ -20,6 +20,7 @@ def __init__(self): # Feel free to add any instance variables you want. self.destination_floor = None self.callbacks = None + self.orders = [] def on_called(self, floor, direction): """ @@ -31,6 +32,8 @@ def on_called(self, floor, direction): direction: the direction the caller wants to go, up or down """ self.destination_floor = floor + self.orders.append(floor) + def on_floor_selected(self, floor): """ @@ -41,6 +44,7 @@ def on_floor_selected(self, floor): floor: the floor that was requested """ self.destination_floor = floor + self.orders.append(floor) def on_floor_changed(self): """ @@ -49,6 +53,11 @@ def on_floor_changed(self): """ if self.destination_floor == self.callbacks.current_floor: self.callbacks.motor_direction = None + if len(self.orders) > 0: + self.orders.pop() + if len(self.orders) > 0: + self.destination_floor = self.orders.pop() + def on_ready(self): """ From dfeb6f97b4925bbba7f5f1712fd5c4b0c366cbd5 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sat, 23 Sep 2017 12:45:08 -0500 Subject: [PATCH 03/19] Up to line 167 --- elevator.py | 58 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/elevator.py b/elevator.py index 7732b20..24c1347 100644 --- a/elevator.py +++ b/elevator.py @@ -20,7 +20,10 @@ def __init__(self): # Feel free to add any instance variables you want. self.destination_floor = None self.callbacks = None - self.orders = [] + self.orders = {} + self.orders[UP] = [] + self.orders[DOWN] = [] + self.current_direction = None def on_called(self, floor, direction): """ @@ -31,8 +34,15 @@ def on_called(self, floor, direction): floor: the floor that the elevator is being called to direction: the direction the caller wants to go, up or down """ + + target_direction = self.direction_to(floor) + + if self.current_direction is None: + # Change direction + self.current_direction = target_direction + self.destination_floor = floor - self.orders.append(floor) + self.orders[direction].insert(0, floor) def on_floor_selected(self, floor): @@ -43,21 +53,38 @@ def on_floor_selected(self, floor): floor: the floor that was requested """ - self.destination_floor = floor - self.orders.append(floor) + + target_direction = self.direction_to(floor) + + self.orders[target_direction].insert(0, floor) + + # sort the list so closer floors are attended first + self.orders[target_direction].sort() + + if self.current_direction is None: + self.current_direction = target_direction + + self.destination_floor = self.orders[self.current_direction][0] def on_floor_changed(self): """ This lets you know that the elevator has moved one floor up or down. You should decide whether or not you want to stop the elevator. """ + if self.destination_floor == self.callbacks.current_floor: self.callbacks.motor_direction = None - if len(self.orders) > 0: - self.orders.pop() - if len(self.orders) > 0: - self.destination_floor = self.orders.pop() + if self.current_direction and self.orders[self.current_direction]: + self.destination_floor = self.orders[self.current_direction].pop() + if self.current_direction and not self.orders[self.current_direction]: + other_direction = self.other_direction(self.current_direction) + if other_direction and self.orders[other_direction]: + self.current_direction = other_direction + # Set the new target floor + self.destination_floor = self.orders[self.current_direction].pop() + else: + self.current_direction = None def on_ready(self): """ @@ -69,3 +96,18 @@ def on_ready(self): self.callbacks.motor_direction = UP elif self.destination_floor < self.callbacks.current_floor: self.callbacks.motor_direction = DOWN + + def direction_to(self, floor): + direction = None + if floor > self.callbacks.current_floor: + direction = UP + elif floor < self.callbacks.current_floor: + direction = DOWN + return direction + + def other_direction(self, direction): + if UP == direction: + return DOWN + if DOWN == direction: + return UP + return None From 6b80e90ddfed3dac5a64cf15c44803b100d88bef Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sat, 23 Sep 2017 13:37:16 -0500 Subject: [PATCH 04/19] Up to 173 --- elevator.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/elevator.py b/elevator.py index 24c1347..5da4b23 100644 --- a/elevator.py +++ b/elevator.py @@ -56,6 +56,9 @@ def on_floor_selected(self, floor): target_direction = self.direction_to(floor) + if self.current_direction and self.current_direction != target_direction: + return + self.orders[target_direction].insert(0, floor) # sort the list so closer floors are attended first @@ -75,16 +78,19 @@ def on_floor_changed(self): if self.destination_floor == self.callbacks.current_floor: self.callbacks.motor_direction = None if self.current_direction and self.orders[self.current_direction]: - self.destination_floor = self.orders[self.current_direction].pop() + self.orders[self.current_direction].pop(0) + if self.orders[self.current_direction]: + self.destination_floor = self.orders[self.current_direction][0] if self.current_direction and not self.orders[self.current_direction]: other_direction = self.other_direction(self.current_direction) if other_direction and self.orders[other_direction]: self.current_direction = other_direction # Set the new target floor - self.destination_floor = self.orders[self.current_direction].pop() - else: - self.current_direction = None + if self.orders[self.current_direction]: + self.destination_floor = self.orders[self.current_direction][0] + #else: + # self.current_direction = None def on_ready(self): """ @@ -92,6 +98,7 @@ def on_ready(self): Maybe passengers have embarked and disembarked. The doors are closed, time to actually move, if necessary. """ + #print "on ready: dest floor: %d" % self.destination_floor if self.destination_floor > self.callbacks.current_floor: self.callbacks.motor_direction = UP elif self.destination_floor < self.callbacks.current_floor: From 5554ceda87eba35d588be12118b4bafa0627b53b Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sat, 23 Sep 2017 22:45:25 -0500 Subject: [PATCH 05/19] Line 179 --- elevator.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/elevator.py b/elevator.py index 5da4b23..8ca7a0a 100644 --- a/elevator.py +++ b/elevator.py @@ -89,8 +89,9 @@ def on_floor_changed(self): # Set the new target floor if self.orders[self.current_direction]: self.destination_floor = self.orders[self.current_direction][0] - #else: - # self.current_direction = None + + if not self.orders[UP] and not self.orders[DOWN]: + self.current_direction = None # Elevator is idle def on_ready(self): """ @@ -98,7 +99,7 @@ def on_ready(self): Maybe passengers have embarked and disembarked. The doors are closed, time to actually move, if necessary. """ - #print "on ready: dest floor: %d" % self.destination_floor + # print "on ready: dest floor: %d" % self.destination_floor if self.destination_floor > self.callbacks.current_floor: self.callbacks.motor_direction = UP elif self.destination_floor < self.callbacks.current_floor: From 12274eb08c0c076f7ea5d0718c29caa81d2b4e49 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sat, 23 Sep 2017 23:14:24 -0500 Subject: [PATCH 06/19] Up to line 195 --- elevator.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/elevator.py b/elevator.py index 8ca7a0a..65169a9 100644 --- a/elevator.py +++ b/elevator.py @@ -24,6 +24,7 @@ def __init__(self): self.orders[UP] = [] self.orders[DOWN] = [] self.current_direction = None + self.bounded_direction = None def on_called(self, floor, direction): """ @@ -54,18 +55,22 @@ def on_floor_selected(self, floor): floor: the floor that was requested """ - target_direction = self.direction_to(floor) + direction_to_floor = self.direction_to(floor) + + if self.bounded_direction and direction_to_floor == self.bounded_direction: + self.current_direction = self.bounded_direction + self.bounded_direction = None - if self.current_direction and self.current_direction != target_direction: + if self.current_direction and self.current_direction != direction_to_floor: return - self.orders[target_direction].insert(0, floor) + self.orders[direction_to_floor].insert(0, floor) # sort the list so closer floors are attended first - self.orders[target_direction].sort() + self.orders[direction_to_floor].sort() if self.current_direction is None: - self.current_direction = target_direction + self.current_direction = direction_to_floor self.destination_floor = self.orders[self.current_direction][0] @@ -77,12 +82,13 @@ def on_floor_changed(self): if self.destination_floor == self.callbacks.current_floor: self.callbacks.motor_direction = None - if self.current_direction and self.orders[self.current_direction]: - self.orders[self.current_direction].pop(0) - if self.orders[self.current_direction]: - self.destination_floor = self.orders[self.current_direction][0] + self.orders[self.current_direction].pop(0) + if self.orders[self.current_direction]: + self.destination_floor = self.orders[self.current_direction][0] + else: + self.bounded_direction = self.current_direction - if self.current_direction and not self.orders[self.current_direction]: + if not self.orders[self.current_direction]: other_direction = self.other_direction(self.current_direction) if other_direction and self.orders[other_direction]: self.current_direction = other_direction @@ -90,9 +96,13 @@ def on_floor_changed(self): if self.orders[self.current_direction]: self.destination_floor = self.orders[self.current_direction][0] - if not self.orders[UP] and not self.orders[DOWN]: + if self.is_idle(): self.current_direction = None # Elevator is idle + def is_idle(self): + return not self.orders[UP] and not self.orders[DOWN] + + def on_ready(self): """ This is called when the elevator is ready to go. From db96a20093b8cc85697f9a21cd56ba9932a695e0 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sat, 23 Sep 2017 23:24:21 -0500 Subject: [PATCH 07/19] Up to line 226 --- elevator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/elevator.py b/elevator.py index 65169a9..8792fc0 100644 --- a/elevator.py +++ b/elevator.py @@ -123,7 +123,8 @@ def direction_to(self, floor): direction = DOWN return direction - def other_direction(self, direction): + @staticmethod + def other_direction(direction): if UP == direction: return DOWN if DOWN == direction: From 40f5737e67e041126e583338bb80b37d83101bd8 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sun, 24 Sep 2017 11:10:44 -0500 Subject: [PATCH 08/19] Up to line 232, 264 --- elevator.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/elevator.py b/elevator.py index 8792fc0..699080a 100644 --- a/elevator.py +++ b/elevator.py @@ -57,11 +57,17 @@ def on_floor_selected(self, floor): direction_to_floor = self.direction_to(floor) - if self.bounded_direction and direction_to_floor == self.bounded_direction: - self.current_direction = self.bounded_direction - self.bounded_direction = None + if self.bounded_direction: + if direction_to_floor == self.bounded_direction: + self.current_direction = self.bounded_direction + self.bounded_direction = None + else: + self.bounded_direction = None + return if self.current_direction and self.current_direction != direction_to_floor: + # Set it to wait for requests to move to the other direction + self.current_direction = self.other_direction(self.current_direction) return self.orders[direction_to_floor].insert(0, floor) @@ -84,7 +90,13 @@ def on_floor_changed(self): self.callbacks.motor_direction = None self.orders[self.current_direction].pop(0) if self.orders[self.current_direction]: - self.destination_floor = self.orders[self.current_direction][0] + next_destination = self.orders[self.current_direction][0] + if next_destination != self.callbacks.current_floor: + self.destination_floor = next_destination + else: + self.orders[self.current_direction].pop(0) # drop it, already there + self.destination_floor = None + self.bounded_direction = self.current_direction else: self.bounded_direction = self.current_direction @@ -99,10 +111,6 @@ def on_floor_changed(self): if self.is_idle(): self.current_direction = None # Elevator is idle - def is_idle(self): - return not self.orders[UP] and not self.orders[DOWN] - - def on_ready(self): """ This is called when the elevator is ready to go. @@ -114,6 +122,8 @@ def on_ready(self): self.callbacks.motor_direction = UP elif self.destination_floor < self.callbacks.current_floor: self.callbacks.motor_direction = DOWN + else: + self.bounded_direction = None def direction_to(self, floor): direction = None @@ -123,6 +133,9 @@ def direction_to(self, floor): direction = DOWN return direction + def is_idle(self): + return not self.orders[UP] and not self.orders[DOWN] + @staticmethod def other_direction(direction): if UP == direction: From 055232cb942bdc83c21b4a6317e614cc1df93531 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sun, 24 Sep 2017 17:03:02 -0500 Subject: [PATCH 09/19] Up to line 268 --- elevator.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/elevator.py b/elevator.py index 699080a..6074ccb 100644 --- a/elevator.py +++ b/elevator.py @@ -2,6 +2,7 @@ DOWN = 2 FLOOR_COUNT = 6 + class ElevatorLogic(object): """ An incorrect implementation. Can you make it pass all the tests? @@ -42,9 +43,14 @@ def on_called(self, floor, direction): # Change direction self.current_direction = target_direction - self.destination_floor = floor - self.orders[direction].insert(0, floor) + if self.callbacks.current_floor != floor: + self.destination_floor = floor + self.orders[direction].insert(0, floor) + else: + # Missed the boat, come back later + self.orders[self.other_direction(self.current_direction)].insert(0, floor) + # print "on called. Status: ", self.status() def on_floor_selected(self, floor): """ @@ -57,6 +63,10 @@ def on_floor_selected(self, floor): direction_to_floor = self.direction_to(floor) + if direction_to_floor is None: + # print "missed the boat. status: %s " % self.status() + return + if self.bounded_direction: if direction_to_floor == self.bounded_direction: self.current_direction = self.bounded_direction @@ -111,6 +121,8 @@ def on_floor_changed(self): if self.is_idle(): self.current_direction = None # Elevator is idle + # print "on_changed. status: %s" % self.status() + def on_ready(self): """ This is called when the elevator is ready to go. @@ -125,6 +137,8 @@ def on_ready(self): else: self.bounded_direction = None + # print "on ready. status: %s" % self.status() + def direction_to(self, floor): direction = None if floor > self.callbacks.current_floor: @@ -143,3 +157,23 @@ def other_direction(direction): if DOWN == direction: return UP return None + + def status(self): + def direction_str(direction): + if UP == direction: + return "UP" + elif DOWN == direction: + return "DOWN" + else: + return "None" + + return """Current direction: %s + Current floor: %d + Destination floor: %d + orders UP: %s + orders DOWN: %s + """ % (direction_str(self.current_direction), + self.callbacks.current_floor, + self.destination_floor, + self.orders[UP], + self.orders[DOWN]) From 214e6752e69a216151f1273334a0c30d364cf02c Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sun, 24 Sep 2017 17:21:33 -0500 Subject: [PATCH 10/19] Up to line 312 sans fuzzy --- elevator.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/elevator.py b/elevator.py index 6074ccb..c2afd6e 100644 --- a/elevator.py +++ b/elevator.py @@ -44,8 +44,10 @@ def on_called(self, floor, direction): self.current_direction = target_direction if self.callbacks.current_floor != floor: - self.destination_floor = floor self.orders[direction].insert(0, floor) + # Reorder + self.orders[direction].sort(reverse=direction == DOWN) + self.destination_floor = self.orders[direction][0] else: # Missed the boat, come back later self.orders[self.other_direction(self.current_direction)].insert(0, floor) From 6482c84cf55d1ea619b6031f90e5db892dd38366 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sun, 24 Sep 2017 17:45:33 -0500 Subject: [PATCH 11/19] Up to line 345 --- elevator.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/elevator.py b/elevator.py index c2afd6e..0f56d36 100644 --- a/elevator.py +++ b/elevator.py @@ -37,17 +37,21 @@ def on_called(self, floor, direction): direction: the direction the caller wants to go, up or down """ - target_direction = self.direction_to(floor) + direction_to_floor = self.direction_to(floor) if self.current_direction is None: # Change direction - self.current_direction = target_direction + self.current_direction = direction_to_floor if self.callbacks.current_floor != floor: self.orders[direction].insert(0, floor) # Reorder - self.orders[direction].sort(reverse=direction == DOWN) - self.destination_floor = self.orders[direction][0] + self.orders[UP].sort() + self.orders[DOWN].sort(reverse=True) + if self.current_direction == UP and self.orders[UP]: + self.destination_floor = self.orders[UP][0] + else: + self.destination_floor = self.orders[direction][0] else: # Missed the boat, come back later self.orders[self.other_direction(self.current_direction)].insert(0, floor) From 12769d9ba0dbc7e39c3db67b7ae7ced216af6c5b Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sun, 24 Sep 2017 18:07:21 -0500 Subject: [PATCH 12/19] Up to line 367 sans fuzzy --- elevator.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/elevator.py b/elevator.py index 0f56d36..aec055a 100644 --- a/elevator.py +++ b/elevator.py @@ -104,7 +104,10 @@ def on_floor_changed(self): if self.destination_floor == self.callbacks.current_floor: self.callbacks.motor_direction = None - self.orders[self.current_direction].pop(0) + if self.orders[self.current_direction]: + self.orders[self.current_direction].pop(0) + else: + self.orders[self.other_direction(self.current_direction)].pop(0) #something had to be served ( if self.orders[self.current_direction]: next_destination = self.orders[self.current_direction][0] if next_destination != self.callbacks.current_floor: From dba92ad2a2dcafaa75666088ba560f79b707e3f3 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sun, 24 Sep 2017 22:02:33 -0500 Subject: [PATCH 13/19] Up to line 378 --- elevator.py | 82 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/elevator.py b/elevator.py index aec055a..b14fd60 100644 --- a/elevator.py +++ b/elevator.py @@ -2,6 +2,7 @@ DOWN = 2 FLOOR_COUNT = 6 +import time class ElevatorLogic(object): """ @@ -17,6 +18,14 @@ class ElevatorLogic(object): elevator by setting the `motor_direction` property. See below for how this is done. """ + class Call(object): + def __init__(self, floor, time): + self.floor = floor + self.time = time + + def __repr__(self): + return "%d" % self.floor + def __init__(self): # Feel free to add any instance variables you want. self.destination_floor = None @@ -44,19 +53,36 @@ def on_called(self, floor, direction): self.current_direction = direction_to_floor if self.callbacks.current_floor != floor: - self.orders[direction].insert(0, floor) + self.index(direction, floor) # Reorder - self.orders[UP].sort() - self.orders[DOWN].sort(reverse=True) + self.sort(UP) + self.sort(DOWN) if self.current_direction == UP and self.orders[UP]: - self.destination_floor = self.orders[UP][0] + self.destination_floor = self.orders[UP][0].floor else: - self.destination_floor = self.orders[direction][0] + self.destination_floor = self.orders[direction][0].floor else: # Missed the boat, come back later - self.orders[self.other_direction(self.current_direction)].insert(0, floor) + self.index(self.other_direction(self.current_direction), floor) - # print "on called. Status: ", self.status() + # print "direction to floor: ", self.direction_str(direction_to_floor) + self.log("on called") + + def index(self, direction, floor): + self.orders[direction].insert(0, self.Call(floor, time.time())) + + def sort(self, direction): + if direction == UP: + if self.callbacks.motor_direction: + self.orders[UP].sort(key=lambda x: x.floor) + elif all(x.floor > self.callbacks.current_floor for x in self.orders[UP]): + self.orders[UP].sort(key=lambda x: x.floor) + else: + self.orders[UP].sort(key=lambda x: x.time) + elif direction == DOWN: + self.orders[DOWN].sort(key=lambda x: x.time, reverse=True) + else: + pass def on_floor_selected(self, floor): """ @@ -86,15 +112,18 @@ def on_floor_selected(self, floor): self.current_direction = self.other_direction(self.current_direction) return - self.orders[direction_to_floor].insert(0, floor) + self.index(direction_to_floor, floor) # sort the list so closer floors are attended first - self.orders[direction_to_floor].sort() + # self.orders[direction_to_floor].sort() + self.sort(direction_to_floor) if self.current_direction is None: self.current_direction = direction_to_floor - self.destination_floor = self.orders[self.current_direction][0] + self.destination_floor = self.orders[self.current_direction][0].floor + + self.log("on floor selected") def on_floor_changed(self): """ @@ -107,9 +136,9 @@ def on_floor_changed(self): if self.orders[self.current_direction]: self.orders[self.current_direction].pop(0) else: - self.orders[self.other_direction(self.current_direction)].pop(0) #something had to be served ( + self.orders[self.other_direction(self.current_direction)].pop(0) # something had to be served ( if self.orders[self.current_direction]: - next_destination = self.orders[self.current_direction][0] + next_destination = self.orders[self.current_direction][0].floor if next_destination != self.callbacks.current_floor: self.destination_floor = next_destination else: @@ -125,12 +154,12 @@ def on_floor_changed(self): self.current_direction = other_direction # Set the new target floor if self.orders[self.current_direction]: - self.destination_floor = self.orders[self.current_direction][0] + self.destination_floor = self.orders[self.current_direction][0].floor if self.is_idle(): self.current_direction = None # Elevator is idle - # print "on_changed. status: %s" % self.status() + self.log("on_changed") def on_ready(self): """ @@ -146,7 +175,7 @@ def on_ready(self): else: self.bounded_direction = None - # print "on ready. status: %s" % self.status() + self.log("on ready") def direction_to(self, floor): direction = None @@ -167,22 +196,27 @@ def other_direction(direction): return UP return None - def status(self): - def direction_str(direction): - if UP == direction: - return "UP" - elif DOWN == direction: - return "DOWN" - else: - return "None" + @staticmethod + def direction_str(direction): + if UP == direction: + return "UP" + elif DOWN == direction: + return "DOWN" + else: + return "None" + def status(self): return """Current direction: %s Current floor: %d Destination floor: %d orders UP: %s orders DOWN: %s - """ % (direction_str(self.current_direction), + """ % (self.direction_str(self.current_direction), self.callbacks.current_floor, self.destination_floor, self.orders[UP], self.orders[DOWN]) + + def log(self, msg): + pass + # print "%s. status: %s" % (msg, self.status()) From 25827b6048627ab2eac9e42fa0a93fa2587ca430 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Sun, 24 Sep 2017 23:19:09 -0500 Subject: [PATCH 14/19] logging and wip --- elevator.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/elevator.py b/elevator.py index b14fd60..148a567 100644 --- a/elevator.py +++ b/elevator.py @@ -96,20 +96,24 @@ def on_floor_selected(self, floor): direction_to_floor = self.direction_to(floor) if direction_to_floor is None: - # print "missed the boat. status: %s " % self.status() + self.log("missed the boat") return if self.bounded_direction: + self.log("floor selected. bounded direction detected. direction to floor %s" + % self.direction_str(direction_to_floor)) if direction_to_floor == self.bounded_direction: self.current_direction = self.bounded_direction self.bounded_direction = None else: + self.log("floor selection ignored") self.bounded_direction = None return if self.current_direction and self.current_direction != direction_to_floor: # Set it to wait for requests to move to the other direction self.current_direction = self.other_direction(self.current_direction) + self.log("floor selection ignored") return self.index(direction_to_floor, floor) @@ -132,11 +136,14 @@ def on_floor_changed(self): """ if self.destination_floor == self.callbacks.current_floor: + self.log("on change. Destiny %d reached" % self.destination_floor) self.callbacks.motor_direction = None + if self.orders[self.current_direction]: self.orders[self.current_direction].pop(0) else: self.orders[self.other_direction(self.current_direction)].pop(0) # something had to be served ( + if self.orders[self.current_direction]: next_destination = self.orders[self.current_direction][0].floor if next_destination != self.callbacks.current_floor: @@ -145,6 +152,17 @@ def on_floor_changed(self): self.orders[self.current_direction].pop(0) # drop it, already there self.destination_floor = None self.bounded_direction = self.current_direction + + # Inspect the other queue, if floor matches current floor, bind to that direction + # other_direction = self.other_direction(self.current_direction) + # if self.orders[other_direction]: + # floor = self.orders[other_direction][0].floor + # if floor == self.callbacks.current_floor: + # # Don't serve that + # self.orders[other_direction].pop(0) + # # self.bounded_direction = other_direction + # # pass + else: self.bounded_direction = self.current_direction @@ -206,17 +224,20 @@ def direction_str(direction): return "None" def status(self): - return """Current direction: %s - Current floor: %d - Destination floor: %d - orders UP: %s - orders DOWN: %s + return """\ + Current direction: %s + Current floor: %d + Destination floor: %d + Bounded direction: %s + orders UP: %s + orders DOWN: %s """ % (self.direction_str(self.current_direction), self.callbacks.current_floor, self.destination_floor, + self.direction_str(self.bounded_direction), self.orders[UP], self.orders[DOWN]) def log(self, msg): + # print "%s. \nstatus:\n%s" % (msg, self.status()) pass - # print "%s. status: %s" % (msg, self.status()) From f1d0859241de7051d02a8ca27ae8c568ec32e64a Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Tue, 26 Sep 2017 23:24:02 -0500 Subject: [PATCH 15/19] Up to line 485 --- elevator.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/elevator.py b/elevator.py index 148a567..acac8cd 100644 --- a/elevator.py +++ b/elevator.py @@ -99,15 +99,24 @@ def on_floor_selected(self, floor): self.log("missed the boat") return + # Check the other queue for duplicates + other_direction = self.other_direction(direction_to_floor) + if self.orders[other_direction]: + _floor = self.orders[other_direction][0].floor + if _floor == floor: + # Serve that, but not this floor request (line 485) + return + if self.bounded_direction: - self.log("floor selected. bounded direction detected. direction to floor %s" - % self.direction_str(direction_to_floor)) + self.log("floor selected. bounded direction detected. direction to floor %d: %s" + % (floor, self.direction_str(direction_to_floor)) + ) if direction_to_floor == self.bounded_direction: self.current_direction = self.bounded_direction self.bounded_direction = None else: self.log("floor selection ignored") - self.bounded_direction = None + # self.bounded_direction = None return if self.current_direction and self.current_direction != direction_to_floor: @@ -153,16 +162,6 @@ def on_floor_changed(self): self.destination_floor = None self.bounded_direction = self.current_direction - # Inspect the other queue, if floor matches current floor, bind to that direction - # other_direction = self.other_direction(self.current_direction) - # if self.orders[other_direction]: - # floor = self.orders[other_direction][0].floor - # if floor == self.callbacks.current_floor: - # # Don't serve that - # self.orders[other_direction].pop(0) - # # self.bounded_direction = other_direction - # # pass - else: self.bounded_direction = self.current_direction From 15bbaa7b064fc1342196a3e62921684ba9fcb6a2 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Wed, 27 Sep 2017 12:49:02 -0500 Subject: [PATCH 16/19] Up to line 498 --- elevator.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/elevator.py b/elevator.py index acac8cd..a0bd87d 100644 --- a/elevator.py +++ b/elevator.py @@ -69,6 +69,8 @@ def on_called(self, floor, direction): self.log("on called") def index(self, direction, floor): + if not direction: + return self.orders[direction].insert(0, self.Call(floor, time.time())) def sort(self, direction): From 49e8c2272bec8590bd8c15b12e564daa39111b67 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Wed, 27 Sep 2017 15:17:42 -0500 Subject: [PATCH 17/19] Up to line 566 sans fuzzy. --- elevator.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/elevator.py b/elevator.py index a0bd87d..ce78ee1 100644 --- a/elevator.py +++ b/elevator.py @@ -70,7 +70,7 @@ def on_called(self, floor, direction): def index(self, direction, floor): if not direction: - return + return self.orders[direction].insert(0, self.Call(floor, time.time())) def sort(self, direction): @@ -117,14 +117,28 @@ def on_floor_selected(self, floor): self.current_direction = self.bounded_direction self.bounded_direction = None else: - self.log("floor selection ignored") + self.log("floor selection ignored. Mismatch between bounded direction and direction to floor selected") # self.bounded_direction = None return if self.current_direction and self.current_direction != direction_to_floor: # Set it to wait for requests to move to the other direction - self.current_direction = self.other_direction(self.current_direction) - self.log("floor selection ignored") + other_direction = self.other_direction(self.current_direction) + self.current_direction = other_direction + self.log("""\ + floor selection ignored. + floor selected: %d + Direction to floor: %s. + Must wait for requests to move to the other direction""" + % (floor, self.direction_str(direction_to_floor))) + # Clear for the next call + if self.callbacks.current_floor == self.destination_floor: + self.log("Clear for the next call") + # Reverse again + other_direction = self.other_direction(other_direction) + if self.orders[other_direction] and self.orders[other_direction][0].floor == self.callbacks.current_floor: + self.orders[other_direction].pop(0) + self.current_direction = None return self.index(direction_to_floor, floor) From c040db946e28238bf3c7f206a7af07e275c5db8c Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Wed, 27 Sep 2017 15:42:26 -0500 Subject: [PATCH 18/19] Isolated fuzzy test. Some fixes. --- .gitignore | 1 + elevator.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 1513baa..922fb8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ MyElevator.py elevator.pyc +fuzzy.md \ No newline at end of file diff --git a/elevator.py b/elevator.py index ce78ee1..25165b7 100644 --- a/elevator.py +++ b/elevator.py @@ -181,7 +181,7 @@ def on_floor_changed(self): else: self.bounded_direction = self.current_direction - if not self.orders[self.current_direction]: + if self.current_direction and not self.orders[self.current_direction]: other_direction = self.other_direction(self.current_direction) if other_direction and self.orders[other_direction]: self.current_direction = other_direction @@ -241,8 +241,8 @@ def direction_str(direction): def status(self): return """\ Current direction: %s - Current floor: %d - Destination floor: %d + Current floor: %s + Destination floor: %s Bounded direction: %s orders UP: %s orders DOWN: %s @@ -254,5 +254,5 @@ def status(self): self.orders[DOWN]) def log(self, msg): - # print "%s. \nstatus:\n%s" % (msg, self.status()) + print "%s. \nstatus:\n%s" % (msg, self.status()) pass From 3e1766819e563145f20df849d434197f824a7c44 Mon Sep 17 00:00:00 2001 From: Ivan Rodriguez Date: Wed, 27 Sep 2017 17:50:23 -0500 Subject: [PATCH 19/19] Added further fuzzy checks. All tests passing. About 20s to run in a i7 mac --- elevator.py | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/elevator.py b/elevator.py index 25165b7..fe8581f 100644 --- a/elevator.py +++ b/elevator.py @@ -46,6 +46,11 @@ def on_called(self, floor, direction): direction: the direction the caller wants to go, up or down """ + if not self.valid_floor(floor) or direction not in [UP, DOWN]: + return + + + direction_to_floor = self.direction_to(floor) if self.current_direction is None: @@ -86,6 +91,10 @@ def sort(self, direction): else: pass + @staticmethod + def valid_floor(floor): + return floor >= 1 or floor <= FLOOR_COUNT + def on_floor_selected(self, floor): """ This is called when somebody on the elevator chooses a floor. @@ -95,6 +104,10 @@ def on_floor_selected(self, floor): floor: the floor that was requested """ + if not self.valid_floor(floor): + return + + direction_to_floor = self.direction_to(floor) if direction_to_floor is None: @@ -164,12 +177,13 @@ def on_floor_changed(self): self.log("on change. Destiny %d reached" % self.destination_floor) self.callbacks.motor_direction = None - if self.orders[self.current_direction]: + if self.current_direction and self.orders[self.current_direction]: self.orders[self.current_direction].pop(0) else: - self.orders[self.other_direction(self.current_direction)].pop(0) # something had to be served ( + if self.current_direction and self.orders[self.other_direction(self.current_direction)]: + self.orders[self.other_direction(self.current_direction)].pop(0) # something had to be served ( - if self.orders[self.current_direction]: + if self.current_direction and self.orders[self.current_direction]: next_destination = self.orders[self.current_direction][0].floor if next_destination != self.callbacks.current_floor: self.destination_floor = next_destination @@ -192,6 +206,17 @@ def on_floor_changed(self): if self.is_idle(): self.current_direction = None # Elevator is idle + if self.callbacks.current_floor <= 1 and self.callbacks.motor_direction == DOWN: + # self.callbacks.current_floor = 1 + self.callbacks.motor_direction = None + self.current_direction = None + self.bounded_direction = None + + if self.callbacks.motor_direction == UP and self.callbacks.current_floor == FLOOR_COUNT: + self.callbacks.motor_direction = DOWN + self.bounded_direction = None + self.destination_floor = FLOOR_COUNT + self.log("on_changed") def on_ready(self): @@ -200,6 +225,13 @@ def on_ready(self): Maybe passengers have embarked and disembarked. The doors are closed, time to actually move, if necessary. """ + + if self.destination_floor and not self.valid_floor(self.destination_floor): + self.destination_floor = None + self.callbacks.motor_direction = None + + + # print "on ready: dest floor: %d" % self.destination_floor if self.destination_floor > self.callbacks.current_floor: self.callbacks.motor_direction = UP @@ -208,6 +240,15 @@ def on_ready(self): else: self.bounded_direction = None + if self.callbacks.motor_direction == DOWN and self.callbacks.current_floor == 1: + self.callbacks.motor_direction = None + + if self.callbacks.motor_direction == UP and self.callbacks.current_floor == FLOOR_COUNT: + self.callbacks.motor_direction = None + self.bounded_direction = None + self.destination_floor = None + + self.log("on ready") def direction_to(self, floor): @@ -254,5 +295,5 @@ def status(self): self.orders[DOWN]) def log(self, msg): - print "%s. \nstatus:\n%s" % (msg, self.status()) + # print "%s. \nstatus:\n%s" % (msg, self.status()) pass