GEDLIB  1.0
common_types.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_COMMON_TYPES_HPP_
28 #define SRC_ENV_COMMON_TYPES_HPP_
29 
30 // Include standard libraries.
31 #include <cstddef>
32 #include <functional>
33 #include <limits>
34 #include <vector>
35 #include <list>
36 #include <initializer_list>
37 #include <map>
38 #include <queue>
39 #include <string>
40 #include <unordered_set>
41 #include <chrono>
42 #include <cmath>
43 #include <iostream>
44 #include <ios>
45 #include <sstream>
46 #include <fstream>
47 #include <algorithm>
48 #include <random>
49 #include <stdexcept>
50 #include <sstream>
51 #include <typeinfo>
52 #ifdef _OPENMP
53 #include <omp.h>
54 #endif
55 
56 // Include Boost libraries.
57 #include <boost/graph/adjacency_list.hpp>
58 #include <boost/graph/adjacency_matrix.hpp>
59 #include <boost/graph/copy.hpp>
60 #include <boost/graph/max_cardinality_matching.hpp>
61 #include <boost/property_tree/ptree.hpp>
62 #include <boost/property_tree/xml_parser.hpp>
63 
64 // Include Gurobi.
65 #ifdef GUROBI
66 #include <gurobi_c++.h>
67 #endif
68 
69 // Include external non-standard libraries.
70 #ifndef LSAPE_IndexType
71 #define LSAPE_IndexType std::size_t
72 #endif
73 #include <lsap.h>
74 #include <lsape.h>
75 #include <svm.h>
76 #include <doublefann.h>
77 #include <fann_cpp.h>
78 #include <Dense>
79 #include <nomad.hpp>
80 
81 // Include helper classes.
82 #include "timer.hpp"
83 #include "error.hpp"
84 #include "progress_bar.hpp"
85 
86 
87 namespace ged {
88 
92 typedef std::chrono::duration<double> Seconds;
93 
97 typedef std::map<std::string, std::string> GXLLabel;
98 
105 template<class Key, class Value>
106 std::ostream & operator<<(std::ostream & os, const std::map<Key, Value> & map) {
107  os << "{ ";
108  for (const auto & key_val : map) {
109  os << "(" << key_val.first << "," << key_val.second << ") ";
110  }
111  os << "}";
112  return os;
113 }
114 
118 typedef std::string GXLNodeID;
119 
123 typedef std::size_t LabelID;
124 
128 struct NoLabel {};
129 
130 constexpr bool operator==(NoLabel const &, NoLabel const &) {return true;}
131 
136 constexpr LabelID invalid_label() {return std::numeric_limits<LabelID>::max();}
137 
142 constexpr LabelID dummy_label() {return 0;}
143 
144 
149 constexpr std::size_t undefined() {return std::numeric_limits<std::size_t>::max();}
150 
155 constexpr double pi() {return 3.141592653589793238463;}
156 
160 struct Options {
161 
162 
166  enum class GEDMethod {
167 #ifdef GUROBI
168  F1,
169  F2,
170  COMPACT_MIP,
171  BLP_NO_EDGE_LABELS,
172 #endif /* GUROBI */
173  BRANCH,
174  BRANCH_FAST,
175  BRANCH_TIGHT,
176  BRANCH_UNIFORM,
177  BRANCH_COMPACT,
178  PARTITION,
179  HYBRID,
180  RING,
181  ANCHOR_AWARE_GED,
182  WALKS,
183  IPFP,
184  BIPARTITE,
185  SUBGRAPH,
186  NODE,
187  RING_ML,
188  BIPARTITE_ML,
189  REFINE,
190  BP_BEAM,
191  SIMULATED_ANNEALING,
192  HED,
193  STAR
194  };
195 
199  enum class EditCosts {
200  CHEM_1,
201  CHEM_2,
202  CMU,
203  GREC_1,
204  GREC_2,
205  PROTEIN,
206  FINGERPRINT,
207  LETTER,
208  CONSTANT
209  };
210 
214  enum class GXLNodeEdgeType {
215  LABELED,
216  UNLABELED
217  };
218 
228  enum class InitType {
229  LAZY_WITHOUT_SHUFFLED_COPIES,
230  EAGER_WITHOUT_SHUFFLED_COPIES,
231  LAZY_WITH_SHUFFLED_COPIES,
232  EAGER_WITH_SHUFFLED_COPIES
233  };
234 
235 };
236 
243 std::ostream & operator<<(std::ostream & os, const Options::GEDMethod & ged_method) {
244  switch (ged_method) {
246  os << "BRANCH";
247  break;
249  os << "BRANCH_FAST";
250  break;
252  os << "BRANCH_TIGHT";
253  break;
255  os << "BRANCH_UNIFORM";
256  break;
258  os << "BRANCH_COMPACT";
259  break;
261  os << "PARTITION";
262  break;
264  os << "HYBRID";
265  break;
267  os << "RING";
268  break;
270  os << "ANCHOR_AWARE_GED";
271  break;
273  os << "WALKS";
274  break;
276  os << "IPFP";
277  break;
279  os << "BIPARTITE";
280  break;
282  os << "SUBGRAPH";
283  break;
285  os << "NODE";
286  break;
288  os << "RING_ML";
289  break;
291  os << "BIPARTITE_ML";
292  break;
294  os << "REFINE";
295  break;
297  os << "BP_BEAM";
298  break;
300  os << "SIMULATED_ANNEALING";
301  break;
303  os << "HED";
304  break;
306  os << "STAR";
307  break;
308 #ifdef GUROBI
309  case Options::GEDMethod::F1:
310  os << "F1";
311  break;
312  case Options::GEDMethod::F2:
313  os << "F2";
314  break;
315  case Options::GEDMethod::COMPACT_MIP:
316  os << "COMPACT_MIP";
317  break;
318  case Options::GEDMethod::BLP_NO_EDGE_LABELS:
319  os << "BLP_NO_EDGE_LABELS";
320  break;
321 #endif
322  }
323  return os;
324 }
325 
332 template<class UserNodeID, class UserNodeLabel, class UserEdgeLabel> struct ExchangeGraph {
333 
334  std::size_t id;
335 
336  std::size_t num_nodes;
337 
338  std::size_t num_edges;
339 
340  std::vector<UserNodeID> original_node_ids;
341 
342  std::vector<UserNodeLabel> node_labels;
343 
344  std::vector<std::vector<std::size_t>> adj_matrix;
345 
346  std::map<std::pair<std::size_t, std::size_t>, UserEdgeLabel> edge_labels;
347 
348  bool operator==(const ExchangeGraph<UserNodeID, UserNodeLabel, UserEdgeLabel> & rhs) const {
349  return ((original_node_ids == rhs.original_node_ids) and (node_labels == rhs.node_labels) and (adj_matrix == rhs.adj_matrix) and (edge_labels == rhs.edge_labels));
350  };
351 };
352 
353 #ifdef ENABLE_GRAPH_STREAMING
354 
364 template<class UserNodeID, class UserNodeLabel, class UserEdgeLabel>
365 std::ostream & operator<<(std::ostream & os, const ExchangeGraph<UserNodeID, UserNodeLabel, UserEdgeLabel> & graph) {
366  os << "graph [\n";
367  os << "\tid = " << graph.id << "\n";
368  os << "\tdirected = 0\n";
369  os << "\tnum_nodes = " << graph.num_nodes << "\n";
370  os << "\tnum_edges = " << graph.num_edges << "\n";
371  for (std::size_t node_id{0}; node_id < graph.num_nodes; node_id++) {
372  os << "\tnode [\n";
373  os << "\t\tinternal_id = " << node_id << "\n";
374  os << "\t\toriginal_id = \"" << graph.original_node_ids.at(node_id) << "\"\n";
375  os << "\t\tlabel = \"" << graph.node_labels.at(node_id) << "\"\n";
376  os << "\t]\n";
377  }
378  for (std::size_t tail_id{0}; tail_id < graph.num_nodes; tail_id++) {
379  for (std::size_t head_id{tail_id + 1}; head_id < graph.num_nodes; head_id++) {
380  if (graph.adj_matrix[node_id][head_id] == 1) {
381  os << "\tedge [\n";
382  os << "\t\tinternal_id_tail = " << tail_id << "\n";
383  os << "\t\tinternal_id_head = " << head_id << "\n";
384  os << "\t\tlabel = \"" << graph.edge_labels[std::make_pair(tail_id, head_id)] << "\"\n";
385  os << "\t]\n";
386  }
387  }
388  }
389  os << "]\n";
390  return os;
391 }
392 #endif /* ENABLE_GRAPH_STREAMING */
393 
394 }
395 
396 #endif /* SRC_ENV_COMMON_TYPES_HPP_ */
Computes a lower bound for general edit costs.
Definition: hed.hpp:46
std::string GXLNodeID
Type of node IDs of graphs given in the .gxl file format.
std::size_t id
Internal ID of the graph.
Mixed integer linear programming formulation of the graph edit distance.
Definition: f1.hpp:42
constexpr double pi()
Provides constant with double precision.
Selects ged::Branch.
std::vector< std::vector< std::size_t > > adj_matrix
Adjacency matrix.
std::ostream & operator<<(std::ostream &os, const std::map< Key, Value > &map)
Streams std::map.
Selects ged::BranchFast.
Selects ged::AnchorAwareGED.
Simple graph class used for communication with user.
ged::ProgressBar class declaration.
Selects ged::Walks.
GEDMethod
Selects the method.
std::vector< UserNodeLabel > node_labels
The labels of all nodes.
GXLNodeEdgeType
Selects whether nodes or edges of graphs given in GXL file format are labeled or unlabeled.
std::vector< UserNodeID > original_node_ids
The original IDs of all nodes.
Selects ged::BranchTight.
std::size_t LabelID
Internally used type of node and edge labels.
Selects ged::Partition.
std::chrono::duration< double > Seconds
Internally used type for measurements in seconds.
InitType
Selects the initialization type of the environment.
Selects ged::SimulatedAnnealing.
Edit costs for graphs contain in CMU dataset.
Definition: cmu.hpp:53
Selects ged::BipartiteML.
Selects ged::Bipartite.
std::map< std::string, std::string > GXLLabel
Type of node and edge labels of graphs given in the .gxl file format.
constexpr LabelID invalid_label()
Returns an invalid label.
std::size_t num_nodes
The number of nodes. Nodes have IDs between 0 and num_nodes - 1.
std::size_t num_edges
The number of edges.
std::map< std::pair< std::size_t, std::size_t >, UserEdgeLabel > edge_labels
A hash map with a key-value pair ((internal_tail_id, internal_head_id), label) for each edge...
constexpr LabelID dummy_label()
Returns a dummy label.
constexpr std::size_t undefined()
Returns undefined size.
ged::Error class declaration.
ged::Timer class declaration.
Global namespace for GEDLIB.
EditCosts
Selects the edit costs.
Selects ged::Refine.
Selects ged::Hybrid.
Selects ged::BranchUniform.
Type of dummy labels for unlabeled nodes and edges.
Computes an upper bound for general edit costs.
Definition: ipfp.hpp:64
Contains enums for options employed by ged::GEDEnv.
Selects ged::Subgraph.
Mixed integer linear programming formulation of the graph edit distance.
Definition: f2.hpp:42
Selects ged::BranchCompact.