SHARE
TWEET

Untitled

a guest May 20th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Import PuLP modeler functions
  2. from pulp import *
  3.  
  4. #Arrays are shops with prices for material i
  5. #Each position in the array represents the cost of a material
  6. #across all shops,for ex: costs[i][j] represents the cost of material j in shop i
  7. #a 0 represents a material not found in the shop
  8. costs = ([1,0,6,10,4],[0,3,10,8,0],[2,4,6,11,3],[0,0,5,8,4],[1,4,6,8,3])
  9. num_shops = 5
  10. num_elems = 5
  11.  
  12.  
  13. # Create the 'prob' variable to contain the problem data
  14. prob = LpProblem("The Shop aggregator Problem",LpMinimize)
  15.  
  16. # The variables are created with a lower limit of zero and a maximal number of 1
  17. i=0
  18. j=0
  19. Vars = {}
  20. for shop in costs:
  21.     for elem in shop:
  22.         #print("var:",i,j)
  23.         var_name = "var_"+str(i)+str(j)
  24.         Vars[var_name] = LpVariable(var_name,0,1,LpInteger)
  25.         j+=1
  26.     var_name = "ship_"+str(i)
  27.     Vars[var_name] = LpVariable(var_name,0,1,LpInteger)
  28.     i+=1
  29.     j=0
  30.  
  31. #We create then the objective function:
  32. i=0
  33. j=0
  34. objective_funct = None
  35. for shop in costs:
  36.     for elem in shop:
  37.         var_name = "var_"+str(i)+str(j)
  38.         objective_funct += Vars[var_name]*costs[i][j]
  39.         j+=1
  40.     var_name = "ship_"+str(i)
  41.     objective_funct += Vars[var_name]
  42.     i+=1
  43.     j=0
  44.  
  45. #Add objective to the problem
  46. prob += objective_funct
  47.  
  48. #First restriction, only one element of each material required
  49. for j in range(num_elems):
  50.     r1=None
  51.     for i in range(num_shops):
  52.         if costs[i][j]>0:
  53.             var_name = "var_"+str(i)+str(j)
  54.             r1 += Vars[var_name]
  55.     prob += r1 >= 1
  56.  
  57. #Last restriction, a shipment for at least one element
  58. for i in range(num_shops):    
  59.     ship_name = "ship_"+str(i)
  60.     for j in range(num_elems):
  61.         r3 = None
  62.         var_name = "var_"+str(i)+str(j)
  63.         r3 += Vars[var_name]
  64.         prob += Vars[ship_name] >= r3
  65.  
  66. #We (the solver, of course) solve the problem
  67. GLPK().solve(prob)
  68. for v in prob.variables():
  69.     print(v.name, "=", v.varValue)
  70.  
  71. print("Objective =", value(prob.objective) )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top