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.

median_graph_estimator.py 36 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Mar 16 18:04:55 2020
  5. @author: ljia
  6. """
  7. import numpy as np
  8. from gklearn.ged.env import AlgorithmState
  9. from gklearn.ged.util import misc
  10. from gklearn.utils import Timer
  11. import time
  12. from tqdm import tqdm
  13. import sys
  14. import networkx as nx
  15. class MedianGraphEstimator(object):
  16. def __init__(self, ged_env, constant_node_costs):
  17. """Constructor.
  18. Parameters
  19. ----------
  20. ged_env : gklearn.gedlib.gedlibpy.GEDEnv
  21. Initialized GED environment. The edit costs must be set by the user.
  22. constant_node_costs : Boolean
  23. Set to True if the node relabeling costs are constant.
  24. """
  25. self.__ged_env = ged_env
  26. self.__init_method = 'BRANCH_FAST'
  27. self.__init_options = ''
  28. self.__descent_method = 'BRANCH_FAST'
  29. self.__descent_options = ''
  30. self.__refine_method = 'IPFP'
  31. self.__refine_options = ''
  32. self.__constant_node_costs = constant_node_costs
  33. self.__labeled_nodes = (ged_env.get_num_node_labels() > 1)
  34. self.__node_del_cost = ged_env.get_node_del_cost(ged_env.get_node_label(1))
  35. self.__node_ins_cost = ged_env.get_node_ins_cost(ged_env.get_node_label(1))
  36. self.__labeled_edges = (ged_env.get_num_edge_labels() > 1)
  37. self.__edge_del_cost = ged_env.get_edge_del_cost(ged_env.get_edge_label(1))
  38. self.__edge_ins_cost = ged_env.get_edge_ins_cost(ged_env.get_edge_label(1))
  39. self.__init_type = 'RANDOM'
  40. self.__num_random_inits = 10
  41. self.__desired_num_random_inits = 10
  42. self.__use_real_randomness = True
  43. self.__seed = 0
  44. self.__refine = True
  45. self.__time_limit_in_sec = 0
  46. self.__epsilon = 0.0001
  47. self.__max_itrs = 100
  48. self.__max_itrs_without_update = 3
  49. self.__num_inits_increase_order = 10
  50. self.__init_type_increase_order = 'K-MEANS++'
  51. self.__max_itrs_increase_order = 10
  52. self.__print_to_stdout = 2
  53. self.__median_id = np.inf # @todo: check
  54. self.__median_node_id_prefix = '' # @todo: check
  55. self.__node_maps_from_median = {}
  56. self.__sum_of_distances = 0
  57. self.__best_init_sum_of_distances = np.inf
  58. self.__converged_sum_of_distances = np.inf
  59. self.__runtime = None
  60. self.__runtime_initialized = None
  61. self.__runtime_converged = None
  62. self.__itrs = [] # @todo: check: {} ?
  63. self.__num_decrease_order = 0
  64. self.__num_increase_order = 0
  65. self.__num_converged_descents = 0
  66. self.__state = AlgorithmState.TERMINATED
  67. self.__label_names = {}
  68. if ged_env is None:
  69. raise Exception('The GED environment pointer passed to the constructor of MedianGraphEstimator is null.')
  70. elif not ged_env.is_initialized():
  71. raise Exception('The GED environment is uninitialized. Call gedlibpy.GEDEnv.init() before passing it to the constructor of MedianGraphEstimator.')
  72. def set_options(self, options):
  73. """Sets the options of the estimator.
  74. Parameters
  75. ----------
  76. options : string
  77. String that specifies with which options to run the estimator.
  78. """
  79. self.__set_default_options()
  80. options_map = misc.options_string_to_options_map(options)
  81. for opt_name, opt_val in options_map.items():
  82. if opt_name == 'init-type':
  83. self.__init_type = opt_val
  84. if opt_val != 'MEDOID' and opt_val != 'RANDOM' and opt_val != 'MIN' and opt_val != 'MAX' and opt_val != 'MEAN':
  85. raise Exception('Invalid argument ' + opt_val + ' for option init-type. Usage: options = "[--init-type RANDOM|MEDOID|EMPTY|MIN|MAX|MEAN] [...]"')
  86. elif opt_name == 'random-inits':
  87. try:
  88. self.__num_random_inits = int(opt_val)
  89. self.__desired_num_random_inits = self.__num_random_inits
  90. except:
  91. raise Exception('Invalid argument "' + opt_val + '" for option random-inits. Usage: options = "[--random-inits <convertible to int greater 0>]"')
  92. if self.__num_random_inits <= 0:
  93. raise Exception('Invalid argument "' + opt_val + '" for option random-inits. Usage: options = "[--random-inits <convertible to int greater 0>]"')
  94. elif opt_name == 'randomness':
  95. if opt_val == 'PSEUDO':
  96. self.__use_real_randomness = False
  97. elif opt_val == 'REAL':
  98. self.__use_real_randomness = True
  99. else:
  100. raise Exception('Invalid argument "' + opt_val + '" for option randomness. Usage: options = "[--randomness REAL|PSEUDO] [...]"')
  101. elif opt_name == 'stdout':
  102. if opt_val == '0':
  103. self.__print_to_stdout = 0
  104. elif opt_val == '1':
  105. self.__print_to_stdout = 1
  106. elif opt_val == '2':
  107. self.__print_to_stdout = 2
  108. else:
  109. raise Exception('Invalid argument "' + opt_val + '" for option stdout. Usage: options = "[--stdout 0|1|2] [...]"')
  110. elif opt_name == 'refine':
  111. if opt_val == 'TRUE':
  112. self.__refine = True
  113. elif opt_val == 'FALSE':
  114. self.__refine = False
  115. else:
  116. raise Exception('Invalid argument "' + opt_val + '" for option refine. Usage: options = "[--refine TRUE|FALSE] [...]"')
  117. elif opt_name == 'time-limit':
  118. try:
  119. self.__time_limit_in_sec = float(opt_val)
  120. except:
  121. raise Exception('Invalid argument "' + opt_val + '" for option time-limit. Usage: options = "[--time-limit <convertible to double>] [...]')
  122. elif opt_name == 'max-itrs':
  123. try:
  124. self.__max_itrs = int(opt_val)
  125. except:
  126. raise Exception('Invalid argument "' + opt_val + '" for option max-itrs. Usage: options = "[--max-itrs <convertible to int>] [...]')
  127. elif opt_name == 'max-itrs-without-update':
  128. try:
  129. self.__max_itrs_without_update = int(opt_val)
  130. except:
  131. raise Exception('Invalid argument "' + opt_val + '" for option max-itrs-without-update. Usage: options = "[--max-itrs-without-update <convertible to int>] [...]')
  132. elif opt_name == 'seed':
  133. try:
  134. self.__seed = int(opt_val)
  135. except:
  136. raise Exception('Invalid argument "' + opt_val + '" for option seed. Usage: options = "[--seed <convertible to int greater equal 0>] [...]')
  137. elif opt_name == 'epsilon':
  138. try:
  139. self.__epsilon = float(opt_val)
  140. except:
  141. raise Exception('Invalid argument "' + opt_val + '" for option epsilon. Usage: options = "[--epsilon <convertible to double greater 0>] [...]')
  142. if self.__epsilon <= 0:
  143. raise Exception('Invalid argument "' + opt_val + '" for option epsilon. Usage: options = "[--epsilon <convertible to double greater 0>] [...]')
  144. elif opt_name == 'inits-increase-order':
  145. try:
  146. self.__num_inits_increase_order = int(opt_val)
  147. except:
  148. raise Exception('Invalid argument "' + opt_val + '" for option inits-increase-order. Usage: options = "[--inits-increase-order <convertible to int greater 0>]"')
  149. if self.__num_inits_increase_order <= 0:
  150. raise Exception('Invalid argument "' + opt_val + '" for option inits-increase-order. Usage: options = "[--inits-increase-order <convertible to int greater 0>]"')
  151. elif opt_name == 'init-type-increase-order':
  152. self.__init_type_increase_order = opt_val
  153. if opt_val != 'CLUSTERS' and opt_val != 'K-MEANS++':
  154. raise Exception('Invalid argument ' + opt_val + ' for option init-type-increase-order. Usage: options = "[--init-type-increase-order CLUSTERS|K-MEANS++] [...]"')
  155. elif opt_name == 'max-itrs-increase-order':
  156. try:
  157. self.__max_itrs_increase_order = int(opt_val)
  158. except:
  159. raise Exception('Invalid argument "' + opt_val + '" for option max-itrs-increase-order. Usage: options = "[--max-itrs-increase-order <convertible to int>] [...]')
  160. else:
  161. valid_options = '[--init-type <arg>] [--random-inits <arg>] [--randomness <arg>] [--seed <arg>] [--stdout <arg>] '
  162. valid_options += '[--time-limit <arg>] [--max-itrs <arg>] [--epsilon <arg>] '
  163. valid_options += '[--inits-increase-order <arg>] [--init-type-increase-order <arg>] [--max-itrs-increase-order <arg>]'
  164. raise Exception('Invalid option "' + opt_name + '". Usage: options = "' + valid_options + '"')
  165. def set_init_method(self, init_method, init_options=''):
  166. """Selects method to be used for computing the initial medoid graph.
  167. Parameters
  168. ----------
  169. init_method : string
  170. The selected method. Default: ged::Options::GEDMethod::BRANCH_UNIFORM.
  171. init_options : string
  172. The options for the selected method. Default: "".
  173. Notes
  174. -----
  175. Has no effect unless "--init-type MEDOID" is passed to set_options().
  176. """
  177. self.__init_method = init_method;
  178. self.__init_options = init_options;
  179. def set_descent_method(self, descent_method, descent_options=''):
  180. """Selects method to be used for block gradient descent..
  181. Parameters
  182. ----------
  183. descent_method : string
  184. The selected method. Default: ged::Options::GEDMethod::BRANCH_FAST.
  185. descent_options : string
  186. The options for the selected method. Default: "".
  187. Notes
  188. -----
  189. Has no effect unless "--init-type MEDOID" is passed to set_options().
  190. """
  191. self.__descent_method = descent_method;
  192. self.__descent_options = descent_options;
  193. def set_refine_method(self, refine_method, refine_options):
  194. """Selects method to be used for improving the sum of distances and the node maps for the converged median.
  195. Parameters
  196. ----------
  197. refine_method : string
  198. The selected method. Default: "IPFP".
  199. refine_options : string
  200. The options for the selected method. Default: "".
  201. Notes
  202. -----
  203. Has no effect if "--refine FALSE" is passed to set_options().
  204. """
  205. self.__refine_method = refine_method
  206. self.__refine_options = refine_options
  207. def run(self, graph_ids, set_median_id, gen_median_id):
  208. """Computes a generalized median graph.
  209. Parameters
  210. ----------
  211. graph_ids : list[integer]
  212. The IDs of the graphs for which the median should be computed. Must have been added to the environment passed to the constructor.
  213. set_median_id : integer
  214. The ID of the computed set-median. A dummy graph with this ID must have been added to the environment passed to the constructor. Upon termination, the computed median can be obtained via gklearn.gedlib.gedlibpy.GEDEnv.get_graph().
  215. gen_median_id : integer
  216. The ID of the computed generalized median. Upon termination, the computed median can be obtained via gklearn.gedlib.gedlibpy.GEDEnv.get_graph().
  217. """
  218. # Sanity checks.
  219. if len(graph_ids) == 0:
  220. raise Exception('Empty vector of graph IDs, unable to compute median.')
  221. all_graphs_empty = True
  222. for graph_id in graph_ids:
  223. if self.__ged_env.get_graph_num_nodes(graph_id) > 0:
  224. self.__median_node_id_prefix = self.__ged_env.get_original_node_ids(graph_id)[0]
  225. all_graphs_empty = False
  226. break
  227. if all_graphs_empty:
  228. raise Exception('All graphs in the collection are empty.')
  229. # Start timer and record start time.
  230. start = time.time()
  231. timer = Timer(self.__time_limit_in_sec)
  232. self.__median_id = gen_median_id
  233. self.__state = AlgorithmState.TERMINATED
  234. # Get ExchangeGraph representations of the input graphs.
  235. graphs = {}
  236. for graph_id in graph_ids:
  237. # @todo: get_nx_graph() function may need to be modified according to the coming code.
  238. graphs[graph_id] = self.__ged_env.get_nx_graph(graph_id, True, True, False)
  239. # print(self.__ged_env.get_graph_internal_id(0))
  240. # print(graphs[0].graph)
  241. # print(graphs[0].nodes(data=True))
  242. # print(graphs[0].edges(data=True))
  243. # print(nx.adjacency_matrix(graphs[0]))
  244. # Construct initial medians.
  245. medians = []
  246. self.__construct_initial_medians(graph_ids, timer, medians)
  247. end_init = time.time()
  248. self.__runtime_initialized = end_init - start
  249. # print(medians[0].graph)
  250. # print(medians[0].nodes(data=True))
  251. # print(medians[0].edges(data=True))
  252. # print(nx.adjacency_matrix(medians[0]))
  253. # Reset information about iterations and number of times the median decreases and increases.
  254. self.__itrs = [0] * len(medians)
  255. self.__num_decrease_order = 0
  256. self.__num_increase_order = 0
  257. self.__num_converged_descents = 0
  258. # Initialize the best median.
  259. best_sum_of_distances = np.inf
  260. self.__best_init_sum_of_distances = np.inf
  261. node_maps_from_best_median = {}
  262. # Run block gradient descent from all initial medians.
  263. self.__ged_env.set_method(self.__descent_method, self.__descent_options)
  264. for median_pos in range(0, len(medians)):
  265. # Terminate if the timer has expired and at least one SOD has been computed.
  266. if timer.expired() and median_pos > 0:
  267. break
  268. # Print information about current iteration.
  269. if self.__print_to_stdout == 2:
  270. print('\n===========================================================')
  271. print('Block gradient descent for initial median', str(median_pos + 1), 'of', str(len(medians)), '.')
  272. print('-----------------------------------------------------------')
  273. # Get reference to the median.
  274. median = medians[median_pos]
  275. # Load initial median into the environment.
  276. self.__ged_env.load_nx_graph(median, gen_median_id)
  277. self.__ged_env.init(self.__ged_env.get_init_type())
  278. # Print information about current iteration.
  279. if self.__print_to_stdout == 2:
  280. progress = tqdm(desc='Computing initial node maps', total=len(graph_ids), file=sys.stdout)
  281. # Compute node maps and sum of distances for initial median.
  282. self.__sum_of_distances = 0
  283. self.__node_maps_from_median.clear() # @todo
  284. for graph_id in graph_ids:
  285. self.__ged_env.run_method(gen_median_id, graph_id)
  286. self.__node_maps_from_median[graph_id] = self.__ged_env.get_node_map(gen_median_id, graph_id)
  287. # print(self.__node_maps_from_median[graph_id])
  288. self.__sum_of_distances += self.__ged_env.get_induced_cost(gen_median_id, graph_id) # @todo: the C++ implementation for this function in GedLibBind.ipp re-call get_node_map() once more, this is not neccessary.
  289. # print(self.__sum_of_distances)
  290. # Print information about current iteration.
  291. if self.__print_to_stdout == 2:
  292. progress.update(1)
  293. self.__best_init_sum_of_distances = min(self.__best_init_sum_of_distances, self.__sum_of_distances)
  294. self.__ged_env.load_nx_graph(median, set_median_id)
  295. # print(self.__best_init_sum_of_distances)
  296. # Print information about current iteration.
  297. if self.__print_to_stdout == 2:
  298. print('\n')
  299. # Run block gradient descent from initial median.
  300. converged = False
  301. itrs_without_update = 0
  302. while not self.__termination_criterion_met(converged, timer, self.__itrs[median_pos], itrs_without_update):
  303. # Print information about current iteration.
  304. if self.__print_to_stdout == 2:
  305. print('\n===========================================================')
  306. print('Iteration', str(self.__itrs[median_pos] + 1), 'for initial median', str(median_pos + 1), 'of', str(len(medians)), '.')
  307. print('-----------------------------------------------------------')
  308. # Initialize flags that tell us what happened in the iteration.
  309. median_modified = False
  310. node_maps_modified = False
  311. decreased_order = False
  312. increased_order = False
  313. # Update the median. # @todo!!!!!!!!!!!!!!!!!!!!!!
  314. median_modified = self.__update_median(graphs, median)
  315. if not median_modified or self.__itrs[median_pos] == 0:
  316. decreased_order = False
  317. if not decreased_order or self.__itrs[median_pos] == 0:
  318. increased_order = False
  319. # Update the number of iterations without update of the median.
  320. if median_modified or decreased_order or increased_order:
  321. itrs_without_update = 0
  322. else:
  323. itrs_without_update += 1
  324. # Print information about current iteration.
  325. if self.__print_to_stdout == 2:
  326. print('Loading median to environment: ... ', end='')
  327. # Load the median into the environment.
  328. # @todo: should this function use the original node label?
  329. self.__ged_env.load_nx_graph(median, gen_median_id)
  330. self.__ged_env.init(self.__ged_env.get_init_type())
  331. # Print information about current iteration.
  332. if self.__print_to_stdout == 2:
  333. print('done.')
  334. # Print information about current iteration.
  335. if self.__print_to_stdout == 2:
  336. print('Updating induced costs: ... ', end='')
  337. # Compute induced costs of the old node maps w.r.t. the updated median.
  338. for graph_id in graph_ids:
  339. # print(self.__ged_env.get_induced_cost(gen_median_id, graph_id))
  340. # @todo: watch out if compute_induced_cost is correct, this may influence: increase/decrease order, induced_cost() in the following code.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  341. self.__ged_env.compute_induced_cost(gen_median_id, graph_id)
  342. # print('---------------------------------------')
  343. # print(self.__ged_env.get_induced_cost(gen_median_id, graph_id))
  344. # Print information about current iteration.
  345. if self.__print_to_stdout == 2:
  346. print('done.')
  347. # Update the node maps.
  348. node_maps_modified = self.__update_node_maps() # @todo
  349. # Update the order of the median if no improvement can be found with the current order.
  350. # Update the sum of distances.
  351. old_sum_of_distances = self.__sum_of_distances
  352. self.__sum_of_distances = 0
  353. for graph_id in self.__node_maps_from_median:
  354. self.__sum_of_distances += self.__ged_env.get_induced_cost(gen_median_id, graph_id) # @todo: see above.
  355. # Print information about current iteration.
  356. if self.__print_to_stdout == 2:
  357. print('Old local SOD: ', old_sum_of_distances)
  358. print('New local SOD: ', self.__sum_of_distances)
  359. print('Best converged SOD: ', best_sum_of_distances)
  360. print('Modified median: ', median_modified)
  361. print('Modified node maps: ', node_maps_modified)
  362. print('Decreased order: ', decreased_order)
  363. print('Increased order: ', increased_order)
  364. print('===========================================================\n')
  365. converged = not (median_modified or node_maps_modified or decreased_order or increased_order)
  366. self.__itrs[median_pos] += 1
  367. # Update the best median.
  368. if self.__sum_of_distances < best_sum_of_distances:
  369. best_sum_of_distances = self.__sum_of_distances
  370. node_maps_from_best_median = self.__node_maps_from_median
  371. best_median = median
  372. # Update the number of converged descents.
  373. if converged:
  374. self.__num_converged_descents += 1
  375. # Store the best encountered median.
  376. self.__sum_of_distances = best_sum_of_distances
  377. self.__node_maps_from_median = node_maps_from_best_median
  378. self.__ged_env.load_nx_graph(best_median, gen_median_id)
  379. self.__ged_env.init(self.__ged_env.get_init_type())
  380. end_descent = time.time()
  381. self.__runtime_converged = end_descent - start
  382. # Refine the sum of distances and the node maps for the converged median.
  383. self.__converged_sum_of_distances = self.__sum_of_distances
  384. if self.__refine:
  385. self.__improve_sum_of_distances(timer) # @todo
  386. # Record end time, set runtime and reset the number of initial medians.
  387. end = time.time()
  388. self.__runtime = end - start
  389. self.__num_random_inits = self.__desired_num_random_inits
  390. # Print global information.
  391. if self.__print_to_stdout != 0:
  392. print('\n===========================================================')
  393. print('Finished computation of generalized median graph.')
  394. print('-----------------------------------------------------------')
  395. print('Best SOD after initialization: ', self.__best_init_sum_of_distances)
  396. print('Converged SOD: ', self.__converged_sum_of_distances)
  397. if self.__refine:
  398. print('Refined SOD: ', self.__sum_of_distances)
  399. print('Overall runtime: ', self.__runtime)
  400. print('Runtime of initialization: ', self.__runtime_initialized)
  401. print('Runtime of block gradient descent: ', self.__runtime_converged - self.__runtime_initialized)
  402. if self.__refine:
  403. print('Runtime of refinement: ', self.__runtime - self.__runtime_converged)
  404. print('Number of initial medians: ', len(medians))
  405. total_itr = 0
  406. num_started_descents = 0
  407. for itr in self.__itrs:
  408. total_itr += itr
  409. if itr > 0:
  410. num_started_descents += 1
  411. print('Size of graph collection: ', len(graph_ids))
  412. print('Number of started descents: ', num_started_descents)
  413. print('Number of converged descents: ', self.__num_converged_descents)
  414. print('Overall number of iterations: ', total_itr)
  415. print('Overall number of times the order decreased: ', self.__num_decrease_order)
  416. print('Overall number of times the order increased: ', self.__num_increase_order)
  417. print('===========================================================\n')
  418. def get_sum_of_distances(self, state=''):
  419. """Returns the sum of distances.
  420. Parameters
  421. ----------
  422. state : string
  423. The state of the estimator. Can be 'initialized' or 'converged'. Default: ""
  424. Returns
  425. -------
  426. float
  427. The sum of distances (SOD) of the median when the estimator was in the state `state` during the last call to run(). If `state` is not given, the converged SOD (without refinement) or refined SOD (with refinement) is returned.
  428. """
  429. if not self.__median_available():
  430. raise Exception('No median has been computed. Call run() before calling get_sum_of_distances().')
  431. if state == 'initialized':
  432. return self.__best_init_sum_of_distances
  433. if state == 'converged':
  434. return self.__converged_sum_of_distances
  435. return self.__sum_of_distances
  436. def __set_default_options(self):
  437. self.__init_type = 'RANDOM'
  438. self.__num_random_inits = 10
  439. self.__desired_num_random_inits = 10
  440. self.__use_real_randomness = True
  441. self.__seed = 0
  442. self.__refine = True
  443. self.__time_limit_in_sec = 0
  444. self.__epsilon = 0.0001
  445. self.__max_itrs = 100
  446. self.__max_itrs_without_update = 3
  447. self.__num_inits_increase_order = 10
  448. self.__init_type_increase_order = 'K-MEANS++'
  449. self.__max_itrs_increase_order = 10
  450. self.__print_to_stdout = 2
  451. self.__label_names = {}
  452. def __construct_initial_medians(self, graph_ids, timer, initial_medians):
  453. # Print information about current iteration.
  454. if self.__print_to_stdout == 2:
  455. print('\n===========================================================')
  456. print('Constructing initial median(s).')
  457. print('-----------------------------------------------------------')
  458. # Compute or sample the initial median(s).
  459. initial_medians.clear()
  460. if self.__init_type == 'MEDOID':
  461. self.__compute_medoid(graph_ids, timer, initial_medians)
  462. elif self.__init_type == 'MAX':
  463. pass # @todo
  464. # compute_max_order_graph_(graph_ids, initial_medians)
  465. elif self.__init_type == 'MIN':
  466. pass # @todo
  467. # compute_min_order_graph_(graph_ids, initial_medians)
  468. elif self.__init_type == 'MEAN':
  469. pass # @todo
  470. # compute_mean_order_graph_(graph_ids, initial_medians)
  471. else:
  472. pass # @todo
  473. # sample_initial_medians_(graph_ids, initial_medians)
  474. # Print information about current iteration.
  475. if self.__print_to_stdout == 2:
  476. print('===========================================================')
  477. def __compute_medoid(self, graph_ids, timer, initial_medians):
  478. # Use method selected for initialization phase.
  479. self.__ged_env.set_method(self.__init_method, self.__init_options)
  480. # Print information about current iteration.
  481. if self.__print_to_stdout == 2:
  482. progress = tqdm(desc='Computing medoid', total=len(graph_ids), file=sys.stdout)
  483. # Compute the medoid.
  484. medoid_id = graph_ids[0]
  485. best_sum_of_distances = np.inf
  486. for g_id in graph_ids:
  487. if timer.expired():
  488. self.__state = AlgorithmState.CALLED
  489. break
  490. sum_of_distances = 0
  491. for h_id in graph_ids:
  492. self.__ged_env.run_method(g_id, h_id)
  493. sum_of_distances += self.__ged_env.get_upper_bound(g_id, h_id)
  494. if sum_of_distances < best_sum_of_distances:
  495. best_sum_of_distances = sum_of_distances
  496. medoid_id = g_id
  497. # Print information about current iteration.
  498. if self.__print_to_stdout == 2:
  499. progress.update(1)
  500. initial_medians.append(self.__ged_env.get_nx_graph(medoid_id, True, True, False)) # @todo
  501. # Print information about current iteration.
  502. if self.__print_to_stdout == 2:
  503. print('\n')
  504. def __termination_criterion_met(self, converged, timer, itr, itrs_without_update):
  505. if timer.expired() or (itr >= self.__max_itrs if self.__max_itrs >= 0 else False):
  506. if self.__state == AlgorithmState.TERMINATED:
  507. self.__state = AlgorithmState.INITIALIZED
  508. return True
  509. return converged or (itrs_without_update > self.__max_itrs_without_update if self.__max_itrs_without_update >= 0 else False)
  510. def __update_median(self, graphs, median):
  511. # Print information about current iteration.
  512. if self.__print_to_stdout == 2:
  513. print('Updating median: ', end='')
  514. # Store copy of the old median.
  515. old_median = median.copy() # @todo: this is just a shallow copy.
  516. # Update the node labels.
  517. if self.__labeled_nodes:
  518. self.__update_node_labels(graphs, median)
  519. # Update the edges and their labels.
  520. self.__update_edges(graphs, median)
  521. # Print information about current iteration.
  522. if self.__print_to_stdout == 2:
  523. print('done.')
  524. return not self.__are_graphs_equal(median, old_median)
  525. def __update_node_labels(self, graphs, median):
  526. # Print information about current iteration.
  527. if self.__print_to_stdout == 2:
  528. print('nodes ... ', end='')
  529. # Iterate through all nodes of the median.
  530. for i in range(0, nx.number_of_nodes(median)):
  531. # print('i: ', i)
  532. # Collect the labels of the substituted nodes.
  533. node_labels = []
  534. for graph_id, graph in graphs.items():
  535. # print('graph_id: ', graph_id)
  536. # print(self.__node_maps_from_median[graph_id])
  537. k = self.__get_node_image_from_map(self.__node_maps_from_median[graph_id], i)
  538. # print('k: ', k)
  539. if k != np.inf:
  540. node_labels.append(graph.nodes[k])
  541. # Compute the median label and update the median.
  542. if len(node_labels) > 0:
  543. # median_label = self.__ged_env.get_median_node_label(node_labels)
  544. median_label = self.__get_median_node_label(node_labels)
  545. if self.__ged_env.get_node_rel_cost(median.nodes[i], median_label) > self.__epsilon:
  546. nx.set_node_attributes(median, {i: median_label})
  547. def __update_edges(self, graphs, median):
  548. # Print information about current iteration.
  549. if self.__print_to_stdout == 2:
  550. print('edges ... ', end='')
  551. # Clear the adjacency lists of the median and reset number of edges to 0.
  552. median_edges = list(median.edges)
  553. for (head, tail) in median_edges:
  554. median.remove_edge(head, tail)
  555. # @todo: what if edge is not labeled?
  556. # Iterate through all possible edges (i,j) of the median.
  557. for i in range(0, nx.number_of_nodes(median)):
  558. for j in range(i + 1, nx.number_of_nodes(median)):
  559. # Collect the labels of the edges to which (i,j) is mapped by the node maps.
  560. edge_labels = []
  561. for graph_id, graph in graphs.items():
  562. k = self.__get_node_image_from_map(self.__node_maps_from_median[graph_id], i)
  563. l = self.__get_node_image_from_map(self.__node_maps_from_median[graph_id], j)
  564. if k != np.inf and l != np.inf:
  565. if graph.has_edge(k, l):
  566. edge_labels.append(graph.edges[(k, l)])
  567. # Compute the median edge label and the overall edge relabeling cost.
  568. rel_cost = 0
  569. median_label = self.__ged_env.get_edge_label(1)
  570. if median.has_edge(i, j):
  571. median_label = median.edges[(i, j)]
  572. if self.__labeled_edges and len(edge_labels) > 0:
  573. new_median_label = self.__get_median_edge_label(edge_labels)
  574. if self.__ged_env.get_edge_rel_cost(median_label, new_median_label) > self.__epsilon:
  575. median_label = new_median_label
  576. for edge_label in edge_labels:
  577. rel_cost += self.__ged_env.get_edge_rel_cost(median_label, edge_label)
  578. # Update the median.
  579. if rel_cost < (self.__edge_ins_cost + self.__edge_del_cost) * len(edge_labels) - self.__edge_del_cost * len(graphs):
  580. median.add_edge(i, j, **median_label)
  581. else:
  582. if median.has_edge(i, j):
  583. median.remove_edge(i, j)
  584. def __update_node_maps(self):
  585. # Print information about current iteration.
  586. if self.__print_to_stdout == 2:
  587. progress = tqdm(desc='Updating node maps', total=len(self.__node_maps_from_median), file=sys.stdout)
  588. # Update the node maps.
  589. node_maps_were_modified = False
  590. for graph_id in self.__node_maps_from_median:
  591. self.__ged_env.run_method(self.__median_id, graph_id)
  592. if self.__ged_env.get_upper_bound(self.__median_id, graph_id) < self.__ged_env.get_induced_cost(self.__median_id, graph_id) - self.__epsilon: # @todo: see above.
  593. self.__node_maps_from_median[graph_id] = self.__ged_env.get_node_map(self.__median_id, graph_id) # @todo: node_map may not assigned.
  594. node_maps_were_modified = True
  595. # Print information about current iteration.
  596. if self.__print_to_stdout == 2:
  597. progress.update(1)
  598. # Print information about current iteration.
  599. if self.__print_to_stdout == 2:
  600. print('\n')
  601. # Return true if the node maps were modified.
  602. return node_maps_were_modified
  603. def __improve_sum_of_distances(self, timer):
  604. pass
  605. def __median_available(self):
  606. return self.__median_id != np.inf
  607. def __get_node_image_from_map(self, node_map, node):
  608. """
  609. Return ID of the node mapping of `node` in `node_map`.
  610. Parameters
  611. ----------
  612. node_map : list[tuple(int, int)]
  613. List of node maps where the mapping node is found.
  614. node : int
  615. The mapping node of this node is returned
  616. Raises
  617. ------
  618. Exception
  619. If the node with ID `node` is not contained in the source nodes of the node map.
  620. Returns
  621. -------
  622. int
  623. ID of the mapping of `node`.
  624. Notes
  625. -----
  626. This function is not implemented in the `ged::MedianGraphEstimator` class of the `GEDLIB` library. Instead it is a Python implementation of the `ged::NodeMap::image` function.
  627. """
  628. if node < len(node_map):
  629. return node_map[node][1] if node_map[node][1] < len(node_map) else np.inf
  630. else:
  631. raise Exception('The node with ID ', str(node), ' is not contained in the source nodes of the node map.')
  632. return np.inf
  633. def __are_graphs_equal(self, g1, g2):
  634. """
  635. Check if the two graphs are equal.
  636. Parameters
  637. ----------
  638. g1 : NetworkX graph object
  639. Graph 1 to be compared.
  640. g2 : NetworkX graph object
  641. Graph 2 to be compared.
  642. Returns
  643. -------
  644. bool
  645. True if the two graph are equal.
  646. Notes
  647. -----
  648. This is not an identical check. Here the two graphs are equal if and only if their original_node_ids, nodes, all node labels, edges and all edge labels are equal. This function is specifically designed for class `MedianGraphEstimator` and should not be used elsewhere.
  649. """
  650. # check original node ids.
  651. if not g1.graph['original_node_ids'] == g2.graph['original_node_ids']:
  652. return False
  653. # check nodes.
  654. nlist1 = [n for n in g1.nodes(data=True)]
  655. nlist2 = [n for n in g2.nodes(data=True)]
  656. if not nlist1 == nlist2:
  657. return False
  658. # check edges.
  659. elist1 = [n for n in g1.edges(data=True)]
  660. elist2 = [n for n in g2.edges(data=True)]
  661. if not elist1 == elist2:
  662. return False
  663. return True
  664. def compute_my_cost(g, h, node_map):
  665. cost = 0.0
  666. for node in g.nodes:
  667. cost += 0
  668. def set_label_names(self, node_labels=[], edge_labels=[], node_attrs=[], edge_attrs=[]):
  669. self.__label_names = {'node_labels': node_labels, 'edge_labels': edge_labels,
  670. 'node_attrs': node_attrs, 'edge_attrs': edge_attrs}
  671. def __get_median_node_label(self, node_labels):
  672. if len(self.__label_names['node_labels']) > 0:
  673. return self.__get_median_label_symbolic(node_labels)
  674. elif len(self.__label_names['node_attrs']) > 0:
  675. return self.__get_median_label_nonsymbolic(node_labels)
  676. else:
  677. raise Exception('Node label names are not given.')
  678. def __get_median_edge_label(self, edge_labels):
  679. if len(self.__label_names['edge_labels']) > 0:
  680. return self.__get_median_label_symbolic(edge_labels)
  681. elif len(self.__label_names['edge_attrs']) > 0:
  682. return self.__get_median_label_nonsymbolic(edge_labels)
  683. else:
  684. raise Exception('Edge label names are not given.')
  685. def __get_median_label_symbolic(self, labels):
  686. # Construct histogram.
  687. hist = {}
  688. for label in labels:
  689. label = tuple([kv for kv in label.items()]) # @todo: this may be slow.
  690. if label not in hist:
  691. hist[label] = 1
  692. else:
  693. hist[label] += 1
  694. # Return the label that appears most frequently.
  695. best_count = 0
  696. median_label = {}
  697. for label, count in hist.items():
  698. if count > best_count:
  699. best_count = count
  700. median_label = {kv[0]: kv[1] for kv in label}
  701. return median_label
  702. def __get_median_label_nonsymbolic(self, labels):
  703. if len(labels) == 0:
  704. return {} # @todo
  705. else:
  706. # Transform the labels into coordinates and compute mean label as initial solution.
  707. labels_as_coords = []
  708. sums = {}
  709. for key, val in labels[0].items():
  710. sums[key] = 0
  711. for label in labels:
  712. coords = {}
  713. for key, val in label.items():
  714. label = float(val)
  715. sums[key] += label
  716. coords[key] = label
  717. labels_as_coords.append(coords)
  718. median = {}
  719. for key, val in sums.items():
  720. median[key] = val / len(labels)
  721. # Run main loop of Weiszfeld's Algorithm.
  722. epsilon = 0.0001
  723. delta = 1.0
  724. num_itrs = 0
  725. all_equal = False
  726. while ((delta > epsilon) and (num_itrs < 100) and (not all_equal)):
  727. numerator = {}
  728. for key, val in sums.items():
  729. numerator[key] = 0
  730. denominator = 0
  731. for label_as_coord in labels_as_coords:
  732. norm = 0
  733. for key, val in label_as_coord.items():
  734. norm += (val - median[key]) ** 2
  735. norm += np.sqrt(norm)
  736. if norm > 0:
  737. for key, val in label_as_coord.items():
  738. numerator[key] += val / norm
  739. denominator += 1.0 / norm
  740. if denominator == 0:
  741. all_equal = True
  742. else:
  743. new_median = {}
  744. delta = 0.0
  745. for key, val in numerator.items():
  746. this_median = val / denominator
  747. new_median[key] = this_median
  748. delta += np.abs(median[key] - this_median)
  749. median = new_median
  750. num_itrs += 1
  751. # Transform the solution to strings and return it.
  752. median_label = {}
  753. for key, val in median.items():
  754. median_label[key] = str(val)
  755. return median_label
  756. # def __get_median_edge_label_symbolic(self, edge_labels):
  757. # pass
  758. # def __get_median_edge_label_nonsymbolic(self, edge_labels):
  759. # if len(edge_labels) == 0:
  760. # return {}
  761. # else:
  762. # # Transform the labels into coordinates and compute mean label as initial solution.
  763. # edge_labels_as_coords = []
  764. # sums = {}
  765. # for key, val in edge_labels[0].items():
  766. # sums[key] = 0
  767. # for edge_label in edge_labels:
  768. # coords = {}
  769. # for key, val in edge_label.items():
  770. # label = float(val)
  771. # sums[key] += label
  772. # coords[key] = label
  773. # edge_labels_as_coords.append(coords)
  774. # median = {}
  775. # for key, val in sums.items():
  776. # median[key] = val / len(edge_labels)
  777. #
  778. # # Run main loop of Weiszfeld's Algorithm.
  779. # epsilon = 0.0001
  780. # delta = 1.0
  781. # num_itrs = 0
  782. # all_equal = False
  783. # while ((delta > epsilon) and (num_itrs < 100) and (not all_equal)):
  784. # numerator = {}
  785. # for key, val in sums.items():
  786. # numerator[key] = 0
  787. # denominator = 0
  788. # for edge_label_as_coord in edge_labels_as_coords:
  789. # norm = 0
  790. # for key, val in edge_label_as_coord.items():
  791. # norm += (val - median[key]) ** 2
  792. # norm += np.sqrt(norm)
  793. # if norm > 0:
  794. # for key, val in edge_label_as_coord.items():
  795. # numerator[key] += val / norm
  796. # denominator += 1.0 / norm
  797. # if denominator == 0:
  798. # all_equal = True
  799. # else:
  800. # new_median = {}
  801. # delta = 0.0
  802. # for key, val in numerator.items():
  803. # this_median = val / denominator
  804. # new_median[key] = this_median
  805. # delta += np.abs(median[key] - this_median)
  806. # median = new_median
  807. #
  808. # num_itrs += 1
  809. #
  810. # # Transform the solution to ged::GXLLabel and return it.
  811. # median_label = {}
  812. # for key, val in median.items():
  813. # median_label[key] = str(val)
  814. # return median_label

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