Memory management 在《圣人》中使用nauty_-geng的记忆者

Memory management 在《圣人》中使用nauty_-geng的记忆者,memory-management,sage,Memory Management,Sage,我试图让Sage生成所有具有11个顶点、30条边和4个团的图。我输入了以下内容: g11=[g for g in graphs.nauty_geng('11 30') if g.clique_number()==4] 过了一会儿,我看到以下消息: MemoryError Traceback (most recent call last) <ipython-input-6-1ec9660b8e07> in <modu

我试图让Sage生成所有具有11个顶点、30条边和4个团的图。我输入了以下内容:

 g11=[g for g in graphs.nauty_geng('11 30') if g.clique_number()==4]
过了一会儿,我看到以下消息:

MemoryError                               Traceback (most recent call last)
<ipython-input-6-1ec9660b8e07> in <module>()
----> 1 g11=[g for g in graphs.nauty_geng('11 30') if g.clique_number()==Integer(4)]

/opt/sagemath-8.6/local/lib/python2.7/site-packages/sage/graphs/graph.pyc in clique_number(self, algorithm, cliques, solver, verbose)
   6072         self._scream_if_not_simple(allow_loops=False)
   6073         if algorithm == "Cliquer":
-> 6074             from sage.graphs.cliquer import clique_number
   6075             return clique_number(self)
   6076         elif algorithm == "networkx":
MemoryError回溯(最近一次调用)
在()
---->1 g11=[g表示图中的g.nauty_geng('11 30'),如果g.group_number()==Integer(4)]
/团号中的opt/sagemath-8.6/local/lib/python2.7/site-packages/sage/graphs/graph.pyc(自我、算法、团、解算器、详细)
6072 self.\u尖叫\u如果\u不简单(允许\u循环=错误)
6073如果算法==“Cliquer”:
->6074来自sage.graphs.cliquer导入集团号
6075返回集团号(自身)
6076 elif算法==“networkx”:
我的内存似乎没有足够的内存来让圣人帮我做这件事。有没有办法让Sage将这些信息存储到其他地方?Sage是否只需要使用RAM内存?我有1 TB的可用存储空间

如果这是不可能的,那么我如何解决这个问题?提前谢谢你

上市前盘点 有时将感兴趣的数学对象存储在列表中是非常困难的 太雄心勃勃了,因为那样会占用太多的内存

第一步可能是计算有多少这样的图表, 以及在尝试之前迭代它们所需的时间 储存它们

下面的计时是在一台特定的机器上进行的;他们可能会有所不同 在其他机器上

计算团数为4的具有30条边的11个顶点上的图 花了大约两个小时

sage: g_11_30 = graphs.nauty_geng('11 30:30')
sage: g_11_30_c4 = (g for g in g_11_30 if g.clique_number() == 4)
sage: %time nb_g_11_30_c4 = sum(1 for g in g_11_30_c4)
CPU times: user 2h 12min 9s, sys: 1min 9s, total: 2h 13min 18s
Wall time: 2h 13min 18s
sage: nb_cg_11_30_c4
58211868
仅计算连接的数据所用的时间大致相同

sage: cg_11_30 = graphs.nauty_geng('11 30:30 -c')
sage: cg_11_30_c4 = (g for g in cg_11_30 if g.clique_number() == 4)
sage: %time nb_cg_11_30_c4 = sum(1 for g in cg_11_30_c4)
CPU times: user 2h 13min 27s, sys: 1min 11s, total: 2h 14min 38s
Wall time: 2h 14min 39s
sage: nb_cg_11_30_c4
58182054
我们看到大约有5820万个图位于11个顶点上,有30条边 第四集团,大多数都有联系——只有29814个没有联系。 如果我们只关心那些不相连的,那就大不一样了

迭代而不是列表 如果存储这些图不可行,我们知道可以遍历它们 两个小时后,每次我们都想了解他们

了解迭代vs列表的一个好方法是运行

例如,取集合中的第一个图形并检查其边 及其 ():

第二个:

sage: g = next(g_11_30_c4)
sage: print(g.edges(labels=False))
[(0, 7), (0, 8), (0, 9), (0, 10), (1, 7), (1, 8), (1, 9), (1, 10),
(2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10),
(4, 8), (4, 9), (4, 10), (5, 8), (5, 9), (5, 10), (6, 8), (6, 9),
(6, 10), (7, 8), (7, 9), (7, 10), (8, 10), (9, 10)]
sage: g.graph6_string()
'J???Fb~~v~_'
等等

存储较小的等效数据 如果图表本身太多而无法存储在列表中,也许我们可以 对这些图使用更紧凑的表示,这将占用更多的时间 内存更少。例如,边列表可以让我们轻松地重建 图表;非常紧凑的“graph6字符串”也是如此

为了给我们一个想法,让我们比较一下 作为Sage对象的前一万个图形列表, 其图形边列表作为Sage对象列出, 以及作为文本文件的图形6字符串:

sage: g_11_30 = graphs.nauty_geng('11 30:30')
sage: g_11_30_c4 = (g for g in g_11_30 if g.clique_number() == 4)
sage: graphs = [next(g_11_30_c4) for _ in range(10^4)]
sage: save(graphs, "g_11_30_c4_1e4_graph_bare")

sage: g_11_30 = graphs.nauty_geng('11 30:30')
sage: g_11_30_c4 = (g for g in g_11_30 if g.clique_number() == 4)
sage: edges = [next(g_11_30_c4).edges(labels=False) for _ in range(10^4)]
sage: save(edges, "g_11_30_c4_1e4_graph_edges")

sage: g_11_30 = graphs.nauty_geng('11 30:30')
sage: g_11_30_c4 = (g for g in g_11_30 if g.clique_number() == 4)
sage: s = '\n'.join(next(g_11_30_c4).graph6_string() for _ in range(10^4))
sage: with open('g_11_30_c4_graph_graph6.txt', 'w') as f:
....:     f.write(s)
....:
119999
比较相应的文件大小:

  • g_11_30_c4_1e4_图形_bare.sobj
    :971K
  • g_11_30_c4_1e4_图_边。sobj:775K
  • g_11_30_c4_1e4_graph_graph6.txt
    :117K
显然,graph6格式获胜,并存储了所有5820万个图形 以这种格式保存文本文件需要约5820*117K,即约680M

我们还可以将其存储在编号为0到99的100个文件中,如下所示:

sage: n = 100
sage: for k in range(N):
....:     gk = graphs.nauty_geng('11 30:30 {}/{}'.format(k, n))
....:     ggk = (g for g in gk if g.clique_number() == 4)
....:     s = '\n'.join(g.graph6_string() for g in ggk)
....:     with open('g_11_30_c4_graph_graph6_file_{}_of_{}.txt'
....:               .format(k, n - 1), 'w') as f:
....:         f.write(s)
这将使我们能够在几次会议上研究这些图表,而不必做任何修改 nauty每次工作两个小时

推荐阅读,具体取决于Sage基于的Python版本:

sage: n = 100
sage: for k in range(N):
....:     gk = graphs.nauty_geng('11 30:30 {}/{}'.format(k, n))
....:     ggk = (g for g in gk if g.clique_number() == 4)
....:     s = '\n'.join(g.graph6_string() for g in ggk)
....:     with open('g_11_30_c4_graph_graph6_file_{}_of_{}.txt'
....:               .format(k, n - 1), 'w') as f:
....:         f.write(s)