示例#1
0
        public void Execute(int year)
        {
            // make sure we are in a year that we should be simulating.
            int deltaYear = year - FirstYear;
            var log       = Repository.GetRepository(LogSource);

            NumberOfDeaths = 0;
            log.WriteToLog($"Finding people who will be dying for Year {year}");
            RandomGenerator.ExecuteWithProvider((rand) =>
            {
                var persons        = Repository.GetRepository(PersonRepository);
                int numberOfDeaths = 0;
                // first find all persons who will be having a child
                foreach (var person in persons)
                {
                    if (person.Age > MaximumAge)
                    {
                        person.Living = false;
                        numberOfDeaths++;
                    }
                    else
                    {
                        var index = GetDataIndex(person.Age, person.Sex, person.MaritalStatus, deltaYear);
                        if (rand.Take() < DeathRateData[index])
                        {
                            person.Living = false;
                            numberOfDeaths++;
                        }
                    }
                }
                NumberOfDeaths = numberOfDeaths;
            });
            Thread.MemoryBarrier();
            log.WriteToLog($"Number of deaths in {year}: {NumberOfDeaths}");
        }
        public void Execute(int year)
        {
            var log = Repository.GetRepository(LogSource);

            log.WriteToLog($"Finding people who will be giving birth for Year {year}");
            // 1) Get people who want to enter the market
            AddPeopleToMarket(year, out _males, out _females);
            log.WriteToLog($"Marriage Market: Year {year}, Males Selected {_males.Count}, Females Selected {_females.Count}");
            // 2) Match people
            var currentDate = new Date(year, 0);

            _randomGenerator.ExecuteWithProvider((rand) =>
            {
                Execute(rand, year, 0);
            });
            _males   = null;
            _females = null;
            log.WriteToLog($"Marriage Market: Year {year}, Couples married {MarriagesInYear}");
        }
示例#3
0
        private HashSet <Person> GetPersonsToOutMigrate(Repository <Person> persons, float rateForYear)
        {
            var toOutMigrate = new HashSet <Person>();

            RandomGenerator.ExecuteWithProvider((rand) =>
            {
                foreach (var person in persons)
                {
                    if (person.Living && person.Age >= AgeOfMaturity)
                    {
                        if (rand.Take() < rateForYear)
                        {
                            toOutMigrate.Add(person);
                        }
                    }
                }
            });
            return(toOutMigrate);
        }
示例#4
0
文件: Divorce.cs 项目: l5d1l5/ILUTE
        public void Execute(int year)
        {
            if (!LogSource.Loaded)
            {
                LogSource.LoadData();
            }
            var log = LogSource.GiveData();

            log.WriteToLog($"Starting divorce for year {year}");
            var           families = Repository.GetRepository(Families);
            List <Family> toDivoce = new List <Family>();

            RandomGenerator.ExecuteWithProvider((rand) =>
            {
                foreach (var family in families)
                {
                    var female = family.FemaleHead;
                    var male   = family.MaleHead;
                    // if the family is married
                    if (female != null && male != null && female.Spouse == male)
                    {
                        var pick = rand.Take();
                        if (CheckIfShouldDivorse(pick, family, year))
                        {
                            toDivoce.Add(family);
                        }
                    }
                }
            });
            log.WriteToLog($"Divorce average probability: {DivorceProbability / NumberOfTimes}");
            log.WriteToLog($"Finished computing candidates to divorce for year {year} with {toDivoce.Count} divorces.");
            NumberOfTimes = toDivoce.Count;
            // After identifying all of the families to be divorced, do so.
            foreach (var family in toDivoce)
            {
                family.Divorse(families);
            }
            log.WriteToLog("Finished divorcing all families.");
        }
示例#5
0
        public void Execute(int year)
        {
            // make sure we are in a year that we should be simulating.
            int deltaYear = year - FirstYear;
            var log       = Repository.GetRepository(LogSource);

            log.WriteToLog($"Finding people who will be giving birth for Year {year}");
            var persons  = Repository.GetRepository(PersonRepository);
            var families = Repository.GetRepository(FamilyRepository);

            List <Person> havingAChild = new List <Person>();

            // first find all persons who will be having a child
            RandomGenerator.ExecuteWithProvider((rand) =>
            {
                foreach (var person in persons)
                {
                    if (person.Sex == Sex.Female && person.Age >= AgeOfMaturity && person.Living)
                    {
                        var pick  = rand.Take();
                        var index = GetDataIndex(person.Age, person.MaritalStatus, deltaYear);
                        if (pick < BirthRateData[index])
                        {
                            havingAChild.Add(person);
                        }
                    }
                }
            });
            log.WriteToLog($"Processing Births for Year {year}");
            var numberOfChildrenBorn = havingAChild.Count;

            BirthsInLastYear = numberOfChildrenBorn;
            RandomGenerator.ExecuteWithProvider((rand) =>
            {
                // process each person having a child
                foreach (var mother in havingAChild)
                {
                    // create the child
                    var baby = new Person
                    {
                        Sex    = rand.Take() < ProbabilityOfBabyBeingFemale ? Sex.Female : Sex.Male,
                        Mother = mother,
                        Father = mother.Spouse
                    };
                    persons.AddNew(baby);
                    var originalFamily = mother.Family;
                    baby.Family        = originalFamily;
                    if (mother.Children.Count <= 1)
                    {
                        switch (originalFamily.Persons.Count)
                        {
                        case 0:
                        case 1:

                            {
                                // if this family only has a single person in it there can be no father

                                // In all cases, either the mother just needs to add her baby to her existing family
                                originalFamily.Persons.Add(baby);
                            }
                            break;

                        default:
                            {
                                // we need to see if this is going to make the mother leave her current family.
                                var father = mother.Spouse;
                                if (father != null)
                                {
                                    // if she already has a spouse then she is already in a family so we don't need the more expensive logic
                                    originalFamily.Persons.Add(baby);
                                }
                                else
                                {
                                    // if the mother is not the female head of the family she needs to generate a new one
                                    if (mother != mother.Family.FemaleHead)
                                    {
                                        AddMotherAndBabyToNewFamily(families, mother, baby, originalFamily);
                                    }
                                    else
                                    {
                                        originalFamily.Persons.Add(baby);
                                    }
                                }
                            }
                            break;
                        }
                    }
                    else
                    {
                        // add this baby its siblings
                        originalFamily.Persons.Add(baby);
                    }
                    baby.Siblings.AddRange(mother.Children);
                    mother.AddChild(baby);
                    mother.Spouse?.AddChild(baby);
                }
            });
            log.WriteToLog($"Finished processing {numberOfChildrenBorn} births for Year {year}");
        }