-
Notifications
You must be signed in to change notification settings - Fork 10
/
alldifferent.jl
349 lines (303 loc) · 11.8 KB
/
alldifferent.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
include("algorithms/matching.jl")
@enum State begin
used = 1
unused = 2
removed = 3
vital = 4
end
"""
AllDifferent(x::Array{<:AbstractIntVar}, trailer::SeaPearl.Trailer)
AllDifferent constraint, enforcing ∀ i ≠ j ∈ ⟦1, length(x)⟧, x[i] ≠ x[j].
The implementation of this contraint is inspired by:
https://www.researchgate.net/publication/200034395_A_Filtering_Algorithm_for_Constraints_of_Difference_in_CSPs
Many of the functions below relate to algorithms depicted in the paper, and their
documentation refer to parts of the overall algorithm.
"""
struct AllDifferent <: Constraint
x::Array{<:AbstractIntVar}
active::StateObject{Bool}
initialized::StateObject{Bool}
matching::Vector{StateObject{Tuple{Int, Int, Bool}}}
remainingEdges::RSparseBitSet{UInt64}
edgeToIndex::Dict{LightGraphs.Edge{Int}, Int}
indexToEdge::Vector{LightGraphs.Edge{Int}}
nodesMin::Int
numberOfVars::Int
numberOfVals::Int
numberOfEdges::Int
function AllDifferent(x::Array{<:AbstractIntVar}, trailer)::AllDifferent
max = Base.maximum(var -> maximum(var.domain), x)
min = Base.minimum(var -> minimum(var.domain), x)
range = max - min + 1
active = StateObject{Bool}(true, trailer)
initialized = StateObject{Bool}(false, trailer)
numberOfVars = length(x)
numberOfEdges = sum(var -> length(var.domain), x)
matching = Vector{StateObject{Tuple{Int, Int, Bool}}}(undef, numberOfVars)
for i = 1:numberOfVars
matching[i] = StateObject{Tuple{Int, Int, Bool}}((0, 0, false), trailer)
end
remainingEdges = RSparseBitSet{UInt64}(numberOfEdges, trailer)
edgeToIndex = Dict{LightGraphs.Edge{Int}, Int}()
indexToEdge = Vector{LightGraphs.Edge{Int}}(undef, numberOfEdges)
constraint = new(x,
active,
initialized,
matching,
remainingEdges,
edgeToIndex,
indexToEdge,
min,
numberOfVars,
range,
numberOfEdges
)
counter = 1
for (idx, var) in enumerate(x)
addOnDomainChange!(var, constraint)
for val in var.domain
dst = numberOfVars + val - min + 1
constraint.edgeToIndex[LightGraphs.Edge(idx, dst)] = counter
constraint.indexToEdge[counter] = LightGraphs.Edge(idx, dst)
counter += 1
end
end
return constraint
end
end
"""
valToNode(constraint, value)::Int
Return the node index of a value.
"""
function valToNode(con::AllDifferent, val::Int)
return con.numberOfVars + val - con.nodesMin + 1
end
"""
nodeToVal(constraint, node)::Int
Return the underlying value of a node.
"""
function nodeToVal(con::AllDifferent, node::Int)
return node - con.numberOfVars + con.nodesMin - 1
end
"""
orderEdge(edge)::LightGraphs.Edge
Return the ordered version of an edge, i.e. with e.src ≤ e.dst.
"""
function orderEdge(e::LightGraphs.Edge{Int})::LightGraphs.Edge{Int}
src, dst = e.src < e.dst ? (e.src, e.dst) : (e.dst, e.src)
return LightGraphs.Edge(src, dst)
end
function updateremaining!(constraint::AllDifferent, removed::BitVector)
clearMask!(constraint.remainingEdges)
addToMask!(constraint.remainingEdges, bitVectorToUInt64Vector(removed))
reverseMask!(constraint.remainingEdges)
intersectWithMask!(constraint.remainingEdges)
end
function updatevital!(constraint::AllDifferent, vital::BitVector)
for match in constraint.matching
e = LightGraphs.Edge(match.value[1], match.value[2])
idx = constraint.edgeToIndex[e]
setValue!(match, (e.src, e.dst, vital[idx]))
end
end
function getvital(constraint)
vital = BitVector(undef, constraint.numberOfEdges) .= false
for match in constraint.matching
if match.value[3]
vital[constraint.edgeToIndex[LightGraphs.Edge(match.value[1], match.value[2])]] = true
end
end
return vital
end
"""
initializeGraphs!(constraint)
Return the graph and the empty directed graph of a variable-value problem.
"""
function initializeGraphs!(con::AllDifferent)::Pair{LightGraphs.Graph{Int}, LightGraphs.DiGraph{Int}}
numberOfNodes = con.numberOfVars + con.numberOfVals
edgeFilter = BitVector(con.remainingEdges)[1:con.numberOfEdges]
allEdges = con.indexToEdge[edgeFilter]
graph = LightGraphs.Graph(allEdges)
digraph = LightGraphs.DiGraph(LightGraphs.nv(graph))
if LightGraphs.nv(graph) < numberOfNodes
LightGraphs.add_vertices!(graph, numberOfNodes - LightGraphs.nv(graph))
end
return Pair(graph, digraph)
end
"""
getAllEdges(digraph, parents)::Set{LightGraphs.Edge}
Return all the edges visited by a BFS on `digraph` encoded in `parents`.
"""
function getAllEdges(digraph::LightGraphs.DiGraph{Int}, parents::Vector{Int})::Set{LightGraphs.Edge{Int}}
edgeSet = Set{LightGraphs.Edge{Int}}()
for i = 1:LightGraphs.nv(digraph)
if parents[i] > 0 && parents[i] != i
validneighbors = filter(v -> parents[v] > 0, LightGraphs.inneighbors(digraph, i))
validedges = map(v -> orderEdge(LightGraphs.Edge(v, i)), validneighbors)
union!(edgeSet, validedges)
end
end
return edgeSet
end
remapedge(edge::LightGraphs.Edge{Int}, component::Vector{Int}) = LightGraphs.Edge(component[edge.src], component[edge.dst])
"""
removeEdges!(constraint, prunedValue, graph, digraph)
Remove all the unnecessary edges in graph and digraph as in the original paper.
Update `constraint.edgesState` with the new status of each edge, remove some
edges from `graph` and `digraph` and push the removed values in `prunedValue`.
Following exactly the procedure in the function with the same name in the original
paper.
"""
function removeEdges!(constraint::AllDifferent, prunedValues::Vector{Vector{Int}}, graph::LightGraphs.Graph{Int}, digraph::LightGraphs.DiGraph{Int})
unused = BitVector(constraint.remainingEdges)[1:constraint.numberOfEdges]
vital = BitVector(undef, constraint.numberOfEdges) .= false
removed = BitVector(undef, constraint.numberOfEdges) .= .~ unused
used = BitVector(undef, constraint.numberOfEdges) .= false
allValues = constraint.numberOfVars+1:LightGraphs.nv(digraph)
freeValues = filter(v -> LightGraphs.indegree(digraph,v) == 0, allValues)
seen = fill(false, constraint.numberOfVals)
components = filter(comp -> length(comp)>1, LightGraphs.strongly_connected_components(digraph))
for component in components
edgeSet = orderEdge.(remapedge.(LightGraphs.edges(digraph[component]), [component]))
edgeIndices = getindex.([constraint.edgeToIndex], edgeSet)
used[edgeIndices] .= true
unused[edgeIndices] .= false
end
for node in freeValues
if seen[node - constraint.numberOfVars]
continue
end
parents = LightGraphs.bfs_parents(digraph, node; dir=:out)
edgeSet = getAllEdges(digraph, parents)
edgeIndices = getindex.([constraint.edgeToIndex], edgeSet)
used[edgeIndices] .= true
unused[edgeIndices] .= false
reached = filter(v -> parents[v] > 0, allValues)
seen[reached .- constraint.numberOfVars] .= true
end
edgeIndices = map(constraint.matching) do pair
var, val = pair.value
e = LightGraphs.Edge(var, val)
return constraint.edgeToIndex[e]
end
vital[edgeIndices] .= true .& unused[edgeIndices]
unused[edgeIndices] .= false
rest = constraint.indexToEdge[unused]
reversedRest = map(e -> LightGraphs.Edge(e.dst, e.src), rest)
LightGraphs.rem_edge!.([graph], rest)
LightGraphs.rem_edge!.([digraph], reversedRest)
removed[unused] .= true
foreach(rest) do e
var, val = e.src, e.dst
push!(prunedValues[var], nodeToVal(constraint, val))
end
updateremaining!(constraint, removed)
updatevital!(constraint, vital)
end
"""
updateEdgesState!(constraint)::Set{LightGraphs.Edge}
Return all the pruned values not already encoded in the constraint state.
"""
function updateEdgesState!(constraint::AllDifferent, prunedDomains::CPModification)
modif = Set{LightGraphs.Edge}()
for (idx, var) in enumerate(constraint.x)
if haskey(prunedDomains, var.id)
union!(modif, LightGraphs.Edge.([idx], valToNode.([constraint], prunedDomains[var.id])))
end
end
return modif
end
"""
propagate!(constraint::AllDifferent, toPropagate::Set{Constraint}, prunedDomains::CPModification)
`AllDifferent` propagation function. Implement the full procedure of the paper.
"""
function propagate!(constraint::AllDifferent, toPropagate::Set{Constraint}, prunedDomains::CPModification)
if !constraint.active.value
return true
end
# Variables Initialization
graph, digraph = initializeGraphs!(constraint)
# Run only once, when constraint is first propagated
if !constraint.initialized.value
matching = maximumMatching!(graph, digraph, constraint.numberOfVars)
if matching.size < constraint.numberOfVars
return false
end
for (idx, match) in enumerate(matching.matches)
setValue!(constraint.matching[idx], (match..., false))
end
setValue!(constraint.initialized, true)
# Otherwise just read the stored values
else
matching = Matching{Int}(length(constraint.matching), map(match -> (match.value[1] => match.value[2]), constraint.matching))
buildDigraph!(digraph, graph, matching)
end
# TODO change this with the CPModification
modifications = updateEdgesState!(constraint, prunedDomains)
prunedValues = Vector{Vector{Int}}(undef, constraint.numberOfVars)
for i = 1:constraint.numberOfVars
prunedValues[i] = Int[]
end
removed = .~ BitVector(constraint.remainingEdges)[1:constraint.numberOfEdges]
vital = getvital(constraint)
needrematching = false
for e in modifications
rev_e = LightGraphs.Edge(e.dst, e.src)
idx = constraint.edgeToIndex[e]
if e in LightGraphs.edges(graph)
if vital[idx]
return false
elseif e in LightGraphs.edges(digraph)
needrematching = true
LightGraphs.rem_edge!(digraph, e)
else
LightGraphs.rem_edge!(digraph, rev_e)
end
LightGraphs.rem_edge!(graph, e)
# TODO get rid of edgesState
removed[idx] = true
end
end
updateremaining!(constraint, removed)
if needrematching
matching = maximizeMatching!(digraph, constraint.numberOfVars)
if matching.size < constraint.numberOfVars
return false
end
for (idx, match) in enumerate(matching.matches)
setValue!(constraint.matching[idx], (match..., false))
end
end
removeEdges!(constraint, prunedValues, graph, digraph)
for (prunedVar, var) in zip(prunedValues, constraint.x)
if !isempty(prunedVar)
for val in prunedVar
remove!(var.domain, val)
end
triggerDomainChange!(toPropagate, var)
addToPrunedDomains!(prunedDomains, var, prunedVar)
end
end
if constraint in toPropagate
pop!(toPropagate, constraint)
end
if all(var -> length(var.domain) <= 1, constraint.x)
setValue!(constraint.active, false)
end
for var in constraint.x
if isempty(var.domain)
return false
end
end
return true
end
variablesArray(constraint::AllDifferent) = constraint.x
function Base.show(io::IO, ::MIME"text/plain", con::AllDifferent)
print(io, string(typeof(con)), ": ", join([var.id for var in con.x], " != "), ", active = ", con.active)
for var in con.x
print(io, "\n ", var)
end
end
function Base.show(io::IO, con::AllDifferent)
print(io, string(typeof(con)), ": ", join([var.id for var in con.x], " != "))
end