Beanshell Scripting Appendix

Main Menu           Previous Topic                                                           Next Topic


File SPDynamicsSingleRun.bsh

This Beanshell script was used to generate Figures 1 and 2 in Slide 3 of Topic 2 of this tutorial.

/*
* This will run a single SmallpoxDynamicsSociety and collect the most important
* data, such as the number of people on the queue, the total infected population,
* etc.
*/

// define the arrays that will hold the values
// after defining numDays - the variable which tells over how many days the data
// will be collected
// e.g. bsh% numDays = 115;
infected = new double[numDays];
queued = new double[numDays];
immune = new double[numDays];
dead = new double[numDays];

// to run the base case from "Smallpox Attack," run
// bsh% SPDSingleRunExperiment( true, 0.005, 0.0000001, 1000 );
// to record results for mass vaccination, and
// bsh% SPDSingleRunExperiment( false, 0.03, 0.0000001, 1000 );
// for trace vaccination
void SPDSingleRunExperiment( boolean massVaccination, double timeStep, double beta,
    double initInfected )
{
    double stepsPerDay = 1.0 / timeStep;
    double totalPopulation = 1E7;
    double initSusceptible = totalPopulation - initInfected;

    for (int i=0; i        infected[i] = 0;
        queued[i] = 0;
        immune[i] = 0;
        dead[i] = 0;
    }

    soc = SocietyFactory.createSmallpoxDynamicsSociety( massVaccination, timeStep );

    //// set the values of beta and initial number of people infected

    // set the initial numbers of infected and susceptible individuals
    soc.getTimeModel().alterKnowledge( initSusceptible, 0, 0 );
    soc.getTimeModel().alterKnowledge( initInfected, 0, 2 );

    // set beta
    edu.umich.si.infoelite.minischeme.Interpreter.evaluate(
        new MinischemeExpression( "(val beta " + beta + ")" ),
        soc.environment, soc.primitives );

    // now run the society for the appropriate number of steps and collect data
    double step = 0.0;
    int day = 0;
    while (day < numDays) {
        infected[day] = soc.getAgent(2).getKnowledge(0) +
            soc.getAgent(3).getKnowledge(0) +
            soc.getAgent(4).getKnowledge(0) +
            soc.getAgent(5).getKnowledge(0) +
            soc.getAgent(7).getKnowledge(0) +
            soc.getAgent(8).getKnowledge(0) +
            soc.getAgent(9).getKnowledge(0) +
            soc.getAgent(14).getKnowledge(0) +
            soc.getAgent(10).getKnowledge(0) +
            soc.getAgent(11).getKnowledge(0) +
            soc.getAgent(12).getKnowledge(0) +
            soc.getAgent(13).getKnowledge(0);
        queued[day] = soc.getAgent(6).getKnowledge(0) +
            soc.getAgent(7).getKnowledge(0) +
            soc.getAgent(8).getKnowledge(0) +
            soc.getAgent(9).getKnowledge(0);
        immune[day] = soc.getAgent(15).getKnowledge(0);
        dead[day] = soc.getAgent(16).getKnowledge(0);

        print("Day " + day);
        print("infected " + infected[day]+ " : queued " + queued[day] +
            " : immune " + immune[day] + " : dead " + dead[day]);

        // this ensures that we run the society for an integer number of steps every time,
        // yet from time to time we catch up, and run an extra step to account for the fact
        // that in general, the number of steps in a day is not whole
        soc.run( (int)(step + stepsPerDay) - (int)step );
        step += stepsPerDay;
        day++;
    }

    print("Smallpox Dynamics Experiment complete!");
}

// after running the experiment call to write results to a file:
void writeSDSRResultsToFile() {
    str = "infected queued immune dead\n";
    for (int i=0; i    str += infected[i] + " " + queued[i] + " " + immune[i] + " " + dead[i] + "\n";


    writeStringToFile( str );
}

                   Previous Slide                                                           Next Slide