GEDLIB  1.0
ged_env.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_ENV_GED_ENV_HPP_
28 #define SRC_ENV_GED_ENV_HPP_
29 
30 #include "common_types.hpp"
31 #include "ged_graph.hpp"
32 #include "node_map.hpp"
33 #include "../methods/all_methods.hpp"
34 #include "ged_data.hpp"
35 
40 namespace ged {
41 
48 template<class UserNodeID, class UserNodeLabel, class UserEdgeLabel>
49 class GEDEnv {
50 public:
51 
55  ~GEDEnv();
56 
60  GEDEnv();
61 
67  void set_edit_costs(Options::EditCosts edit_costs, std::initializer_list<double> edit_cost_constants = {});
68 
73  void set_edit_costs(EditCosts<UserNodeLabel, UserEdgeLabel> * edit_costs);
74 
81  GEDGraph::GraphID add_graph(const std::string & graph_name = "", const std::string & graph_class = "");
82 
87  void clear_graph(GEDGraph::GraphID graph_id);
88 
95  void add_node(GEDGraph::GraphID graph_id, const UserNodeID & node_id, const UserNodeLabel & node_label);
96 
105  void add_edge(GEDGraph::GraphID graph_id, const UserNodeID & tail, const UserNodeID & head, const UserEdgeLabel & edge_label, bool ignore_duplicates = true);
106 
116  GEDGraph::GraphID load_exchange_graph(const ged::ExchangeGraph<UserNodeID, UserNodeLabel, UserEdgeLabel> & exchange_graph, GEDGraph::GraphID graph_id = ged::undefined(), const std::string & graph_name = "", const std::string & graph_class = "");
117 
132  GEDGraph::GraphID load_gxl_graph(const std::string & file_name, Options::GXLNodeEdgeType node_type, Options::GXLNodeEdgeType edge_type,
133  const std::unordered_set<std::string> & irrelevant_node_attributes, const std::unordered_set<std::string> & irrelevant_edge_attributes, GEDGraph::GraphID graph_id = ged::undefined(), const std::string & graph_class = "");
134 
147  std::vector<GEDGraph::GraphID> load_gxl_graphs(const std::string & graph_dir, const std::string & collection_file,
149  const std::unordered_set<std::string> & irrelevant_node_attributes = std::unordered_set<std::string>(), const std::unordered_set<std::string> & irrelevant_edge_attributes = std::unordered_set<std::string>());
150 
156 
162  void set_method(Options::GEDMethod method, const std::string & options = std::string(""));
163 
170 
174  void init_method();
175 
181  std::pair<GEDGraph::GraphID, GEDGraph::GraphID> graph_ids() const;
182 
187  std::size_t num_graphs() const;
188 
195  double get_lower_bound(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const;
196 
203  double get_upper_bound(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const;
204 
211  const NodeMap & get_node_map(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const;
212 
219  double get_runtime(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const;
220 
225  double get_init_time() const;
226 
233  void compute_induced_cost(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id, NodeMap & node_map) const;
234 
240  ExchangeGraph<UserNodeID, UserNodeLabel, UserEdgeLabel> get_graph(GEDGraph::GraphID graph_id) const;
241 
247  const std::string & get_graph_class(GEDGraph::GraphID graph_id) const;
248 
254  const std::string & get_graph_name(GEDGraph::GraphID graph_id) const;
255 
260  bool quasimetric_costs() const;
261 
267  std::size_t get_num_nodes(GEDGraph::GraphID graph_id) const;
268 
273  double get_avg_num_nodes() const;
274 
275 private:
276 
277  bool initialized_;
278 
279  std::vector<GEDGraph::GraphID> new_graph_ids_;
280 
281  GEDData<UserNodeLabel, UserEdgeLabel> ged_data_;
282 
283  // Variables needed for approximating ged_instance_.
284 
285  std::map<std::pair<GEDGraph::GraphID, GEDGraph::GraphID>, double> lower_bounds_;
286 
287  std::map<std::pair<GEDGraph::GraphID, GEDGraph::GraphID>, double> upper_bounds_;
288 
289  std::map<std::pair<GEDGraph::GraphID, GEDGraph::GraphID>, Seconds> runtimes_;
290 
291  std::map<std::pair<GEDGraph::GraphID, GEDGraph::GraphID>, NodeMap> node_maps_;
292 
293  std::vector<std::map<UserNodeID, GEDGraph::NodeID>> original_to_internal_node_ids_;
294 
295  std::vector<std::map<GEDGraph::NodeID, UserNodeID>> internal_to_original_node_ids_;
296 
297  GEDMethod<UserNodeLabel, UserEdgeLabel> * ged_method_;
298 
299  void read_gxl_label_from_ptree_(const boost::property_tree::ptree::value_type & node_or_edge, const std::unordered_set<std::string> & irrelevant_attributes, const std::string & file, GXLLabel & label);
300 
301  void construct_shuffled_graph_copy_(GEDGraph::GraphID graph_id);
302 
303  GEDGraph::GraphID add_or_clear_shuffled_graph_copy_(GEDGraph::GraphID graph_id);
304 
305  std::string to_string_(UserNodeID node_id);
306 
307 };
308 
309 }
310 
311 #include "ged_env.ipp"
312 
313 namespace ged {
314 
315 #ifdef GXL_GEDLIB_SHARED
316 #ifndef SRC_ENV_GED_ENV_GXL_CPP_
317 extern template class GEDEnv<GXLNodeID, GXLLabel, GXLLabel>;
318 #endif /* SRC_ENV_GED_ENV_GXL_CPP_ */
319 #endif /* GXL_GEDLIB_SHARED */
320 
321 }
322 
323 #endif /* SRC_ENV_GED_ENV_HPP_ */
GEDGraph::GraphID load_gxl_graph(const std::string &file_name, Options::GXLNodeEdgeType node_type, Options::GXLNodeEdgeType edge_type, const std::unordered_set< std::string > &irrelevant_node_attributes, const std::unordered_set< std::string > &irrelevant_edge_attributes, GEDGraph::GraphID graph_id=ged::undefined(), const std::string &graph_class="")
Load graph given in the GXL file format.
std::size_t num_graphs() const
The number of graphs contained in the environment.
Definition: ged_env.ipp:514
bool quasimetric_costs() const
Checks if the edit costs are quasimetric.
Definition: ged_env.ipp:648
ged::GEDEnv class definition.
std::pair< GEDGraph::GraphID, GEDGraph::GraphID > graph_ids() const
Provides access to the IDs of the graphs contained in the environment.
Definition: ged_env.ipp:507
std::vector< GEDGraph >::size_type GraphID
Type of internally used graph IDs.
Definition: ged_graph.hpp:112
ged::NodeMap class declaration.
void init(Options::InitType init_type=Options::InitType::EAGER_WITHOUT_SHUFFLED_COPIES)
Initializes the environment.
Definition: ged_env.ipp:655
ExchangeGraph< UserNodeID, UserNodeLabel, UserEdgeLabel > get_graph(GEDGraph::GraphID graph_id) const
Returns ged::ExchangeGraph representation.
Definition: ged_env.ipp:610
ged::GEDGraph class declaration.
GEDGraph::GraphID load_exchange_graph(const ged::ExchangeGraph< UserNodeID, UserNodeLabel, UserEdgeLabel > &exchange_graph, GEDGraph::GraphID graph_id=ged::undefined(), const std::string &graph_name="", const std::string &graph_class="")
Loads ged::ExchangeGraph into the environment.
Definition: ged_env.ipp:104
std::vector< GEDGraph::GraphID > load_gxl_graphs(const std::string &graph_dir, const std::string &collection_file, Options::GXLNodeEdgeType node_type=Options::GXLNodeEdgeType::LABELED, Options::GXLNodeEdgeType edge_type=Options::GXLNodeEdgeType::LABELED, const std::unordered_set< std::string > &irrelevant_node_attributes=std::unordered_set< std::string >(), const std::unordered_set< std::string > &irrelevant_edge_attributes=std::unordered_set< std::string >())
Loads graphs given in the GXL file format.
double get_init_time() const
Returns initialization time.
Definition: ged_env.ipp:634
Simple graph class used for communication with user.
void set_edit_costs(Options::EditCosts edit_costs, std::initializer_list< double > edit_cost_constants={})
Sets the edit costs to one of the predefined edit costs.
Definition: ged_env.ipp:55
void init_method()
Initializes the method specified by call to set_method().
Definition: ged_env.ipp:521
GEDMethod
Selects the method.
GXLNodeEdgeType
Selects whether nodes or edges of graphs given in GXL file format are labeled or unlabeled.
const std::string & get_graph_class(GEDGraph::GraphID graph_id) const
Returns the graph class.
Definition: ged_env.ipp:578
double get_runtime(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const
Returns runtime.
Definition: ged_env.ipp:567
std::chrono::duration< double > Seconds
Internally used type for measurements in seconds.
InitType
Selects the initialization type of the environment.
GEDEnv()
Constructor.
Definition: ged_env.ipp:40
double get_avg_num_nodes() const
Returns average number of nodes.
Definition: ged_env.ipp:592
void add_node(GEDGraph::GraphID graph_id, const UserNodeID &node_id, const UserNodeLabel &node_label)
Adds a labeled node.
Definition: ged_env.ipp:339
Type declarations used by various classes.
void compute_induced_cost(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id, NodeMap &node_map) const
Computes the edit cost between two graphs induced by a node map.
Definition: ged_env.ipp:641
std::map< std::string, std::string > GXLLabel
Type of node and edge labels of graphs given in the .gxl file format.
double get_upper_bound(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const
Returns upper bound for edit distance between the input graphs.
Definition: ged_env.ipp:545
std::size_t get_num_nodes(GEDGraph::GraphID graph_id) const
Returns the number of nodes.
Definition: ged_env.ipp:585
constexpr std::size_t undefined()
Returns undefined size.
void clear_graph(GEDGraph::GraphID graph_id)
Clears and de-initializes a graph that has previously been added to the environment. Call init() after calling this method.
Definition: ged_env.ipp:89
void run_method(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id)
Runs the GED method specified by call to set_method() between the graphs with IDs g_id and h_id...
Definition: ged_env.ipp:471
void set_method(Options::GEDMethod method, const std::string &options=std::string(""))
Sets the GEDMethod to be used by run_method().
Definition: ged_env.ipp:384
Global namespace for GEDLIB.
void add_edge(GEDGraph::GraphID graph_id, const UserNodeID &tail, const UserNodeID &head, const UserEdgeLabel &edge_label, bool ignore_duplicates=true)
Adds a labeled edge.
Definition: ged_env.ipp:359
Eager initialization, no shuffled graph copies are constructed.
EditCosts
Selects the edit costs.
const NodeMap & get_node_map(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const
Returns node map between the input graphs.
Definition: ged_env.ipp:556
ged::GEDData class declaration.
const std::string & get_graph_name(GEDGraph::GraphID graph_id) const
Returns the graph name.
Definition: ged_env.ipp:603
GEDGraph::GraphID add_graph(const std::string &graph_name="", const std::string &graph_class="")
Adds a new uninitialized graph to the environment. Call init() after calling this method...
Definition: ged_env.ipp:69
~GEDEnv()
Destructor.
Definition: ged_env.ipp:34
double get_lower_bound(GEDGraph::GraphID g_id, GEDGraph::GraphID h_id) const
Returns lower bound for edit distance between the input graphs.
Definition: ged_env.ipp:534