Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix json printer #1009

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Algorithm/branchcutprice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Parameters :
function BranchCutAndPriceAlgorithm(;
maxnumnodes::Int = 100000,
branchingtreefile::String = "",
jsonfile::String = "",
opt_atol::Float64 = Coluna.DEF_OPTIMALITY_ATOL,
opt_rtol::Float64 = Coluna.DEF_OPTIMALITY_RTOL,
restmastipheur_timelimit::Int = 600,
Expand Down Expand Up @@ -151,6 +152,7 @@ function BranchCutAndPriceAlgorithm(;
dividealg = branching,
maxnumnodes = maxnumnodes,
branchingtreefile = branchingtreefile,
jsonfile = jsonfile,
opt_atol = opt_atol;
opt_rtol = opt_rtol
)
Expand Down
34 changes: 20 additions & 14 deletions src/Algorithm/treesearch/printer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,23 @@ filename(f::JSONFilePrinter) = f.filename

function init_tree_search_file!(f::JSONFilePrinter)
open(filename(f), "w") do file
println(file, "{")
println(file, "\t\"nodes\": [")
println(file, "[")
end
return
end

# To get rid of "Inf".
function _printed_num(num)
if isinf(num)
if num < 0
return -99999999999
else
return +99999999999
end
end
return num
end

function print_node_in_tree_search_file!(f::JSONFilePrinter, node::PrintedNode, sp::PrinterSearchSpace, env)
is_root_node = iszero(getdepth(node.inner))
current_node_id = node.tree_order_id
Expand All @@ -146,22 +157,18 @@ function print_node_in_tree_search_file!(f::JSONFilePrinter, node::PrintedNode,
infeasible = getterminationstatus(node.inner.conquer_output) == INFEASIBLE

open(filename(f), "r+") do file
# rewind the closing brace character
seekend(file)
pos = position(file)
seek(file, pos - 1)

# start writing over this character
@printf file "\n\t\t{\"node_id\": %i, " current_node_id
@printf file "\n\t\t{ \"node_id\": %i, " current_node_id
@printf file "\"depth\": %i, " current_node_depth
@printf file "\"parent_id\": %s, " is_root_node ? "null" : string(current_parent_id)
@printf file "\"time\": %.0f, " time
@printf file "\"primal_bound\": %.4f, " global_pb
@printf file "\"local_dual_bound\": %.4f, " local_db
@printf file "\"global_dual_bound\": %.4f, " global_db
@printf file "\"time\": %.2f, " time
@printf file "\"primal_bound\": %.4f, " _printed_num(global_pb)
@printf file "\"local_dual_bound\": %.4f, " _printed_num(local_db)
@printf file "\"global_dual_bound\": %.4f, " _printed_num(global_db)
@printf file "\"pruned\": %s, " gap_closed ? "true" : "false"
@printf file "\"infeasible\": %s, " infeasible ? "true" : "false"
@printf file "\"branch\": \"%s\"}," is_root_node ? "null" : br_constr_description
@printf file "\"branch\": %s },\n" is_root_node ? "null" : string("\"", br_constr_description, "\"")
end
return
end
Expand All @@ -171,10 +178,9 @@ function close_tree_search_file!(f::JSONFilePrinter)
# rewind the closing brace character
seekend(file)
pos = position(file)
seek(file, pos - 1)
seek(file, pos - 2)
# just move the closing brace to the next line
println(file, "\n\t]")
println(file, "}")
end
return
end
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/gap/generalized_assignment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function gap_toy_instance_2()
@test MOI.get(model, MOI.NumberOfVariables()) == length(x)
@test MOI.get(model, MOI.SolverName()) == "Coluna"
end
register!(e2e_tests, "gap", gap_toy_instance)
register!(e2e_tests, "gap", gap_toy_instance_2)

function gap_strong_branching()
data = ClD.GeneralizedAssignment.data("mediumgapcuts3.txt")
Expand Down