Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/notebooks/chap03.ipynb
Views: 531
Modeling and Simulation in Python
Chapter 3
Copyright 2017 Allen Downey
More than one State object
Here's the code from the previous chapter, with two changes:
I've added DocStrings that explain what each function does, and what parameters it takes.
I've added a parameter named
state
to the functions so they work with whateverState
object we give them, instead of always usingbikeshare
. That makes it possible to work with more than oneState
object.
And here's run_simulation
, which is a solution to the exercise at the end of the previous notebook.
Now we can create more than one State
object:
Whenever we call a function, we indicate which State
object to work with:
And you can confirm that the different objects are getting updated independently:
Negative bikes
In the code we have so far, the number of bikes at one of the locations can go negative, and the number of bikes at the other location can exceed the actual number of bikes in the system.
If you run this simulation a few times, it happens often.
We can fix this problem using the return
statement to exit the function early if an update would cause negative bikes.
Now if you run the simulation again, it should behave.
Comparison operators
The if
statements in the previous section used the comparison operator ==
. The other comparison operators are listed in the book.
It is easy to confuse the comparison operator ==
with the assignment operator =
.
Remember that =
creates a variable or gives an existing variable a new value.
Whereas ==
compares two values and returns True
if they are equal.
You can use ==
in an if
statement.
But if you use =
in an if
statement, you get an error.
Exercise: Add an else
clause to the if
statement above, and print an appropriate message.
Replace the ==
operator with one or two of the other comparison operators, and confirm they do what you expect.
Metrics
Now that we have a working simulation, we'll use it to evaluate alternative designs and see how good or bad they are. The metric we'll use is the number of customers who arrive and find no bikes available, which might indicate a design problem.
First we'll make a new State
object that creates and initializes additional state variables to keep track of the metrics.
Next we need versions of bike_to_wellesley
and bike_to_olin
that update the metrics.
Now when we run a simulation, it keeps track of unhappy customers.
After the simulation, we can print the number of unhappy customers at each location.
Exercises
Exercise: As another metric, we might be interested in the time until the first customer arrives and doesn't find a bike. To make that work, we have to add a "clock" to keep track of how many time steps have elapsed:
Create a new
State
object with an additional state variable,clock
, initialized to 0.Write a modified version of
step
that adds one to the clock each time it is invoked.
Test your code by running the simulation and check the value of clock
at the end.
Exercise: Continuing the previous exercise, let's record the time when the first customer arrives and doesn't find a bike.
Create a new
State
object with an additional state variable,t_first_empty
, initialized to -1 as a special value to indicate that it has not been set.Write a modified version of
step
that checks whetherolin_empty
andwellesley_empty
are 0. If not, it should sett_first_empty
toclock
(but only ift_first_empty
has not already been set).
Test your code by running the simulation and printing the values of olin_empty
, wellesley_empty
, and t_first_empty
at the end.