facility.py


# !/usr/bin/env python3.7版权所有2023,Gurobi Opt狗万app足彩imization, LLC #设施位置:一家公司目前将其产品从5个工厂运送到4个仓库。狗万滚球球它正在考虑关闭一些工厂以降低成本。为了尽量减少运输和固定成本,公司应该关闭哪些工厂?狗万滚球球注意,这个例子使用的是列表而不是字典。因为它不能处理稀疏数据,所以列表是一个合理的选择。# #基于前线系统的一个例子:# http://www.solver.com/disfacility.htm #经许可使用手机万博登录。进口gurobipy作为gp从gurobipy进口GRB #仓库需求千单位需求=[15,18,14,20]#工厂容量千单位容量=[20,22,17,19,18]#每个工厂的固定成本固定成本=[12000,15000,17000,13000,16000]#每千单位运输成本transCosts = [4000, 2000, 3000, 2500, 4500], [2500, 2600, 3400, 3000, 4000], [1200, 1800, 2600, 4100, 3000], [2200, 2600, 3100, 3700,#工厂和仓库的范围工厂= Range (len(容量))仓库= Range (len(需求))#模型m = gp.Model(“设施”)#工厂开放决策变量:如果工厂p是开放的,则开放[p] == 1。open = m.d addvars (plants, vtype=GRB.)BINARY, obj=fixedCosts, name="open") # Transportation decision variables: transport[w,p] captures the # optimal quantity to transport to warehouse w from plant p transport = 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)