aGrUM  0.16.0
graphChangesGenerator4K2_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  STRUCT_CONSTRAINT& constraint) :
39  _constraint(&constraint) {
40  GUM_CONSTRUCTOR(GraphChangesGenerator4K2);
41  }
42 
44  template < typename STRUCT_CONSTRAINT >
46  const GraphChangesGenerator4K2& from) :
47  _graph(from._graph),
48  _constraint(from._constraint), _order(from._order),
51  GUM_CONS_CPY(GraphChangesGenerator4K2);
52  }
53 
55  template < typename STRUCT_CONSTRAINT >
57  GraphChangesGenerator4K2&& from) :
58  _graph(std::move(from._graph)),
59  _constraint(from._constraint), _order(std::move(from._order)),
60  _legal_changes(std::move(from._legal_changes)),
62  GUM_CONS_MOV(GraphChangesGenerator4K2);
63  }
64 
66  template < typename STRUCT_CONSTRAINT >
68  GUM_DESTRUCTOR(GraphChangesGenerator4K2);
69  }
70 
72  template < typename STRUCT_CONSTRAINT >
73  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
75  operator=(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT >& from) {
76  if (this != &from) {
77  _graph = from._graph;
78  _constraint = from._constraint;
79  _order = from._order;
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  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
90  operator=(GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&& from) {
91  if (this != &from) {
92  _graph = std::move(from._graph);
93  _constraint = std::move(from._constraint);
94  _order = std::move(from._order);
95  _legal_changes = std::move(from._legal_changes);
96  __max_threads_number = from.__max_threads_number;
97  }
98  return *this;
99  }
100 
102  template < typename STRUCT_CONSTRAINT >
104  _legal_changes.clear();
105 
106  // for all the pairs of nodes, consider adding, reverse and removing arcs
107  std::vector< Set< GraphChange > > legal_changes;
108 # pragma omp parallel num_threads(int(__max_threads_number))
109  {
110  int num_threads = getNumberOfRunningThreads();
111 
112 # pragma omp single
113  {
114  // resize the change vectors so that each thread can write to its
115  // own vector
116  legal_changes.resize(num_threads);
117  }
118 
119  const Size this_thread = getThreadNumber();
120 
121  for (Idx i = 0, j = 0; j < _order.size(); i = (i + 1) % num_threads, ++j) {
122  if (i == this_thread) {
123  for (Idx k = j + 1; k < _order.size(); ++k) {
124  // try arc additions
125  ArcAddition arc_add(_order[j], _order[k]);
126  if (!_constraint->isAlwaysInvalid(arc_add)) {
127  legal_changes[this_thread].insert(std::move(arc_add));
128  }
129  }
130  }
131  }
132  }
133 
134  // now store the changes into the protected vectors of the
135  // GraphChangesGenerator4K2
136  for (const auto& changes : legal_changes) {
137  for (const auto& change : changes) {
138  _legal_changes.insert(std::move(change));
139  }
140  }
141  }
142 
144  template < typename STRUCT_CONSTRAINT >
146  const DiGraph& graph) {
147  // sets the current graph
148  _graph = graph;
149 
150  // check that all the nodes of the graph belong to the sequence.
151  // If some are missing, add them in increasing order into the sequence.
152  // If some element of _order do not belong to the graph, remove them
153  for (auto node = _order.beginSafe(); node != _order.endSafe(); ++node) {
154  if (!graph.exists(*node)) { _order.erase(node); }
155  }
156  for (const auto node : graph) {
157  if (!_order.exists(node)) { _order.insert(node); }
158  }
159 
160  // generate the set of all changes
161  _createChanges();
162  }
163 
165  template < typename STRUCT_CONSTRAINT >
167  const Sequence< NodeId >& order) {
168  _order = order;
169  }
170 
172  template < typename STRUCT_CONSTRAINT >
174  const std::vector< NodeId >& order) {
175  _order.clear();
176  for (const auto node : order) {
177  _order.insert(node);
178  }
179  }
180 
182  template < typename STRUCT_CONSTRAINT >
183  INLINE void
185  _legal_changes.clear();
186  }
187 
189  template < typename STRUCT_CONSTRAINT >
192  return _legal_changes.cbegin();
193  }
194 
196  template < typename STRUCT_CONSTRAINT >
199  return _legal_changes.cend();
200  }
201 
203  template < typename STRUCT_CONSTRAINT >
205  const ArcAddition& change) {}
206 
208  template < typename STRUCT_CONSTRAINT >
210  const ArcDeletion& change) {}
211 
213  template < typename STRUCT_CONSTRAINT >
215  const ArcReversal& change) {}
216 
218  template < typename STRUCT_CONSTRAINT >
220  const GraphChange& change) {}
221 
223  template < typename STRUCT_CONSTRAINT >
224  INLINE void
226  if (_legal_changes.size()) _legal_changes.clear();
227  }
228 
230  template < typename STRUCT_CONSTRAINT >
232  Size nb) noexcept {
233 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
234  if (nb == 0) nb = getMaxNumberOfThreads();
236 # else
238 # endif /* _OPENMP && GUM_DEBUG_MODE */
239  }
240 
242  template < typename STRUCT_CONSTRAINT >
243  INLINE STRUCT_CONSTRAINT&
245  return *_constraint;
246  }
247 
248  } /* namespace learning */
249 
250 } /* namespace gum */
251 
252 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
typename Set< GraphChange >::const_iterator iterator
the iterator for parsing the list of possible graph change operators
GraphChangesGenerator4K2(STRUCT_CONSTRAINT &constraint)
default constructor
void clear()
Clear the sequence.
Definition: sequence_tpl.h:271
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
Definition: sequence_tpl.h:634
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
void notifyGetCompleted()
notifies the generator that we have parsed all its legal changes
unsigned int getThreadNumber()
Get the calling thread id.
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:38
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
Set< GraphChange > _legal_changes
the current set of graph changes
void setGraph(const DiGraph &graph)
sets a new graph from which the generator will compute possible changes
STL namespace.
unsigned int getMaxNumberOfThreads()
Returns the maximum number of threads at any time.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void setOrder(const Sequence< NodeId > &order)
set a new order on the random variables
STRUCT_CONSTRAINT & constraint() const noexcept
returns the constraint that is used by the generator
iterator_safe beginSafe() const
Returns a safe begin iterator.
Definition: sequence_tpl.h:627
void setMaxNbThreads(Size nb) noexcept
sets the maximum number of threads used to compute the set of changes
DiGraph _graph
the graph on which we generate operators
GraphChangesGenerator4K2< STRUCT_CONSTRAINT > & operator=(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT > &from)
copy operator
STRUCT_CONSTRAINT * _constraint
the structural constraint used to restrict the changes
void modifyGraph(const ArcAddition &change)
notify the generator of a change applied to the graph
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:402
void _createChanges()
create the set of legal and illegal changes from a given graph
Size __max_threads_number
the max number of threads authorized
Sequence< NodeId > _order
the order on the variables
void erase(const Key &k)
Remove an element from the sequence.
Definition: sequence_tpl.h:453
virtual ~GraphChangesGenerator4K2()
destructor
void clearChanges() noexcept
empty the set of possible change operators that can be applied
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408