Skip to content

Commit

Permalink
Merge pull request #11 from kibaekkim/kkim
Browse files Browse the repository at this point in the history
resolved #10
  • Loading branch information
kibaekkim authored Aug 14, 2020
2 parents b9038d9 + b3266bb commit ce82fea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
10 changes: 7 additions & 3 deletions src/impl-JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ create_child_node(current_node::JuMPNode{T}, variable::JuMP.VariableRef, lb::Rea
create_child_node_with_lb(current_node::JuMPNode{T}, variable::JuMP.VariableRef, lb::Real) where T<:AbstractBranch = create_child_node(current_node, VariableBranch(Dict(variable=>lb), empty_variable_bound()))
create_child_node_with_ub(current_node::JuMPNode{T}, variable::JuMP.VariableRef, ub::Real) where T<:AbstractBranch = create_child_node(current_node, VariableBranch(empty_variable_bound(), Dict(variable=>ub)))

function processed!(tree::AbstractTree, node::JuMPNode)
# Rollback the branch objects
adjust_branch!(node.auxiliary_data["bounds_changed"])

Base.push!(tree.processed, node)
end

# basic bounding function
function bound!(node::JuMPNode)
JuMP.optimize!(node.model)
Expand All @@ -69,9 +76,6 @@ function bound!(node::JuMPNode)
@warn "Unexpected node solution status: $(node.solution_status)"
node.bound = -Inf
end

# Rollback the branch objects
adjust_branch!(node.auxiliary_data["bounds_changed"])
end

function apply_changes!(node::JuMPNode)
Expand Down
19 changes: 0 additions & 19 deletions src/node.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

abstract type AbstractNode end
abstract type AbstractBranch end

Expand All @@ -17,24 +16,6 @@ macro abstract_node_fields()
end)
end

# mutable struct AbstractNode{T<:AbstractBranch}
# @abstract_node_fields

# function AbstractNode(
# id = -1,
# parent = nothing,
# depth = 0,
# branch = nothing,
# solution_status = MOI.OPTIMIZE_NOT_CALLED,
# bound = -Inf,
# solution = Dict{Any,Real}())
# return new{T}(id, parent, depth, branch, solution_status, bound, solution)
# end
# end

# return node solution
node_solution(node::AbstractNode) = node.solution

# empty branching function
branch(node::AbstractNode)::Vector{AbstractNode} = []

Expand Down
6 changes: 3 additions & 3 deletions src/tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ mutable struct AbstractTree
node_counter::Int
nodes::Vector{AbstractNode}
processed::Vector{AbstractNode}
best_bound::Real # e.g., lower bound in minimizing MILP
best_incumbent::Real # e.g., upper bound in minimizing MILP
best_bound::Real # e.g., lower bound for minimization
best_incumbent::Real # e.g., upper bound for minimization

function AbstractTree(node_counter::Int = 0)
return new(node_counter, [], [], -Inf, Inf)
Expand Down Expand Up @@ -36,7 +36,7 @@ function push!(tree::AbstractTree, node::AbstractNode)
end

# add a set of nodes to tree
function push!(tree::AbstractTree, nodes::Vector{AbstractNode})
function push!(tree::AbstractTree, nodes::Vector{T}) where T<:AbstractNode
for node in nodes
push!(tree, node)
end
Expand Down
23 changes: 18 additions & 5 deletions test/unittest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ using MathOptInterface
const BB = BranchAndBound
const MOI = MathOptInterface

@testset "Abstract Node" begin
struct MyNode <: BB.AbstractNode end
node = MyNode()
@test BB.branch(node) == []
BB.bound!(node)
BB.heuristics!(node)
BB.apply_changes!(node)
end

model = Model(GLPK.Optimizer)
@variable(model, 0 <= x <= 1)
@objective(model, Min, x)
Expand Down Expand Up @@ -59,18 +68,22 @@ end

@testset "Marked root node as processed" begin
BB.processed!(tree, current_node)
@test isempty(tree.nodes)
@test BB.termination(tree)
@test length(tree.processed) == 1
@test tree.processed[1] == current_node
end

@testset "Empty branching" begin
BB.branch!(tree, current_node)
@test tree.node_counter == 1
@test length(tree.nodes) == 0
@test length(tree.processed) == 1
end

@testset "Branching at root" begin
child1 = BB.create_child_node_with_lb(current_node, x, 0.5)
child2 = BB.create_child_node_with_ub(current_node, x, 0.5)

BB.push!(tree, child1)
BB.push!(tree, child2)

BB.push!(tree, [child1,child2])
@test tree.node_counter == 3
@test length(tree.nodes) == 2
@test length(tree.processed) == 1
Expand Down

4 comments on commit ce82fea

@kibaekkim
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/19407

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" ce82feac33a56030d8269d073d98d46e154ccae6
git push origin v0.1.0

@kibaekkim
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/24061

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" ce82feac33a56030d8269d073d98d46e154ccae6
git push origin v0.1.0

Please sign in to comment.