コード例 #1
0
        private void OnWFAnimalSell(object sender, EventArgs e)
        {
            RuminantHerd ruminantHerd = Resources.RuminantHerd();

            Finance     Accounts    = Resources.FinanceResource() as Finance;
            FinanceType bankAccount = Accounts.GetFirst();

            int    trucks     = 0;
            double saleValue  = 0;
            double saleWeight = 0;
            int    head       = 0;

            // get current untrucked list of animals flagged for sale
            List <Ruminant> herd = ruminantHerd.Herd.Where(a => a.SaleFlag != Common.HerdChangeReason.None & a.Breed == BreedName).OrderByDescending(a => a.Weight).ToList();

            // if sale herd > min loads before allowing sale
            if (herd.Select(a => a.Weight / 450.0).Sum() / Number450kgPerTruck >= MinimumTrucksBeforeSelling)
            {
                // while truck to fill
                while (herd.Select(a => a.Weight / 450.0).Sum() / Number450kgPerTruck > MinimumLoadBeforeSelling)
                {
                    bool nonloaded = true;
                    trucks++;
                    double load450kgs = 0;
                    // while truck below carrying capacity load individuals
                    foreach (var ind in herd)
                    {
                        if (load450kgs + (ind.Weight / 450.0) <= Number450kgPerTruck)
                        {
                            nonloaded = false;
                            head++;
                            load450kgs += ind.Weight / 450.0;
                            RuminantValue getvalue = PriceList.Where(a => a.Age < ind.Age).OrderBy(a => a.Age).LastOrDefault();
                            saleValue  += getvalue.SellValue * ((getvalue.Style == Common.PricingStyleType.perKg) ? ind.Weight : 1.0);
                            saleWeight += ind.Weight;
                            ruminantHerd.RemoveRuminant(ind);
                        }
                    }
                    if (nonloaded)
                    {
                        Summary.WriteWarning(this, String.Format("There was a problem loading the sale truck as sale individuals did not meet the loading criteria for breed {0}", BreedName));
                        break;
                    }
                    herd = ruminantHerd.Herd.Where(a => a.SaleFlag != Common.HerdChangeReason.None & a.Breed == BreedName).OrderByDescending(a => a.Weight).ToList();
                }

                if (trucks > 0 & bankAccount != null)
                {
                    // calculate transport costs
                    double transportCost = trucks * DistanceToMarket * CostPerKmTrucking;
                    bankAccount.Remove(transportCost, this.Name, "Transport");
                    // calculate MLA fees
                    double mlaCost = head * MLAFees;
                    bankAccount.Remove(mlaCost, this.Name, "R&DFee");
                    // calculate yard fees
                    double yardCost = head * YardFees;
                    bankAccount.Remove(yardCost, this.Name, "YardCosts");
                    // calculate commission
                    double commissionCost = saleValue * SalesCommission;
                    bankAccount.Remove(commissionCost, this.Name, "SalesCommission");

                    // add and remove from bank
                    bankAccount.Add(saleValue, this.Name, "Sales");
                }
            }
        }
コード例 #2
0
        private void OnWFAnimalDeath(object sender, EventArgs e)
        {
            // remove individuals that died
            // currently performed in the month after weight has been adjusted
            // and before breeding, trading, culling etc (See Clock event order)

            // Calculated by
            // critical weight &
            // juvenile (unweaned) death based on mothers weight &
            // adult weight adjusted base mortality.

            RuminantHerd    ruminantHerd = Resources.RuminantHerd();
            List <Ruminant> herd         = ruminantHerd.Herd;

            // weight based mortality
            List <Ruminant> died = herd.Where(a => a.Weight < (a.HighWeight * (1.0 - a.BreedParams.ProportionOfMaxWeightToSurvive))).ToList();

            // set died flag
            died.Select(a => { a.SaleFlag = Common.HerdChangeReason.Died; return(a); }).ToList();
            ruminantHerd.RemoveRuminant(died);

            TMyRandom randomGenerator = new TMyRandom(10);

            foreach (var ind in ruminantHerd.Herd)
            {
                double mortalityRate = 0;
                if (!ind.Weaned)
                {
                    mortalityRate = 0;
                    if (ind.Mother.Weight < ind.BreedParams.CriticalCowWeight * ind.StandardReferenceWeight)
                    {
                        mortalityRate = ind.BreedParams.JuvenileMortalityMaximum;
                    }
                    else
                    {
                        mortalityRate = Math.Exp(-Math.Pow(ind.BreedParams.JuvenileMortalityCoefficient * (ind.Weight / ind.NormalisedAnimalWeight), ind.BreedParams.JuvenileMortalityExponent)) / 100;
                    }
                    mortalityRate += mortalityRate + ind.BreedParams.MortalityBase;
                    mortalityRate  = Math.Max(mortalityRate, ind.BreedParams.JuvenileMortalityMaximum);
                }
                else
                {
                    mortalityRate = 1 - (1 - ind.BreedParams.MortalityBase) * (1 - Math.Exp(Math.Pow(-(ind.BreedParams.MortalityCoefficient * (ind.Weight / ind.NormalisedAnimalWeight - ind.BreedParams.MortalityIntercept)), ind.BreedParams.MortalityExponent)));
                }
                if (randomGenerator.RandNo <= mortalityRate)
                {
                    ind.Died = true;
                }
            }

            died = herd.Where(a => a.Died).ToList();
            died.Select(a => { a.SaleFlag = Common.HerdChangeReason.Died; return(a); }).ToList();
            ruminantHerd.RemoveRuminant(died);

            //// mortality calculation (calculation actually calculates the survival probability)
            //for (int i = herd.Count; i >= 0; i--)
            //{
            //	double mortalityRate = 0;
            //	if (!herd[i].Weaned)
            //	{
            //		mortalityRate = 0;
            //		if (herd[i].Mother.Weight < herd[i].BreedParams.CriticalCowWeight * herd[i].StandardReferenceWeight)
            //		{
            //			mortalityRate = herd[i].BreedParams.JuvenileMortalityMaximum;
            //		}
            //		else
            //		{
            //			mortalityRate = Math.Exp(-Math.Pow(herd[i].BreedParams.JuvenileMortalityCoefficient * (herd[i].Weight / herd[i].NormalisedAnimalWeight), herd[i].BreedParams.JuvenileMortalityExponent)) / 100;
            //		}
            //		mortalityRate += mortalityRate + herd[i].BreedParams.MortalityBase;
            //		mortalityRate = Math.Max(mortalityRate, herd[i].BreedParams.JuvenileMortalityMaximum);
            //	}
            //	else
            //	{
            //		mortalityRate = 1 - (1 - herd[i].BreedParams.MortalityBase) * (1 - Math.Exp(Math.Pow(-(herd[i].BreedParams.MortalityCoefficient * (herd[i].Weight / herd[i].NormalisedAnimalWeight - herd[i].BreedParams.MortalityIntercept)), herd[i].BreedParams.MortalityExponent)));
            //	}
            //	if (randomGenerator.RandNo <= mortalityRate)
            //	{
            //		herd[i].Died = true;
            //	}
            //}
        }