Advertisement
Guest User

Fixed JuMP Code

a guest
Sep 29th, 2020
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###################Load Julia packages##########################################
  2. using JuMP #package containing modeling features for optimization
  3. using DataFrames #package facilitating input and output using dataframes
  4. using CSV
  5. using Cbc #load Cbc solver
  6. using LinearAlgebra
  7. using Statistics
  8.  
  9. ###################Input data and initialize parameters##########################
  10. forecastAndPrice=DataFrame!(CSV.File("RedTomatoForecastAndPrice_OffPeakDisc.csv"))
  11. #forecastAndPrice=readtable("RedTomatoForecastAndPrice_OffPeakDisc.csv") #read forecast and price from file
  12. demand = forecastAndPrice[:,2] #extract demand column of forecastAndPrice data frame
  13. price  = forecastAndPrice[:,3] #extract price column of forecastAndPrice data frame
  14.  
  15. #Specify cost parameters
  16. c_materials = 10 #Materials cost per unit
  17. c_holding = 2 #Holding cost per unit/month
  18. c_stockout = 5 #Stockout cost per unit/month
  19. c_hiring = 300 #Hiring cost per worker
  20. c_layoff = 500 #Layoff cost per worker
  21. c_regular = 4 #Regular time wage per hour
  22. c_overtime = 6 #Over time wage per hour
  23. c_subcontract = 30 #Subcontracting per unit
  24.  
  25. #Specify other planning parameters
  26. labReq = 4 #Labor hours per unit
  27. hrsPerDay = 8 #hours per day
  28. daysPerMonth = 20 #Days per month
  29. maxOTPerMonth= 10 #Maximum over time per month
  30. startingWorkForce= 80 #Maximum over time per month
  31. endingWorkForce= 0 #ending workforce level
  32. startingBackorder= 0 #starting backorder
  33. endingBackorder= 0 #ending backorder
  34. startInv= 1000 #starting inventory
  35. endInv= 500 #ending inventory
  36.  
  37. ########################Formulate optimization model####################################
  38. nPeriods=size(demand,1) #obtain number of planning periods from forecast
  39. #m = Model() #create Julia optimization model object
  40. m = Model(with_optimizer(Cbc.Optimizer, logLevel=3))
  41.  
  42. #Create decision variables
  43. @variable(m, W[0:nPeriods] >= 0,Int) #number of employees
  44. @variable(m, H[1:nPeriods] >= 0, Int) #number of employees hired
  45. @variable(m, L[1:nPeriods] >= 0, Int) #number of employess laid off
  46. @variable(m, P[1:nPeriods] >= 0, Int) #number of units produced
  47. @variable(m, Inv[0:nPeriods] >= 0) #number of units in inventory
  48. @variable(m, S[0:nPeriods] >= 0) #number of units backordered
  49. @variable(m, C[1:nPeriods] >= 0, Int) #number of units subcontracted
  50. @variable(m, O[1:nPeriods] >= 0) #number of overtime hours
  51.  
  52. #Create objective function components
  53. @expression(m, regTimeCost[t=1:nPeriods],  c_regular*hrsPerDay*daysPerMonth*W[t]) #regular time cost
  54. @expression(m, OTCost[t=1:nPeriods],  c_overtime*O[t]) #over time cost
  55. @expression(m, hiringCost[t=1:nPeriods],  c_hiring*H[t]) #hiring cost
  56. @expression(m, layoffCost[t=1:nPeriods],  c_layoff*L[t]) #layoff cost
  57. @expression(m, prodCost[t=1:nPeriods],  c_materials*P[t]) #production cost
  58. @expression(m, invCost[t=1:nPeriods],  c_holding*Inv[t]) #inventory holding cost
  59. @expression(m, stockOutCost[t=1:nPeriods],  c_stockout*S[t]) #stockout cost
  60. @expression(m, subContCost[t=1:nPeriods], c_subcontract*C[t]) #subcontracting cost
  61.  
  62. #Add objective function components together and form "minimization" objective function
  63. @objective(m, Min, sum(regTimeCost[t] + OTCost[t] + hiringCost[t]
  64. + layoffCost[t]  + prodCost[t] + invCost[t] + stockOutCost[t] + subContCost[t] for t=1:nPeriods))
  65.  
  66. #Create constraints
  67. @constraints(m, begin
  68.     const_initWorkforce, W[0]==startingWorkForce #set W[0] to initial workforce
  69.     const_startInv, Inv[0] == startInv #set I[0] to initial inventory
  70.     const_startBackorder,S[0]==startingBackorder #set S[0] to initial backorder
  71.    
  72.     const_workforceFlow[t=1:nPeriods], W[t] == W[t-1] + H[t] - L[t] #balance equations for workforce level
  73.     const_invBal[t=1:nPeriods], Inv[t] == Inv[t-1] + P[t] + C[t] + S[t] - demand[t] - S[t-1] #balance equations for inventory
  74.    
  75.     const_prodCap[t=1:nPeriods], labReq*P[t] <= hrsPerDay*daysPerMonth*W[t] + O[t] #production time constraint
  76.     const_overTime[t=1:nPeriods], O[t] <= maxOTPerMonth*W[t] #maximum overtime constraint
  77.    
  78.     const_endWorkforce, W[nPeriods] >= endingWorkForce #minimum ending workforce constraint
  79.     const_endInv, Inv[nPeriods] >= endInv #minimum ending inventory constraint
  80.     const_endBackorders, S[nPeriods] == endingBackorder #ending backorder constraint    
  81. end)
  82.  
  83. println("****Formulation****\n",m,"\n") #print optimization model
  84.  
  85. ########################Solve model and output solution####################################
  86. #status = solve(m) #solve optimization model
  87. status = optimize!(m) #solve model
  88.  
  89. println("****Solve status and results****")
  90. println("Optimization status: ", status,"\n") #print solver status
  91. # println("Workforce: ", getvalue(W))
  92.  
  93. @show W
  94. @show typeof(W)
  95. @show getvalue.(W)
  96. @show typeof(getvalue.(W))
  97. W_=getindex.((getvalue.(W),), 1:nPeriods)
  98. @show W_
  99. @show typeof(W_)
  100. H_=getvalue.(H)
  101. L_=getvalue.(L)
  102. P_=getvalue.(P)
  103. C_=getvalue.(C)
  104. S_=getindex.((getvalue.(S),), 1:nPeriods)
  105. @show S_
  106. @show typeof(S_)
  107. Inv_=getindex.((getvalue.(Inv),), 1:nPeriods)
  108. @show Inv_
  109. @show typeof(Inv_)
  110.  
  111. #Create data frame with decision variables
  112. decisionDF=DataFrame(W = W_, H = H_, L = L_, P = P_, C = C_, S = S_, Inv = Inv_)
  113.  
  114. #Create data frame with costs per period
  115. costPerPeriodDF = DataFrame(regTimeCost=getvalue.(regTimeCost),OTCost=getvalue.(OTCost),
  116. hiringCost=getvalue.(hiringCost),layoffCost=getvalue.(layoffCost),invCost=getvalue.(invCost),
  117. stockOutCost=getvalue.(stockOutCost),prodCost=getvalue.(prodCost),subContCost=getvalue.(subContCost))
  118.  
  119. #Create data frame containing profit information
  120. Revenue=dot(price,demand) #sum_{t = 1 to nperiods} price(t)*demand(t)
  121. Cost=getobjectivevalue(m)
  122. Profit=Revenue-Cost
  123. profitDF=DataFrame(Revenue=Revenue,Cost=Cost,Profit=Profit)
  124.  
  125. #Process measures
  126. Flowrate=mean(demand) #average demand satisfied by the aggregate plan
  127. Inventory= 0.5*(getvalue.(Inv)[0] + getvalue.(Inv)[nPeriods]) + sum(getindex.((getvalue.(Inv),), 1:(nPeriods-1)))/nPeriods #average inventory
  128. Flowtime=Inventory/Flowrate #flow time via little's law
  129. processDF = DataFrame(Flowrate=Flowrate,Inventory=Inventory,Flowtime=Flowtime) #create data frame with process flow measures
  130.  
  131. println("****Optimal solution****\n",decisionDF,"\n") #print data frame containing decision variables created above
  132. println(profitDF,"\n") #print profit
  133. println("****Cost per period****\n",costPerPeriodDF,"\n") #print cost data frame created above
  134. println("****Process flow measures****\n",processDF) #print data frame containing process flow measures
  135.  
  136. CSV.write("DecVar.csv", decisionDF) #write decision variable values to a csv file
  137. CSV.write("profitSummary.csv", profitDF) #write profit information from data frame to a csv file
  138. CSV.write("CostPerPeriod.csv", costPerPeriodDF) #write cost data frame to a csv file
  139. CSV.write("ProcessFlowMeas.csv", processDF) #write process flow measures to a csv file
  140.  
  141.  
Advertisement
RAW Paste Data Copied
Advertisement