Skip to content

Commit 72f4c36

Browse files
authored
rewrote constraint pvals (#283)
* rewrote constraint pvals * v0.8.1
1 parent 549f039 commit 72f4c36

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ConstrainSolver.jl - Changelog
22

3+
## v0.8.1 (5th of February 2022)
4+
- bugfix when using `CS.Integers` together with an alldifferent constraint. [PR #283](https://github.com/Wikunia/ConstraintSolver.jl/pull/283)
35
## v0.8.0 (8th of January 2022)
46
- Using [TableLogger.jl](https://github.com/Wikunia/TableLogger.jl)
57
- Only support Julia v1.6 and above

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ConstraintSolver"
22
uuid = "e0e52ebd-5523-408d-9ca3-7641f1cd1405"
33
authors = ["Ole Kröger <[email protected]>"]
4-
version = "0.8.0"
4+
version = "0.8.1"
55

66
[deps]
77
ConstraintProgrammingExtensions = "b65d079e-ed98-51d9-b0db-edee61a5c5f8"

src/ConstraintSolver.jl

+4-26
Original file line numberDiff line numberDiff line change
@@ -135,32 +135,11 @@ function set_pvals!(com::CS.CoM, constraint::Constraint)
135135
# nothing to do i.e x <= x will be x-x <= 0 => 0 <= 0 without variables
136136
length(indices) == 0 && return
137137
variables = Variable[v for v in com.search_space[indices]]
138-
pvals_intervals = Vector{NamedTuple}()
139-
push!(pvals_intervals, (from = variables[1].lower_bound, to = variables[1].upper_bound))
140-
for i in 1:length(indices)
141-
extra_from = variables[i].min
142-
extra_to = variables[i].max
143-
comp_inside = false
144-
for cpvals in pvals_intervals
145-
if extra_from >= cpvals.from && extra_to <= cpvals.to
146-
# completely inside the interval already
147-
comp_inside = true
148-
break
149-
elseif extra_from >= cpvals.from && extra_from <= cpvals.to
150-
extra_from = cpvals.to + 1
151-
elseif extra_to <= cpvals.to && extra_to >= cpvals.from
152-
extra_to = cpvals.from - 1
153-
end
154-
end
155-
if !comp_inside && extra_to >= extra_from
156-
push!(pvals_intervals, (from = extra_from, to = extra_to))
157-
end
138+
pvals_set = Set{Int}()
139+
for variable in variables
140+
union!(pvals_set, values(variable))
158141
end
159-
pvals = collect((pvals_intervals[1].from):(pvals_intervals[1].to))
160-
for interval in pvals_intervals[2:end]
161-
pvals = vcat(pvals, collect((interval.from):(interval.to)))
162-
end
163-
constraint.pvals = pvals
142+
constraint.pvals = collect(pvals_set)
164143
if constraint isa IndicatorConstraint || constraint isa ReifiedConstraint
165144
set_pvals!(com, constraint.inner_constraint)
166145
end
@@ -770,7 +749,6 @@ function solve!(com::CS.CoM)
770749
feasible = prune!(com; pre_backtrack = true, initial_check = true)
771750
# finished pruning will be called in second call a few lines down...
772751

773-
774752
if !feasible
775753
com.solve_time = time() - com.start_time
776754
return :Infeasible

test/unit/constraints/alldifferent.jl

+16-1
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,19 @@ end
235235
@constraint(model, [x[i] - i for i in 1:n] in CS.AllDifferent())
236236

237237
@test_throws DomainError optimize!(model)
238-
end
238+
end
239+
240+
@testset "CS.Integers in AllDifferent" begin
241+
m = Model(optimizer_with_attributes(CS.Optimizer, "logging"=>[]))
242+
@variable(m, 0 <= x[1:3] <= 20, Int)
243+
@constraint(m, x[1] in CS.Integers([3,5]))
244+
@constraint(m, x[2] in CS.Integers([3,5]))
245+
@constraint(m, x in CS.AllDifferent())
246+
optimize!(m)
247+
@test JuMP.termination_status(m) == MOI.OPTIMAL
248+
vals = convert.(Int, JuMP.value.(x))
249+
@test allunique(vals)
250+
@test vals[1] in [3,5]
251+
@test vals[2] in [3,5]
252+
@test vals[3] in collect(0:20)
253+
end

0 commit comments

Comments
 (0)