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

Issue with Integer Linear Programming using 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", warehouses, cat = "Binary", 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

这是创建的模型:

Supply-Demand:

MINIMIZE 60X_1 + 90X_2 + 135X_3 + 100Y_1 + 120Y_2 + 80Y_3 + 0

SUBJECT TO Warehouse_decision: X_1 + X_2 + X_3 - Y_1 - Y_2 - Y_3 = 0

VARIABLES 0 <= X_1 <= 1 Integer 0 <= X_2 <= 1 Integer 0 <= X_3 <= 1 Integer 0 <= Y_1 <= 1 Integer 0 <= Y_2 <= 1 Integer 0 <= Y_3 <= 1 Integer

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

我不确定我错过了什么。我以前没有用过 PuLP,所以我无法找出原因。

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

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

"""
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])