manbet体育手机客户端


netflow.py


#!/usr/bin/python # Copyright 2019, 狗万app足彩Gurobi Optimization, LLC #解决多商品流问题两种产品manbetx官网手机登入(“铅笔”和“钢笔”)在两个城市(“底特律”和“丹佛”)生产,必须被发送到3个城市(“波士顿”、“纽约”和“西雅图”)的仓库以满足需求(“流入[h,i]”)。# #交通网络上的流量必须遵守弧容量约束#('容量[i,j]')。目标是最小化弧#运输成本的总和('cost[i,j]')。from gurobipy import * # Base data commodities = ['Pencils', 'Pens'] nodes = ['Detroit', 'Denver', 'Boston', 'New York', 'Seattle'] arcs, capacity = multidict({('底特律','波士顿'):100,('底特律','纽约'):80,('底特律','西雅图'):120,('丹佛','波士顿'):120,('丹佛','纽约'):120,('丹佛','纽约'):120,('丹佛','纽约'):120,('丹佛','西雅图'):120}) #三联商品来源-目的地成本={('铅笔','底特律','波士顿'):10,('铅笔','底特律','纽约'):20,('铅笔','底特律','西雅图'):60,('铅笔','丹佛','波士顿'):40,('铅笔','丹佛','纽约'):40,('铅笔','丹佛','西雅图'):30,('钢笔','底特律','波士顿'):20, ('Pens', '底特律','纽约'):20,('Pens', '底特律','波士顿'):80,('Pens', '丹佛','波士顿'):60,('Pens', '丹佛','纽约'):70,('Pens', '丹佛','西雅图'):30}#对商品城市流入的需求={('铅笔','底特律'):50,('铅笔','波士顿'):-50,('铅笔','波士顿'):50,('铅笔','波士顿'):-50年,(“铅笔”,“西雅图”):-10年,(“笔”,“底特律”):60岁(“笔”、“掘金”):40岁(“笔”、“波士顿”):-40年,(“笔”、“纽约”):-30(“笔”、“西雅图”):-30} #创建优化模型m =模型('netflow') #创建变量flow = m.a dvars (commodities, arcs, obj=cost, name="flow") # Arc-capacity constraints . addconstrucs ((flow.sum('*',i,j) <= capacity[i,j] for i,j in arcs), "cap") #使用Python循环的等效版本# for i,j in arcs: # m.addConstr(sum(flow[h,i,j] for h in commodities) <= capacity[i,j], # "cap[%s,%s]" % (i, j)) # Flow-conservation constraints m.addConstrs( (flow.sum(h,'*',j) + inflow[h,j] == flow.sum(h,j,'*') for h in commodities for j in nodes), "node") # Alternate version: # m.addConstrs( # (quicksum(flow[h,i,j] for i,j in arcs.select('*',j)) + inflow[h,j] == # quicksum(flow[h,j,k] for j,k in arcs.select(j,'*')) # for h in commodities for j in nodes), "node") # Compute optimal solution m.optimize() # Print solution if m.status == GRB.Status.OPTIMAL: solution = m.getAttr('x', flow) for h in commodities: print('\nOptimal flows for %s:' % h) for i,j in arcs: if solution[h,i,j] > 0: print('%s -> %s: %g' % (i, j, solution[h,i,j]))