简体   繁体   English

如何解决这个线性规划问题?

[英]How to solve this linear programming problem?

Consider a set of city districts.考虑一组城市区域。 There is a candidate for building a medical emergency service station in every city district.每个城区都有一个医疗急救服务站的建设候选。 The cost of building an ambulance station is given by the parameter and the total budget for building stations throughout the city is 50 million.建设救护站的费用由参数给定,全市建设救护站的总预算为5000万。 Furthermore, if a station is built in city district 2, it must not be station built in city district 6. Determine in which city districts to build ambulance stations so that the maximum travel time is minimized.此外,如果车站建在城市 2 区,则不得在城市 6 区建站。确定在哪些城市区建造救护站,以使最大行驶时间最小化。 What is the optimal maximum travel time?最佳的最长旅行时间是多少? The cost of building stations in the city district ∈, are: [32,20,25,30,40,29] The city districts and the driving distances between them are interpreted in the following graph:在市区 ∈ 建造车站的成本是: [32,20,25,30,40,29] 市区和它们之间的行驶距离如下图解释:

 ([[ 0,  4, 12, 27, 25, 58],
   [ 4,  0, 24, 16, 29, 38],
   [12, 24,  0, 31, 14, 30],
   [27, 16, 31,  0, 21,  8],
   [25, 29, 14, 21,  0, 11],
   [58, 38, 30,  8, 11,  0]])

The right answer is to build stations in districts 2 and 4, and optimal travel time is 24.正确答案是在 2 区和 4 区建站,最佳旅行时间为 24。

I tried to solve it, and here is my code, but the answer is not right.我试图解决它,这是我的代码,但答案不正确。 It has to be completet with PULP它必须与 PULP 一起完成

`from pulp import *
import numpy as np

# the cost of building of the stations
cost1 = [32,20,25,30,40,29]

# the city districts and the driving distances between them
d = np.array([[0,4,12,27,25,58],[4,0,24,16,29,38],[12,24,0,31,14,30],[27,16,31,0,21,8],          [25,29,14,21,0,11],[58,38,30,8,11,0]])

# set of city districts
cities1 = [1, 2, 3, 4, 5, 6]
cities2 = [1, 2, 3, 4, 5, 6]

# costs
n = dict(zip(cities1,cost1))

# dict of every distance between the cities
a1 = dict(zip(cities1, zip(*d)))

a = {key: dict(enumerate(value, 1)) for key, value in a1.items()}


# model
model = LpProblem("Set_covering", LpMinimize)

# shortcut introduction
shortcut = [(i,j) for i in cities1 for j in cities2]

# declaration of variables
x = LpVariable.dicts("x",(cities1),0, cat = 'Binary')
maximum = LpVariable("Maximum", 0, cat = 'Continuous')

# purpose function
model += maximum

# restrictive conditions
for (i,j) in shortcut:
    model += maximum >= x[i]*a[i][j]
for j in cities1:
    model += lpSum(x[i]*a[i][j] for i in cities2) >= 1

model += lpSum(n[i]*x[i] for i in cities1) <=50
model += lpSum(x[2]+x[6]) <= 1


# List of results
model.solve()
print("Status:", LpStatus[model.status])

for v in model.variables():
    if v.varValue > 0:
        print(v.name, "=", v.varValue)
    
print("The total travel time to all places from both stations is: ", value(model.objective))`

You have created a slightly different model from the problem described.您创建的 model 与所描述的问题略有不同 You are getting the maximum of the travel times from amongst the locations that could service another location.您正在从可以服务于另一个位置的位置中获得最大的旅行时间。 When you select (correctly) to build at locations 2 & 4, your "maximization" constraint is picking up the 38 because that is the maximum of all of the stations that could service location 6. But you say, "I wouldn't service station 6 from station 2, I'd do it from station 4 and take the 8 distance..." And similarly for others.当您 select(正确地)在位置 2 和 4 进行构建时,您的“最大化”约束会选择 38,因为这是可以为位置 6 提供服务的所有站点中的最大值。但是您说,“我不会提供服务从第 2 站到第 6 站,我会从第 4 站开始,然后走第 8 个距离……”其他人也一样。

But that information is not in your model. I'd suggest a modest reformulation... you are almost there and you have many of the concepts covered.但该信息不在您的 model 中。我建议适度重新表述......您几乎已经完成,并且涵盖了许多概念。 The info you want to capture is that "I want to service location j from station i ."您要捕获的信息是“我想从站点i到位置j提供服务”。 So introduce that variable and constrain it properly.所以引入那个变量并适当地约束它。 Something like:就像是:

service[i, j] ∈ {0, 1}

and some necessary constraints on that and then use your max constraint across that variable.以及对此的一些必要约束,然后在该变量上使用最大约束。

Clearly, you'll need to link the service variable to having a station built at i (a constraint between the two, for each i ).显然,您需要将service变量链接到在i处建造的站点(两者之间的约束,对于每个i )。 You could either make individual constraints for each i, j tied to x[i] for that or use a big-M summation if you have learned that concept already.您可以为每个与x[i]相关联的i, j设置单独的约束,或者如果您已经了解了该概念,则可以使用 big-M 求和。

And you will need to ensure that every j is serviced (sounds like a summation constraint for each j ) which will replace your other constraint forcing the distance to be greater than 1.并且您将需要确保为每个j提供服务(听起来像是每个j的求和约束),这将取代您的其他约束,迫使距离大于 1。

Comment back if you are stuck.如果您遇到困难,请回复。 Good luck w/ the assignment!祝你好运!

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

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