aGrUM  0.14.2
graphChangesGenerator4UndiGraph_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  GraphChangesGenerator4UndiGraph(STRUCT_CONSTRAINT& constraint) :
36  _constraint(&constraint) {
37  GUM_CONSTRUCTOR(GraphChangesGenerator4UndiGraph);
38  }
39 
41  template < typename STRUCT_CONSTRAINT >
44  const GraphChangesGenerator4UndiGraph& from) :
45  _graph(from._graph),
48  GUM_CONS_CPY(GraphChangesGenerator4UndiGraph);
49  }
50 
52  template < typename STRUCT_CONSTRAINT >
55  _graph(std::move(from._graph)),
57  _legal_changes(std::move(from._legal_changes)),
59  GUM_CONS_MOV(GraphChangesGenerator4UndiGraph);
60  }
61 
63  template < typename STRUCT_CONSTRAINT >
65  STRUCT_CONSTRAINT >::~GraphChangesGenerator4UndiGraph() {
66  GUM_DESTRUCTOR(GraphChangesGenerator4UndiGraph);
67  }
68 
70  template < typename STRUCT_CONSTRAINT >
71  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
73  const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& from) {
74  if (this != &from) {
75  _graph = from._graph;
76  _constraint = from._constraint;
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  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
87  operator=(GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&& from) {
88  if (this != &from) {
89  _graph = std::move(from._graph);
90  _constraint = std::move(from._constraint);
91  _legal_changes = std::move(from._legal_changes);
92  __max_threads_number = from.__max_threads_number;
93  }
94  return *this;
95  }
96 
98  template < typename STRUCT_CONSTRAINT >
100  _legal_changes.clear();
101 
102  // for all the pairs of nodes, consider adding, reverse and removing edges
103  std::vector< Set< GraphChange > > legal_changes;
104 # pragma omp parallel num_threads(__max_threads_number)
105  {
106  int num_threads = getNumberOfRunningThreads();
107 
108 # pragma omp single
109  {
110  // resize the change vectors so that each thread can write to its
111  // own vector
112  legal_changes.resize(num_threads);
113  }
114 
115  const Idx this_thread = getThreadNumber();
116 
117  Idx i = 0;
118  for (const auto node1 : _graph) {
119  if (i == this_thread) {
120  for (const auto node2 : _graph) {
121  if (node1 != node2) {
122  // try edge additions
123  EdgeAddition edge_add(node1, node2);
124  if (!_constraint->isAlwaysInvalid(edge_add)) {
125  legal_changes[this_thread].insert(std::move(edge_add));
126  }
127 
128  // try edge deletion
129  EdgeDeletion edge_del(node1, node2);
130  if (!_constraint->isAlwaysInvalid(edge_del)) {
131  legal_changes[this_thread].insert(std::move(edge_del));
132  }
133  }
134  }
135  }
136  ++i;
137  i %= num_threads;
138  }
139  }
140 
141  // now store the changes into the protected vectors of the
142  // GraphChangesGenerator4UndiGraph
143  for (const auto& changes : legal_changes) {
144  for (const auto& change : changes) {
145  _legal_changes.insert(std::move(change));
146  }
147  }
148  }
149 
151  template < typename STRUCT_CONSTRAINT >
153  const UndiGraph& graph) {
154  // sets the current graph
155  _graph = graph;
156 
157  // generate the set of all changes
158  _createChanges();
159  }
160 
162  template < typename STRUCT_CONSTRAINT >
164  STRUCT_CONSTRAINT >::clearChanges() noexcept {
165  _legal_changes.clear();
166  }
167 
169  template < typename STRUCT_CONSTRAINT >
172  return _legal_changes.cbegin();
173  }
174 
176  template < typename STRUCT_CONSTRAINT >
177  INLINE const typename GraphChangesGenerator4UndiGraph<
178  STRUCT_CONSTRAINT >::iterator&
180  return _legal_changes.cend();
181  }
182 
184  template < typename STRUCT_CONSTRAINT >
186  const EdgeAddition& change) {}
187 
189  template < typename STRUCT_CONSTRAINT >
191  const EdgeDeletion& change) {}
192 
194  template < typename STRUCT_CONSTRAINT >
196  const GraphChange& change) {}
197 
199  template < typename STRUCT_CONSTRAINT >
200  INLINE void
202  if (_legal_changes.size()) _legal_changes.clear();
203  }
204 
206  template < typename STRUCT_CONSTRAINT >
207  INLINE void
209  Size nb) noexcept {
210 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
211  if (nb == 0) nb = getMaxNumberOfThreads();
213 # else
215 # endif /* _OPENMP && GUM_DEBUG_MODE */
216  }
217 
219  template < typename STRUCT_CONSTRAINT >
220  INLINE STRUCT_CONSTRAINT&
222  noexcept {
223  return *_constraint;
224  }
225 
226  } /* namespace learning */
227 
228 } /* namespace gum */
229 
230 #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
gum is the global namespace for all aGrUM entities
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:45
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