繁体   English   中英

Integer 使用 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

这是创建的 model:

供应需求:

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

受 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

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

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

正如 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])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM