Advanced Computing Platform for Theoretical Physics

Commit 600a3e89 authored by Pengfei Zhou's avatar Pengfei Zhou
Browse files

wl-test

parent e81590fe
......@@ -31,4 +31,5 @@ parser.add_argument("-fvsenum", action='store_true',
parser.add_argument("-permutation",action='store_true',help='an isomorphism bijection')
parser.add_argument('-output', action='store_true')
parser.add_argument('-nx_iso',action='store_true')
parser.add_argument('-wl', action='store_true')
args = parser.parse_args()
......@@ -116,6 +116,15 @@ if __name__ == '__main__':
G = nx.Graph()
G.add_nodes_from(np.arange(args.n))
G.add_edges_from(edges)
if args.wl:
time_1=time.time()
n2,edges2=convert('Generated_graphs.20.03.g6',args.which+1)
G1=nx.Graph()
G1.add_nodes_from(np.arange(n2))
G1.add_edges_from(edges2)
from wlkernel.weisfeiler_lehman import is_possibly_isomorphic
print(is_possibly_isomorphic(G,G1))
print("time: {:.2g} sec".format(time.time()-time_1))
if args.nx_iso:
time_1=time.time()
n2,edges2=convert('Generated_graphs.20.03.g6',args.which+1)
......
import numpy as np
import torch as th
import dgl
def _send_color(edges):
return {'color': edges.src['color']}
def _gen_create_multiset(num_nodes):
def _create_multiset(nodes):
end = nodes.mailbox['color'].shape[1]
multiset = th.zeros((nodes.batch_size(), num_nodes)) - 1
multiset[:, 0] = nodes.data['color']
multiset[:, 1:end + 1] = nodes.mailbox['color'].sort().values
return {'color': multiset}
return _create_multiset
def _to_color(colors):
colors = colors[colors >= 0]
self_color = colors.astype(int).astype(str)[0]
neighbour_color = colors[1:].astype(int).astype(str).tolist()
return self_color + '|' + ','.join(neighbour_color)
def _update_colors(G):
G.update_all()
return list(map(_to_color, G.ndata.pop('color').cpu().numpy()))
def is_possibly_isomorphic(G, H, max_iter=10):
"""Check if the two given graphs are possibly isomorphic by the 1-Weisfeiler-Lehman algorithm.
Arguments:
G {networkx.classes.graph.Graph} -- Graph
H {networkx.classes.graph.Graph} -- Graph
Keyword Arguments:
max_iter {int} -- The max number of iterations(default: {10})
Returns:
bool -- Return "False" if definitely not isomorphic
"""
G = dgl.DGLGraph(G)
H = dgl.DGLGraph(H)
# If the number of nodes is different, return False.
if G.number_of_nodes() != H.number_of_nodes():
return False
# Set initial colors
G.ndata['color'] = np.zeros(G.number_of_nodes())
H.ndata['color'] = np.zeros(H.number_of_nodes())
N = G.number_of_dst_nodes()
current_max_color = 0
G.register_message_func(_send_color)
G.register_reduce_func(_gen_create_multiset(N))
H.register_message_func(_send_color)
H.register_reduce_func(_gen_create_multiset(N))
# Refine colors until convergence
for i in range(max_iter):
G_colors = _update_colors(G)
H_colors = _update_colors(H)
G_unique_colors, G_counts = np.unique(G_colors, return_counts=True)
H_unique_colors, H_counts = np.unique(H_colors, return_counts=True)
G_multiset = dict(zip(G_unique_colors, G_counts))
H_multiset = dict(zip(H_unique_colors, H_counts))
if G_multiset != H_multiset:
return False
# Recoloring (str -> int)
unique_colors = np.unique(np.append(G_unique_colors, H_unique_colors))
recolor_map = {color: i + 1 for i, color in enumerate(unique_colors)}
G.ndata['color'] = np.array([recolor_map[color]
for color in G_colors]) + current_max_color
H.ndata['color'] = np.array([recolor_map[color]
for color in H_colors]) + current_max_color
current_max_color += len(unique_colors)
return True
def subtree_kernel(G, H, max_iter=10):
"""Calculate the Weisfeiler Lehman graph kernel.
Arguments:
G {networkx.classes.graph.Graph} -- Graph
H {networkx.classes.graph.Graph} -- Graph
Keyword Arguments:
max_iter {int} -- The max number of iterations (default: {10})
Returns:
float -- The value of the subtree kernel
"""
G = dgl.DGLGraph(G)
H = dgl.DGLGraph(H)
kernel_value = 0
# Set initial colors
G.ndata['color'] = np.zeros(G.number_of_nodes())
H.ndata['color'] = np.zeros(H.number_of_nodes())
N = G.number_of_dst_nodes()
current_max_color = 0
G.register_message_func(_send_color)
G.register_reduce_func(_gen_create_multiset(N))
H.register_message_func(_send_color)
H.register_reduce_func(_gen_create_multiset(N))
# Refine colors until convergence
for i in range(max_iter):
G_colors = _update_colors(G)
H_colors = _update_colors(H)
G_unique_colors, G_counts = np.unique(G_colors, return_counts=True)
H_unique_colors, H_counts = np.unique(H_colors, return_counts=True)
G_multiset = dict(zip(G_unique_colors, G_counts))
H_multiset = dict(zip(H_unique_colors, H_counts))
# Recoloring (str -> int)
unique_colors = np.unique(np.append(G_unique_colors, H_unique_colors))
recolor_map = {color: i + 1 for i, color in enumerate(unique_colors)}
# Add the value of the subtree kernel in i-th step
G_color_vector = np.zeros(len(unique_colors))
H_color_vector = np.zeros(len(unique_colors))
for color, i in recolor_map.items():
G_color_vector[i - 1] = G_multiset[color] if color in G_multiset else 0
H_color_vector[i - 1] = H_multiset[color] if color in H_multiset else 0
kernel_value += np.dot(G_color_vector, H_color_vector)
G.ndata['color'] = np.array([recolor_map[color]
for color in G_colors]) + current_max_color
H.ndata['color'] = np.array([recolor_map[color]
for color in H_colors]) + current_max_color
current_max_color += len(unique_colors)
return kernel_value
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment