Lecture notes for Computer Science I by James Tam Return to the course web page

CPSC 231: Assignment 4

New Concepts to be applied for the assignment

Introduction

Computer simulations are re-creation of a real-world scenario. Simulations are important for many reasons including (but not limited to) safety and cost. Also simulations may assist in decision making by allowing alternatives to be tested before a course of action is chosen. For this assignment you are to create a simple simulation of the traffic on a one-way three-lane road. Cars enter the from the west and leave in the east. Areas outside of the road cannot be traversed by cars. Furthermore parts of the road may narrow (and widen). Potholes, which are also impassible, may randomly appear in the simulation.

At the start of the simulation the user can specify the duration of the simulation and the arrival rate of cars. Then the simulation will run on a turn by turn basis and display it's current before pausing and prompting the user to continue. When the simulation ends statistics will be displayed:

Features to implement

  • Displays an introduction at the beginning of the simulation just after it starts. The introduction should explain how the program works.
 
  • Displays a conclusion when the simulation ends. If your program generates statistics about the simulation then this information should be shown during the sign off.
 
  • The simulation is initialized with the starting positions provided in the resource file "traffic.py"
 
  • Show the current state of the simulation. The output should be reasonably neat and presentable so the user can understand what is currently happening with the simulation. The columns and rows should be labeled in the fashion shown the screenshot.
 
  • The simulation runs for a fixed number of turns. The basic version of the program will run the simulation for 20 turns. A slightly superior version will prompt the user for this information when the simulation is first started. At the end of each turn the program will show the current state of the simulation, and the program will pause until the user hits enter, before proceeding to the next turn.
 
  • Cars move in a west to east fashion (that means that cars farthest east move first)
 
 
  • Basic version: cars move 1 square per turn.
 
 
  • Cars only move onto empty squares (avoids impassible areas and potholes - if that feature is implemented).
 
 
  • When the square in front of a car is occupied it will try to move forward one square in a diagonal fashion (up/north is attempted before down/south).
 
  • Cars that reach the eastern most side are removed from the simulation.
 
  • Cars randomly appear in the three west-most (entry) squares: cars only appear in a particular square if that square is empty.
 
 
  • Basic version: 50% chance that a car will appear in a particular entry square. The probability is not the chance that a car will appear in one of the three entry squares. Instead there is a 50% chance for each square that a car will appear there.
 
 
  • Advanced version: at the start of the program the user is prompted to enter a probability. The probability should be entered in a percentage form e.g., 50.5  instead of 0.505. While prompting for the percent the program should inform that the user that he or she can use the default probability of 50% by entering a value outside of the range of 0 – 100%.
 
  • Cars can accelerate or stop when moving straight forward (east). Each car will move 0 – 3 squares when moving in a purely west to east direction. All squares between the source and destination must be empty. (If there is at least one obstacle between it’s current location and the destination then it won’t move). If a car is blocked from moving multiple squares then the program should first try to move single square forward and then if it's still blocked it will try moving diagonally. Cars that change lanes can only move one square.
 
  • At the end of each turn if the user types ‘d’ ‘or D’ and hits enter (instead of just enter) it will toggle debugging mode. When in debugging mode the program should display messages that reflect the current state of the program in order to help you debug it (e.g., a message appears when a function is called, a message displays the contents of variables). Generally the exact content of these messages is up to your discretion. But you should incorporate them into your program as you are creating it in order to test and debug it rather than just adding this feature at the very end. The minimum requirements for debugging mode is: the program should indicate that debugging mode is turned on when the city is displayed (no message appears if it’s off), feedback will be immediately shown as debugging mode is toggled. [Example] Other debugging messages must appear but their content is up to you.

 
  • There's a 25% chance that a pothole randomly appears on one of the empty squares. This probability is checked once a turn to see if one pothole appears in one of the empty squares. (It isn’t checked individually for each empty square). Squares with potholes are marked with an ‘X’. Once a pothole appears in the simulation it remains there until the end.
 
  • Simulation tracks and displays during the signoff information about the three statistics: duration, exits from the simulation, new entries into the simulation.
 

General structure of the program

Initialize world (the starting location of cars, empty spaces and inaccessible areas must be the same as my program)

While the simulation isn't finished: order of activities that occur during each turn

Submitting your work:

  1. Similar to the previous assignment you must create a README file that lists all the features that were implemented. Assignments (source code/'dot-py' files containing your Python program and your file README) must be electronically submitted according to [the assignment submission requirements].
  2. As a reminder, you are not allowed to work in groups for this class. Copying the work of another student will be regarded as academic misconduct (cheating).  For additional details about what is and is not okay for this class please refer to the following [link].
  3. Before you submit your assignment to help make sure that you haven't missed anything here is a [checklist] of items to be used in marking.

Online resources:

To help you see how the features work there are a few text files that show the output of my solution. They can be found in UNIX under: /home/231/assignments/assignment4.

Also I found a few online traffic simulations that you can run through your web browser:

And to show that simulations aren't just fun and games and can be a part of serious research: