/
constraints.jl
121 lines (107 loc) · 3.96 KB
/
constraints.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
abstract type AbstractConstraint end
struct PopulationConstraint <: AbstractConstraint
min_pop::Int
max_pop::Int
end
"""
ContiguityConstraint()
Initializes and returns a `ContiguityConstraint` object.
"""
struct ContiguityConstraint <: AbstractConstraint
# No metadata (for now); implements the `AbstractConstraint` interface.
end
"""
PopulationConstraint(graph::BaseGraph,
partition::Partition,
tolerance::Float64)::PopulationConstraint
Initializes a `PopulationConstraint` that stores the minimum and maximum populations
a district in a `partition` could have within `tolerance`.
*Returns* the PopulationConstraint object.
"""
function PopulationConstraint(graph::BaseGraph,
partition::Partition,
tolerance::Float64)::PopulationConstraint
ideal_pop = graph.total_pop / partition.num_dists
# no particular reason to not use floor() instead of ceil()
min_pop = Int(ceil((1-tolerance) * ideal_pop))
max_pop = Int(floor((1+tolerance) * ideal_pop))
return PopulationConstraint(min_pop, max_pop)
end
"""
satisfy_constraint(constraint::PopulationConstraint,
proposal::RecomProposal)
Test whether a RecomProposal satisfies a population constraint.
"""
function satisfy_constraint(constraint::PopulationConstraint,
proposal::RecomProposal)
if proposal.D₁_pop >= constraint.min_pop && proposal.D₁_pop <= constraint.max_pop
if proposal.D₂_pop >= constraint.min_pop && proposal.D₂_pop <= constraint.max_pop
return true
end
end
return false
end
"""
satisfy_constraint(constraint::PopulationConstraint,
D₁_pop::Int,
D₂_pop::Int)
Test whether two population counts satisfy a PopulationConstraint.
"""
function satisfy_constraint(constraint::PopulationConstraint,
D₁_pop::Int,
D₂_pop::Int)
if D₁_pop >= constraint.min_pop && D₁_pop <= constraint.max_pop
if D₂_pop >= constraint.min_pop && D₂_pop <= constraint.max_pop
return true
end
end
return false
end
"""
satisfy_constraint(constraint::ContiguityConstraint,
graph::BaseGraph,
partition::Partition,
flip::FlipProposal)
Test whether a FlipProposal satisfies the contiguity constraint.
Based on Parker's implementation on GitHub, located in the Flips.jl
repository at src/constraints.jl.
"""
function satisfy_constraint(constraint::ContiguityConstraint,
graph::BaseGraph,
partition::Partition,
flip::FlipProposal)
# get node's neighbors who were in its old district
neighbors = [n for n in graph.neighbors[flip.node]
if partition.assignments[n] == flip.D₁]
if isempty(neighbors) # this is the only node of this district left!
return false
end
source_node = pop!(neighbors)
# DFS search to verify contiguity is not broken
@inbounds for target_node in neighbors
visited = zeros(Bool, graph.num_nodes)
queue = Queue{Int}(64) # TODO: auto-tune?
enqueue!(queue, target_node)
visited[target_node] = true
found = false
while !isempty(queue)
curr_node = dequeue!(queue)
if curr_node == source_node
found = true
break
end
for neighbor in graph.neighbors[curr_node]
if (!visited[neighbor] &&
partition.assignments[neighbor] == flip.D₁ &&
neighbor != flip.node)
visited[neighbor] = true
enqueue!(queue, neighbor)
end
end
end
if (isempty(queue) && !found)
return false
end
end
return true
end