[ create a new paste ] login | about

Link: http://codepad.org/Qkk9dJkH    [ raw code | output | fork ]

Perl, pasted on Oct 28:
#SIR based disease spread model in perl..

#!/usr/bin/perl

#use 5.10.1;
use strict;
use warnings;
use Data::Dumper;
use Storable qw(dclone);

use vars qw/$in  %population %population_copy $infectivity /;

#Initialize variables and parameters
my $NUM_IND = 500;
my $CONTACT_RATE = 5;
my $INFECTIOUS_PERIOD = 4;
my $LATENT_PERIOD = 2;
my $INFECTIVITY = 0.1;
my $DURATION = 50;

#Age groups for homework

#my $CR_agegrp_1 = 9;
#my $inf_agegrp_1 = 0.17;

#my $CR_agegrp_2 = 7;
#my $inf_agegrp_2 = 0.16;

#my $CR_agegrp_3 = 6;
#my $inf_agegrp_3 = 0.13;

#my $CR_agegrp_4 = 4;
#my $inf_agegrp_4 = 0.12;


#Add parameters to population hash
 

#this runs the loop until it reaches the population (num_ind)
for(my $i = 0; $i< $NUM_IND; $i++) 
{
	$population{$i}{'infstate'} = 0; #Infection states: S=0, L=1 I=2, R=3
	$population{$i}{'age'} = int(rand(80));
	$population{$i}{'dayOfInf'} = 0;
}

#Infect some individuals
for(my $i = 0; $i < 5; $i++) 
{
	$in = int(rand($NUM_IND));
	$population{$in}{'infState'} = 2;

}

#Print SIR before start of simulation
my @SIR = (0,0,0,0);


foreach my $person (keys %population) 
{
	$SIR[$population{$person}{'infState'}]++ if defined $population{$person}{'infState'} ;
}


#Shows what the initial parameters are
print "Before initiation\n";
print "SUS: ".$SIR[0]."\tLAT: " .$SIR[1]. "\tINF: ".$SIR[2]."\tREC: ".$SIR[3]."\n";

#Create clone of population for the calculations
for(my $day = 0; $day < $DURATION; $day++) 
{
	#Create temporary copy
	my %population_copy = %{dclone(\%population)};

	#generate contacts for each person
	foreach my $person (keys %population) 
	{
		#make CR contacts for each person per day
		if(defined $population{$person}{'infState'} && $population{$person}{'infState'} == 1) 
		{

			#Generate the actual contacts
			for(my $i = 0; $i<$CONTACT_RATE; $i++) 
			{

				#prevent a person from infecting themself
				#this copies "person" to "r" to r so that calculations can be made
				my $r = $person;
				#This compares $r to $person, If they are the same,
				#it picks a new person via the RNG
				while($r == $person ) 
				{
					$r = int(rand($NUM_IND));

				}

				#This infects a person based on probability. If
				#the random number is less than infectivity, the
				#person becomes infected. Otherwise they stay in
				#The susceptible compartment.
				if(rand() < $infectivity) 
				{
					if($population_copy{$r}{'infState'} == 0)
					{
						$population_copy{$r}{'infState'} = 1;
					}


					#This is the code for the latent period. *************
					for(my $day_l = $LATENT_PERIOD; $day_l < 1; $day--) 
					{
						if ($day_l == 0){
							$population_copy{$r}{'infState'} = 2;
						}
					}
				}
			}
		}
	}
}
#This copies the contents of the working data back to the original hash
	%population = %{dclone(\%population_copy)};


#This updates the stats at the end of the code. 
	my @SAEC = (0,0,0,0);

	foreach my $person (keys %population) 
	{
		$SAEC[$population{$person}{'infState'}]++ if defined $population{$person}{'infState'};
	}

#print the results
	print "Day $DURATION\n";
	print "SUS: ".$SAEC[0]."\tLAT: ".$SAEC[1]."\tINF: ".$SAEC[2]."\tREC: ".$SAEC[3]."\n";


Output:
1
2
3
4
Before initiation
SUS: 0	LAT: 0	INF: 5	REC: 0
Day 50
SUS: 0	LAT: 0	INF: 0	REC: 0


Create a new paste based on this one


Comments: