Problem Set 4: Elevator Simulation
[Due March 24, 11:59pm]

Updated March 25. Per email list:

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.

  1. The elevators move according to their current state.
  2. Sims move.
  3. 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.

Sims transition between states as described by the following diagram.

Sim state 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) ...
  (t21, f21); (t22, f22) ...
  (t31, f31); (t32, f32); (t33, f33) ...
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.

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.

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].

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.

Elevator state diagram

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:

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:

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.