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

add removeBetween function to manipulate IntDomain (#430) #431

Closed
wants to merge 2 commits into from
Closed
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: 1 addition & 1 deletion src/CP/constraints/sumlessthan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct SumLessThan <: Constraint
numberOfFreeVars ::StateObject{Int}
sumOfFixedVars ::StateObject{Int}
freeIds ::Array{Int}
function SumLessThan(x::Array{<:AbstractIntVar}, upper::Int, trailer)
function SumLessThan(x::Array{<:AbstractIntVar}, upper::Int, trailer)
@assert !isempty(x)

freeIds = zeros(length(x))
Expand Down
20 changes: 20 additions & 0 deletions src/CP/variables/IntDomain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ function removeBelow!(dom::IntDomain, value::Int)
return pruned
end

"""
removeBetween!(dom::IntDomain, min::Int, max::Int)

Remove every integer of `dom` that is *strictly* between `min` and `max`. Return the pruned values.
"""
function removeBetween!(dom::IntDomain, min::Int, max::Int)
if dom.max.value < max && dom.min.value > min
return removeAll!(dom)
end

pruned = Int[]
for i in min+1:(max-1)
if i in dom
remove!(dom, i)
push!(pruned, i)
end
end
return pruned
end

"""
assign!(dom::IntDomain, value::Int)

Expand Down
9 changes: 9 additions & 0 deletions test/CP/variables/IntDomain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@
@test removed == [11, 12, 13]
end

@testset "removeBetween!()" begin
trailer = SeaPearl.Trailer()
dom = SeaPearl.IntDomain(trailer, 5, 10)

removed = SeaPearl.removeBetween!(dom, 11, 14)

@test removed == [12, 13]
end

@testset "updateMinFromRemovedVal!()" begin
trailer = SeaPearl.Trailer()
x = SeaPearl.IntVar(5, 10, "x", trailer)
Expand Down
Loading