Python基于excel输入定义变量名和条件

Python基于excel输入定义变量名和条件,python,linear-programming,pyomo,pulp,Python,Linear Programming,Pyomo,Pulp,我正在用python处理LP问题 分配选择: Type Qty_Available Cost For_Protein For_Carb Protein_Can 50 10 TRUE FALSE Soup 15 8 FALSE TRUE EnergyDrink 10 15 TRUE

我正在用python处理LP问题

分配选择:

Type          Qty_Available   Cost  For_Protein    For_Carb
Protein_Can         50         10      TRUE          FALSE
Soup                15          8      FALSE         TRUE
EnergyDrink         10         15      TRUE          TRUE
            
数据帧

StudentID   Age ProteinDef  CarbDef
A01         16      TRUE     TRUE
A02         17      FALSE    TRUE
A03         16      FALSE    FALSE
A04         18      TRUE     TRUE
A05         16      FALSE   TRUE
B01         18      FALSE   FALSE
B02         18      TRUE    TRUE
B03         20      FALSE   TRUE
B04         19      FALSE   FALSE
B05         19      TRUE    FALSE
我的代码运行良好。我面临的问题是,分配选择每月都会根据可用性进行更改,并在excel中提供。但是,我的代码是硬编码的(如下所示)

Protein_Can=纸浆.LpVariable.dicts(“Protein_Can”,df.StudentID,纸浆.LpBinary)
Soup=pulp.LpVariable.dictsSoupdf.StudentID,pulp.LpBinary)
EnergyDrink=plup.LpVariable.dicts(“EnergyDrink”,df.StudentID,plup.LpBinary)
成本[id]=蛋白质罐头[id]*蛋白质罐头成本+汤[id]*汤成本+能量饮料[id]*能量饮料成本
对于df.StudentID中的id:
如果df.loc[id]['ProteinDef']==TRUE:
prob+=汤[id]是

你需要更好地利用集合。如果你有一篇关于线性规划的文章,或者浏览一些教程,你应该能够找到很多例子。您不应该像现在这样直接将数据硬编码到公式中。在建立数学模型之前,可以从文件中读取数据,也可以单独开发数据,但不能在约束公式中这样做

上面的例子有两个主集合,每个主集合有两个子集合。(以下为伪代码)

主要设备 食物(或“分配选择”): 您可以使用这些字符串名称作为集合成员,也可以只使用索引并在索引列表中跟踪名称,而不考虑问题

学生: 子集 上述每一组都有两个亚组,分别与
蛋白质
碳水化合物
相关。例如,对于食品:

Foods_P = {Protein_can, energy_drink}  
Foods_C = ...
当您读入数据时,这些应该很容易设置,使用它们将使您的程序更干净、更快

使用集合。。。 完成后,只需2个变量即可处理食物:

avail[food]
cost[food]
其中
food
Foods

您的模型似乎是一个分配模型,在该模型中,您将为特定学生分配一定数量的特定食物,因此您应该有一些双索引的决策变量:

x[f, s]  # an integer/real number representing the assignment of x quantity of food f to student s
对于约束和成本的类似构造应该到位

从Excel中读取。 你有几个选择

  • 使用熊猫阅读信息,这可能是过分的,没有必要
  • 使用直接的Excel界面,如
    openpyxl
    ,这同样是过分的
  • 设置Excel文件后,将每个选项卡另存为一个单独的.csv文件(数据标准),然后让python读取csv,这很简单。(在Excel中使用“另存为”功能并选择.csv…忽略比尔·盖茨的警告)
  • 你应该制作两个CSV,一个用于食物,一个用于学生。然后可以这样理解:

    # script to import csv files
    
    file_name = 'food.csv'
    
    qty_dict = {}
    cost_dict = {}
    foods = set()
    food_p = set()
    food_c = set()
    
    with open(file_name, 'r') as fin:
        fin.readline()  # read the header and discard it
        # process the rest
        for line in fin:
            data = line.strip().split(',')
            key = data[0]           # the type name
            qty = int(data[1])      # int conversion of qty
            cost = int(data[2])     # int conversion of cost
            qty_dict[key] = qty
            cost_dict[key] = cost
    
            # test the booleans, construct the sets
            if data[3].lower() == 'true':
                food_p.add(key)
            if data[4].lower() == 'true':
                food_c.add(key)
    
    print (cost_dict)
    print (food_p)
    
    
    # do same for students...
    
    # make pulp model
    
    # you probably have a decision variable X[f,s] which is probably something like:
    
    X = pulp.LpVariable.dicts("assign", [(f, s) for f in foods for s in students], cat='Binary')
    
    我没有安装
    纸浆
    ,因此最后一行是准确的,但没有经过测试

    您应该能够使用读入期间创建的词典和集合设置约束和目标

    Excel中的食品快照:


    啊。好啊我以为你在想如何设置模型部分。第二个问题是如何从类似Excel的文件中提取。我会补充我的答案…太好了。。。。我刚刚发现自己在快速编辑阅读部分。我忘了在里面放一个循环来读取整个文件。固定的。
    avail[food]
    cost[food]
    
    x[f, s]  # an integer/real number representing the assignment of x quantity of food f to student s
    
    # script to import csv files
    
    file_name = 'food.csv'
    
    qty_dict = {}
    cost_dict = {}
    foods = set()
    food_p = set()
    food_c = set()
    
    with open(file_name, 'r') as fin:
        fin.readline()  # read the header and discard it
        # process the rest
        for line in fin:
            data = line.strip().split(',')
            key = data[0]           # the type name
            qty = int(data[1])      # int conversion of qty
            cost = int(data[2])     # int conversion of cost
            qty_dict[key] = qty
            cost_dict[key] = cost
    
            # test the booleans, construct the sets
            if data[3].lower() == 'true':
                food_p.add(key)
            if data[4].lower() == 'true':
                food_c.add(key)
    
    print (cost_dict)
    print (food_p)
    
    
    # do same for students...
    
    # make pulp model
    
    # you probably have a decision variable X[f,s] which is probably something like:
    
    X = pulp.LpVariable.dicts("assign", [(f, s) for f in foods for s in students], cat='Binary')