Hi ,
The callback is not working in GLPK. The below code is working in fine Gurobi.
using JuMP, Gurobi,MathOptInterface,GLPK
model = Model(Gurobi.Optimizer)
set_optimizer_attribute(model, "OutputFlag", 0)
L = [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 ];
P =[20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20];
L0 = [1 3] ;
t=1;
d=1;
B=5;
Task = 25
Task_1 = Task+1;
Task_2 =Task+2;
T=Task_2;
reasons = UInt8[]
function my_callback_function(cb_data, cb_where::Cint)
# function my_callback_function(cb_data)
#xpr3 = GenericAffExpr{Float64,VariableRef}()
#xpr2= GenericAffExpr{Float64,VariableRef}()
push!(cb_calls, cb_where)
#println("cb_where",cb_where)
if cb_where != GRB_CB_MIPSOL && cb_where != GRB_CB_MIPNODE
return
end
if cb_where == GRB_CB_MIPNODE
resultP = Ref{Cint}()
GRBcbget(cb_data, cb_where, GRB_CB_MIPNODE_STATUS, resultP)
if resultP[] != GRB_OPTIMAL
return # Solution is something other than optimal.
end
end
if cb_where == GRB_CB_MIPSOL
# reason = GLPK.glp_ios_reason(cb_data.tree)
# push!(reasons, reason)
# if reason != GLPK.GLP_IROWGEN
# return
# end
#println("manoj inside mycallback_juliaMIP sol")
Gurobi.load_callback_variable_primal(cb_data, cb_where)
#N=10
K=Loaders
Task =25
Task_1 = Task+1;
Task_2 =Task+2;
T=Task_2
components = [];
nComps = 0;
S=[]
secFound =false;
precFound =false;
visited = Bool[];
path = UInt8[]
secFound = false;
flag =false
for k=1:Loaders
for i in 1:Task_2
push!(components,0);
end
_y = callback_value.(Ref(cb_data), y_ik)
_x = callback_value.(Ref(cb_data), x_kij)
# _y = callback_value(cb_data, y_ik)
#_x = callback_value(cb_data, x_kij)
u = 1;
flag = false;
nComps = 0#0; #manoj2402 changed to 1
components[1]=1;
empty!(path);
while flag == false
# println("manoj 192 $flag")
for v=2:Task_2
if u!=v
if _x[k,u,v]>0.99999 # manoj 2402 changed > to >=
#println("manoj 164 u $u v $v T $T k $k _x ",_x[k,u,v])
u=v;
v=T+2;
# println("manoj 154 u $u v $v T $T k $k ");
if (u==T)
flag =true;
# println("manoj 157 u $u v $v T $T k $k ");
else
push!(path,u);
end # if (u==length(inputparam.N))
components[u]=1;
end # if _x[k,u,v]>0.9
end #if u!=v
end #for v
end #while
nComps+=1;
for i =2:Task_1;
if (components[i]==0 && _y[i-1,k]>0.99999) # manoj 2402 changed > to >=
u=i;
flag =false;
nComps+=1;
empty!(S);
while flag ==false
# println("flag 180 $flag")
for j=2:Task_2
if u!=j
if (_x[k,u,j]>0.9 )# manoj 1502 changed > to >=
components[u] = nComps;
#println("manoj 185")
u=j;
j =T+2;
push!(S,u);
if u==i
flag =true;
# println("manoj 191")
xpr3=0
xpr2=0
for g = 1:length(S)
for h = 1:length(S)
if (g!=h )
xpr2 += x_kij[k,S[g],S[h]]
xpr3 += _x[k,S[g],S[h]]
end
end
end
# println("xpr3 494 ",xpr3 ," S ",S , length(S)-1+1e-6 )
if xpr3>length(S)-1#+1e-6
# println("inside applying constraint")
con = @build_constraint(xpr2 <=length(S)-1)
# println("Adding 247 ",length(S))
#MOI.submit(model, MOI.LazyConstraint(cb_data), con);
MOI.submit(model, MOI.LazyConstraint(cb_data), con);
#MOI.set(model, MOI.UserCutCallback(cb_data),con)
secFound = true;
end
# println("manoj 257")
end # if u==i
end # _x[k,u,j]>0.9
end #u!=j
end #j=2:length(inputparam.N)
end #while flag ==false
end ## (components[i]==0 && _y[i-1,k]>0.5)
end # for i =2:11;
empty!(S);
push!(S,1); # push!(S,0); manoj 2402 changed 0 to 1
for i=1:Task
push!(visited,false);
#println("manoj 268")
#visited[i]=false
end
precFound = false
if length(path)>=2
for p=1:length(path)
u = path[p]
push!(S,u)
visited[u-1] = true
# println("manoj 277")
#= if(length(predec[u-1])>0)
for i=1:length(predec[u-1])
if _y[predec[u-1,i]-1,k] >0.9999 &&
!visited[predec[u-1,i]-1]
xpr =0;
for m=1:length(S)
for n=1:length(S)
if (m!=n && n!=0)
xrp+=x_kij[k,S[m],S[n]]
end
end
end
xpr+=y_ik[u,k]
con = @build_constraint(xpr<=length(S)-1)
MOI.submit(mod, MOI.LazyConstraint(cb_data), con)
precFound = true
end#
end # for i
end# if(length(predec[u-1])>0)
if precFound == true
p =length(path)
end =#
end #p=1:length(path)
end #length(path)>=2 =#
end #k=1:2
end # cb_where == GRB_CB_MIPSOL
#return 1
#println("mamoj here 261")
end
#println("iter===",iter)
#expr = GenericAffExpr{Float64,VariableRef}()
@variable(model,x_kij[k in 1:Loaders,i in 1:Task_1,j in 1:Task_2], Bin);
@variable(model,y_ik[i in 1:Task, k in 1:Loaders], Bin);
#println("manoj 256 ", Task)
#@constraint(model,cut_1,x_kij[1,1,1]==0) #manoj 10032021
#println(" ")
@constraint(model,con_1[k in 1:Loaders,i in 1:Task_1,j in 2:Task_2;i==j],
x_kij[k,i,j]==0);
#@constraint(model,x_kij[1,3,8]==1)
#@constraint(model,x_kij[1,8,4]==1)
#println("con_1 ",con_1)
####new
#@constraint(model,con_1[k in 1:2,i in 1:11,j in 1:12;i==j],x_kij[k,i,j]==0);
#println("con_1",con_1)
@constraint(model,con_2[k in 1:Loaders,i in 1:Task_1],x_kij[k,i,1]==0); #i in Task+0
# println("con_2 ",con_2)
@variable(model,eta);
@objective(model, Min, eta)
@constraint(model,con_3[k in 1:Loaders],sum(x_kij[k,1,j] for j in 2:Task_2 )==1) # j in Task+T
#println("con_3 ",con_3)
@constraint(model,con_4[k in 1:Loaders],sum(x_kij[k,j,Task_2] for j in 1:Task_1 )==1) # j in Task+0
# println("con_4 ",con_4)
#//assignment constraints
# @constraint(model,con_5[ i=2:11,k=1:2],
# sum(x_kij[k,i,j] for j in 2:12 if i != j)-y_ik[i-1,k]==0); #j in Task+T
# @constraint(model,con_6[ i=2:11 ,k=1:2],
# sum(x_kij[k,j,i] for j in 1:11 if i != j)-y_ik[i-1,k]==0); # j in Task+0
for i=2:Task_1 # i in tasks
for k=1:Loaders
expr =0;
for j=2:Task_2 # j in tasks +T
if i!=j
expr+=x_kij[k,i,j]
end
end
@constraint(model,expr-y_ik[i-1,k]==0)
#println("581 i $i k$k " ,expr)
#println("584 ",expr)
expr=0;
for j=1:Task_1 # j in Task+0
if i!=j
expr+=x_kij[k,j,i]
end
end
@constraint(model,expr-y_ik[i-1,k]==0)
# println("589 i $i k$k " ,expr)
#println("593 ",expr)
end
end
#//flow conservation
#@constraint(model,con_7[i =1:10],sum(y_ik[i,k] for k in 1:2)==1)
for i =1:Task
expr =0
for k=1:Loaders
expr+=y_ik[i,k]
end
@constraint(model,expr==1)
#println("601 i $i " ,expr)
end
#//MinMax approach
for k=1:Loaders
xpr =0
for j=2:Task_1 # j in Task
#xpr = xpr +
xpr += x_kij[k,1,j]*(t*abs(L0[k]-L[j-1])+P[j-1])
#xpr += x_kij[k,1,j] * (t * abs(L0[k] - L[j - 1]) + P[j - 1])
end
#println("expr 636 ",xpr)
for i =2:Task_1 #i,j in tasks
for j =2:Task_1
if i != j
#xpr =xpr+
#println("i ",i,"j ",j)
xpr += x_kij[k,i,j]*(t*abs(L[i-1]-L[j-1])+P[j-1])
#println("manoj 569")
#xpr += x_kij[k,i,j] * (t * abs(L[i - 1] - L[j - 1]) + P[j - 1]);
end
end
end
@constraint(model, xpr<=eta)
# println("649 " ,xpr)
#println("xpr=",xpr )
end
for i=2:Task_1 #ij in Task
for j=i+1:Task_1 #j=2:Task_1#
if i!=j
for k=1:Loaders
#xpr=0
# println("583 i,j",i,j)
xpr = x_kij[k,i,j]+x_kij[k,j,i]
@constraint(model,xpr<=1)
#println("636 i $i j $j k$k " ,xpr)
#println("641 ",xpr)
end
end
end
end
# @constraint(model,con_9[ i=2:11, j=i+1:11; i!=j],
# sum(x_kij[k,i,j]+x_kij[k,j,i] for k=1:2)<=1)
for k=1:Loaders
K=Loaders
min = (k-1)*(d+1)+1;
max = B - (K-k)*(1+d);
# println(" ")
# println("min $min max $max")
xpr3 =0
for i=1:Task
if (L[i]<min || L[i]>max)
#println("L[i]=",L[i], "=",i)
xpr3 +=y_ik[i,k]
#println("manoj 603")
end
end
@constraint(model,xpr3==0)
#println("663 " ,xpr3)
end
# if iter>1
# addNoGood()
# end
#cb_calls = Cint[]
cb_calls = Cint[]
MOI.set(model, MOI.RawParameter("LazyConstraints"), 1)
MOI.set(model, Gurobi.CallbackFunction(), my_callback_function)
```
Thanks for your help.
Regatds,
Manoj