纸浆求解器约束

Pulp Solver constraint

我正在使用 PULP 创建一个 LP 求解器,以最大限度地降低成本。我想添加一个上限值来跳过一些结果。

costs = dict(zip(Variety_items,tempdf['cost']))
Bright = dict(zip(Variety_items,tempdf['Bright']))
Colour = dict(zip(Variety_items,tempdf['colour']))
Thickness = dict(zip(Variety_items,tempdf['Thickness']))

prob = LpProblem("Problem", LpMinimize)

Variety_items_var = LpVariable.dicts("B",Variety_items,0)
prob += lpSum([costs[i]*Variety_items_var[i] for i in Variety_items])
prob += lpSum([Variety_items_var[i] for i in Variety_items]) == 1, "PercentagesSum"
prob += lpSum([Bright[i] * Variety_items_var[i] for i in Variety_items]) == 40, "C1"
prob += lpSum([Colour[i] * Variety_items_var[i] for i in Variety_items]) == 90, "C2"
prob += lpSum([Thickness[i] * Variety_items_var[i] for i in Variety_items]) <= 15, "C3"

prob.solve()

以下是以101.40471619为最佳成本的结果组合。 需要的帮助是:最小值应该是 0.07,我得到的结果是 0.03,我想将其添加为约束

B_XXX_1 = 0.0
B_XXX_14 = 0.0
B_XXX_16 = 0.21351179
B_XXX_2 = 0.0
B_XXX_3 = 0.14404079
B_XXX_4 = 0.0
B_XXX_5 = 0.0
B_XXX_6 = 0.0
B_DX_15 = 0.6042065
B_DX_17 = 0.0
B_DX_7 = 0.0
B_DX_8 = 0.0
B_DP_11 = 0.0
B_DP_12 = 0.0
B_DP_13 = 0.038240918
B_TX_10 = 0.0
B_TX_9 = 0.0

注意:我没有附上完整的代码。它的 Pandas DF 到 dict 转换。

假设您希望 x 为 0 或介于常量 L 和 U 之间。引入二进制变量 d 和约束条件

  d*L <= x <= d*U

(其实是两个约束).

这有时被称为半连续变量。一些求解器和建模工具直接支持将其作为变量类型,但在其他求解器和建模工具中,例如 PuLP,您需要使用二进制变量对其进行建模。