aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
graphChangesGenerator4K2_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 a
24  * (directed) 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  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >::GraphChangesGenerator4K2(
37  STRUCT_CONSTRAINT& constraint) :
38  constraint_(&constraint) {
39  GUM_CONSTRUCTOR(GraphChangesGenerator4K2);
40  }
41 
42  /// copy constructor
43  template < typename STRUCT_CONSTRAINT >
50  }
51 
52  /// move operator
53  template < typename STRUCT_CONSTRAINT >
61  }
62 
63  /// destructor
64  template < typename STRUCT_CONSTRAINT >
67  }
68 
69  /// copy operator
70  template < typename STRUCT_CONSTRAINT >
74  if (this != &from) {
75  graph_ = from.graph_;
77  order_ = from.order_;
80  }
81  return *this;
82  }
83 
84  /// move operator
85  template < typename STRUCT_CONSTRAINT >
89  if (this != &from) {
95  }
96  return *this;
97  }
98 
99  /// create the set of legal and illegal changes from a given graph
100  template < typename STRUCT_CONSTRAINT >
103 
104  // for all the pairs of nodes, consider adding, reverse and removing arcs
106 # pragma omp parallel num_threads(int(_max_threads_number_))
107  {
109 
110 # pragma omp single
111  {
112  // resize the change vectors so that each thread can write to its
113  // own vector
115  }
116 
117  const Size this_thread = getThreadNumber();
118 
119  for (Idx i = 0, j = 0; j < order_.size(); i = (i + 1) % num_threads, ++j) {
120  if (i == this_thread) {
121  for (Idx k = j + 1; k < order_.size(); ++k) {
122  // try arc additions
126  }
127  }
128  }
129  }
130  }
131 
132  // now store the changes into the protected vectors of the
133  // GraphChangesGenerator4K2
134  for (const auto& changes: legal_changes) {
135  for (const auto& change: changes) {
137  }
138  }
139  }
140 
141  /// sets a new graph from which the operator will compute possible changes
142  template < typename STRUCT_CONSTRAINT >
144  // sets the current graph
145  graph_ = graph;
146 
147  // check that all the nodes of the graph belong to the sequence.
148  // If some are missing, add them in increasing order into the sequence.
149  // If some element of order_ do not belong to the graph, remove them
150  for (auto node = order_.beginSafe(); node != order_.endSafe(); ++node) {
151  if (!graph.exists(*node)) { order_.erase(node); }
152  }
153  for (const auto node: graph) {
154  if (!order_.exists(node)) { order_.insert(node); }
155  }
156 
157  // generate the set of all changes
158  createChanges_();
159  }
160 
161  /// set a new order on the random variables
162  template < typename STRUCT_CONSTRAINT >
163  INLINE void
165  order_ = order;
166  }
167 
168  /// set a new order on the random variables
169  template < typename STRUCT_CONSTRAINT >
170  INLINE void
172  order_.clear();
173  for (const auto node: order) {
174  order_.insert(node);
175  }
176  }
177 
178  /// empty the set of possible change operators that can be applied
179  template < typename STRUCT_CONSTRAINT >
182  }
183 
184  /// returns an (unsafe) iterator on the beginning of the list of operators
185  template < typename STRUCT_CONSTRAINT >
188  return legal_changes_.cbegin();
189  }
190 
191  /// returns an (unsafe) iterator on the end of the list of operators
192  template < typename STRUCT_CONSTRAINT >
195  return legal_changes_.cend();
196  }
197 
198  /// notify the operator set of a change applied to the graph
199  template < typename STRUCT_CONSTRAINT >
200  INLINE void
202 
203  /// notify the operator set of a change applied to the graph
204  template < typename STRUCT_CONSTRAINT >
205  INLINE void
207 
208  /// notify the operator set of a change applied to the graph
209  template < typename STRUCT_CONSTRAINT >
210  INLINE void
212 
213  /// notify the operator set of a change applied to the graph
214  template < typename STRUCT_CONSTRAINT >
215  INLINE void
217 
218  /// notifies the generator that we have parsed all its legal changes
219  template < typename STRUCT_CONSTRAINT >
222  }
223 
224  /// sets the maximum number of threads used to perform countings
225  template < typename STRUCT_CONSTRAINT >
227 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
228  if (nb == 0) nb = getMaxNumberOfThreads();
230 # else
232 # endif /* _OPENMP && GUM_DEBUG_MODE */
233  }
234 
235  /// returns the constraint that is used by the generator
236  template < typename STRUCT_CONSTRAINT >
239  return *constraint_;
240  }
241 
242  } /* namespace learning */
243 
244 } /* namespace gum */
245 
246 #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)