Uncategorized

pandas – Optimization of Cattle Feed Composition using Python with multiple conditions


I have a DataFrame df1 representing different ingredients (Maize, Jowar, Bajara Rice_Broken Wheat) for cattle feed, each with associated cost and nutritional parameters (ME_Kcal_per_Kg, C_Protein, C_Fat and Chloride) & Weight represents the weight assigned in case to maintain the ratio of different ingredients.

      Item    Cost   Weight   ME_Kcal_per_Kg  C_Protein  C_Fat   Chloride  Ingredients_constraints_Min  Ingredients_constraints_Max
      Maize    60       1            3.30        9.0      4.0      0.04                           10                           60
      Jowar    42     0.7            3.00       10.0      3.0      0.10                            0                           30
     Bajara    20       1            2.64       12.7      4.9      0.00                            0                           30
Rice_Broken     9       1            2.60        7.9      1.7      0.00                           20                           40
      Wheat    24       1            3.10       14.0      2.6      0.05                            0                           20

Another DataFrame df2 defines nutrient limits for the cattle feed.

Nutrients_Limits  ME_Kcal_per_Kg  C_Protein   C_Fat   Chloride
             Min            2.78        26.5       5       0.3
             Max            2.90        28.0       7       0.5

The goal is to find the combination of ingredients from df1 that minimizes cost while meeting the nutritional constraints defined in df2.

The conditions are:

1. The quantity of each ingredient should not exceed the values specified in the Ingredients_constraints_Max column in df1.

2. The quantity of each ingredient should not fall below the values specified in the Ingredients_constraints_Min column in df1.

3. The combination of different ingredients should not exceed 100 in total.

4. The nutritional parameters (Weight, ME_Kcal_per_Kg, C_Protein, C_Fat, Chloride) should meet the limits specified in df2.

The Code Snipped for 1st condition is here, unable to integrate second condition.

import numpy as np
from scipy.optimize import linprog

# Extracting relevant data from the DataFrame
costs = df1['Cost'].values
constraints_min = df1['Ingredients_constraints_Min'].values
constraints_max = df1['Ingredients_constraints_Max'].values

# Coefficients for the linear programming problem
c = costs
A_eq = np.ones((1, len(costs)))
b_eq = np.array([100])

# Bounds for each ingredient quantity
bounds = [(constraints_min[i], constraints_max[i]) for i in range(len(costs))]

# Solving the linear programming problem
result = linprog(c, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method='highs')

# Extracting the solution
solution_quantities = result.x

# Displaying the results
for i, item in enumerate(df1['Item']):
    print(f"{item}: {solution_quantities[i]}")

print(f"Total Cost: {result.fun}")

How to find the combination of ingredients that minimizes cost while satisfying these nutritional constraints? Any help would be greatly appreciated.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *