Skip to content

Commit

Permalink
restore some unit tests of Algorithms (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
guimarqu authored Jul 12, 2023
1 parent 1547f1a commit 9ecb4fa
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 753 deletions.
26 changes: 14 additions & 12 deletions test/unit/Algorithm/explore.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct NodeAe1 <: ClA.AbstractNode
struct NodeAe1 <: TreeSearch.AbstractNode
id::Int
depth::Int
parent::Union{Nothing, NodeAe1}
Expand Down Expand Up @@ -51,16 +51,18 @@ end

TreeSearch.tree_search_output(space::CustomSearchSpaceAe1, _) = space.visit_order

@testset "Algorithm - treesearch exploration" begin
@testset "Depth-First Search" begin
search_space = CustomSearchSpaceAe1(2, 3, 11)
visit_order = ClA.tree_search(ClA.DepthFirstStrategy(), search_space, nothing, nothing)
@test visit_order == [1, 3, 5, 7, 6, 4, 9, 8, 2, 11, 10]
end
function test_dfs()
search_space = CustomSearchSpaceAe1(2, 3, 11)
visit_order = ClA.tree_search(ClA.DepthFirstStrategy(), search_space, nothing, nothing)
@test visit_order == [1, 3, 5, 7, 6, 4, 9, 8, 2, 11, 10]
return
end
register!(unit_tests, "explore", test_dfs)

@testset "Best-First Search" begin
search_space = CustomSearchSpaceAe1(2, 3, 11)
visit_order = ClA.tree_search(CustomBestFirstSearch(), search_space, nothing, nothing)
@test visit_order == [1, 3, 5, 7, 6, 4, 9, 8, 2, 11, 10]
end

function test_bfs()
search_space = CustomSearchSpaceAe1(2, 3, 11)
visit_order = ClA.tree_search(CustomBestFirstSearch(), search_space, nothing, nothing)
@test visit_order == [1, 3, 5, 7, 6, 4, 9, 8, 2, 11, 10]
end
register!(unit_tests, "explore", test_bfs)
585 changes: 294 additions & 291 deletions test/unit/Algorithm/optimizationstate.jl

Large diffs are not rendered by default.

100 changes: 50 additions & 50 deletions test/unit/Algorithm/presolve.jl
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
@testset "Algorithm - Presolve" begin
@testset "RemovalOfFixedVariables" begin
@test ClA._fix_var(0.99, 1.01, 0.1)
@test !ClA._fix_var(0.98, 1.1, 0.1)
@test ClA._fix_var(1.5, 1.5, 0.1)

@test_broken !ClA._infeasible_var(1.09, 0.91, 0.1)
@test ClA._infeasible_var(1.2, 0.9, 0.1)

# Create the following formulation:
# min x1 + 2x2 + 3x3
# st. x1 >= 1
# x2 >= 2
# x3 >= 3
# x1 + 2x2 + 3x3 >= 10
env = CL.Env{ClMP.VarId}(CL.Params())
form = ClMP.create_formulation!(env, ClMP.DwMaster())
vars = Dict{String, ClMP.Variable}()
for i in 1:3
x = ClMP.setvar!(form, "x$i", ClMP.OriginalVar; cost = i, lb = i)
vars["x$i"] = x
end

members = Dict{ClMP.VarId,Float64}(
ClMP.getid(vars["x1"]) => 1,
ClMP.getid(vars["x2"]) => 2,
ClMP.getid(vars["x3"]) => 3
)
c = ClMP.setconstr!(form, "c", ClMP.OriginalConstr;
rhs = 10, sense = ClMP.Greater, members = members
)
DynamicSparseArrays.closefillmode!(ClMP.getcoefmatrix(form))

@test ClMP.getcurrhs(form, c) == 10
# @testset "Algorithm - Presolve" begin
# @testset "RemovalOfFixedVariables" begin
# @test ClA._fix_var(0.99, 1.01, 0.1)
# @test !ClA._fix_var(0.98, 1.1, 0.1)
# @test ClA._fix_var(1.5, 1.5, 0.1)

# @test_broken !ClA._infeasible_var(1.09, 0.91, 0.1)
# @test ClA._infeasible_var(1.2, 0.9, 0.1)

# # Create the following formulation:
# # min x1 + 2x2 + 3x3
# # st. x1 >= 1
# # x2 >= 2
# # x3 >= 3
# # x1 + 2x2 + 3x3 >= 10
# env = CL.Env{ClMP.VarId}(CL.Params())
# form = ClMP.create_formulation!(env, ClMP.DwMaster())
# vars = Dict{String, ClMP.Variable}()
# for i in 1:3
# x = ClMP.setvar!(form, "x$i", ClMP.OriginalVar; cost = i, lb = i)
# vars["x$i"] = x
# end

# members = Dict{ClMP.VarId,Float64}(
# ClMP.getid(vars["x1"]) => 1,
# ClMP.getid(vars["x2"]) => 2,
# ClMP.getid(vars["x3"]) => 3
# )
# c = ClMP.setconstr!(form, "c", ClMP.OriginalConstr;
# rhs = 10, sense = ClMP.Greater, members = members
# )
# DynamicSparseArrays.closefillmode!(ClMP.getcoefmatrix(form))

# @test ClMP.getcurrhs(form, c) == 10

ClMP.setcurlb!(form, vars["x1"], 2)
ClMP.setcurub!(form, vars["x1"], 2)
@test ClMP.getcurrhs(form, c) == 10
# ClMP.setcurlb!(form, vars["x1"], 2)
# ClMP.setcurub!(form, vars["x1"], 2)
# @test ClMP.getcurrhs(form, c) == 10

ClA.treat!(form, ClA.RemovalOfFixedVariables(1e-6))
# ClA.treat!(form, ClA.RemovalOfFixedVariables(1e-6))

@test ClMP.getcurrhs(form, c) == 10 - 2
# @test ClMP.getcurrhs(form, c) == 10 - 2

ClMP.setcurlb!(form, vars["x2"], 3)
ClMP.setcurub!(form, vars["x2"], 3)
# ClMP.setcurlb!(form, vars["x2"], 3)
# ClMP.setcurub!(form, vars["x2"], 3)

ClA.treat!(form, ClA.RemovalOfFixedVariables(1e-6))
# ClA.treat!(form, ClA.RemovalOfFixedVariables(1e-6))

@test ClMP.getcurrhs(form, c) == 10 - 2 - 2*3
# @test ClMP.getcurrhs(form, c) == 10 - 2 - 2*3

ClMP.unfix!(form, vars["x1"])
ClMP.setcurlb!(form, vars["x1"], 1)
ClA.treat!(form, ClA.RemovalOfFixedVariables(1e-6))
@test ClMP.getcurrhs(form, c) == 10 - 2*3
return
end
end
# ClMP.unfix!(form, vars["x1"])
# ClMP.setcurlb!(form, vars["x1"], 1)
# ClA.treat!(form, ClA.RemovalOfFixedVariables(1e-6))
# @test ClMP.getcurrhs(form, c) == 10 - 2*3
# return
# end
# end
5 changes: 3 additions & 2 deletions test/unit/Algorithm/record_mastercolumns.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testset "Unit - MasterColumnsRecord" begin
function unit_master_columns_record()
function test_record(state, cost, lb, ub, fixed)
@test state.cost == cost
@test state.lb == lb
Expand Down Expand Up @@ -75,4 +75,5 @@
test_var(form, vars["v1"], 1, 5, Inf, false)
test_var(form, vars["v2"], 2, 0, 12, false)
test_var(form, vars["v3"], 4.6, 3.5, 3.5, true)
end
end
register!(unit_tests, "master_columns_record", unit_master_columns_record)
87 changes: 43 additions & 44 deletions test/unit/Algorithm/subsolvers.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
@testset "Algorithm - Subsolvers" begin
@testset "MoiOptimize - Parameters reset after optimize_with_moi!" begin
# Create the formulation:
# Min x
# s.t. x >= 1

env = Env{ClMP.VarId}(Coluna.Params())
form = ClMP.create_formulation!(env, ClMP.Original(), obj_sense = ClMP.MinSense)
ClMP.setvar!(form, "x", ClMP.OriginalVar; cost = 1, lb = 1)
closefillmode!(ClMP.getcoefmatrix(form))

algo1 = ClA.MoiOptimize(
time_limit = 1200,
silent = false,
custom_parameters = Dict(
"it_lim" => 60
)
function reset_parameters_after_optimize_with_moi()
# Create the formulation:
# Min x
# s.t. x >= 1

env = Env{ClMP.VarId}(Coluna.Params())
form = ClMP.create_formulation!(env, ClMP.Original(), obj_sense = ClMP.MinSense)
ClMP.setvar!(form, "x", ClMP.OriginalVar; cost = 1, lb = 1)
closefillmode!(ClMP.getcoefmatrix(form))

algo1 = ClA.MoiOptimize(
time_limit = 1200,
silent = false,
custom_parameters = Dict(
"it_lim" => 60
)
)

algo2 = ClA.MoiOptimize(
silent = false,
custom_parameters = Dict(
"mip_gap" => 0.03
)
algo2 = ClA.MoiOptimize(
silent = false,
custom_parameters = Dict(
"mip_gap" => 0.03
)
)

optimizer = ClMP.MoiOptimizer(MOI._instantiate_and_check(GLPK.Optimizer))
optimizer = ClMP.MoiOptimizer(MOI._instantiate_and_check(GLPK.Optimizer))

get_time_lim() = MOI.get(optimizer.inner, MOI.TimeLimitSec())
get_silent() = MOI.get(optimizer.inner, MOI.Silent())
get_it_lim() = MOI.get(optimizer.inner, MOI.RawOptimizerAttribute("it_lim"))
get_mip_gap() = MOI.get(optimizer.inner, MOI.RawOptimizerAttribute("mip_gap"))
get_time_lim() = MOI.get(optimizer.inner, MOI.TimeLimitSec())
get_silent() = MOI.get(optimizer.inner, MOI.Silent())
get_it_lim() = MOI.get(optimizer.inner, MOI.RawOptimizerAttribute("it_lim"))
get_mip_gap() = MOI.get(optimizer.inner, MOI.RawOptimizerAttribute("mip_gap"))

default_time_lim = get_time_lim()
default_silent = get_silent()
default_it_lim = get_it_lim()
default_mip_gap = get_mip_gap()
default_time_lim = get_time_lim()
default_silent = get_silent()
default_it_lim = get_it_lim()
default_mip_gap = get_mip_gap()

ClA.optimize_with_moi!(optimizer, form, algo1, ClA.OptimizationState(form))
ClA.optimize_with_moi!(optimizer, form, algo1, ClA.OptimizationState(form))

@test get_time_lim() == default_time_lim
@test get_silent() == default_silent
@test get_it_lim() == default_it_lim
@test get_mip_gap() == default_mip_gap
@test get_time_lim() == default_time_lim
@test get_silent() == default_silent
@test get_it_lim() == default_it_lim
@test get_mip_gap() == default_mip_gap

ClA.optimize_with_moi!(optimizer, form, algo2, ClA.OptimizationState(form))
ClA.optimize_with_moi!(optimizer, form, algo2, ClA.OptimizationState(form))

@test get_time_lim() == default_time_lim
@test get_silent() == default_silent
@test get_it_lim() == default_it_lim
@test get_mip_gap() == default_mip_gap
return
end
end
@test get_time_lim() == default_time_lim
@test get_silent() == default_silent
@test get_it_lim() == default_it_lim
@test get_mip_gap() == default_mip_gap
return
end
register!(unit_tests, "subsolvers", reset_parameters_after_optimize_with_moi)
Loading

0 comments on commit 9ecb4fa

Please sign in to comment.