Пример #1
0
        private void OnStartOfSimulation(object sender, EventArgs e)
        {
            // determine resource type from name
            TransmuteResourceType = resources.FindResourceType <ResourceBaseWithTransactions, IResourceType>(this, TransmuteResourceTypeName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore);

            if (TransmuteResourceType != null)
            {
                ResourceGroup = (TransmuteResourceType as IModel).Parent as ResourceBaseWithTransactions;

                var shortfallResourceType = (TransmuteResourceType as IModel).FindAncestor <IResourceType>();
                shortfallPacketSize   = (Parent as Transmutation).TransmutationPacketSize;
                shortfallWholePackets = (Parent as Transmutation).UseWholePackets;

                // get pricing of shortfall resource
                if (shortfallResourceType != null && TransmuteStyle == TransmuteStyle.UsePricing)
                {
                    shortfallPricing      = shortfallResourceType.Price(PurchaseOrSalePricingStyleType.Purchase);
                    shortfallPacketSize   = shortfallPricing.PacketSize;
                    shortfallWholePackets = shortfallPricing.UseWholePackets;

                    // get pricing of transmute resource
                    if (!(TransmuteResourceType is FinanceType))
                    {
                        transmutePricing = TransmuteResourceType.Price(PurchaseOrSalePricingStyleType.Sale);
                        if (FinanceTypeForTransactionsName != "No transactions")
                        {
                            // link to first bank account
                            financeType = resources.FindResourceType <Finance, FinanceType>(this, FinanceTypeForTransactionsName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.ReportWarning);
                        }
                    }
                }
            }
        }
Пример #2
0
 private void OnCLEMInitialiseActivity(object sender, EventArgs e)
 {
     if (TruckMethaneEmissions > 0)
     {
         if (MethaneStoreName is null || MethaneStoreName == "Use store named Methane if present")
         {
             methaneStore = resources.FindResourceType <GreenhouseGases, GreenhouseGasesType>(this, "Methane", OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore);
         }
         else
         {
             methaneStore = resources.FindResourceType <GreenhouseGases, GreenhouseGasesType>(this, MethaneStoreName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop) as GreenhouseGasesType;
         }
     }
Пример #3
0
        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);
        }
Пример #4
0
        /// <inheritdoc/>
        public override string ModelSummary()
        {
            using (StringWriter htmlWriter = new StringWriter())
            {
                if (this.Parent.GetType() != typeof(LabourActivityFeed))
                {
                    htmlWriter.Write("<div class=\"warningbanner\">This Labour Feed Group must be placed beneath a Labour Activity Feed component</div>");
                    return(htmlWriter.ToString());
                }

                LabourFeedActivityTypes ft = (this.Parent as LabourActivityFeed).FeedStyle;
                htmlWriter.Write("\r\n<div class=\"activityentry\">");
                switch (ft)
                {
                case LabourFeedActivityTypes.SpecifiedDailyAmountPerAE:
                case LabourFeedActivityTypes.SpecifiedDailyAmountPerIndividual:
                    htmlWriter.Write("<span class=\"" + ((Value <= 0) ? "errorlink" : "setvalue") + "\">" + Value.ToString() + "</span>");
                    break;

                default:
                    break;
                }

                ZoneCLEM           zoneCLEM  = FindAncestor <ZoneCLEM>();
                ResourcesHolder    resHolder = zoneCLEM.FindChild <ResourcesHolder>();
                HumanFoodStoreType food      = resHolder.FindResourceType <HumanFoodStore, HumanFoodStoreType>(this, (this.Parent as LabourActivityFeed).FeedTypeName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore);
                if (food != null)
                {
                    htmlWriter.Write(" " + food.Units + " ");
                }

                htmlWriter.Write("<span class=\"setvalue\">");
                switch (ft)
                {
                case LabourFeedActivityTypes.SpecifiedDailyAmountPerIndividual:
                    htmlWriter.Write(" per individual per day");
                    break;

                case LabourFeedActivityTypes.SpecifiedDailyAmountPerAE:
                    htmlWriter.Write(" per AE per day");
                    break;

                default:
                    break;
                }
                htmlWriter.Write("</span> ");
                switch (ft)
                {
                case LabourFeedActivityTypes.SpecifiedDailyAmountPerAE:
                case LabourFeedActivityTypes.SpecifiedDailyAmountPerIndividual:
                    htmlWriter.Write("is fed to each individual");
                    break;
                }
                htmlWriter.Write(" that matches the following conditions:");

                htmlWriter.Write("</div>");
                return(htmlWriter.ToString());
            }
        }
Пример #5
0
        /// <summary>
        /// Method to determine available non-labour resources and take if requested.
        /// </summary>
        /// <param name="request">Resource request details</param>
        /// <param name="removeFromResource">Determines if only calculating available labour or labour removed</param>
        /// <returns></returns>
        private double TakeNonLabour(ResourceRequest request, bool removeFromResource)
        {
            // get available resource
            if (request.Resource == null)
                //If it hasn't been assigned try and find it now.
                request.Resource = Resources.FindResourceType<ResourceBaseWithTransactions, IResourceType>(request, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore);

            if (request.Resource != null)
                // get amount available
                request.Available = Math.Min(request.Resource.Amount, request.Required);

            if(removeFromResource && request.Resource != null)
                request.Resource.Remove(request);

            return request.Available;
        }
Пример #6
0
        private void OnStartOfSimulation(object sender, EventArgs e)
        {
            // get herd for transmutating
            ResourceGroup             = resources.FindResourceGroup <RuminantHerd>();
            TransmuteResourceTypeName = ResourceGroup.Name;
            shortfallPacketSize       = (Parent as Transmutation).TransmutationPacketSize;
            shortfallWholePackets     = (Parent as Transmutation).UseWholePackets;
            groupings = ResourceGroup.FindAllChildren <RuminantGroup>();

            var shortfallResourceType = this.FindAncestor <IResourceType>();

            if (shortfallResourceType != null && TransmuteStyle == TransmuteStyle.UsePricing)
            {
                shortfallPricing = shortfallResourceType.Price(PurchaseOrSalePricingStyleType.Purchase);
                if (FinanceTypeForTransactionsName != "No transactions")
                {
                    // link to first bank account
                    financeType = resources.FindResourceType <Finance, FinanceType>(this, FinanceTypeForTransactionsName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.ReportWarning);
                }
            }
        }
Пример #7
0
        private void OnCLEMInitialiseActivity(object sender, EventArgs e)
        {
            people      = Resources.FindResourceGroup <Labour>();
            food        = Resources.FindResourceGroup <HumanFoodStore>();
            bankAccount = Resources.FindResourceType <Finance, FinanceType>(this, AccountName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore);

            Market = food.FindAncestor <ResourcesHolder>().FoundMarket;

            resourcesHolder = base.Resources;
            // if market is present point to market to find the resource
            if (Market != null)
            {
                resourcesHolder = Market.FindChild <ResourcesHolder>();
            }

            // set the food store linked in any TargetPurchase if target proportion set > 0
            // check that all purchase resources have transmutation or recalulate the proportion
            var targetPurchases = this.FindAllChildren <LabourActivityFeedTargetPurchase>().Where(a => a.TargetProportion > 0).ToList();

            if (targetPurchases.Any())
            {
                double checkPropAvailable = 0;
                double totPropAvailable   = 0;
                bool   adjusted           = false;
                foreach (var item in targetPurchases)
                {
                    checkPropAvailable += item.TargetProportion;
                    item.FoodStore      = resourcesHolder.FindResourceType <HumanFoodStore, HumanFoodStoreType>(this, item.FoodStoreName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore);
                    if (item.FoodStore.TransmutationDefined)
                    {
                        totPropAvailable         += item.TargetProportion;
                        item.ProportionToPurchase = item.TargetProportion;
                    }
                    else
                    {
                        string warn = $"The HumanFoodStoreType [r={item.FoodStore.FullPath}] does not have a Transmutation required to be a LabourActivityFeedTargetPurchase [a={item.FullPath}] of [a={this.FullPath}]{Environment.NewLine}This HumanFoodStore will not be allocated and the remaining purchase proportions have been adjusted";
                        Warnings.CheckAndWrite(warn, Summary, this, MessageType.Warning);
                        adjusted = true;
                        item.ProportionToPurchase = 0;
                    }
                }

                if (Math.Abs(1 - checkPropAvailable) < 0.0001)
                {
                    if (!adjusted)
                    {
                        string warn = $"The TargetProportions provided for [a=LabourActivityFeedTargetPurchase] provided for [a={this.FullPath}] do not sum to 1.{Environment.NewLine}These purchase proportions have been adjusted";
                        Warnings.CheckAndWrite(warn, Summary, this, MessageType.Warning);
                    }
                    adjusted = true;
                }

                // recalculate proportions to buy based on transmuation of resource allowed
                if (adjusted)
                {
                    foreach (var item in targetPurchases.Where(a => a.FoodStore.TransmutationDefined))
                    {
                        item.ProportionToPurchase = item.TargetProportion / totPropAvailable;
                    }
                }
            }
        }
Пример #8
0
 private void OnCLEMInitialiseActivity(object sender, EventArgs e)
 {
     BankAccount = resources.FindResourceType <Finance, FinanceType>(this, AccountName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.ReportErrorAndStop);
 }
Пример #9
0
 private void OnCLEMInitialiseActivity(object sender, EventArgs e)
 {
     ResourceTypeModel = resources.FindResourceType <ResourceBaseWithTransactions, IResourceType>(this, ResourceTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop);
 }
        /// <inheritdoc/>
        public override List <ResourceRequest> GetResourcesNeededForActivity()
        {
            if (people is null | food is null)
            {
                return(null);
            }

            List <LabourType> peopleList = people.Items.Where(a => IncludeHiredLabour || a.Hired == false).ToList();

            peopleList.Select(a => { a.FeedToTargetIntake = 0; return(a); }).ToList();

            // determine AEs to be fed
            double aE = peopleList.Sum(a => a.AdultEquivalent);

            if (aE <= 0)
            {
                return(null);
            }

            int daysInMonth = DateTime.DaysInMonth(clock.Today.Year, clock.Today.Month);

            // determine feed limits (max kg per AE per day * AEs * days)
            double intakeLimit = DailyIntakeLimit * aE * daysInMonth;

            // remove previous consumption
            double otherIntake = this.DailyIntakeOtherSources * aE * daysInMonth;

            otherIntake += peopleList.Sum(a => a.GetAmountConsumed());

            List <LabourActivityFeedTarget> labourActivityFeedTargets = this.FindAllChildren <LabourActivityFeedTarget>().ToList();

            // determine targets
            foreach (LabourActivityFeedTarget target in labourActivityFeedTargets)
            {
                // calculate target
                target.Target = target.TargetValue * aE * daysInMonth;

                // set initial level based on off store inputs
                target.CurrentAchieved = target.OtherSourcesValue * aE * daysInMonth;

                // calculate current level from previous intake this month (LabourActivityFeed)
                target.CurrentAchieved += people.GetDietaryValue(target.Metric, IncludeHiredLabour, true) * aE * daysInMonth;

                // add sources outside of this activity to peoples' diets
                if (target.OtherSourcesValue > 0)
                {
                    foreach (var person in peopleList)
                    {
                        LabourDietComponent outsideEat = new LabourDietComponent();
                        outsideEat.AddOtherSource(target.Metric, target.OtherSourcesValue * person.AdultEquivalent * daysInMonth);
                        person.AddIntake(outsideEat);
                    }
                }
            }

            // get max months before spoiling of all food stored (will be zero for non perishable food)
            int maxFoodAge = food.FindAllChildren <HumanFoodStoreType>().Max(a => a.Pools.Select(b => a.UseByAge - b.Age).DefaultIfEmpty(0).Max());

            // create list of all food parcels
            List <HumanFoodParcel> foodParcels = new List <HumanFoodParcel>();

            foreach (HumanFoodStoreType foodStore in food.FindAllChildren <HumanFoodStoreType>().ToList())
            {
                foreach (HumanFoodStorePool pool in foodStore.Pools)
                {
                    foodParcels.Add(new HumanFoodParcel()
                    {
                        FoodStore = foodStore,
                        Pool      = pool,
                        Expires   = ((foodStore.UseByAge == 0) ? maxFoodAge + 1: foodStore.UseByAge - pool.Age)
                    });
                }
            }

            foodParcels = foodParcels.OrderBy(a => a.Expires).ToList();

            // if a market exists add the available market produce to the list below that ordered above.
            // order market food by price ascending
            // this will include market available food in the decisions.
            // will need to purchase this food before taking it if cost associated.
            // We can check if the parent of the human food store used is a market and charge accordingly.

            // for each market
            List <HumanFoodParcel> marketFoodParcels = new List <HumanFoodParcel>();
            ResourcesHolder        resources         = Resources.FoundMarket.Resources;

            if (resources != null)
            {
                HumanFoodStore food = resources.FindResourceGroup <HumanFoodStore>();
                if (food != null)
                {
                    foreach (HumanFoodStoreType foodStore in food.FindAllChildren <HumanFoodStoreType>())
                    {
                        foreach (HumanFoodStorePool pool in foodStore.Pools)
                        {
                            marketFoodParcels.Add(new HumanFoodParcel()
                            {
                                FoodStore = foodStore,
                                Pool      = pool,
                                Expires   = ((foodStore.UseByAge == 0) ? maxFoodAge + 1 : foodStore.UseByAge - pool.Age)
                            });
                        }
                    }
                }
            }
            foodParcels.AddRange(marketFoodParcels.OrderBy(a => a.FoodStore.Price(PurchaseOrSalePricingStyleType.Purchase).PricePerPacket));

            double fundsAvailable = double.PositiveInfinity;

            if (bankAccount != null)
            {
                fundsAvailable = bankAccount.FundsAvailable;
            }

            int    parcelIndex = 0;
            double intake      = otherIntake;

            // start eating food from list from that about to expire first
            while (parcelIndex < foodParcels.Count)
            {
                foodParcels[parcelIndex].Proportion = 0;
                if (intake < intakeLimit & (labourActivityFeedTargets.Where(a => !a.TargetMet).Count() > 0 | foodParcels[parcelIndex].Expires == 0))
                {
                    // still able to eat and target not met or food about to expire this timestep
                    // reduce by amout that can be eaten
                    double propCanBeEaten = Math.Min(1, (intakeLimit - intake) / (foodParcels[parcelIndex].FoodStore.EdibleProportion * foodParcels[parcelIndex].Pool.Amount));
                    // reduce to target limits
                    double propToTarget = 1;
                    if (foodParcels[parcelIndex].Expires != 0)
                    {
                        // if the food is not going to spoil
                        // then adjust what can be eaten up to target otherwise allow over target consumption to avoid waste

                        LabourActivityFeedTarget targetUnfilled = labourActivityFeedTargets.Where(a => !a.TargetMet).FirstOrDefault();
                        if (targetUnfilled != null)
                        {
                            // calculate reduction to metric target
                            double metricneeded = Math.Max(0, targetUnfilled.Target - targetUnfilled.CurrentAchieved);
                            double amountneeded = metricneeded / foodParcels[parcelIndex].FoodStore.ConversionFactor(targetUnfilled.Metric);

                            propToTarget = Math.Min(1, amountneeded / (foodParcels[parcelIndex].FoodStore.EdibleProportion * foodParcels[parcelIndex].Pool.Amount));
                        }
                    }

                    foodParcels[parcelIndex].Proportion = Math.Min(propCanBeEaten, propToTarget);

                    // work out if there will be a cost limitation, only if a price structure exists for the resource
                    double propToPrice = 1;
                    if (foodParcels[parcelIndex].FoodStore.PricingExists(PurchaseOrSalePricingStyleType.Purchase))
                    {
                        ResourcePricing price = foodParcels[parcelIndex].FoodStore.Price(PurchaseOrSalePricingStyleType.Purchase);
                        double          cost  = (foodParcels[parcelIndex].Pool.Amount * foodParcels[parcelIndex].Proportion) / price.PacketSize * price.PricePerPacket;
                        //if (cost > 0 && cost > fundsAvailable)
                        //{
                        //    // need to get some more money

                        //    // sell cattle based on selling groups till run out of cattle or meet shortfall

                        //    // adjust fundsAvailable with new money

                        //}
                        if (cost > 0)
                        {
                            propToPrice = Math.Min(1, fundsAvailable / cost);
                            // remove cost from running check tally
                            fundsAvailable = Math.Max(0, fundsAvailable - (cost * propToPrice));

                            // real finance transactions will happen in the do activity as stuff is allocated
                            // there should not be shortfall as all the checks and reductions have happened here
                        }
                    }
                    foodParcels[parcelIndex].Proportion *= propToPrice;

                    // update intake
                    double newIntake = (foodParcels[parcelIndex].FoodStore.EdibleProportion * foodParcels[parcelIndex].Pool.Amount * foodParcels[parcelIndex].Proportion);
                    intake += newIntake;
                    // update metrics
                    foreach (LabourActivityFeedTarget target in labourActivityFeedTargets)
                    {
                        target.CurrentAchieved += newIntake * foodParcels[parcelIndex].FoodStore.ConversionFactor(target.Metric);
                    }
                }
                else if (intake >= intakeLimit && labourActivityFeedTargets.Where(a => !a.TargetMet).Count() > 1)
                {
                    // full but could still reach target with some substitution
                    // but can substitute to remove a previous target

                    // does the current parcel have better target values than any previous non age 0 pool of a different food type
                }
                else
                {
                    break;
                }
                parcelIndex++;
            }

            // fill resource requests
            List <ResourceRequest> requests = new List <ResourceRequest>();

            foreach (var item in foodParcels.GroupBy(a => a.FoodStore))
            {
                double amount = item.Sum(a => a.Pool.Amount * a.Proportion);
                if (amount > 0)
                {
                    double financeLimit = 1;
                    // if obtained from the market make financial transaction before taking
                    ResourcePricing price = item.Key.Price(PurchaseOrSalePricingStyleType.Sale);
                    if (bankAccount != null && item.Key.Parent.Parent.Parent == Market && price.PricePerPacket > 0)
                    {
                        // if shortfall reduce purchase
                        ResourceRequest marketRequest = new ResourceRequest
                        {
                            ActivityModel               = this,
                            Required                    = amount / price.PacketSize * price.PricePerPacket,
                            AllowTransmutation          = false,
                            Category                    = TransactionCategory,
                            MarketTransactionMultiplier = 1
                        };
                        bankAccount.Remove(marketRequest);
                    }

                    requests.Add(new ResourceRequest()
                    {
                        Resource           = item.Key,
                        ResourceType       = typeof(HumanFoodStore),
                        AllowTransmutation = false,
                        Required           = amount * financeLimit,
                        ResourceTypeName   = item.Key.Name,
                        ActivityModel      = this,
                        Category           = TransactionCategory
                    });
                }
            }

            // if still hungry and funds available, try buy food in excess of what stores (private or market) offered using transmutation if present.
            // This will force the market or private sources to purchase more food to meet demand if transmutation available.
            // if no market is present it will look to transmutating from its own stores if possible.
            // this means that other than a purchase from market (above) this activity doesn't need to worry about financial tranactions.
            if (intake < intakeLimit && (labourActivityFeedTargets.Where(a => !a.TargetMet).Count() > 0) && fundsAvailable > 0)
            {
                ResourcesHolder resourcesHolder = base.Resources;
                // if market is present point to market to find the resource
                if (Market != null)
                {
                    resourcesHolder = Market.FindChild <ResourcesHolder>();
                }

                // don't worry about money anymore. The over request will be handled by the transmutation.
                // move through specified purchase list
                foreach (LabourActivityFeedTargetPurchase purchase in this.FindAllChildren <LabourActivityFeedTargetPurchase>())
                {
                    HumanFoodStoreType foodtype = resourcesHolder.FindResourceType <HumanFoodStore, HumanFoodStoreType>(this, purchase.FoodStoreName, OnMissingResourceActionTypes.Ignore, OnMissingResourceActionTypes.Ignore);
                    if (foodtype != null && (foodtype.TransmutationDefined & intake < intakeLimit))
                    {
                        LabourActivityFeedTarget targetUnfilled = labourActivityFeedTargets.Where(a => !a.TargetMet).FirstOrDefault();
                        if (targetUnfilled != null)
                        {
                            // calculate reduction to metric target
                            double metricneeded = Math.Max(0, (targetUnfilled.Target - targetUnfilled.CurrentAchieved));
                            double amountneeded = metricneeded / foodtype.ConversionFactor(targetUnfilled.Metric);

                            if (intake + amountneeded > intakeLimit)
                            {
                                amountneeded = intakeLimit - intake;
                            }

                            double amountfood = amountneeded / foodtype.EdibleProportion;

                            // update intake
                            intake += amountfood;

                            // find in requests or create a new one
                            ResourceRequest foodRequestFound = requests.Find(a => a.Resource == foodtype);
                            if (foodRequestFound is null)
                            {
                                requests.Add(new ResourceRequest()
                                {
                                    Resource           = foodtype,
                                    ResourceType       = typeof(HumanFoodStore),
                                    AllowTransmutation = true,
                                    Required           = amountfood,
                                    ResourceTypeName   = purchase.FoodStoreName.Split('.')[1],
                                    ActivityModel      = this,
                                    Category           = TransactionCategory
                                });
                            }
                            else
                            {
                                foodRequestFound.Required          += amountneeded;
                                foodRequestFound.AllowTransmutation = true;
                            }
                        }
                    }
                }
                // NOTE: proportions of purchased food are not modified if the sum does not add up to 1 or some of the food types are not available.
            }
            return(requests);
        }
Пример #11
0
 private void OnCLEMInitialiseActivity(object sender, EventArgs e)
 {
     GrazeFoodStoreModel = resources.FindResourceType <GrazeFoodStore, GrazeFoodStoreType>(this, GrazeFoodStoreTypeName, OnMissingResourceActionTypes.ReportErrorAndStop, OnMissingResourceActionTypes.ReportErrorAndStop);
 }