Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using JuMP
- using CPLEX
- EPSVAL = 0.0000001;
- function loka(a,b,c,f)
- tic()
- # ----------------------------------------------
- # Setup the master problem with the initial data
- #-----------------------------------------------
- #m - number of suppliers ( we use i to index suppliers )
- #n - number of destinations ( we use j to index destinations )
- (m,n) = size(c)
- ub=zeros(Int64, m,n)
- for i=1:m,j=1:n
- ub[i,j] = min(a[i],b[j])
- end
- #(m,n) = size(f)
- # Model for the master problem, parameter ensures that CPLEX does not produce output
- Master = Model(solver = CplexSolver(CPX_PARAM_SIMDISPLAY=0))
- # Define one initial "artificial" variable
- @variable(Master, lambda[i=1:1] >=0)
- #@variable(Master, lambda[1] >=0)
- # Setting the objective
- @objective(Master, Min, 100000*lambda[1])
- #@objective(Master, Min, sum(lambda[i] for i=1:m))
- # constraints: one per destination
- @constraint(Master, consDestination[j=1:n], b[j]*lambda[1] == b[j])
- # constraints: one per supplier
- @constraint(Master, consSupplier[i=1:m], lambda[1] == 1)
- println(Master)
- # ----------------------------------------------
- # Setup the pricing problems
- #-----------------------------------------------
- # we need a sub problem per supplier. Here we declare an array for the sub-problems
- subProblems = Array{JuMP.Model}(m)
- #subProblem=Model(solver = CplexSolver(CPX_PARAM_MIPDISPLAY=0)) # Model for the subproblem
- # variables for sub problem
- x = Array{JuMP.Variable}(m,n)
- y = Array{JuMP.Variable}(m,n)
- for i=1:m
- # Model for the pricing problem, parameter ensures that CPLEX does not produce output
- subProblems[i]=Model(solver = CplexSolver(CPX_PARAM_MIPDISPLAY=0))
- # Model for the subproblem
- # x: variables:
- for j=1:n
- x[i,j] = @variable(subProblems[i], category=:Int, lowerbound=0, basename="x[$i,$j]")
- y[i,j] = @variable(subProblems[i], category=:Bin, basename="y[$i,$j]")
- end
- # empty objective for now:
- @objective(subProblems[i], Min, 0);
- @constraint(subProblems[i],sum(x[i,j] for j=1:n) <= a[i])
- @constraint(subProblems[i], con2[j=1:n], x[i,j] - ub[i,j] *y[i,j]<= 0)
- end
- # branching
- @constraint(subProblems[8], y[8,6] == 0)
- @constraint(subProblems[4], y[4,6] == 0)
- @constraint(subProblems[4], y[4,3] == 0)
- @constraint(subProblems[4], y[4,8] == 0)
- @constraint(subProblems[6], y[6,1] == 0)
- @constraint(subProblems[6], y[6,6] == 0)
- println(y)
- iteration=1 # Counter for the while loop
- done = false;
- patterns = Array{Array{Int64,1},1}()
- # create pattern corresponding to initial artificial column
- pattern = ones(Int64, n+m)
- push!(patterns,pattern)
- logFile = open("A2log.txt","w")
- write(logFile,"iteration,master_obj\r\n")
- #while (current solution of the master problem is suboptimal, i.e., subproblem objective value > 0)
- while !done
- # Solve the master problem
- statusControlFlow=solve(Master)
- println("Iteration: ",iteration,", Objective value: ", getobjectivevalue(Master))
- logString = string(iteration, ",", getobjectivevalue(Master),"\r\n")
- write(logFile,logString)
- #Collect the dual variables
- pi = getdual(consDestination)
- kappa = getdual(consSupplier)
- println("pi: $pi")
- println("kappa: $kappa")
- done = true
- # solve all sub problems:
- for i=1:m
- # Set the objective that results from the current dual variables
- @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])
- solve(subProblems[i])
- reducedCost=getobjectivevalue(subProblems[i])
- println(" reduced cost for supplier $i: ", reducedCost);
- if (reducedCost < -EPSVAL)
- done = false
- xVal = getvalue(x[i,:])
- yVal = getvalue(y[i,:])
- println("yVal= $yVal")
- println("xVal = $xVal")
- pattern=zeros(Int64,n+m)
- touchedConstraints = ConstraintRef[]
- vals = Float64[]
- for j=1:n
- if xVal[j] > 0.01
- push!(touchedConstraints, consDestination[j])
- push!(vals,xVal[j])
- pattern[j] = 1
- #push!(patterns, pattern)
- end
- end
- push!(touchedConstraints, consSupplier[i])
- push!(vals,1)
- pattern[n+i] = 1
- push!(patterns, pattern)
- origCost = sum(c[i,j]*xVal[j] + f[i,j]* yVal[j] for j=1:n)
- @variable(
- Master, # Model to be modified
- lambdaNew >= 0, # New variable to be added
- #objective=1,
- objective=origCost, # cost coefficient of new varaible in the objective
- inconstraints=touchedConstraints , # constraints to be modified
- coefficients=vals # the coefficients of the variable in those constraints
- )
- push!(lambda, lambdaNew) # Pushing the new variable in the array of new variables
- end
- end
- iteration=iteration+1
- end # While loop ends
- flush(logFile)
- close(logFile)
- writeLP(Master, "master.lp", genericnames=false);
- println("Objective value: ", getobjectivevalue(Master))
- println("Optimal LP solution is: ", getvalue(lambda))
- println("Patterns:")
- lambdaVal = getvalue(lambda)
- println("Destination:")
- for j = 1:n
- for l = 1:length(lambdaVal)
- if lambdaVal[l] > 0.01
- print("$(patterns[l][j]) ")
- end
- end
- println()
- end
- println("Supplier")
- for i = 1:m
- for l = 1:length(lambdaVal)
- if lambdaVal[l] > 0.01
- print("$(patterns[l][n+i]) ")
- end
- end
- println()
- end
- println("value:")
- for l = 1:length(lambdaVal)
- if lambdaVal[l] > 0.01
- print(lambdaVal[l], " ")
- end
- end
- toc()
- end
- include("FCTP_InstanceGen.jl")
- using FCTP_InstanceGen
- m = 20
- n = 20
- fixedWeight = 25
- seed = 4
- a,b,c,f = generateInstance(m,n,fixedWeight, seed)
- println(c,f,a,b)
- tic()
- loka(a,b,c,f)
- toc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement