Browse Source

1. allow pre-image algorithm based on IAM to use all k nearest graphs as the initials of IAM.

2. fix a bug: in preimage_iam_random_mix function, sort D_k after updating it.
v0.1
jajupmochi 5 years ago
parent
commit
edc3db457a
3 changed files with 528 additions and 314 deletions
  1. +419
    -235
      preimage/gk_iam.py
  2. +81
    -66
      preimage/iam.py
  3. +28
    -13
      preimage/test_random_mutag.py

+ 419
- 235
preimage/gk_iam.py View File

@@ -281,7 +281,7 @@ def gk_iam_nearest(Gn, alpha, idx_gi, Kmatrix, k, r_max):




def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max, def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
gkernel, epsilon=0.001,
gkernel, epsilon=0.001, InitIAMWithAllDk=True,
params_iam={'c_ei': 1, 'c_er': 1, 'c_es': 1, params_iam={'c_ei': 1, 'c_er': 1, 'c_es': 1,
'ite_max': 50, 'epsilon': 0.001, 'ite_max': 50, 'epsilon': 0.001,
'removeNodes': True, 'connected': False}, 'removeNodes': True, 'connected': False},
@@ -298,7 +298,7 @@ def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
distances will be used as the new ones. distances will be used as the new ones.
""" """
# compute k nearest neighbors of phi in DN. # compute k nearest neighbors of phi in DN.
dis_list = [] # distance between g_star and each graph.
dis_all = [] # distance between g_star and each graph.
term3 = 0 term3 = 0
for i1, a1 in enumerate(alpha): for i1, a1 in enumerate(alpha):
for i2, a2 in enumerate(alpha): for i2, a2 in enumerate(alpha):
@@ -309,18 +309,17 @@ def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
# k_g2_list[ig]) + (alpha * alpha * k_list[0] + alpha * # k_g2_list[ig]) + (alpha * alpha * k_list[0] + alpha *
# (1 - alpha) * k_g2_list[0] + (1 - alpha) * alpha * # (1 - alpha) * k_g2_list[0] + (1 - alpha) * alpha *
# k_g1_list[6] + (1 - alpha) * (1 - alpha) * k_list[6]) # k_g1_list[6] + (1 - alpha) * (1 - alpha) * k_list[6])
dis_list.append(dtemp)
dis_all.append(dtemp)
# sort # sort
sort_idx = np.argsort(dis_list)
dis_gs = [dis_list[idis] for idis in sort_idx[0:k]] # the k shortest distances
nb_best = len(np.argwhere(dis_gs == dis_gs[0]).flatten().tolist())
g0hat_list = [Gn_init[idx] for idx in sort_idx[0:nb_best]] # the nearest neighbors of phi in DN
if dis_gs[0] == 0: # the exact pre-image.
sort_idx = np.argsort(dis_all)
dis_k = [dis_all[idis] for idis in sort_idx[0:k]] # the k shortest distances
nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
ghat_list = [Gn_init[idx].copy() for idx in sort_idx[0:nb_best]] # the nearest neighbors of phi in DN
if dis_k[0] == 0: # the exact pre-image.
print('The exact pre-image is found from the input dataset.') print('The exact pre-image is found from the input dataset.')
return 0, g0hat_list, 0, 0
dhat = dis_gs[0] # the nearest distance
ghat_list = [g.copy() for g in g0hat_list]
return 0, ghat_list, 0, 0
dhat = dis_k[0] # the nearest distance
# for g in ghat_list: # for g in ghat_list:
# draw_Letter_graph(g) # draw_Letter_graph(g)
# nx.draw_networkx(g) # nx.draw_networkx(g)
@@ -335,8 +334,6 @@ def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
# draw_Letter_graph(g) # draw_Letter_graph(g)
print(gi.nodes(data=True)) print(gi.nodes(data=True))
print(gi.edges(data=True)) print(gi.edges(data=True))
Gs_nearest = [g.copy() for g in Gk]
Gn_nearest_median = [g.copy() for g in Gs_nearest]
# gihat_list = [] # gihat_list = []
# i = 1 # i = 1
@@ -344,32 +341,137 @@ def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
itr_total = 0 itr_total = 0
# cur_dis = dhat # cur_dis = dhat
# old_dis = cur_dis * 2 # old_dis = cur_dis * 2
dis_list = [dhat]
dis_of_each_itr = [dhat]
found = False found = False
nb_updated = 0 nb_updated = 0
nb_updated_k = 0
while r < r_max:# and not found: # @todo: if not found?# and np.abs(old_dis - cur_dis) > epsilon: while r < r_max:# and not found: # @todo: if not found?# and np.abs(old_dis - cur_dis) > epsilon:
print('\nCurrent preimage iteration =', r)
print('\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-')
print('Current preimage iteration =', r)
print('Total preimage iteration =', itr_total, '\n') print('Total preimage iteration =', itr_total, '\n')
found = False found = False
# Gs_nearest = Gk + gihat_list # Gs_nearest = Gk + gihat_list
# g_tmp = iam(Gs_nearest) # g_tmp = iam(Gs_nearest)
g_tmp_list, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
Gn_nearest_median, Gs_nearest, params_ged=params_ged, **params_iam)
Gn_nearest_median = [g.copy() for g in Gk]
if InitIAMWithAllDk: # each graph in D_k is used to initialize IAM.
ghat_new_list = []
for g_tmp in Gk:
Gn_nearest_init = [g_tmp.copy()]
ghat_new_list_tmp, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
Gn_nearest_median, Gn_nearest_init, params_ged=params_ged,
**params_iam)
ghat_new_list += ghat_new_list_tmp
else: # only the best graph in D_k is used to initialize IAM.
Gn_nearest_init = [g.copy() for g in Gk]
ghat_new_list, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
Gn_nearest_median, Gn_nearest_init, params_ged=params_ged,
**params_iam)

# for g in g_tmp_list: # for g in g_tmp_list:
# nx.draw_networkx(g) # nx.draw_networkx(g)
# plt.show() # plt.show()
# draw_Letter_graph(g) # draw_Letter_graph(g)
# print(g.nodes(data=True)) # print(g.nodes(data=True))
# print(g.edges(data=True)) # print(g.edges(data=True))
# compute distance between \psi and the new generated graphs. # compute distance between \psi and the new generated graphs.
knew = compute_kernel(g_tmp_list + Gn_median, gkernel, False)
dnew_list = []
for idx, g_tmp in enumerate(g_tmp_list):
knew = compute_kernel(ghat_new_list + Gn_median, gkernel, False)
dhat_new_list = []
for idx, g_tmp in enumerate(ghat_new_list):
# @todo: the term3 below could use the one at the beginning of the function. # @todo: the term3 below could use the one at the beginning of the function.
dnew_list.append(dis_gstar(idx, range(len(g_tmp_list),
len(g_tmp_list) + len(Gn_median) + 1),
alpha, knew, withterm3=False))
dhat_new_list.append(dis_gstar(idx, range(len(ghat_new_list),
len(ghat_new_list) + len(Gn_median) + 1),
alpha, knew, withterm3=False))
for idx_g, ghat_new in enumerate(ghat_new_list):
# ghat_new = ghat_new_list[0].copy()
dhat_new = dhat_new_list[idx_g]
# if the new distance is smaller than the max of D_k.
if dhat_new < dis_k[-1] and np.abs(dhat_new - dis_k[-1]) >= epsilon:
# check if the new distance is the same as one in D_k.
is_duplicate = False
for dis_tmp in dis_k[1:-1]:
if np.abs(dhat_new - dis_tmp) < epsilon:
is_duplicate = True
print('IAM: generated duplicate k nearest graph.')
break
if not is_duplicate:
if np.abs(dhat_new - dhat) < epsilon:
print('IAM: I am equal!')
# dhat = dhat_new
# ghat_list = [ghat_new.copy()]
# is_iam_duplicate = True
else:
print('IAM: we got better k nearest neighbors!')
nb_updated_k += 1
print('the k nearest neighbors are updated',
nb_updated_k, 'times.')
dis_k = [dhat_new] + dis_k[0:k-1] # add the new nearest distance.
Gk = [nx.convert_node_labels_to_integers(ghat_new.copy())] \
+ Gk[0:k-1] # add the corresponding graph.
sort_idx = np.argsort(dis_k)
dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
Gk = [Gk[idx] for idx in sort_idx[0:k]]
if dhat_new < dhat:
print('IAM: I have smaller distance!')
print(str(dhat) + '->' + str(dhat_new))
dhat = dhat_new
ghat_list = [Gk[0].copy()]
r = 0
nb_updated += 1
print('the graph is updated', nb_updated, 'times.')
nx.draw(Gk[0], labels=nx.get_node_attributes(Gk[0], 'atom'),
with_labels=True)
## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
plt.show()
found = True
if not found:
r += 1

# else: # @todo: may not work.
# dis_k = dhat_new_list + dis_k # add the new nearest distances.
# Gk = [nx.convert_node_labels_to_integers(g.copy()) for g
# in ghat_new_list] + Gk # add the corresponding graphs.
# sort_idx = np.argsort(dis_k)
# if len([i for i in sort_idx[0:k] if i < len(dhat_new_list)]) > 0:
# print('We got new k nearest neighbors! Hurray!')
# dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
# # print(dis_k[-1])
# Gk = [Gk[idx] for idx in sort_idx[0:k]]
# nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
# if dhat_new < dhat:
# print('I have smaller distance!')
# print(str(dhat) + '->' + str(dhat_new))
# dhat = dis_k[0]
# idx_best_list = np.argwhere(dis_k == dhat_new).flatten().tolist()
# ghat_list = [Gk[idx].copy() for idx in idx_best_list]
# # for g in ghat_list:
# ## nx.draw_networkx(g)
# ## plt.show()
# # draw_Letter_graph(g)
# # print(g.nodes(data=True))
# # print(g.edges(data=True))
# r = 0
# found = True
# nb_updated += 1
# print('the graph is updated by IAM', nb_updated, 'times.')
#
# nx.draw(ghat_list[0], labels=nx.get_node_attributes(ghat_list[0], 'atom'),
# with_labels=True)
# ## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
# plt.show()
# else:
# dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
# Gk = [Gk[idx] for idx in sort_idx[0:k]]
# # Gn_nearest_median = [g.copy() for g in Gn_nearest_init]
# if not found:
# r += 1
# dnew = knew[0, 0] - 2 * (alpha[0] * knew[0, 1] + alpha[1] * # dnew = knew[0, 0] - 2 * (alpha[0] * knew[0, 1] + alpha[1] *
# knew[0, 2]) + (alpha[0] * alpha[0] * k_list[0] + alpha[0] * # knew[0, 2]) + (alpha[0] * alpha[0] * k_list[0] + alpha[0] *
@@ -377,20 +479,20 @@ def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
# k_g1_list[1] + alpha[1] * alpha[1] * k_list[1]) # k_g1_list[1] + alpha[1] * alpha[1] * k_list[1])
# # find the new k nearest graphs. # # find the new k nearest graphs.
# dnew_best = min(dnew_list)
# dis_gs = dnew_list + dis_gs # add the new nearest distances.
# dhat_new = min(dnew_list)
# dis_k = dnew_list + dis_k # add the new nearest distances.
# Gs_nearest = [g.copy() for g in g_tmp_list] + Gs_nearest # add the corresponding graphs. # Gs_nearest = [g.copy() for g in g_tmp_list] + Gs_nearest # add the corresponding graphs.
# sort_idx = np.argsort(dis_gs)
# sort_idx = np.argsort(dis_k)
# if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0: # if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0:
# print('We got new k nearest neighbors! Hurray!') # print('We got new k nearest neighbors! Hurray!')
# dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
## print(dis_gs[-1])
# dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
## print(dis_k[-1])
# Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]] # Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
# nb_best = len(np.argwhere(dis_gs == dis_gs[0]).flatten().tolist())
# nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
# if dnew_best < dhat and np.abs(dnew_best - dhat) > epsilon: # if dnew_best < dhat and np.abs(dnew_best - dhat) > epsilon:
# print('I have smaller distance!') # print('I have smaller distance!')
# print(str(dhat) + '->' + str(dis_gs[0]))
# dhat = dis_gs[0]
# print(str(dhat) + '->' + str(dis_k[0]))
# dhat = dis_k[0]
# idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist() # idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist()
# ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list] # ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list]
## for g in ghat_list: ## for g in ghat_list:
@@ -406,67 +508,72 @@ def gk_iam_nearest_multi(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
# print('I have almost equal distance!') # print('I have almost equal distance!')
# print(str(dhat) + '->' + str(dnew_best)) # print(str(dhat) + '->' + str(dnew_best))
# else: # else:
# dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]]
# dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
# Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]] # Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
# Gn_nearest_median = [g.copy() for g in Gs_nearest] # Gn_nearest_median = [g.copy() for g in Gs_nearest]
# if not found: # if not found:
# r += 1 # r += 1
# find the new k nearest graphs.
dnew_best = min(dnew_list)
if np.abs(dnew_best - dhat) >= epsilon:
dis_gs = dnew_list + dis_gs # add the new nearest distances.
Gs_nearest = [g.copy() for g in g_tmp_list] + Gs_nearest # add the corresponding graphs.
sort_idx = np.argsort(dis_gs)
else: # if the new distance is equal to the old one.
# @todo: works if only one graph is generated.
Gs_nearest[0] = g_tmp_list[0].copy()
sort_idx = np.argsort(dis_gs)
if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0:
print('We got new k nearest neighbors! Hurray!')
dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
# print(dis_gs[-1])
Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
nb_best = len(np.argwhere(dis_gs == dis_gs[0]).flatten().tolist())
if dnew_best < dhat and np.abs(dnew_best - dhat) >= epsilon:
print('I have smaller distance!')
print(str(dhat) + '->' + str(dis_gs[0]))
dhat = dis_gs[0]
idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist()
ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list]
# for g in ghat_list:
## nx.draw_networkx(g)
## plt.show()
# draw_Letter_graph(g)
# print(g.nodes(data=True))
# print(g.edges(data=True))
r = 0
found = True
nb_updated += 1
elif np.abs(dnew_best - dhat) < epsilon:
print('I have almost equal distance!')
print(str(dhat) + '->' + str(dnew_best))
else:
dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]]
Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
Gn_nearest_median = [g.copy() for g in Gs_nearest]
if not found:
r += 1
# # find the new k nearest graphs.
# dnew_best = min(dnew_list)
# if np.abs(dnew_best - dhat) >= epsilon:
# dis_k = dnew_list + dis_k # add the new nearest distances.
# Gs_nearest_init = [g.copy() for g in g_tmp_list] + Gs_nearest_init # add the corresponding graphs.
# sort_idx = np.argsort(dis_k)
# else: # if the new distance is equal to the old one.
# # @todo: works if only one graph is generated.
# Gs_nearest_init[0] = g_tmp_list[0]_init.copy()
# sort_idx = np.argsort(dis_k)
# if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0:
# print('We got new k nearest neighbors! Hurray!')
# dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
## print(dis_k[-1])
# Gs_nearest_init = [Gs_nearest_init[idx] for idx in sort_idx[0:k]]
# nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
# if dnew_best < dhat and np.abs(dnew_best - dhat) >= epsilon:
# print('I have smaller distance!')
# print(str(dhat) + '->' + str(dis_k[0]))
# dhat = dis_k[0]
# idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist()
# ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list]
## for g in ghat_list:
### nx.draw_networkx(g)
### plt.show()
## draw_Letter_graph(g)
## print(g.nodes(data=True))
## print(g.edges(data=True))
# r = 0
# found = True
# nb_updated += 1
# elif np.abs(dnew_best - dhat) < epsilon:
# print('I have almost equal distance!')
# print(str(dhat) + '->' + str(dnew_best))
# else:
# dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
# Gs_nearest_init = [Gs_nearest_init[idx] for idx in sort_idx[0:k]]
# Gn_nearest_median = [g.copy() for g in Gs_nearest]
# if not found:
# r += 1
# old_dis = cur_dis # old_dis = cur_dis
# cur_dis = dnew_best # cur_dis = dnew_best
dis_list.append(dhat)
dis_of_each_itr.append(dhat)
itr_total += 1 itr_total += 1
print('\nthe k shortest distances are', dis_k)
print('the shortest distances for previous iterations are', dis_of_each_itr)
print('\nthe graph is updated', nb_updated, 'times.') print('\nthe graph is updated', nb_updated, 'times.')
print('distances in kernel space:', dis_list, '\n')
print('\nthe k nearest neighbors are updated', nb_updated_k, 'times.')
print('distances in kernel space:', dis_of_each_itr, '\n')
return dhat, ghat_list, dis_list[-1], nb_updated
return dhat, ghat_list, dis_of_each_itr[-1], nb_updated, nb_updated_k







def preimage_iam_random_mix(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max, def preimage_iam_random_mix(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max,
l_max, gkernel, epsilon=0.001,
l_max, gkernel, epsilon=0.001,
allDkInitRandom=False, InitIAMWithAllDk=False,
params_iam={'c_ei': 1, 'c_er': 1, 'c_es': 1, params_iam={'c_ei': 1, 'c_er': 1, 'c_es': 1,
'ite_max': 50, 'epsilon': 0.001, 'ite_max': 50, 'epsilon': 0.001,
'removeNodes': True, 'connected': False}, 'removeNodes': True, 'connected': False},
@@ -484,25 +591,24 @@ def preimage_iam_random_mix(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max
""" """
Gn_init = [nx.convert_node_labels_to_integers(g) for g in Gn_init] Gn_init = [nx.convert_node_labels_to_integers(g) for g in Gn_init]
# compute k nearest neighbors of phi in DN. # compute k nearest neighbors of phi in DN.
dis_list = [] # distance between g_star and each graph.
dis_all = [] # distance between g_star and each graph.
term3 = 0 term3 = 0
for i1, a1 in enumerate(alpha): for i1, a1 in enumerate(alpha):
for i2, a2 in enumerate(alpha): for i2, a2 in enumerate(alpha):
term3 += a1 * a2 * Kmatrix[idx_gi[i1], idx_gi[i2]] term3 += a1 * a2 * Kmatrix[idx_gi[i1], idx_gi[i2]]
for ig, g in tqdm(enumerate(Gn_init), desc='computing distances', file=sys.stdout): for ig, g in tqdm(enumerate(Gn_init), desc='computing distances', file=sys.stdout):
dtemp = dis_gstar(ig, idx_gi, alpha, Kmatrix, term3=term3) dtemp = dis_gstar(ig, idx_gi, alpha, Kmatrix, term3=term3)
dis_list.append(dtemp)
dis_all.append(dtemp)
# sort # sort
sort_idx = np.argsort(dis_list)
dis_gs = [dis_list[idis] for idis in sort_idx[0:k]] # the k shortest distances
nb_best = len(np.argwhere(dis_gs == dis_gs[0]).flatten().tolist())
g0hat_list = [Gn_init[idx] for idx in sort_idx[0:nb_best]] # the nearest neighbors of phi in DN
if dis_gs[0] == 0: # the exact pre-image.
sort_idx = np.argsort(dis_all)
dis_k = [dis_all[idis] for idis in sort_idx[0:k]] # the k shortest distances
nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
ghat_list = [Gn_init[idx].copy() for idx in sort_idx[0:nb_best]] # the nearest neighbors of psi in DN
if dis_k[0] == 0: # the exact pre-image.
print('The exact pre-image is found from the input dataset.') print('The exact pre-image is found from the input dataset.')
return 0, g0hat_list, 0, 0
dhat = dis_gs[0] # the nearest distance
ghat_list = [g.copy() for g in g0hat_list]
return 0, ghat_list, 0, 0
dhat = dis_k[0] # the nearest distance
# for g in ghat_list: # for g in ghat_list:
# draw_Letter_graph(g) # draw_Letter_graph(g)
# nx.draw_networkx(g) # nx.draw_networkx(g)
@@ -517,8 +623,6 @@ def preimage_iam_random_mix(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max
# draw_Letter_graph(g) # draw_Letter_graph(g)
print(gi.nodes(data=True)) print(gi.nodes(data=True))
print(gi.edges(data=True)) print(gi.edges(data=True))
Gs_nearest = [g.copy() for g in Gk]
Gn_nearest_median = [g.copy() for g in Gs_nearest]
# gihat_list = [] # gihat_list = []
# i = 1 # i = 1
@@ -526,10 +630,13 @@ def preimage_iam_random_mix(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max
itr_total = 0 itr_total = 0
# cur_dis = dhat # cur_dis = dhat
# old_dis = cur_dis * 2 # old_dis = cur_dis * 2
dis_list = [dhat]
dis_of_each_itr = [dhat]
found = False found = False
nb_updated_iam = 0 nb_updated_iam = 0
nb_updated_k_iam = 0
nb_updated_random = 0 nb_updated_random = 0
nb_updated_k_random = 0
is_iam_duplicate = False
while r < r_max: # and not found: # @todo: if not found?# and np.abs(old_dis - cur_dis) > epsilon: while r < r_max: # and not found: # @todo: if not found?# and np.abs(old_dis - cur_dis) > epsilon:
print('\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-') print('\n-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-')
print('Current preimage iteration =', r) print('Current preimage iteration =', r)
@@ -537,174 +644,251 @@ def preimage_iam_random_mix(Gn_init, Gn_median, alpha, idx_gi, Kmatrix, k, r_max
found = False found = False
# Gs_nearest = Gk + gihat_list # Gs_nearest = Gk + gihat_list
# g_tmp = iam(Gs_nearest) # g_tmp = iam(Gs_nearest)
g_tmp_list, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
Gn_nearest_median, Gs_nearest, params_ged=params_ged, **params_iam)
# for g in g_tmp_list:
# nx.draw_networkx(g)
# plt.show()
# draw_Letter_graph(g)
# print(g.nodes(data=True))
# print(g.edges(data=True))
# compute distance between \psi and the new generated graphs.
knew = compute_kernel(g_tmp_list + Gn_median, gkernel, False)
dnew_list = []
for idx, g_tmp in enumerate(g_tmp_list):
# @todo: the term3 below could use the one at the beginning of the function.
dnew_list.append(dis_gstar(idx, range(len(g_tmp_list),
len(g_tmp_list) + len(Gn_median) + 1),
alpha, knew, withterm3=False))
if not is_iam_duplicate:
Gn_nearest_init = [g.copy() for g in Gk]
Gn_nearest_median = [g.copy() for g in Gk]
ghat_new_list, _ = iam_moreGraphsAsInit_tryAllPossibleBestGraphs(
Gn_nearest_median, Gn_nearest_init, params_ged=params_ged, **params_iam)
# for g in ghat_new_list:
# nx.draw_networkx(g)
# plt.show()
# draw_Letter_graph(g)
# print(g.nodes(data=True))
# print(g.edges(data=True))
# compute distance between \psi and the new generated graphs.
knew = compute_kernel(ghat_new_list + Gn_median, gkernel, False)
dhat_new_list = []
for idx, g_tmp in enumerate(ghat_new_list):
# @todo: the term3 below could use the one at the beginning of the function.
dhat_new_list.append(dis_gstar(idx, range(len(ghat_new_list),
len(ghat_new_list) + len(Gn_median) + 1),
alpha, knew, withterm3=False))
ghat_new = ghat_new_list[0].copy()
dhat_new = min(dhat_new_list)
# find the new k nearest graphs. # find the new k nearest graphs.
# @todo: for now only consider the situation when only one graph is generated by IAM. # @todo: for now only consider the situation when only one graph is generated by IAM.
dnew_best = min(dnew_list)
gnew_best = g_tmp_list[0].copy()
# when new distance is equal to the old one, use random generation.
if np.abs(dnew_best - dhat) < epsilon or dhat < dnew_best:
# Gs_nearest[0] = g_tmp_list[0].copy()
# sort_idx = np.argsort(dis_gs)
print('Distance almost equal or worse, switching to random generation now.')
print(str(dhat) + '->' + str(dnew_best))
# when new distance is not smaller than the max of D_k, use random generation.
if np.abs(dhat_new - dis_k[-1]) < epsilon or dis_k[-1] < dhat_new or \
is_iam_duplicate:
# Gs_nearest[0] = ghat_new_list[0].copy()
# sort_idx = np.argsort(dis_k)
print('Distance not better, switching to random generation now.')
print(str(dhat) + '->' + str(dhat_new))
if dnew_best > dhat and np.abs(dnew_best - dhat) >= epsilon:
dnew_best = dhat
gnew_best = Gs_nearest[0].copy()
if allDkInitRandom: # use all k nearest graphs as the initials.
init_list = [g_init.copy() for g_init in Gk]
else: # use just the nearest graph as the initial.
init_list = [Gk[0].copy()]
# number of edges to be changed. # number of edges to be changed.
# @todo what if the log is negetive? how to choose alpha (scalar)? seems fdgs is always 1.
# fdgs = dnew_best
fdgs = nb_updated_random + 1
if fdgs < 1:
fdgs = 1
fdgs = int(np.ceil(np.log(fdgs)))
if fdgs < 1:
fdgs += 1
# fdgs = nb_updated_random + 1 # @todo:
# @todo: should we use just half of the adjacency matrix for undirected graphs?
nb_vpairs = nx.number_of_nodes(gnew_best) * (nx.number_of_nodes(gnew_best) - 1)
if len(init_list) == 1:
# @todo what if the log is negetive? how to choose alpha (scalar)? seems fdgs is always 1.
# fdgs = dhat_new
fdgs = nb_updated_random + 1
if fdgs < 1:
fdgs = 1
fdgs = int(np.ceil(np.log(fdgs)))
if fdgs < 1:
fdgs += 1
# fdgs = nb_updated_random + 1 # @todo:
fdgs_list = [fdgs]
else:
# @todo what if the log is negetive? how to choose alpha (scalar)?
fdgs_list = np.array(dis_k[:])
if np.min(fdgs_list) < 1:
fdgs_list /= dis_k[0]
fdgs_list = [int(item) for item in np.ceil(np.log(fdgs_list))]
if np.min(fdgs_list) < 1:
fdgs_list = np.array(fdgs_list) + 1
l = 0 l = 0
while l < l_max:
# add and delete edges.
gtemp = gnew_best.copy()
np.random.seed()
# which edges to change.
# @todo: what if fdgs is bigger than nb_vpairs?
idx_change = random.sample(range(nb_vpairs), fdgs if
fdgs < nb_vpairs else nb_vpairs)
found_random = False
while l < l_max and not found_random:
for idx_g, g_tmp in enumerate(init_list):
# add and delete edges.
ghat_new = nx.convert_node_labels_to_integers(g_tmp.copy())
# @todo: should we use just half of the adjacency matrix for undirected graphs?
nb_vpairs = nx.number_of_nodes(ghat_new) * (nx.number_of_nodes(ghat_new) - 1)
np.random.seed()
# which edges to change.
# @todo: what if fdgs is bigger than nb_vpairs?
idx_change = random.sample(range(nb_vpairs), fdgs_list[idx_g] if
fdgs_list[idx_g] < nb_vpairs else nb_vpairs)
# idx_change = np.random.randint(0, nx.number_of_nodes(gs) * # idx_change = np.random.randint(0, nx.number_of_nodes(gs) *
# (nx.number_of_nodes(gs) - 1), fdgs) # (nx.number_of_nodes(gs) - 1), fdgs)
for item in idx_change:
node1 = int(item / (nx.number_of_nodes(gtemp) - 1))
node2 = (item - node1 * (nx.number_of_nodes(gtemp) - 1))
if node2 >= node1: # skip the self pair.
node2 += 1
# @todo: is the randomness correct?
if not gtemp.has_edge(node1, node2):
gtemp.add_edge(node1, node2)
# nx.draw_networkx(gs)
# plt.show()
# nx.draw_networkx(gtemp)
# plt.show()
for item in idx_change:
node1 = int(item / (nx.number_of_nodes(ghat_new) - 1))
node2 = (item - node1 * (nx.number_of_nodes(ghat_new) - 1))
if node2 >= node1: # skip the self pair.
node2 += 1
# @todo: is the randomness correct?
if not ghat_new.has_edge(node1, node2):
ghat_new.add_edge(node1, node2)
# nx.draw_networkx(gs)
# plt.show()
# nx.draw_networkx(ghat_new)
# plt.show()
else:
ghat_new.remove_edge(node1, node2)
# nx.draw_networkx(gs)
# plt.show()
# nx.draw_networkx(ghat_new)
# plt.show()
# nx.draw_networkx(ghat_new)
# plt.show()
# compute distance between \psi and the new generated graph.
knew = compute_kernel([ghat_new] + Gn_median, gkernel, verbose=False)
dhat_new = dis_gstar(0, [1, 2], alpha, knew, withterm3=False)
# @todo: the new distance is smaller or also equal?
if dhat_new < dis_k[-1] and np.abs(dhat_new - dis_k[-1]) >= epsilon:
# check if the new distance is the same as one in D_k.
is_duplicate = False
for dis_tmp in dis_k[1:-1]:
if np.abs(dhat_new - dis_tmp) < epsilon:
is_duplicate = True
print('Random: generated duplicate k nearest graph.')
break
if not is_duplicate:
if np.abs(dhat_new - dhat) < epsilon:
print('Random: I am equal!')
# dhat = dhat_new
# ghat_list = [ghat_new.copy()]
else:
print('Random: we got better k nearest neighbors!')
print('l =', str(l))
nb_updated_k_random += 1
print('the k nearest neighbors are updated by random generation',
nb_updated_k_random, 'times.')
dis_k = [dhat_new] + dis_k # add the new nearest distances.
Gk = [ghat_new.copy()] + Gk # add the corresponding graphs.
sort_idx = np.argsort(dis_k)
dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
Gk = [Gk[idx] for idx in sort_idx[0:k]]
if dhat_new < dhat:
print('\nRandom: I am smaller!')
print('l =', str(l))
print(dhat, '->', dhat_new)
dhat = dhat_new
ghat_list = [ghat_new.copy()]
r = 0
nb_updated_random += 1
print('the graph is updated by random generation',
nb_updated_random, 'times.')
nx.draw(ghat_new, labels=nx.get_node_attributes(ghat_new, 'atom'),
with_labels=True)
## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
plt.show()
found_random = True
break
l += 1
if not found_random: # l == l_max:
r += 1
is_iam_duplicate = False
else: # if the new distance is smaller than the max of D_k.
if len(dhat_new_list) == 1:
# check if the new distance is the same as one in D_k.
is_duplicate = False
for dis_tmp in dis_k[1:-1]:
if np.abs(dhat_new - dis_tmp) < epsilon:
is_duplicate = True
print('IAM: generated duplicate k nearest graph.')
break
if not is_duplicate:
if np.abs(dhat_new - dhat) < epsilon:
print('IAM: I am equal!')
# dhat = dhat_new
# ghat_list = [ghat_new.copy()]
is_iam_duplicate = True
else: else:
gtemp.remove_edge(node1, node2)
# nx.draw_networkx(gs)
# plt.show()
# nx.draw_networkx(gtemp)
# plt.show()
# nx.draw_networkx(gtemp)
# plt.show()
print('IAM: we got better k nearest neighbors!')
nb_updated_k_iam += 1
print('the k nearest neighbors are updated by IAM',
nb_updated_k_iam, 'times.')
# compute distance between \psi and the new generated graph.
knew = compute_kernel([gtemp] + Gn_median, gkernel, verbose=False)
dnew = dis_gstar(0, [1, 2], alpha, knew, withterm3=False)
# @todo: the new distance is smaller or also equal?
if dnew < dnew_best or np.abs(dnew_best - dnew) < epsilon:
if np.abs(dnew_best - dnew) < epsilon:
print('I am equal!')
dnew_best = dnew
gnew_best = gtemp.copy()
else:
print('\nI am smaller!')
print('l =', str(l))
print(dnew_best, '->', dnew)
dis_gs = [dnew] + dis_gs # add the new nearest distances.
Gs_nearest = [gtemp.copy()] + Gs_nearest # add the corresponding graphs.
sort_idx = np.argsort(dis_gs)
dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
Gn_nearest_median = [g.copy() for g in Gs_nearest]
dhat = dnew
nb_updated_random += 1
found = True # found better graph.
dis_k = dhat_new_list + dis_k[0:k-1] # add the new nearest distances.
Gk = [nx.convert_node_labels_to_integers(ghat_new_list[0].copy())] \
+ Gk[0:k-1] # add the corresponding graphs.
sort_idx = np.argsort(dis_k)
dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
Gk = [Gk[idx] for idx in sort_idx[0:k]]
if dhat_new < dhat:
print('IAM: I have smaller distance!')
print(str(dhat) + '->' + str(dhat_new))
dhat = dhat_new
ghat_list = [Gk[0].copy()]
r = 0
nb_updated_iam += 1
print('the graph is updated by IAM', nb_updated_iam, 'times.')
nx.draw(Gk[0], labels=nx.get_node_attributes(Gk[0], 'atom'),
with_labels=True)
## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
plt.show()
else:
is_iam_duplicate = True
else: # @todo: may not work.
dis_k = dhat_new_list + dis_k # add the new nearest distances.
Gk = [nx.convert_node_labels_to_integers(g.copy()) for g
in ghat_new_list] + Gk # add the corresponding graphs.
sort_idx = np.argsort(dis_k)
if len([i for i in sort_idx[0:k] if i < len(dhat_new_list)]) > 0:
print('We got new k nearest neighbors! Hurray!')
dis_k = [dis_k[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
# print(dis_k[-1])
Gk = [Gk[idx] for idx in sort_idx[0:k]]
nb_best = len(np.argwhere(dis_k == dis_k[0]).flatten().tolist())
if dhat_new < dhat:
print('I have smaller distance!')
print(str(dhat) + '->' + str(dhat_new))
dhat = dis_k[0]
idx_best_list = np.argwhere(dis_k == dhat_new).flatten().tolist()
ghat_list = [Gk[idx].copy() for idx in idx_best_list]
# for g in ghat_list:
## nx.draw_networkx(g)
## plt.show()
# draw_Letter_graph(g)
# print(g.nodes(data=True))
# print(g.edges(data=True))
r = 0 r = 0
print('the graph is updated by random generation',
nb_updated_random, 'times.')
nx.draw(gtemp, labels=nx.get_node_attributes(gtemp, 'atom'),
found = True
nb_updated_iam += 1
print('the graph is updated by IAM', nb_updated_iam, 'times.')
nx.draw(ghat_list[0], labels=nx.get_node_attributes(ghat_list[0], 'atom'),
with_labels=True) with_labels=True)
## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
plt.show() plt.show()
break
# nx.draw_networkx(gtemp)
# plt.show()
# print(gtemp.nodes(data=True))
# print(gtemp.edges(data=True))
l += 1
if l == l_max:
r += 1
else: # if the new distance is not equal to the old one.
dis_gs = dnew_list + dis_gs # add the new nearest distances.
Gs_nearest = [nx.convert_node_labels_to_integers(g).copy() for g
in g_tmp_list] + Gs_nearest # add the corresponding graphs.
sort_idx = np.argsort(dis_gs)
if len([i for i in sort_idx[0:k] if i < len(dnew_list)]) > 0:
print('We got new k nearest neighbors! Hurray!')
dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]] # the new k nearest distances.
# print(dis_gs[-1])
Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
nb_best = len(np.argwhere(dis_gs == dis_gs[0]).flatten().tolist())
if dnew_best < dhat:
print('I have smaller distance!')
print(str(dhat) + '->' + str(dis_gs[0]))
dhat = dis_gs[0]
idx_best_list = np.argwhere(dnew_list == dhat).flatten().tolist()
ghat_list = [g_tmp_list[idx].copy() for idx in idx_best_list]
# for g in ghat_list:
## nx.draw_networkx(g)
## plt.show()
# draw_Letter_graph(g)
# print(g.nodes(data=True))
# print(g.edges(data=True))
r = 0
found = True
nb_updated_iam += 1
print('the graph is updated by IAM', nb_updated_iam, 'times.')
nx.draw(ghat_list[0], labels=nx.get_node_attributes(ghat_list[0], 'atom'),
with_labels=True)
## plt.savefig("results/gk_iam/simple_two/xx" + str(i) + ".png", format="PNG")
plt.show()
else:
dis_gs = [dis_gs[idx] for idx in sort_idx[0:k]]
Gs_nearest = [Gs_nearest[idx] for idx in sort_idx[0:k]]
Gn_nearest_median = [g.copy() for g in Gs_nearest]
if not found:
r += 1
else:
dis_k = [dis_k[idx] for idx in sort_idx[0:k]]
Gk = [Gk[idx] for idx in sort_idx[0:k]]
# Gn_nearest_median = [g.copy() for g in Gn_nearest_init]
if not found:
r += 1
# old_dis = cur_dis # old_dis = cur_dis
# cur_dis = dnew_best # cur_dis = dnew_best
dis_list.append(dhat)
dis_of_each_itr.append(dhat)
itr_total += 1 itr_total += 1
print('\nthe k shortest distances are', dis_gs)
print('the shortest distances for previous iterations are', dis_list)
print('\nthe k shortest distances are', dis_k)
print('the shortest distances for previous iterations are', dis_of_each_itr)
print('\nthe graph is updated by IAM', nb_updated_iam, 'times, and by random generation', print('\nthe graph is updated by IAM', nb_updated_iam, 'times, and by random generation',
nb_updated_random, 'times.') nb_updated_random, 'times.')
print('distances in kernel space:', dis_list, '\n')
print('\nthe k nearest neighbors are updated by IAM', nb_updated_k_iam,
'times, and by random generation', nb_updated_k_random, 'times.')
print('distances in kernel space:', dis_of_each_itr, '\n')
return dhat, ghat_list, dis_list[-1], nb_updated_iam, nb_updated_random
return dhat, ghat_list, dis_of_each_itr[-1], \
nb_updated_iam, nb_updated_random, nb_updated_k_iam, nb_updated_k_random




############################################################################### ###############################################################################


+ 81
- 66
preimage/iam.py View File

@@ -23,7 +23,8 @@ from pygraph.utils.utils import graph_isIdentical, get_node_labels, get_edge_lab
def iam_moreGraphsAsInit_tryAllPossibleBestGraphs(Gn_median, Gn_candidate, def iam_moreGraphsAsInit_tryAllPossibleBestGraphs(Gn_median, Gn_candidate,
c_ei=3, c_er=3, c_es=1, ite_max=50, epsilon=0.001, c_ei=3, c_er=3, c_es=1, ite_max=50, epsilon=0.001,
node_label='atom', edge_label='bond_type', node_label='atom', edge_label='bond_type',
connected=False, removeNodes=True, AllBestInit=True,
connected=False, removeNodes=True, allBestInit=False, allBestNodes=False,
allBestEdges=False,
params_ged={'ged_cost': 'CHEM_1', 'ged_method': 'IPFP', 'saveGXL': 'benoit'}): params_ged={'ged_cost': 'CHEM_1', 'ged_method': 'IPFP', 'saveGXL': 'benoit'}):
"""See my name, then you know what I do. """See my name, then you know what I do.
""" """
@@ -74,24 +75,37 @@ def iam_moreGraphsAsInit_tryAllPossibleBestGraphs(Gn_median, Gn_candidate,
label_list.append(label_r) label_list.append(label_r)
# get the best labels. # get the best labels.
idx_max = np.argwhere(h_i0_list == np.max(h_i0_list)).flatten().tolist() idx_max = np.argwhere(h_i0_list == np.max(h_i0_list)).flatten().tolist()
nlabel_best = [label_list[idx] for idx in idx_max]
# generate "best" graphs with regard to "best" node labels.
G_new_list_nd = []
for g in G_new_list: # @todo: seems it can be simplified. The G_new_list will only contain 1 graph for now.
for nl in nlabel_best:
g_tmp = g.copy()
if nl == label_r:
g_tmp.remove_node(nd)
else:
g_tmp.nodes[nd][node_label] = nl
G_new_list_nd.append(g_tmp)
# nx.draw_networkx(g_tmp)
# import matplotlib.pyplot as plt
# plt.show()
# print(g_tmp.nodes(data=True))
# print(g_tmp.edges(data=True))
G_new_list = [ggg.copy() for ggg in G_new_list_nd]

if allBestNodes: # choose all best graphs.
nlabel_best = [label_list[idx] for idx in idx_max]
# generate "best" graphs with regard to "best" node labels.
G_new_list_nd = []
for g in G_new_list: # @todo: seems it can be simplified. The G_new_list will only contain 1 graph for now.
for nl in nlabel_best:
g_tmp = g.copy()
if nl == label_r:
g_tmp.remove_node(nd)
else:
g_tmp.nodes[nd][node_label] = nl
G_new_list_nd.append(g_tmp)
# nx.draw_networkx(g_tmp)
# import matplotlib.pyplot as plt
# plt.show()
# print(g_tmp.nodes(data=True))
# print(g_tmp.edges(data=True))
G_new_list = [ggg.copy() for ggg in G_new_list_nd]
else:
# choose one of the best randomly.
h_ij0_max = h_i0_list[idx_max[0]]
idx_rdm = random.randint(0, len(idx_max) - 1)
best_label = label_list[idx_max[idx_rdm]]
# check whether a_ij is 0 or 1.
g_new = G_new_list[0]
if best_label == label_r:
g_new.remove_node(nd)
else:
g_new.nodes[nd][node_label] = best_label
G_new_list = [g_new]
else: # labels are non-symbolic else: # labels are non-symbolic
for ndi, (nd, _) in enumerate(G.nodes(data=True)): for ndi, (nd, _) in enumerate(G.nodes(data=True)):
Si_norm = 0 Si_norm = 0
@@ -148,56 +162,57 @@ def iam_moreGraphsAsInit_tryAllPossibleBestGraphs(Gn_median, Gn_candidate,
# label_list.append(label_r) # label_list.append(label_r)
# get the best labels. # get the best labels.
# choose all best graphs.
idx_max = np.argwhere(h_ij0_list == np.max(h_ij0_list)).flatten().tolist() idx_max = np.argwhere(h_ij0_list == np.max(h_ij0_list)).flatten().tolist()
elabel_best = [label_list[idx] for idx in idx_max]
h_ij0_max = [h_ij0_list[idx] for idx in idx_max]
# generate "best" graphs with regard to "best" node labels.
G_new_list_ed = []
for g_tmp in g_tmp_list: # @todo: seems it can be simplified. The G_new_list will only contain 1 graph for now.
for idxl, el in enumerate(elabel_best):
g_tmp_copy = g_tmp.copy()
# check whether a_ij is 0 or 1.
sij_norm = 0
for idx, g in enumerate(Gn_median):
pi_i = pi_p_forward[idx][nd1i]
pi_j = pi_p_forward[idx][nd2i]
if g.has_node(pi_i) and g.has_node(pi_j) and \
g.has_edge(pi_i, pi_j):
sij_norm += 1
if h_ij0_max[idxl] > len(Gn_median) * c_er / c_es + \
sij_norm * (1 - (c_er + c_ei) / c_es):
if not g_tmp_copy.has_edge(nd1, nd2):
g_tmp_copy.add_edge(nd1, nd2)
g_tmp_copy.edges[nd1, nd2][edge_label] = elabel_best[idxl]
else:
if g_tmp_copy.has_edge(nd1, nd2):
g_tmp_copy.remove_edge(nd1, nd2)
G_new_list_ed.append(g_tmp_copy)
g_tmp_list = [ggg.copy() for ggg in G_new_list_ed]
if allBestEdges: # choose all best graphs.
elabel_best = [label_list[idx] for idx in idx_max]
h_ij0_max = [h_ij0_list[idx] for idx in idx_max]
# generate "best" graphs with regard to "best" node labels.
G_new_list_ed = []
for g_tmp in g_tmp_list: # @todo: seems it can be simplified. The G_new_list will only contain 1 graph for now.
for idxl, el in enumerate(elabel_best):
g_tmp_copy = g_tmp.copy()
# check whether a_ij is 0 or 1.
sij_norm = 0
for idx, g in enumerate(Gn_median):
pi_i = pi_p_forward[idx][nd1i]
pi_j = pi_p_forward[idx][nd2i]
if g.has_node(pi_i) and g.has_node(pi_j) and \
g.has_edge(pi_i, pi_j):
sij_norm += 1
if h_ij0_max[idxl] > len(Gn_median) * c_er / c_es + \
sij_norm * (1 - (c_er + c_ei) / c_es):
if not g_tmp_copy.has_edge(nd1, nd2):
g_tmp_copy.add_edge(nd1, nd2)
g_tmp_copy.edges[nd1, nd2][edge_label] = elabel_best[idxl]
else:
if g_tmp_copy.has_edge(nd1, nd2):
g_tmp_copy.remove_edge(nd1, nd2)
G_new_list_ed.append(g_tmp_copy)
g_tmp_list = [ggg.copy() for ggg in G_new_list_ed]
else: # choose one of the best randomly.
h_ij0_max = h_ij0_list[idx_max[0]]
idx_rdm = random.randint(0, len(idx_max) - 1)
best_label = label_list[idx_max[idx_rdm]]
# check whether a_ij is 0 or 1.
sij_norm = 0
for idx, g in enumerate(Gn_median):
pi_i = pi_p_forward[idx][nd1i]
pi_j = pi_p_forward[idx][nd2i]
if g.has_node(pi_i) and g.has_node(pi_j) and g.has_edge(pi_i, pi_j):
sij_norm += 1
if h_ij0_max > len(Gn_median) * c_er / c_es + sij_norm * (1 - (c_er + c_ei) / c_es):
if not g_new.has_edge(nd1, nd2):
g_new.add_edge(nd1, nd2)
g_new.edges[nd1, nd2][edge_label] = best_label
else:
if g_new.has_edge(nd1, nd2):
g_new.remove_edge(nd1, nd2)
g_tmp_list = [g_new]
G_new_list_edge += g_tmp_list G_new_list_edge += g_tmp_list
G_new_list = [ggg.copy() for ggg in G_new_list_edge] G_new_list = [ggg.copy() for ggg in G_new_list_edge]
# # choose one of the best randomly.
# idx_max = np.argwhere(h_ij0_list == np.max(h_ij0_list)).flatten().tolist()
# h_ij0_max = h_ij0_list[idx_max[0]]
# idx_rdm = random.randint(0, len(idx_max) - 1)
# best_label = label_list[idx_max[idx_rdm]]
#
# # check whether a_ij is 0 or 1.
# sij_norm = 0
# for idx, g in enumerate(Gn_median):
# pi_i = pi_p_forward[idx][nd1i]
# pi_j = pi_p_forward[idx][nd2i]
# if g.has_node(pi_i) and g.has_node(pi_j) and g.has_edge(pi_i, pi_j):
# sij_norm += 1
# if h_ij0_max > len(Gn_median) * c_er / c_es + sij_norm * (1 - (c_er + c_ei) / c_es):
# if not g_new.has_edge(nd1, nd2):
# g_new.add_edge(nd1, nd2)
# g_new.edges[nd1, nd2][edge_label] = best_label
# else:
# if g_new.has_edge(nd1, nd2):
# g_new.remove_edge(nd1, nd2)
else: # if edges are unlabeled else: # if edges are unlabeled
# @todo: is this even right? G or g_tmp? check if the new one is right # @todo: is this even right? G or g_tmp? check if the new one is right
# @todo: works only for undirected graphs. # @todo: works only for undirected graphs.
@@ -362,7 +377,7 @@ def iam_moreGraphsAsInit_tryAllPossibleBestGraphs(Gn_median, Gn_candidate,
dis_list, pi_forward_all = median_distance(Gn_candidate, Gn_median, dis_list, pi_forward_all = median_distance(Gn_candidate, Gn_median,
**params_ged) **params_ged)
# find all smallest distances. # find all smallest distances.
if AllBestInit: # try all best init graphs.
if allBestInit: # try all best init graphs.
idx_min_list = range(len(dis_list)) idx_min_list = range(len(dis_list))
dis_min = dis_list dis_min = dis_list
else: else:


+ 28
- 13
preimage/test_random_mutag.py View File

@@ -34,7 +34,7 @@ def test_preimage_mix_2combination_all_pairs():
lmbda = 0.03 # termination probalility lmbda = 0.03 # termination probalility
r_max = 10 # iteration limit for pre-image. r_max = 10 # iteration limit for pre-image.
l_max = 500 # update limit for random generation l_max = 500 # update limit for random generation
alpha_range = np.linspace(0.7, 1, 4)
alpha_range = np.linspace(0.5, 0.5, 1)
k = 5 # k nearest neighbors k = 5 # k nearest neighbors
epsilon = 1e-6 epsilon = 1e-6
# parameters for GED function # parameters for GED function
@@ -115,13 +115,16 @@ def test_preimage_mix_2combination_all_pairs():
sod_gs_min_list = [] sod_gs_min_list = []
nb_updated_list_iam = [] nb_updated_list_iam = []
nb_updated_list_random = [] nb_updated_list_random = []
nb_updated_k_list_iam = []
nb_updated_k_list_random = []
g_best = [] g_best = []
# for each alpha # for each alpha
for alpha in alpha_range: for alpha in alpha_range:
print('\n-------------------------------------------------------\n') print('\n-------------------------------------------------------\n')
print('alpha =', alpha) print('alpha =', alpha)
time0 = time.time() time0 = time.time()
dhat, ghat_list, sod_ks, nb_updated_iam, nb_updated_random = \
dhat, ghat_list, dis_of_each_itr, nb_updated_iam, nb_updated_random, \
nb_updated_k_iam, nb_updated_k_random = \
preimage_iam_random_mix(Gn, [g1, g2], preimage_iam_random_mix(Gn, [g1, g2],
[alpha, 1 - alpha], range(len(Gn), len(Gn) + 2), km, k, r_max, [alpha, 1 - alpha], range(len(Gn), len(Gn) + 2), km, k, r_max,
l_max, gkernel, epsilon=epsilon, l_max, gkernel, epsilon=epsilon,
@@ -136,7 +139,9 @@ def test_preimage_mix_2combination_all_pairs():
dis_ks_min_list.append(dhat) dis_ks_min_list.append(dhat)
g_best.append(ghat_list) g_best.append(ghat_list)
nb_updated_list_iam.append(nb_updated_iam) nb_updated_list_iam.append(nb_updated_iam)
nb_updated_list_random.append(nb_updated_random)
nb_updated_list_random.append(nb_updated_random)
nb_updated_k_list_iam.append(nb_updated_k_iam)
nb_updated_k_list_random.append(nb_updated_k_random)
# show best graphs and save them to file. # show best graphs and save them to file.
for idx, item in enumerate(alpha_range): for idx, item in enumerate(alpha_range):
@@ -168,9 +173,13 @@ def test_preimage_mix_2combination_all_pairs():
print('\nsods in graph space: ', sod_gs_list) print('\nsods in graph space: ', sod_gs_list)
print('\nsmallest sod in graph space for each alpha: ', sod_gs_min_list) print('\nsmallest sod in graph space for each alpha: ', sod_gs_min_list)
print('\nsmallest distance in kernel space for each alpha: ', dis_ks_min_list) print('\nsmallest distance in kernel space for each alpha: ', dis_ks_min_list)
print('\nnumber of updates for each alpha by IAM: ', nb_updated_list_iam)
print('\nnumber of updates for each alpha by random generation: ',
print('\nnumber of updates of the best graph for each alpha by IAM: ', nb_updated_list_iam)
print('\nnumber of updates of the best graph for each alpha by random generation: ',
nb_updated_list_random) nb_updated_list_random)
print('\nnumber of updates of k nearest graphs for each alpha by IAM: ',
nb_updated_k_list_iam)
print('\nnumber of updates of k nearest graphs for each alpha by random generation: ',
nb_updated_k_list_random)
print('\ntimes:', time_list) print('\ntimes:', time_list)
nb_update_mat_iam[idx1, idx2] = nb_updated_list_iam[0] nb_update_mat_iam[idx1, idx2] = nb_updated_list_iam[0]
nb_update_mat_random[idx1, idx2] = nb_updated_list_random[0] nb_update_mat_random[idx1, idx2] = nb_updated_list_random[0]
@@ -196,8 +205,8 @@ def test_gkiam_2combination_all_pairs():
lmbda = 0.03 # termination probalility lmbda = 0.03 # termination probalility
r_max = 10 # iteration limit for pre-image. r_max = 10 # iteration limit for pre-image.
alpha_range = np.linspace(1, 1, 1)
k = 5 # k nearest neighbors
alpha_range = np.linspace(0.5, 0.5, 1)
k = 10 # k nearest neighbors
epsilon = 1e-6 epsilon = 1e-6
# parameters for GED function # parameters for GED function
ged_cost='CHEM_1' ged_cost='CHEM_1'
@@ -274,14 +283,16 @@ def test_gkiam_2combination_all_pairs():
dis_ks_min_list = [] dis_ks_min_list = []
sod_gs_list = [] sod_gs_list = []
sod_gs_min_list = [] sod_gs_min_list = []
nb_updated_list = []
nb_updated_list = []
nb_updated_k_list = []
g_best = [] g_best = []
# for each alpha # for each alpha
for alpha in alpha_range: for alpha in alpha_range:
print('\n-------------------------------------------------------\n') print('\n-------------------------------------------------------\n')
print('alpha =', alpha) print('alpha =', alpha)
time0 = time.time() time0 = time.time()
dhat, ghat_list, sod_ks, nb_updated = gk_iam_nearest_multi(Gn, [g1, g2],
dhat, ghat_list, sod_ks, nb_updated, nb_updated_k = \
gk_iam_nearest_multi(Gn, [g1, g2],
[alpha, 1 - alpha], range(len(Gn), len(Gn) + 2), km, k, r_max, [alpha, 1 - alpha], range(len(Gn), len(Gn) + 2), km, k, r_max,
gkernel, epsilon=epsilon, gkernel, epsilon=epsilon,
params_iam={'c_ei': c_ei, 'c_er': c_er, 'c_es': c_es, params_iam={'c_ei': c_ei, 'c_er': c_er, 'c_es': c_es,
@@ -294,7 +305,8 @@ def test_gkiam_2combination_all_pairs():
time_list.append(time_total) time_list.append(time_total)
dis_ks_min_list.append(dhat) dis_ks_min_list.append(dhat)
g_best.append(ghat_list) g_best.append(ghat_list)
nb_updated_list.append(nb_updated)
nb_updated_list.append(nb_updated)
nb_updated_k_list.append(nb_updated_k)
# show best graphs and save them to file. # show best graphs and save them to file.
for idx, item in enumerate(alpha_range): for idx, item in enumerate(alpha_range):
@@ -326,7 +338,10 @@ def test_gkiam_2combination_all_pairs():
print('\nsods in graph space: ', sod_gs_list) print('\nsods in graph space: ', sod_gs_list)
print('\nsmallest sod in graph space for each alpha: ', sod_gs_min_list) print('\nsmallest sod in graph space for each alpha: ', sod_gs_min_list)
print('\nsmallest distance in kernel space for each alpha: ', dis_ks_min_list) print('\nsmallest distance in kernel space for each alpha: ', dis_ks_min_list)
print('\nnumber of updates for each alpha: ', nb_updated_list)
print('\nnumber of updates of the best graph for each alpha: ',
nb_updated_list)
print('\nnumber of updates of the k nearest graphs for each alpha: ',
nb_updated_k_list)
print('\ntimes:', time_list) print('\ntimes:', time_list)
nb_update_mat[idx1, idx2] = nb_updated_list[0] nb_update_mat[idx1, idx2] = nb_updated_list[0]
@@ -595,5 +610,5 @@ if __name__ == '__main__':
# random pre-image paper.) # random pre-image paper.)
# test_random_preimage_2combination() # test_random_preimage_2combination()
# test_gkiam_2combination() # test_gkiam_2combination()
# test_gkiam_2combination_all_pairs()
test_preimage_mix_2combination_all_pairs()
test_gkiam_2combination_all_pairs()
# test_preimage_mix_2combination_all_pairs()

Loading…
Cancel
Save