Skip to content
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
78 changes: 64 additions & 14 deletions src/algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,48 @@ function termination_status(model::PolicyGraph)
end

"""
relax_integrality(model::PolicyGraph)::NTuple{Vector{VariableRef}, 2}
relax_integrality(model::JuMP.Model)

Relax all binary and integer constraints in `model`.
Returns two vectors:
the first containing a list of binary variables and previous bounds,
and the second containing a list of integer variables.

See also [`enforce_integrality`](@ref).
"""
function relax_integrality(m::JuMP.Model)
# Note: if integrality restriction is added via @constraint then this loop doesn't catch it.
binaries = Tuple{JuMP.VariableRef, Float64, Float64}[]
integers = JuMP.VariableRef[]
# Run through all variables on model and unset integrality
for x in JuMP.all_variables(m)
if JuMP.is_binary(x)
x_lb, x_ub = NaN, NaN
JuMP.unset_binary(x)
# Store upper and lower bounds
if JuMP.has_lower_bound(x)
x_lb = JuMP.lower_bound(x)
JuMP.set_lower_bound(x, max(x_lb, 0.0))
else
JuMP.set_lower_bound(x, 0.0)
end
if JuMP.has_upper_bound(x)
x_ub = JuMP.upper_bound(x)
JuMP.set_upper_bound(x, min(x_ub, 1.0))
else
JuMP.set_upper_bound(x, 1.0)
end
push!(binaries, (x, x_lb, x_ub))
elseif JuMP.is_integer(x)
JuMP.unset_integer(x)
push!(integers, x)
end
end
return binaries, integers
end

"""
relax_integrality(model::PolicyGraph)

Relax all binary and integer constraints in all subproblems in `model`. Return
two vectors, the first containing a list of binary variables, and the second
Expand All @@ -695,35 +736,44 @@ containing a list of integer variables.
See also [`enforce_integrality`](@ref).
"""
function relax_integrality(model::PolicyGraph)
binaries = JuMP.VariableRef[]
binaries = Tuple{JuMP.VariableRef, Float64, Float64}[]
integers = JuMP.VariableRef[]
for (key, node) in model.nodes
for x in JuMP.all_variables(node.subproblem)
if JuMP.is_binary(x)
JuMP.unset_binary(x)
push!(binaries, x)
elseif JuMP.is_integer(x)
JuMP.unset_integer(x)
push!(integers, x)
end
end
bins, ints = relax_integrality(node.subproblem)
append!(binaries, bins)
append!(integers, ints)
end
return binaries, integers
end

"""
enforce_integrality(
binaries::Vector{VariableRef}, integers::Vector{VariableRef})
binaries::Vector{Tuple{JuMP.VariableRef, Float64, Float64}},
integers::Vector{VariableRef})

Set all variables in `binaries` to `SingleVariable-in-ZeroOne()`, and all
variables in `integers` to `SingleVariable-in-Integer()`.

See also [`relax_integrality`](@ref).
"""
function enforce_integrality(
binaries::Vector{VariableRef}, integers::Vector{VariableRef})
binaries::Vector{Tuple{JuMP.VariableRef, Float64, Float64}},
integers::Vector{VariableRef}
)
JuMP.set_integer.(integers)
JuMP.set_binary.(binaries)
for (x, x_lb, x_ub) in binaries
if isnan(x_lb)
JuMP.delete_lower_bound(x)
else
JuMP.set_lower_bound(x, x_lb)
end
if isnan(x_ub)
JuMP.delete_upper_bound(x)
else
JuMP.set_upper_bound(x, x_ub)
end
JuMP.set_binary(x)
end
return
end

Expand Down
94 changes: 94 additions & 0 deletions test/algorithm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,97 @@ end
@test pre_optimize_called == 1
@test post_optimize_called == 3
end

@testset "relax_integrality and enforce_integrality" begin
model = SDDP.LinearPolicyGraph(
stages = 2, lower_bound = 0.0,
direct_mode = false
) do sp, t
@variable(sp, x, SDDP.State, initial_value = 2.0)
@variable(sp, b1, Bin)
@variable(sp, 0.2 <= b2, Bin)
@variable(sp, 0.5 <= b3 <= 1.2, Bin)
@variable(sp, i1, Int)
@variable(sp, 6.2 >= i2, Int)
@variable(sp, -8 <= i3 <= 2, Int)
@stageobjective(sp, b1 + b2 + b2 + i3 + i1)
end

for node in [model[1], model[2]]
@test JuMP.is_binary(node.subproblem[:b1])
@test !JuMP.has_lower_bound(node.subproblem[:b1])
@test !JuMP.has_upper_bound(node.subproblem[:b1])

@test JuMP.is_binary(node.subproblem[:b2])
@test JuMP.lower_bound(node.subproblem[:b2]) == 0.2
@test !JuMP.has_upper_bound(node.subproblem[:b2])

@test JuMP.is_binary(node.subproblem[:b3])
@test JuMP.lower_bound(node.subproblem[:b3]) == 0.5
@test JuMP.upper_bound(node.subproblem[:b3]) == 1.2

@test JuMP.is_integer(node.subproblem[:i1])
@test !JuMP.has_lower_bound(node.subproblem[:i1])
@test !JuMP.has_upper_bound(node.subproblem[:i1])

@test JuMP.is_integer(node.subproblem[:i2])
@test JuMP.upper_bound(node.subproblem[:i2]) == 6.2
@test !JuMP.has_lower_bound(node.subproblem[:i2])

@test JuMP.is_integer(node.subproblem[:i3])
@test JuMP.lower_bound(node.subproblem[:i3]) == -8
@test JuMP.upper_bound(node.subproblem[:i3]) == 2
end
binaries, integers = SDDP.relax_integrality(model)
for node in [model[1], model[2]]
@test !JuMP.is_binary(node.subproblem[:b1])
@test JuMP.lower_bound(node.subproblem[:b1]) == 0.0
@test JuMP.upper_bound(node.subproblem[:b1]) == 1.0

@test !JuMP.is_binary(node.subproblem[:b2])
@test JuMP.lower_bound(node.subproblem[:b2]) == 0.2
@test JuMP.upper_bound(node.subproblem[:b2]) == 1.0

@test !JuMP.is_binary(node.subproblem[:b3])
@test JuMP.lower_bound(node.subproblem[:b3]) == 0.5
@test JuMP.upper_bound(node.subproblem[:b3]) == 1.0

@test !JuMP.is_integer(node.subproblem[:i1])
@test !JuMP.has_lower_bound(node.subproblem[:i1])
@test !JuMP.has_upper_bound(node.subproblem[:i1])

@test !JuMP.is_integer(node.subproblem[:i2])
@test JuMP.upper_bound(node.subproblem[:i2]) == 6.2
@test !JuMP.has_lower_bound(node.subproblem[:i2])

@test !JuMP.is_integer(node.subproblem[:i3])
@test JuMP.lower_bound(node.subproblem[:i3]) == -8
@test JuMP.upper_bound(node.subproblem[:i3]) == 2
end
SDDP.enforce_integrality(binaries, integers)
for node in [model[1], model[2]]
@test JuMP.is_binary(node.subproblem[:b1])
@test !JuMP.has_lower_bound(node.subproblem[:b1])
@test !JuMP.has_upper_bound(node.subproblem[:b1])

@test JuMP.is_binary(node.subproblem[:b2])
@test JuMP.lower_bound(node.subproblem[:b2]) == 0.2
@test !JuMP.has_upper_bound(node.subproblem[:b2])

@test JuMP.is_binary(node.subproblem[:b3])
@test JuMP.lower_bound(node.subproblem[:b3]) == 0.5
@test JuMP.upper_bound(node.subproblem[:b3]) == 1.2

@test JuMP.is_integer(node.subproblem[:i1])
@test !JuMP.has_lower_bound(node.subproblem[:i1])
@test !JuMP.has_upper_bound(node.subproblem[:i1])

@test JuMP.is_integer(node.subproblem[:i2])
@test JuMP.upper_bound(node.subproblem[:i2]) == 6.2
@test !JuMP.has_lower_bound(node.subproblem[:i2])

@test JuMP.is_integer(node.subproblem[:i3])
@test JuMP.lower_bound(node.subproblem[:i3]) == -8
@test JuMP.upper_bound(node.subproblem[:i3]) == 2
end
end