aGrUM  0.16.0
graphChangesGenerator4UndiGraph_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  GraphChangesGenerator4UndiGraph(STRUCT_CONSTRAINT& constraint) :
39  _constraint(&constraint) {
40  GUM_CONSTRUCTOR(GraphChangesGenerator4UndiGraph);
41  }
42 
44  template < typename STRUCT_CONSTRAINT >
47  const GraphChangesGenerator4UndiGraph& from) :
48  _graph(from._graph),
51  GUM_CONS_CPY(GraphChangesGenerator4UndiGraph);
52  }
53 
55  template < typename STRUCT_CONSTRAINT >
58  _graph(std::move(from._graph)),
60  _legal_changes(std::move(from._legal_changes)),
62  GUM_CONS_MOV(GraphChangesGenerator4UndiGraph);
63  }
64 
66  template < typename STRUCT_CONSTRAINT >
68  STRUCT_CONSTRAINT >::~GraphChangesGenerator4UndiGraph() {
69  GUM_DESTRUCTOR(GraphChangesGenerator4UndiGraph);
70  }
71 
73  template < typename STRUCT_CONSTRAINT >
74  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
76  const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& from) {
77  if (this != &from) {
78  _graph = from._graph;
79  _constraint = from._constraint;
80  _legal_changes = from._legal_changes;
81  __max_threads_number = from.__max_threads_number;
82  }
83  return *this;
84  }
85 
87  template < typename STRUCT_CONSTRAINT >
88  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
90  operator=(GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&& from) {
91  if (this != &from) {
92  _graph = std::move(from._graph);
93  _constraint = std::move(from._constraint);
94  _legal_changes = std::move(from._legal_changes);
95  __max_threads_number = from.__max_threads_number;
96  }
97  return *this;
98  }
99 
101  template < typename STRUCT_CONSTRAINT >
103  _legal_changes.clear();
104 
105  // for all the pairs of nodes, consider adding, reverse and removing edges
106  std::vector< Set< GraphChange > > legal_changes;
107 # pragma omp parallel num_threads(__max_threads_number)
108  {
109  int num_threads = getNumberOfRunningThreads();
110 
111 # pragma omp single
112  {
113  // resize the change vectors so that each thread can write to its
114  // own vector
115  legal_changes.resize(num_threads);
116  }
117 
118  const Idx this_thread = getThreadNumber();
119 
120  Idx i = 0;
121  for (const auto node1 : _graph) {
122  if (i == this_thread) {
123  for (const auto node2 : _graph) {
124  if (node1 != node2) {
125  // try edge additions
126  EdgeAddition edge_add(node1, node2);
127  if (!_constraint->isAlwaysInvalid(edge_add)) {
128  legal_changes[this_thread].insert(std::move(edge_add));
129  }
130 
131  // try edge deletion
132  EdgeDeletion edge_del(node1, node2);
133  if (!_constraint->isAlwaysInvalid(edge_del)) {
134  legal_changes[this_thread].insert(std::move(edge_del));
135  }
136  }
137  }
138  }
139  ++i;
140  i %= num_threads;
141  }
142  }
143 
144  // now store the changes into the protected vectors of the
145  // GraphChangesGenerator4UndiGraph
146  for (const auto& changes : legal_changes) {
147  for (const auto& change : changes) {
148  _legal_changes.insert(std::move(change));
149  }
150  }
151  }
152 
154  template < typename STRUCT_CONSTRAINT >
156  const UndiGraph& graph) {
157  // sets the current graph
158  _graph = graph;
159 
160  // generate the set of all changes
161  _createChanges();
162  }
163 
165  template < typename STRUCT_CONSTRAINT >
167  STRUCT_CONSTRAINT >::clearChanges() noexcept {
168  _legal_changes.clear();
169  }
170 
172  template < typename STRUCT_CONSTRAINT >
175  return _legal_changes.cbegin();
176  }
177 
179  template < typename STRUCT_CONSTRAINT >
180  INLINE const typename GraphChangesGenerator4UndiGraph<
181  STRUCT_CONSTRAINT >::iterator&
183  return _legal_changes.cend();
184  }
185 
187  template < typename STRUCT_CONSTRAINT >
189  const EdgeAddition& change) {}
190 
192  template < typename STRUCT_CONSTRAINT >
194  const EdgeDeletion& change) {}
195 
197  template < typename STRUCT_CONSTRAINT >
199  const GraphChange& change) {}
200 
202  template < typename STRUCT_CONSTRAINT >
203  INLINE void
205  if (_legal_changes.size()) _legal_changes.clear();
206  }
207 
209  template < typename STRUCT_CONSTRAINT >
210  INLINE void
212  Size nb) noexcept {
213 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
214  if (nb == 0) nb = getMaxNumberOfThreads();
216 # else
218 # endif /* _OPENMP && GUM_DEBUG_MODE */
219  }
220 
222  template < typename STRUCT_CONSTRAINT >
223  INLINE STRUCT_CONSTRAINT&
225  noexcept {
226  return *_constraint;
227  }
228 
229  } /* namespace learning */
230 
231 } /* namespace gum */
232 
233 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
void _createChanges()
create the set of legal and illegal changes from a given graph
void notifyGetCompleted()
notifies the generator that we have parsed all its legal changes
unsigned int getThreadNumber()
Get the calling thread id.
void setGraph(const UndiGraph &graph)
sets a new graph from which the operator will compute possible changes
iterator begin() const
returns an (unsafe) iterator on the beginning of the list of operators
STL namespace.
unsigned int getMaxNumberOfThreads()
Returns the maximum number of threads at any time.
STRUCT_CONSTRAINT * _constraint
a reference on the structural constraint used to restrict the changes
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
GraphChangesGenerator4UndiGraph(STRUCT_CONSTRAINT &constraint)
default constructor
UndiGraph _graph
the graph on which we generate operators
void clearChanges() noexcept
empty the set of possible change operators that can be applied
Set< GraphChange > _legal_changes
the current set of operators
typename Set< GraphChange >::const_iterator iterator
the iterator for parsing the list of possible graph change operators
Size __max_threads_number
the max number of threads authorized
void setMaxNbThreads(Size nb) noexcept
sets the maximum number of threads used to compute the set of changes
STRUCT_CONSTRAINT & constraint() const noexcept
returns the constraint that is used by the generator
GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT > & operator=(const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT > &from)
copy operator
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void modifyGraph(const EdgeAddition &change)
notify the operator set of a change applied to the graph
const iterator & end() const
returns an (unsafe) iterator on the end of the list of operators