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 20 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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, get_nb_edit_operations_nonsymbolic
  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', Kmatrix=None,
  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,
  42. Kmatrix=Kmatrix, gkernel=gkernel)
  43. dis_k_vec = []
  44. for i in range(len(dis_k_mat)):
  45. # for j in range(i, len(dis_k_mat)):
  46. for j in range(i + 1, len(dis_k_mat)):
  47. dis_k_vec.append(dis_k_mat[i, j])
  48. dis_k_vec = np.array(dis_k_vec)
  49. # init ged.
  50. print('\ninitial:')
  51. time0 = time.time()
  52. params_ged['dataset'] = dataset
  53. params_ged['edit_cost_constant'] = init_costs
  54. ged_vec_init, ged_mat, n_edit_operations = compute_geds(Gn, params_ged,
  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. np.savez('results/xp_fit_method/fit_data_debug' + str(itr) + '.gm',
  69. nb_cost_mat=nb_cost_mat, dis_k_vec=dis_k_vec,
  70. n_edit_operations=n_edit_operations, ged_vec_init=ged_vec_init,
  71. ged_mat=ged_mat)
  72. edit_costs_new, residual = update_costs(nb_cost_mat, dis_k_vec,
  73. dataset=dataset, cost=params_ged['cost'])
  74. for i in range(len(edit_costs_new)):
  75. if -1e-9 <= edit_costs_new[i] <= 1e-9:
  76. edit_costs_new[i] = 0
  77. if edit_costs_new[i] < 0:
  78. raise ValueError('The edit cost is negative.')
  79. # for i in range(len(edit_costs_new)):
  80. # if edit_costs_new[i] < 0:
  81. # edit_costs_new[i] = 0
  82. # compute new GEDs and numbers of edit operations.
  83. params_ged['edit_cost_constant'] = edit_costs_new # np.array([edit_costs_new[0], edit_costs_new[1], 0.75])
  84. ged_vec, ged_mat, n_edit_operations = compute_geds(Gn, params_ged,
  85. parallel=parallel)
  86. residual_list.append(np.sqrt(np.sum(np.square(np.array(ged_vec) - dis_k_vec))))
  87. time_list.append(time.time() - time0)
  88. edit_cost_list.append(edit_costs_new)
  89. nb_cost_mat = np.array(n_edit_operations)
  90. nb_cost_mat_list.append(nb_cost_mat)
  91. print('edit_costs:', edit_costs_new)
  92. print('residual_list:', residual_list)
  93. return edit_costs_new, residual_list, edit_cost_list, dis_k_mat, ged_mat, \
  94. time_list, nb_cost_mat_list
  95. def compute_geds(Gn, params_ged, parallel=False):
  96. edit_cost_name = params_ged['cost']
  97. if edit_cost_name == 'LETTER' or edit_cost_name == 'LETTER2':
  98. get_nb_eo = get_nb_edit_operations_letter
  99. elif edit_cost_name == 'NON_SYMBOLIC':
  100. get_nb_eo = get_nb_edit_operations_nonsymbolic
  101. else:
  102. get_nb_eo = get_nb_edit_operations
  103. ged_mat = np.zeros((len(Gn), len(Gn)))
  104. if parallel:
  105. # print('parallel')
  106. # len_itr = int(len(Gn) * (len(Gn) + 1) / 2)
  107. len_itr = int(len(Gn) * (len(Gn) - 1) / 2)
  108. ged_vec = [0 for i in range(len_itr)]
  109. n_edit_operations = [0 for i in range(len_itr)]
  110. # itr = combinations_with_replacement(range(0, len(Gn)), 2)
  111. itr = combinations(range(0, len(Gn)), 2)
  112. n_jobs = multiprocessing.cpu_count()
  113. if len_itr < 100 * n_jobs:
  114. chunksize = int(len_itr / n_jobs) + 1
  115. else:
  116. chunksize = 100
  117. def init_worker(gn_toshare):
  118. global G_gn
  119. G_gn = gn_toshare
  120. do_partial = partial(_wrapper_compute_ged_parallel, params_ged, get_nb_eo)
  121. pool = Pool(processes=n_jobs, initializer=init_worker, initargs=(Gn,))
  122. iterator = tqdm(pool.imap_unordered(do_partial, itr, chunksize),
  123. desc='computing GEDs', file=sys.stdout)
  124. # iterator = pool.imap_unordered(do_partial, itr, chunksize)
  125. for i, j, dis, n_eo_tmp in iterator:
  126. idx_itr = int(len(Gn) * i + j - (i + 1) * (i + 2) / 2)
  127. ged_vec[idx_itr] = dis
  128. ged_mat[i][j] = dis
  129. ged_mat[j][i] = dis
  130. n_edit_operations[idx_itr] = n_eo_tmp
  131. # print('\n-------------------------------------------')
  132. # print(i, j, idx_itr, dis)
  133. pool.close()
  134. pool.join()
  135. else:
  136. ged_vec = []
  137. n_edit_operations = []
  138. for i in tqdm(range(len(Gn)), desc='computing GEDs', file=sys.stdout):
  139. # for i in range(len(Gn)):
  140. for j in range(i + 1, len(Gn)):
  141. dis, pi_forward, pi_backward = GED(Gn[i], Gn[j], **params_ged)
  142. ged_vec.append(dis)
  143. ged_mat[i][j] = dis
  144. ged_mat[j][i] = dis
  145. n_eo_tmp = get_nb_eo(Gn[i], Gn[j], pi_forward, pi_backward)
  146. n_edit_operations.append(n_eo_tmp)
  147. return ged_vec, ged_mat, n_edit_operations
  148. def _wrapper_compute_ged_parallel(params_ged, get_nb_eo, itr):
  149. i = itr[0]
  150. j = itr[1]
  151. dis, n_eo_tmp = _compute_ged_parallel(G_gn[i], G_gn[j], params_ged, get_nb_eo)
  152. return i, j, dis, n_eo_tmp
  153. def _compute_ged_parallel(g1, g2, params_ged, get_nb_eo):
  154. dis, pi_forward, pi_backward = GED(g1, g2, **params_ged)
  155. n_eo_tmp = get_nb_eo(g1, g2, pi_forward, pi_backward) # [0,0,0,0,0,0]
  156. return dis, n_eo_tmp
  157. def update_costs(nb_cost_mat, dis_k_vec, dataset='monoterpenoides',
  158. cost='CONSTANT', rw_constraints='inequality'):
  159. # if dataset == 'Letter-high':
  160. if cost == 'LETTER':
  161. pass
  162. # # method 1: set alpha automatically, just tune c_vir and c_eir by
  163. # # LMS using cvxpy.
  164. # alpha = 0.5
  165. # coeff = 100 # np.max(alpha * nb_cost_mat[:,4] / dis_k_vec)
  166. ## if np.count_nonzero(nb_cost_mat[:,4]) == 0:
  167. ## alpha = 0.75
  168. ## else:
  169. ## alpha = np.min([dis_k_vec / c_vs for c_vs in nb_cost_mat[:,4] if c_vs != 0])
  170. ## alpha = alpha * 0.99
  171. # param_vir = alpha * (nb_cost_mat[:,0] + nb_cost_mat[:,1])
  172. # param_eir = (1 - alpha) * (nb_cost_mat[:,4] + nb_cost_mat[:,5])
  173. # nb_cost_mat_new = np.column_stack((param_vir, param_eir))
  174. # dis_new = coeff * dis_k_vec - alpha * nb_cost_mat[:,3]
  175. #
  176. # x = cp.Variable(nb_cost_mat_new.shape[1])
  177. # cost = cp.sum_squares(nb_cost_mat_new * x - dis_new)
  178. # constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
  179. # prob = cp.Problem(cp.Minimize(cost), constraints)
  180. # prob.solve()
  181. # edit_costs_new = x.value
  182. # edit_costs_new = np.array([edit_costs_new[0], edit_costs_new[1], alpha])
  183. # residual = np.sqrt(prob.value)
  184. # # method 2: tune c_vir, c_eir and alpha by nonlinear programming by
  185. # # scipy.optimize.minimize.
  186. # w0 = nb_cost_mat[:,0] + nb_cost_mat[:,1]
  187. # w1 = nb_cost_mat[:,4] + nb_cost_mat[:,5]
  188. # w2 = nb_cost_mat[:,3]
  189. # w3 = dis_k_vec
  190. # func_min = lambda x: np.sum((w0 * x[0] * x[3] + w1 * x[1] * (1 - x[2]) \
  191. # + w2 * x[2] - w3 * x[3]) ** 2)
  192. # bounds = ((0, None), (0., None), (0.5, 0.5), (0, None))
  193. # res = minimize(func_min, [0.9, 1.7, 0.75, 10], bounds=bounds)
  194. # edit_costs_new = res.x[0:3]
  195. # residual = res.fun
  196. # method 3: tune c_vir, c_eir and alpha by nonlinear programming using cvxpy.
  197. # # method 4: tune c_vir, c_eir and alpha by QP function
  198. # # scipy.optimize.least_squares. An initial guess is required.
  199. # w0 = nb_cost_mat[:,0] + nb_cost_mat[:,1]
  200. # w1 = nb_cost_mat[:,4] + nb_cost_mat[:,5]
  201. # w2 = nb_cost_mat[:,3]
  202. # w3 = dis_k_vec
  203. # func = lambda x: (w0 * x[0] * x[3] + w1 * x[1] * (1 - x[2]) \
  204. # + w2 * x[2] - w3 * x[3]) ** 2
  205. # res = optimize.root(func, [0.9, 1.7, 0.75, 100])
  206. # edit_costs_new = res.x
  207. # residual = None
  208. elif cost == 'LETTER2':
  209. # # 1. if c_vi != c_vr, c_ei != c_er.
  210. # nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  211. # x = cp.Variable(nb_cost_mat_new.shape[1])
  212. # cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  213. ## # 1.1 no constraints.
  214. ## constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
  215. # # 1.2 c_vs <= c_vi + c_vr.
  216. # constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
  217. # np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  218. ## # 2. if c_vi == c_vr, c_ei == c_er.
  219. ## nb_cost_mat_new = nb_cost_mat[:,[0,3,4]]
  220. ## nb_cost_mat_new[:,0] += nb_cost_mat[:,1]
  221. ## nb_cost_mat_new[:,2] += nb_cost_mat[:,5]
  222. ## x = cp.Variable(nb_cost_mat_new.shape[1])
  223. ## cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  224. ## # 2.1 no constraints.
  225. ## constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
  226. ### # 2.2 c_vs <= c_vi + c_vr.
  227. ### constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
  228. ### np.array([2.0, -1.0, 0.0]).T@x >= 0.0]
  229. #
  230. # prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  231. # prob.solve()
  232. # edit_costs_new = [x.value[0], x.value[0], x.value[1], x.value[2], x.value[2]]
  233. # edit_costs_new = np.array(edit_costs_new)
  234. # residual = np.sqrt(prob.value)
  235. if rw_constraints == 'inequality':
  236. # c_vs <= c_vi + c_vr.
  237. nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  238. x = cp.Variable(nb_cost_mat_new.shape[1])
  239. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  240. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
  241. np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  242. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  243. prob.solve()
  244. edit_costs_new = x.value
  245. residual = np.sqrt(prob.value)
  246. elif rw_constraints == '2constraints':
  247. # c_vs <= c_vi + c_vr and c_vi == c_vr, c_ei == c_er.
  248. nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  249. x = cp.Variable(nb_cost_mat_new.shape[1])
  250. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  251. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
  252. np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0,
  253. np.array([1.0, -1.0, 0.0, 0.0, 0.0]).T@x == 0.0,
  254. np.array([0.0, 0.0, 0.0, 1.0, -1.0]).T@x == 0.0]
  255. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  256. prob.solve()
  257. edit_costs_new = x.value
  258. residual = np.sqrt(prob.value)
  259. elif rw_constraints == 'no-constraint':
  260. # no constraint.
  261. nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  262. x = cp.Variable(nb_cost_mat_new.shape[1])
  263. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  264. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
  265. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  266. prob.solve()
  267. edit_costs_new = x.value
  268. residual = np.sqrt(prob.value)
  269. # elif method == 'inequality_modified':
  270. # # c_vs <= c_vi + c_vr.
  271. # nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  272. # x = cp.Variable(nb_cost_mat_new.shape[1])
  273. # cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  274. # constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
  275. # np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  276. # prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  277. # prob.solve()
  278. # # use same costs for insertion and removal rather than the fitted costs.
  279. # edit_costs_new = [x.value[0], x.value[0], x.value[1], x.value[2], x.value[2]]
  280. # edit_costs_new = np.array(edit_costs_new)
  281. # residual = np.sqrt(prob.value)
  282. elif cost == 'NON_SYMBOLIC':
  283. is_n_attr = np.count_nonzero(nb_cost_mat[:,2])
  284. is_e_attr = np.count_nonzero(nb_cost_mat[:,5])
  285. if dataset == 'SYNTHETICnew':
  286. # nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4]]
  287. nb_cost_mat_new = nb_cost_mat[:,[2,3,4]]
  288. x = cp.Variable(nb_cost_mat_new.shape[1])
  289. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  290. # constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
  291. # np.array([0.0, 0.0, 0.0, 1.0, -1.0]).T@x == 0.0]
  292. # constraints = [x >= [0.0001 for i in range(nb_cost_mat_new.shape[1])]]
  293. constraints = [x >= [0.0001 for i in range(nb_cost_mat_new.shape[1])],
  294. np.array([0.0, 1.0, -1.0]).T@x == 0.0]
  295. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  296. prob.solve()
  297. # print(x.value)
  298. edit_costs_new = np.concatenate((np.array([0.0, 0.0]), x.value,
  299. np.array([0.0])))
  300. residual = np.sqrt(prob.value)
  301. elif rw_constraints == 'inequality':
  302. # c_vs <= c_vi + c_vr.
  303. if is_n_attr and is_e_attr:
  304. nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4,5]]
  305. x = cp.Variable(nb_cost_mat_new.shape[1])
  306. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  307. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
  308. np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
  309. np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
  310. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  311. prob.solve()
  312. edit_costs_new = x.value
  313. residual = np.sqrt(prob.value)
  314. elif is_n_attr and not is_e_attr:
  315. nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4]]
  316. x = cp.Variable(nb_cost_mat_new.shape[1])
  317. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  318. constraints = [x >= [0.001 for i in range(nb_cost_mat_new.shape[1])],
  319. np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  320. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  321. prob.solve()
  322. print(x.value)
  323. edit_costs_new = np.concatenate((x.value, np.array([0.0])))
  324. residual = np.sqrt(prob.value)
  325. elif not is_n_attr and is_e_attr:
  326. nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
  327. x = cp.Variable(nb_cost_mat_new.shape[1])
  328. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  329. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
  330. np.array([0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
  331. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  332. prob.solve()
  333. edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), x.value[2:]))
  334. residual = np.sqrt(prob.value)
  335. else:
  336. nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4]]
  337. x = cp.Variable(nb_cost_mat_new.shape[1])
  338. cost_fun = cp.sum_squares(nb_cost_mat_new * x - dis_k_vec)
  339. constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
  340. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  341. prob.solve()
  342. edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]),
  343. x.value[2:], np.array([0.0])))
  344. residual = np.sqrt(prob.value)
  345. else:
  346. # # method 1: simple least square method.
  347. # edit_costs_new, residual, _, _ = np.linalg.lstsq(nb_cost_mat, dis_k_vec,
  348. # rcond=None)
  349. # # method 2: least square method with x_i >= 0.
  350. # edit_costs_new, residual = optimize.nnls(nb_cost_mat, dis_k_vec)
  351. # method 3: solve as a quadratic program with constraints.
  352. # P = np.dot(nb_cost_mat.T, nb_cost_mat)
  353. # q_T = -2 * np.dot(dis_k_vec.T, nb_cost_mat)
  354. # G = -1 * np.identity(nb_cost_mat.shape[1])
  355. # h = np.array([0 for i in range(nb_cost_mat.shape[1])])
  356. # A = np.array([1 for i in range(nb_cost_mat.shape[1])])
  357. # b = 1
  358. # x = cp.Variable(nb_cost_mat.shape[1])
  359. # prob = cp.Problem(cp.Minimize(cp.quad_form(x, P) + q_T@x),
  360. # [G@x <= h])
  361. # prob.solve()
  362. # edit_costs_new = x.value
  363. # residual = prob.value - np.dot(dis_k_vec.T, dis_k_vec)
  364. # G = -1 * np.identity(nb_cost_mat.shape[1])
  365. # h = np.array([0 for i in range(nb_cost_mat.shape[1])])
  366. x = cp.Variable(nb_cost_mat.shape[1])
  367. cost_fun = cp.sum_squares(nb_cost_mat * x - dis_k_vec)
  368. constraints = [x >= [0.0 for i in range(nb_cost_mat.shape[1])],
  369. # np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
  370. np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
  371. np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
  372. prob = cp.Problem(cp.Minimize(cost_fun), constraints)
  373. prob.solve()
  374. edit_costs_new = x.value
  375. residual = np.sqrt(prob.value)
  376. # method 4:
  377. return edit_costs_new, residual
  378. if __name__ == '__main__':
  379. print('check test_fitDistance.py')

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