我需要根据另一位研究人员的工作创建一个六角形晶格网络。研究人员在此处的黄色突出显示的文本部分中描述了他们的网络结构。
这里有来自实际论文的更多信息(同样,我只需要六角晶格结构的帮助):
全文在这里。
我怀疑我错过了什么。这是我的代码:
import networkx as nx
import random
import matplotlib.pyplot as plt
def create_hexagonal_lattice_graph(rows, cols):
G = nx.hexagonal_lattice_graph(rows, cols)
simple_G = nx.Graph(G)
return simple_G
# Define the number of rows and columns to get approximately 200 nodes
rows, cols = 6, 6 # Adjusting to get roughly 200 nodes (10*10 grid)
# Create the initial hexagonal lattice graph
G = create_hexagonal_lattice_graph(rows, cols)
# Ensure all nodes have degree 6 by adding missing edges
for node in G.nodes():
while G.degree(node) < 6:
possible_nodes = set(G.nodes()) - set(G.neighbors(node)) - {node}
new_neighbor = random.choice(list(possible_nodes))
G.add_edge(node, new_neighbor)
# Check the number of nodes and ensure all have degree 6
num_nodes = G.number_of_nodes()
degrees = dict(G.degree())
degree_check = all(degree == 6 for degree in degrees.values())
print(f"Number of nodes: {num_nodes}")
print(f"All nodes have degree 6: {degree_check}")
plt.title("Initial Hexagonal Lattice")
nx.draw_circular(G, node_size=20, with_labels=False)
结果没有我希望的度分布和聚类系数。我觉得我缺少构建网络所需的信息。如何修改代码来创建满足这些基本特征(Z=6;CC=.4)的无向六边形点阵图?
和切比雪夫距离 来几何地 接近它:
import networkx as nx
from libpysal import weights
def lattice(r, c, no_ortho):
"""Return Graph and nodes' positions."""
from itertools import product
grid = list(product(range(r), range(c)))
if no_ortho: grid = grid[1::2]
G = weights.DistanceBand(
grid,
threshold=int(no_ortho) + 1,
p=float("inf"), # chebyshev
silence_warnings=True,
).to_networkx()
if no_ortho:
G.remove_edges_from(
[(u, v) for u, v in G.edges if abs(u - v) == 1]
)
return G, dict(zip(G, grid))
A = lattice(7, 7, True)
B = lattice(7, 7, False)