LpVariable 字典函数

Python 供应链分析

Aaren Stubberfield

Supply Chain Analytics Mgr.

从简单到复杂

复杂面包房示例

# 定义决策变量
A = LpVariable('A', lowBound=0, cat='Integer')
B = LpVariable('B', lowBound=0, cat='Integer')
C = LpVariable('C', lowBound=0, cat='Integer')
D = LpVariable('D', lowBound=0, cat='Integer')
E = LpVariable('E', lowBound=0, cat='Integer')
F = LpVariable('F', lowBound=0, cat='Integer')
# 定义目标函数
var_dict = {"A":A, "B":B, "C":C, "D":D, "E":E, "F":F}

# 定义目标函数
model += lpSum([profit_by_cake[type] * var_dict[type] for type in cake_types])
Python 供应链分析

使用 LpVariable.dicts()

LpVariable(name, indexs, lowBound=None, upBound=None, cat='Continuous')
  • name = 每个创建的 LP 变量名的前缀
  • indexs = 作为字典键的字符串列表
  • lowBound = 下界
  • upBound = 上界
  • cat = 变量类型
    • Integer
    • Binary
    • Continuous(默认)
Python 供应链分析

结合列表推导的 LpVariable.dicts()

  • LpVariable.dicts() 常与 Python 列表推导配合使用

运输优化

# 定义决策变量
customers = ['East','South','Midwest','West']
warehouse = ['New York','Atlanta']
transport = LpVariable.dicts("route", [(w,c) for w in warehouse for c in customers],
                              lowBound=0, cat='Integer')

# 定义目标 model += lpSum([cost[(w,c)]*transport[(w,c)] for w in warehouse for c in customers])
Python 供应链分析

小结

  • 为复杂问题创建大量 LP 变量
  • LpVariable.dicts()
  • 搭配列表推导使用
Python 供应链分析

现在轮到您动手了

Python 供应链分析

Preparing Video For Download...