分布式网络线性规划约束

时间:2015-12-04 01:20:46

标签: python linear-programming pulp

我尝试使用Pulp框架在Python中编写一个线性程序 - 捐赠诊所提供的血液被送到配送中心,然后送到医院使用。困难在于创建约束来决定是否使用配送中心并保留从进入DC的流程到退出的流程?

# Import PuLP modeler functions
from pulp import *

# Creates a list of all the supply nodes
Clinics = ["San Francisco",
      "Los Angeles",
      "Phoenix",
      "Denver"]
# Creates a list of all demand nodes
Hospitals = ["San Diego",
      "Miami",
      "Tucson",
      "Dallas"]

#Creates list of all transshipment nodes
Dist = ["NYC",
    "Boston"]

# Creates a dictionary for the number of units of supply at each clinic c
supplyData = {#Clinic     Supply
      "San Francisco":17,
      "Los Angeles"  :20,
      "Phoenix"      :17,
      "Denver"       :20,
      }


# Creates a dictionary for the number of units of demand at each hospital h
demand = { #Hospital    Demand
      "San Diego":17,
      "Miami"  :10,
      "Tucson"   :15,
      "Dallas"   :12
      }

# Creates a dictionary the fixed cost of running each dist centre
dcCost = { #Dist       Min Flow Fixed Cost
      "NYC":      [0,700],
      "Boston":   [0,700],
      }

# Creates a list of costs for each transportation path
costsCDC = [  #Dist
     #NY BO
     [5, 3], #SF
     [4, 7], #LA    Clinics
     [6, 5], #PH
     [9, 8]  #DE
     ]

costsDCH =  [  #Hospitals
     #SD MI TU DA
     [5, 3, 2, 6], #NY
     [4, 7, 8, 10] #BO    Dist
     ]

# Creates a list of tuples containing all the possible routes
Routes = [(c,dc) for c in Clinics for dc in Dist]
Routes2 = [(dc,h) for dc in Dist for h in Hospitals]

# Splits the dictionaries
(minCap,fixedCost) = splitDict(dcCost)

# The cost data is made into a dictionary
costs = makeDict([Clinics,Dist],costsCDC,0)
costs2 = makeDict([Dist, Hospitals], costsDCH,0)
# Creates the problem variables of the Flow on the Arcs
flow = LpVariable.dicts("Route",(Clinics,Dist),0,None,LpInteger)
flow2 = LpVariable.dicts("Route2",(Dist,Hospitals),0,None,LpInteger)

# Creates the master problem variables of whether to build the DC or not
build = LpVariable.dicts("BuildDC",Dist,0,1,LpInteger)

# Creates the 'prob' variable to contain the problem data
prob = LpProblem("Test Problem",LpMinimize)

# The objective function is added to prob - The sum of the transportation costs (c -> dc; dc -> h) and the DC fixed costs
prob += lpSum([flow[c][dc]*costs[c][dc] for (c,dc) in Routes])+lpSum([fixedCost[dc]*build[dc] for dc in Dist]) +lpSum([flow2[dc][h]*costs2[dc][h] for (dc,h) in Routes2]),"Total Costs"

# The Supply maximum constraints are added for each supply node (clinics)
for c in Clinics:
    prob += lpSum([flow[c][dc] for dc in Dist])>=supplyData[c], "Sum of units out of Clinic %s"%c

# The Demand minimum constraints are added for each demand node (hospital)
for h in Hospitals:
    prob += lpSum([flow2[dc][h] for dc in Dist])>=demand[h], "Sum of units into Hospitals %s"%h

1 个答案:

答案 0 :(得分:1)

这通常通过以下约束来处理:如果dc关闭,则所有流入和流出的流量都应为零。例如。 flow [i,dc]< = IsOpen [dc] * MaxFlow [i,dc]其中IsOpen是二进制变量,MaxFlow是常量。 (类似于flow [dc,j])。