问题描述
我正在Python中使用PuLP,并使用LpMaximize最大化决策变量。假设我有几种组合,每种组合都有某些特定点。在这种情况下,决策变量是“点”,因为点是我要最大化的点。我本次练习的预期目标是找到具有最高分的组合。我的代码:
prob = LpProblem("Allocation",LpMaximize)
alloc_vars = LpVariable.dicts("Allocation",combinations,1,LpBinary)
prob += lpSum(binary.loc[i]["points"]*alloc_vars[i] for i in combinations)
for j in vendors:
prob += lpSum([alloc_vars[i]*binary.loc[i][j] for i in combinations]) == 1
#"binary" is a matrix file/dataframe that's commonly used in PuLP solver
prob.solve()
print("Status: ",LpStatus[prob.status])
var_output = []
for i in alloc_vars:
for j in vendors:
var_output += [{
'Vendor': j,'Combination': i,'Allocation': alloc_vars[i].varValue*binary.loc[i][j],}]
[i for i in var_output if i['Allocation']==1]
value(prob.objective)
selected_combinations = set([i['Combination'] for i in var_output if i['Allocation']==1])
样本输入:(这是突出的虚拟数据)
| Combination | Vendor 1 | Vendor 2 | Vendor 3 | Vendor 4 | Points | A | B | C | D |
| Comb1 | A | | | | 73 | 1 | 0 | 0 | 0 |
| Comb2 | B | | | | 54 | 0 | 1 | 0 | 0 |
| Comb3 | C | | | | 47 | 0 | 0 | 1 | 0 |
| Comb4 | D | | | | 89 | 0 | 0 | 0 | 1 |
| Comb5 | A | B | | | 73 | 1 | 1 | 0 | 0 |
| Comb6 | B | A | | | -43 | 1 | 1 | 0 | 0 |
| Comb7 | D | C | A | | 111 | 1 | 0 | 1 | 1 |
...
...
...
作为输出,我得到了最高分的组合。我不确定这些组合的选择情况如何。作为一个极端的例子,我有一个输入数据框,所有组合都具有0点,但是在运行PuLP求解器后仍然得到了一些组合。因此,这让我想知道,PuLP如何找到哪种组合可以提供最大的积分?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)