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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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, NodeMap
  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): # @todo: differ dummy_node from undifined node?
  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.__update_order = True
  45. self.__refine = True
  46. self.__time_limit_in_sec = 0
  47. self.__epsilon = 0.0001
  48. self.__max_itrs = 100
  49. self.__max_itrs_without_update = 3
  50. self.__num_inits_increase_order = 10
  51. self.__init_type_increase_order = 'K-MEANS++'
  52. self.__max_itrs_increase_order = 10
  53. self.__print_to_stdout = 2
  54. self.__median_id = np.inf # @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 == 'update-order':
  111. if opt_val == 'TRUE':
  112. self.__update_order = True
  113. elif opt_val == 'FALSE':
  114. self.__update_order = False
  115. else:
  116. raise Exception('Invalid argument "' + opt_val + '" for option update-order. Usage: options = "[--update-order TRUE|FALSE] [...]"')
  117. elif opt_name == 'refine':
  118. if opt_val == 'TRUE':
  119. self.__refine = True
  120. elif opt_val == 'FALSE':
  121. self.__refine = False
  122. else:
  123. raise Exception('Invalid argument "' + opt_val + '" for option refine. Usage: options = "[--refine TRUE|FALSE] [...]"')
  124. elif opt_name == 'time-limit':
  125. try:
  126. self.__time_limit_in_sec = float(opt_val)
  127. except:
  128. raise Exception('Invalid argument "' + opt_val + '" for option time-limit. Usage: options = "[--time-limit <convertible to double>] [...]')
  129. elif opt_name == 'max-itrs':
  130. try:
  131. self.__max_itrs = int(opt_val)
  132. except:
  133. raise Exception('Invalid argument "' + opt_val + '" for option max-itrs. Usage: options = "[--max-itrs <convertible to int>] [...]')
  134. elif opt_name == 'max-itrs-without-update':
  135. try:
  136. self.__max_itrs_without_update = int(opt_val)
  137. except:
  138. raise Exception('Invalid argument "' + opt_val + '" for option max-itrs-without-update. Usage: options = "[--max-itrs-without-update <convertible to int>] [...]')
  139. elif opt_name == 'seed':
  140. try:
  141. self.__seed = int(opt_val)
  142. except:
  143. raise Exception('Invalid argument "' + opt_val + '" for option seed. Usage: options = "[--seed <convertible to int greater equal 0>] [...]')
  144. elif opt_name == 'epsilon':
  145. try:
  146. self.__epsilon = float(opt_val)
  147. except:
  148. raise Exception('Invalid argument "' + opt_val + '" for option epsilon. Usage: options = "[--epsilon <convertible to double greater 0>] [...]')
  149. if self.__epsilon <= 0:
  150. raise Exception('Invalid argument "' + opt_val + '" for option epsilon. Usage: options = "[--epsilon <convertible to double greater 0>] [...]')
  151. elif opt_name == 'inits-increase-order':
  152. try:
  153. self.__num_inits_increase_order = int(opt_val)
  154. except:
  155. raise Exception('Invalid argument "' + opt_val + '" for option inits-increase-order. Usage: options = "[--inits-increase-order <convertible to int greater 0>]"')
  156. if self.__num_inits_increase_order <= 0:
  157. raise Exception('Invalid argument "' + opt_val + '" for option inits-increase-order. Usage: options = "[--inits-increase-order <convertible to int greater 0>]"')
  158. elif opt_name == 'init-type-increase-order':
  159. self.__init_type_increase_order = opt_val
  160. if opt_val != 'CLUSTERS' and opt_val != 'K-MEANS++':
  161. raise Exception('Invalid argument ' + opt_val + ' for option init-type-increase-order. Usage: options = "[--init-type-increase-order CLUSTERS|K-MEANS++] [...]"')
  162. elif opt_name == 'max-itrs-increase-order':
  163. try:
  164. self.__max_itrs_increase_order = int(opt_val)
  165. except:
  166. raise Exception('Invalid argument "' + opt_val + '" for option max-itrs-increase-order. Usage: options = "[--max-itrs-increase-order <convertible to int>] [...]')
  167. else:
  168. valid_options = '[--init-type <arg>] [--random-inits <arg>] [--randomness <arg>] [--seed <arg>] [--stdout <arg>] '
  169. valid_options += '[--time-limit <arg>] [--max-itrs <arg>] [--epsilon <arg>] '
  170. valid_options += '[--inits-increase-order <arg>] [--init-type-increase-order <arg>] [--max-itrs-increase-order <arg>]'
  171. raise Exception('Invalid option "' + opt_name + '". Usage: options = "' + valid_options + '"')
  172. def set_init_method(self, init_method, init_options=''):
  173. """Selects method to be used for computing the initial medoid graph.
  174. Parameters
  175. ----------
  176. init_method : string
  177. The selected method. Default: ged::Options::GEDMethod::BRANCH_UNIFORM.
  178. init_options : string
  179. The options for the selected method. Default: "".
  180. Notes
  181. -----
  182. Has no effect unless "--init-type MEDOID" is passed to set_options().
  183. """
  184. self.__init_method = init_method;
  185. self.__init_options = init_options;
  186. def set_descent_method(self, descent_method, descent_options=''):
  187. """Selects method to be used for block gradient descent..
  188. Parameters
  189. ----------
  190. descent_method : string
  191. The selected method. Default: ged::Options::GEDMethod::BRANCH_FAST.
  192. descent_options : string
  193. The options for the selected method. Default: "".
  194. Notes
  195. -----
  196. Has no effect unless "--init-type MEDOID" is passed to set_options().
  197. """
  198. self.__descent_method = descent_method;
  199. self.__descent_options = descent_options;
  200. def set_refine_method(self, refine_method, refine_options):
  201. """Selects method to be used for improving the sum of distances and the node maps for the converged median.
  202. Parameters
  203. ----------
  204. refine_method : string
  205. The selected method. Default: "IPFP".
  206. refine_options : string
  207. The options for the selected method. Default: "".
  208. Notes
  209. -----
  210. Has no effect if "--refine FALSE" is passed to set_options().
  211. """
  212. self.__refine_method = refine_method
  213. self.__refine_options = refine_options
  214. def run(self, graph_ids, set_median_id, gen_median_id):
  215. """Computes a generalized median graph.
  216. Parameters
  217. ----------
  218. graph_ids : list[integer]
  219. The IDs of the graphs for which the median should be computed. Must have been added to the environment passed to the constructor.
  220. set_median_id : integer
  221. 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().
  222. gen_median_id : integer
  223. The ID of the computed generalized median. Upon termination, the computed median can be obtained via gklearn.gedlib.gedlibpy.GEDEnv.get_graph().
  224. """
  225. # Sanity checks.
  226. if len(graph_ids) == 0:
  227. raise Exception('Empty vector of graph IDs, unable to compute median.')
  228. all_graphs_empty = True
  229. for graph_id in graph_ids:
  230. if self.__ged_env.get_graph_num_nodes(graph_id) > 0:
  231. all_graphs_empty = False
  232. break
  233. if all_graphs_empty:
  234. raise Exception('All graphs in the collection are empty.')
  235. # Start timer and record start time.
  236. start = time.time()
  237. timer = Timer(self.__time_limit_in_sec)
  238. self.__median_id = gen_median_id
  239. self.__state = AlgorithmState.TERMINATED
  240. # Get ExchangeGraph representations of the input graphs.
  241. graphs = {}
  242. for graph_id in graph_ids:
  243. # @todo: get_nx_graph() function may need to be modified according to the coming code.
  244. graphs[graph_id] = self.__ged_env.get_nx_graph(graph_id, True, True, False)
  245. # print(self.__ged_env.get_graph_internal_id(0))
  246. # print(graphs[0].graph)
  247. # print(graphs[0].nodes(data=True))
  248. # print(graphs[0].edges(data=True))
  249. # print(nx.adjacency_matrix(graphs[0]))
  250. # Construct initial medians.
  251. medians = []
  252. self.__construct_initial_medians(graph_ids, timer, medians)
  253. end_init = time.time()
  254. self.__runtime_initialized = end_init - start
  255. # print(medians[0].graph)
  256. # print(medians[0].nodes(data=True))
  257. # print(medians[0].edges(data=True))
  258. # print(nx.adjacency_matrix(medians[0]))
  259. # Reset information about iterations and number of times the median decreases and increases.
  260. self.__itrs = [0] * len(medians)
  261. self.__num_decrease_order = 0
  262. self.__num_increase_order = 0
  263. self.__num_converged_descents = 0
  264. # Initialize the best median.
  265. best_sum_of_distances = np.inf
  266. self.__best_init_sum_of_distances = np.inf
  267. node_maps_from_best_median = {}
  268. # Run block gradient descent from all initial medians.
  269. self.__ged_env.set_method(self.__descent_method, self.__descent_options)
  270. for median_pos in range(0, len(medians)):
  271. # Terminate if the timer has expired and at least one SOD has been computed.
  272. if timer.expired() and median_pos > 0:
  273. break
  274. # Print information about current iteration.
  275. if self.__print_to_stdout == 2:
  276. print('\n===========================================================')
  277. print('Block gradient descent for initial median', str(median_pos + 1), 'of', str(len(medians)), '.')
  278. print('-----------------------------------------------------------')
  279. # Get reference to the median.
  280. median = medians[median_pos]
  281. # Load initial median into the environment.
  282. self.__ged_env.load_nx_graph(median, gen_median_id)
  283. self.__ged_env.init(self.__ged_env.get_init_type())
  284. # Print information about current iteration.
  285. if self.__print_to_stdout == 2:
  286. progress = tqdm(desc='Computing initial node maps', total=len(graph_ids), file=sys.stdout)
  287. # Compute node maps and sum of distances for initial median.
  288. self.__sum_of_distances = 0
  289. self.__node_maps_from_median.clear()
  290. for graph_id in graph_ids:
  291. self.__ged_env.run_method(gen_median_id, graph_id)
  292. self.__node_maps_from_median[graph_id] = self.__ged_env.get_node_map(gen_median_id, graph_id)
  293. # print(self.__node_maps_from_median[graph_id])
  294. self.__sum_of_distances += self.__node_maps_from_median[graph_id].induced_cost()
  295. # print(self.__sum_of_distances)
  296. # Print information about current iteration.
  297. if self.__print_to_stdout == 2:
  298. progress.update(1)
  299. self.__best_init_sum_of_distances = min(self.__best_init_sum_of_distances, self.__sum_of_distances)
  300. self.__ged_env.load_nx_graph(median, set_median_id)
  301. # print(self.__best_init_sum_of_distances)
  302. # Print information about current iteration.
  303. if self.__print_to_stdout == 2:
  304. print('\n')
  305. # Run block gradient descent from initial median.
  306. converged = False
  307. itrs_without_update = 0
  308. while not self.__termination_criterion_met(converged, timer, self.__itrs[median_pos], itrs_without_update):
  309. # Print information about current iteration.
  310. if self.__print_to_stdout == 2:
  311. print('\n===========================================================')
  312. print('Iteration', str(self.__itrs[median_pos] + 1), 'for initial median', str(median_pos + 1), 'of', str(len(medians)), '.')
  313. print('-----------------------------------------------------------')
  314. # Initialize flags that tell us what happened in the iteration.
  315. median_modified = False
  316. node_maps_modified = False
  317. decreased_order = False
  318. increased_order = False
  319. # Update the median.
  320. median_modified = self.__update_median(graphs, median)
  321. if self.__update_order:
  322. if not median_modified or self.__itrs[median_pos] == 0:
  323. decreased_order = self.__decrease_order(graphs, median)
  324. if not decreased_order or self.__itrs[median_pos] == 0:
  325. increased_order = self.__increase_order(graphs, median)
  326. # Update the number of iterations without update of the median.
  327. if median_modified or decreased_order or increased_order:
  328. itrs_without_update = 0
  329. else:
  330. itrs_without_update += 1
  331. # Print information about current iteration.
  332. if self.__print_to_stdout == 2:
  333. print('Loading median to environment: ... ', end='')
  334. # Load the median into the environment.
  335. # @todo: should this function use the original node label?
  336. self.__ged_env.load_nx_graph(median, gen_median_id)
  337. self.__ged_env.init(self.__ged_env.get_init_type())
  338. # Print information about current iteration.
  339. if self.__print_to_stdout == 2:
  340. print('done.')
  341. # Print information about current iteration.
  342. if self.__print_to_stdout == 2:
  343. print('Updating induced costs: ... ', end='')
  344. # Compute induced costs of the old node maps w.r.t. the updated median.
  345. for graph_id in graph_ids:
  346. # print(self.__node_maps_from_median[graph_id].induced_cost())
  347. # xxx = self.__node_maps_from_median[graph_id]
  348. self.__ged_env.compute_induced_cost(gen_median_id, graph_id, self.__node_maps_from_median[graph_id])
  349. # print('---------------------------------------')
  350. # print(self.__node_maps_from_median[graph_id].induced_cost())
  351. # @todo:!!!!!!!!!!!!!!!!!!!!!!!!!!!!This value is a slight different from the c++ program, which might be a bug! Use it very carefully!
  352. # Print information about current iteration.
  353. if self.__print_to_stdout == 2:
  354. print('done.')
  355. # Update the node maps.
  356. node_maps_modified = self.__update_node_maps()
  357. # Update the order of the median if no improvement can be found with the current order.
  358. # Update the sum of distances.
  359. old_sum_of_distances = self.__sum_of_distances
  360. self.__sum_of_distances = 0
  361. for graph_id, node_map in self.__node_maps_from_median.items():
  362. self.__sum_of_distances += node_map.induced_cost()
  363. # print(self.__sum_of_distances)
  364. # Print information about current iteration.
  365. if self.__print_to_stdout == 2:
  366. print('Old local SOD: ', old_sum_of_distances)
  367. print('New local SOD: ', self.__sum_of_distances)
  368. print('Best converged SOD: ', best_sum_of_distances)
  369. print('Modified median: ', median_modified)
  370. print('Modified node maps: ', node_maps_modified)
  371. print('Decreased order: ', decreased_order)
  372. print('Increased order: ', increased_order)
  373. print('===========================================================\n')
  374. converged = not (median_modified or node_maps_modified or decreased_order or increased_order)
  375. self.__itrs[median_pos] += 1
  376. # Update the best median.
  377. if self.__sum_of_distances < best_sum_of_distances:
  378. best_sum_of_distances = self.__sum_of_distances
  379. node_maps_from_best_median = self.__node_maps_from_median.copy() # @todo: this is a shallow copy, not sure if it is enough.
  380. best_median = median
  381. # Update the number of converged descents.
  382. if converged:
  383. self.__num_converged_descents += 1
  384. # Store the best encountered median.
  385. self.__sum_of_distances = best_sum_of_distances
  386. self.__node_maps_from_median = node_maps_from_best_median
  387. self.__ged_env.load_nx_graph(best_median, gen_median_id)
  388. self.__ged_env.init(self.__ged_env.get_init_type())
  389. end_descent = time.time()
  390. self.__runtime_converged = end_descent - start
  391. # Refine the sum of distances and the node maps for the converged median.
  392. self.__converged_sum_of_distances = self.__sum_of_distances
  393. if self.__refine:
  394. self.__improve_sum_of_distances(timer)
  395. # Record end time, set runtime and reset the number of initial medians.
  396. end = time.time()
  397. self.__runtime = end - start
  398. self.__num_random_inits = self.__desired_num_random_inits
  399. # Print global information.
  400. if self.__print_to_stdout != 0:
  401. print('\n===========================================================')
  402. print('Finished computation of generalized median graph.')
  403. print('-----------------------------------------------------------')
  404. print('Best SOD after initialization: ', self.__best_init_sum_of_distances)
  405. print('Converged SOD: ', self.__converged_sum_of_distances)
  406. if self.__refine:
  407. print('Refined SOD: ', self.__sum_of_distances)
  408. print('Overall runtime: ', self.__runtime)
  409. print('Runtime of initialization: ', self.__runtime_initialized)
  410. print('Runtime of block gradient descent: ', self.__runtime_converged - self.__runtime_initialized)
  411. if self.__refine:
  412. print('Runtime of refinement: ', self.__runtime - self.__runtime_converged)
  413. print('Number of initial medians: ', len(medians))
  414. total_itr = 0
  415. num_started_descents = 0
  416. for itr in self.__itrs:
  417. total_itr += itr
  418. if itr > 0:
  419. num_started_descents += 1
  420. print('Size of graph collection: ', len(graph_ids))
  421. print('Number of started descents: ', num_started_descents)
  422. print('Number of converged descents: ', self.__num_converged_descents)
  423. print('Overall number of iterations: ', total_itr)
  424. print('Overall number of times the order decreased: ', self.__num_decrease_order)
  425. print('Overall number of times the order increased: ', self.__num_increase_order)
  426. print('===========================================================\n')
  427. def __improve_sum_of_distances(self, timer): # @todo: go through and test
  428. # Use method selected for refinement phase.
  429. self.__ged_env.set_method(self.__refine_method, self.__refine_options)
  430. # Print information about current iteration.
  431. if self.__print_to_stdout == 2:
  432. progress = tqdm(desc='Improving node maps', total=len(self.__node_maps_from_median), file=sys.stdout)
  433. print('\n===========================================================')
  434. print('Improving node maps and SOD for converged median.')
  435. print('-----------------------------------------------------------')
  436. progress.update(1)
  437. # Improving the node maps.
  438. for graph_id, node_map in self.__node_maps_from_median.items():
  439. if time.expired():
  440. if self.__state == AlgorithmState.TERMINATED:
  441. self.__state = AlgorithmState.CONVERGED
  442. break
  443. self.__ged_env.run_method(self.__gen_median_id, graph_id)
  444. if self.__ged_env.get_upper_bound(self.__gen_median_id, graph_id) < node_map.induced_cost():
  445. self.__node_maps_from_median[graph_id] = self.__ged_env.get_node_map(self.__gen_median_id, graph_id)
  446. self.__sum_of_distances += self.__node_maps_from_median[graph_id].induced_cost()
  447. # Print information.
  448. if self.__print_to_stdout == 2:
  449. progress.update(1)
  450. self.__sum_of_distances = 0.0
  451. for key, val in self.__node_maps_from_median.items():
  452. self.__sum_of_distances += val.induced_cost()
  453. # Print information.
  454. if self.__print_to_stdout == 2:
  455. print('===========================================================\n')
  456. def __median_available(self):
  457. return self.__gen_median_id != np.inf
  458. def get_state(self):
  459. if not self.__median_available():
  460. raise Exception('No median has been computed. Call run() before calling get_state().')
  461. return self.__state
  462. def get_sum_of_distances(self, state=''):
  463. """Returns the sum of distances.
  464. Parameters
  465. ----------
  466. state : string
  467. The state of the estimator. Can be 'initialized' or 'converged'. Default: ""
  468. Returns
  469. -------
  470. float
  471. 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.
  472. """
  473. if not self.__median_available():
  474. raise Exception('No median has been computed. Call run() before calling get_sum_of_distances().')
  475. if state == 'initialized':
  476. return self.__best_init_sum_of_distances
  477. if state == 'converged':
  478. return self.__converged_sum_of_distances
  479. return self.__sum_of_distances
  480. def get_runtime(self, state):
  481. if not self.__median_available():
  482. raise Exception('No median has been computed. Call run() before calling get_runtime().')
  483. if state == AlgorithmState.INITIALIZED:
  484. return self.__runtime_initialized
  485. if state == AlgorithmState.CONVERGED:
  486. return self.__runtime_converged
  487. return self.__runtime
  488. def get_num_itrs(self):
  489. if not self.__median_available():
  490. raise Exception('No median has been computed. Call run() before calling get_num_itrs().')
  491. return self.__itrs
  492. def get_num_times_order_decreased(self):
  493. if not self.__median_available():
  494. raise Exception('No median has been computed. Call run() before calling get_num_times_order_decreased().')
  495. return self.__num_decrease_order
  496. def get_num_times_order_increased(self):
  497. if not self.__median_available():
  498. raise Exception('No median has been computed. Call run() before calling get_num_times_order_increased().')
  499. return self.__num_increase_order
  500. def get_num_converged_descents(self):
  501. if not self.__median_available():
  502. raise Exception('No median has been computed. Call run() before calling get_num_converged_descents().')
  503. return self.__num_converged_descents
  504. def get_ged_env(self):
  505. return self.__ged_env
  506. def __set_default_options(self):
  507. self.__init_type = 'RANDOM'
  508. self.__num_random_inits = 10
  509. self.__desired_num_random_inits = 10
  510. self.__use_real_randomness = True
  511. self.__seed = 0
  512. self.__update_order = True
  513. self.__refine = True
  514. self.__time_limit_in_sec = 0
  515. self.__epsilon = 0.0001
  516. self.__max_itrs = 100
  517. self.__max_itrs_without_update = 3
  518. self.__num_inits_increase_order = 10
  519. self.__init_type_increase_order = 'K-MEANS++'
  520. self.__max_itrs_increase_order = 10
  521. self.__print_to_stdout = 2
  522. self.__label_names = {}
  523. def __construct_initial_medians(self, graph_ids, timer, initial_medians):
  524. # Print information about current iteration.
  525. if self.__print_to_stdout == 2:
  526. print('\n===========================================================')
  527. print('Constructing initial median(s).')
  528. print('-----------------------------------------------------------')
  529. # Compute or sample the initial median(s).
  530. initial_medians.clear()
  531. if self.__init_type == 'MEDOID':
  532. self.__compute_medoid(graph_ids, timer, initial_medians)
  533. elif self.__init_type == 'MAX':
  534. pass # @todo
  535. # compute_max_order_graph_(graph_ids, initial_medians)
  536. elif self.__init_type == 'MIN':
  537. pass # @todo
  538. # compute_min_order_graph_(graph_ids, initial_medians)
  539. elif self.__init_type == 'MEAN':
  540. pass # @todo
  541. # compute_mean_order_graph_(graph_ids, initial_medians)
  542. else:
  543. pass # @todo
  544. # sample_initial_medians_(graph_ids, initial_medians)
  545. # Print information about current iteration.
  546. if self.__print_to_stdout == 2:
  547. print('===========================================================')
  548. def __compute_medoid(self, graph_ids, timer, initial_medians):
  549. # Use method selected for initialization phase.
  550. self.__ged_env.set_method(self.__init_method, self.__init_options)
  551. # Print information about current iteration.
  552. if self.__print_to_stdout == 2:
  553. progress = tqdm(desc='Computing medoid', total=len(graph_ids), file=sys.stdout)
  554. # Compute the medoid.
  555. medoid_id = graph_ids[0]
  556. best_sum_of_distances = np.inf
  557. for g_id in graph_ids:
  558. if timer.expired():
  559. self.__state = AlgorithmState.CALLED
  560. break
  561. sum_of_distances = 0
  562. for h_id in graph_ids:
  563. self.__ged_env.run_method(g_id, h_id)
  564. sum_of_distances += self.__ged_env.get_upper_bound(g_id, h_id)
  565. if sum_of_distances < best_sum_of_distances:
  566. best_sum_of_distances = sum_of_distances
  567. medoid_id = g_id
  568. # Print information about current iteration.
  569. if self.__print_to_stdout == 2:
  570. progress.update(1)
  571. initial_medians.append(self.__ged_env.get_nx_graph(medoid_id, True, True, False)) # @todo
  572. # Print information about current iteration.
  573. if self.__print_to_stdout == 2:
  574. print('\n')
  575. def __termination_criterion_met(self, converged, timer, itr, itrs_without_update):
  576. if timer.expired() or (itr >= self.__max_itrs if self.__max_itrs >= 0 else False):
  577. if self.__state == AlgorithmState.TERMINATED:
  578. self.__state = AlgorithmState.INITIALIZED
  579. return True
  580. return converged or (itrs_without_update > self.__max_itrs_without_update if self.__max_itrs_without_update >= 0 else False)
  581. def __update_median(self, graphs, median):
  582. # Print information about current iteration.
  583. if self.__print_to_stdout == 2:
  584. print('Updating median: ', end='')
  585. # Store copy of the old median.
  586. old_median = median.copy() # @todo: this is just a shallow copy.
  587. # Update the node labels.
  588. if self.__labeled_nodes:
  589. self.__update_node_labels(graphs, median)
  590. # Update the edges and their labels.
  591. self.__update_edges(graphs, median)
  592. # Print information about current iteration.
  593. if self.__print_to_stdout == 2:
  594. print('done.')
  595. return not self.__are_graphs_equal(median, old_median)
  596. def __update_node_labels(self, graphs, median):
  597. # Print information about current iteration.
  598. if self.__print_to_stdout == 2:
  599. print('nodes ... ', end='')
  600. # Iterate through all nodes of the median.
  601. for i in range(0, nx.number_of_nodes(median)):
  602. # print('i: ', i)
  603. # Collect the labels of the substituted nodes.
  604. node_labels = []
  605. for graph_id, graph in graphs.items():
  606. # print('graph_id: ', graph_id)
  607. # print(self.__node_maps_from_median[graph_id])
  608. k = self.__node_maps_from_median[graph_id].image(i)
  609. # print('k: ', k)
  610. if k != np.inf:
  611. node_labels.append(graph.nodes[k])
  612. # Compute the median label and update the median.
  613. if len(node_labels) > 0:
  614. # median_label = self.__ged_env.get_median_node_label(node_labels)
  615. median_label = self.__get_median_node_label(node_labels)
  616. if self.__ged_env.get_node_rel_cost(median.nodes[i], median_label) > self.__epsilon:
  617. nx.set_node_attributes(median, {i: median_label})
  618. def __update_edges(self, graphs, median):
  619. # Print information about current iteration.
  620. if self.__print_to_stdout == 2:
  621. print('edges ... ', end='')
  622. # # Clear the adjacency lists of the median and reset number of edges to 0.
  623. # median_edges = list(median.edges)
  624. # for (head, tail) in median_edges:
  625. # median.remove_edge(head, tail)
  626. # @todo: what if edge is not labeled?
  627. # Iterate through all possible edges (i,j) of the median.
  628. for i in range(0, nx.number_of_nodes(median)):
  629. for j in range(i + 1, nx.number_of_nodes(median)):
  630. # Collect the labels of the edges to which (i,j) is mapped by the node maps.
  631. edge_labels = []
  632. for graph_id, graph in graphs.items():
  633. k = self.__node_maps_from_median[graph_id].image(i)
  634. l = self.__node_maps_from_median[graph_id].image(j)
  635. if k != np.inf and l != np.inf:
  636. if graph.has_edge(k, l):
  637. edge_labels.append(graph.edges[(k, l)])
  638. # Compute the median edge label and the overall edge relabeling cost.
  639. rel_cost = 0
  640. median_label = self.__ged_env.get_edge_label(1)
  641. if median.has_edge(i, j):
  642. median_label = median.edges[(i, j)]
  643. if self.__labeled_edges and len(edge_labels) > 0:
  644. new_median_label = self.__get_median_edge_label(edge_labels)
  645. if self.__ged_env.get_edge_rel_cost(median_label, new_median_label) > self.__epsilon:
  646. median_label = new_median_label
  647. for edge_label in edge_labels:
  648. rel_cost += self.__ged_env.get_edge_rel_cost(median_label, edge_label)
  649. # Update the median.
  650. if median.has_edge(i, j):
  651. median.remove_edge(i, j)
  652. if rel_cost < (self.__edge_ins_cost + self.__edge_del_cost) * len(edge_labels) - self.__edge_del_cost * len(graphs):
  653. median.add_edge(i, j, **median_label)
  654. # else:
  655. # if median.has_edge(i, j):
  656. # median.remove_edge(i, j)
  657. def __update_node_maps(self):
  658. # Print information about current iteration.
  659. if self.__print_to_stdout == 2:
  660. progress = tqdm(desc='Updating node maps', total=len(self.__node_maps_from_median), file=sys.stdout)
  661. # Update the node maps.
  662. node_maps_were_modified = False
  663. for graph_id, node_map in self.__node_maps_from_median.items():
  664. self.__ged_env.run_method(self.__median_id, graph_id)
  665. if self.__ged_env.get_upper_bound(self.__median_id, graph_id) < node_map.induced_cost() - self.__epsilon:
  666. # xxx = self.__node_maps_from_median[graph_id]
  667. self.__node_maps_from_median[graph_id] = self.__ged_env.get_node_map(self.__median_id, graph_id)
  668. # yyy = self.__node_maps_from_median[graph_id]
  669. node_maps_were_modified = True
  670. # Print information about current iteration.
  671. if self.__print_to_stdout == 2:
  672. progress.update(1)
  673. # Print information about current iteration.
  674. if self.__print_to_stdout == 2:
  675. print('\n')
  676. # Return true if the node maps were modified.
  677. return node_maps_were_modified
  678. def __decrease_order(self, graphs, median):
  679. # Print information about current iteration
  680. if self.__print_to_stdout == 2:
  681. print('Trying to decrease order: ... ', end='')
  682. # Initialize ID of the node that is to be deleted.
  683. id_deleted_node = [None] # @todo: or np.inf
  684. decreased_order = False
  685. # Decrease the order as long as the best deletion delta is negative.
  686. while self.__compute_best_deletion_delta(graphs, median, id_deleted_node) < -self.__epsilon:
  687. decreased_order = True
  688. median = self.__delete_node_from_median(id_deleted_node[0], median)
  689. # Print information about current iteration.
  690. if self.__print_to_stdout == 2:
  691. print('done.')
  692. # Return true iff the order was decreased.
  693. return decreased_order
  694. def __compute_best_deletion_delta(self, graphs, median, id_deleted_node):
  695. best_delta = 0.0
  696. # Determine node that should be deleted (if any).
  697. for i in range(0, nx.number_of_nodes(median)):
  698. # Compute cost delta.
  699. delta = 0.0
  700. for graph_id, graph in graphs.items():
  701. k = self.__node_maps_from_median[graph_id].image(i)
  702. if k == np.inf:
  703. delta -= self.__node_del_cost
  704. else:
  705. delta += self.__node_ins_cost - self.__ged_env.get_node_rel_cost(median.nodes[i], graph.nodes[k])
  706. for j, j_label in median[i].items():
  707. l = self.__node_maps_from_median[graph_id].image(j)
  708. if k == np.inf or l == np.inf:
  709. delta -= self.__edge_del_cost
  710. elif not graph.has_edge(k, l):
  711. delta -= self.__edge_del_cost
  712. else:
  713. delta += self.__edge_ins_cost - self.__ged_env.get_edge_rel_cost(j_label, graph.edges[(k, l)])
  714. # Update best deletion delta.
  715. if delta < best_delta - self.__epsilon:
  716. best_delta = delta
  717. id_deleted_node[0] = i
  718. # id_deleted_node[0] = 3 # @todo:
  719. return best_delta
  720. def __delete_node_from_median(self, id_deleted_node, median):
  721. # Update the median.
  722. median.remove_node(id_deleted_node)
  723. median = nx.convert_node_labels_to_integers(median, first_label=0, ordering='default', label_attribute=None) # @todo: This doesn't guarantee that the order is the same as in G.
  724. # Update the node maps.
  725. for key, node_map in self.__node_maps_from_median.items():
  726. new_node_map = NodeMap(nx.number_of_nodes(median), node_map.num_target_nodes())
  727. is_unassigned_target_node = [True] * node_map.num_target_nodes()
  728. for i in range(0, nx.number_of_nodes(median) + 1):
  729. if i != id_deleted_node:
  730. new_i = (i if i < id_deleted_node else i - 1)
  731. k = node_map.image(i)
  732. new_node_map.add_assignment(new_i, k)
  733. if k != np.inf:
  734. is_unassigned_target_node[k] = False
  735. for k in range(0, node_map.num_target_nodes()):
  736. if is_unassigned_target_node[k]:
  737. new_node_map.add_assignment(np.inf, k)
  738. # print(new_node_map.get_forward_map(), new_node_map.get_backward_map())
  739. self.__node_maps_from_median[key] = new_node_map
  740. # Increase overall number of decreases.
  741. self.__num_decrease_order += 1
  742. return median
  743. def __increase_order(self, graphs, median):
  744. # Print information about current iteration.
  745. if self.__print_to_stdout == 2:
  746. print('Trying to increase order: ... ', end='')
  747. # Initialize the best configuration and the best label of the node that is to be inserted.
  748. best_config = {}
  749. best_label = self.__ged_env.get_node_label(1)
  750. increased_order = False
  751. # Increase the order as long as the best insertion delta is negative.
  752. while self.__compute_best_insertion_delta(graphs, best_config, best_label) < - self.__epsilon:
  753. increased_order = True
  754. self.__add_node_to_median(best_config, best_label, median)
  755. # Print information about current iteration.
  756. if self.__print_to_stdout == 2:
  757. print('done.')
  758. # Return true iff the order was increased.
  759. return increased_order
  760. def __compute_best_insertion_delta(self, graphs, best_config, best_label):
  761. # Construct sets of inserted nodes.
  762. no_inserted_node = True
  763. inserted_nodes = {}
  764. for graph_id, graph in graphs.items():
  765. inserted_nodes[graph_id] = []
  766. best_config[graph_id] = np.inf
  767. for k in range(nx.number_of_nodes(graph)):
  768. if self.__node_maps_from_median[graph_id].pre_image(k) == np.inf:
  769. no_inserted_node = False
  770. inserted_nodes[graph_id].append((k, tuple(item for item in graph.nodes[k].items()))) # @todo: can order of label names be garantteed?
  771. # Return 0.0 if no node is inserted in any of the graphs.
  772. if no_inserted_node:
  773. return 0.0
  774. # Compute insertion configuration, label, and delta.
  775. best_delta = 0.0 # @todo
  776. if len(self.__label_names['node_labels']) == 0 and len(self.__label_names['node_attrs']) == 0: # @todo
  777. best_delta = self.__compute_insertion_delta_unlabeled(inserted_nodes, best_config, best_label)
  778. elif len(self.__label_names['node_labels']) > 0: # self.__constant_node_costs:
  779. best_delta = self.__compute_insertion_delta_constant(inserted_nodes, best_config, best_label)
  780. else:
  781. best_delta = self.__compute_insertion_delta_generic(inserted_nodes, best_config, best_label)
  782. # Return the best delta.
  783. return best_delta
  784. def __compute_insertion_delta_unlabeled(self, inserted_nodes, best_config, best_label): # @todo: go through and test.
  785. # Construct the nest configuration and compute its insertion delta.
  786. best_delta = 0.0
  787. best_config.clear()
  788. for graph_id, node_set in inserted_nodes.items():
  789. if len(node_set) == 0:
  790. best_config[graph_id] = np.inf
  791. best_delta += self.__node_del_cost
  792. else:
  793. best_config[graph_id] = node_set[0][0]
  794. best_delta -= self.__node_ins_cost
  795. # Return the best insertion delta.
  796. return best_delta
  797. def __compute_insertion_delta_constant(self, inserted_nodes, best_config, best_label):
  798. # Construct histogram and inverse label maps.
  799. hist = {}
  800. inverse_label_maps = {}
  801. for graph_id, node_set in inserted_nodes.items():
  802. inverse_label_maps[graph_id] = {}
  803. for node in node_set:
  804. k = node[0]
  805. label = node[1]
  806. if label not in inverse_label_maps[graph_id]:
  807. inverse_label_maps[graph_id][label] = k
  808. if label not in hist:
  809. hist[label] = 1
  810. else:
  811. hist[label] += 1
  812. # Determine the best label.
  813. best_count = 0
  814. for key, val in hist.items():
  815. if val > best_count:
  816. best_count = val
  817. best_label_tuple = key
  818. # get best label.
  819. best_label.clear()
  820. for key, val in best_label_tuple:
  821. best_label[key] = val
  822. # Construct the best configuration and compute its insertion delta.
  823. best_config.clear()
  824. best_delta = 0.0
  825. node_rel_cost = self.__ged_env.get_node_rel_cost(self.__ged_env.get_node_label(1), self.__ged_env.get_node_label(2))
  826. triangle_ineq_holds = (node_rel_cost <= self.__node_del_cost + self.__node_ins_cost)
  827. for graph_id, _ in inserted_nodes.items():
  828. if best_label_tuple in inverse_label_maps[graph_id]:
  829. best_config[graph_id] = inverse_label_maps[graph_id][best_label_tuple]
  830. best_delta -= self.__node_ins_cost
  831. elif triangle_ineq_holds and not len(inserted_nodes[graph_id]) == 0:
  832. best_config[graph_id] = inserted_nodes[graph_id][0][0]
  833. best_delta += node_rel_cost - self.__node_ins_cost
  834. else:
  835. best_config[graph_id] = np.inf
  836. best_delta += self.__node_del_cost
  837. # Return the best insertion delta.
  838. return best_delta
  839. def __compute_insertion_delta_generic(self, inserted_nodes, best_config, best_label):
  840. # Collect all node labels of inserted nodes.
  841. node_labels = []
  842. for _, node_set in inserted_nodes.items():
  843. for node in node_set:
  844. node_labels.append(node[1])
  845. # Compute node label medians that serve as initial solutions for block gradient descent.
  846. initial_node_labels = []
  847. self.__compute_initial_node_labels(node_labels, initial_node_labels)
  848. # Determine best insertion configuration, label, and delta via parallel block gradient descent from all initial node labels.
  849. best_delta = 0.0
  850. for node_label in initial_node_labels:
  851. # Construct local configuration.
  852. config = {}
  853. for graph_id, _ in inserted_nodes.items():
  854. config[graph_id] = tuple((np.inf, tuple(item for item in self.__ged_env.get_node_label(1).items())))
  855. # Run block gradient descent.
  856. converged = False
  857. itr = 0
  858. while not self.__insertion_termination_criterion_met(converged, itr):
  859. converged = not self.__update_config(node_label, inserted_nodes, config, node_labels)
  860. node_label_dict = dict(node_label)
  861. converged = converged and (not self.__update_node_label([dict(item) for item in node_labels], node_label_dict)) # @todo: the dict is tupled again in the function, can be better.
  862. node_label = tuple(item for item in node_label_dict.items()) # @todo: watch out: initial_node_labels[i] is not modified here.
  863. itr += 1
  864. # Compute insertion delta of converged solution.
  865. delta = 0.0
  866. for _, node in config.items():
  867. if node[0] == np.inf:
  868. delta += self.__node_del_cost
  869. else:
  870. delta += self.__ged_env.get_node_rel_cost(dict(node_label), dict(node[1])) - self.__node_ins_cost
  871. # Update best delta and global configuration if improvement has been found.
  872. if delta < best_delta - self.__epsilon:
  873. best_delta = delta
  874. best_label.clear()
  875. for key, val in node_label:
  876. best_label[key] = val
  877. best_config.clear()
  878. for graph_id, val in config.items():
  879. best_config[graph_id] = val[0]
  880. # Return the best delta.
  881. return best_delta
  882. def __compute_initial_node_labels(self, node_labels, median_labels):
  883. median_labels.clear()
  884. if self.__use_real_randomness: # @todo: may not work if parallelized.
  885. rng = np.random.randint(0, high=2**32 - 1, size=1)
  886. urng = np.random.RandomState(seed=rng[0])
  887. else:
  888. urng = np.random.RandomState(seed=self.__seed)
  889. # Generate the initial node label medians.
  890. if self.__init_type_increase_order == 'K-MEANS++':
  891. # Use k-means++ heuristic to generate the initial node label medians.
  892. already_selected = [False] * len(node_labels)
  893. selected_label_id = urng.randint(low=0, high=len(node_labels), size=1)[0] # c++ test: 23
  894. median_labels.append(node_labels[selected_label_id])
  895. already_selected[selected_label_id] = True
  896. # xxx = [41, 0, 18, 9, 6, 14, 21, 25, 33] for c++ test
  897. # iii = 0 for c++ test
  898. while len(median_labels) < self.__num_inits_increase_order:
  899. weights = [np.inf] * len(node_labels)
  900. for label_id in range(0, len(node_labels)):
  901. if already_selected[label_id]:
  902. weights[label_id] = 0
  903. continue
  904. for label in median_labels:
  905. weights[label_id] = min(weights[label_id], self.__ged_env.get_node_rel_cost(dict(label), dict(node_labels[label_id])))
  906. sum_weight = np.sum(weights)
  907. if sum_weight == 0:
  908. p = np.array([1 / len(weights)] * len(weights))
  909. else:
  910. p = np.array(weights) / np.sum(weights)
  911. selected_label_id = urng.choice(range(0, len(weights)), size=1, p=p)[0] # for c++ test: xxx[iii]
  912. # iii += 1 for c++ test
  913. median_labels.append(node_labels[selected_label_id])
  914. already_selected[selected_label_id] = True
  915. else:
  916. # Compute the initial node medians as the medians of randomly generated clusters of (roughly) equal size.
  917. # @todo: go through and test.
  918. shuffled_node_labels = [np.inf] * len(node_labels) #@todo: random?
  919. # @todo: std::shuffle(shuffled_node_labels.begin(), shuffled_node_labels.end(), urng);?
  920. cluster_size = len(node_labels) / self.__num_inits_increase_order
  921. pos = 0.0
  922. cluster = []
  923. while len(median_labels) < self.__num_inits_increase_order - 1:
  924. while pos < (len(median_labels) + 1) * cluster_size:
  925. cluster.append(shuffled_node_labels[pos])
  926. pos += 1
  927. median_labels.append(self.__get_median_node_label(cluster))
  928. cluster.clear()
  929. while pos < len(shuffled_node_labels):
  930. pos += 1
  931. cluster.append(shuffled_node_labels[pos])
  932. median_labels.append(self.__get_median_node_label(cluster))
  933. cluster.clear()
  934. # Run Lloyd's Algorithm.
  935. converged = False
  936. closest_median_ids = [np.inf] * len(node_labels)
  937. clusters = [[] for _ in range(len(median_labels))]
  938. itr = 1
  939. while not self.__insertion_termination_criterion_met(converged, itr):
  940. converged = not self.__update_clusters(node_labels, median_labels, closest_median_ids)
  941. if not converged:
  942. for cluster in clusters:
  943. cluster.clear()
  944. for label_id in range(0, len(node_labels)):
  945. clusters[closest_median_ids[label_id]].append(node_labels[label_id])
  946. for cluster_id in range(0, len(clusters)):
  947. node_label = dict(median_labels[cluster_id])
  948. self.__update_node_label([dict(item) for item in clusters[cluster_id]], node_label) # @todo: the dict is tupled again in the function, can be better.
  949. median_labels[cluster_id] = tuple(item for item in node_label.items())
  950. itr += 1
  951. def __insertion_termination_criterion_met(self, converged, itr):
  952. return converged or (itr >= self.__max_itrs_increase_order if self.__max_itrs_increase_order > 0 else False)
  953. def __update_config(self, node_label, inserted_nodes, config, node_labels):
  954. # Determine the best configuration.
  955. config_modified = False
  956. for graph_id, node_set in inserted_nodes.items():
  957. best_assignment = config[graph_id]
  958. best_cost = 0.0
  959. if best_assignment[0] == np.inf:
  960. best_cost = self.__node_del_cost
  961. else:
  962. best_cost = self.__ged_env.get_node_rel_cost(dict(node_label), dict(best_assignment[1])) - self.__node_ins_cost
  963. for node in node_set:
  964. cost = self.__ged_env.get_node_rel_cost(dict(node_label), dict(node[1])) - self.__node_ins_cost
  965. if cost < best_cost - self.__epsilon:
  966. best_cost = cost
  967. best_assignment = node
  968. config_modified = True
  969. if self.__node_del_cost < best_cost - self.__epsilon:
  970. best_cost = self.__node_del_cost
  971. best_assignment = tuple((np.inf, best_assignment[1]))
  972. config_modified = True
  973. config[graph_id] = best_assignment
  974. # Collect the node labels contained in the best configuration.
  975. node_labels.clear()
  976. for key, val in config.items():
  977. if val[0] != np.inf:
  978. node_labels.append(val[1])
  979. # Return true if the configuration was modified.
  980. return config_modified
  981. def __update_node_label(self, node_labels, node_label):
  982. new_node_label = self.__get_median_node_label(node_labels)
  983. if self.__ged_env.get_node_rel_cost(new_node_label, node_label) > self.__epsilon:
  984. node_label.clear()
  985. for key, val in new_node_label.items():
  986. node_label[key] = val
  987. return True
  988. return False
  989. def __update_clusters(self, node_labels, median_labels, closest_median_ids):
  990. # Determine the closest median for each node label.
  991. clusters_modified = False
  992. for label_id in range(0, len(node_labels)):
  993. closest_median_id = np.inf
  994. dist_to_closest_median = np.inf
  995. for median_id in range(0, len(median_labels)):
  996. dist_to_median = self.__ged_env.get_node_rel_cost(dict(median_labels[median_id]), dict(node_labels[label_id]))
  997. if dist_to_median < dist_to_closest_median - self.__epsilon:
  998. dist_to_closest_median = dist_to_median
  999. closest_median_id = median_id
  1000. if closest_median_id != closest_median_ids[label_id]:
  1001. closest_median_ids[label_id] = closest_median_id
  1002. clusters_modified = True
  1003. # Return true if the clusters were modified.
  1004. return clusters_modified
  1005. def __add_node_to_median(self, best_config, best_label, median):
  1006. # Update the median.
  1007. median.add_node(nx.number_of_nodes(median), **best_label)
  1008. # Update the node maps.
  1009. for graph_id, node_map in self.__node_maps_from_median.items():
  1010. node_map_as_rel = []
  1011. node_map.as_relation(node_map_as_rel)
  1012. new_node_map = NodeMap(nx.number_of_nodes(median), node_map.num_target_nodes())
  1013. for assignment in node_map_as_rel:
  1014. new_node_map.add_assignment(assignment[0], assignment[1])
  1015. new_node_map.add_assignment(nx.number_of_nodes(median) - 1, best_config[graph_id])
  1016. self.__node_maps_from_median[graph_id] = new_node_map
  1017. # Increase overall number of increases.
  1018. self.__num_increase_order += 1
  1019. def __improve_sum_of_distances(self, timer):
  1020. pass
  1021. def __median_available(self):
  1022. return self.__median_id != np.inf
  1023. # def __get_node_image_from_map(self, node_map, node):
  1024. # """
  1025. # Return ID of the node mapping of `node` in `node_map`.
  1026. # Parameters
  1027. # ----------
  1028. # node_map : list[tuple(int, int)]
  1029. # List of node maps where the mapping node is found.
  1030. #
  1031. # node : int
  1032. # The mapping node of this node is returned
  1033. # Raises
  1034. # ------
  1035. # Exception
  1036. # If the node with ID `node` is not contained in the source nodes of the node map.
  1037. # Returns
  1038. # -------
  1039. # int
  1040. # ID of the mapping of `node`.
  1041. #
  1042. # Notes
  1043. # -----
  1044. # 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.
  1045. # """
  1046. # if node < len(node_map):
  1047. # return node_map[node][1] if node_map[node][1] < len(node_map) else np.inf
  1048. # else:
  1049. # raise Exception('The node with ID ', str(node), ' is not contained in the source nodes of the node map.')
  1050. # return np.inf
  1051. def __are_graphs_equal(self, g1, g2):
  1052. """
  1053. Check if the two graphs are equal.
  1054. Parameters
  1055. ----------
  1056. g1 : NetworkX graph object
  1057. Graph 1 to be compared.
  1058. g2 : NetworkX graph object
  1059. Graph 2 to be compared.
  1060. Returns
  1061. -------
  1062. bool
  1063. True if the two graph are equal.
  1064. Notes
  1065. -----
  1066. 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.
  1067. """
  1068. # check original node ids.
  1069. if not g1.graph['original_node_ids'] == g2.graph['original_node_ids']:
  1070. return False
  1071. # check nodes.
  1072. nlist1 = [n for n in g1.nodes(data=True)]
  1073. nlist2 = [n for n in g2.nodes(data=True)]
  1074. if not nlist1 == nlist2:
  1075. return False
  1076. # check edges.
  1077. elist1 = [n for n in g1.edges(data=True)]
  1078. elist2 = [n for n in g2.edges(data=True)]
  1079. if not elist1 == elist2:
  1080. return False
  1081. return True
  1082. def compute_my_cost(g, h, node_map):
  1083. cost = 0.0
  1084. for node in g.nodes:
  1085. cost += 0
  1086. def set_label_names(self, node_labels=[], edge_labels=[], node_attrs=[], edge_attrs=[]):
  1087. self.__label_names = {'node_labels': node_labels, 'edge_labels': edge_labels,
  1088. 'node_attrs': node_attrs, 'edge_attrs': edge_attrs}
  1089. def __get_median_node_label(self, node_labels):
  1090. if len(self.__label_names['node_labels']) > 0:
  1091. return self.__get_median_label_symbolic(node_labels)
  1092. elif len(self.__label_names['node_attrs']) > 0:
  1093. return self.__get_median_label_nonsymbolic(node_labels)
  1094. else:
  1095. raise Exception('Node label names are not given.')
  1096. def __get_median_edge_label(self, edge_labels):
  1097. if len(self.__label_names['edge_labels']) > 0:
  1098. return self.__get_median_label_symbolic(edge_labels)
  1099. elif len(self.__label_names['edge_attrs']) > 0:
  1100. return self.__get_median_label_nonsymbolic(edge_labels)
  1101. else:
  1102. raise Exception('Edge label names are not given.')
  1103. def __get_median_label_symbolic(self, labels):
  1104. # Construct histogram.
  1105. hist = {}
  1106. for label in labels:
  1107. label = tuple([kv for kv in label.items()]) # @todo: this may be slow.
  1108. if label not in hist:
  1109. hist[label] = 1
  1110. else:
  1111. hist[label] += 1
  1112. # Return the label that appears most frequently.
  1113. best_count = 0
  1114. median_label = {}
  1115. for label, count in hist.items():
  1116. if count > best_count:
  1117. best_count = count
  1118. median_label = {kv[0]: kv[1] for kv in label}
  1119. return median_label
  1120. def __get_median_label_nonsymbolic(self, labels):
  1121. if len(labels) == 0:
  1122. return {} # @todo
  1123. else:
  1124. # Transform the labels into coordinates and compute mean label as initial solution.
  1125. labels_as_coords = []
  1126. sums = {}
  1127. for key, val in labels[0].items():
  1128. sums[key] = 0
  1129. for label in labels:
  1130. coords = {}
  1131. for key, val in label.items():
  1132. label_f = float(val)
  1133. sums[key] += label_f
  1134. coords[key] = label_f
  1135. labels_as_coords.append(coords)
  1136. median = {}
  1137. for key, val in sums.items():
  1138. median[key] = val / len(labels)
  1139. # Run main loop of Weiszfeld's Algorithm.
  1140. epsilon = 0.0001
  1141. delta = 1.0
  1142. num_itrs = 0
  1143. all_equal = False
  1144. while ((delta > epsilon) and (num_itrs < 100) and (not all_equal)):
  1145. numerator = {}
  1146. for key, val in sums.items():
  1147. numerator[key] = 0
  1148. denominator = 0
  1149. for label_as_coord in labels_as_coords:
  1150. norm = 0
  1151. for key, val in label_as_coord.items():
  1152. norm += (val - median[key]) ** 2
  1153. norm = np.sqrt(norm)
  1154. if norm > 0:
  1155. for key, val in label_as_coord.items():
  1156. numerator[key] += val / norm
  1157. denominator += 1.0 / norm
  1158. if denominator == 0:
  1159. all_equal = True
  1160. else:
  1161. new_median = {}
  1162. delta = 0.0
  1163. for key, val in numerator.items():
  1164. this_median = val / denominator
  1165. new_median[key] = this_median
  1166. delta += np.abs(median[key] - this_median)
  1167. median = new_median
  1168. num_itrs += 1
  1169. # Transform the solution to strings and return it.
  1170. median_label = {}
  1171. for key, val in median.items():
  1172. median_label[key] = str(val)
  1173. return median_label
  1174. # def __get_median_edge_label_symbolic(self, edge_labels):
  1175. # pass
  1176. # def __get_median_edge_label_nonsymbolic(self, edge_labels):
  1177. # if len(edge_labels) == 0:
  1178. # return {}
  1179. # else:
  1180. # # Transform the labels into coordinates and compute mean label as initial solution.
  1181. # edge_labels_as_coords = []
  1182. # sums = {}
  1183. # for key, val in edge_labels[0].items():
  1184. # sums[key] = 0
  1185. # for edge_label in edge_labels:
  1186. # coords = {}
  1187. # for key, val in edge_label.items():
  1188. # label = float(val)
  1189. # sums[key] += label
  1190. # coords[key] = label
  1191. # edge_labels_as_coords.append(coords)
  1192. # median = {}
  1193. # for key, val in sums.items():
  1194. # median[key] = val / len(edge_labels)
  1195. #
  1196. # # Run main loop of Weiszfeld's Algorithm.
  1197. # epsilon = 0.0001
  1198. # delta = 1.0
  1199. # num_itrs = 0
  1200. # all_equal = False
  1201. # while ((delta > epsilon) and (num_itrs < 100) and (not all_equal)):
  1202. # numerator = {}
  1203. # for key, val in sums.items():
  1204. # numerator[key] = 0
  1205. # denominator = 0
  1206. # for edge_label_as_coord in edge_labels_as_coords:
  1207. # norm = 0
  1208. # for key, val in edge_label_as_coord.items():
  1209. # norm += (val - median[key]) ** 2
  1210. # norm += np.sqrt(norm)
  1211. # if norm > 0:
  1212. # for key, val in edge_label_as_coord.items():
  1213. # numerator[key] += val / norm
  1214. # denominator += 1.0 / norm
  1215. # if denominator == 0:
  1216. # all_equal = True
  1217. # else:
  1218. # new_median = {}
  1219. # delta = 0.0
  1220. # for key, val in numerator.items():
  1221. # this_median = val / denominator
  1222. # new_median[key] = this_median
  1223. # delta += np.abs(median[key] - this_median)
  1224. # median = new_median
  1225. #
  1226. # num_itrs += 1
  1227. #
  1228. # # Transform the solution to ged::GXLLabel and return it.
  1229. # median_label = {}
  1230. # for key, val in median.items():
  1231. # median_label[key] = str(val)
  1232. # return median_label

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