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.

ged_method.py 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Jun 18 15:52:35 2020
  5. @author: ljia
  6. """
  7. import numpy as np
  8. import time
  9. import networkx as nx
  10. class GEDMethod(object):
  11. def __init__(self, ged_data):
  12. self._initialized = False
  13. self._ged_data = ged_data
  14. self._options = None
  15. self._lower_bound = 0
  16. self._upper_bound = np.inf
  17. self._node_map = [0, 0] # @todo
  18. self._runtime = None
  19. self._init_time = None
  20. def init(self):
  21. """Initializes the method with options specified by set_options().
  22. """
  23. start = time.time()
  24. self._ged_init()
  25. end = time.time()
  26. self._init_time = end - start
  27. self._initialized = True
  28. def set_options(self, options):
  29. """
  30. /*!
  31. * @brief Sets the options of the method.
  32. * @param[in] options String of the form <tt>[--@<option@> @<arg@>] [...]</tt>, where @p option contains neither spaces nor single quotes,
  33. * and @p arg contains neither spaces nor single quotes or is of the form <tt>'[--@<sub-option@> @<sub-arg@>] [...]'</tt>,
  34. * where both @p sub-option and @p sub-arg contain neither spaces nor single quotes.
  35. */
  36. """
  37. self._ged_set_default_options()
  38. for key, val in options.items():
  39. if not self._ged_parse_option(key, val):
  40. raise Exception('Invalid option "', key, '". Usage: options = "' + self._ged_valid_options_string() + '".') # @todo: not implemented.
  41. self._initialized = False
  42. def run(self, g_id, h_id):
  43. """
  44. /*!
  45. * @brief Runs the method with options specified by set_options().
  46. * @param[in] g_id ID of input graph.
  47. * @param[in] h_id ID of input graph.
  48. */
  49. """
  50. start = time.time()
  51. result = self.run_as_util(self._ged_data._graphs[g_id], self._ged_data._graphs[h_id])
  52. end = time.time()
  53. self._lower_bound = result['lower_bound']
  54. self._upper_bound = result['upper_bound']
  55. if len(result['node_maps']) > 0:
  56. self._node_map = result['node_maps'][0]
  57. self._runtime = end - start
  58. def run_as_util(self, g, h):
  59. """
  60. /*!
  61. * @brief Runs the method with options specified by set_options().
  62. * @param[in] g Input graph.
  63. * @param[in] h Input graph.
  64. * @param[out] result Result variable.
  65. */
  66. """
  67. # Compute optimal solution and return if at least one of the two graphs is empty.
  68. if nx.number_of_nodes(g) == 0 or nx.number_of_nodes(h) == 0:
  69. print('This is not implemented.')
  70. pass # @todo:
  71. # Run the method.
  72. return self._ged_run(g, h)
  73. def get_upper_bound(self):
  74. """
  75. /*!
  76. * @brief Returns an upper bound.
  77. * @return Upper bound for graph edit distance provided by last call to run() or -1 if the method does not yield an upper bound.
  78. */
  79. """
  80. return self._upper_bound
  81. def get_lower_bound(self):
  82. """
  83. /*!
  84. * @brief Returns a lower bound.
  85. * @return Lower bound for graph edit distance provided by last call to run() or -1 if the method does not yield a lower bound.
  86. */
  87. """
  88. return self._lower_bound
  89. def get_runtime(self):
  90. """
  91. /*!
  92. * @brief Returns the runtime.
  93. * @return Runtime of last call to run() in seconds.
  94. */
  95. """
  96. return self._runtime
  97. def get_init_time(self):
  98. """
  99. /*!
  100. * @brief Returns the initialization time.
  101. * @return Runtime of last call to init() in seconds.
  102. */
  103. """
  104. return self._init_time
  105. def get_node_map(self):
  106. """
  107. /*!
  108. * @brief Returns a graph matching.
  109. * @return Constant reference to graph matching provided by last call to run() or to an empty matching if the method does not yield a matching.
  110. */
  111. """
  112. return self._node_map
  113. def _ged_init(self):
  114. """
  115. /*!
  116. * @brief Initializes the method.
  117. * @note Must be overridden by derived classes that require initialization.
  118. */
  119. """
  120. pass
  121. def _ged_parse_option(self, option, arg):
  122. """
  123. /*!
  124. * @brief Parses one option.
  125. * @param[in] option The name of the option.
  126. * @param[in] arg The argument of the option.
  127. * @return Boolean @p true if @p option is a valid option name for the method and @p false otherwise.
  128. * @note Must be overridden by derived classes that have options.
  129. */
  130. """
  131. return False
  132. def _ged_run(self, g, h):
  133. """
  134. /*!
  135. * @brief Runs the method with options specified by set_options().
  136. * @param[in] g Input graph.
  137. * @param[in] h Input graph.
  138. * @param[out] result Result variable.
  139. * @note Must be overridden by derived classes.
  140. */
  141. """
  142. return {}
  143. def _ged_valid_options_string(self):
  144. """
  145. /*!
  146. * @brief Returns string of all valid options.
  147. * @return String of the form <tt>[--@<option@> @<arg@>] [...]</tt>.
  148. * @note Must be overridden by derived classes that have options.
  149. */
  150. """
  151. return ''
  152. def _ged_set_default_options(self):
  153. """
  154. /*!
  155. * @brief Sets all options to default values.
  156. * @note Must be overridden by derived classes that have options.
  157. */
  158. """
  159. pass

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