python - 在纸浆中实施 MCLP
问题描述
几个小时前,我刚开始使用 Pulp。我正在解决一个 MCLP 问题,但我不知道如何实现 Ni 公式:见下图。我的想法是,如果一个需求节点被覆盖,那么另一个小于 100m 的需求节点也应该被设施覆盖。
解决方案
有几种方法可以做到这一点。与您的公式最匹配的方法是使用 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)
推荐阅读
- r - 运行 devtools::release() 以准备在 R 中发布包时 git 检查错误
- python - Python数据框:如何返回列中出现的次数?
- nginx - 在nginx上部署gatsby刷新时重定向地址错误并带出端口号?
- amazon-web-services - AWS ENI(弹性网络接口)和NAT网关在路由表配置中的区别
- git - git 分支上的 Dependabot
- java - (Java) 为什么超类可以有子类类型的变量?
- python-3.x - 如何开始突出显示单词
- spring-boot - 无法实例化 [org.springframework.cloud.stream.binding.BindingService]:工厂方法 'bindingService' 抛出异常
- javascript - 如何让我的页脚从顶部覆盖 100% 的页面高度和宽度?
- python - python中的.append非常慢,有什么办法可以改善吗?