我是几个小时前刚开始使用Pulp的。我正在解决MCLP问题,但是我不知道如何实现Ni公式:请参见下面的图片。我的想法是,如果覆盖了一个需求节点,那么设施也应覆盖另一个小于100m的需求节点。

python - 在 PuLP 中实现MCLP-LMLPHP

最佳答案

有几种方法可以做到这一点。与您的公式最接近的方式是使用python的“列表理解”功能。参见下文,其应输出:

Status: Optimal
Population Served is =  100.0
x =  [1. 0.]


一个简单的例子,带有伪数据:

import numpy as np
import pandas as pd
from pulp import *

# Some dummy data, let's have 3 demand nodes and 2 possible sites
I = [0,1,2]
J = [0,1]
S = 100
d = [[50, 150], [80, 110], [160, 10]]

a = [80, 20, 30]
P = 1

# Compute the sets Ni
# NB: this will be a list in which each item is a list of nodes
# within the threshold distance of the i'th node
N = [[j for j in J if d[i][j] < S] for i in I]

# Formulate optimisation

prob = LpProblem("MCLP", LpMaximize)
x = LpVariable.dicts("x", J, 0)
y = LpVariable.dicts("y", I, 0)

# Objective
prob += lpSum([a[i]*y[i] for i in I])

# Constraints
for i in I:
    prob += lpSum([x[j] for j in N[i]]) >= y[i]

prob += lpSum([x[j] for j in J]) == P

# Solve problem
prob.solve()

x_soln = np.array([x[j].varValue for j in J])

# And print some output
print (("Status:"), LpStatus[prob.status])
print ("Population Served is = ", value(prob.objective))
print ("x = ", x_soln)

关于python - 在 PuLP 中实现MCLP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51501074/

10-12 21:50