使用 PuLP 的整数线性规划问题

问题描述

我正在尝试使用 PuLP 解决此 ILP。 问题说明我们需要最小化建造仓库的成本。我们需要根据最低成本来决定建造哪个仓库。这是LP的样子

min ∑transportcost * X_i,∑fixedcost * Y_j

[其中 i 和 j 是仓库# = 1/2/3]

约束应该是: X_i & Y_j 应该属于 0 或 1,因为它们是决策变量 X_i == Y_j

这是我写的代码

import pulp
from pulp import *
import pandas as pd
import numpy as np

n_warehouse = 3

warehouse_fixed_costs = [100,120,80]
cost_per_km = [3,3,3]
distances = [20,30,45]
transport_costs = [cost_per_km[i]*distances[i] for i in range(len(cost_per_km))]
warehouses = ['1','2','3']

model = LpProblem("Supply-Demand",LpMinimize)

transport_warehouse_dv = LpVariable.matrix("X",warehouses,cat = "Binary",lowBound = 0)
transport_warehouse_allocation = np.array(transport_warehouse_dv)

fixed_cost_warehouse_dv = LpVariable.matrix("Y",lowBound = 0)
fixed_cost_warehouse_allocation = np.array(fixed_cost_warehouse_dv)

obj_func = lpSum(transport_costs*transport_warehouse_allocation)
obj_func += lpSum(warehouse_fixed_costs*fixed_cost_warehouse_allocation)
model += obj_func

const = lpSum(transport_warehouse_allocation) == fixed_cost_warehouse_allocation,"Warehouse decision"
model += const

model

这是创建的模型:

供需:

最小化 60X_1 + 90X_2 + 135X_3 + 100Y_1 + 120Y_2 + 80Y_3 + 0

服从仓库决策:X_1 + X_2 + X_3 - Y_1 - Y_2 - Y_3 = 0

变量 0

当我解决模型时,我得到了最佳解决方案,但没有一个决策变量保持值为 1。总成本也为 0。理想情况下,答案应该是 160,因为仓库 1 的成本最低。

我不确定我错过了什么。我之前没有使用过 PuLP,所以我无法弄清楚原因。

解决方法

正如 Erwin 指出的,最便宜的选择是不建造任何仓库。

您需要添加一个约束来强制构建一个仓库

"""
selects the cheapest warehouse to build

programmer: Michael R. Gibbs
"""

from pulp import LpProblem,LpMinimize,LpVariable,lpSum,PULP_CBC_CMD,value
import pandas as pd
import numpy as np

# we are building only one warehouse
warehouses_to_build = 1

# define the costs for the warehouse choices
warehouse_fixed_costs = [100,120,80]
cost_per_km = [3,3,3]
distances = [20,30,45]
transport_costs = [cost * dist for cost,dist in zip(cost_per_km,distances)]
warehouses = ['1','2','3']

model = LpProblem("Supply-Demand",LpMinimize)

# to build or not to build flag for each warehouse
build_warehouse_flag = LpVariable.matrix("build",warehouses,cat = "Binary",lowBound = 0)

# no warehouses is the cheepest choice
# this forces warehouses to be built
model += lpSum(build_warehouse_flag) == warehouses_to_build

# cost to be minimized
obj = lpSum([trans_cost * flag for trans_cost,flag in zip(transport_costs,build_warehouse_flag)])
obj += lpSum([fix_cost * flag for fix_cost,flag in zip(warehouse_fixed_costs,build_warehouse_flag)])
model += obj

print(model)

solver = PULP_CBC_CMD()
model.solve(solver)

print("------------------ build results -----------------------")
print([(flag,value(flag)) for flag in build_warehouse_flag])