Advertisement
Guest User

asdadsads

a guest
Apr 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7.  
  8. namespace PopulationSim2k19
  9. {
  10.     public class Agent
  11.     {
  12.         public int ID { get; set; } = 0;
  13.         public int Age { get; set; } = 0;
  14.         public bool Dead { get; set; } = false;
  15.         public int Repro { get; set; } = 780; //age at reproductive maturity
  16.         public int PRepro { get; set; } = 2080; //post-reproductive maturity,
  17.         public bool Pregnant { get; set; } = false;
  18.         public double FertilityRoll { get; set; } = 0.02; //%chance of pregnancy,
  19.         public int WeekOfPregnancy { get; set; } = 0;
  20.         public int Gestation { get; set; } = 38; //length in weeks until birth
  21.         public double PGestation { get; set; } = 0.5; //post-gestational regeneration (multiplier of gestation time),
  22.         public bool PGestational { get; set; } = false; //post-gestational stage boolean,
  23.         public int PGestationalWeek { get; set; } = 0;
  24.         public int Pregnancies { get; set; } = 0;
  25.         public bool Adult { get; set; } = false;
  26.  
  27.  
  28.         public Agent()
  29.         {
  30.         }
  31.     }
  32.  
  33.     class Program
  34.     {
  35.         static readonly int pop_num = 1000;
  36.         static readonly int desired_length = 20;
  37.         static readonly int desired_simulations = 52000; //52000
  38.         static readonly bool verboseoutput = true;
  39.         static readonly double prerepro = 0.35;
  40.         static readonly double repro = 0.6;
  41.         static readonly double postrepro = 0.05;
  42.         static readonly double basedeathfactor = 0.02;
  43.         static readonly double basefertilityfactor = 0.02;
  44.         static readonly int sim_length = desired_length * 52;
  45.         static readonly int max_pop_size = 10000;
  46.         static readonly int sim_interval = 10;
  47.         static readonly double sim_time_length = 0.066;
  48.         static int pop_actual = pop_num;
  49.         static int week = 0;
  50.         static int sim_number = 0;
  51.         static Stopwatch sw = new Stopwatch();
  52.         static List<Agent> pop = new List<Agent>();
  53.         static System.IO.StreamWriter F = new System.IO.StreamWriter(@"C:\Users\Elmeri\Desktop\results.txt");
  54.  
  55.  
  56.         static void CreateLogLine(int numDead, int numPopulation, int numBorn, int numPregnant)
  57.         {
  58.             F.WriteLine("\n" + week + "\t" + numDead + "\t" + numPopulation + "\t" + numBorn + "\t" + numPregnant);
  59.         }
  60.  
  61.         static void CreateLastLog()
  62.         {
  63.             if (verboseoutput)
  64.                 return;
  65.  
  66.             F.WriteLine("\n*At week " + week + " the simulation ended with " + pop_actual + " agents");
  67.         }
  68.  
  69.         static double GetRandomNumber(double minimum, double maximum)
  70.         {
  71.             Random random = new Random();
  72.             return random.NextDouble() * (maximum - minimum) + minimum;
  73.         }
  74.  
  75.         static void CreateAgents(int numAgents, string creationType)
  76.         {
  77.             if (creationType == "SimStart")
  78.             {
  79.                 double prereprocount = Math.Ceiling(pop_num * prerepro);
  80.                 double reprocount = Math.Ceiling(pop_num * repro);
  81.                 double postreprocount = Math.Ceiling(pop_num * postrepro);
  82.                 Console.WriteLine("Starting simulation " + sim_number + " with agent configuration " + prereprocount + "/" + reprocount + "/" + postreprocount + " for a total of " + (prereprocount + reprocount + postreprocount) + " agents");
  83.  
  84.                 reprocount = reprocount + prereprocount;
  85.                 postreprocount = postreprocount + reprocount;
  86.  
  87.                 for (int i = 0; i < numAgents; i++)
  88.                 {
  89.                     if ((prereprocount > i) && (i >= 0))
  90.                     {
  91.                         Agent newAgent = new Agent();
  92.                         newAgent.Age = (int)GetRandomNumber(0.0, newAgent.Repro);
  93.                         pop.Add(newAgent);
  94.                     }
  95.                     else if ((reprocount > i) && (i >= prereprocount))
  96.                     {
  97.                         Agent newAgent = new Agent();
  98.                         newAgent.Age = (int)GetRandomNumber(newAgent.Repro, newAgent.PRepro);
  99.                         pop.Add(newAgent);
  100.                     }
  101.                     else if ((postreprocount > i) && (i >= reprocount))
  102.                     {
  103.                         Agent newAgent = new Agent();
  104.                         newAgent.Age = (int)GetRandomNumber(newAgent.PRepro, 3000);
  105.                         pop.Add(newAgent);
  106.                     }
  107.                 }
  108.             }
  109.             else if (creationType == "Birth")
  110.             {
  111.                 for (int i = 0; i < numAgents; i++)
  112.                 {
  113.                     Agent newAgent = new Agent();
  114.                     pop.Add(newAgent);
  115.                 }
  116.             }
  117.         }
  118.  
  119.         static bool Mortality(int ageWeeks)
  120.         {
  121.             double input = (double)ageWeeks / 1000.0;
  122.             double deathFunction = 0.1 * (Math.Pow(0.6 * (input + 1.0), 2.0) * Math.Pow(0.01 * (input - 2.5), 2.0) * Math.Pow(0.9 * (input - 2.0), 2.0) + basedeathfactor);
  123.             Random random = new Random();
  124.             double deathRoll = random.NextDouble();
  125.             if (deathRoll <= deathFunction)
  126.             {
  127.                 return true;
  128.             }
  129.             return false;
  130.         }
  131.  
  132.  
  133.         static bool Fecundity(double chancepregnant)
  134.         {
  135.             Random random = new Random();
  136.             double pRoll = random.NextDouble();
  137.             if (pRoll < chancepregnant)
  138.             {
  139.                 return false;
  140.             }
  141.             return true;
  142.         }
  143.  
  144.         static void Simulate(int dsrlngth)
  145.         {
  146.             for (int i = 0; i < dsrlngth; i++)
  147.             {
  148.                 if ((pop_actual > max_pop_size) || (pop_actual <= 10) || (week == dsrlngth))
  149.                 {
  150.                     sw.Stop();
  151.                     double time_length = sw.Elapsed.TotalSeconds;
  152.                     Console.WriteLine("The simulation has ended after " + time_length + " seconds with " + pop_actual + " agents and " + week + " weeks");
  153.                     break;
  154.                 }
  155.  
  156.                 int deaths = 0;
  157.                 int births = 0;
  158.                 int pregnants = 0;
  159.  
  160.                 List<int> deadindices = new List<int>();
  161.  
  162.                 //age all the agents
  163.                 foreach (Agent a in pop)
  164.                 {
  165.                     a.Age++;
  166.                     if (Mortality(a.Age))
  167.                     {
  168.                         a.Dead = true;
  169.                         deadindices.Add(pop.IndexOf(a));
  170.                         deaths++;
  171.                     }
  172.                 }
  173.                 //remove dead ones to prevent dead ones from being pregnant
  174.                 foreach (int ind in deadindices)
  175.                 {
  176.                     pop.RemoveAt(ind);
  177.                 }
  178.                 //make agents pregnant, advance their fetal ages, check for whether it is time for birth, check whether the agent can be returned to a fertile status (post-gestational period)
  179.                 foreach (Agent a in pop)
  180.                 {
  181.                     //check for pregnant or post-gestational
  182.                     if (a.Pregnant)
  183.                     {
  184.                         pregnants++;
  185.                         //check for whether its delivery time
  186.                         if (a.WeekOfPregnancy >= a.Gestation)
  187.                         {
  188.                             a.Pregnant = false;
  189.                             a.Pregnancies++;
  190.                             a.PGestational = true;
  191.                             births++;
  192.                         }
  193.                         a.WeekOfPregnancy++;
  194.                     }
  195.                     else if (a.PGestational)
  196.                     {
  197.                         //check for whether the post - birth recovery period is over
  198.                         if (a.PGestationalWeek >= (a.Gestation * a.PGestation))
  199.                         {
  200.                             a.PGestational = false;
  201.                         }
  202.                         a.PGestationalWeek++;
  203.                     }
  204.                     else if (!a.Pregnant && !a.PGestational)
  205.                     {
  206.                         //calculate chance of pregnancy and manipulate the agent accordingly
  207.                         if (a.Age > a.Repro && a.Age < a.PRepro)
  208.                         {
  209.                             if (Fecundity(a.FertilityRoll))
  210.                                 a.Pregnant = true;
  211.                         }
  212.                     }
  213.                 }
  214.                 for (int b = 0; b < births; b++)
  215.                 {
  216.                     CreateAgents(1, "Birth");
  217.                 }
  218.                 pop_actual = (pop_actual - deaths) + births;
  219.                 week++;
  220.                 CreateLogLine(deaths, pop_actual, births, pregnants);
  221.             }
  222.         }
  223.  
  224.  
  225.         static void Main(string[] args)
  226.         {
  227.             sw.Start();
  228.             F.WriteLine("week \t deaths \t population \t births \t pregnancies");
  229.             CreateAgents(pop_num, "SimStart");
  230.             Simulate(desired_simulations);
  231.             F.Close();
  232.         }
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement