aGrUM  0.16.0
graphChangesGeneratorOnSubDiGraph_tpl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 namespace gum {
32 
33  namespace learning {
34 
36  template < typename STRUCT_CONSTRAINT >
38  GraphChangesGeneratorOnSubDiGraph(STRUCT_CONSTRAINT& constraint) :
39  _constraint(&constraint) {
40  GUM_CONSTRUCTOR(GraphChangesGeneratorOnSubDiGraph);
41  }
42 
44  template < typename STRUCT_CONSTRAINT >
53  }
54 
56  template < typename STRUCT_CONSTRAINT >
61  _target_nodes(std::move(from._target_nodes)),
62  _tail_nodes(std::move(from._tail_nodes)),
63  _legal_changes(std::move(from._legal_changes)),
66  }
67 
69  template < typename STRUCT_CONSTRAINT >
71  STRUCT_CONSTRAINT >::~GraphChangesGeneratorOnSubDiGraph() {
72  GUM_DESTRUCTOR(GraphChangesGeneratorOnSubDiGraph);
73  }
74 
76  template < typename STRUCT_CONSTRAINT >
77  GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >&
79  const GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >& from) {
80  if (this != &from) {
81  _constraint = from._constraint;
82  _target_nodes = from._target_nodes;
83  _tail_nodes = from._tail_nodes;
84  _legal_changes = from._legal_changes;
85  __max_threads_number = from.__max_threads_number;
86  }
87  return *this;
88  }
89 
91  template < typename STRUCT_CONSTRAINT >
92  GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >&
94  GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >&& from) {
95  if (this != &from) {
96  _constraint = std::move(from._constraint);
97  _target_nodes = std::move(from._target_nodes);
98  _tail_nodes = std::move(from._tail_nodes);
99  _legal_changes = std::move(from._legal_changes);
100  __max_threads_number = from.__max_threads_number;
101  }
102  return *this;
103  }
104 
106  template < typename STRUCT_CONSTRAINT >
108  _legal_changes.clear();
109 
110  // for all the pairs of nodes, consider adding, reverse and removing arcs
111  std::vector< Set< GraphChange > > legal_changes;
112 # pragma omp parallel num_threads(__max_threads_number)
113  {
114  int num_threads = getNumberOfRunningThreads();
115 
116 # pragma omp single
117  {
118  // resize the change vectors so that each thread can write to its
119  // own vector
120  legal_changes.resize(num_threads);
121  }
122 
123  const Size this_thread = getThreadNumber();
124 
125  Idx i = 0;
126  for (const auto node1 : _tail_nodes) {
127  if (i == this_thread) {
128  for (const auto node2 : _target_nodes) {
129  if (node1 != node2) {
130  // try arc additions
131  ArcAddition arc_add(node1, node2);
132  if (!_constraint->isAlwaysInvalid(arc_add)) {
133  legal_changes[this_thread].insert(std::move(arc_add));
134  }
135 
136  // try arc deletion
137  ArcDeletion arc_del(node1, node2);
138  if (!_constraint->isAlwaysInvalid(arc_del)) {
139  legal_changes[this_thread].insert(std::move(arc_del));
140  }
141 
142  // try arc reversal
143  ArcReversal arc_rev(node1, node2);
144  if (!_constraint->isAlwaysInvalid(arc_rev)) {
145  legal_changes[this_thread].insert(std::move(arc_rev));
146  }
147  }
148  }
149  }
150  ++i;
151  i %= num_threads;
152  }
153  }
154 
155  // now store the changes into the protected vectors of the
156  // GraphChangesGeneratorOnSubDiGraph
157  for (const auto& changes : legal_changes) {
158  for (const auto& change : changes) {
159  _legal_changes.insert(std::move(change));
160  }
161  }
162  }
163 
165  template < typename STRUCT_CONSTRAINT >
167  const DiGraph& graph) {
168  // generate the set of all changes
169  _createChanges();
170  }
171 
173  template < typename STRUCT_CONSTRAINT >
175  const NodeSet& nodes) {
176  _target_nodes = nodes;
177  }
178 
180  template < typename STRUCT_CONSTRAINT >
182  NodeId node) {
183  _target_nodes.insert(node);
184  }
185 
187  template < typename STRUCT_CONSTRAINT >
188  INLINE void
190  NodeId node) {
191  _target_nodes.erase(node);
192  }
193 
195  template < typename STRUCT_CONSTRAINT >
197  const NodeSet& nodes) {
198  _tail_nodes = nodes;
199  }
200 
202  template < typename STRUCT_CONSTRAINT >
204  Size nb_nodes) {
205  _tail_nodes.clear();
206  for (Idx i = 0; i < nb_nodes; ++i) {
207  _tail_nodes.insert(i);
208  }
209  }
210 
212  template < typename STRUCT_CONSTRAINT >
214  NodeId node) {
215  _tail_nodes = node;
216  }
217 
219  template < typename STRUCT_CONSTRAINT >
221  NodeId node) {
222  _tail_nodes.erase(node);
223  }
224 
226  template < typename STRUCT_CONSTRAINT >
228  STRUCT_CONSTRAINT >::clearChanges() noexcept {
229  _legal_changes.clear();
230  }
231 
233  template < typename STRUCT_CONSTRAINT >
234  INLINE
237  return _legal_changes.cbegin();
238  }
239 
241  template < typename STRUCT_CONSTRAINT >
242  INLINE const typename GraphChangesGeneratorOnSubDiGraph<
243  STRUCT_CONSTRAINT >::iterator&
245  return _legal_changes.cend();
246  }
247 
249  template < typename STRUCT_CONSTRAINT >
250  INLINE void
252  const ArcAddition& change) {}
253 
255  template < typename STRUCT_CONSTRAINT >
256  INLINE void
258  const ArcDeletion& change) {}
259 
261  template < typename STRUCT_CONSTRAINT >
262  INLINE void
264  const ArcReversal& change) {}
265 
267  template < typename STRUCT_CONSTRAINT >
268  INLINE void
270  const GraphChange& change) {}
271 
273  template < typename STRUCT_CONSTRAINT >
275  STRUCT_CONSTRAINT >::notifyGetCompleted() {
276  if (_legal_changes.size()) _legal_changes.clear();
277  }
278 
280  template < typename STRUCT_CONSTRAINT >
281  INLINE void
283  Size nb) noexcept {
284 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
285  if (nb == 0) nb = getMaxNumberOfThreads();
287 # else
289 # endif /* _OPENMP && GUM_DEBUG_MODE */
290  }
291 
293  template < typename STRUCT_CONSTRAINT >
294  INLINE STRUCT_CONSTRAINT&
296  noexcept {
297  return *_constraint;
298  }
299 
300  } /* namespace learning */
301 
302 } /* namespace gum */
303 
304 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > & operator=(const GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > &from)
copy operator
NodeSet _tail_nodes
the tail nodes (other extremities than the targets)
STRUCT_CONSTRAINT & constraint() const noexcept
returns the constraint that is used by the generator
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
unsigned int getThreadNumber()
Get the calling thread id.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Set< GraphChange > _legal_changes
the current set of operators
void eraseTarget(NodeId node)
removes a target
GraphChangesGeneratorOnSubDiGraph(STRUCT_CONSTRAINT &constraint)
default constructor
void eraseTail(NodeId node)
removes a tail node
STL namespace.
unsigned int getMaxNumberOfThreads()
Returns the maximum number of threads at any time.
const iterator & end() const
returns an (unsafe) iterator on the end of the list of operators
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void setTails(const NodeSet &nodes)
assign a set of "tail" nodes
void modifyGraph(const ArcAddition &change)
notify the operator set of a change applied to the graph
void clearChanges() noexcept
empty the set of possible change operators that can be applied
Size __max_threads_number
the max number of threads authorized
void setGraph(const DiGraph &graph)
sets a new graph from which the operator will compute possible changes
void _createChanges()
create the set of legal and illegal changes from a given graph
iterator begin() const
returns an (unsafe) iterator on the beginning of the list of operators
STRUCT_CONSTRAINT * _constraint
a reference on the structural constraint used to restrict the changes
void setMaxNbThreads(Size nb) noexcept
sets the maximum number of threads used to compute the set of changes
void setTargets(const NodeSet &nodes)
assign a set of target nodes
void addTarget(NodeId node)
adds a new target node
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void addTail(NodeId node)
adds a new "tail" node
Size NodeId
Type for node ids.
Definition: graphElements.h:98
typename Set< GraphChange >::const_iterator iterator
the iterator for parsing the list of possible graph change operators
void notifyGetCompleted()
notifies the generator that we have parsed all its legal changes