aGrUM  0.14.2
graphChangesGenerator4K2_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}@lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it wil be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 namespace gum {
29 
30  namespace learning {
31 
33  template < typename STRUCT_CONSTRAINT >
35  STRUCT_CONSTRAINT& constraint) :
36  _constraint(&constraint) {
37  GUM_CONSTRUCTOR(GraphChangesGenerator4K2);
38  }
39 
41  template < typename STRUCT_CONSTRAINT >
43  const GraphChangesGenerator4K2& from) :
44  _graph(from._graph),
45  _constraint(from._constraint), _order(from._order),
48  GUM_CONS_CPY(GraphChangesGenerator4K2);
49  }
50 
52  template < typename STRUCT_CONSTRAINT >
54  GraphChangesGenerator4K2&& from) :
55  _graph(std::move(from._graph)),
56  _constraint(from._constraint), _order(std::move(from._order)),
57  _legal_changes(std::move(from._legal_changes)),
59  GUM_CONS_MOV(GraphChangesGenerator4K2);
60  }
61 
63  template < typename STRUCT_CONSTRAINT >
65  GUM_DESTRUCTOR(GraphChangesGenerator4K2);
66  }
67 
69  template < typename STRUCT_CONSTRAINT >
70  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
72  operator=(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT >& from) {
73  if (this != &from) {
74  _graph = from._graph;
75  _constraint = from._constraint;
76  _order = from._order;
77  _legal_changes = from._legal_changes;
78  __max_threads_number = from.__max_threads_number;
79  }
80  return *this;
81  }
82 
84  template < typename STRUCT_CONSTRAINT >
85  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
87  operator=(GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&& from) {
88  if (this != &from) {
89  _graph = std::move(from._graph);
90  _constraint = std::move(from._constraint);
91  _order = std::move(from._order);
92  _legal_changes = std::move(from._legal_changes);
93  __max_threads_number = from.__max_threads_number;
94  }
95  return *this;
96  }
97 
99  template < typename STRUCT_CONSTRAINT >
101  _legal_changes.clear();
102 
103  // for all the pairs of nodes, consider adding, reverse and removing arcs
104  std::vector< Set< GraphChange > > legal_changes;
105 # pragma omp parallel num_threads(int(__max_threads_number))
106  {
107  int num_threads = getNumberOfRunningThreads();
108 
109 # pragma omp single
110  {
111  // resize the change vectors so that each thread can write to its
112  // own vector
113  legal_changes.resize(num_threads);
114  }
115 
116  const Size this_thread = getThreadNumber();
117 
118  for (Idx i = 0, j = 0; j < _order.size(); i = (i + 1) % num_threads, ++j) {
119  if (i == this_thread) {
120  for (Idx k = j + 1; k < _order.size(); ++k) {
121  // try arc additions
122  ArcAddition arc_add(_order[j], _order[k]);
123  if (!_constraint->isAlwaysInvalid(arc_add)) {
124  legal_changes[this_thread].insert(std::move(arc_add));
125  }
126  }
127  }
128  }
129  }
130 
131  // now store the changes into the protected vectors of the
132  // GraphChangesGenerator4K2
133  for (const auto& changes : legal_changes) {
134  for (const auto& change : changes) {
135  _legal_changes.insert(std::move(change));
136  }
137  }
138  }
139 
141  template < typename STRUCT_CONSTRAINT >
143  const DiGraph& graph) {
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 
162  template < typename STRUCT_CONSTRAINT >
164  const Sequence< NodeId >& order) {
165  _order = order;
166  }
167 
169  template < typename STRUCT_CONSTRAINT >
171  const std::vector< NodeId >& order) {
172  _order.clear();
173  for (const auto node : order) {
174  _order.insert(node);
175  }
176  }
177 
179  template < typename STRUCT_CONSTRAINT >
180  INLINE void
182  _legal_changes.clear();
183  }
184 
186  template < typename STRUCT_CONSTRAINT >
189  return _legal_changes.cbegin();
190  }
191 
193  template < typename STRUCT_CONSTRAINT >
196  return _legal_changes.cend();
197  }
198 
200  template < typename STRUCT_CONSTRAINT >
202  const ArcAddition& change) {}
203 
205  template < typename STRUCT_CONSTRAINT >
207  const ArcDeletion& change) {}
208 
210  template < typename STRUCT_CONSTRAINT >
212  const ArcReversal& change) {}
213 
215  template < typename STRUCT_CONSTRAINT >
217  const GraphChange& change) {}
218 
220  template < typename STRUCT_CONSTRAINT >
221  INLINE void
223  if (_legal_changes.size()) _legal_changes.clear();
224  }
225 
227  template < typename STRUCT_CONSTRAINT >
229  Size nb) noexcept {
230 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
231  if (nb == 0) nb = getMaxNumberOfThreads();
233 # else
235 # endif /* _OPENMP && GUM_DEBUG_MODE */
236  }
237 
239  template < typename STRUCT_CONSTRAINT >
240  INLINE STRUCT_CONSTRAINT&
242  return *_constraint;
243  }
244 
245  } /* namespace learning */
246 
247 } /* namespace gum */
248 
249 #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:268
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
Definition: sequence_tpl.h:631
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:35
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.
gum is the global namespace for all aGrUM entities
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:624
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:399
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:450
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:45
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:405