Assignment 10

Due Mar. 31

by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Simple multiple choice questions, 1 point each:

  1. In the reference solution to MP7, class Infection Rule begins this way:
    class InfectionRule {
        private final double median;    // median of the distribution
        private final double sigma;     // sigma of the distribution
        private final double recovery;  // recovery probability

    All the fields of an infection rule are private and final, set by the constructor! How do infection rules have an influence on the rest of the simulation?
    a) The class provides getter methods for the private fields.
    b) These fields have no influence outside this class.
    c) Outsiders just need the results of draws from these distributions. — correct
    d) Outsiders pass in lambda expressions that can access these fields.
    e) Subclasses of InfectionRule can access these fields.

    By inspection of the code, a) and d) are false. These fields clearly have an effect outside the class, so b) is false. Private classes cannot be seen by subclasses, and InfectionRule has no subclasses, so d) is false.

    The methods recover and duration do precisely what is described in c), they draw from the appropriate random distributions and return the result of that draw instead of exposing the parameters of that distribution.

  2. Here is a bit of Java code from Role.populateRoles in the reference solution to MP7:
    // the ratio inf/pop is probability this person is infected
    if (rand.nextFloat() < ((float)inf / (float)pop)) {
        p.infect( 0.0 );
        inf = inf - 1;
    pop = pop - 1;

    This is inside some loops that iterate over the list allRoles and over all the people in each role.

    a) Most of the infected people will be created early in the process.
    b) The distribution of infected people this creates is uniform. — correct
    c) Most of the infected people will be created late in the process.
    d) Shuffling allRoles first would improve the randomization.
    e) Creating people first and then shuffling them would make it more random.

    This always creates a uniform distribution because, if there are n people left to create and i people left to infect, the probability of the next person being infected is n/i -- By induction, each person has the likelihood of being infected that you would expect for a uniform distribution, so the entire distribution must be uniform.

    That argument explains why b) is preferred to a) and c). Shuffling the order people get created in, either by shuffling the roles first, option d), or by shuffling the list of people before selecting people to infect, option e), does nothing useful because an already uniform random distribution cannot be made more random by additional shuffling.

  3. In the reference solution to MP7, pk.populate(p,sched) adds a person and a schedule to a particular kind of place, and PlaceKind.distributePeople() finishes distributing people to their actual places. Why was putting people into places split up into two parts?
    a) To make it easier to associate people, places and schedules.
    b) To break correlations between people and their associated places. — correct
    c) To prevent places from becoming overpopulated.
    d) To isolate the tasks involved in different classes.
    e) To allow most fields of PlaceKind to be declared to be private.

    Splitting the problem made it harder, so a) is false. This has no impact on the number of people created, so c) is false.

    PlaceKind.distributePeople and pk.populate(p,sched) both refer to methods in class PlaceKind, so d) is false. Yes, the fields in PlaceKind are private, but splitting the work into two methods doesn't see to depend on this, so e) is false.

    Look at

    PlaceKind.distributePeople and note that the first thing it does for every kind of place is shuffle the list of all people associated with that kind of place. That shuffle breaks the correlation between people and the different places they are associated with, so b) is correct.

  4. Suppose p.doit( double t ) is a schedulable event service routine applicable to the object p at time t, under the simulation framework we have been using. The problem is, after this event was scheduled, circumstances changed and the time at which the event should occur has changed. To support this,
    a) we must change the time of the already scheduled event.
    b) we must schedule a new event and delete the old one.
    c) doit must check variables in p to see what to do.
    d) we schedule a new occurence of doit at the new time.
    e) both c) and d). — correct

    We can rule out options a) and b) because the problem is stated in the context of "the simulation framework we have been using." That framework has no mechanism for rescheduling or deleting events. You could add such a mechanism, but that would be a change of framework.

    If there is no mechanism to delete events, then the scheduled action will occur at the time originally scheduled. If that action is not supposed to happen then, the only way to prevent it is to have the action begin by checking some state variable(s) to see if anything should be done. So, c) is part of the answer.

    If we can't change the time of an already scheduled event and we want something to happen at a different time, our only alternative is to schedule a new event, so d) is part of the answer. Option e) is therefore correct.

  5. Think about arrivals and departures from places.
    a) When an uninfected person arrives at a place where someone is contagious, it never schedules an infection event for itself.
    b) When a contagious person arrives at a place, all that is needed is to increment a count of the number of contagious people there.
    c) When an uninfected person leaves a place, it must un-schedule any pending infection events for itself. — correct
    d) When a contagious person leaves a place, all it has to do is unschedule all pending infection events for that place.
    e) All of the above.

    When an uninfected person arrives in a place where someone is contagious, it probably has to predict when it will be infected based on the number of contagious people there. That sounds like the opposite of a).

    when a contagious person arrives at a place, it certainly sounds sensible to update the number of contagious people in that place, but this probably causes the infection events for everyone there to be rescheduled, which is more than b) suggests.

    When an uninfected person leaves a place, all the infection events scheduled for itself are now irrelevant because it is no longer in contact with the people in that place. Therefore, c) seems right.

    When a contagious person leaves a place, it makes sense to cancel all the contageon events that were a consequence of that person being there. However, if there are still contagious people there, these contageon events need to be rescheduled in terms of the number of contagious people remaining so d) is as wrong as b).

    Since some of the above are wrong, e) is wrong.