python设施开放式SCIP优化

2024-10-03 11:22:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用SCIP optimization来找到设施的最佳开放顺序 给定距离住宅区的距离,并对数量进行加权 该地区的居民人数

我已经建立了距离字典,以便从设施到 每个住宅区应产生设施的[2, 1, 0]订单输出

但是,我收到的输出是[0, 1, 2]

另外,如果我把alpha改为正值,它没有效果

import pandas as pd
from pyscipopt import Model, quicksum, multidict, exp
num_fac_to_open = 3
order_to_open = []
opened_fac = []
closed_fac = [0, 1, 2]
# Facility id
S = [0, 1, 2]
# Residential block id
R = [10, 11, 12]
distance_dict = {(0, 10): 0.8, (1, 10): 150.6, (2, 10): 100007.8, (0, 11): 1.0, (1, 11): 2012.1, (2, 11): 10009.2, (0, 12): 3.2, (1, 12): 1798.3, (2, 12): 10006.3}
population_dict = {10:54, 11:46, 12:22}
alpha = -1
# n is the desired number of facilities to open
n = len(opened_fac) + num_fac_to_open
# create a model
model = Model()
z, y= {}, {}
for s in S:
    # x_i is binary, 1 if service facility i is opened, 0 otherwise
    z[s] = model.addVar(vtype="B")
    for r in R:
        # y_i,j is binary, 1 if service facility i is assigned to residential area j, 0 otherwise
        y[s, r] = model.addVar(vtype="B")

for r in R:
    model.addCons(quicksum(y[s, r] for s in S) == 1)
#
for s in S:
    for r in R:
        model.addCons(y[s, r]-z[s] <= 0)
#
model.addCons(quicksum(z[s] for s in S) == n)
#
for facility in opened_fac:
    model.addCons(z[facility] == 1)

x, w = {}, {}
for r in R:
    x[r] = model.addVar(vtype="C", name="x(%s)"%(r))
    w[r] = model.addVar(vtype="C", name="w(%s)"%(r))
for r in R:
    x[r] = quicksum(distance_dict[s, r]*y[s, r] for s in S)
    exp_power = alpha*population_dict[r]*x[r]
    model.addCons((w[r] - exp(exp_power)) >= 0)
#
#print(quicksum(w[r] for r in R))
model.setObjective(quicksum(w[r] for r in R), 'minimize')
model.optimize()

new_facilities = []
for s in S:
    if ((model.getVal(z[s]) == 1) and (not s in opened_fac)):
        new_facilities.append(s)
        if len(new_facilities) == num_fac_to_open:
            break
print(new_facilities)

我正在尝试优化以下问题:

目的是{}

其中W_r = exp(population_dict[r]*sum_{s∈S} d_r,s * y_r,s){}

在这个问题上有任何帮助都会很好


Tags: toinformodelifisopendict
1条回答
网友
1楼 · 发布于 2024-10-03 11:22:59

您在此处发布的程序仅查找要打开的设施以及如何将这些设施分配给住宅区。代码中的任何内容都不会导致S的元素改变它们的顺序,这就是为什么当您最终循环S时,您会以其原始顺序获得设施

首先,您需要从数学上定义设施的订购方式。实施将取决于这一定义。一个简单的选择是获得一些反映设施重要性的分数,并根据这些分数对设施进行排序

相关问题 更多 >