//constructor public Simulation(int n, Trial t) { trialResult = t; trialNumber = n; }
//function: run sim private void RunSim(List <simpleInd> p, int m, int t, double amr) { List <Trial> trialList = new List <Trial>(); //counters used at end of sim to display crude results int biggerPops = 0; int extinctPops = 0; int smallerPops = 0; int neutralPops = 0; for (int i = 0; i < t; i++) //Trial loop: run for the number of trials { Trial trial = new Trial(); //new trial has started, add monthly results to this List <simpleInd> trialPop = new List <simpleInd>(p); //population to change within trial lblRunning.Text = "Conducting trial " + i.ToString() + " of " + t; lblRunning.Invalidate(); lblRunning.Update(); //Risk related variables int riskLength = VitalRates.RISKPERIOD; //counter for AMRs, assumes month 0 of sim was NOT a takeover month so set counter above threshold bool amrPeriod = false; //start of sim, default is no risk for (int j = 0; j < m; j++) //Month loop: run until you reach the # months { //Step 1: age everyone up a month and add a month to necessary counters AgeUp(trialPop); //add a month to everyone's age trial.MonthOfTrial = j + 1; //this is the month within the trial //Step 2: Give birth GiveBirth(trialPop); //turn pregnancies that are long enough into babies //Step 3: Decide if takeover occurred and if you're still under risk period bool takeover = calc.CoinFlip(amr); //weighted coin flip for a new takeover if (takeover) //takeover has occurred { riskLength = 0; //reset the risk period length to 0 amrPeriod = true; //you are in a risk period } else { riskLength++; //no new takeover but youre in a risk period already, so add a month to risk counter } if (riskLength >= VitalRates.RISKPERIOD) { amrPeriod = false; //if your risk period counter hits the threshold, turn off the risk variable } Month newMonth = new Month(trialPop.Count(), amrPeriod, takeover); //create a month to keep track of variables //Step 4: generate survival rates for each age class based on amrPeriod double adRate = calc.SampleBeta(vr.ReturnSurvMean(2, amrPeriod), vr.ReturnSurvSd(2, amrPeriod)); double juvRate = calc.SampleBeta(vr.ReturnSurvMean(1, amrPeriod), vr.ReturnSurvSd(1, amrPeriod)); double infRate = calc.SampleBeta(vr.ReturnSurvMean(0, amrPeriod), vr.ReturnSurvSd(0, amrPeriod)); double rate; //Step 5: flip the weighted coin of death for each ind in your pop for (int k = trialPop.Count - 1; k >= 0; k--) //loop through pop, can't use foreach because you subtract inds if they die { if (trialPop[k].AgeClass == 2) { rate = adRate; } else if (trialPop[k].AgeClass == 1) { rate = juvRate; } else { rate = infRate; } bool surv = calc.CoinFlip(rate); //TEST MESSAGE //MessageBox.Show("For ind " + trialPop[k].IndID + " age class " + trialPop[k].AgeClass + // ", survival probability = " + rate + ". Survived? " + surv); int?babyToKill = null; if (surv == false) //individual died { //if ind is adult female with dependent infant remove baby too if (trialPop[k].DepInf & trialPop[k].DepInfFem) { babyToKill = trialPop[k].DepInfID; } //getting baby ID from a dead mom //if ind is a dependent infant clear record from mom foreach (simpleInd mom in trialPop) { if (mom.IndID == trialPop[k].MomID) { mom.DepInf = false; //no longer has dependent infant mom.DepInfID = 0; //remove infant ID from mom mom.DepInfFem = false; //i dont think this matters mom.LastInfSurv = false; //puts mom on the accelerated IBI path } //mom alterations } //search population for the mom in question trialPop.RemoveAt(k); //remove ind from population if (babyToKill.HasValue) { trialPop.RemoveAll(x => x.IndID == babyToKill); //lambda expression "remove all instances in trialPop where the Individ X has ID that the DepInfID" newMonth.Deaths++; } newMonth.Deaths++; } //death loop } //Step 6: flip the death coin for infant males (only to release blocks from moms) foreach (simpleInd maleMoms in trialPop) { if (maleMoms.DepInf & maleMoms.DepInfFem == false) { bool surv = calc.CoinFlip(infRate); if (!surv) //baby died, clear blocks from mom { maleMoms.DepInf = false; maleMoms.LastInfSurv = false; maleMoms.DepInfID = 0; } } } //Step 7: surviving females have a chance to conceive. for (int l = trialPop.Count - 1; l >= 0; l--) { if (!trialPop[l].Preg) //monthly conception attempt for individs not pregnant. juve and infant likelihood is 0 { trialPop[l].Preg = calc.CoinFlip(vr.ReturnReprodMean(trialPop[l])); //weighted coin flip for baby or no } } newMonth.PopEnd = trialPop.Count(); RefreshPopulation(trialPop, lstCurrentPop, txtCurrentPop); //TEST MESSAGE //MessageBox.Show("End of month. AMR? " + amrPeriod); trial.MonthResults = newMonth; }//End of single month trialList.Add(trial); }//End of all trials //pull summary statistics from simulation foreach (Trial trial in trialList) { if (trial.MonthResults.PopEnd == 0) { extinctPops++; } else if (trial.MonthResults.PopEnd > trial.MonthResults.PopStart + VitalRates.NEUTRALGROWTH) { biggerPops++; } else if (trial.MonthResults.PopEnd < trial.MonthResults.PopStart - VitalRates.NEUTRALGROWTH) { smallerPops++; } else { neutralPops++; } } MessageBox.Show("In total you conducted " + trialList.Count() + " trials of a starting population of " + p.Count() + ". The population increased in " + biggerPops + " trials, stayed the same in " + neutralPops + ", decreased in " + smallerPops + ", and went extinct in " + extinctPops); lblRunning.Text = ""; lblRunning.Invalidate(); lblRunning.Update(); }//Full function