aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphChangesGeneratorOnSubDiGraph_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 class for computing the set of graph changes (over a subgraph)
24  * transmitted to learning algorithms
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  GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >::
37  GraphChangesGeneratorOnSubDiGraph(STRUCT_CONSTRAINT& constraint) :
38  constraint_(&constraint) {
39  GUM_CONSTRUCTOR(GraphChangesGeneratorOnSubDiGraph);
40  }
41 
42  /// copy constructor
43  template < typename STRUCT_CONSTRAINT >
52  }
53 
54  /// move operator
55  template < typename STRUCT_CONSTRAINT >
65  }
66 
67  /// destructor
68  template < typename STRUCT_CONSTRAINT >
72  }
73 
74  /// copy operator
75  template < typename STRUCT_CONSTRAINT >
79  if (this != &from) {
85  }
86  return *this;
87  }
88 
89  /// move operator
90  template < typename STRUCT_CONSTRAINT >
94  if (this != &from) {
100  }
101  return *this;
102  }
103 
104  /// create the set of legal and illegal changes from a given graph
105  template < typename STRUCT_CONSTRAINT >
108 
109  // for all the pairs of nodes, consider adding, reverse and removing arcs
111 # pragma omp parallel num_threads(max_threads_number__)
112  {
114 
115 # pragma omp single
116  {
117  // resize the change vectors so that each thread can write to its
118  // own vector
120  }
121 
122  const Size this_thread = getThreadNumber();
123 
124  Idx i = 0;
125  for (const auto node1: tail_nodes_) {
126  if (i == this_thread) {
127  for (const auto node2: target_nodes_) {
128  if (node1 != node2) {
129  // try arc additions
133  }
134 
135  // try arc deletion
139  }
140 
141  // try arc reversal
145  }
146  }
147  }
148  }
149  ++i;
150  i %= num_threads;
151  }
152  }
153 
154  // now store the changes into the protected vectors of the
155  // GraphChangesGeneratorOnSubDiGraph
156  for (const auto& changes: legal_changes) {
157  for (const auto& change: changes) {
159  }
160  }
161  }
162 
163  /// sets a new graph from which the operator will compute possible changes
164  template < typename STRUCT_CONSTRAINT >
166  const DiGraph& graph) {
167  // generate the set of all changes
168  createChanges_();
169  }
170 
171  /// assign a set of target nodes
172  template < typename STRUCT_CONSTRAINT >
174  const NodeSet& nodes) {
176  }
177 
178  /// adds a new target node
179  template < typename STRUCT_CONSTRAINT >
181  NodeId node) {
183  }
184 
185  /// removes a target
186  template < typename STRUCT_CONSTRAINT >
187  INLINE void
189  NodeId node) {
191  }
192 
193  /// assign a set of "tail" nodes
194  template < typename STRUCT_CONSTRAINT >
196  const NodeSet& nodes) {
197  tail_nodes_ = nodes;
198  }
199 
200  /// assign a set of "tail" nodes
201  template < typename STRUCT_CONSTRAINT >
203  Size nb_nodes) {
204  tail_nodes_.clear();
205  for (Idx i = 0; i < nb_nodes; ++i) {
207  }
208  }
209 
210  /// adds a new "tail" node
211  template < typename STRUCT_CONSTRAINT >
213  NodeId node) {
215  }
216 
217  /// removes a tail node
218  template < typename STRUCT_CONSTRAINT >
220  NodeId node) {
222  }
223 
224  /// empty the set of possible change operators that can be applied
225  template < typename STRUCT_CONSTRAINT >
227  STRUCT_CONSTRAINT >::clearChanges() noexcept {
229  }
230 
231  /// returns an (unsafe) iterator on the beginning of the list of operators
232  template < typename STRUCT_CONSTRAINT >
233  INLINE
236  return legal_changes_.cbegin();
237  }
238 
239  /// returns an (unsafe) iterator on the end of the list of operators
240  template < typename STRUCT_CONSTRAINT >
244  return legal_changes_.cend();
245  }
246 
247  /// notify the operator set of a change applied to the graph
248  template < typename STRUCT_CONSTRAINT >
249  INLINE void
251  const ArcAddition& change) {}
252 
253  /// notify the operator set of a change applied to the graph
254  template < typename STRUCT_CONSTRAINT >
255  INLINE void
257  const ArcDeletion& change) {}
258 
259  /// notify the operator set of a change applied to the graph
260  template < typename STRUCT_CONSTRAINT >
261  INLINE void
263  const ArcReversal& change) {}
264 
265  /// notify the operator set of a change applied to the graph
266  template < typename STRUCT_CONSTRAINT >
267  INLINE void
269  const GraphChange& change) {}
270 
271  /// notifies the generator that we have parsed all its legal changes
272  template < typename STRUCT_CONSTRAINT >
276  }
277 
278  /// sets the maximum number of threads used to perform countings
279  template < typename STRUCT_CONSTRAINT >
280  INLINE void
282  Size nb) noexcept {
283 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
284  if (nb == 0) nb = getMaxNumberOfThreads();
286 # else
288 # endif /* _OPENMP && GUM_DEBUG_MODE */
289  }
290 
291  /// returns the constraint that is used by the generator
292  template < typename STRUCT_CONSTRAINT >
295  const noexcept {
296  return *constraint_;
297  }
298 
299  } /* namespace learning */
300 
301 } /* namespace gum */
302 
303 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)