private void OnCLEMInitialiseActivity(object sender, EventArgs e) { // get details from parent breeding activity breedParent = this.Parent.Parent as RuminantActivityBreed; if (breedParent is null) { throw new ApsimXException(this, $"Invalid grandparent component of [a={this.Name}]. Expecting [a=RuminantActivityBreed].[a=RuminantActivityControlledMating].[f=ActivityTimerBreedForMilking]"); } // breed params breedParams = Resources.GetResourceItem(this, breedParent.PredictedHerdBreed, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; // determine min time between conceptions with full milk production double conceiveInterval = Convert.ToInt32(breedParams.GestationLength + Math.Ceiling(breedParams.MilkingDays / 30.4), CultureInfo.InvariantCulture); // get the milking period double milkInterval = Math.Ceiling(breedParams.MilkingDays / 30.4); double numGroups = milkInterval / conceiveInterval; // determine number of breeders each timing // reduce interval to increase milk production }
private void OnCLEMInitialiseActivity(object sender, EventArgs e) { // get details from parent breeding activity controlledMatingParent = this.Parent as RuminantActivityControlledMating; if (controlledMatingParent is null) { throw new ApsimXException(this, $"Invalid parent component of [a={this.Name}]. Expecting [a=RuminantActivityControlledMating].[f=ActivityTimerBreedForMilking]"); } breedParent = controlledMatingParent.Parent as RuminantActivityBreed; breedParams = resources.FindResourceType <RuminantHerd, RuminantType>(this, breedParent.PredictedHerdBreed, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop); int monthsOfMilking = Convert.ToInt32(Math.Ceiling(breedParams.MilkingDays / 30.4), CultureInfo.InvariantCulture); shortenLactationMonths = Math.Max(0, monthsOfMilking - ShortenLactationMonths); pregnancyDuration = Convert.ToInt32(breedParams.GestationLength, CultureInfo.InvariantCulture); // determine min time between conceptions with full milk production minus cut short and resting minConceiveInterval = Math.Max(0, pregnancyDuration + shortenLactationMonths + RestMonths); startBreedCycleGestationOffsett = shortenLactationMonths - pregnancyDuration; if (startBreedCycleGestationOffsett < pregnancyDuration * -1) { throw new Exception("Cannot handle condition where milking cycle starts before pregnancy"); } // get the milking period milkingsPerConceptionsCycle = Math.Ceiling((minConceiveInterval * 1.0) / monthsOfMilking); }
private void OnCLEMInitialiseActivity(object sender, EventArgs e) { this.InitialiseHerd(false, true); breedParams = Resources.GetResourceItem(this, typeof(RuminantHerd), this.PredictedHerdName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; // max sires if (MaximumSiresKept < 1 & MaximumSiresKept > 0) { SiresKept = Convert.ToInt32(Math.Ceiling(MaximumBreedersKept * MaximumSiresKept), CultureInfo.InvariantCulture); } else { SiresKept = Convert.ToInt32(Math.Truncate(MaximumSiresKept), CultureInfo.InvariantCulture); } if (FillBreedingMalesAtStartup) { RuminantHerd herd = Resources.RuminantHerd(); if (herd != null) { // get number in herd int numberPresent = this.CurrentHerd(false).Where(a => a.Gender == Sex.Male).Cast <RuminantMale>().Where(a => a.BreedingSire).Count(); // fill to number needed for (int i = numberPresent; i < SiresKept; i++) { RuminantMale newbull = new RuminantMale(48, Sex.Male, 450, breedParams) { Breed = this.PredictedHerdBreed, HerdName = this.PredictedHerdName, BreedingSire = true, ID = herd.NextUniqueID, PreviousWeight = 450, SaleFlag = HerdChangeReason.InitialHerd }; herd.AddRuminant(newbull, this); } } } // check GrazeFoodStoreExists grazeStore = ""; if (GrazeFoodStoreName != null && !GrazeFoodStoreName.StartsWith("Not specified")) { grazeStore = GrazeFoodStoreName.Split('.').Last(); foodStore = Resources.GetResourceItem(this, GrazeFoodStoreName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as GrazeFoodStoreType; } // check for managed paddocks and warn if animals placed in yards. if (grazeStore == "") { var ah = Apsim.Find(this, typeof(ActivitiesHolder)); if (Apsim.ChildrenRecursively(ah, typeof(PastureActivityManage)).Count() != 0) { Summary.WriteWarning(this, String.Format("Animals purchased by [a={0}] are currently placed in [Not specified - general yards] while a managed pasture is available. These animals will not graze until mustered and will require feeding while in yards.\nSolution: Set the [GrazeFoodStore to place purchase in] located in the properties [General].[PastureDetails]", this.Name)); } } }
private void OnCLEMInitialiseActivity(object sender, EventArgs e) { this.InitialiseHerd(false, false); // get herd to add to herdToUse = Resources.GetResourceItem(this, typeof(RuminantHerd), this.PredictedHerdName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; if (!herdToUse.PricingAvailable()) { Summary.WriteWarning(this, "No pricing is supplied for herd [" + PredictedHerdName + "] and so no pricing will be included with [" + this.Name + "]"); } }
private void SetParameters(RuminantType ruminant) { List <Tuple <string, string, double> > parameters = new List <Tuple <string, string, double> >() { new Tuple <string, string, double>("concep_rate_assym", "ConceptionRateAsymptote", 1), new Tuple <string, string, double>("concep_rate_coeff", "ConceptionRateCoefficient", 1), new Tuple <string, string, double>("concep_rate_incpt", "ConceptionRateIntercept", 1), new Tuple <string, string, double>("birth_SRW", "SRWBirth", 1), new Tuple <string, string, double>("cashmere_coeff", "CashmereCoefficient", 1), new Tuple <string, string, double>("Critical_cow_wt", "CriticalCowWeight", 0.01), new Tuple <string, string, double>("grwth_coeff1", "AgeGrowthRateCoefficient", 1), new Tuple <string, string, double>("grwth_coeff2", "SRWGrowthScalar", 1), new Tuple <string, string, double>("intake_coeff", "IntakeCoefficient", 1), new Tuple <string, string, double>("intake_incpt", "IntakeIntercept", 1), new Tuple <string, string, double>("IPI_coeff", "InterParturitionIntervalCoefficient", 1), new Tuple <string, string, double>("IPI_incpt", "InterParturitionIntervalIntercept", 1), new Tuple <string, string, double>("Joining_age", "MinimumAge1stMating", 1), new Tuple <string, string, double>("Joining_size", "MinimumSize1stMating", 0.01), new Tuple <string, string, double>("juvenile_mort_coeff", "JuvenileMortalityCoefficient", 1), new Tuple <string, string, double>("juvenile_mort_exp", "JuvenileMortalityExponent", 1), new Tuple <string, string, double>("juvenile_mort_max", "JuvenileMortalityMaximum", 0.01), new Tuple <string, string, double>("kg_coeff", "EGrowthEfficiencyCoefficient", 1), new Tuple <string, string, double>("kg_incpt", "EGrowthEfficiencyIntercept", 1), new Tuple <string, string, double>("kl_coeff", "ELactationEfficiencyCoefficient", 1), new Tuple <string, string, double>("kl_incpt", "ELactationEfficiencyIntercept", 1), new Tuple <string, string, double>("km_coeff", "EMaintEfficiencyCoefficient", 1), new Tuple <string, string, double>("km_incpt", "EMaintEfficiencyIntercept", 1), new Tuple <string, string, double>("kme", "Kme", 1), new Tuple <string, string, double>("Milk_Curve_nonsuck", "MilkCurveNonSuckling", 1), new Tuple <string, string, double>("Milk_Curve_suck", "MilkCurveSuckling", 1), new Tuple <string, string, double>("Milk_end", "MilkingDays", 30), new Tuple <string, string, double>("Milk_intake_coeff", "MilkIntakeCoefficient", 1), new Tuple <string, string, double>("Milk_intake_incpt", "MilkIntakeIntercept", 1), new Tuple <string, string, double>("Milk_max", "MilkPeakYield", 1), new Tuple <string, string, double>("Milk_offset_day", "MilkOffsetDay", 1), new Tuple <string, string, double>("Milk_Peak_day", "MilkPeakDay", 1), new Tuple <string, string, double>("Mortality_base", "MortalityBase", 0.01), new Tuple <string, string, double>("protein_coeff", "ProteinCoefficient", 1), new Tuple <string, string, double>("Rum_gest_int", "GestationLength", 1), new Tuple <string, string, double>("SRW", "SRWFemale", 1), new Tuple <string, string, double>("Twin_rate", "TwinRate", 1), new Tuple <string, string, double>("wool_coeff", "WoolCoefficient", 1) }; int index = Breeds.IndexOf(ruminant.Breed); foreach (var parameter in parameters) { double value = GetValue <double>(FindFirst(Source, parameter.Item1), index) * parameter.Item3; ruminant.GetType().GetProperty(parameter.Item2).SetValue(ruminant, value); } }
public IEnumerable <RuminantType> GetRuminants(RuminantHerd herd) { List <RuminantType> types = new List <RuminantType>(); // Iterate over all breeds, adding cohorts and pricing to each foreach (string breed in PresentBreeds) { RuminantType type = new RuminantType(herd, breed); SetParameters(type); types.Add(type); } return(types); }
private void OnCLEMInitialiseActivity(object sender, EventArgs e) { this.InitialiseHerd(false, true); breedParams = Resources.GetResourceItem(this, typeof(RuminantHerd), this.PredictedHerdBreed, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; // check GrazeFoodStoreExists if (GrazeFoodStoreName == null) { GrazeFoodStoreName = ""; } if (GrazeFoodStoreName != "") { foodStore = Resources.GetResourceItem(this, typeof(GrazeFoodStore), GrazeFoodStoreName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as GrazeFoodStoreType; } }
private void OnCLEMInitialiseActivity(object sender, EventArgs e) { this.InitialiseHerd(false, false); // check if labour and warn it is not used for this activity labour = Apsim.Children(this, typeof(LabourFilterGroupSpecified)).Cast <LabourFilterGroupSpecified>().ToList(); // this.Children.Where(a => a.GetType() == typeof(LabourFilterGroupSpecified)).Cast<LabourFilterGroupSpecified>().ToList(); if (labour != null) { Summary.WriteWarning(this, "Warning: Labour was supplied for activity [" + this.Name + "] but is not used for Trade activities. Please add labour requirements to the Buy/Sell Activity associated with this trade herd."); } // get herd to add to herdToUse = Resources.GetResourceItem(this, typeof(RuminantHerd), this.PredictedHerdName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; if (!herdToUse.PricingAvailable()) { Summary.WriteWarning(this, "Warning: No pricing is supplied for herd [" + PredictedHerdName + "] and so no pricing will be included with [" + this.Name + "]"); } }
/// <summary> /// Map the IAT parameters to their CLEM counterpart /// </summary> public void SetParameters(RuminantType ruminant) { foreach (var map in Maps) { // Find the subtable which contains the parameter var table = this.GetType().GetProperty(map.Table).GetValue(this, null) as SubTable; // Find the row which contains the parameter (if it exists) int row = table.RowNames.FindIndex(s => s == map.ParamIAT); if (row < 0) { continue; } // Convert the value of the parameter to CLEM double value = table.GetData <double>(row, RumActiveID) * map.Proportion; // Set the value of the CLEM parameter ruminant.GetType().GetProperty(map.ParamCLEM).SetValue(ruminant, value); } }
private void OnCLEMInitialiseActivity(object sender, EventArgs e) { this.InitialiseHerd(false, false); // get herd to add to herdToUse = Resources.FindResourceType <RuminantHerd, RuminantType>(this, this.PredictedHerdName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop); if (!herdToUse.PricingAvailable()) { Summary.WriteMessage(this, "No pricing is supplied for herd [" + PredictedHerdName + "] and so no pricing will be included with [" + this.Name + "]", MessageType.Warning); } // check GrazeFoodStoreExists grazeStore = ""; if (GrazeFoodStoreName != null && !GrazeFoodStoreName.StartsWith("Not specified")) { grazeStore = GrazeFoodStoreName.Split('.').Last(); } // check for managed paddocks and warn if animals placed in yards. if (grazeStore == "") { var ah = this.FindInScope <ActivitiesHolder>(); if (ah.FindAllDescendants <PastureActivityManage>().Count() != 0) { Summary.WriteMessage(this, String.Format("Trade animals purchased by [a={0}] are currently placed in [Not specified - general yards] while a managed pasture is available. These animals will not graze until moved and will require feeding while in yards.\r\nSolution: Set the [GrazeFoodStore to place purchase in] located in the properties [General].[PastureDetails]", this.Name), MessageType.Warning); } } numberToStock = this.FindAllChildren <Relationship>().FirstOrDefault() as Relationship; if (numberToStock != null) { if (grazeStore != "") { foodStore = Resources.FindResourceType <GrazeFoodStore, GrazeFoodStoreType>(this, GrazeFoodStoreName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop); } } }
/// <summary> /// Model all present ruminant breeds /// </summary> public IEnumerable <RuminantType> GetRuminants(RuminantHerd parent) { List <RuminantType> ruminants = new List <RuminantType>(); // Iterate over all the present breeds foreach (int id in RumIDs) { RumActiveID = id; string breed = RumNumbers.ColumnNames[id].Replace(".", ""); RuminantType ruminant = new RuminantType(parent) { Name = breed, Breed = breed }; SetParameters(ruminant); ruminants.Add(ruminant); } return(ruminants); }
public RuminantInitialCohorts(RuminantType parent) : base(parent) { Name = "InitialCohorts"; Add(Source.GetCohorts(this)); }
private void OnCLEMInitialiseActivity(object sender, EventArgs e) { // create local version of max breeders so we can modify without affecting user set value maxBreeders = Math.Max(this.MaximumBreedersKept, this.MinimumBreedersKept); this.InitialiseHerd(false, true); breedParams = Resources.GetResourceItem(this, typeof(RuminantHerd), this.PredictedHerdName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as RuminantType; decimal breederHerdSize = 0; if (AdjustBreedingFemalesAtStartup) { RuminantHerd herd = Resources.RuminantHerd(); List <Ruminant> rumHerd = this.CurrentHerd(false); if (rumHerd != null && rumHerd.Count() > 0) { int numberAdded = 0; RuminantType breedParams = rumHerd.FirstOrDefault().BreedParams; RuminantInitialCohorts cohorts = Apsim.Children(rumHerd.FirstOrDefault().BreedParams, typeof(RuminantInitialCohorts)).FirstOrDefault() as RuminantInitialCohorts; if (cohorts != null) { List <RuminantTypeCohort> cohortList = Apsim.Children(cohorts, typeof(RuminantTypeCohort)).Cast <RuminantTypeCohort>().Where(a => a.Gender == Sex.Female && (a.Age >= breedParams.MinimumAge1stMating & a.Age <= this.MaximumBreederAge)).ToList(); int initialBreeders = Convert.ToInt32(cohortList.Sum(a => a.Number)); if (initialBreeders < this.MinimumBreedersKept) { double scaleFactor = this.MinimumBreedersKept / Convert.ToDouble(initialBreeders); // add new individuals foreach (var item in cohortList) { int numberToAdd = Convert.ToInt32(Math.Round(item.Number * scaleFactor) - item.Number); foreach (var newind in item.CreateIndividuals(numberToAdd)) { newind.SaleFlag = HerdChangeReason.FillInitialHerd; herd.AddRuminant(newind, this); numberAdded++; } } if (numberAdded == 0) { throw new ApsimXException(this, $"Unable to scale breeding female population up to the maximum breeders kept at startup\nNo cohorts representing breeders were found in the initial herd structure [r=InitialCohorts] for [r={breedParams.Name}]\nAdd at least one initial cohort that meets the breeder criteria of age at first mating and max age kept"); } breederHerdSize = initialBreeders + numberAdded; } else if (initialBreeders > maxBreeders) { int reduceBy = Math.Max(0, initialBreeders - maxBreeders); // reduce initial herd size // randomly select the individuals to remove form the breeder herd List <Ruminant> breeders = rumHerd.Where(a => a.Gender == Sex.Female && a.Age > breedParams.MinimumAge1stMating && a.Age < this.MaximumBreederAge).OrderBy(x => Guid.NewGuid()).Take(reduceBy).ToList(); foreach (var item in breeders) { item.SaleFlag = HerdChangeReason.ReduceInitialHerd; herd.RemoveRuminant(item, this); reduceBy--; } if (reduceBy > 0) { // add warning string warn = $"Unable to reduce breeders at the start of the simulation to number required [{maxBreeders}] using [a={this.Name}]"; if (!Warnings.Exists(warn)) { Summary.WriteWarning(this, warn); Warnings.Add(warn); } } breederHerdSize = maxBreeders; } } else { throw new ApsimXException(this, $"Unable to adjust breeding female population to the maximum breeders kept at startup\nNo initial herd structure [r=InitialCohorts] has been provided in [r={breedParams.Name}]"); } } } // max sires if (MaximumSiresKept < 1 & MaximumSiresKept > 0) { SiresKept = Convert.ToInt32(Math.Ceiling(maxBreeders * breederHerdSize), CultureInfo.InvariantCulture); } else { SiresKept = Convert.ToInt32(Math.Truncate(MaximumSiresKept), CultureInfo.InvariantCulture); } if (AdjustBreedingMalesAtStartup) { RuminantHerd herd = Resources.RuminantHerd(); if (herd != null) { // get number in herd List <Ruminant> rumHerd = this.CurrentHerd(false); int numberPresent = rumHerd.Where(a => a.Gender == Sex.Male).Cast <RuminantMale>().Where(a => a.BreedingSire).Count(); if (numberPresent < SiresKept) { // fill to number needed for (int i = numberPresent; i < SiresKept; i++) { RuminantMale newSire = new RuminantMale(SireAgeAtPurchase, Sex.Male, 0, breedParams) { Breed = this.PredictedHerdBreed, HerdName = this.PredictedHerdName, BreedingSire = true, ID = herd.NextUniqueID, SaleFlag = HerdChangeReason.FillInitialHerd }; herd.AddRuminant(newSire, this); } } else if (numberPresent > SiresKept) { // reduce initial herd. int reduceBy = Math.Max(0, numberPresent - SiresKept); // reduce initial sire herd size // randomly select the individuals to remove form the breeder herd List <RuminantMale> sires = rumHerd.Where(a => a.Gender == Sex.Male).Cast <RuminantMale>().Where(a => a.BreedingSire).OrderBy(x => Guid.NewGuid()).Take(reduceBy).ToList(); foreach (var item in sires) { item.SaleFlag = HerdChangeReason.ReduceInitialHerd; herd.RemoveRuminant(item, this); reduceBy--; } if (reduceBy > 0) { // add warning string warn = $"Unable to reduce breeding sires at the start of the simulation to number required [{SiresKept}] using [a={this.Name}]"; if (!Warnings.Exists(warn)) { Summary.WriteWarning(this, warn); Warnings.Add(warn); } } } } } // check GrazeFoodStoreExists for breeders grazeStoreBreeders = ""; if (GrazeFoodStoreNameBreeders != null && !GrazeFoodStoreNameBreeders.StartsWith("Not specified")) { grazeStoreBreeders = GrazeFoodStoreNameBreeders.Split('.').Last(); foodStoreBreeders = Resources.GetResourceItem(this, GrazeFoodStoreNameBreeders, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as GrazeFoodStoreType; } // check for managed paddocks and warn if breeders placed in yards. if (grazeStoreBreeders == "" && this.MaximumProportionBreedersPerPurchase > 0) { var ah = Apsim.Find(this, typeof(ActivitiesHolder)); if (Apsim.ChildrenRecursively(ah, typeof(PastureActivityManage)).Count() != 0) { Summary.WriteWarning(this, String.Format("Breeders purchased by [a={0}] are currently placed in [Not specified - general yards] while a managed pasture is available. These animals will not graze until mustered and will require feeding while in yards.\nSolution: Set the [GrazeFoodStore to place purchase in] located in the properties [General].[PastureDetails]", this.Name)); } } // check GrazeFoodStoreExists for sires grazeStoreSires = ""; if (GrazeFoodStoreNameSires != null && !GrazeFoodStoreNameSires.StartsWith("Not specified")) { grazeStoreSires = GrazeFoodStoreNameSires.Split('.').Last(); foodStoreSires = Resources.GetResourceItem(this, GrazeFoodStoreNameSires, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as GrazeFoodStoreType; } // check for managed paddocks and warn if sires placed in yards. if (grazeStoreBreeders == "" && this.SiresKept > 0) { var ah = Apsim.Find(this, typeof(ActivitiesHolder)); if (Apsim.ChildrenRecursively(ah, typeof(PastureActivityManage)).Count() != 0) { Summary.WriteWarning(this, String.Format("Sires purchased by [a={0}] are currently placed in [Not specified - general yards] while a managed pasture is available. These animals will not graze until mustered and will require feeding while in yards.\nSolution: Set the [GrazeFoodStore to place purchase in] located in the properties [General].[PastureDetails]", this.Name)); } } }