Facility.py.py.


Facility.py.py.


#!/ usr / bin / env python3.7#版权所有2021,guro狗万app足彩bi优化,llc#设施所在地:公司目前将产品从5个植物#到4个仓库运送。狗万滚球球它正在考虑关闭一些植物以减少#成本。公司应该关闭哪些植物,以便最小化#运输和固定成本?狗万滚球球##注意此示例使用列表而不是词典。由于#由于#它不适用于稀疏数据,列表是一个合理的选项。##基于前线系统的示例:#http://www.solver.com/disfacility.htm#用于许可。手机万博登录从Gurobipy导入Grb#仓库需求的Gurobipy进口Gurobipy以数千个单位需求= [15,18,14,20]#植物容量成千上万的单位容量= [20,22,17,19,18]#固定成本每个植物固定级= [12000,15000,17000,13000,16000]#运输成本每千单位转基因= [[4000,2000,3000,2500,4500],[2500,2600,3400,3000,4000],[1200,1800,2600,4100,3000],[2200,2600,3100,3700,3200]]#植物和仓库植物的范围=范围(Len(容量))仓库=范围(Len(需求))#型号m =GP.Model(“工厂”)#工厂打开决策变量:打开[P] == 1如果植物P打开。open = m.addvars(植物,vtype = grb.binary,obj = fictioncosts,name =“打开”)#运输决策变量:传输[w,p]捕获#最佳数量从工厂p传输到仓库w m.addVars(warehouses, plants, obj=transCosts, name="trans") # You could use Python looping constructs and m.addVar() to create # these decision variables instead. The following would be equivalent # to the preceding two statements... # # open = [] # for p in plants: # open.append(m.addVar(vtype=GRB.BINARY, # obj=fixedCosts[p], # name="open[%d]" % p)) # # transport = [] # for w in warehouses: # transport.append([]) # for p in plants: # transport[w].append(m.addVar(obj=transCosts[w][p], # name="trans[%d,%d]" % (w, p))) # The objective is to minimize the total fixed and variable costs m.modelSense = GRB.MINIMIZE # Production constraints # Note that the right-hand limit sets the production to zero if the plant # is closed m.addConstrs( (transport.sum('*', p) <= capacity[p]*open[p] for p in plants), "Capacity") # Using Python looping constructs, the preceding would be... # # for p in plants: # m.addConstr(sum(transport[w][p] for w in warehouses) # <= capacity[p] * open[p], "Capacity[%d]" % p) # Demand constraints m.addConstrs( (transport.sum(w) == demand[w] for w in warehouses), "Demand") # ... and the preceding would be ... # for w in warehouses: # m.addConstr(sum(transport[w][p] for p in plants) == demand[w], # "Demand[%d]" % w) # Save model m.write('facilityPY.lp') # Guess at the starting point: close the plant with the highest fixed costs; # open all others # First open all plants for p in plants: open[p].start = 1.0 # Now close the plant with the highest fixed cost print('Initial guess:') maxFixed = max(fixedCosts) for p in plants: if fixedCosts[p] == maxFixed: open[p].start = 0.0 print('Closing plant %s' % p) break print('') # Use barrier to solve root relaxation m.Params.method = 2 # Solve m.optimize() # Print solution print('\nTOTAL COSTS: %g' % m.objVal) print('SOLUTION:') for p in plants: if open[p].x > 0.99: print('Plant %s open' % p) for w in warehouses: if transport[w, p].x > 0: print(' Transport %g units to warehouse %s' % (transport[w, p].x, w)) else: print('Plant %s closed!' % p)