Problem Set 4: Elevator Simulation
[Due March 24, 11:59pm]
Updated March 25. Per email list:
- You may assume there will be no more than 12 Sims.
- It is sufficient to load some IElevatorPlanner from user-selected dlls. It is not necessary to provide the user with a way of choosing between multiple IElevelatorPlanner implementations in a particular dll.
- During grading, Peter-Michael discovered a bug in the spec. There's no direct way for the Elevator control to discover when an elevator is full. This means that a full elevator will---with many otherwise reasonable controllers---wait, doors open, on a floor with many sims who would like to get on but won't fit. This should be corrected for future incarnations of the assignment.
In this problem set you will simulate elevators in a building.
As described below, this assignment will use dynamic code loading to allow different programs to control elevators, and to generate simulated riders.
While this assignment is divided into three parts for pedagogical purposes. It is not necessarily beneficial to work the problem set in precisely this order.
Overview
In this problem set, you will be modeling a building with several elevators and floors.
The simulation occurs in a building. You need to track what happens to Sims (simulated people) who ride elevators to different floors in the building. For simplicity we will fix the building size at 5 floors, and assume that there are 2 elevators. For convenience we will label the floors 0-4 and the elevators 0 and 1.
The simulation is turned-based. On every turn three things occur in the following order.
- The elevators move according to their current state.
- Sims move.
- The elevator controller updates elevator state.
Sims
Sim states Sims can be in one of two states: Idle or GoingTo(n). They have the following meanings.
- GoingTo(n): The Sim wants to go to floor n.
- Idle: The Sim is resting between elevator rides.
Sims transition between states as described by the following diagram.
At simulation start, Sims have state Idle.
Sim position Every Sim has a current location. The set of possible locations for a Sim is
{Elevator 0, Elevator 1, Floor 0, Floor 1, ... , Floor 4}.Initially, every Sim has position Floor 1. (Floor 0 is the basement.)
We say a Sim is riding elevator i when it has position Elevator i. We say a Sim is on floor f when it has position Floor f.
Sim scripting Each Sim stores a script describing the trips that it wishes to take. A Sim script has the form of a sequence of pairs of integers:
(t1,f1); (t2,f2); (t3,f3); …We call each pair an instruction. The pair (t1,f1) indicates that at time t1 the Sim wishes to travel to floor f1.
As described below, the Sim will take the trip described by (ti,fi) before the trip described by (ti+1,fi+1). This is true even when ti+1 < ti.
The definition of Sim scripts suggests a simple file format to
representation populations of Sims. A Sim script file is text file which has
the form
(t11, f11); (t12, f12); (t13, f13) ...
where each line represents the script for a particular Sim. You will need to
be able to configure the Sims in your simulator by reading Sim scripts.
(t21, f21); (t22, f22) ...
(t31, f31); (t32, f32); (t33, f33) ...
Sim movement Sims move according to a set of rules.
Consider Sim S with current state state, position p and instruction script I. Assume the simulation is at time T.
- If state is Idle, then
- If I is empty, then do nothing.
- If I has form
(t0,f0);I′, then
- If T < t0 then do nothing.
- If T ≥ t0 then set state to GoingTo(f0) and set I to I′.
- If state is GoingTo(f), then
- If p = Floor f, then set state to Idle.
- If S is riding e and e is on floor f in elevator state Open, then set p to Floor f.
- If p = Floor f0 (≠ Floor f), then
- Let d = Down if f < f0, otherwise let d = Up.
- If elevator e is on floor f0, with state Open, indicator light d, and fewer than 5 riders, then set p = Elevator e.
Note that slow elevator service may cause a Sim to acquire a backlog of instructions. The rules above ensure that all Sim script instruction are executed in order; that is, Sims don't skip elevator trips, even when behind schedule.
Elevators
Simulated elevators move between building floors, carrying riders. We will model elevators using several piece of state.
Elevator height Every elevator has a current height measured in feet. Height is always an integer value between 0 and 40. When an elevator's height is multiple of 10, we will say the elevator is on floor height/10. For example, an elevator with height 30 is on floor 3. At simulation start, all elevators have height 0.
Elevator actions An evaluator may be in one of four states: Open, Stopped, GoingUp, GoingDown. During at turn, the elevator's state may have an effect as described below. No action may cause an elevator's height to leave the range [0,40].
- Open: Sims may enter or exit the elevator.
- GoingUp: the elevator's height increases at a rate of 5 feet/turn.
- GoingDown: the elevator's height decreases at a rate of 5 feet/turn.
- Stopped: no effect.
Only certain sequences of elevator actions are legal. In particular, an elevator cannot open it's doors immediately after moving. (That would be dangerous!) An elevator can change from state s to state s' only when indicated the following state transition diagram, by an edge from s to s'. For instance an elevator in state GoingDown may change to state Stopped, but not to state Open or GoingUp.
Elevators begin in state Stopped.
Riders Each elevator may hold up to 5 Sims who are called the elevator's riders.
Indicator Light Each elevator displays a light which is in one of the following states: Up, Down, None. As described below, Sims will consider an elevator's light when deciding whether to become riders.
You may represent elevators in any way you like, subject to the design principles discussed in class.
Elevator Planning
It would be possible to let elevators independently decide which stops
to make. However it's better to provide centralized control to coordinate
elevators movement.
The simulator will use a single
method to select actions for the entire set of elevators. This method, called
PlanStep
, is specified by the following interface.
interface IElevatorPlanner{
//PlanStep(controls, upFloors, downFloors)
//Requires controls[e] corresponds to Elevator e
//Requires upFloors[i] true iff a Sim on floor i is in
// state GoingTo(j) with j > i
//Requires downFloors[i] true iff a Sim on floor i is in
// state GoingTo(j) with j > i
//Updates controls.
void PlanStep(IElevatorControl[] controls,
bool[] upFloors, bool[] downFloors);
}
PlanStep
should use methods of the objects in the
controls
array to effect the outside world.
PlanStep
may keep and use internal state to help make decisions,
but must not interact with simulation except though this interface.
Intuitively, the upFloors
and downFloors
arrays
model the buttons on each floor, pressed by Sims waiting for elevators.
PlanStep
would be better defined as a delegate. We
do not do so here to simplify the use of reflection described below.
The IElevatorPlanner
interface depends a class for controlling
elevators, and two associated enums. These are defined as follows.
enum ElevAction { Open, Stopped, GoingUp, GoingDown };
enum ElevLight { Up, Down, None };
Interface IElevatorControl{
//StopRequested(i) returns true iff some elevator rider
//is in state GoingTo(i).
//Checks i in [0..4].
bool StopRequested(int i);
//CurrentState is elevator's current state.
ElevAction ActionState {get; }
//CurrentState is elevator's current state.
//Checks that ActionState -> is a valid transition.
void SetAction(ElevAction s)
//Light State is the current state of the elevator's light
Light Indicator {get; set;}
//Height is the current height of the elevator
int Height {get;}
}
What to turn in
Write a graphical user interface that shows the current state of the simulation, and that can be used to animate simulation steps. (Hint: The System.Windows.Forms.PictureBox provides one useful way to display images.) Your interface should include the following functionality:
- A user may hover their mouse over an elevator or Sim to see its current state. (Hint: See System.Windows.Forms.ToolTip.)
- The user must be able to advance the simulator by 1, 10, or 50 steps.
- The user must be able to configure the simulator by,
- using reflection to load a dll file containing an implementation of
IElevatorPlanner
that run the elevators, and - loading a file of Sim scripts to configure Sims.
- using reflection to load a dll file containing an implementation of
Your submission must contain one or more new implementation of
IElevatorPlanner
.
To help you get started, we have provided a solution file with three projects:
- ElevatorPlanner: A project containing the
IElevelatorPlanner
interface. - SampleElevatorPlanners: Two simple sample elevator planners.
- ElevatorSimulation: A Windows Form application to base your simulation on.
More details can be found in the readme.txt file provided with the starter solution. Feel free to base your submission on this code.
Challenge Problem: Animation
Animate the elevator simulator so that the user can watch elevator steps occur at constant rate.