Python 纸浆(线性规划)公司如何选择最佳解决方案?
我在Python中使用纸浆,并使用LpMaximize最大化决策变量。假设我有几个组合,每个组合都有特定的点。本例中的决策变量是“点”,因为点是我试图最大化的。我本练习的目标是找到具有最高点的组合。我的代码:Python 纸浆(线性规划)公司如何选择最佳解决方案?,python,linear-programming,pulp,Python,Linear Programming,Pulp,我在Python中使用纸浆,并使用LpMaximize最大化决策变量。假设我有几个组合,每个组合都有特定的点。本例中的决策变量是“点”,因为点是我试图最大化的。我本练习的目标是找到具有最高点的组合。我的代码: prob = LpProblem("Allocation",LpMaximize) alloc_vars = LpVariable.dicts("Allocation",combinations,0,1,LpBinary) prob += lpS
prob = LpProblem("Allocation",LpMaximize)
alloc_vars = LpVariable.dicts("Allocation",combinations,0,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])
示例输入:(这是jut虚拟数据)
作为输出,我得到具有最大点数的组合。我不确定这些组合到底是如何选择的。作为一个例子,我有一个输入数据框,其中所有的组合都有0个点,但在运行纸浆解算器后仍然得到一些组合。因此,这让我想知道,纸浆如何找到哪种组合可以得到最大分数?“如何”可能不是最好的问题。更好的问题是“为什么它选择给定的解决方案”。这完全取决于你提供给它的模型。即目标和约束。如果你的问题是“纸浆如何找到最佳解决方案”,那么就我所知,答案是单纯形算法的实现,带有某种形式的分支和二进制变量的边界。如果你的问题是,一个特定的解决方案是如何从许多相同的最优解决方案中选择的(在最优解决方案是非唯一的问题中),那么我认为它是随机的。它可能是“可重复的”,但没有必要重复。如果你关心这些同样最优的解决方案,你应该修改你的目标函数。
| 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 |
...
...
...