RUFAS.routines.animal.ration.ration_driver module#
- class RUFAS.routines.animal.ration.ration_driver.RationManager#
Bases:
object
Ration formulation is performed via collection and comparison of feed supply and animal requirements
Calls to AnimalRequirements and AvailableFeeds (in this file until Feed refactor) collect the necessary information, which is then sent to RationOptimization Finally, RationReporter is a suite of methods used to send information to output manager (and other submodules of RuFaS)
- classmethod formulate_ration(pen: Pen, available_feeds: AvailableFeedsTypedDict, animal_grouping_scenario: AnimalGroupingScenario, sim_day: int) Tuple[Dict[str, float], Dict[str, float]] #
Function that links the ration_driver file with the calc_ration function in animal_manager.py. Returns a dictionary of the rations by feed and status of the NLP optimization.
Parameters#
- penPen
The pen a ration is being formulated for.
- available_feedsAvailableFeedsTypedDict
An object of class AvailableFeeds.
- animal_grouping_scenarioAnimalGroupingScenario enum
A grouping scenario of animals used in the current simulation, specified in AnimalGroupingScenario enum and AnimalManager class.
- sim_dayint
Current simulation day of the simulation.
Returns#
- Dict[str, float]
Formulated ration.
- Dict[str, float]
Summary of ration content.
- classmethod handle_failed_constraints(num_attempts: int, solution: OptimizeResult, ration_optimizer: RationOptimizer, ration_config: RationConfig, pen: Pen, available_feeds: AvailableFeedsTypedDict, sim_day: int, info_map: Dict[str, Any]) None #
Handle and log failed constraints during the ration optimization process.
This method identifies and logs the constraints that failed during the optimization process for a specific pen of animals. It gathers relevant information about the failed attempt, including the simulation day, the number of attempts, the failed constraints, the attempted ration, and the pen’s nutrient requirements. This information is then added to the output manager via a variable.
Parameters:#
- num_attemptsint
The number of ration formulation attempts made so far.
- solutionscipy.optimize.OptimizeResult
The result of the optimization process.
- ration_optimizerRationOptimizer
A RationOptimizer object.
- ration_configRationConfig
A RationConfig object.
- penPen
The pen of animals for which the failed constraints are being handled.
- available_feedsAvailableFeedsTypedDict
A dictionary of available feeds for ration formulation.
- sim_dayint
Day of simulation.
- info_mapDict[str, Any]
A dictionary containing additional information to be logged with the failed constraints summary.
Returns:#
None
- static calc_milk_average(pen: Pen) float #
Calculates average milk produced by a cow in a pen.
Parameters#
pen: an object of class Pen
Returns#
- float
Average running milk
- classmethod reduce_milk_production(pen: Pen, reduction: float) None #
Reduces milk production for all animals in a pen. Only does so if post-reduction production would be above 1.0 KG.
Parameters#
pen: an object of class Pen
- reduction: float
The kg amount of lactation should be reduced in each loop, per animal
- classmethod make_ration_from_solution(available_feeds: AvailableFeedsTypedDict, solution: OptimizeResult) Dict[str, float | str] #
Generates ration dictionary from scipy result
Parameters#
- available_feedsDict[str, Dict[str, List[str] | List[int] | List[float]]]
A DefaultDict of the AvailableFeeds class attributes defined in ration_driver.py
- solutionOptimizeResult
Object from scipy package.
Returns#
- Dict[str, float]
Dictionary of formulated ration, with keys as feed IDs, values as kg fed per animal.
- classmethod make_solution_from_fixed_ration(ration: Dict[str, float | str]) List[float] #
makes solution object from returned fixed ration for use in get_ration_vals function in ration_optimizer.py Simply puts the value in triplicate, and multiplies by the MEact defined in ration_config
Parameters#
- rationDict[str, float]
Dictionary of formulated ration, with keys as feed IDs, values as kg fed per animal.
Returns#
- List[float]
List of kg fed per animal, in triplicate, to match scipy.OptimizeResult object.
- classmethod get_user_defined_ration(req: AnimalRequirements, pen: Pen, available_feeds: AvailableFeedsTypedDict, animal_grouping_scenario: AnimalGroupingScenario, sim_day: int) tuple[Dict[str, float | str], Dict[str, float]] #
Function that links the ration_driver file with the calc_ration function in pen.py. Returns a dictionary of the rations by feed and status of the NLP optimization.
There are multiple outcomes here. 1: User has defined a lactation reduction percent of 0.0, and tolerance of 0.0, thus the fixed ration is used. 2: Optimization is a success, and optimized solution (within the tolerance bounds) is used. 3: Optimization fails
3a: If lactation reduction is set to 0.0, no reattempt is made 3b: Optimization attempted until lactation reduction threshold is reached.
(e.g. milk_reduction_maximum)
Parameters#
- reqRequirements
Requirements for the given pen.
- penPen
The pen a ration is being formulated for.
- available_feedsAvailableFeeds
Available feeds object to get information from.
- animal_grouping_scenarioAnimalGroupingScenario
Grouping scenario chosen for current simulation.
- sim_day: int
Current simulation day of the simulation.
Returns#
- rationDict[str, float]
Dictionary of formulated ration, with keys as feed IDs, values as kg fed.
ration_vals : Dict[str, float]
- class RUFAS.routines.animal.ration.ration_driver.RationReporter#
Bases:
object
Reports information about a formulated ration.
- classmethod report_ration(ration: Dict[str, float | str], available_feeds: Dict[str, Dict[str, float]]) Tuple[Dict[str, float], Dict[str, float]] #
Calculates information in the ration about nutrient information including nutrient amounts and concentrations. Returns a dictionary of nutrient amounts and nutrient calculations respectively. Psuedocode for these calculations are located in Ration Class Variables in Animal Module Pseduocode.
Parameters#
- rationDict
Dictionary of formulated ration, with keys as feed IDs, values as kg fed.
- available_feedsDict
Available feeds dictionary from the Feed class object.
Returns#
- Tuple[Dict, Dict]
- Tuple of two dictionaries: one of nutrient amounts, the other of nutrient concentration (as a percentage
of fed dry matter).
- classmethod report_ration_supply(ration: Dict[str, float], available_feeds: AvailableFeedsTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) Dict[str, float] #
Nutrient and energy supply of a formulated ration Different from the report_ration method, since this takes into account digestibility and other factors
Parameters#
- rationDict[str, float]
Dictionary of formulated ration, with keys as feed IDs, values as kg fed.
- available_feedsDict
available feeds dictionary from the Feed class object.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- Dict[str, float]
Dictionary of nutrients and energy supplied by a formulated ration.
- static get_TDN_discount(ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
- Crucial step to take into account Total digestible nutrient (TDN)
digesitbility (% of DM).
Initial step in net energy (NE) and metabolizable energy (ME) calculations.
Parameters#
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
- Total digestible nutrient (% of DM) discount, which is TDN digestibility decrease that is a result of
TDN amount relative to TDN concentration.
Notes#
See the following pseudocode for all equations [A.Cow.E.3]-[A.Heifer.E.3]
- static get_DE(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns actual digestible energy of feed item, Mcal. Calculation is performed on a per feed item basis
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
actual digestible energy of feed item, Mcal.
- static get_ME(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns metabolizable energy of feed item.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
metabolizable energy of feed i, Mcal.
Notes#
- Division of DE_act by kg_fed is to get the DE for each item on a per kg basis,
as is used in the subsequent calculations.
- static get_NE_maintenance_and_activity(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns net energy of feed item available for maintenance requirements.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Net energy of feed i, Mcal.
Notes#
- Division of ME_item by kg_fed is to get the ME for each item on a per kg basis,
as is used in the subsequent calculations.
- static get_NE_lactation(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns net energy of feed item available for lactation requirements.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Net energy of feed i, Mcal.
Notes#
- Division of DE_act and ME_item by kg_fed is to get the DE and ME for each item on a per kg basis,
as is used in the subsequent calculations.
- static get_NE_growth(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns net energy of feed item available for growth requirements.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Net energy of feed i, Mcal.
Notes#
- Division of ME_item by kg_fed is to get the metabolizable energy for each item on a per kg basis,
as is used in the subsequent calculations.
- static get_calcium(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns calcium supply of feed item.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Calcium supply of feed item, g.
- static get_phosphorus(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns phosphorus supply of feed item.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Phosphorus supply, g.
- static get_fat(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns fat supply of feed item.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Fat supply of feed item, g.
- static get_fat_percentage(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns fat content percentage of feed item.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Dry matter fat percentage of feed item.
- static get_forage_NDF(kg_fed: float, feed_item_info: FeedInfoTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns forage neutral detergent fiber content of feed item, g.
Parameters#
- kg_fedfloat
Kilograms of feed item in ration.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, Dict[str, float]]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Forage neutral detergent fiber content of feed item, g.
- static get_metabolizable_protein(ration: Dict[str, float], available_feeds: AvailableFeedsTypedDict, ration_report: Dict[str, Dict[str, float]], body_weight: float) float #
Returns metabolizable protein supply of feed item, g.
Parameters#
- rationDict[str, float]
Dictionary of formulated ration, with keys as feed IDs, values as kg fed.
- available_feedsDict
available feeds dictionary from the Feed class object.
- feed_item_infoDict
Dictionary of nutrient and energy information of feed item.
- ration_reportDict[str, float]
Dictionary of nutrient amount and concentrations.
- body_weightfloat
Animal body weight in kg.
Returns#
- float
Metabolizable protein supply of feed item, g.
- class RUFAS.routines.animal.ration.ration_driver.AvailableFeeds#
Bases:
object
Stores the information of the feeds available at the end of a ration interval to be used in the non-linear program ration formulation.
- __init__() None #
- feed_nutrients(feed: Feed) None #
Class function that manipulates the available feeds nutrient information into list (valid for input in the non-linear program) and populates the corresponding class variables.
- Args:
feed: an instance of the Feed class object
- get_feed_data_from_feed_ids(feed_ids: Set[int])#
Returns a subset of data from all the available feeds based on the given set of feed ids.
Parameters#
- feed_idsSet[int]
A set of feed ids.
Returns#
- Dict
A dictionary that contains a subset of data from all the available feeds based on the given set of feed ids.
Raises#
- KeyError
If the feed id is not found in the feed id list.