Assignment 9

Due Oct 22, on line

Part of the homework for CS:2820, Fall 2020
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Simple multiple choice questions, 1 point each:

  1. Here is a bit of Java code from class Workplace in the reference solution to MP6 (minus comments):
    private void open( double time ) {
        System.out.println( this.toString() + " opened at time " + time );
    
        Simulator.schedule(
            time + 8 * Simulator.hour,
            (double t)-> this.close( t )
        );
    }
    

    The time units used in this program are declared in ...
    a) class Time
    b) class Simulator.Time
    c) class Simulator
    d) class Workplace
    e) each class that needs to use time units

  2. Here is a bit of Java code from class Epidemic in the reference solution to MP6 (minus comments):
    Simulator.schedule( endTime, (double t)->System.exit( 0 ) );
    

    This is rather stupid, it just kills the program, while really, we should replace the call to System.exit( 0 ) with a call to a routine to wrap up the simulation and print out a final report of the results. Which of the following would be the best replacement for the above line?
    a) Simulator.schedule( endTime, (double t)->wrapUp( t ) );
    b) Simulator.schedule( endTime, (double t)->this.wrapUp( t ) );
    c) Simulator.schedule( endTime, (double t)->Simulator.wrapUp( t ) );
    d) Simulator.schedule( endTime, (double t)->wrapUp() );
    e) Simulator.schedule( endTime, (double t)->this.wrapUp() );

  3. In both the vehicle and epidemic simulations, we have seen sequences of events where each event deterministically causes the next in sequence: Workplace openings scheduled closings that schedule openings for ever and ever until the end of time. Source intersection production events each schedule the next production event which schedules the next forever until the end of time. Stop-light change events schedule the next light-change event. All of these are examples of:
    a) Simulated threads
    b) Simulated processes
    c) Simulated methods
    d) Simulated classes
    e) Simulated lambda expressions

  4. In the current version of the Epidemic simulator, log-normal distributions are used in several places, derived from an instance of class Random created in buildCommunity. It would be better to add class MyRandom with a new added method:
    a) nextLogNormal()
    b) nextLogNormal( mean )
    d) nextLogNormal( median )
    c) nextLogNormal( mean, scatter )
    e) nextLogNormal( median, scatter )

  5. Consider a method in class Person called travelTo(pl, t) that causes a person to leave their current place and travel to place pl, with the trip taking time t. Assume that all instances of Person have place, a variable indicating where they are now. Assume that places have arrive(per) and depart(per) methods to signal the arrival and departure of person per at that place. travelTo ...
    a) does this.place.depart(this) then does pl.arrive(this)
    b) schedules this.place.depart(this) at time+t then does pl.arrive(this)
    c) does this.place.depart(this) then schedules pl.arrive(this) at time+t
    d) schedules both this.place.depart(this) and pl.arrive(this) at time+t
    e) schedules pl.depart(this) at time+t then does this.place.arrive(this)

Machine Problem 7 -- due Monday, Oct 26

a) Add class MyRandom to to your epidemic simulator, and fix all use of randomness in the simulator to properly use it. This includes moving the generation of log-normal distributions into MyRandom.

b) Make places keep track of their occupants, with methods arrive(p) and depart(p) to signal the arrival and departure of person p at that place.

c) Add a method travelTo(p, t) for each person that that causes that person to leave their current place and arrive at place p at time t at that time.

d) For each employee, make them travel to their workplace in the morning leaving home 25 minutes before the workplace opening time. Note that all workplaces currently have the same opening time, but this could change in the future, so each workplace should have its own opening time attribute. Travel times to work should be log-normally distributed, with a median travel time of 20 minutes and a scatter of 3 minutes (yes, a fraction of the workers will be late for work).

e) For each workplace, when that workplace closes, all employees should be sent home. The travel time home will follow the same rules as the travel time to work.

f) Cut out all unnecessary debugging output, except, for each employee, make the simulation report all arrivals and departures of people from places and the times at which they occur.

A student asked: The place.arrive(person) and place.depart(person) methods do not have a time parameter, so they cannot be called from Simulator.schedule().

This is correc. Not all methods are scheduled at future times; arrive and depart make immediate changes to the state of the model. They are designed to be called from methods of class Person (or anywhere else) that are scheduled.

A student asked: Do you want us to use arrive and depart to ...

As I said in lecture, I have deliberately left many design decisions to you in this assignment.

A student asked: What should the output look like.

The assignment said to delete extraneous output, so all we need to see is the employees commuting to and from work. Remember, all the output you are producing up to this point is just debugging output so you can convince yourself and us that the simulator is working, at least as far as it goes. Nobody wants to wade through reams of extraneous debug output.

It even makes sense to simplify the to-string methods of people and places to their default form. I simply commented out about 4 lines of code in my solution so I could focus on the output that told me about commuters.

A student asked: Can we change arrive(p) and depart(p) to arrive(p,t) and depart(p,t) where p is the person arriving or departing this place and t is the time at which this happens?

Yes. This makes sense, particularly in light of the output required in item f) above.