aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
graphChangesGenerator4UndiGraph_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The basic class for computing the next graph changes possible in an
24  * undirected structure learning algorithm
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 
30 namespace gum {
31 
32  namespace learning {
33 
34  /// default constructor
35  template < typename STRUCT_CONSTRAINT >
36  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::GraphChangesGenerator4UndiGraph(
37  STRUCT_CONSTRAINT& constraint) :
38  constraint_(&constraint) {
39  GUM_CONSTRUCTOR(GraphChangesGenerator4UndiGraph);
40  }
41 
42  /// copy constructor
43  template < typename STRUCT_CONSTRAINT >
50  }
51 
52  /// move operator
53  template < typename STRUCT_CONSTRAINT >
60  }
61 
62  /// destructor
63  template < typename STRUCT_CONSTRAINT >
66  }
67 
68  /// copy operator
69  template < typename STRUCT_CONSTRAINT >
73  if (this != &from) {
74  graph_ = from.graph_;
78  }
79  return *this;
80  }
81 
82  /// move operator
83  template < typename STRUCT_CONSTRAINT >
87  if (this != &from) {
92  }
93  return *this;
94  }
95 
96  /// create the set of legal and illegal changes from a given graph
97  template < typename STRUCT_CONSTRAINT >
100 
101  // for all the pairs of nodes, consider adding, reverse and removing edges
103 # pragma omp parallel num_threads(_max_threads_number_)
104  {
106 
107 # pragma omp single
108  {
109  // resize the change vectors so that each thread can write to its
110  // own vector
112  }
113 
114  const Idx this_thread = getThreadNumber();
115 
116  Idx i = 0;
117  for (const auto node1: graph_) {
118  if (i == this_thread) {
119  for (const auto node2: graph_) {
120  if (node1 != node2) {
121  // try edge additions
125  }
126 
127  // try edge deletion
131  }
132  }
133  }
134  }
135  ++i;
136  i %= num_threads;
137  }
138  }
139 
140  // now store the changes into the protected vectors of the
141  // GraphChangesGenerator4UndiGraph
142  for (const auto& changes: legal_changes) {
143  for (const auto& change: changes) {
145  }
146  }
147  }
148 
149  /// sets a new graph from which the operator will compute possible changes
150  template < typename STRUCT_CONSTRAINT >
151  INLINE void
153  // sets the current graph
154  graph_ = graph;
155 
156  // generate the set of all changes
157  createChanges_();
158  }
159 
160  /// empty the set of possible change operators that can be applied
161  template < typename STRUCT_CONSTRAINT >
164  }
165 
166  /// returns an (unsafe) iterator on the beginning of the list of operators
167  template < typename STRUCT_CONSTRAINT >
170  return legal_changes_.cbegin();
171  }
172 
173  /// returns an (unsafe) iterator on the end of the list of operators
174  template < typename STRUCT_CONSTRAINT >
177  return legal_changes_.cend();
178  }
179 
180  /// notify the operator set of a change applied to the graph
181  template < typename STRUCT_CONSTRAINT >
183  const EdgeAddition& change) {}
184 
185  /// notify the operator set of a change applied to the graph
186  template < typename STRUCT_CONSTRAINT >
188  const EdgeDeletion& change) {}
189 
190  /// notify the operator set of a change applied to the graph
191  template < typename STRUCT_CONSTRAINT >
193  const GraphChange& change) {}
194 
195  /// notifies the generator that we have parsed all its legal changes
196  template < typename STRUCT_CONSTRAINT >
199  }
200 
201  /// sets the maximum number of threads used to perform countings
202  template < typename STRUCT_CONSTRAINT >
203  INLINE void
205 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
206  if (nb == 0) nb = getMaxNumberOfThreads();
208 # else
210 # endif /* _OPENMP && GUM_DEBUG_MODE */
211  }
212 
213  /// returns the constraint that is used by the generator
214  template < typename STRUCT_CONSTRAINT >
217  return *constraint_;
218  }
219 
220  } /* namespace learning */
221 
222 } /* namespace gum */
223 
224 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)