Beanshell Scripting Appendix

Main Menu           Previous Topic                                                           Next Topic


File SPDynamicsExperiment.bsh

This Beanshell script was used to generate Figure 3 in Slide 4 of Topic 2 of this tutorial.

/*
* This script contains functions needed to run experiments to reproduce results from
* "Emergency response to a smallpox attack: The case for mass vaccination" by Kaplan, Craft,
* and Wein (submitted to the PNAS office directly).
*/

// deaths[i][j] is the number of deaths when the initial fraction of infected people is iE-3
//    and the basic reproductive ratio is approximately j
// To obtain the results in the article, set 0 < i < 10 and 0 < j < 20
deaths = new double[11][21];

/*
* This collects data for the SmallpoxDynamics experiment. To obtain the same data as in the
* paper, try the following:
* bsh% SPDExperiment( true, 0.008, 115 );
* to get a 3-D graph for the case of mass vaccination, or
* bsh% SPDExperiment( false, 0.03, 350 );
* for a run of the trace vaccination scenario.
* The experiments should take about 4.5 hours on a 1.0 GHz Windows machine
*
* massVaccination Set to true if you want the mass vaccination experiment to be performed
*     otherwise set to false if you would like the trace vaccination
* time-step Number of days in a single time step of the discrete experiment. If this value is
*     too high, the functions will be linearized over periods of time that are too large,
*     and there is danger of both an overflow and a result that deviates too much from the
*     true value. On the other hand, a time-step paramater that's too low will result in
*     exorbitant running times. SocietyFactory sets it to 0.005 for mass vaccination, and
*     to 0.03 for trace vaccination.
* numDays Number of days for the experiment to run. In the paper, the base case results (number
*     ) are reported for 350 days in the case of trace vaccination, and for 115 days
*     in the case of mass vaccination.
*/
void SPDExperiment( boolean massVaccination, double timeStep, int numDays )
{
    int numSteps = (int) ( (double)numDays / timeStep );
    double beta;
    double initInfected;
    double initSusceptible;
    double totalPopulation = 1E7;
    double r3 = 1.0 / 3.0;

    for (int i=0; i<11; i++ ) {
        for (int j; j<21; j++ ) {
            deaths[i][j] = 0;
        }
    }

    initSoc = SocietyFactory.createSmallpoxDynamicsSociety( massVaccination, timeStep );

    for (int i=0; i<11; i++ ) {
        for (int j=0; j<21; j++ ) {

            // don't run initSoc, make a clone to run so that you can reuse initSoc
            // in the future
            soc = (Society) initSoc.clone();


            //// set the values of beta and initial number of people infected according to i and j

            // set initial infected and susceptible populations
            initInfected = totalPopulation * (double)i * 1E-3;
            initSusceptible = totalPopulation - initInfected;
            soc.getTimeModel().alterKnowledge( initSusceptible, 0, 0 );
            soc.getTimeModel().alterKnowledge( initInfected, 0, 2 );

            // set beta
            beta = (double)j / (r3 * initSusceptible);
            edu.umich.si.infoelite.minischeme.Interpreter.evaluate(
                new MinischemeExpression( "(set beta " + beta + ")" ),
                soc.environment, soc.primitives );

            // now run the society for the appropriate number of steps
            soc.run( numSteps );

            // collect and record data - the current population of the dead state - agent 16
            deaths[i][j] = soc.getAgent(16).getKnowledge(0);

            // print out the result
            print( "i " + i + " : j " + j + " : deaths " + deaths[i][j] );
        }
    }
    print("Smallpox Dynamics Experiment complete!");
}

void writeSDResultsToFile() {
    str = "";
    for (int i=0; i<11; i++) {
        for (int j=0; j<21; j++)
            str += deaths[i][j] + " ";
        str+= "\n";
    }
    writeStringToFile( str );
}

                   Previous Slide                                                           Next Slide