#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define SQR(u) ((u)*(u)) #define PI 3.14159265358979323846 #define pop 1000000 //100000 //1000 //population size #define nsamples 100001//1001 //sample size #define tmax 2000 //number of days #define numstates 4 //0 - dead, 1 - S, 2 - I, 3 - R #define daycycles 3 // Three 8 hour cycles per day int seed1 = 12345; int seed2 = 1234567; //int N_priv = 0; //Defined according to pop, using mean number of residents int init_infected = 0; int total_infected; int virvariations; // number o different viruses // Probabilities float mutate_percentage = 0.05; //percentage of population that must be infected for virus mutation float beta = 0.2; //1.0; float prob_death = 0.01; float prob_mutation = 0.0001; float isolation_reg = 0.45; float isolation_wkd = 0.60; float isolation; // Times int incubation = 5; //5; int recovery = 14; int immunity = 120; // days immune to a virus vector > v( tmax , vector (numstates, 0)); vector > vm( tmax , vector (numstates, 0)); vector meanbeta( tmax , 0); // Priorities (in which agents choose to move) vector priorities(pop); /************************************************************************/ class Agent { public: int status; //0 - dead, 1 - S, 2 - I, 3 - R int priv_cell; //position on vector that stores private spaces - residence int pub_cell; //position on vector that stores temporary public spaces int visitor; int virustype; vector destination; // vector that stores public destination, depending on number of cycles per day and isolation //vector vir_R; // vector that stores virus types that the agent contracted //vector vir_immunity; //time to become susceptible again int t_immunity; int t_incubation; int t_infection; }; vector agent; /************************************************************************/ class PublicSpace { public: int capacity; int occupancy[3]; vector publ_I; //stores infected population by virus types, to check if space is has someone infected who might infect someone susceptible //vector publ_S; //stores susceptible population by virus types, to calculate infection probability float publ_S; vector publ_probs; //calculated probabilities }; vector publicspace; /************************************************************************/ class PrivateSpace { public: int capacity; vector priv_I; //vector priv_S; float priv_S; vector priv_probs; //calculated probabilities }; vector privatespace; class Virus { public: float beta_vir; float prob_death_vir; vector vir_I; }; vector virus; /************************************************************************/ /*** FUNCTIONS ***/ /************************************************************************/ //combined RNG, adapted from Numerical recipes struct Ranq1 { unsigned long long v; Ranq1(unsigned long long j): v(4101842887655102017LL) { v = v ^ j; v = int64(); } inline unsigned long long int64() { v = v ^ v >> 21; v = v ^ v << 35; v = v ^ v >> 4; return v * 2685821657736338717LL; } inline double doub(){ return 5.42101086242752217E-20 * int64(); } }; /************************************************************************/ struct NormalDev : Ranq1 { double mu, sig; NormalDev(double mmu, double ssig, unsigned long long i) : Ranq1(i), mu(mmu), sig(ssig){} double dev() { double u,v,x,y,q; do { u = doub(); v = 1.7156*(doub() - 0.5); x = u - 0.449871; y = fabs(v) + 0.386595; q = SQR(x) + y*(0.19600*y-0.25472*x); } while (q > 0.27597 && (q > 0.27846 || SQR(v) > -4.*log(u)*SQR(u))); return mu + sig*v/u; } }; Ranq1 r(seed1); /************************************************************************/ // Adapted from Numerical Recipes int poissrnd_small(double mean) { double L = exp(-mean); double p = 1; int result = 0; do { result++; p *= r.doub(); } while (p > L); result--; return result; } int poissrnd_large(double mean) { double rr, rn; double x, m; double sqrt_mean = sqrt(mean); double log_mean = log(mean); double g_x; double f_m; do { do { rn = r.doub(); x = mean + sqrt_mean*tan(PI*(rn-1/2.0)); } while (x < 0); g_x = sqrt_mean/(PI*((x-mean)*(x-mean) + mean)); m = floor(x); f_m = exp(m*log_mean - mean - lgamma(m + 1)); rr = f_m / g_x / 2.4; } while (r.doub() > rr); return (int)m; } int poissrnd(double mean) { if (mean < 60) { return poissrnd_small(mean); } else { return poissrnd_large(mean); } } /************************************************************************/ void populate_poisson() { int residents; // number of residents per private cell int positioned = 0; // positioned population int locals = 0.99*pop; while(positioned= locals) { residents = locals-positioned; } privatespace.push_back(PrivateSpace()); privatespace[privatespace.size()-1].capacity = 0; for (int j=0; j isolation || agent[ag].visitor == 1) { outcycles = r.doub()*2+1; // leaves home once or twice, randomly for (int j=0; j= publicspace[pubcell].capacity); agent[ag].destination[cycle] = pubcell; // choose a public space, increase it's occupancy agent[ag].pub_cell = pubcell; publicspace[pubcell].occupancy[cycle]++; } } }//*/ for (cycle = 0; cycle= int(mutate_percentage*agent.size())) agent[rn].virustype = int(r.doub()*virus.size()); else agent[rn].virustype = 0; } //*/ } /************************************************************************/ void update() { // updates agents - synchronous for(int i=0; i 7 && r.doub() < virus[agent[i].virustype].prob_death_vir) { agent[i].status = 0; } agent[i].t_infection--; } // reduce infection time and updates status if necessary // if incubation time is 0, becomes infected if (agent[i].status == 1 && agent[i].t_incubation == 0) { agent[i].t_infection = recovery; agent[i].status = 2; } // else keeps incubating else if (agent[i].status == 1 && agent[i].t_incubation != -1) { agent[i].t_incubation--; } } } /************************************************************************/ void countagents(int t) { double betasum = 0; float inf_ag = 0; //0 - dead, 1 - S, 2 - V, 3 - I, 4 - R for (int i=0; i0) { updatevisitors(t); interact(t); update(); } countagents(t); } if (sample%50==0) { std::stringstream ss; ss << "Mean_sample" << sample << ".txt"; std::string s = ss.str(); ofstream myfile(s.c_str()); for(int t=1; t