[ create a new paste ] login | about

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

minorsecond - 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);

#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;

my %population = ();



#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 $sus = 0;
my $lat = 0;
my $inf = 0;
my $rec = 0;

foreach my $person (keys %population) 

{
	if($population{$person}{'infState'} == 0) 
        {
		$sus++;
	}
	
	if($population{$person}{'infState'} == 1) 
        {
		$lat++;
	}

	if($population{$person}{'infState'} == 2) 
        {
		$inf++;
	}

	if($population{$person}{'infState'} == 3) 
        {
		$rec++;
	}
}


#Shows what the initial parameters are
print "Before initiation\n";
print "SUS: ".$sus."\tLAT: " .$lat. "\tINF: ".$inf."\tREC: ".$rec."\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($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 $susc = 0;
my $late = 0;
my $infe = 0;
my $reco = 0;

foreach my $person (keys %population) 
{
	if($population{$person}{'infState'} == 0)
     {
		$susc++; 
     }
	if($population{$person}{'infState'} == 1)
     {
		$late++; 
     }
	if($population{$person}{'infState'} == 2)
     {
		$infe++; 
     }
	if($population{$person}{'infState'} == 3)
     {
		$reco++; 
     }
}

#print the results
print "Day ".$day. "\n";
print "SUS: ".$susc."\tLAT: ".$late."\tINF: ".$infe."\tREC: ".$reco."\n";


Output:
1
2
3
Missing right curly or square bracket at line 175, at end of line
syntax error at line 175, at EOF
Execution aborted due to compilation errors.


Create a new paste based on this one


Comments: