
So You Think You Can Program an Elevator - mshang0
https://github.com/mshang/python-elevator-challenge/blob/master/README.md
======
noir_lord
Very similar immediately playable JavaScript version.

[http://play.elevatorsaga.com/](http://play.elevatorsaga.com/)

Which I'm now closing as I lost too much time to this the first time I saw it.

~~~
kilroy123
This is fantastic. Are there other good programming challenge / games like
this? I'd love to see a collection of other similar games.

~~~
jvolkman
Robocode is another one:
[http://robocode.sourceforge.net/](http://robocode.sourceforge.net/)

~~~
kiliankoe
I spent way too much time with Robocode a few years back. Those advanced
"surfing" bots some people built though... It was quite disheartening having
not the slightest bit of a chance as a novice programmer.

------
phasmantistes
This is both cool and disappointing. I'm sure this is more challenging than it
looks, and it's cool to see emergent complexity from a (seemingly) simply set
of conditions to satisfy.

However, the reason I think about elevator programming isn't to make an
elevator behave the way they do today. It's to make an elevator behave
_better_ than they do today.

For example:

* I don't want an elevator to close and reopen its doors on the same floor. That's horrible user experience.

* All the interesting problems arise when there are multiple elevators servicing the same set of floors. So let's control more than one elevator at the same time.

* If the elevator is on the ground floor, and has downward calls on both the 1st and 20th floors, then it is likely more efficient to service the 1st floor first rather than bypassing them just so you can keep travelling in the same direction.

I'd like to see a version of this that, rather than enforcing arbitrary rules,
just has a lot of series of inputs and lets people compete to service those
inputs in the shortest number of steps.

~~~
neil_s
THIS.

However, the criterion you're optimizing for is still unclear. Should it be:
a) the total number of steps the elevator takes b) the average number of steps
each user has to wait c) the maximum number of steps each user has to wait d)
the 90th percentile of user waiting steps

Note: here, I'm counting the steps spent waiting for an elevator as well as
the steps spent in the elevator till the destination as the same, but
presumably you'd want to count steps spent inside the elevator as costlier,
further complicating matters.

And this not even considering what happens if your algorithm answers 20 calls,
without going to any destination floors, when the elevator capacity is only 10
people.

That's what makes this such an interesting problem.

~~~
friendzis
This can be accomplished in a gamified way: Game harness simulates people
comming to an elevator, pressing destination button and waiting for some time
(some wait longer, some wait shorter, should also depend on distance) before
giving up and taking stairs. The elevator could or could not know about it's
capacity. Assigning "price", optionally differentiated by input/output floors,
to passenger, result would be €/time or €/level. Level - predetermined
sequence of passengers.

Interesting thing here is that "price" does not have to depend on distance.
Taking passengers travelling short distances might look cost effective, but
that could have negative effect on number of people going to a 20th floor.

------
arnarbi
This was was posed as the the first homework assignment in my first undergrad
CS class (15 years ago). I don't think a single person managed to do it.

It's a classic example of a problem that you think is appropriate to throw at
new programmers but find out it goes terribly wrong because the hard part is
designing a suitable state machine first. If you just start coding without a
full model that covers all the edge cases, it quickly degenerates to a mess of
ad-hoc heuristics and spaghetti.

~~~
nosequel
Same exact thing, about 16 years ago for me. He presented it (I think) to see
who procrastinated on the project, knowing it'd take way longer than it
sounded.

~~~
philsnow
Same as me, 16 years as well. Was this kind of assignment in vogue at the
time, discussed between professors / grad students at academic conferences?

My assignment involved writing assembly on some virtual machine with a weird
byte/word size and dozens of addressing modes, as well as a (virtual) tape
device. I think it was called CUSP but I can't find any references for it now.

------
clarkmoody
A project during my graduate work was to solve this problem. You can read my
write up, which includes pseudo-code for the best algorithm I found[1].

Long story short: a simple, rules-based algorithm defeated my best efforts at
having multiple elevators working together to coordinate their action.

[1]:
[https://clarkmoody.com/Moody_AgentBasedElevatorControl.pdf](https://clarkmoody.com/Moody_AgentBasedElevatorControl.pdf)

~~~
wodenokoto
This looks really funny. Is it a spoiler if I haven't done the posted link
yet? If so I'll read it later :)

~~~
clarkmoody
Not related to the posted link, so no spoilers :-)

------
n0us
Then there is the alternative system where you do not request UP or DOWN but
you request the floor, otherwise known as destination dispatch
[https://en.wikipedia.org/wiki/Destination_dispatch](https://en.wikipedia.org/wiki/Destination_dispatch)

which introduces a whole new set of complexity.

~~~
Zikes
I think I read once that when elevators still had operators, larger buildings
would have one or more dispatch agents in the lobby that would line people up
at the appropriate elevator for the range of floors they intended to visit.

~~~
The_Magistrate
Taller buildings with complex and highly trafficked elevators still do this
only using electronics. Employees swipe their badges at a podium which reads
what floor they work on and indicates which elevator they should proceed to.
The system can intelligently schedule the elevator for that person to use
knowing where their destination is.

~~~
caf
I'm in a building with a mere nine floors that has such a system (the only
buttons in the elevators themselves are door open, door close and alarm). I
imagine that the technology will increasingly appear in the smaller buildings
as their elevators are built or refurbished.

~~~
Raticide
What if you need to visit someone on another floor?

~~~
caf
There's touchscreens in the lift lobbies where you can select an override
floor before scanning your pass.

------
tn13
When I was teaching assistant at University for Real Time Safety Critical
Systems class this was one of the star assignments in the class.

Instead of writing just a dummy program we built an actual elevator using
servo motors and asked students to code using different logic

1\. Single elevator 2\. Double elevator 2\. Double elevator with various kinds
of optimizations (reduce the energy used v/s reduce the time for each
passenger).

All had to be done using embedded C on an ATMEGA processor.

Then we let students do the exact same coding using Esterel where you
essentially represent everything as the state diagram.

It was so much fun for bother teachers as well as students because when you
deal with real hardware suddenly the scope of problems goes beyond merely
getting the logic right.

------
timberburn
Over a phone interview with Segment.io, I was asked to program an elevator
controller. It was described to perform like an HTTP server would handle
requests... They were getting confused when I started writing code for a state
machine. Glad I got a job offer somewhere else.

------
brk
On the topic of elevators, opening this link will cause you to (happily) lose
an hour of productivity:
[https://m.youtube.com/watch?v=1Uh_N1O3E4E](https://m.youtube.com/watch?v=1Uh_N1O3E4E)

~~~
anotheryou
thx.

An elevator once dropped me one and a half floors with open doors (both, floor
station and cabin doors). I think the cable slipped over the transport wheel.
Felt funny in the stomach, but we all kept standing when we hit the end-stop
in the pit. "safest way of travel" huh!

------
mattlong
[http://codelift.org/](http://codelift.org/) is an elevator challenge a friend
of mine[0] built that'll throw scenarios of various numbers of floors and
elevators at you.

[0]:
[https://news.ycombinator.com/user?id=silvamerica](https://news.ycombinator.com/user?id=silvamerica)

------
prutschman
I have a pet theory that Sim Tower came out of someone playing around with
elevator algorithms for fun and then turning that into a game.

~~~
cridenour
Not a theory, reality.
[https://en.wikipedia.org/wiki/SimTower#Gameplay](https://en.wikipedia.org/wiki/SimTower#Gameplay)

~~~
jcsnv
Awesome, my favorite game when I was younger.

------
exelius
No, I never did really. Programming an elevator sounds very tedious, but it
never sounded very hard. Elevators are heavy equipment, and there are edge
cases around safety near heavy equipment: I'd rather not have to learn those
first-hand.

However, I do think this is a great exercise for people new to programming.
It's a common enough scenario that most people can recite 80% of the
requirements of an elevator from memory.

------
valgaze
There's an old legend in the construction world that the fastest way to reduce
complaints about slow elevators in an office building is to add mirrors to the
doors & hallways. The tenants (allegedly) spend so much time looking at
themselves that they'll overlook an inefficient elevator system

------
nekopa
Nice. To the author, is there anyway to change the simulation to try
programming 2 elevators in the same shaft (
[http://www.thyssenkruppelevator.co.uk/new-
installation/eleva...](http://www.thyssenkruppelevator.co.uk/new-
installation/elevators/passenger-elevators/twin/) ) or Double-decker elevators
( [https://en.wikipedia.org/wiki/Double-
deck_elevator](https://en.wikipedia.org/wiki/Double-deck_elevator) )?

Because, you know, I really have no work I need to accomplish this year...

------
kneel
I've always wanted to see the option to deselect a floor if you press a button
twice. Is there a good reason why this doesn't happen?

~~~
amorphid
One thought comes to mind. What if the button light is not working? Hit the N
button once, and you are on your way to floor N. But if the light is busted,
you would hit the light again, and now you have deselected your destination. I
think button presses should be indempotent for this reason alone.

~~~
Skunkleton
This is why the standard practice of picking up the elevator phone and
requesting a floor cancellation exists.

~~~
jandrese
Seriously? Has anybody reading this ever done this? It seems to me that by the
time the operator picks up and you tell them what you're trying to do and what
elevator you are on it would be too late.

------
aaronkrolik
This is a lot like an assignment for my operating systems class to introduce
threads/lock (each elevator and rider is a thread).

[https://users.cs.duke.edu/~chase/cps310/lab3-elevator.html](https://users.cs.duke.edu/~chase/cps310/lab3-elevator.html)

~~~
monksy
Nice! They shoot for automated grading. I wish I had that when I was a TA.

------
southphillyman
This was one of my white board exercises during the interview for my current
job. I found it interesting because it seems very easy when the question is
asked but it allows for a lot of communication between the interviewer and
interviewee as the edge cases are fleshed out.

------
mrfusion
I think traffic signal optimization would be another neat game.

------
phibit
This is great! Many times I've thought it would be cool to develop a
programming logic harness for elevator logic, but I've never come close to
starting.

I think many developers have been waiting for an elevator thinking "Damn this
thing is DUMB! I could do a way better job coding the logic for this..." Can
finally test it out.

------
danbolt
Elevators are a great exercise to get people started in writing organized
programs. My university's introductory assembly class had us do it in ARM, and
it really taught me a lot about being structured. I'm happy to see this sort
of stuff on the front page.

------
lotask
Wow, how thoughts can be materialised easily. I was thinking about elevator
simulation for the past year, but didn't get into implementing anything..
Think this is a good challenge for me to start doing this. Nice!

------
chrissnell
This post reminded me of this classic Apple //c commercial:

[https://www.youtube.com/watch?v=B3rKBw5ZNL4](https://www.youtube.com/watch?v=B3rKBw5ZNL4)

------
andmarios
Usually elevators are controlled by PLCs which can be programmed using ladder.

If you know a bit about boolean algebra, it is easy to learn ladder and
program an elevator the real way. :)

------
joelthelion
Someone should make a real contest (maybe a Kaggle contest), with real data
and all. With well-chosen metrics I think it could be a lot of fun and a great
success.

------
flippyhead
Maybe an elevator but I very much doubt a Wonkavator!

~~~
n0us
I actually wonder how the space and time complexity of the algorithm would
increase as the number of possible dimensions of travel increases.

------
platz
They've removed the elevator buttons inside the carriage at my work; now one
can only select a single floor from outside the elevator.

~~~
ben_bai
The buttons are still there, just hidden under a cover. If not your building
has a serious fire safety problem.

------
cranium
I can't exactly remember why I found it, but there is a book about this very
topic! _Elevator Traffic Handbook_ , by Gina Barney

------
avodonosov
Good challenge. I've thought myself many times what would be elevator logic,
and it's not trivial.

------
mrfusion
This would be fun as an html5 game. I'm not sure how you'd enter the logic
though.

------
yeukhon
Elevator and vending machine are two good examples for learning about state
machine.

------
swehner
Funny they refer to it as "business logic."

------
sfall
what no elevator recall?

------
gsam
>So You Think You Can Program an Elevator

No, especially not in practice. I would never proclaim to have such a belief
unless it was my job to do so. Even then, I wouldn't be convinced I was doing
a good job of it. The amount of unforeseen complexity in most systems is no
joke.

