aGrUM  0.14.2
graphChangesGenerator4DiGraph_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  GraphChangesGenerator4DiGraph(STRUCT_CONSTRAINT& constraint) :
36  _constraint(&constraint) {
37  GUM_CONSTRUCTOR(GraphChangesGenerator4DiGraph);
38  }
39 
41  template < typename STRUCT_CONSTRAINT >
44  _graph(from._graph),
47  GUM_CONS_CPY(GraphChangesGenerator4DiGraph);
48  }
49 
51  template < typename STRUCT_CONSTRAINT >
54  _graph(std::move(from._graph)),
56  _legal_changes(std::move(from._legal_changes)),
58  GUM_CONS_MOV(GraphChangesGenerator4DiGraph);
59  }
60 
62  template < typename STRUCT_CONSTRAINT >
64  STRUCT_CONSTRAINT >::~GraphChangesGenerator4DiGraph() {
65  GUM_DESTRUCTOR(GraphChangesGenerator4DiGraph);
66  }
67 
69  template < typename STRUCT_CONSTRAINT >
70  GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
72  const GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >& from) {
73  if (this != &from) {
74  _graph = from._graph;
75  _constraint = from._constraint;
76  _legal_changes = from._legal_changes;
77  __max_threads_number = from.__max_threads_number;
78  }
79  return *this;
80  }
81 
83  template < typename STRUCT_CONSTRAINT >
84  GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
86  operator=(GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&& from) {
87  if (this != &from) {
88  _graph = std::move(from._graph);
89  _constraint = std::move(from._constraint);
90  _legal_changes = std::move(from._legal_changes);
91  __max_threads_number = from.__max_threads_number;
92  }
93  return *this;
94  }
95 
97  template < typename STRUCT_CONSTRAINT >
99  _legal_changes.clear();
100 
101  // for all the pairs of nodes, consider adding, reverse and removing arcs
102  std::vector< Set< GraphChange > > legal_changes;
103 # pragma omp parallel num_threads(int(__max_threads_number))
104  {
105  int num_threads = getNumberOfRunningThreads();
106 
107 # pragma omp single
108  {
109  // resize the change vectors so that each thread can write to its
110  // own vector
111  legal_changes.resize(num_threads);
112  }
113 
114  const Size this_thread = getThreadNumber();
115 
116  Idx i = 0;
117  for (const auto node1 : _graph) {
118  if (i == this_thread) {
119  for (const auto node2 : _graph) {
120  if (node1 != node2) {
121  // try arc additions
122  ArcAddition arc_add(node1, node2);
123  if (!_constraint->isAlwaysInvalid(arc_add)) {
124  legal_changes[this_thread].insert(std::move(arc_add));
125  }
126 
127  // try arc deletion
128  ArcDeletion arc_del(node1, node2);
129  if (!_constraint->isAlwaysInvalid(arc_del)) {
130  legal_changes[this_thread].insert(std::move(arc_del));
131  }
132 
133  // try arc reversal
134  ArcReversal arc_rev(node1, node2);
135  if (!_constraint->isAlwaysInvalid(arc_rev)) {
136  legal_changes[this_thread].insert(std::move(arc_rev));
137  }
138  }
139  }
140  }
141  ++i;
142  i %= num_threads;
143  }
144  }
145 
146  // now store the changes into the protected vectors of the
147  // GraphChangesGenerator4DiGraph
148  for (const auto& changes : legal_changes) {
149  for (const auto& change : changes) {
150  _legal_changes.insert(std::move(change));
151  }
152  }
153  }
154 
156  template < typename STRUCT_CONSTRAINT >
158  const DiGraph& graph) {
159  // sets the current graph
160  _graph = graph;
161 
162  // generate the set of all changes
163  _createChanges();
164  }
165 
167  template < typename STRUCT_CONSTRAINT >
168  INLINE void GraphChangesGenerator4DiGraph<
169  STRUCT_CONSTRAINT >::clearChanges() noexcept {
170  _legal_changes.clear();
171  }
172 
174  template < typename STRUCT_CONSTRAINT >
177  return _legal_changes.cbegin();
178  }
179 
181  template < typename STRUCT_CONSTRAINT >
182  INLINE const typename GraphChangesGenerator4DiGraph<
183  STRUCT_CONSTRAINT >::iterator&
185  return _legal_changes.cend();
186  }
187 
189  template < typename STRUCT_CONSTRAINT >
191  const ArcAddition& change) {}
192 
194  template < typename STRUCT_CONSTRAINT >
196  const ArcDeletion& change) {}
197 
199  template < typename STRUCT_CONSTRAINT >
201  const ArcReversal& change) {}
202 
204  template < typename STRUCT_CONSTRAINT >
206  const GraphChange& change) {}
207 
209  template < typename STRUCT_CONSTRAINT >
210  INLINE void
212  if (_legal_changes.size()) _legal_changes.clear();
213  }
214 
216  template < typename STRUCT_CONSTRAINT >
217  INLINE void
219  Size nb) noexcept {
220 # if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
221  if (nb == 0) nb = getMaxNumberOfThreads();
223 # else
225 # endif /* _OPENMP && GUM_DEBUG_MODE */
226  }
227 
229  template < typename STRUCT_CONSTRAINT >
230  INLINE STRUCT_CONSTRAINT&
232  noexcept {
233  return *_constraint;
234  }
235 
236  } /* namespace learning */
237 
238 } /* namespace gum */
239 
240 #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
gum is the global namespace for all aGrUM entities
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:45