GEDLIB  1.0
branch_tight.hpp
Go to the documentation of this file.
1 /***************************************************************************
2 * *
3 * Copyright (C) 2018 by David B. Blumenthal *
4 * *
5 * This file is part of GEDLIB. *
6 * *
7 * GEDLIB is free software: you can redistribute it and/or modify it *
8 * under the terms of the GNU Lesser General Public License as published *
9 * by the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * GEDLIB is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with GEDLIB. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21 
27 #ifndef SRC_METHDOS_BRANCH_TIGHT_HPP_
28 #define SRC_METHDOS_BRANCH_TIGHT_HPP_
29 
30 namespace ged {
31 
50 template<class UserNodeLabel, class UserEdgeLabel>
51 class BranchTight : public GEDMethod<UserNodeLabel, UserEdgeLabel> {
52 
53 public:
54  virtual ~BranchTight();
55 
57 
58 private:
59 
60  enum UpperBoundOption_ {NO, FIRST, LAST, BEST};
61 
62  typedef boost::associative_property_map<GEDGraph::NodeNodeMap> MateMap_;
63 
64  typedef std::map<std::size_t, std::size_t> SizeTSizeTMap_;
65 
66  class Weights_ {
67 
68  public:
69  Weights_(std::size_t size_master);
70 
71  double get_weight(std::size_t row_in_master, std::size_t col_in_master, std::size_t row_sub_in_master, std::size_t col_sub_in_master) const;
72 
73  void set_weight(std::size_t row_in_master, std::size_t col_in_master, std::size_t row_sub_in_master, std::size_t col_sub_in_master, double weight);
74 
75  private:
76  std::size_t size_master_;
77 
78  std::vector<double> weights_;
79 
80  };
81 
82  class SubproblemSolvers_ {
83 
84  public:
85  SubproblemSolvers_(std::size_t size_master, std::size_t degree);
86 
87  LSAPSolver & solver(std::size_t row_in_master, std::size_t col_in_master);
88 
89  const LSAPSolver & solver(std::size_t row_in_master, std::size_t col_in_master) const;
90 
91  DMatrix & subproblem(std::size_t row_in_master, std::size_t col_in_master);
92 
93  const DMatrix & subproblem(std::size_t row_in_master, std::size_t col_in_master) const;
94 
95  SizeTSizeTMap_ & rows_subproblem_to_master(std::size_t row_in_master, std::size_t col_in_master);
96 
97  std::size_t row_in_master(std::size_t row_in_master, std::size_t col_in_master, std::size_t row_in_subproblem) const;
98 
99  SizeTSizeTMap_ & cols_subproblem_to_master(std::size_t row_in_master, std::size_t col_in_master);
100 
101  std::size_t col_in_master(std::size_t row_in_master, std::size_t col_in_master, std::size_t col_in_subproblem) const;
102 
103  std::size_t get_size() const;
104 
105  std::size_t get_degree() const;
106 
107  void solve(std::size_t num_threads);
108 
109  void clear_solutions();
110 
111  private:
112  std::size_t size_master_;
113 
114  std::size_t degree_;
115 
116  std::vector<DMatrix> subproblems_;
117 
118  std::vector<LSAPSolver> subproblem_solvers_;
119 
120  std::vector<SizeTSizeTMap_> rows_sub_to_master_;
121 
122  std::vector<SizeTSizeTMap_> cols_sub_to_master_;
123  };
124 
125  class KFactor_ {
126 
127  public:
128  KFactor_(std::size_t num_nodes);
129 
130  bool contains_edge(std::size_t id_i, std::size_t id_k) const;
131 
132  void add_edge(std::size_t id_i, std::size_t id_k);
133 
134  std::size_t num_nodes() const;
135 
136  void clear_edges();
137 
138  private:
139  std::size_t num_nodes_;
140 
141  std::vector<bool> is_edge_;
142  };
143 
144  std::size_t max_itrs_;
145 
146  double range_;
147 
148  double epsilon_;
149 
150  UpperBoundOption_ upper_bound_option_;
151 
152  bool naive_regularization_;
153 
154  std::size_t num_threads_;
155 
156  double time_limit_in_sec_;
157 
158  // Member functions inherited from GEDMethod.
159 
160  virtual void ged_run_(const GEDGraph & g, const GEDGraph & h, Result & result) final;
161 
162  virtual void ged_set_default_options_() final;
163 
164  virtual bool ged_parse_option_(const std::string & option, const std::string & arg) final;
165 
166  virtual std::string ged_valid_options_string_() const;
167 
168  // Private helper functions.
169 
170  bool termination_criterion_met_(const std::size_t & current_itr, const double & last_improvement, Result & result);
171 
172  void init_subproblems_(const GEDGraph & g, const GEDGraph & h, SubproblemSolvers_ & subproblem_solver) const;
173 
174  void init_node_costs_(const GEDGraph & g, const GEDGraph & h, DMatrix & node_costs) const;
175 
176  void update_master_problem_costs_(const SubproblemSolvers_ & subproblems_solver, const DMatrix & node_costs, DMatrix & master_problem) const;
177 
178  void update_subproblem_costs_(const Weights_ & weights, std::size_t degree, SubproblemSolvers_ & subproblems_solver) const;
179 
180  void update_weights_(const LSAPSolver & master_problem_solver, std::size_t degree, const SubproblemSolvers_ & subproblems_solver, Weights_ & weights) const;
181 
182  std::size_t regularize_(GEDGraph & g, GEDGraph & h) const;
183 
184  std::size_t naive_regularize_(GEDGraph & g, GEDGraph & h) const;
185 
186  void fill_up_smaller_graph_(GEDGraph & g, GEDGraph & h) const;
187 
188  void fill_up_both_graphs_(GEDGraph & g, GEDGraph & h) const;
189 
190  void construct_complement_graph_(const GEDGraph & graph, GEDGraph & complement_graph, GEDGraph::NodeNodeMap & complement_to_graph) const;
191 
192  void regularize_from_k_factor_(const KFactor_ & k_factor, const GEDGraph::NodeSizeTMap & graph_to_k_factor, GEDGraph & graph) const;
193 
194  bool compute_k_factor_(const GEDGraph & complement_graph, LabelID k, const GEDGraph::NodeSizeTMap & complement_graph_to_k_factor, KFactor_ & k_factor) const;
195 
196  bool construct_transformed_complement_graph_(const GEDGraph & complement_graph, std::size_t k, GEDGraph & transformed_complement_graph, GEDGraph::NodeNodeMap & transformed_to_original_nodes) const;
197 
198 };
199 
200 }
201 
202 #endif /* SRC_METHDOS_BRANCH_TIGHT_HPP_ */
std::map< NodeID, NodeID > NodeNodeMap
Map that assigns node IDs to node IDs.
Definition: ged_graph.hpp:114
Contains the standardized input data along with basic functionality.
Definition: ged_data.hpp:55
virtual void ged_set_default_options_() final
Sets all options to default values.
virtual void ged_run_(const GEDGraph &g, const GEDGraph &h, Result &result) final
Runs the method with options specified by set_options().
std::map< NodeID, std::size_t > NodeSizeTMap
Map that assigns node IDs to integers.
Definition: ged_graph.hpp:116
A wrapper structure for the result of calls to ged::GEDMethod::run_as_util() and ged::GEDMethod::ged_...
Definition: result.hpp:38
Abstract class for the (suboptimal) computation of the graph edit distance.
Definition: ged_method.hpp:40
Computes lower and upper bounds for general edit costs.
std::size_t LabelID
Internally used type of node and edge labels.
virtual bool ged_parse_option_(const std::string &option, const std::string &arg) final
Parses one option.
The normalized input graphs used by GEDLIB. All labels are integers.
Definition: ged_graph.hpp:104
Global namespace for GEDLIB.
This class solves LSAP instances by calling the library lsape available at https://bougleux.users.greyc.fr/lsape/.
Definition: lsap_solver.hpp:39
virtual std::string ged_valid_options_string_() const
Returns string of all valid options.