CPLEX - Setting branching priority

Hi all!

I have been trying to set branching priority (in CPLEX) for a set of my variables (z) but I cannot do it. I tried using the linear indices and the internal model, but I get a method not found error.

Source code:

model_indices = zeros(length(indices.V)*length(indices.H))
priorities = zeros(length(indices.V)*length(indices.H))
idx = 1
for i=indices.V_cus, t=indices.H
    lin_idx = linearindex(z[i,t])
    model_indices[idx] = lin_idx
    priorities[idx] = 10
    idx += 1
end
CPLEX.set_branching_priority(m.internalModel, model_indices, priorities)

Error:
LoadError: MethodError: no method matching set_branching_priority(::CPLEX.CplexMathProgModel, ::Array{Float64,1}, ::Array{Float64,1})
Closest candidates are:
set_branching_priority(::Any, ::Any, ::Any, !Matched::Any) at C:\Users\lmano.julia\v0.6\CPLEX\src\cpx_solve.jl:21
set_branching_priority(!Matched::CPLEX.Model, ::Any, ::Any) at C:\Users\lmano.julia\v0.6\CPLEX\src\cpx_solve.jl:18
set_branching_priority(!Matched::CPLEX.Model, ::Any) at C:\Users\lmano.julia\v0.6\CPLEX\src\cpx_solve.jl:15
while loading C:\MyWork[Workspace] MIRP\Julia\MIRP\src\run.jl, in expression starting on line 34

The problem seems to be that CPLEX.jl set_branching_priority function needs a CPLEX.Model object while the internal model is a CPLEX.CplexMathProgModel. Is that a possible mismatch that needs to be fixed or I am doing something wrong. Any ideas on how to fix this?

Thanks,
Lefteris

Hi Lefteris,

You can access the CPLEX Model as JuMP.internalmodel(m).inner.

m.internalModel is actual a MathProgBase wrapper of the internal C model. You can see the struct here:
https://github.com/JuliaOpt/CPLEX.jl/blob/81d7dbb9947960f56c81f7b32dcdf612fa99f7ba/src/CplexSolverInterface.jl#L3-L14

In regard to set_branching_priority, you seem to know what your are doing. You can find the source in CPLEX.jl here:
https://github.com/JuliaOpt/CPLEX.jl/blob/ab9099cd4886c8d12ffb20218f80391ae61f5cea/src/cpx_solve.jl#L24-L37

You can find the C documentation for copyorder here:
https://www.ibm.com/support/knowledgecenter/SSSA5P_12.6.1/ilog.odms.cplex.help/refcallablelibrary/mipapi/copyorder.html

Thank you very much Oscar! Indeed it works when I use the inner field of the internalmodel struct.

Hi Oscar and Lefteris,

Thanks for your post. I’m also using set_branching_priority. I encounter a problem. Before I use CPLEX to solve the model, model.internalModel seems to be Void.

So I tried to solve(model) first, then model.internalModel.inner becomes a CPLEX object. I did the following experiment

priorities = [1, 2]
model_indices = [1, 2]
CPLEX.set_branching_priority(m.internalModel.inner, model_indices, priorities)

Then I got the following error

Root node processing (before b&c):
Real time = 0.00 sec. (0.25 ticks)
Sequential b&c:
Real time = 0.00 sec. (0.00 ticks)
------------
Total (root+branch&cut) = 0.00 sec. (0.25 ticks)
ERROR: CPLEX: error getting error message(!)
Stacktrace:
[1] get_error_msg(::CPLEX.Env, ::Int32) at /home/canl1/.julia/v0.6/CPLEX/src/cpx_env.jl:71
[2] Type at /home/canl1/.julia/v0.6/CPLEX/src/cpx_env.jl:89 [inlined]
[3] optimize!(::CPLEX.Model) at /home/canl1/.julia/v0.6/CPLEX/src/cpx_solve.jl:11
[4] optimize!(::CPLEX.CplexMathProgModel) at /home/canl1/.julia/v0.6/CPLEX/src/CplexSolverInterface.jl:191
[5] #solve#116(::Bool, ::Bool, ::Bool, ::Array{Any,1}, ::Function, ::JuMP.Model) at /home/canl1/.julia/v0.6/JuMP/src/solvers.jl:175
[6] solve(::JuMP.Model) at /home/canl1/.julia/v0.6/JuMP/src/solvers.jl:150

I would appreciate it if you can share some solutions to this problem. Thanks!

Best regards,
Can

Please provide a minimum working example:

using JuMP
using CPLEX
m = Model(solver=CplexSolver())
@variable(m, x[i in 1:2], Bin)
@objective(m, Min, x[1] + x[2])
priorities = [1, 2]
model_indices = [1, 2]
CPLEX.set_branching_priority(m.internalModel.inner, model_indices, priorities)

The error is

ERROR: LoadError: type Void has no field inner
Stacktrace:
 [1] include_from_node1(::String) at ./loading.jl:576
 [2] include(::String) at ./sysimg.jl:14
 [3] process_options(::Base.JLOptions) at ./client.jl:305
 [4] _start() at ./client.jl:371
while loading /home/canl1/work/learnbranching/temp.jl, in expression starting on line 8

Run JuMP.build(m) prior to setting the branching priority.

Thanks Oscar!

Hi everyone,

I was wondering if the CPLEX.set_branching_priority function still exists in the latest versions please?
I tried running the suggested codes but I always get

UndefVarError: set_branching_priority not defined

Can anyone help me please?
Thank you

Hi @SalmaHammani, welcome to the forum.

I was wondering if the CPLEX.set_branching_priority function still exists in the latest versions please?

It does not exist.

You’ll need to use https://www.ibm.com/docs/en/icos/20.1.0?topic=c-cpxxcopyorder-cpxcopyorder

Here’s an example:

julia> using JuMP, CPLEX

julia> function c_column(cplex::CPLEX.Optimizer, x::VariableRef)
           return Cint(CPLEX.column(cplex, index(x)) - 1)
       end
c_column (generic function with 1 method)

julia> model = direct_model(CPLEX.Optimizer())
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: DIRECT
Solver name: CPLEX

julia> @variable(model, x[1:3], Bin)
3-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]

julia> cplex = backend(model)
Ptr{Nothing} @0x00007fb4b18c1a30

julia> indices = c_column.(cplex, x)
3-element Vector{Int32}:
 0
 1
 2

julia> priority = Cint[1, 2, 1]
3-element Vector{Int32}:
 1
 2
 1

julia> direction = [CPX_BRANCH_UP, CPX_BRANCH_UP, CPX_BRANCH_UP]
3-element Vector{Int64}:
 1
 1
 1

julia> CPLEX.CPXcopyorder(cplex.env, cplex.lp, length(indices), indices, priority, direction)
0