Advertisement
Guest User

Untitled

a guest
May 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. using JuMP
  2. using CPLEX
  3.  
  4.  
  5. EPSVAL = 0.0000001;
  6.  
  7.  
  8. function loka(a,b,c,f)
  9. tic()
  10.  
  11. # ----------------------------------------------
  12. # Setup the master problem with the initial data
  13. #-----------------------------------------------
  14. #m - number of suppliers ( we use i to index suppliers )
  15. #n - number of destinations ( we use j to index destinations )
  16. (m,n) = size(c)
  17.  
  18. ub=zeros(Int64, m,n)
  19. for i=1:m,j=1:n
  20. ub[i,j] = min(a[i],b[j])
  21. end
  22.  
  23. #(m,n) = size(f)
  24. # Model for the master problem, parameter ensures that CPLEX does not produce output
  25. Master = Model(solver = CplexSolver(CPX_PARAM_SIMDISPLAY=0))
  26. # Define one initial "artificial" variable
  27. @variable(Master, lambda[i=1:1] >=0)
  28. #@variable(Master, lambda[1] >=0)
  29. # Setting the objective
  30. @objective(Master, Min, 100000*lambda[1])
  31. #@objective(Master, Min, sum(lambda[i] for i=1:m))
  32. # constraints: one per destination
  33. @constraint(Master, consDestination[j=1:n], b[j]*lambda[1] == b[j])
  34. # constraints: one per supplier
  35. @constraint(Master, consSupplier[i=1:m], lambda[1] == 1)
  36.  
  37.  
  38. println(Master)
  39.  
  40. # ----------------------------------------------
  41. # Setup the pricing problems
  42. #-----------------------------------------------
  43. # we need a sub problem per supplier. Here we declare an array for the sub-problems
  44. subProblems = Array{JuMP.Model}(m)
  45. #subProblem=Model(solver = CplexSolver(CPX_PARAM_MIPDISPLAY=0)) # Model for the subproblem
  46.  
  47. # variables for sub problem
  48. x = Array{JuMP.Variable}(m,n)
  49. y = Array{JuMP.Variable}(m,n)
  50.  
  51. for i=1:m
  52. # Model for the pricing problem, parameter ensures that CPLEX does not produce output
  53. subProblems[i]=Model(solver = CplexSolver(CPX_PARAM_MIPDISPLAY=0))
  54. # Model for the subproblem
  55. # x: variables:
  56. for j=1:n
  57. x[i,j] = @variable(subProblems[i], category=:Int, lowerbound=0, basename="x[$i,$j]")
  58. y[i,j] = @variable(subProblems[i], category=:Bin, basename="y[$i,$j]")
  59. end
  60. # empty objective for now:
  61. @objective(subProblems[i], Min, 0);
  62. @constraint(subProblems[i],sum(x[i,j] for j=1:n) <= a[i])
  63. @constraint(subProblems[i], con2[j=1:n], x[i,j] - ub[i,j] *y[i,j]<= 0)
  64. end
  65.  
  66. # branching
  67. @constraint(subProblems[8], y[8,6] == 0)
  68. @constraint(subProblems[4], y[4,6] == 0)
  69. @constraint(subProblems[4], y[4,3] == 0)
  70. @constraint(subProblems[4], y[4,8] == 0)
  71. @constraint(subProblems[6], y[6,1] == 0)
  72. @constraint(subProblems[6], y[6,6] == 0)
  73.  
  74. println(y)
  75.  
  76. iteration=1 # Counter for the while loop
  77. done = false;
  78. patterns = Array{Array{Int64,1},1}()
  79. # create pattern corresponding to initial artificial column
  80. pattern = ones(Int64, n+m)
  81. push!(patterns,pattern)
  82. logFile = open("A2log.txt","w")
  83. write(logFile,"iteration,master_obj\r\n")
  84. #while (current solution of the master problem is suboptimal, i.e., subproblem objective value > 0)
  85. while !done
  86. # Solve the master problem
  87. statusControlFlow=solve(Master)
  88. println("Iteration: ",iteration,", Objective value: ", getobjectivevalue(Master))
  89. logString = string(iteration, ",", getobjectivevalue(Master),"\r\n")
  90. write(logFile,logString)
  91.  
  92. #Collect the dual variables
  93. pi = getdual(consDestination)
  94. kappa = getdual(consSupplier)
  95. println("pi: $pi")
  96. println("kappa: $kappa")
  97.  
  98. done = true
  99. # solve all sub problems:
  100. for i=1:m
  101. # Set the objective that results from the current dual variables
  102. @objective(subProblems[i], Min, sum(c[i,j]*x[i,j] for j=1:n) + sum(f[i,j]*y[i,j] for j=1:n) - sum(pi[j]*x[i,j] for j=1:n) - kappa[i])
  103. solve(subProblems[i])
  104. reducedCost=getobjectivevalue(subProblems[i])
  105. println(" reduced cost for supplier $i: ", reducedCost);
  106.  
  107. if (reducedCost < -EPSVAL)
  108. done = false
  109. xVal = getvalue(x[i,:])
  110. yVal = getvalue(y[i,:])
  111. println("yVal= $yVal")
  112. println("xVal = $xVal")
  113. pattern=zeros(Int64,n+m)
  114. touchedConstraints = ConstraintRef[]
  115. vals = Float64[]
  116. for j=1:n
  117. if xVal[j] > 0.01
  118. push!(touchedConstraints, consDestination[j])
  119. push!(vals,xVal[j])
  120. pattern[j] = 1
  121. #push!(patterns, pattern)
  122. end
  123. end
  124. push!(touchedConstraints, consSupplier[i])
  125. push!(vals,1)
  126. pattern[n+i] = 1
  127. push!(patterns, pattern)
  128.  
  129. origCost = sum(c[i,j]*xVal[j] + f[i,j]* yVal[j] for j=1:n)
  130.  
  131. @variable(
  132. Master, # Model to be modified
  133. lambdaNew >= 0, # New variable to be added
  134. #objective=1,
  135. objective=origCost, # cost coefficient of new varaible in the objective
  136. inconstraints=touchedConstraints , # constraints to be modified
  137. coefficients=vals # the coefficients of the variable in those constraints
  138. )
  139. push!(lambda, lambdaNew) # Pushing the new variable in the array of new variables
  140. end
  141. end
  142. iteration=iteration+1
  143. end # While loop ends
  144. flush(logFile)
  145. close(logFile)
  146. writeLP(Master, "master.lp", genericnames=false);
  147. println("Objective value: ", getobjectivevalue(Master))
  148. println("Optimal LP solution is: ", getvalue(lambda))
  149. println("Patterns:")
  150. lambdaVal = getvalue(lambda)
  151. println("Destination:")
  152. for j = 1:n
  153. for l = 1:length(lambdaVal)
  154. if lambdaVal[l] > 0.01
  155. print("$(patterns[l][j]) ")
  156. end
  157. end
  158. println()
  159. end
  160. println("Supplier")
  161. for i = 1:m
  162. for l = 1:length(lambdaVal)
  163. if lambdaVal[l] > 0.01
  164. print("$(patterns[l][n+i]) ")
  165. end
  166. end
  167. println()
  168. end
  169.  
  170. println("value:")
  171. for l = 1:length(lambdaVal)
  172. if lambdaVal[l] > 0.01
  173. print(lambdaVal[l], " ")
  174. end
  175. end
  176.  
  177. toc()
  178. end
  179.  
  180.  
  181. include("FCTP_InstanceGen.jl")
  182. using FCTP_InstanceGen
  183.  
  184.  
  185.  
  186. m = 20
  187. n = 20
  188. fixedWeight = 25
  189. seed = 4
  190.  
  191.  
  192. a,b,c,f = generateInstance(m,n,fixedWeight, seed)
  193.  
  194. println(c,f,a,b)
  195. tic()
  196. loka(a,b,c,f)
  197. toc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement