aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphChangesGenerator4DiGraph.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief The basic class for computing the set of digraph changes allowed by
24  * the user to be executed by the learning algorithms
25  *
26  * Structure learning algorithm try different modifications of the graph. Class
27  * GraphChangesGenerator4DiGraph provides a simple way to compute those that we
28  * wish to perform. For instance, in the basic LocalSearch algorithm for
29  *learning
30  * directed graphs, one may expect that all possible arc additions, deletions
31  *and
32  * reversals can be applied and GraphChangesGenerator4DiGraph provides exactly
33  * this set of operations. However, there may be cases where we would like to
34  * apply these operators, say, only on a subgraph. In this case, we should use
35  * the derived class of GraphChangesGenerator4DiGraph named
36  * GraphChangesGeneratorOnSubDiGraph. Anyway, all the search generators should
37  * have the following minimal methods:
38  * - void setGraph ( const DiGraph& ) : assigns a new graph as a starting
39  * point to the generator of graph change operators
40  * - void modifyGraph ( const GraphChange& ) : indicate to the generator
41  * that the graph has been changed and that we need to compute the new
42  * operators that result from this change
43  * - void clearChanges () : empty the set of possible changes
44  * - methods begin () and end () that return iterators allowing to parse the
45  * available set of changes.
46  *
47  * Basically, the idea is to use method setGraph at the beginning of the
48  * structure learning in order to initialize the possible set of changes.
49  * Then, parse this set using a for ( auto iter = generator.begin ();
50  * iter != generator.end (); ++iter ) loop and compute the scores
51  * induced by these changes. When this is done, flush the generator by
52  * calling method clearChanges. Then iterate changes and after each new change
53  * applied, used again the iterator for loop, and so on. Note that, whenever
54  * you execute method modifyGraph, this will automatically flush the current
55  * list of changes and put into the list only the changes that are affected
56  * by the graph modification.
57  *
58  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
59  */
60 #ifndef GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_DIGRAPH_H
61 #define GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_DIGRAPH_H
62 
63 #include <agrum/agrum.h>
64 #include <agrum/tools/core/OMPThreads.h>
65 #include <agrum/tools/core/set.h>
66 #include <agrum/tools/graphs/diGraph.h>
67 #include <agrum/BN/learning/structureUtils/IGraphChangesGenerator4DiGraph.h>
68 #include <agrum/BN/learning/structureUtils/graphChange.h>
69 
70 namespace gum {
71 
72  namespace learning {
73 
74  /** @class GraphChangesGenerator4DiGraph
75  * @brief The basic class for computing the next graph changes possible in a
76  * structure learning algorithm
77  *
78  * Structure learning algorithm try different modifications of the graph.
79  *Class
80  * GraphChangesGenerator4DiGraph provides a simple way to compute those that
81  * we wish to perform. For instance, in the basic LocalSearch algorithm for
82  * learning directed graphs, one may expect that all possible arc additions,
83  * deletions and reversals can be applied and GraphChangesGenerator4DiGraph
84  * provides exactly this set of operations. However, there may be cases
85  *where
86  * we would like to apply these operators, say, only on a subgraph. In this
87  * case, we should use the derived class of GraphChangesGenerator4DiGraph
88  *named
89  * GraphChangesGeneratorOnSubDiGraph. Anyway, all the search generators
90  * should have the following minimal methods:
91  * - void setGraph ( const DiGraph& ) : assigns a new graph as a starting
92  * point to the generator of graph change operators
93  * - void modifyGraph ( const GraphChange& ) : indicate to the generator
94  * that the graph has been changed and that we need to compute the new
95  * operators that result from this change
96  * - void clearChanges () : empty the set of possible changes
97  * - methods begin () and end () that return iterators allowing to parse
98  *the
99  * available set of changes.
100  *
101  * Basically, the idea is to use method setGraph at the beginning of the
102  * structure learning in order to initialize the possible set of changes.
103  * Then, parse this set using a for ( auto iter = generator.begin ();
104  * iter != generator.end (); ++iter ) loop and compute the scores
105  * induced by these changes. When this is done, flush the generator by
106  * calling method clearChanges. Then iterate changes and after each new
107  *change
108  * applied, used again the iterator for loop, and so on. Note that, whenever
109  * you execute method modifyGraph, this will automatically flush the current
110  * list of changes and put into the list only the changes that are affected
111  * by the graph modification.
112  *
113  * @ingroup learning_group
114  */
115  template < typename STRUCT_CONSTRAINT >
117  public:
118  /// the iterator for parsing the list of possible graph change operators
119  using iterator = typename Set< GraphChange >::const_iterator;
120 
121  /// the const iterator for parsing the list of graph change operators
123 
124  // ##########################################################################
125  /// @name Constructors / Destructors
126  // ##########################################################################
127  /// @{
128 
129  /// default constructor
130  GraphChangesGenerator4DiGraph(STRUCT_CONSTRAINT& constraint);
131 
132  /// copy constructor
134  const GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >& from);
135 
136  /// move operator
138  GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&& from);
139 
140  /// destructor
142 
143  /// @}
144 
145  // ##########################################################################
146  /// @name Operators
147  // ##########################################################################
148  /// @{
149 
150  /// copy operator
151  GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
152  operator=(const GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >& from);
153 
154  /// move operator
155  GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&
156  operator=(GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT >&& from);
157 
158  /// @}
159 
160  // ##########################################################################
161  /// @name Iterators
162  // ##########################################################################
163  /// @{
164 
165  /// returns an (unsafe) iterator on the beginning of the list of operators
166  iterator begin() const;
167 
168  /// returns an (unsafe) iterator on the end of the list of operators
169  const iterator& end() const;
170 
171  /// @}
172 
173  // ##########################################################################
174  /// @name Accessors / Modifiers
175  // ##########################################################################
176  /// @{
177 
178  /// returns the constraint that is used by the generator
179  STRUCT_CONSTRAINT& constraint() const noexcept;
180 
181  /// sets a new graph from which the generator will compute possible
182  /// changes
183  void setGraph(const DiGraph& graph);
184 
185  /// notify the generator of a change applied to the graph
186  void modifyGraph(const ArcAddition& change);
187 
188  /// notify the generator of a change applied to the graph
189  void modifyGraph(const ArcDeletion& change);
190 
191  /// notify the generator of a change applied to the graph
192  void modifyGraph(const ArcReversal& change);
193 
194  /// notify the generator of a change applied to the graph
195  void modifyGraph(const GraphChange& change);
196 
197  /// empty the set of possible change operators that can be applied
198  void clearChanges() noexcept;
199 
200  /// notifies the generator that we have parsed all its legal changes
201  void notifyGetCompleted();
202 
203  /// sets the maximum number of threads used to compute the set of changes
204  void setMaxNbThreads(Size nb) noexcept;
205 
206  /// @}
207 
208  protected:
209  /// the graph on which we generate operators
211 
212  /// the structural constraint used to restrict the changes
213  STRUCT_CONSTRAINT* constraint_;
214 
215  /// the current set of graph changes
217 
218  /// create the set of legal and illegal changes from a given graph
219  void createChanges_();
220 
221  private:
222 /// the max number of threads authorized
223 #if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
225 #else
227 #endif /* GUM_DEBUG_MODE */
228  };
229 
230  } /* namespace learning */
231 
232 } /* namespace gum */
233 
234 /// always include the templated functions
235 #include <agrum/BN/learning/structureUtils/graphChangesGenerator4DiGraph_tpl.h>
236 
237 #endif /* GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_DIGRAPH_H */
STRUCT_CONSTRAINT * constraint_
the structural constraint used to restrict the changes
void modifyGraph(const GraphChange &change)
notify the generator of a change applied to the graph
GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT > & operator=(GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT > &&from)
move operator
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void createChanges_()
create the set of legal and illegal changes from a given graph
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
DiGraph graph_
the graph on which we generate operators
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
virtual ~GraphChangesGenerator4DiGraph()
destructor
The basic class for computing the next graph changes possible in a structure learning algorithm...
STRUCT_CONSTRAINT & constraint() const noexcept
returns the constraint that is used by the generator
GraphChangesGenerator4DiGraph(const GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT > &from)
copy constructor
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
Set< GraphChange > legal_changes_
the current set of graph changes
void notifyGetCompleted()
notifies the generator that we have parsed all its legal changes
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Size max_threads_number__
the max number of threads authorized
GraphChangesGenerator4DiGraph(GraphChangesGenerator4DiGraph< STRUCT_CONSTRAINT > &&from)
move operator