Skip to content

Commit

Permalink
better minors generation
Browse files Browse the repository at this point in the history
  • Loading branch information
harshangrjn committed Feb 16, 2024
1 parent 3429f79 commit db12323
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
LaplacianOpt.jl Change Log
=========================

### Staged
- Solver logging option added in `optimizer.jl`

### v0.6.2
- Minor fix for testing eigen cut orthogonality if `projected_eigen_cuts` is active

Expand Down
7 changes: 4 additions & 3 deletions examples/instances/100_nodes/100_1.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/instances/100_nodes/100_1_old.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions examples/optimizer.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#=====================================#
# MIP solvers (commercial, but fast) #
#=====================================#
function get_gurobi()
function get_gurobi(; solver_log = true)
GRB_ENV = Gurobi.Env()
return optimizer_with_attributes(
() -> Gurobi.Optimizer(GRB_ENV),
MOI.Silent() => true,
MOI.Silent() => !solver_log,
# "MIPFocus" => 3, # Focus on optimality over feasibility
"Presolve" => 1,
)
end

function get_cplex()
function get_cplex(; solver_log = true)
return JuMP.optimizer_with_attributes(
CPLEX.Optimizer,
MOI.Silent() => true,
MOI.Silent() => !solver_log,
# "CPX_PARAM_EPGAP" => 1E-6,
# "CPX_PARAM_EPOPT" => 1E-8,
# "CPX_PARAM_MIPEMPHASIS" => 1, # Focus on optimality over feasibility
Expand All @@ -26,8 +26,8 @@ end
# MIP solvers (open-source, but slow) #
#======================================#

function get_glpk()
return JuMP.optimizer_with_attributes(GLPK.Optimizer, MOI.Silent() => false)
function get_glpk(; solver_log = true)
return JuMP.optimizer_with_attributes(GLPK.Optimizer, MOI.Silent() => !solver_log)
end

# https://github.com/jump-dev/HiGHS.jl
Expand All @@ -43,10 +43,10 @@ end
# Continuous nonlinear programming solver (open-source) #
#========================================================#

function get_ipopt()
function get_ipopt(; solver_log = true)
return JuMP.optimizer_with_attributes(
Ipopt.Optimizer,
MOI.Silent() => true,
MOI.Silent() => !solver_log,
"sb" => "yes",
"max_iter" => Int(1E4),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/run_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include("optimizer.jl")
# MIP solver #
# (> Cplex 22.1 performs the best) #
#-----------------------------------#
lopt_optimizer = get_cplex()
lopt_optimizer = get_cplex(solver_log = true)

#-------------------------------------#
# User-defined input graphs #
Expand Down Expand Up @@ -43,7 +43,7 @@ end
#-------------------------------#
# User-defined params #
#-------------------------------#
num_nodes = 8
num_nodes = 100
instance = 1
data_dict, augment_budget = data_I(num_nodes, instance)
# data_dict, augment_budget = data_II()
Expand Down
44 changes: 19 additions & 25 deletions src/utility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,35 +292,29 @@ function get_unique_cycles(
end

"""
get_minor_idx(num_nodes::Int64, size::Int64)
get_minor_idx(vector::Vector{Int64}, size::Int64)
Given the number of nodes (size of the square matrix) and the
preferred size of principal sub-matrices (`<= num_nodes`), this function
outputs all the indices of correspondingly sized sub-matrices.
Given a vector and the desired size of tuples, this
recursive function outputs all possible tuples of the
specified size from the elements of the vector.
"""
function get_minor_idx(num_nodes::Int64, size::Int64)
minor_idx = Vector{Tuple{Int64,Vararg{Int64}}}()

for i in 1:num_nodes
push!(minor_idx, (i,))
end

if size >= 2
for _ in 1:(size-1)
global minor_idx_tmp = Vector{Tuple{Int64,Vararg{Int64}}}()
for k in 1:length(minor_idx)
idx_k = minor_idx[k]
if idx_k[end] <= (num_nodes - 1)
for ii in collect((idx_k[end]+1):num_nodes)
push!(minor_idx_tmp, Tuple(push!(collect(idx_k), ii)))
end
end
function get_minor_idx(vector::Vector{Int64}, size::Int64)
if size == 1
return [(x,) for x in vector]
elseif size == length(vector)
return [tuple(vector...)]
else
tuples = []
for i in 1:length(vector)
element = vector[i]
sub_tuples = LOpt.get_minor_idx(vector[(i+1):end], size - 1)
for sub_tuple in sub_tuples
push!(tuples, (element, sub_tuple...))
end
minor_idx = deepcopy(minor_idx_tmp)
end
end

return minor_idx_tmp
return tuples
end
end

function _PMinorIdx(N::Int64, sizes::Vector{Int64})
Expand All @@ -335,7 +329,7 @@ function _PMinorIdx(N::Int64, sizes::Vector{Int64})
end
for k in unique(sizes)
if (k >= 2) && (k <= N)
minor_idx_dict[k] = LOpt.get_minor_idx(N, k)
minor_idx_dict[k] = LOpt.get_minor_idx(collect(1:N), k)
end
end
end
Expand Down

0 comments on commit db12323

Please # to comment.