public void RemovePerson(Person personToRemove) { Persons.Remove(personToRemove); if (personToRemove == FemaleHead) { FemaleHead = null; } else if (personToRemove == MaleHead) { MaleHead = null; } if (FemaleHead == null && MaleHead == null && Persons.Count > 0) { UpdateFamilyHead(); } }
private void LoadPersons() { WriteToLog("Starting to Load Persons"); var personRepo = Repository.GetRepository(RepositoryPerson); var familyRepo = Repository.GetRepository(RepositoryFamily); int personsWithNegativeFamilyIndex = 0; List<Family> toAddAfterwards = new List<Family>(); using (var reader = new CsvReader(InitialPersonFile, true)) { /* (Columns) 00 personid, pumiid, familyid, dwellingid, prov, cmapust, hhclass, htype, unitsp, hhincp, 10 ompp, grosrtp, rentp, hhstat, efstat, efsize, cfstat, cfsize, mscfinc, cfincp, 20 agep, sexp, marstp, mob5p, pr5p, lfact71, lfact, hrswk, lstwkp, wkswk, 30 fptwk, preschp, occ81p, occ71p, ind80p, ind70p, cowp, hlosp, hgrad, psuv, 40 psot, trnuc, dgree, dgmfs, ethnicor, vismin, abethnic, duethnic, geethnic, scethnic, 50 huethnic, poethnic, ukethnic, crethnic, grethnic, itethnic, prethnic, jeethinc, waethnic, saethnic, 60 chethnic, fiethnic, eaethnic, blethnic, birtplac, citizens, yrimmig, immigage, offlang, homelang, 70 mothertg, totincp, wagesp, selfip, invstp, oasgip, cqppbp, famalp, chdcrp, uicbnp, 80 govtip, retirp, otincp, hmainp, tenurp, rcondp, valuep, room, id; */ // there is no header at the moment so we don't need to burn a line int columns; if (FilesContainHeaders) { reader.LoadLine(); } while (reader.LoadLine(out columns)) { if (columns >= 89) { int personid, familyid, dwellingid, hhstat, cfstat, agep, sexp, marstp, lfact, occ81p, ind80p, totincp, hlosp; int dgmfs, psuv, psot, trnuc, dgree; reader.Get(out personid, 0); reader.Get(out familyid, 2); reader.Get(out dwellingid, 3); reader.Get(out hhstat, 13); reader.Get(out cfstat, 16); reader.Get(out agep, 20); reader.Get(out sexp, 21); reader.Get(out marstp, 22); reader.Get(out lfact, 26); reader.Get(out occ81p, 32); reader.Get(out ind80p, 34); reader.Get(out totincp, 71); reader.Get(out hlosp, 37); reader.Get(out dgmfs, 43); reader.Get(out psuv, 39); reader.Get(out psot, 40); reader.Get(out trnuc, 41); reader.Get(out dgree, 42); Family personsFamily; // if they are living alone create a new family for them if (familyid < 0) { // if the person has no family and no dwelling just continue // this would mean that they live in a collective if (dwellingid < 0) { continue; } personsWithNegativeFamilyIndex++; personsFamily = new Family(); toAddAfterwards.Add(personsFamily); } else if (!familyRepo.TryGet(familyid, out personsFamily)) { // otherwise create the new family personsFamily = new Family(); familyRepo.AddNew(familyid, personsFamily); } Person p; //TODO: Finish filling out the personal information for this individual personRepo.AddNew(personid, (p = new Person() { Age = agep, Family = personsFamily, Living = true, Sex = sexp == 2 ? Sex.Male : Sex.Female })); // add the person to their family personsFamily.Persons.Add(p); } } // fill in the rest foreach (var family in toAddAfterwards) { familyRepo.AddNew(family); } WriteToLog("Total number of families loaded: " + familyRepo.Count); WriteToLog("Total number of persons loaded: " + personRepo.Count); } }
internal Family CloneFamily(Family family) { var newFamily = new Family(); var newPersons = newFamily.Persons; var oldPersons = family.Persons; //clone all of the individuals to make a map foreach (var person in oldPersons) { var newPerson = new Person() { Age = person.Age, Sex = person.Sex, Family = newFamily, LabourForceStatus = person.LabourForceStatus, Spouse = person.Spouse, MaritalStatus = person.MaritalStatus }; newPersons.Add(newPerson); } for (int i = 0; i < oldPersons.Count; i++) { // copy the children if (oldPersons[i].Children.Count > 0) { foreach (var child in oldPersons[i].Children) { newPersons[i].Children.Add(newPersons[oldPersons.IndexOf(child)]); } } if (oldPersons[i].Siblings.Count > 0) { foreach (var child in oldPersons[i].Siblings) { newPersons[i].Siblings.Add(newPersons[oldPersons.IndexOf(child)]); } } } return newFamily; }
private float GetWifeCovariate(Person female, int yearsMarried, int currentYear) { var v = 0.0f; var yearOfBirth = currentYear - female.Age; var ageAtOfMarriage = female.Age - yearsMarried; if (yearOfBirth < 1945) { v += WBORNBEFORE1945; } else if (yearOfBirth > 1959) { v += WBORNAFTER1959; } if (ageAtOfMarriage < 20) { v += WBEGUNUNDER20; } if (female.ExSpouses.Count > 0) { v += WPREDIV; } var from25 = female.Age - 25; v += WSQFROM25 * (from25 * from25); return v; }
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(); baby.Sex = rand.Take() < ProbabilityOfBabyBeingFemale ? Sex.Female : Sex.Male; baby.Mother = mother; baby.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}"); }
private static void AddMotherAndBabyToNewFamily(Repository<Family> families, Person mother, Person baby, Family originalFamily) { // Baby family is setup later to reflect the mother's family Family newFamily = new Family(); newFamily.Persons.Add(mother); newFamily.Persons.Add(baby); var household = originalFamily.Household; household?.Families.Add(newFamily); newFamily.Household = household; originalFamily.Persons.Remove(mother); mother.Family = newFamily; baby.Family = newFamily; newFamily.FemaleHead = mother; families.AddNew(newFamily); }
private void RemoveChild(Person person) { Children.Remove(person); }
private void RemoveSpouse(Person person) { Spouse = null; MaritalStatus = MaritalStatus.Widowed; }
internal void AddChild(Person baby) { foreach(var child in Children) { if(!child.Siblings.Contains(baby)) { child.Siblings.Add(baby); } } }
private void RemoveSibling(Person person) { Siblings.Remove(person); }
private void RemoveParent(Person parent) { if(Mother == parent) { Mother = null; } else { Father = null; } }