/// <summary> /// Read parameters from json resource. /// </summary> private Genotype ReadParametersFromResource() { List <Exception> exceptions; var simulations = FileFormat.ReadFromString <Simulations>(ReflectionUtilities.GetResourceAsString(nameOfStockResource), out exceptions); if (exceptions.Count > 0) { throw new Exception($"Invalid stock resource found. Name of resource: {nameOfStockResource}"); } parameters = simulations.Children[0] as Genotype; return(parameters); }
/// <summary> /// Default fibre diameter as a function of age, sex, time since shearing and fleece weight /// </summary> /// <param name="Params"></param> /// <param name="iAgeDays"></param> /// <param name="Repr"></param> /// <param name="iFleeceDays"></param> /// <param name="fGFW"></param> /// <returns></returns> static public double DefaultMicron(Genotype Params, int iAgeDays, GrazType.ReproType Repr, int iFleeceDays, double fGFW) { double fPotFleece; if ((iFleeceDays > 0) && (fGFW > 0.0)) { fPotFleece = DefaultFleece(Params, iAgeDays, Repr, iFleeceDays); return(Params.MaxFleeceDiam * Math.Pow(fGFW / fPotFleece, Params.WoolC[13])); } else { return(Params.MaxFleeceDiam); } }
/// <summary>Construct an animal parameter set from a source parameters set.</summary> /// <param name="srcSet">The source parameter set.</param> public Genotype(Genotype srcSet) { //create a new array for (int i = 0; i < ConceiveSigs.Length; i++) { ConceiveSigs[i] = new double[2]; } CopyParams(srcSet); if (srcSet != null) { Initialise(); } }
/// <summary> /// Read parameters from json resource. /// </summary> private Genotype ReadParametersFromResource() { Simulations simulations; try { simulations = FileFormat.ReadFromString <Simulations>(ReflectionUtilities.GetResourceAsString(nameOfStockResource), e => throw e, false); } catch (Exception err) { throw new Exception($"Invalid stock resource found. Name of resource: {nameOfStockResource}", err); } parameters = simulations.Children[0] as Genotype; return(parameters); }
/// <summary> /// Set parentage of a genotype. /// </summary> /// <param name="parentBreed">The parent breed.</param> /// <param name="proportion">The proportion of the parent.</param> private void SetParentage(Genotype parentBreed, double proportion) { for (int k = 0; k <= parentBreed.FParentage.Length - 1; k++) { int i = 0; while ((i < FParentage.Length) && (parentBreed.FParentage[k].sBaseBreed != FParentage[i].sBaseBreed)) { i++; } if (i == FParentage.Length) { Array.Resize(ref FParentage, i + 1); FParentage[i].sBaseBreed = parentBreed.FParentage[k].sBaseBreed; FParentage[i].fPropn = 0.0; } FParentage[i].fPropn = FParentage[i].fPropn + proportion * parentBreed.FParentage[k].fPropn; } }
/// <summary> /// Default fleece weight as a function of age, sex and time since shearing /// </summary> /// <param name="Params"></param> /// <param name="iAgeDays"></param> /// <param name="Repr"></param> /// <param name="iFleeceDays"></param> /// <returns></returns> static public double DefaultFleece(Genotype Params, int iAgeDays, GrazType.ReproType Repr, int iFleeceDays) { double Result; double fMeanAgeFactor; iFleeceDays = Math.Min(iFleeceDays, iAgeDays); if ((Params.Animal == GrazType.AnimalType.Sheep) && (iFleeceDays > 0)) { fMeanAgeFactor = 1.0 - (1.0 - Params.WoolC[5]) * (Math.Exp(-Params.WoolC[12] * (iAgeDays - iFleeceDays)) - Math.Exp(-Params.WoolC[12] * iAgeDays)) / (Params.WoolC[12] * iFleeceDays); Result = Params.FleeceRatio * Params.SexStdRefWt(Repr) * fMeanAgeFactor * iFleeceDays / 365.0; } else { Result = 0.0; } return(Result); }
/// <summary> /// Create a genotype cross. /// </summary> /// <param name="nameOfNewGenotype">Name of new genotype. Can be null.</param> /// <param name="damBreedName">Dam breed name.</param> /// <param name="damProportion">Proportion dam.</param> /// <param name="sireBreedName">Sire breed name.</param> /// <param name="sireProportion">Proportion sire.</param> private Genotype CreateGenotypeCross(string nameOfNewGenotype, string damBreedName, double damProportion, string sireBreedName, double sireProportion) { if (damProportion + sireProportion != 1) { throw new Exception("When creating a cross breed the total proportions must be equal to one."); } var damBreed = stock.Genotypes.Get(damBreedName); damBreed.Initialise(); var sireBreed = stock.Genotypes.Get(sireBreedName); sireBreed.Initialise(); Genotype newGenotype = new Genotype(nameOfNewGenotype, damBreed, sireBreed, damProportion, sireProportion); // Add the new genotype into our list. stock.Genotypes.Add(newGenotype); return(newGenotype); }
/// <summary>Constructor</summary> /// <param name="animalParameterSet"></param> public GenotypeWrapper(Genotype animalParameterSet) { parameters = animalParameterSet; Name = animalParameterSet.Name; AnimalType = animalParameterSet.Animal.ToString(); }
/// <summary> /// Constructor for a breed cross. Blend two animal parameter sets. /// </summary> /// <param name="nameOfNewGenotype">The name given to the new parameter set.</param> /// <param name="damBreed">The dam breed.</param> /// <param name="sireBreed">The sire breed.</param> /// <param name="damProportion">The dam proportion.</param> /// <param name="sireProportion">The sire proportion.</param> /// <returns></returns> public Genotype(string nameOfNewGenotype, Genotype damBreed, Genotype sireBreed, double damProportion, double sireProportion) { sEditor = damBreed.sEditor; sEditDate = damBreed.sEditDate; Animal = damBreed.Animal; bDairyBreed = damBreed.bDairyBreed; MaxYoung = damBreed.MaxYoung; OvulationPeriod = damBreed.OvulationPeriod; Puberty = damBreed.Puberty; BreedSRW = damProportion * damBreed.BreedSRW + sireProportion * sireBreed.BreedSRW; PotFleeceWt = damProportion * damBreed.PotFleeceWt + sireProportion * sireBreed.PotFleeceWt; FDairyIntakePeak = damProportion * damBreed.FDairyIntakePeak + sireProportion * sireBreed.FDairyIntakePeak; FleeceRatio = damProportion * damBreed.FleeceRatio + sireProportion * sireBreed.FleeceRatio; MaxFleeceDiam = damProportion * damBreed.MaxFleeceDiam + sireProportion * sireBreed.MaxFleeceDiam; PeakMilk = damProportion * damBreed.PeakMilk + sireProportion * sireBreed.PeakMilk; for (int idx = 1; idx <= 2; idx++) { MortRate[idx] = damProportion * damBreed.MortRate[idx] + sireProportion * sireBreed.MortRate[idx]; } for (int idx = 1; idx <= 2; idx++) { MortAge[idx] = damProportion * damBreed.MortAge[idx] + sireProportion * sireBreed.MortAge[idx]; } MortIntensity = damProportion * damBreed.MortIntensity + sireProportion * sireBreed.MortIntensity; MortCondConst = damProportion * damBreed.MortCondConst + sireProportion * sireBreed.MortCondConst; MortWtDiff = damProportion * damBreed.MortWtDiff + sireProportion * sireBreed.MortWtDiff; for (int idx = 0; idx < SRWScalars.Length; idx++) { SRWScalars[idx] = damProportion * damBreed.SRWScalars[idx] + sireProportion * sireBreed.SRWScalars[idx]; } for (int idx = 1; idx < GrowthC.Length; idx++) { GrowthC[idx] = damProportion * damBreed.GrowthC[idx] + sireProportion * sireBreed.GrowthC[idx]; } for (int idx = 1; idx < IntakeC.Length; idx++) { IntakeC[idx] = damProportion * damBreed.IntakeC[idx] + sireProportion * sireBreed.IntakeC[idx]; } for (int idx = 0; idx < IntakeLactC.Length; idx++) { IntakeLactC[idx] = damProportion * damBreed.IntakeLactC[idx] + sireProportion * sireBreed.IntakeLactC[idx]; } for (int idx = 1; idx < GrazeC.Length; idx++) { GrazeC[idx] = damProportion * damBreed.GrazeC[idx] + sireProportion * sireBreed.GrazeC[idx]; } for (int idx = 1; idx < EfficC.Length; idx++) { EfficC[idx] = damProportion * damBreed.EfficC[idx] + sireProportion * sireBreed.EfficC[idx]; } for (int idx = 1; idx < MaintC.Length; idx++) { MaintC[idx] = damProportion * damBreed.MaintC[idx] + sireProportion * sireBreed.MaintC[idx]; } for (int idx = 1; idx < DgProtC.Length; idx++) { DgProtC[idx] = damProportion * damBreed.DgProtC[idx] + sireProportion * sireBreed.DgProtC[idx]; } for (int idx = 1; idx < ProtC.Length; idx++) { ProtC[idx] = damProportion * damBreed.ProtC[idx] + sireProportion * sireBreed.ProtC[idx]; } for (int idx = 1; idx < PregC.Length; idx++) { PregC[idx] = damProportion * damBreed.PregC[idx] + sireProportion * sireBreed.PregC[idx]; } for (int idx = 1; idx < PregScale.Length; idx++) { PregScale[idx] = damProportion * damBreed.PregScale[idx] + sireProportion * sireBreed.PregScale[idx]; } for (int idx = 1; idx < BirthWtScale.Length; idx++) { BirthWtScale[idx] = damProportion * damBreed.BirthWtScale[idx] + sireProportion * sireBreed.BirthWtScale[idx]; } for (int idx = 1; idx < PeakLactC.Length; idx++) { PeakLactC[idx] = damProportion * damBreed.PeakLactC[idx] + sireProportion * sireBreed.PeakLactC[idx]; } for (int idx = 1; idx < LactC.Length; idx++) { LactC[idx] = damProportion * damBreed.LactC[idx] + sireProportion * sireBreed.LactC[idx]; } for (int idx = 1; idx < WoolC.Length; idx++) { WoolC[idx] = damProportion * damBreed.WoolC[idx] + sireProportion * sireBreed.WoolC[idx]; } for (int idx = 1; idx < ChillC.Length; idx++) { ChillC[idx] = damProportion * damBreed.ChillC[idx] + sireProportion * sireBreed.ChillC[idx]; } for (int idx = 1; idx < GainC.Length; idx++) { GainC[idx] = damProportion * damBreed.GainC[idx] + sireProportion * sireBreed.GainC[idx]; } for (int idx = 1; idx < PhosC.Length; idx++) { PhosC[idx] = damProportion * damBreed.PhosC[idx] + sireProportion * sireBreed.PhosC[idx]; } for (int idx = 1; idx < SulfC.Length; idx++) { SulfC[idx] = damProportion * damBreed.SulfC[idx] + sireProportion * sireBreed.SulfC[idx]; } for (int idx = 1; idx < MethC.Length; idx++) { MethC[idx] = damProportion * damBreed.MethC[idx] + sireProportion * sireBreed.MethC[idx]; } for (int idx = 1; idx < AshAlkC.Length; idx++) { AshAlkC[idx] = damProportion * damBreed.AshAlkC[idx] + sireProportion * sireBreed.AshAlkC[idx]; } for (int idx = 1; idx < DayLengthConst.Length; idx++) { DayLengthConst[idx] = damProportion * damBreed.DayLengthConst[idx] + sireProportion * sireBreed.DayLengthConst[idx]; } for (int idx = 0; idx < ToxaemiaSigs.Length; idx++) { ToxaemiaSigs[idx] = damProportion * damBreed.ToxaemiaSigs[idx] + sireProportion * sireBreed.ToxaemiaSigs[idx]; } for (int idx = 0; idx < DystokiaSigs.Length; idx++) { DystokiaSigs[idx] = damProportion * damBreed.DystokiaSigs[idx] + sireProportion * sireBreed.DystokiaSigs[idx]; } for (int idx = 0; idx < ExposureConsts.Length; idx++) { ExposureConsts[idx] = damProportion * damBreed.ExposureConsts[idx] + sireProportion * sireBreed.ExposureConsts[idx]; } FertWtDiff = damProportion * damBreed.FertWtDiff + sireProportion * sireBreed.FertWtDiff; SelfWeanPropn = damProportion * damBreed.SelfWeanPropn + sireProportion * sireBreed.SelfWeanPropn; for (int i = 0; i < ConceiveSigs.Length; i++) { ConceiveSigs[i] = new double[2]; } for (int idx = 1; idx < ConceiveSigs.Length; idx++) { for (int Jdx = 0; Jdx < ConceiveSigs[idx].Length; Jdx++) { ConceiveSigs[idx][Jdx] = damProportion * damBreed.ConceiveSigs[idx][Jdx] + sireProportion * sireBreed.ConceiveSigs[idx][Jdx]; } } for (int idx = 0; idx <= FParentage.Length - 1; idx++) { FParentage[idx].fPropn = 0.0; } SetParentage(damBreed, damProportion); SetParentage(sireBreed, sireProportion); if (!string.IsNullOrEmpty(nameOfNewGenotype)) { Name = nameOfNewGenotype; } else if (FParentage.Length == 1) { Name = FParentage[0].sBaseBreed; } else if (FParentage.Length > 1) { int iDecPlaces = 0; for (int Idx = 0; Idx <= FParentage.Length - 1; Idx++) { if ((FParentage[Idx].fPropn > 0.0005) && (FParentage[Idx].fPropn <= 0.05)) { iDecPlaces = 1; } } Name = ""; for (int Idx = 0; Idx <= FParentage.Length - 1; Idx++) { if (FParentage[Idx].fPropn > 0.0005) { if (Name != "") { Name = Name + ", "; } Name = Name + FParentage[Idx].sBaseBreed + " " + String.Format("{0:0." + new String('0', iDecPlaces) + "}", 100.0 * FParentage[Idx].fPropn) + "%"; } } } }
/// <summary> /// Copies all the animal parameters /// </summary> /// <param name="srcSet">Parameter set to copy from.</param> private void CopyParams(Genotype srcSet) { int Idx; Name = srcSet.Name; DeriveParams(); Genotype prmSet = (Genotype)srcSet; if (prmSet != null) { BreedSRW = prmSet.BreedSRW; PotFleeceWt = prmSet.PotFleeceWt; FDairyIntakePeak = prmSet.FDairyIntakePeak; Array.Resize(ref FParentage, prmSet.FParentage.Length); for (Idx = 0; Idx <= FParentage.Length - 1; Idx++) { FParentage[Idx] = prmSet.FParentage[Idx]; } sEditor = prmSet.sEditor; sEditDate = prmSet.sEditDate; Animal = prmSet.Animal; MaxYoung = prmSet.MaxYoung; Array.Copy(prmSet.SRWScalars, SRWScalars, prmSet.SRWScalars.Length); MaxFleeceDiam = prmSet.MaxFleeceDiam; bDairyBreed = prmSet.bDairyBreed; PeakMilk = prmSet.PeakMilk; Array.Copy(prmSet.MortRate, MortRate, prmSet.MortRate.Length); Array.Copy(prmSet.MortAge, MortAge, prmSet.MortAge.Length); MortIntensity = prmSet.MortIntensity; MortCondConst = prmSet.MortCondConst; MortWtDiff = prmSet.MortWtDiff; Array.Copy(prmSet.GrowthC, GrowthC, prmSet.GrowthC.Length); Array.Copy(prmSet.IntakeC, IntakeC, prmSet.IntakeC.Length); Array.Copy(prmSet.IntakeLactC, IntakeLactC, prmSet.IntakeLactC.Length); Array.Copy(prmSet.GrazeC, GrazeC, prmSet.GrazeC.Length); Array.Copy(prmSet.EfficC, EfficC, prmSet.EfficC.Length); Array.Copy(prmSet.MaintC, MaintC, prmSet.MaintC.Length); Array.Copy(prmSet.DgProtC, DgProtC, prmSet.DgProtC.Length); Array.Copy(prmSet.ProtC, ProtC, prmSet.ProtC.Length); Array.Copy(prmSet.PregC, PregC, prmSet.PregC.Length); Array.Copy(prmSet.PregScale, PregScale, prmSet.PregScale.Length); Array.Copy(prmSet.BirthWtScale, BirthWtScale, prmSet.BirthWtScale.Length); Array.Copy(prmSet.PeakLactC, PeakLactC, prmSet.PeakLactC.Length); Array.Copy(prmSet.LactC, LactC, prmSet.LactC.Length); Array.Copy(prmSet.WoolC, WoolC, prmSet.WoolC.Length); Array.Copy(prmSet.ChillC, ChillC, prmSet.ChillC.Length); Array.Copy(prmSet.GainC, GainC, prmSet.GainC.Length); Array.Copy(prmSet.PhosC, PhosC, prmSet.PhosC.Length); Array.Copy(prmSet.SulfC, SulfC, prmSet.SulfC.Length); Array.Copy(prmSet.MethC, MethC, prmSet.MethC.Length); Array.Copy(prmSet.AshAlkC, AshAlkC, prmSet.AshAlkC.Length); OvulationPeriod = prmSet.OvulationPeriod; Array.Copy(prmSet.Puberty, Puberty, prmSet.Puberty.Length); Array.Copy(prmSet.DayLengthConst, DayLengthConst, prmSet.DayLengthConst.Length); for (int i = 0; i < prmSet.ConceiveSigs.Length; i++) { Array.Copy(prmSet.ConceiveSigs[i], ConceiveSigs[i], prmSet.ConceiveSigs[i].Length); } FertWtDiff = prmSet.FertWtDiff; Array.Copy(prmSet.ToxaemiaSigs, ToxaemiaSigs, prmSet.ToxaemiaSigs.Length); Array.Copy(prmSet.DystokiaSigs, DystokiaSigs, prmSet.DystokiaSigs.Length); Array.Copy(prmSet.ExposureConsts, ExposureConsts, prmSet.ExposureConsts.Length); SelfWeanPropn = prmSet.SelfWeanPropn; } }
/// <summary>Set the user specified genotypes.</summary> /// <param name="animalParameterSet">The user specified animal parameter set.</param> public void Add(Genotype animalParameterSet) { Add(new GenotypeWrapper(animalParameterSet)); }