コード例 #1
0
        private static IEnumerable <SimulationResult> RunSingleSimulation()
        {
            // initialize the population
            foreach (var person in _population)
            {
                person.DayInfected = -1;
                person.DayDied     = -1;
            }

            for (var day = 0; day < _arguments.Days; day++)
            {
                // infect one person to start
                if (day == 0)
                {
                    _population[_random.Next(_arguments.Population)].DayInfected = 0;
                }

                // select people contacted this day and see if they get infected
                foreach (var person in _population.Where(p => p.IsContagious(day) && p.IsAlive))
                {
                    foreach (var contact in GetPeopleContacted(person).Where(p => !p.IsInfected))
                    {
                        if (_random.NextDouble() <= _arguments.Transmission)
                        {
                            contact.DayInfected = day;
                        }
                    }
                }

                // remove infected people who die from the population
                // we assume if you are no longer contagious then you survived
                foreach (var infected in _population.Where(p => p.IsContagious(day) && p.IsAlive))
                {
                    var mortalityToDate = _arguments.Mortality / _arguments.Contagious;

                    if (_random.NextDouble() <= mortalityToDate)
                    {
                        infected.DayDied = day;
                    }
                }

                var retVal = new SimulationResult()
                {
                    Contagious = _population.Count(p => p.IsContagious(day)),
                    Infected   = _population.Count(p => p.IsInfected),
                    Died       = _population.Count(p => !p.IsAlive),
                };

                yield return(retVal);
            }
        }
コード例 #2
0
        private static void OutputSimulationResult(int simNum, SimulationResult lastDay)
        {
            Console.SetCursorPosition(0, 0);

            var simDoc = new Alba.CsConsoleFormat.Document(
                new Grid()
            {
                Color    = ConsoleColor.Gray,
                Columns  = { GridLength.Auto, GridLength.Auto, GridLength.Auto, GridLength.Auto },
                Children =
                {
                    new Cell("Run #")
                    {
                        Stroke = _hdrThickness, Align = Align.Center
                    },
                    new Cell("Infected")
                    {
                        Stroke = _hdrThickness, Align = Align.Center
                    },
                    new Cell("Contagious")
                    {
                        Stroke = _hdrThickness, Align = Align.Center
                    },
                    new Cell("Died")
                    {
                        Stroke = _hdrThickness, Align = Align.Center
                    },
                    new Cell(simNum + 1)
                    {
                        Align = Align.Center, Color = ConsoleColor.DarkRed
                    },
                    new Cell($"{lastDay.Infected / _arguments.Simulations:n0}")
                    {
                        Align = Align.Center, Color = ConsoleColor.Yellow
                    },
                    new Cell($"{lastDay.Contagious / _arguments.Simulations:n0}")
                    {
                        Align = Align.Center, Color = ConsoleColor.DarkCyan
                    },
                    new Cell($"{lastDay.Died / _arguments.Simulations}")
                    {
                        Align = Align.Center, Color = ConsoleColor.Red
                    }
                }
            });

            ConsoleRenderer.RenderDocument(simDoc);
        }