Exemple #1
0
        private void OnStartOfSimulation(object sender, EventArgs e)
        {
            id   = 1;
            Herd = new List <Ruminant>();
            PurchaseIndividuals   = new List <Ruminant>();
            LastIndividualChanged = new Ruminant();

            List <IModel> childNodes = Apsim.Children(this, typeof(IModel));

            foreach (IModel childModel in childNodes)
            {
                //cast the generic IModel to a specfic model.
                RuminantType ruminantType = childModel as RuminantType;
                foreach (var ind in ruminantType.CreateIndividuals())
                {
                    ind.SaleFlag = Common.HerdChangeReason.InitialHerd;
                    AddRuminant(ind);
                }
            }
        }
        /// <summary>
        /// Create the individual ruminant animals using the Cohort parameterisations.
        /// </summary>
        /// <returns></returns>
        public List <Ruminant> CreateIndividuals()
        {
            List <Ruminant> Individuals = new List <Ruminant>();

            IModel       parentNode = Apsim.Parent(this, typeof(IModel));
            RuminantType parent     = parentNode as RuminantType;

            // get Ruminant Herd resource for unique ids
            RuminantHerd ruminantHerd = Resources.RuminantHerd();

            parent = this.Parent as RuminantType;

            if (StartingNumber > 0)
            {
                //TODO: get random generator from global store with seed
                Random rand = new Random();
                for (int i = 1; i <= StartingNumber; i++)
                {
                    object ruminantBase = null;
                    if (this.Gender == Sex.Male)
                    {
                        ruminantBase = new RuminantMale();
                    }
                    else
                    {
                        ruminantBase = new RuminantFemale();
                    }

                    Ruminant ruminant = ruminantBase as Ruminant;

                    ruminant.ID          = ruminantHerd.NextUniqueID;
                    ruminant.BreedParams = parent;
                    ruminant.Breed       = parent.Breed;
                    ruminant.HerdName    = parent.Name;
                    ruminant.Gender      = Gender;
                    ruminant.Age         = StartingAge;
                    ruminant.SaleFlag    = Common.HerdChangeReason.None;
                    if (Suckling)
                    {
                        ruminant.SetUnweaned();
                    }

                    double u1            = rand.NextDouble();
                    double u2            = rand.NextDouble();
                    double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
                                           Math.Sin(2.0 * Math.PI * u2);
                    ruminant.Weight         = StartingWeight + StartingWeightSD * randStdNormal;
                    ruminant.PreviousWeight = ruminant.Weight;

                    if (this.Gender == Sex.Female)
                    {
                        RuminantFemale ruminantFemale = ruminantBase as RuminantFemale;
                        ruminantFemale.DryBreeder         = true;
                        ruminantFemale.WeightAtConception = this.StartingWeight;
                        ruminantFemale.NumberOfBirths     = 0;
                    }

                    Individuals.Add(ruminantBase as Ruminant);
                }
            }

            return(Individuals);
        }