aGrUM  0.16.0
graphChangesGenerator4DiGraph_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  GraphChangesGenerator4DiGraph(STRUCT_CONSTRAINT& constraint) :
39  _constraint(&constraint) {
40  GUM_CONSTRUCTOR(GraphChangesGenerator4DiGraph);
41  }
42 
44  template < typename STRUCT_CONSTRAINT >
47  _graph(from._graph),
50  GUM_CONS_CPY(GraphChangesGenerator4DiGraph);
51  }
52 
54  template < typename STRUCT_CONSTRAINT >
57  _graph(std::move(from._graph)),
59  _legal_changes(std::move(from._legal_changes)),
61  GUM_CONS_MOV(GraphChangesGenerator4DiGraph);
62  }
63 
65  template < typename STRUCT_CONSTRAINT >
67  STRUCT_CONSTRAINT >::~GraphChangesGenerator4DiGraph() {
68  GUM_DESTRUCTOR(GraphChangesGenerator4DiGraph);
69  }
70 
72  template < typename STRUCT_CONSTRAINT >
73  GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
75  const GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >& from) {
76  if (this != &from) {
77  _graph = from._graph;
78  _constraint = from._constraint;
79  _legal_changes = from._legal_changes;
80  __max_threads_number = from.__max_threads_number;
81  }
82  return *this;
83  }
84 
86  template < typename STRUCT_CONSTRAINT >
87  GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
89  operator=(GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&& from) {
90  if (this != &from) {
91  _graph = std::move(from._graph);
92  _constraint = std::move(from._constraint);
93  _legal_changes = std::move(from._legal_changes);
94  __max_threads_number = from.__max_threads_number;
95  }
96  return *this;
97  }
98 
100  template < typename STRUCT_CONSTRAINT >
102  _legal_changes.clear();
103 
104  // for all the pairs of nodes, consider adding, reverse and removing arcs
105  std::vector< Set< GraphChange > > legal_changes;
106 # pragma omp parallel num_threads(int(__max_threads_number))
107  {
108  int num_threads = getNumberOfRunningThreads();
109 
110 # pragma omp single
111  {
112  // resize the change vectors so that each thread can write to its
113  // own vector
114  legal_changes.resize(num_threads);
115  }
116 
117  const Size this_thread = getThreadNumber();
118 
119  Idx i = 0;
120  for (const auto node1 : _graph) {
121  if (i == this_thread) {
122  for (const auto node2 : _graph) {
123  if (node1 != node2) {
124  // try arc additions
125  ArcAddition arc_add(node1, node2);
126  if (!_constraint->isAlwaysInvalid(arc_add)) {
127  legal_changes[this_thread].insert(std::move(arc_add));
128  }
129 
130  // try arc deletion
131  ArcDeletion arc_del(node1, node2);
132  if (!_constraint->isAlwaysInvalid(arc_del)) {
133  legal_changes[this_thread].insert(std::move(arc_del));
134  }
135 
136  // try arc reversal
137  ArcReversal arc_rev(node1, node2);
138  if (!_constraint->isAlwaysInvalid(arc_rev)) {
139  legal_changes[this_thread].insert(std::move(arc_rev));
140  }
141  }
142  }
143  }
144  ++i;
145  i %= num_threads;
146  }
147  }
148 
149  // now store the changes into the protected vectors of the
150  // GraphChangesGenerator4DiGraph
151  for (const auto& changes : legal_changes) {
152  for (const auto& change : changes) {
153  _legal_changes.insert(std::move(change));
154  }
155  }
156  }
157 
159  template < typename STRUCT_CONSTRAINT >
161  const DiGraph& graph) {
162  // sets the current graph
163  _graph = graph;
164 
165  // generate the set of all changes
166  _createChanges();
167  }
168 
170  template < typename STRUCT_CONSTRAINT >
171  INLINE void GraphChangesGenerator4DiGraph<
172  STRUCT_CONSTRAINT >::clearChanges() noexcept {
173  _legal_changes.clear();
174  }
175 
177  template < typename STRUCT_CONSTRAINT >
180  return _legal_changes.cbegin();
181  }
182 
184  template < typename STRUCT_CONSTRAINT >
185  INLINE const typename GraphChangesGenerator4DiGraph<
186  STRUCT_CONSTRAINT >::iterator&
188  return _legal_changes.cend();
189  }
190 
192  template < typename STRUCT_CONSTRAINT >
194  const ArcAddition& change) {}
195 
197  template < typename STRUCT_CONSTRAINT >
199  const ArcDeletion& change) {}
200 
202  template < typename STRUCT_CONSTRAINT >
204  const ArcReversal& change) {}
205 
207  template < typename STRUCT_CONSTRAINT >
209  const GraphChange& change) {}
210 
212  template < typename STRUCT_CONSTRAINT >
213  INLINE void
215  if (_legal_changes.size()) _legal_changes.clear();
216  }
217 
219  template < typename STRUCT_CONSTRAINT >
220  INLINE void
222  Size nb) noexcept {
223 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
224  if (nb == 0) nb = getMaxNumberOfThreads();
226 # else
228 # endif /* _OPENMP && GUM_DEBUG_MODE */
229  }
230 
232  template < typename STRUCT_CONSTRAINT >
233  INLINE STRUCT_CONSTRAINT&
235  noexcept {
236  return *_constraint;
237  }
238 
239  } /* namespace learning */
240 
241 } /* namespace gum */
242 
243 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
unsigned int getThreadNumber()
Get the calling thread id.
Size __max_threads_number
the max number of threads authorized
void modifyGraph(const ArcAddition &change)
notify the generator of a change applied to the graph
STL namespace.
unsigned int getMaxNumberOfThreads()
Returns the maximum number of threads at any time.
void setMaxNbThreads(Size nb) noexcept
sets the maximum number of threads used to compute the set of changes
void setGraph(const DiGraph &graph)
sets a new graph from which the generator will compute possible changes
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
const iterator & end() const
returns an (unsafe) iterator on the end of the list of operators
iterator begin() const
returns an (unsafe) iterator on the beginning of the list of operators
DiGraph _graph
the graph on which we generate operators
Set< GraphChange > _legal_changes
the current set of graph changes
virtual ~GraphChangesGenerator4DiGraph()
destructor
typename Set< GraphChange >::const_iterator iterator
the iterator for parsing the list of possible graph change operators
STRUCT_CONSTRAINT & constraint() const noexcept
returns the constraint that is used by the generator
GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT > & operator=(const GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT > &from)
copy operator
GraphChangesGenerator4DiGraph(STRUCT_CONSTRAINT &constraint)
default constructor
void clearChanges() noexcept
empty the set of possible change operators that can be applied
STRUCT_CONSTRAINT * _constraint
the structural constraint used to restrict the changes
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
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48