python实现奶牛饲料配方

奶牛日粮配方设计

参考:https://blog.csdn.net/qq_20448859/article/details/72330362

需要看明白国家的奶牛饲养标准(2004版本),里面提供各种数据。就是价格难找。

"""
@author: yanglv
202011108:59:16
日粮配方:
妊娠7个月,体重500kg,日平均产乳量20kg,乳脂率3.5%的奶牛的营养需要量
"""

from pulp import *
import xlrd


def get_Ingredients():
    # 得到原料列表
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    Ingredients = Ingredients_sheet.col_values(0)
    return Ingredients


def get_costs():
    # 价格
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    Cost1 = Ingredients_sheet.col_values(1)  # 待写入的,瞎写了一个进去!!!
    ingredients = get_Ingredients()
    Cost = {}
    for i in range(0, len(ingredients)):
        Cost[ingredients[i]] = Cost1[i]
    return Cost


def get_DM():
    # 得到干物质字典
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    DM1 = Ingredients_sheet.col_values(2)
    DM2 = []  # 干物质列表
    ingredients = get_Ingredients()
    for iterm in DM1:
        iterm1 = iterm / 100
        DM2.append(iterm1)
    DM = {}
    for i in range(0, len(DM2)):
        DM[ingredients[i]] = DM2[i]
    return DM


def get_Nel():
    # 得到残奶净能字典,MJ/kg
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    Ne1 = Ingredients_sheet.col_values(11)
    ingredients = get_Ingredients()
    Nel = {}
    for i in range(0, len(ingredients)):
        Nel[ingredients[i]] = Ne1[i]
    return Nel


def get_CP():
    # 可消化粗蛋白:g
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    CP1 = Ingredients_sheet.col_values(8)
    ingredients = get_Ingredients()
    CP = {}
    for i in range(0, len(ingredients)):
        CP[ingredients[i]] = CP1[i]
    return CP


def get_UIP():
    # 可小肠消化除蛋白质,单位g
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    UIP1 = Ingredients_sheet.col_values(15)
    ingredients = get_Ingredients()
    UIP = {}
    for i in range(0, len(ingredients)):
        UIP[ingredients[i]] = UIP1[i]
    return UIP


def get_ca():
    # 得到钙含量,单位
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    Ca1 = Ingredients_sheet.col_values(4)
    Ca2 = []
    ingredients = get_Ingredients()
    for iterm in Ca1:
        iterm1 = iterm / 100.0
        Ca2.append(iterm1)
    Ca = {}
    for i in range(0, len(ingredients)):
        Ca[ingredients[i]] = Ca2[i]
    return Ca


def get_P():
    # 得到钙含量
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    P1 = Ingredients_sheet.col_values(5)
    P2 = []
    ingredients = get_Ingredients()
    for iterm in P1:
        iterm1 = iterm / 100.0
        P2.append(iterm1)
    P = {}
    for i in range(0, len(ingredients)):
        P[ingredients[i]] = P2[i]
    return P

def get_Hu():
    # 得到胡萝卜素字典,单位mg
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    Hu1 = Ingredients_sheet.col_values(22)
    ingredients = get_Ingredients()
    Hu = {}
    for i in range(0, len(ingredients)):
        Hu[ingredients[i]] = Hu1[i]
    return  Hu

def get_Va():
    # 得到VA素字典,单位mg/Kg
    feeddata = xlrd.open_workbook('粗饲料到R表.xlsx')
    Ingredients_sheet = feeddata.sheet_by_index(0)
    Va1 = Ingredients_sheet.col_values(23)
    ingredients = get_Ingredients()
    VA = {}
    for i in range(0, len(ingredients)):
        VA[ingredients[i]] = Va1[i]
    return VA

if __name__ == "__main__":
    ingredients = get_Ingredients()
    dm = get_DM()
    nel = get_Nel()
    cp = get_CP()
    # uip = get_UIP()  # 饲料中缺乏该数据
    ca = get_ca()
    p = get_P()
    costs = get_costs()
    hu = get_Hu()
    va = get_Va()  # 饲料原料中缺乏该数据

    prob = LpProblem("The feed Problem", LpMinimize)
    ingredient_vars = LpVariable.dicts("原料",ingredients, lowBound=0)  # 构建Lp变量字典,变量名以Ingr开头,如Ingr_CHICKEN,下界是0


    prob += lpSum([costs[i] * ingredient_vars[i] for i in ingredients])  # 目标方程:求最低价格
    # 添加约束条件
    prob += lpSum([ingredient_vars[i] for i in ingredients]) == 43  # 设置饲料原料用量之和为40kg
    prob += lpSum([dm[i] * ingredient_vars[i] for i in ingredients]) >= 15.82  # 设置日粮干物质的约束条件为大于15.82
    prob += lpSum([dm[i] * ingredient_vars[i] for i in ingredients]) <= 25
    prob += lpSum([dm[i] * nel[i] * ingredient_vars[i] for i in ingredients]) >= 103.33  # 设置产奶净能为103.33Mj,dm*Nel
    prob += lpSum([cp[i] * ingredient_vars[i] for i in ingredients]) >= 1461  # 设置cp为至少1461g
    # prob += lpSum([uip[i] * ingredient_vars[i] for i in ingredients]) >= 1257  # 小肠设置最小为1257g,缺乏!
    prob += lpSum([ca[i] * ingredient_vars[i] for i in ingredients]) >= 0.124  # 设置ca最小为124g
    prob += lpSum([ca[i] * ingredient_vars[i] for i in ingredients]) <= 0.15
    prob += lpSum([p[i] * ingredient_vars[i] for i in ingredients]) >= 0.083  # 设置p最小83g
    prob += lpSum([dm[i] * hu[i] * ingredient_vars[i] for i in ingredients]) >= 119.4  # 胡萝卜素,单位mg
    # prob += lpSum([dm[i] * va[i] * ingredient_vars[i] for i in ingredients]) >= 15.75  # 维生素A,单位mg 缺乏!
    prob.solve()  # 求解
    print("Status:", LpStatus[prob.status]) # 查看解的状态
    for v in prob.variables(): # 查看解
        print(v.name, "=", v.varValue)
匿名

发表评论

匿名网友