You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

fitDistance.py 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Oct 16 14:20:06 2019
  5. @author: ljia
  6. """
  7. import numpy as np
  8. from tqdm import tqdm
  9. from itertools import combinations_with_replacement, combinations
  10. import multiprocessing
  11. from multiprocessing import Pool
  12. from functools import partial
  13. import time
  14. import random
  15. from scipy import optimize
  16. from scipy.optimize import minimize
  17. import cvxpy as cp
  18. import sys
  19. sys.path.insert(0, "../")
  20. from preimage.ged import GED, get_nb_edit_operations, get_nb_edit_operations_letter
  21. from preimage.utils import kernel_distance_matrix
  22. def fit_GED_to_kernel_distance(Gn, node_label, edge_label, gkernel, itr_max,
  23. params_ged={'lib': 'gedlibpy', 'cost': 'CONSTANT',
  24. 'method': 'IPFP', 'stabilizer': None},
  25. init_costs=[3, 3, 1, 3, 3, 1],
  26. dataset='monoterpenoides',
  27. parallel=True):
  28. dataset = dataset.lower()
  29. # c_vi, c_vr, c_vs, c_ei, c_er, c_es or parts of them.
  30. # random.seed(1)
  31. # cost_rdm = random.sample(range(1, 10), 6)
  32. # init_costs = cost_rdm + [0]
  33. # init_costs = cost_rdm
  34. # init_costs = [3, 3, 1, 3, 3, 1]
  35. # init_costs = [i * 0.01 for i in cost_rdm] + [0]
  36. # init_costs = [0.2, 0.2, 0.2, 0.2, 0.2, 0]
  37. # init_costs = [0, 0, 0.9544, 0.026, 0.0196, 0]
  38. # init_costs = [0.008429912251810438, 0.025461055985319694, 0.2047320869225948, 0.004148727085832133, 0.0, 0]
  39. # idx_cost_nonzeros = [i for i, item in enumerate(edit_costs) if item != 0]
  40. # compute distances in feature space.
  41. dis_k_mat, _, _, _ = kernel_distance_matrix(Gn, node_label, edge_label, gkernel=gkernel)
  42. dis_k_vec = []
  43. for i in range(len(dis_k_mat)):
  44. # for j in range(i, len(dis_k_mat)):
  45. for j in range(i + 1, len(dis_k_mat)):
  46. dis_k_vec.append(dis_k_mat[i, j])
  47. dis_k_vec = np.array(dis_k_vec)
  48. # init ged.
  49. print('\ninitial:')
  50. time0 = time.time()
  51. params_ged['dataset'] = dataset
  52. params_ged['edit_cost_constant'] = init_costs
  53. ged_vec_init, ged_mat, n_edit_operations = compute_geds(Gn, params_ged,
  54. dataset,
  55. parallel=parallel)
  56. residual_list = [np.sqrt(np.sum(np.square(np.array(ged_vec_init) - dis_k_vec)))]
  57. time_list = [time.time() - time0]
  58. edit_cost_list = [init_costs]
  59. nb_cost_mat = np.array(n_edit_operations)
  60. nb_cost_mat_list = [nb_cost_mat]
  61. print('edit_costs:', init_costs)
  62. print('residual_list:', residual_list)
  63. for itr in range(itr_max):
  64. print('\niteration', itr)
  65. time0 = time.time()
  66. # "fit" geds to distances in feature space by tuning edit costs using the
  67. # Least Squares Method.
  68. edit_costs_new, residual = update_costs(nb_cost_mat, dis_k_vec,
  69. dataset=dataset, cost=params_ged['cost'])
  70. for i in range(len(edit_costs_new)):
  71. if -1e-9 <= edit_costs_new[i] <= 1e-9:
  72. edit_costs_new[i] = 0
  73. if edit_costs_new[i] < 0:
  74. raise ValueError('The edit cost is negative.')
  75. # for i in range(len(edit_costs_new)):
  76. # if edit_costs_new[i] < 0:
  77. # edit_costs_new[i] = 0
  78. # compute new GEDs and numbers of edit operations.
  79. params_ged['edit_cost_constant'] = edit_costs_new # np.array([edit_costs_new[0], edit_costs_new[1], 0.75])
  80. ged_vec, ged_mat, n_edit_operations = compute_geds(Gn, params_ged,
  81. dataset,
  82. parallel=parallel)
  83. residual_list.append(np.sqrt(np.sum(np.square(np.array(ged_vec) - dis_k_vec))))
  84. time_list.append(time.time() - time0)
  85. edit_cost_list.append(edit_costs_new)
  86. nb_cost_mat = np.array(n_edit_operations)
  87. nb_cost_mat_list.append(nb_cost_mat)
  88. print('edit_costs:', edit_costs_new)
  89. print('residual_list:', residual_list)
  90. return edit_costs_new, residual_list, edit_cost_list, dis_k_mat, ged_mat, \
  91. time_list, nb_cost_mat_list
  92. def compute_geds(Gn, params_ged, dataset, parallel=False):
  93. get_nb_eo = get_nb_edit_operations_letter if dataset == 'letter' else get_nb_edit_operations
  94. ged_mat = np.zeros((len(Gn), len(Gn)))
  95. if parallel:
  96. # print('parallel')
  97. # len_itr = int(len(Gn) * (len(Gn) + 1) / 2)
  98. len_itr = int(len(Gn) * (len(Gn) - 1) / 2)
  99. ged_vec = [0 for i in range(len_itr)]
  100. n_edit_operations = [0 for i in range(len_itr)]
  101. # itr = combinations_with_replacement(range(0, len(Gn)), 2)
  102. itr = combinations(range(0, len(Gn)), 2)
  103. n_jobs = multiprocessing.cpu_count()
  104. if len_itr < 100 * n_jobs:
  105. chunksize = int(len_itr / n_jobs) + 1
  106. else:
  107. chunksize = 100
  108. def init_worker(gn_toshare):
  109. global G_gn
  110. G_gn = gn_toshare
  111. do_partial = partial(_wrapper_compute_ged_parallel, params_ged, get_nb_eo)
  112. pool = Pool(processes=n_jobs, initializer=init_worker, initargs=(Gn,))
  113. iterator = tqdm(pool.imap_unordered(do_partial, itr, chunksize),
  114. desc='computing GEDs', file=sys.stdout)
  115. # iterator = pool.imap_unordered(do_partial, itr, chunksize)
  116. for i, j, dis, n_eo_tmp in iterator:
  117. idx_itr = int(len(Gn) * i + j - (i + 1) * (i + 2) / 2)
  118. ged_vec[idx_itr] = dis
  119. ged_mat[i][j] = dis
  120. ged_mat[j][i] = dis
  121. n_edit_operations[idx_itr] = n_eo_tmp
  122. # print('\n-------------------------------------------')
  123. # print(i, j, idx_itr, dis)
  124. pool.close()
  125. pool.join()
  126. else:
  127. ged_vec = []
  128. n_edit_operations = []
  129. for i in tqdm(range(len(Gn)), desc='computing GEDs', file=sys.stdout):
  130. # for i in range(len(Gn)):
  131. for j in range(i + 1, len(Gn)):
  132. dis, pi_forward, pi_backward = GED(Gn[i], Gn[j], **params_ged)
  133. ged_vec.append(dis)
  134. ged_mat[i][j] = dis
  135. ged_mat[j][i] = dis
  136. n_eo_tmp = get_nb_eo(Gn[i], Gn[j], pi_forward, pi_backward)
  137. n_edit_operations.append(n_eo_tmp)
  138. return ged_vec, ged_mat, n_edit_operations
  139. def _wrapper_compute_ged_parallel(params_ged, get_nb_eo, itr):
  140. i = itr[0]
  141. j = itr[1]
  142. dis, n_eo_tmp = _compute_ged_parallel(G_gn[i], G_gn[j], params_ged, get_nb_eo)
  143. return i, j, dis, n_eo_tmp
  144. def _compute_ged_parallel(g1, g2, params_ged, get_nb_eo):
  145. dis, pi_forward, pi_backward = GED(g1, g2, **params_ged)
  146. n_eo_tmp = get_nb_eo(g1, g2, pi_forward, pi_backward) # [0,0,0,0,0,0]
  147. return dis, n_eo_tmp
  148. def update_costs(nb_cost_mat, dis_k_vec, dataset='monoterpenoides',
  149. cost='CONSTANT', rw_constraints='2constraints'):
  150. if dataset.lower() == 'letter':
  151. if cost == 'LETTER':
  152. pass
  153. # # method 1: set alpha automatically, just tune c_vir and c_eir by
  154. # # LMS using cvxpy.
  155. # alpha = 0.5
  156. # coeff = 100 # np.max(alpha * nb_cost_mat[:,4] / dis_k_vec)
  157. ## if np.count_nonzero(nb_cost_mat[:,4]) == 0:
  158. ## alpha = 0.75
  159. ## else:
  160. ## alpha = np.min([dis_k_vec / c_vs for c_vs in nb_cost_mat[:,4] if c_vs != 0])
  161. ## alpha = alpha * 0.99
  162. # param_vir = alpha * (nb_cost_mat[:,0] + nb_cost_mat[:,1])
  163. # param_eir = (1 - alpha) * (nb_cost_mat[:,4] + nb_cost_mat[:,5])
  164. # nb_cost_mat_new = np.column_stack((param_vir, param_eir))
  165. # dis_new = coeff * dis_k_vec - alpha * nb_cost_mat[:,3]
  166. #
  167. # x = cp.Variable(nb_cost_mat_new.shape[1])
  168. # cost = cp.sum_squares(nb_cost_mat_new * x - dis_new)
  169. # constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
  170. # prob = cp.Problem(cp.Minimize(cost), constraints)
  171. # prob.solve()
  172. # edit_costs_new = x.value
  173. # edit_costs_new = np.array([edit_costs_new[0], edit_costs_new[1], alpha])
  174. # residual = np.sqrt(prob.value)
  175. # # method 2: tune c_vir, c_eir and alpha by nonlinear programming by
  176. # # scipy.optimize.minimize.
  177. # w0 = nb_cost_mat[:,0] + nb_cost_mat[:,1]
  178. # w1 = nb_cost_mat[:,4] + nb_cost_mat[:,5]
  179. # w2 = nb_cost_mat[:,3]
  180. # w3 = dis_k_vec
  181. # func_min = lambda x: np.sum((w0 * x[0] * x[3] + w1 * x[1] * (1 - x[2]) \
  182. # + w2 * x[2] - w3 * x[3]) ** 2)
  183. # bounds = ((0, None), (0., None), (0.5, 0.5), (0, None))
  184. # res = minimize(func_min, [0.9, 1.7, 0.75, 10], bounds=bounds)
  185. # edit_costs_new = res.x[0:3]
  186. # residual = res.fun
  187. # method 3: tune c_vir, c_eir and alpha by nonlinear programming using cvxpy.
  188. # # method 4: tune c_vir, c_eir and alpha by QP function
  189. # # scipy.optimize.least_squares. An initial guess is required.
  190. # w0 = nb_cost_mat[:,0] + nb_cost_mat[:,1]
  191. # w1 = nb_cost_mat[:,4] + nb_cost_mat[:,5]
  192. # w2 = nb_cost_mat[:,3]
  193. # w3 = dis_k_vec
  194. # func = lambda x: (w0 * x[0] * x[3] + w1 * x[1] * (1 - x[2]) \
  195. # + w2 * x[2] - w3 * x[3]) ** 2
  196. # res = optimize.root(func, [0.9, 1.7, 0.75, 100])
  197. # edit_costs_new = res.x
  198. # residual = None
  199. elif cost == 'LETTER2':
  200. # # 1. if c_vi != c_vr, c_ei != c_er.
  201. # nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  202. # x = cp.Variable(nb_cost_mat_new.shape[1])
  203. # cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  204. ## # 1.1 no constraints.
  205. ## constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
  206. # # 1.2 c_vs <= c_vi + c_vr.
  207. # constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
  208. # np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  209. ## # 2. if c_vi == c_vr, c_ei == c_er.
  210. ## nb_cost_mat_new = nb_cost_mat[:,[0,3,4]]
  211. ## nb_cost_mat_new[:,0] += nb_cost_mat[:,1]
  212. ## nb_cost_mat_new[:,2] += nb_cost_mat[:,5]
  213. ## x = cp.Variable(nb_cost_mat_new.shape[1])
  214. ## cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  215. ## # 2.1 no constraints.
  216. ## constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
  217. ### # 2.2 c_vs <= c_vi + c_vr.
  218. ### constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
  219. ### np.array([2.0, -1.0, 0.0]).T@x >= 0.0]
  220. #
  221. # prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  222. # prob.solve()
  223. # edit_costs_new = [x.value[0], x.value[0], x.value[1], x.value[2], x.value[2]]
  224. # edit_costs_new = np.array(edit_costs_new)
  225. # residual = np.sqrt(prob.value)
  226. if rw_constraints == 'inequality':
  227. # c_vs <= c_vi + c_vr.
  228. nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  229. x = cp.Variable(nb_cost_mat_new.shape[1])
  230. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  231. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
  232. np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  233. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  234. prob.solve()
  235. edit_costs_new = x.value
  236. residual = np.sqrt(prob.value)
  237. elif rw_constraints == '2constraints':
  238. # c_vs <= c_vi + c_vr and c_vi == c_vr, c_ei == c_er.
  239. nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  240. x = cp.Variable(nb_cost_mat_new.shape[1])
  241. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  242. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
  243. np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0,
  244. np.array([1.0, -1.0, 0.0, 0.0, 0.0]).T@x == 0.0,
  245. np.array([0.0, 0.0, 0.0, 1.0, -1.0]).T@x == 0.0]
  246. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  247. prob.solve()
  248. edit_costs_new = x.value
  249. residual = np.sqrt(prob.value)
  250. # elif method == 'inequality_modified':
  251. # # c_vs <= c_vi + c_vr.
  252. # nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  253. # x = cp.Variable(nb_cost_mat_new.shape[1])
  254. # cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  255. # constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
  256. # np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  257. # prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  258. # prob.solve()
  259. # # use same costs for insertion and removal rather than the fitted costs.
  260. # edit_costs_new = [x.value[0], x.value[0], x.value[1], x.value[2], x.value[2]]
  261. # edit_costs_new = np.array(edit_costs_new)
  262. # residual = np.sqrt(prob.value)
  263. else:
  264. # # method 1: simple least square method.
  265. # edit_costs_new, residual, _, _ = np.linalg.lstsq(nb_cost_mat, dis_k_vec,
  266. # rcond=None)
  267. # # method 2: least square method with x_i >= 0.
  268. # edit_costs_new, residual = optimize.nnls(nb_cost_mat, dis_k_vec)
  269. # method 3: solve as a quadratic program with constraints.
  270. # P = np.dot(nb_cost_mat.T, nb_cost_mat)
  271. # q_T = -2 * np.dot(dis_k_vec.T, nb_cost_mat)
  272. # G = -1 * np.identity(nb_cost_mat.shape[1])
  273. # h = np.array([0 for i in range(nb_cost_mat.shape[1])])
  274. # A = np.array([1 for i in range(nb_cost_mat.shape[1])])
  275. # b = 1
  276. # x = cp.Variable(nb_cost_mat.shape[1])
  277. # prob = cp.Problem(cp.Minimize(cp.quad_form(x, P) + q_T@x),
  278. # [G@x <= h])
  279. # prob.solve()
  280. # edit_costs_new = x.value
  281. # residual = prob.value - np.dot(dis_k_vec.T, dis_k_vec)
  282. # G = -1 * np.identity(nb_cost_mat.shape[1])
  283. # h = np.array([0 for i in range(nb_cost_mat.shape[1])])
  284. x = cp.Variable(nb_cost_mat.shape[1])
  285. cost_fun = cp.sum_squares(nb_cost_mat * x - dis_k_vec)
  286. constraints = [x >= [0.0 for i in range(nb_cost_mat.shape[1])],
  287. # np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  288. np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
  289. np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
  290. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  291. prob.solve()
  292. edit_costs_new = x.value
  293. residual = np.sqrt(prob.value)
  294. # method 4:
  295. return edit_costs_new, residual
  296. if __name__ == '__main__':
  297. print('check test_fitDistance.py')

A Python package for graph kernels, graph edit distances and graph pre-image problem.