python - Python:不知道如何获取 FLP 的每个仓库的总成本
问题描述
我正在处理设施位置问题。我在 Python 中创建的模型有效,但我还想获得每个仓库的总成本和每个仓库的客户总数。但是我不知道我怎么能得到这个。有人能帮我吗?我的模型类似于这个问题:https ://scipbook.readthedocs.io/en/latest/flp.html#weak-and-strong-formulations
在代码中,我从 excel 中检索数据,而不是将其放入 python 中。但是,如果您要放入 python 中,则数据在#-符号之后。由于我必须处理大文件,因此从 excel 中检索数据更方便。这是我的代码:
import pandas as pd
from mip import Model, xsum, minimize, BINARY
Customer = list(range(1,6))
Warehouse = [1,2,3]
#Demand
#d = {1:80, 2:270, 3:250, 4:160, 5:180} --> D below is formatted the same as in here, only below it is extracted from excel
df = pd.read_excel('Demandd.xlsx')
D = df.set_index('Customer').to_dict()['Demand']
#Operating costs
#O = {1: 1000, 2:1000, 3:1000}
#Capacity
#C = {1:500, 2:500, 3:500}
#Here we have the same as described previously, below we have explained what indicates the operation costs and what indicates the capacity in WH.
#Two dimensions where dimension 1 is the capacity and dimension 2 is the Operation costs
# For capacity do: WH[j][0]
# For Operation costs do: WH[j][1]
df3 = pd.read_excel('Warehousedata.xlsx')
WH = df3.set_index('WH').T.to_dict(orient='list')
#Costmatrix
#c = {(1,1):4, (1,2):6, (1,3):9,
#(2,1):5, (2,2):4, (2,3):7,
#(3,1):6, (3,2):3, (3,3):4,
#(4,1):8, (4,2):5, (4,3):3,
#(5,1):10, (5,2):8, (5,3):4,
#}
#Also this costs matrix is retieved from excel below and can be indicated by DC[i][j]
df2 = pd.read_excel('/users/basroelofs/downloads/Costmatrix.xlsx') #import deliverycosts per customer per warehouse from excel
DC = df2.set_index('Customer').T.to_dict()
# We create an empty model to fill with variables and constraints
model = Model()
# We create empty variables x and y
x, y = {}, {}
# Filling the Decision variables (where j = warehouse j and i = customer i)
# y[j] is a binary value that says whether warehouse j is open or not
# x[i,j] is the value that says how much warehouse j serves to customer i.
# x[i,j] gives the possibility that one customers is served by multiple warehouses
for j in Warehouse:
y[j] = model.add_var(var_type = BINARY, name="y(%s)"%j)
for i in Customer:
x[i,j] = model.add_var(var_type="C", name="x(%s,%s)"%(i,j))
# Now we create the constraints
# constraint 1: Requires that each customer’s demand must be satisfied
for i in Customer:
model.add_constr(xsum(x[i,j] for j in Warehouse) == D[i], "Demand(%s)"%i)
#Constraint 2: Capacity constraint --> capacity of WH[j] may not be exceeded
#CHECK this!!!!!!!!! IS WH good in LINE 45?
for j in Warehouse:
model.add_constr(xsum(x[i,j] for i in Customer) <= WH[j][0]*y[j], "Capacity(%s)"%i)
#Constraint 3: Provide variable upperbound for variable x
for (i,j) in x:
model.add_constr(x[i,j] <= D[i]*y[j], "Strong(%s,%s)"%(i,j))
# Objective function: Minimize the total costs
# Total costs = Sum(Delivery costs of customer i(for 1 visit) to warehouse j)
# + Sum(Operation costs of warehouse j * y[j])
model.objective = minimize(
xsum(WH[j][1]*y[j] for j in Warehouse) +
xsum(DC[i][j] * x[i,j] for i in Customer for j in Warehouse))
#Optimize model
model.optimize()
EPS = 0.0001
#HERE I PRINT THE OUTCOMES I WANT, HOWEVER I ALSO NEED THE COSTS PER FACILTIY AND TOTAL CUSTOMERS PER FACILITY.
edges = [(i,j) for (i,j) in x if x[i,j].x > EPS]
facilities = [j for j in Warehouse if y[j].x > EPS]
print ("Optimal value=", (model.objective_value))
print ("Facilities at nodes:", facilities)
print ("Edges:", edges)
我尝试了以下方法:
#For the # of customers, but this only give the number of edges:
customers=0
for j in Warehouse:
for i in Customer:
if x[i,j].x > EPS:
customers = customers + 1
#For the total costs per facility/warehouse, but this does not give an answer:
som = 0
for j in Warehouse:
som = som + (DC[i][j] for i in Customer)
print(som)
我使用 MIP 包。所以我需要的是:
- 获取每个仓库的总成本以获得最佳值的代码。
- 每个仓库的客户总数的代码。
在此先感谢人们!
解决方案
推荐阅读
- python - 非 utf8 列准备问题:在转换过程中发现未知类别 ['Fès-Meknès']
- mysql - 为什么依赖父表的普通实体不能命名为弱实体?
- machine-learning - Flux.jl:理解鸢尾花数据集下载源代码。为什么使用 SHA256 哈希?
- nunit - 在 NUnit 中调整测试名称
- python - 在 Python 中从 3D 数组创建 4D 数组
- android - 如何杀死由 NavHostFragment 创建的所有片段?
- java - 如何确定是否在十六进制字符串中设置了一个位?
- python - 使用python将pandas数据框插入actian PSQL数据库表
- xml - 使用 T-SQL 读取 XML
- python - 解析 JSON 对象时,保留字作为数据类中的属性名称