/// <summary> /// Create the individual ruminant animals using the Cohort parameterisations. /// </summary> /// <param name="number">The number of individuals to create</param> /// <param name="initialAttributes">The initial attributes found from parent and this cohort</param> /// <param name="ruminantType">The breed parameters if overwritten</param> /// <returns>List of ruminants</returns> public List <Ruminant> CreateIndividuals(int number, List <ISetAttribute> initialAttributes, RuminantType ruminantType = null) { List <Ruminant> individuals = new List <Ruminant>(); if (number > 0) { RuminantType parent = ruminantType; if (parent is null) { parent = FindAncestor <RuminantType>(); } // get Ruminant Herd resource for unique ids RuminantHerd ruminantHerd = parent.Parent as RuminantHerd; // Resources.FindResourceGroup<RuminantHerd>(); for (int i = 1; i <= number; i++) { double weight = 0; if (Weight > 0) { // avoid accidental small weight if SD provided but weight is 0 // if weight is 0 then the normalised weight will be applied in Ruminant constructor. double u1 = RandomNumberGenerator.Generator.NextDouble(); double u2 = RandomNumberGenerator.Generator.NextDouble(); double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); weight = Weight + WeightSD * randStdNormal; } Ruminant ruminant = Ruminant.Create(Sex, parent, Age, weight); ruminant.ID = ruminantHerd.NextUniqueID; ruminant.Breed = parent.Breed; ruminant.HerdName = parent.Name; ruminant.SaleFlag = HerdChangeReason.None; if (Suckling) { if (Age >= ((parent.NaturalWeaningAge == 0)?parent.GestationLength: parent.NaturalWeaningAge)) { string limitstring = (parent.NaturalWeaningAge == 0) ? $"gestation length [{parent.GestationLength}]" : $"natural weaning age [{parent.NaturalWeaningAge}]"; string warn = $"Individuals older than {limitstring} cannot be assigned as suckling [r={parent.Name}][r={this.Parent.Name}][r={this.Name}]{Environment.NewLine}These individuals have not been assigned suckling."; Warnings.CheckAndWrite(warn, Summary, this, MessageType.Warning); } else { ruminant.SetUnweaned(); } } if (Sire) { if (this.Sex == Sex.Male) { RuminantMale ruminantMale = ruminant as RuminantMale; ruminantMale.Attributes.Add("Sire"); } else { string warn = $"Breeding sire switch is not valid for individual females [r={parent.Name}][r={this.Parent.Name}][r={this.Name}]{Environment.NewLine}These individuals have not been assigned sires. Change Sex to Male to create sires in initial herd."; Warnings.CheckAndWrite(warn, Summary, this, MessageType.Warning); } } // if weight not provided use normalised weight ruminant.PreviousWeight = ruminant.Weight; if (this.Sex == Sex.Female) { RuminantFemale ruminantFemale = ruminant as RuminantFemale; ruminantFemale.WeightAtConception = ruminant.Weight; ruminantFemale.NumberOfBirths = 0; if (setPreviousConception != null) { setPreviousConception.SetConceptionDetails(ruminantFemale); } } // initialise attributes foreach (ISetAttribute item in initialAttributes) { ruminant.Attributes.Add(item.AttributeName, item.GetAttribute(true)); } individuals.Add(ruminant); } // add any mandatory attributes to the list on the ruminant type foreach (var mattrib in initialAttributes.Where(a => a.Mandatory)) { parent.AddMandatoryAttribute(mattrib.AttributeName); } } return(individuals); }
/// <summary> /// Create the individual ruminant animals using the Cohort parameterisations. /// </summary> /// <param name="number">The number of individuals to create</param> /// <param name="initialAttributes">The initial attributes found from parent and this cohort</param> /// <param name="ruminantType">The breed parameters if overwritten</param> /// <returns>List of ruminants</returns> public List <Ruminant> CreateIndividuals(int number, List <ISetAttribute> initialAttributes, RuminantType ruminantType = null) { List <Ruminant> individuals = new List <Ruminant>(); if (number > 0) { RuminantType parent = ruminantType; if (parent is null) { parent = FindAncestor <RuminantType>(); } // get Ruminant Herd resource for unique ids RuminantHerd ruminantHerd = parent.Parent as RuminantHerd; // Resources.FindResourceGroup<RuminantHerd>(); for (int i = 1; i <= number; i++) { double weight = 0; if (Weight > 0) { // avoid accidental small weight if SD provided but weight is 0 // if weight is 0 then the normalised weight will be applied in Ruminant constructor. double u1 = RandomNumberGenerator.Generator.NextDouble(); double u2 = RandomNumberGenerator.Generator.NextDouble(); double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); weight = Weight + WeightSD * randStdNormal; } Ruminant ruminant = Ruminant.Create(Sex, parent, Age, weight); ruminant.ID = ruminantHerd.NextUniqueID; ruminant.Breed = parent.Breed; ruminant.HerdName = parent.Name; ruminant.SaleFlag = HerdChangeReason.None; if (Suckling) { ruminant.SetUnweaned(); } if (Sire) { if (this.Sex == Sex.Male) { RuminantMale ruminantMale = ruminant as RuminantMale; ruminantMale.Attributes.Add("Sire"); } else { Summary.WriteWarning(this, "Breeding sire switch is not valid for individual females [r=" + parent.Name + "].[r=" + this.Parent.Name + "].[r=" + this.Name + "]"); } } // if weight not provided use normalised weight ruminant.PreviousWeight = ruminant.Weight; if (this.Sex == Sex.Female) { RuminantFemale ruminantFemale = ruminant as RuminantFemale; ruminantFemale.WeightAtConception = ruminant.Weight; ruminantFemale.NumberOfBirths = 0; if (setPreviousConception != null) { setPreviousConception.SetConceptionDetails(ruminantFemale); } } // initialise attributes foreach (ISetAttribute item in initialAttributes) { ruminant.Attributes.Add(item.AttributeName, item.GetAttribute(true)); } individuals.Add(ruminant); } // add any mandatory attributes to the list on the ruminant type foreach (var mattrib in initialAttributes.Where(a => a.Mandatory)) { parent.AddMandatoryAttribute(mattrib.AttributeName); } } return(individuals); }