CorePyomo

Contains constraints for core equations and objective.

author: young

AfoLogic.CorePyomo.coremodel_all(trial_name, model, method, nv, print_debug_output, MP_lp_vars)

Wraps all of the core model into a function so it can be run multiple times in a loop

AfoLogic.CorePyomo.f_con_MP(model, MP_lp_vars)

‘ These constraints are to bound the variable levels in the first node of the first year in the MP model. This is when farm conditions have changed but management has not yet reacted. Therefore, key production management is bound to be as per normal (based on the Initial MP run).

AfoLogic.CorePyomo.f_con_asset(model)

Tallies the total asset value to ensure that there is a minimum ROI on farm assets that are selected. The asset value multiplied by opportunity cost on capital is then passed to the objective.

AfoLogic.CorePyomo.f_con_biomass_transfer(model)

Tracks the biomass of each phase and allows the transfer of biomass to either grain, hay or fodder for grazing. Biomass penalties associated with untimely sowing and/or crop grazing are accounted for here as well.

Biomass doesn’t carry between seasons because the choice biomass use must before or at harvest time.

AfoLogic.CorePyomo.f_con_cashflow(model)

Tallies all cashflow in each period and transfers to the next period. Season periods exist so that a transfer can exist between parent and child seasons.

AfoLogic.CorePyomo.f_con_dep(model)

Tallies the depreciation of capital, which is then passed to the objective.

AfoLogic.CorePyomo.f_con_harv(model)

Links the harvest requirement for each rotation with harvesting capacity. Harvest capacity can be provided from farmer labour and machinery or contract services.

AfoLogic.CorePyomo.f_con_harv_stub_nap_cons(model)

Stubble and non-arable pasture may become available part way through a feed period, this means sheep could fill their entire energy requirement for that feed period solely with stubble and non-arable pasture. This is unreasonable because stubble can only be grazed after harvest. To solve this a constraint is implemented which forces a proportion (depending on when harvest occurs in the feed period) of the sheep energy intake to be consumed from pasture.

The constraint is built so that consumption of any stubble requires a certain amount of pasture consumed. However, if one crop is harvested before another, consuming stubble from the latter crop still requires a certain amount of pasture to be consumed. This is a slight limitation because in reality the sheep may have been consuming crop A stubble. However consuming crop A stubble doesn’t provide the ability to consume crop B stubble.

AfoLogic.CorePyomo.f_con_labour_fixed_anyone(model)

Tallies labour used for fixed activities that can be completed by anyone (casual/permanent/manager) and ensures that there is sufficient labour available to carry out the jobs.

AfoLogic.CorePyomo.f_con_labour_fixed_manager(model)

Tallies labour used for fixed activities that can only be completed by the manager and ensures that there is sufficient labour available to carry out the jobs.

AfoLogic.CorePyomo.f_con_labour_phase_anyone(model)

Tallies labour used in the crop enterprise that can be completed by anyone (casual/permanent/manager) and ensures that there is sufficient labour available to carry out the jobs.

AfoLogic.CorePyomo.f_con_labour_phase_perm(model)

Tallies labour used in the crop enterprise that can be completed by permanent or manager staff and ensures that there is sufficient labour available to carry out the jobs.

AfoLogic.CorePyomo.f_con_labour_sheep_anyone(model)

Tallies labour used in the sheep enterprise that can be completed by anyone (casual/permanent/manager) and ensures that there is sufficient labour available to carry out the jobs.

Labour required for fixed R&M (each year irrelevant of stock numbers) of infrastructure get optimised into a labour period.

Labour for variable R&M (depends on stock numbers) gets incurred in the labour period when the stock use the infrastructure. If this is limiting then it could be separated into its own parameter and optimised into a labour period like fixed R&M labour.

AfoLogic.CorePyomo.f_con_labour_sheep_manager(model)

Tallies labour used in the sheep enterprise that can be completed by manager staff and ensures that there is sufficient labour available to carry out the jobs.

AfoLogic.CorePyomo.f_con_labour_sheep_perm(model)

Tallies labour used in the sheep enterprise that can be completed by permanent or manager staff and ensures that there is sufficient labour available to carry out the jobs.

Constrains the consumption of paddock feed with supplement if the animal is not in confinement. This is to represent the fact that livestock will still eat pasture if they’re being fed supplement.

Note: this constraint can make the model infeasible for n11 because the optimal fs cant be met without supplement. To fix this run n33. This may still be infeasible due to sires who are always n33. But sires nut can be changed in sinp or sires can be removed from ME and vol in stockpyomo.

Constrains the consumption of understory and saltbush based on the estimated diet selection of animals grazing salt land pasture. Saltbush info comes from saltbushpyomo and understory comes from pasturepyomo.

AfoLogic.CorePyomo.f_con_makehay(model)

Constrains the hay making requirement for each rotation by hay making capacity. Hay making capacity is provided by contract services.

AfoLogic.CorePyomo.f_con_me(model)

All livestock activities require energy. Provided by pastures, stubbles, supplementary feeding, grazing of green crops and other fodder crops.

The livestock require energy and the quantity required per head depends on the target liveweight profile of the animals (are the animals gaining or losing condition), the proportion of ewes in the flock (because ewes generally require more energy than dry animals) and the level of productivity (particularly the level of reproduction).

The combination of a minimum energy requirement and maximum intake capacity sets a minimum diet quality for the animal that must be provided by the diet selected. This ensures that the diet selected is feasible for the animal to consume i.e. the diet selected cannot consist purely of a large quantity of low quality (cheap) fodder that is beyond the capacity of the animal to consume.

AfoLogic.CorePyomo.f_con_minroe(model)

Tallies the total expenditure to ensure that there is a minimum ROI on cash expenditure.

AfoLogic.CorePyomo.f_con_phasesow(model)

Ensures that the seeding requirements for each rotation phase selected can be met by the capacity of the machinery and/or with the help of contract work.

Links seeding requirement with machinery sowing activity (which accounts for machinery use and labour needed to sow pasture). No p5 set in the constraint because model can optimise sowing time (can only optimise within the periods provided eg dry sowing activity only provides sowing capacity before the break).

p_sow_prov links k and p5 so that:

  1. dry sown landuses are sown before the break.

  2. pasture is sown in the correct p5 periods based on the inputted reseeding date.

  3. wet sown crops are sown after the break of season.

v_seeding_machdays is bound in con_seed_period_days to ensure that the correct days of seeding are provided in each p7 and p5 period.

Notes:

  1. The requirement for seeding is based on v_phase_change_increase rather than v_phase_area

  2. a phase can only be sown in the phase_period for which the phase_increment is selected. If there is insufficient seeding capacity then the selection of v_phase_change_increase must be made in a later phase_period.

Note: this is an equals to constraint to stop the model sowing without a landuse so it can get poc and crop

grazing (both of those activities are provided by seeding).

AfoLogic.CorePyomo.f_con_poc_available(model)

Constrains the foo consumed on crop paddocks before seeding. The foo available is determined by the number of hectares sown and the number of days each hectare can be grazed (calculated in Mach.py) multiplied by the foo available to be consumed on each hectare each day (calculated in Pasture.py).

AfoLogic.CorePyomo.f_con_product_transfer(model)

Links rotation product (grain or baled biomass), feed requirement for supplementary feeding and the sale and purchase of grain/hay. Grain fed to the sheep is purchased unless it is produced on farm. The net grain is either purchased or sold depending on the final balance.

This constraint exists (as well as biomass transfer) because supplement requirement needs to be tracked and fulfilled by either purchasing or harvesting grain.

Currently, the supplement fed during the year is either purchased or transferred from the cropping enterprise at the end of the season. This means we are underestimating variable and fixed storage costs in good seasons because less supplement is fed but in reality the same amount would be stored since a farmer doesn’t know it is going to be a good year when making storage decisions.

AfoLogic.CorePyomo.f_con_totalcap_between(model)

Tallies total capital and transfers to the next period. Cashflow periods exist so that a transfer can exist between parent and child seasons.

Cashflow at the end of the previous yr becomes the starting balance for working capital (cashflow broadcasts to both c0 slices). This only happens between years in the sequence. End to start doesn’t carry over. This is because it was decided that the working capital constraint is more useful if there is no starting balance at the start of the sequence. This means an expensive strategy with a high reward can be bounded using wc (if the end cashflow became the start then a high expense high income strategy would not trigger the constraint).

AfoLogic.CorePyomo.f_con_totalcap_within(model)

Tallies total capital and transfers to the next period. Cashflow periods exist so that a transfer can exist between parent and child seasons.

Total capital is a combination of assets and working capital. Working capital is the sum of the expenses minus any income, since the previous ‘main’ income (e.g. harvest or shearing). This constraint exists so the model user can examine how the farm is structured under different levels of finance. The default is to allow a large amount of finance so that this constraint it not impacting the solution.

Start asset value is included to ensure that retaining sheep from the end of last year until the start of the current cashflow period doesn’t reduce wc (on farm this does not work because there is no concept of start and end of cashflow like there is in the model). If start asset value was not included the model shift strategical off spring sales that would usually occur at shearing until the start of next season which would reduce wc because sales after main shearing offset wc costs (this would be a problem in both SE and DSP).

Note: trade value is not included because it is a valid tactic to sell more animals in a poor season and then buy them back after peak debt or in SQ start the following year understocked.

AfoLogic.CorePyomo.f_con_vol(model)

All livestock activities provide volume to consume feed. The volume required by each feed changes depending on quality and quality of availability.

The combination of a minimum energy requirement and maximum intake capacity sets a minimum diet quality for the animal that must be provided by the diet selected. This ensures that the diet selected is feasible for the animal to consume i.e. the diet selected cannot consist purely of a large quantity of low quality (cheap) fodder that is beyond the capacity of the animal to consume.

AfoLogic.CorePyomo.f_objective(model)

The objective of the model is to maximise expected utility. In the case of risk neutrality, expected utility is equivalent to expected profit, meaning that the optimal farm management plan is that which maximises farm profit. In the case of risk aversion, utility increases at a diminishing rate as profit increases. Thus, when farm profit is low, an extra dollar of profit provides more utility than when farm profit is high. This means a risk averse farmer aims to reduce profit variation (i.e. increase profit in poor years at the cost of reduced profit in the good years). For example, if the crop and stock enterprise on the modelled farm are similar but grain prices are more volatile, then risk aversion will shift resources towards the stock enterprise to reduce risk (profit variation).

The expected return used to calculate utility includes the net cash flow for a given price and weather scenario, minus a cost to represent a minimum return on operating costs incurred (MINROE), minus the cost of depreciation, and minus the opportunity cost on the farm assets (total value of all assets times the discount rate, to ensure that the assets generate a minimum ROA). MINROE and asset opportunity costs are discussed in more detail in the finance section, and their inclusion is controlled by the user.

Constant absolute risk-aversion (CARA) and constant relative risk-aversion (CRRA) are two well known utility functions. Both have been previously used in stochastic farm modelling (see [Kin94, Kin96]) and both methods are included in AFO. A third expo-power utility function is also included in AFO. This provides a flexible middle ground between CARA and CRRA, overcoming many of their limitations and allowing risk aversion to change with the scale of returns. The expo-power function we are using is based on [HL02] variant of the original function introduced by [Sah93]. All three utility functions are nonlinear. To accommodate this in AFO, a piecewise-linear approximation is used, with 13 linear segments applied over the relevant range of profit values.

As an alternative to expected-utility maximisation, AFO also allows a mean–CVaR (Conditional Value at Risk) objective. In this case, the model chooses a farm plan by balancing expected profit against downside (tail) risk in low-profit states.

CARA is a negative exponential curve:

\(U = 1-exp(-a*x)\)

where \(U\) is utility, \(a\) is the Pratt–Arrow coefficient of absolute risk aversion, and \(x\) is the return to management and capital. The coefficient \(a\) is a user input that controls the level of risk aversion. Earlier farm modelling work by [Kin94] used values of 0.000003 and 0.000005 to represent moderate and high levels of risk aversion respectively, based on profit levels typical at the time.

To ensure comparability under contemporary farm conditions—where average profit levels are substantially higher—we calibrate CARA parameters based on relative risk aversion evaluated at a representative modern profit level. We target coefficients of relative risk aversion consistent with Kingwell’s classifications:

  • Moderate risk aversion: 0.78

  • High risk aversion: 1.30

Given the relationship \(R = a x\), the corresponding CARA parameter is computed as: \(a = R / x\). where \(x\) is the representative annual farm profit. This ensures updated parameters express the same behavioural strength of risk aversion as earlier work, but scaled to current economic settings.

CRRA is a power utility function:

\(U = W^(1-R) / (1-R)\)

where \(U\) is utility, \(W\) is terminal wealth, and \(R\) is the relative risk aversion coefficient. The CRRA method affects risk aversion through wealth, meaning that MINROE and asset opportunity costs influence the implied level of risk aversion. [Kin96] used values of \(R\) between 0.1 and 3.0 to represent low to high risk aversion. A key limitation is that CRRA cannot be applied when the terminal wealth level becomes negative.

Expo-power Utility:

Math:

U(x) = (1 - exp(-α x^{1-r})) / α

where:

  • \(x\) is the profit in each weather–price state.

  • \(r\) is a shape parameter controlling how risk aversion changes with the size of profit. For 0 < r < 1, the expo-power utility function exhibits decreasing absolute risk aversion (DARA) and increasing relative risk aversion (IRRA).

  • \(a\) is a scale parameter determining the overall level of risk aversion. It is calibrated so that the coefficient of relative risk aversion at the reference profit level matches the levels used in the CARA formulation.

The curvature parameter \(r\) for the expo-power utility is based on empirical estimates from farmer risk-preference studies. Results using agricultural lotteries yield \(r = 0.641\) ([LS24]). Comparable estimates can be derived from earlier studies using equivalent expo-power formulations, including \(r = 0.635\) from [SST94] and \(r = 0.643\) from [BJR14]. These values cluster tightly around 0.64, and broader evidence from European farming systems suggests a plausible range between 0.6 and 0.8 ([R+22]). Accordingly, we adopt \(r = 0.64\) as a representative baseline value. This value reflects moderate curvature and means relative risk aversion increases with income.

The parameter \(a\) is not taken directly from the literature but is calibrated so that the resulting coefficient of relative risk aversion at the reference profit level (AUD 500,000) matches the same “moderate” and “high” risk aversion levels used for the CARA specification (0.78 and 1.30), ensuring comparability across utility forms.

In this formulation, AFO treats \(x\) (profit) as the “thing at risk,” rather than full terminal wealth. This simplifies both interpretation and implementation, and aligns with the existing CARA-based specification. Wealth can be incorporated in future developments by replacing \(x\) with \(W = W_0 + x\).

Mean–CVaR Tail-Risk Method:

Let \(W\) denote terminal profit in each weather–price state, and define loss as \(L = -W\). For a confidence level \(\alpha \in (0,1)\) (for example, \(\alpha = 0.95\)) and a risk-aversion coefficient \(\lambda > 0\), AFO uses the following CVaR definition:

\[\text{CVaR}_\alpha(L) = \eta + \frac{1}{1-\alpha}\,\mathbb{E}\big[(L - \eta)^+\big],\]

where \(\eta\) is an endogenous VaR-like threshold and \((L - \eta)^+\) denotes the positive part of \(L - \eta\). The objective in this mode is

\[\max \ \mathbb{E}[W] \;-\; \lambda \,\text{CVaR}_\alpha(L),\]

so that higher values of \(\lambda\) place more weight on limiting extreme losses in bad years. This mean–CVaR specification does not rely on a utility transformation of profit. However, the same terminal profit measure is used as in the CARA/CRRA/expo-power cases, so results remain comparable across risk specifications.

Calibration and scaling of terminal wealth points

The piecewise-linear approximation to the utility function is constructed over a grid of terminal profit values given by tw_points. These breakpoints should be chosen to cover the range of profit outcomes implied by the price– weather states in the model, with some buffer for unusually good or bad years. Changing the minimum or maximum values of tw_points does not alter the underlying risk-aversion parameters (e.g. CARA \(a\), CRRA \(R\), expo-power \(r\) and \(a\)), but it does affect how accurately the utility curve is approximated in the extreme tails.

For CRRA and expo-power, AFO applies utility to total wealth,

\[W = W_0 + x,\]

where \(x\) is terminal profit and \(W_0\) is an initial wealth level specified by the user (i_initial_wealth). This shift ensures that wealth remains positive even when profit is negative. However, it also means that the effective scale at which risk aversion is evaluated depends on both profit and initial wealth.

In the CARA and expo-power specifications, risk-aversion coefficients are calibrated at a representative profit level \(x_\text{ref}\). For example, the expo-power coefficient \(a\) is chosen so that the coefficient of relative risk aversion at \(x_\text{ref} = 650{,}000\) matches Kingwell’s “moderate” or “high” risk-aversion benchmarks. When an initial wealth \(W_0\) is introduced, the relevant reference argument becomes

\[W_\text{ref} = W_0 + x_\text{ref}.\]

If \(W_0\) or \(x_\text{ref}\) is changed substantially, the original calibration of \(a\) will no longer imply the same level of relative risk aversion at the new reference wealth. In that case, \(a\) should be recomputed using the expo-power relative risk aversion expression

\[R(W) = r + a (1 - r)\, W^{\,1-r},\]

by solving for \(a\) at the desired target \(R(W_\text{ref})\). An analogous consideration applies to the CARA coefficient \(a\), where the calibration \(a = R / x_\text{ref}\) assumes a particular reference profit scale.

For CRRA, the parameter \(R\) is itself the coefficient of relative risk aversion and does not depend on wealth. Introducing \(W_0\) therefore does not change \(R\) in the Arrow–Pratt sense, although it does change the absolute sensitivity to a given dollar change in profit relative to total wealth. Users who alter tw_points, initial wealth or the representative profit level should be aware of these scale effects and, if necessary, recalibrate the CARA and expo-power coefficients to preserve their intended behavioural interpretation.