我有一些代码可以解决图形着色问题(广泛定义为向无向图分配“颜色”的问题,确保没有两个顶点连接的顶点具有相同的颜色)。我正在尝试使用约束传播来实现解决方案,以提高标准递归回溯算法的效率,但遇到以下错误:
File "C:\Users\danisg\Desktop\coloring\Solver.py",
line 99, in solve
for color in self.domains[var]:
RuntimeError: Set changed size during iteration
这里,对于每个顶点,我为该特定顶点保留set
个可能的特定值:
self.domains = { var: set(self.colors) for var in self.vars }
在进行分配后,我将此约束传播到相邻域,以限制搜索空间:
for key in node.neighbors: # list of keys corresponding to adjacent vertices
if color in self.domains[key]: # remove now to prune possible choices
self.domains[key].remove(color)
这不是引发实际错误的地方(在我的代码中,我指出问题在try-except
块中的位置),但可能是问题的根源。
我是否有正确的想法,如果不是正确的实施?更重要的是,我该如何解决这个问题?另外,是否有必要保留单独的domains
字典?或者我们可以使domain
图中每个节点的属性?
以下是调用此代码的solve
函数:
def solve(self):
uncolored = [var for var in self.vars if self.map[var].color == None]
if len(uncolored) == 0:
return True
var = min(uncolored, key = lambda x: len(self.domains[var]))
node = self.map[var]
old = { var: set(self.domains[var]) for var in self.vars }
for color in self.domains[var]:
if not self._valid(var, color):
continue
self.map[var].color = color
for key in node.neighbors:
if color in self.domains[key]:
self.domains[key].remove(color)
try:
if self.solve():
return True
except:
print('happening now')
self.map[var].color = None
self.domains = old
return False
我的实现使用Node
对象:
class Solver:
class Node:
def __init__(self, var, neighbors, color = None, domain = set()):
self.var = var
self.neighbors = neighbors
self.color = color
self.domain = domain
def __str__(self):
return str((self.var, self.color))
def __init__(self, graph, K):
self.vars = sorted( graph.keys(), key = lambda x: len(graph[x]), reverse = True ) # sort by number of links; start with most constrained
self.colors = range(K)
self.map = { var: self.Node(var, graph[var]) for var in self.vars }
self.domains = { var: set(self.colors) for var in self.vars }
以下是另外两个使用/有用的功能:
def validate(self):
for var in self.vars:
node = self.map[var]
for key in node.neighbors:
if node.color == self.map[key].color:
return False
return True
def _valid(self, var, color):
node = self.map[var]
for key in node.neighbors:
if self.map[key].color == None:
continue
if self.map[key].color == color:
return False
return True
我正在使用的示例图表here。
读取数据的功能:
def read_and_make_graph(input_data):
lines = input_data.split('\n')
first_line = lines[0].split()
node_count = int(first_line[0])
edge_count = int(first_line[1])
graph = {}
for i in range(1, edge_count + 1):
line = lines[i]
parts = line.split()
node, edge = int(parts[0]), int(parts[1])
if node in graph:
graph[node].add(edge)
if edge in graph:
graph[edge].add(node)
if node not in graph:
graph[node] = {edge}
if edge not in graph:
graph[edge] = {node}
return graph
应该按如下方式调用:
file_location = 'C:\\Users\\danisg\\Desktop\\coloring\\data\\gc_50_3'
input_data_file = open(file_location, 'r')
input_data = ''.join(input_data_file.readlines())
input_data_file.close()
graph = read_and_make_graph(input_data)
solver = Solver(graph, 6) # a 6 coloring IS possible
print(solver.solve()) # True if we solved; False if we didn't
答案 0 :(得分:36)
我认为问题在于:
for color in self.domains[var]:
if not self._valid(var, color):
continue
self.map[var].color = color
for key in node.neighbors:
if color in self.domains[key]:
self.domains[key].remove(color) # This is potentially bad.
如果调用key == var
时self.domains[key].remove(color)
,则更改当前正在迭代的集合的大小。您可以使用
for color in self.domains[var].copy():
使用copy()将允许您迭代集合的副本,同时从原始项目中删除项目。