Traceback (most recent call last):
File "F:\code\Pycharm\pythonProject1\main.py", line 107, in
box_sizes, num_boxes = bc.box_covering_algorithm(tree)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\code\Pycharm\pythonProject1\BoxCovering.py", line 111, in box_covering_algorithm
mass = len([n for n in nx.single_source_dijkstra_path_length(G, node, box_size - 1, weight='weight') if n in uncovered_nodes])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\decorators.py", line 788, in func
return argmap._lazy_compile(__wrapper)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<class 'networkx.utils.decorators.argmap'> compilation 38", line 3, in argmap_single_source_dijkstra_path_length_35
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\backends.py", line 967, in call
return self.orig_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 389, in single_source_dijkstra_path_length
return multi_source_dijkstra_path_length(G, {source}, cutoff=cutoff, weight=weight)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\decorators.py", line 788, in func
return argmap._lazy_compile(__wrapper)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<class 'networkx.utils.decorators.argmap'> compilation 42", line 3, in argmap_multi_source_dijkstra_path_length_39
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\backends.py", line 967, in call
return self.orig_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 647, in multi_source_dijkstra_path_length
return _dijkstra_multisource(G, sources, weight, cutoff=cutoff)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 863, in _dijkstra_multisource
if vu_dist > cutoff:
^^^^^^^^^^^^^^^^
TypeError: argument must be an integer
这个报错是什么意思,下面是代码部分
def box_covering_algorithm(G):
box_sizes = np.arange(1, 30) # 设定盒子大小范围
num_boxes = []
code
for box_size in box_sizes:
uncovered_nodes = set(G.nodes())
centers = []
boxes = []
while uncovered_nodes:
max_mass = 0
max_mass_node = None
for node in uncovered_nodes:
mass = len([n for n in nx.single_source_dijkstra_path_length(G, node, box_size - 1, weight='weight') if n in uncovered_nodes])
if mass > max_mass:
max_mass = mass
max_mass_node = node
centers.append(max_mass_node)
box = set(nx.single_source_dijkstra_path_length(G, max_mass_node, (box_size - 1) // 2, weight='weight'))
boxes.append(box)
uncovered_nodes -= box
num_boxes.append(len(boxes))
return box_sizes, num_boxes
Assistant
你遇到的这个错误提示说明,你在调用 networkx 的有关函数时,传递了一个不符合预期类型的参数。具体来说,这个 TypeError: argument must be an integer 的错误显示某个参数必须是一个整数,但是程序传递给它的并不是。
cutoff = box_size - 1
if not isinstance(cutoff, int) or cutoff < 0:
continue # 跳过不合适的 cutoff
mass = len([n for n in nx.single_source_dijkstra_path_length(G, node, cutoff, weight='weight') if n in uncovered_nodes])
for box_size in box_sizes:
uncovered_nodes = set(G.nodes())
centers = []
boxes = []
while uncovered_nodes:
max_mass = 0
max_mass_node = None
for node in uncovered_nodes:
mass = len([n for n in nx.single_source_dijkstra_path_length(G, node, box_size - 1, weight='weight') if n in uncovered_nodes])
if mass > max_mass:
max_mass = mass
max_mass_node = node
centers.append(max_mass_node)
box = set(nx.single_source_dijkstra_path_length(G, max_mass_node, (box_size - 1) // 2, weight='weight'))
boxes.append(box)
uncovered_nodes -= box
num_boxes.append(len(boxes))
return box_sizes, num_boxes
在上述代码中还是报错,但是我将box_size - 1打印出来为1,也不是0也不是浮点数,这是怎么回事?
Traceback (most recent call last):
File "F:\code\Pycharm\pythonProject1\main.py", line 107, in
box_sizes, num_boxes = bc.box_covering_algorithm(tree)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\code\Pycharm\pythonProject1\BoxCovering.py", line 111, in box_covering_algorithm
mass = len([n for n in nx.single_source_dijkstra_path_length(G, node, box_size - 1, weight='weight') if n in uncovered_nodes])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\decorators.py", line 788, in func
return argmap._lazy_compile(__wrapper)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<class 'networkx.utils.decorators.argmap'> compilation 38", line 3, in argmap_single_source_dijkstra_path_length_35
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\backends.py", line 967, in call
return self.orig_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 389, in single_source_dijkstra_path_length
return multi_source_dijkstra_path_length(G, {source}, cutoff=cutoff, weight=weight)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\decorators.py", line 788, in func
return argmap._lazy_compile(__wrapper)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<class 'networkx.utils.decorators.argmap'> compilation 42", line 3, in argmap_multi_source_dijkstra_path_length_39
File "D:\program\Python3.12\Lib\site-packages\networkx\utils\backends.py", line 967, in call
return self.orig_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 647, in multi_source_dijkstra_path_length
return _dijkstra_multisource(G, sources, weight, cutoff=cutoff)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\program\Python3.12\Lib\site-packages\networkx\algorithms\shortest_paths\weighted.py", line 863, in _dijkstra_multisource
if vu_dist > cutoff:
^^^^^^^^^^^^^^^^
TypeError: argument must be an integer
Assistant
问题出现的原因通常与传递给 nx.single_source_dijkstra_path_length 函数的参数有关。在这种情况下,尽管你打印出来的 box_size - 1 的值为 1,但在函数内部仍然可能因为其他参数的问题导致 TypeError: argument must be an integer 这个错误。
可能的原因和解决方法
检查输入图的节点和边的权重
确保图 G 中的边权重都是整数。如果图中的某些边有非整数的权重(例如浮点数或其它类型),这可能会干扰 Dijkstra 算法的运行。你可以用如下代码检查所有边的权重:
python
for u, v, wt in G.edges(data=True):
print(f"Edge from {u} to {v} has weight: {wt['weight']}")
for box_size in box_sizes:
uncovered_nodes = set(G.nodes())
centers = []
boxes = []
while uncovered_nodes:
max_mass = 0
max_mass_node = None
for node in uncovered_nodes:
# 使用自定义 Dijkstra 算法计算路径长度
path_lengths = custom_dijkstra(G, node, box_size - 1)
mass = len([n for n in path_lengths if n in uncovered_nodes])
if mass > max_mass:
max_mass = mass
max_mass_node = node
if max_mass_node is None:
break # 如果找不到中心节点,退出循环
centers.append(max_mass_node)
# 计算当前节点的箱子
box = set(n for n, d in path_lengths.items() if d <= (box_size - 1) // 2)
boxes.append(box)
uncovered_nodes -= box
num_boxes.append(len(boxes))
return box_sizes, num_boxes
for box_size in box_sizes:
uncovered_nodes = set(G.nodes())
centers = []
boxes = []
while uncovered_nodes:
max_mass = 0
max_mass_node = None
for node in uncovered_nodes:
# 使用自定义 Dijkstra 算法计算路径长度
path_lengths = custom_dijkstra(G, node, box_size - 1)
mass = len([n for n in path_lengths if n in uncovered_nodes])
if mass > max_mass:
max_mass = mass
max_mass_node = node
if max_mass_node is None:
break # 如果找不到中心节点,退出循环
centers.append(max_mass_node)
# 计算当前节点的箱子
box = set(n for n, d in path_lengths.items() if d <= (box_size - 1) // 2)
boxes.append(box)
uncovered_nodes -= box
num_boxes.append(len(boxes))
return box_sizes, num_boxes
在上述代码的执行中有如下报错怎么办?
Traceback (most recent call last):
File "F:\code\Pycharm\pythonProject1\main.py", line 107, in
box_sizes, num_boxes = bc.box_covering_algorithm2(tree)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\code\Pycharm\pythonProject1\BoxCovering.py", line 163, in box_covering_algorithm2
path_lengths = custom_dijkstra(G, node, box_size - 1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "F:\code\Pycharm\pythonProject1\BoxCovering.py", line 132, in custom_dijkstra
if current_distance > cutoff:
^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument must be an integer
Assistant
出现 TypeError: argument must be an integer 错误的原因是你在进行比较时,某个参数的类型不符合预期。即预期接收的参数应该是整数,但实际上获得的是其他类型(如浮点数或其他非整数字符串)。
def box_covering_algorithm2(G):
box_sizes = np.arange(2, 30) # 设定盒子大小范围
num_boxes = []
for box_size in box_sizes:
uncovered_nodes = set(G.nodes())
centers = []
boxes = []
while uncovered_nodes:
max_mass = 0
max_mass_node = None
cutoff = int(box_size - 1) # 确保 cutoff 是整数
print(f"Box Size: {box_size}, Cutoff: {cutoff}") # 打印调试信息
for node in uncovered_nodes:
# 使用自定义 Dijkstra 算法计算路径长度
path_lengths = custom_dijkstra(G, node, cutoff)
mass = len([n for n in path_lengths if n in uncovered_nodes])
if mass > max_mass:
max_mass = mass
max_mass_node = node
if max_mass_node is None:
break # 如果找不到中心节点,退出循环
centers.append(max_mass_node)
# 计算当前节点的箱子
box = set(n for n, d in path_lengths.items() if d <= (cutoff // 2))
boxes.append(box)
uncovered_nodes -= box
num_boxes.append(len(boxes))
return box_sizes, num_boxes
def box_covering_algorithm(G):
box_sizes = np.arange(2, 30) # 设定盒子大小范围
num_boxes = []
for box_size in box_sizes:
uncovered_nodes = set(G.nodes())
centers = []
boxes = []
while uncovered_nodes:
max_mass = 0
max_mass_node = None
for node in uncovered_nodes:
# 使用自定义 Dijkstra 算法计算路径长度
path_lengths = custom_dijkstra(G, node, box_size - 1)
mass = len([n for n in path_lengths if n in uncovered_nodes])
if mass > max_mass:
max_mass = mass
max_mass_node = node
if max_mass_node is None:
break # 如果找不到中心节点,退出循环
centers.append(max_mass_node)
# 计算当前节点的箱子
box = set(n for n, d in path_lengths.items() if d <= (box_size - 1) // 2)
boxes.append(box)
uncovered_nodes -= box
num_boxes.append(len(boxes))
return box_sizes, num_boxes