Skip to content

Commit

Permalink
added node limit
Browse files Browse the repository at this point in the history
  • Loading branch information
matbesancon committed Dec 12, 2024
1 parent 6466e4f commit fabd6bd
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ function build_bnb_callback(
end
end

if iteration > tree.root.options[:node_limit]
tree.root.problem.solving_stage = NODE_LIMIT_REACHED
end

fw_time = Dates.value(node.fw_time)
fw_iter = if !isempty(fw_iterations)
fw_iterations[end]
Expand Down
4 changes: 2 additions & 2 deletions src/custom_bonobo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ function Bonobo.get_solution(
) where {N,R,V,S<:FrankWolfeSolution{N,V}}
if isempty(tree.solutions)
@warn "There is no solution in the tree. This behaviour can happen if you have supplied
\na custom domain oracle. In that case, try to increase the time limit. If you have not specified a
\na custom domain oracle. In that case, try to increase the time or node limit. If you have not specified a
\ndomain oracle, please report!"
@assert tree.root.problem.solving_stage == TIME_LIMIT_REACHED
@assert tree.root.problem.solving_stage in (TIME_LIMIT_REACHED, NODE_LIMIT_REACHED)
return nothing
end
return tree.solutions[result].solution
Expand Down
4 changes: 4 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function solve(
dual_gap=1e-6,
rel_dual_gap=1.0e-2,
time_limit=Inf,
node_limit=Inf,
print_iter=100,
dual_gap_decay_factor=0.8,
max_fw_iter=10000,
Expand Down Expand Up @@ -225,6 +226,7 @@ function solve(
:sharpness_constant => sharpness_constant,
:sharpness_exponent => sharpness_exponent,
:time_limit => time_limit,
:node_limit => node_limit,
:usePostsolve => use_postsolve,
:variant => variant,
:use_shadow_set => use_shadow_set,
Expand Down Expand Up @@ -361,6 +363,8 @@ function postsolve(tree, result, time_ref, verbose, max_iteration_post)
tree.root.problem.solving_stage = OPT_TREE_EMPTY
elseif tree.root.problem.solving_stage == TIME_LIMIT_REACHED
status_string = "Time limit reached"
elseif tree.root.problem.solving_stage == NODE_LIMIT_REACHED
status_string = "Node limit reached"
else
status_string = "Optimal (tolerance reached)"
tree.root.problem.solving_stage = OPT_GAP_REACHED
Expand Down
1 change: 1 addition & 0 deletions src/problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Enum for the solving stage
OPT_GAP_REACHED = 1
OPT_TREE_EMPTY = 2
TIME_LIMIT_REACHED = 3
NODE_LIMIT_REACHED = 4
end

"""
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Checks if the branch and bound can be stopped.
By default (in Bonobo) stops then the priority queue is empty.
"""
function Bonobo.terminated(tree::Bonobo.BnBTree{<:FrankWolfeNode})
if tree.root.problem.solving_stage == TIME_LIMIT_REACHED
if tree.root.problem.solving_stage in (TIME_LIMIT_REACHED, NODE_LIMIT_REACHED)
return true
end
absgap = tree.incumbent - tree.lb
Expand Down
6 changes: 6 additions & 0 deletions test/interface_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ end
@test sum(isapprox.(x_adaptive, x_monotonic, atol=1e-6, rtol=1e-3)) == n
@test sum(isapprox.(x_agnostic, x_monotonic, atol=1e-6, rtol=1e-3)) == n
@test sum(isapprox.(x_adaptive, x_agnostic, atol=1e-6, rtol=1e-3)) == n

x_monotonic, _, result_monotonic_node_limit =
Boscia.solve(f, grad!, lmo, verbose=true, line_search=line_search, node_limit=2, print_iter=1)

@test result_monotonic_node_limit[:list_ub] <= 3
@test result_monotonic_node_limit[:status] == "Node limit reached"
end

n = 20
Expand Down

0 comments on commit fabd6bd

Please sign in to comment.