From 4e36204dadeec6f478fda183ecedd064f0f1f64f Mon Sep 17 00:00:00 2001 From: Max Bourgeat <72439470+Max9294D@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:00:15 -0400 Subject: [PATCH 1/2] add removeBetween function to manipulate IntDomain (#430) --- src/CP/constraints/sumlessthan.jl | 2 +- src/CP/variables/IntDomain.jl | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/CP/constraints/sumlessthan.jl b/src/CP/constraints/sumlessthan.jl index 0d14755e1..29b3624e7 100644 --- a/src/CP/constraints/sumlessthan.jl +++ b/src/CP/constraints/sumlessthan.jl @@ -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)) diff --git a/src/CP/variables/IntDomain.jl b/src/CP/variables/IntDomain.jl index 668ab01a2..43d029f8d 100644 --- a/src/CP/variables/IntDomain.jl +++ b/src/CP/variables/IntDomain.jl @@ -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) From 54fc01b8186c5c8f554739f8d9a7a3de9a42e3bb Mon Sep 17 00:00:00 2001 From: Max Bourgeat <72439470+Max9294D@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:37:23 -0400 Subject: [PATCH 2/2] Remove between (#432) * add removeBetween function to manipulate IntDomain * Add test for removeBetween --- test/CP/variables/IntDomain.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/CP/variables/IntDomain.jl b/test/CP/variables/IntDomain.jl index 6e5c0b8a5..314f98c86 100644 --- a/test/CP/variables/IntDomain.jl +++ b/test/CP/variables/IntDomain.jl @@ -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)