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

Score interface & simplify children generation #681

Merged
merged 1 commit into from
Aug 10, 2022
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
6 changes: 3 additions & 3 deletions src/Algorithm/Algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Base: push!

# Utilities to build algorithms
include("utilities/optimizationstate.jl")
include("utilities/helpers.jl")

# Abstract algorithm
include("interface.jl")
Expand Down Expand Up @@ -47,9 +48,8 @@ include("branching/interface.jl")
include("branching/sbnode.jl")
include("divide.jl") # TODO: DivideInput and DivideOutput are already implementation.
include("branching/selectioncriteria.jl")
include("branching/branchinggroup.jl")
include("branching/branchingrule.jl")
include("branching/candidates.jl")
include("branching/scores.jl")
include("branching/single_var_branching.jl")
include("branching/branchingalgo.jl")

include("treesearch.jl")
Expand Down
25 changes: 9 additions & 16 deletions src/Algorithm/branching/branchingalgo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,6 @@ function perform_strong_branching_with_phases!(
end

for (candidate_index, candidate) in enumerate(candidates)
#TO DO: verify if time limit is reached
# TODO: start

if phase_index == 1
generate_children!(candidate, env, reform, parent)
else
regenerate_children!(candidate, parent)
end

if phase_index > 1
sort!(candidate.children, by = x -> get_lp_primal_bound(getoptstate(x)))
Expand Down Expand Up @@ -218,10 +210,10 @@ function perform_strong_branching_with_phases!(

if phase_index < length(algo.phases)
# not the last phase, thus we compute the product score
candidate.score = product_score(candidate.children, getoptstate(parent))
candidate.score = compute_score(ProductScore(), candidate)
else
# the last phase, thus we compute the tree size score
candidate.score = tree_depth_score(candidate.children, getoptstate(parent))
candidate.score = compute_score(TreeDepthScore(), candidate)
end
print_bounds_and_score(candidate, phase_index, max_descr_length)
end
Expand All @@ -248,7 +240,7 @@ end
# - fractional priorities
# - stopping criterion
# - what happens when original_solution or extended_solution are nothing
function _select_candidates_with_branching_rule(rules, phases, selection_criterion, int_tol, parent_is_root, reform, env, original_solution, extended_solution)
function _select_candidates_with_branching_rule(rules, phases, selection_criterion, int_tol, parent_is_root, reform, env, original_solution, extended_solution, parent)
kept_branch_candidates = AbstractBranchingCandidate[]

# We sort branching rules by their root/non-root priority.
Expand Down Expand Up @@ -295,7 +287,7 @@ function _select_candidates_with_branching_rule(rules, phases, selection_criteri
output = run!(
rule, env, reform, BranchingRuleInput(
original_solution, true, max_nb_candidates, selection_criterion,
local_id, int_tol, priority
local_id, int_tol, priority, parent
)
)
append!(kept_branch_candidates, output.groups)
Expand All @@ -305,7 +297,7 @@ function _select_candidates_with_branching_rule(rules, phases, selection_criteri
output = run!(
rule, env, reform, BranchingRuleInput(
extended_solution, false, max_nb_candidates, selection_criterion,
local_id, int_tol, priority
local_id, int_tol, priority, parent
)
)
append!(kept_branch_candidates, output.groups)
Expand Down Expand Up @@ -343,7 +335,7 @@ function run!(algo::StrongBranching, env::Env, reform::Reformulation, input::Div

parent_is_root = iszero(getdepth(parent))
kept_branch_candidates = _select_candidates_with_branching_rule(
algo.rules, algo.phases, algo.selection_criterion, algo.int_tol, parent_is_root, reform, env, original_solution, extended_solution
algo.rules, algo.phases, algo.selection_criterion, algo.int_tol, parent_is_root, reform, env, original_solution, extended_solution, parent
)

if isempty(kept_branch_candidates)
Expand All @@ -353,10 +345,11 @@ function run!(algo::StrongBranching, env::Env, reform::Reformulation, input::Div

# in the case of simple branching, it remains to generate the children
if isempty(algo.phases)
children = generate_children!(kept_branch_candidates[1], env, reform, parent)
children = get_children(first(kept_branch_candidates))
return DivideOutput(children, OptimizationState(getmaster(reform)))
end

sbstate = perform_strong_branching_with_phases!(algo, env, reform, input, kept_branch_candidates)
return DivideOutput(kept_branch_candidates[1].children, sbstate)
children = get_children(first(kept_branch_candidates))
return DivideOutput(children, sbstate)
end
51 changes: 35 additions & 16 deletions src/Algorithm/branching/branchinggroup.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
#######################################################################################
# Branching Group
############################################################################################
struct ProductScore <: AbstractBranchingScore end

function compute_score(::ProductScore, candidate)
parent_lp_dual_bound = get_lp_dual_bound(getoptstate(get_parent(candidate)))
parent_ip_primal_bound = get_ip_primal_bound(getoptstate(get_parent(candidate)))
children_lp_primal_bounds = get_lp_primal_bound.(getoptstate.(get_children(candidate)))
return _product_score(parent_lp_dual_bound, parent_ip_primal_bound, children_lp_primal_bounds)
end

struct TreeDepthScore <: AbstractBranchingScore end

function compute_score(::TreeDepthScore, candidate)
parent_lp_dual_bound = get_lp_dual_bound(getoptstate(get_parent(candidate)))
parent_ip_primal_bound = get_ip_primal_bound(getoptstate(get_parent(candidate)))
children_lp_primal_bounds = get_lp_primal_bound.(getoptstate.(get_children(candidate)))
return _tree_depth_score(parent_lp_dual_bound, parent_ip_primal_bound, children_lp_primal_bounds)
end

# TODO : this method needs code documentation & context
# TODO : unit tests
function product_score(children::Vector{SbNode}, parent_optstate::OptimizationState)
function _product_score(
parent_lp_dual_bound,
parent_ip_primal_bound,
children_lp_primal_bounds::Vector
)
# TO DO : we need to mesure the gap to the cut-off value
parent_lp_dual_bound = get_lp_dual_bound(parent_optstate)
parent_delta = diff(get_ip_primal_bound(parent_optstate), parent_lp_dual_bound)
parent_delta = diff(parent_ip_primal_bound, parent_lp_dual_bound)

all_branches_above_delta = true
deltas = zeros(Float64, length(children))
for (i, node) in enumerate(children)
node_delta = diff(get_lp_primal_bound(getoptstate(node)), parent_lp_dual_bound)
for (i, child_lp_primal_bound) in enumerate(children_lp_primal_bounds)
node_delta = diff(child_lp_primal_bound, parent_lp_dual_bound)
if node_delta < parent_delta
all_branches_above_delta = false
end
Expand Down Expand Up @@ -44,7 +60,7 @@ end

# TODO : this method needs code documentation & context
# TODO ; unit tests
function number_of_leaves(gap::Float64, deltas::Vector{Float64})
function _number_of_leaves(gap::Float64, deltas::Vector{Float64})
inf::Float64 = 0.0
sup::Float64 = 1e20
mid::Float64 = 0.0
Expand All @@ -71,19 +87,22 @@ end

# TODO : this method needs code documentation & context
# TODO : this method needs unit tests
function tree_depth_score(children::Vector{SbNode}, parent_optstate::OptimizationState)
if iszero(length(children))
function _tree_depth_score(
parent_lp_dual_bound,
parent_ip_primal_bound,
children_lp_primal_bounds
)
if iszero(length(children_lp_primal_bounds))
return 0.0
end

# TO DO : we need to mesure the gap to the cut-off value
parent_lp_dual_bound = get_lp_dual_bound(parent_optstate)
parent_delta = diff(get_ip_primal_bound(parent_optstate), parent_lp_dual_bound)
parent_delta = diff(parent_ip_primal_bound, parent_lp_dual_bound)

deltas = zeros(Float64, length(children))
nb_zero_deltas = 0
for (i, node) in enumerate(children)
node_delta = diff(get_lp_primal_bound(getoptstate(node)), parent_lp_dual_bound)
for (i, child_lp_primal_bound) in enumerate(children_lp_primal_bounds)
node_delta = diff(child_lp_primal_bound, parent_lp_dual_bound)
if node_delta < 1e-6 # TO DO : use tolerance here
nb_zero_deltas += 1
end
Expand All @@ -101,7 +120,7 @@ function tree_depth_score(children::Vector{SbNode}, parent_optstate::Optimizatio
elseif length(deltas) == 1
score = -parent_delta / deltas[1]
else
numleaves = number_of_leaves(parent_delta, deltas)
numleaves = _number_of_leaves(parent_delta, deltas)
if numleaves < 0
score = -Inf
else
Expand Down
65 changes: 0 additions & 65 deletions src/Algorithm/branching/branchingrule.jl

This file was deleted.

34 changes: 29 additions & 5 deletions src/Algorithm/branching/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ abstract type AbstractBranchingCandidate end
getdescription(candidate::AbstractBranchingCandidate) =
error("getdescription not defined for branching candidates of type $(typeof(candidate)).")

# Branching candidate and branching rule should be together.
# the rule generates the candidate.

## Note: Branching candidates must be created in the BranchingRule algorithm so they do not need
## a generic constructor.

get_lhs(::AbstractBranchingCandidate) = nothing
get_lhs_distance_to_integer(::AbstractBranchingCandidate) = nothing
get_local_id(::AbstractBranchingCandidate) = nothing
get_children(::AbstractBranchingCandidate) = nothing
set_children!(::AbstractBranchingCandidate, children) = nothing
get_parent(::AbstractBranchingCandidate) = nothing

# TODO: this method should not generate the children of the tree search algorithm.
# However, AbstractBranchingCandidate should implement an interface to retrieve data to
Expand Down Expand Up @@ -46,6 +54,14 @@ select_candidates!(::Vector{C}, selection::AbstractSelectionCriterion, ::Int) wh
error("select_candidates! not defined for branching selection rule $(typeof(selection)).")


############################################################################################
# Branching score
############################################################################################

abstract type AbstractBranchingScore end

compute_score(::AbstractBranchingScore, candidate) = nothing

############################################################################################
# BranchingRuleAlgorithm
############################################################################################
Expand All @@ -62,9 +78,9 @@ struct BranchingRuleInput <: AbstractInput
local_id::Int64
int_tol::Float64
minimum_priority::Float64
parent::Node
end


"""
Output of a branching rule (branching separation algorithm)
It contains the branching candidates generated and the updated local id value
Expand All @@ -79,8 +95,16 @@ abstract type AbstractBranchingRule <: AbstractAlgorithm end
# branching rules are always manager algorithms (they manage storing and restoring storage units)
ismanager(algo::AbstractBranchingRule) = true

run!(rule::AbstractBranchingRule, ::Env, model::AbstractModel, input::BranchingRuleInput) =
error("Method run! in not defined for branching rule $(typeof(rule)), model $(typeof(model)), and input $(typeof(input)).")
apply_branching_rule(rule, env, reform, input) = nothing

function run!(rule::AbstractBranchingRule, env::Env, reform::Reformulation, input::BranchingRuleInput)
candidates = apply_branching_rule(rule, env, reform, input)
local_id = input.local_id + length(candidates)
select_candidates!(candidates, input.criterion, input.max_nb_candidates)

# apply_rule(rule::AbstractBranchingRule) =
# error("Method apply_rule(::$(typeof(rule))).")
for candidate in candidates
children = generate_children!(candidate, env, reform, input.parent)
set_children!(candidate, children)
end
return BranchingRuleOutput(local_id, candidates)
end
Loading