This repository was archived by the owner on Mar 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmap_coloring.py
More file actions
122 lines (102 loc) · 3.67 KB
/
map_coloring.py
File metadata and controls
122 lines (102 loc) · 3.67 KB
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
# Copyright 2019 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import dwavebinarycsp
from hybrid.reference.kerberos import KerberosSampler
from utilities import visualize_map
np.set_printoptions(legacy='1.25')
class Province:
def __init__(self, name):
self.name = name
self.red = name + "_r"
self.green = name + "_g"
self.blue = name + "_b"
self.yellow = name + "_y"
# Set up provinces
bc = Province("bc") # British Columbia
ab = Province("ab") # Alberta
sk = Province("sk") # Saskatchewan
mb = Province("mb") # Manitoba
on = Province("on") # Ontario
qc = Province("qc") # Quebec
nl = Province("nl") # Newfoundland and Labrador
nb = Province("nb") # New Brunswick
pe = Province("pe") # Prince Edward Island
ns = Province("ns") # Nova Scotia
yt = Province("yt") # Yukon
nt = Province("nt") # Northwest Territories
nu = Province("nu") # Nunavut
provinces = [bc, ab, sk, mb, on, qc, nl, nb, pe, ns, yt, nt, nu]
# Set up province neighbours (i.e. shares a border)
neighbours = [(bc, ab),
(bc, nt),
(bc, yt),
(ab, sk),
(ab, nt),
(sk, mb),
(sk, nt),
(mb, on),
(mb, nu),
(on, qc),
(qc, nb),
(qc, nl),
(nb, ns),
(yt, nt),
(nt, nu)]
# Initialize constraint satisfaction problem
csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.BINARY)
not_both = {(0, 1), (1, 0), (0, 0)}
select_one = {(0, 0, 0, 1),
(0, 0, 1, 0),
(0, 1, 0, 0),
(1, 0, 0, 0)}
# Apply one color constraint
for p in provinces:
csp.add_constraint(select_one, {p.red, p.green, p.blue, p.yellow})
# Apply no color sharing between neighbours
for x, y in neighbours:
csp.add_constraint(not_both, {x.red, y.red})
csp.add_constraint(not_both, {x.green, y.green})
csp.add_constraint(not_both, {x.blue, y.blue})
csp.add_constraint(not_both, {x.yellow, y.yellow})
# Combine constraints to form a BQM
bqm = dwavebinarycsp.stitch(csp)
# Solve BQM
solution = KerberosSampler().sample(bqm,
qpu_params={'label': 'Example - Map Coloring'})
best_solution = solution.first.sample
print("Solution: ", best_solution)
# Verify
is_correct = csp.check(best_solution)
print("Does solution satisfy our constraints? {}".format(is_correct))
# Visualize the solution
# Note: The following is purely for visualizing the output and is not necessary
# for the demo.
# Hard code node positions to be reminiscent of the map of Canada
node_positions = {"bc": (0, 1),
"ab": (2, 1),
"sk": (4, 1),
"mb": (6, 1),
"on": (8, 1),
"qc": (10, 1),
"nb": (10, 0),
"ns": (12, 0),
"pe": (12, 1),
"nl": (12, 2),
"yt": (0, 3),
"nt": (2, 3),
"nu": (6, 3)}
nodes = [u.name for u in provinces]
edges = [(u.name, v.name) for u, v in neighbours]
visualize_map(nodes, edges, best_solution, node_positions=node_positions)