aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
graphChangesGenerator4UndiGraph.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 undigraph 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  * GraphChangesGenerator4UndiGraph provides a simple way to compute those that
28  *we
29  * wish to perform. For instance, in the basic PC algorithm for learning
30  * undirected graphs, one may expect that all possible edge additions and
31  * deletions can be applied and GraphChangesGenerator4UndiGraph provides exactly
32  * this set of operations. This class provides the following minimal methods:
33  * - void setGraph ( const UndiGraph& ) : assigns a new graph as a starting
34  * point to the generator of graph change operators
35  * - void modifyGraph ( const GraphChange& ) : indicate to the operator set
36  * that the graph has been changed and that we need to compute the new
37  * operators that result from this change
38  * - void clearChanges () : empty the set of possible operators
39  * - methods begin () and end () that return iterators allowing to parse the
40  * available set of operators.
41  *
42  * Basically, the idea is to use method setGraph at the beginning of the
43  * structure learning in order to initialize the possible set of operators.
44  * Then, parse this set using a for ( auto iter = operator_set.begin ();
45  * iter != operator_set.end (); ++iter ) loop and compute the scores
46  * induced by these changes. When this is done, flush the operator set by
47  * calling method clearChanges. Then iterate changes and after each new change
48  * applied, used again the iterator for loop, and so on. Note that, whenever
49  * you execute method modifyGraph, this will automatically flush the current
50  * list of changes and put into the list only the changes that are affected
51  * by the graph modification.
52  *
53  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
54  */
55 #ifndef GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_UNDIGRAPH_H
56 #define GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_UNDIGRAPH_H
57 
58 #include <agrum/agrum.h>
59 #include <agrum/tools/core/OMPThreads.h>
60 #include <agrum/tools/core/set.h>
61 #include <agrum/tools/graphs/undiGraph.h>
62 #include <agrum/BN/learning/structureUtils/IGraphChangesGenerator4UndiGraph.h>
63 #include <agrum/BN/learning/structureUtils/graphChange.h>
64 
65 namespace gum {
66 
67  namespace learning {
68 
69  /** @class GraphChangesGenerator4UndiGraph
70  * @brief The basic class for computing the next graph changes possible in
71  *an
72  * undirected structure learning algorithm
73  *
74  * Structure learning algorithm try different modifications of the graph.
75  *Class
76  * GraphChangesGenerator4UndiGraph provides a simple way to compute those
77  *that
78  * we wish to perform. For instance, in the basic PC algorithm for learning
79  * undirected graphs, one may expect that all possible edge additions and
80  * deletions can be applied and GraphChangesGenerator4UndiGraph provides
81  * exactly this set of operations. This class provides the following minimal
82  * methods:
83  * - void setGraph ( const UndiGraph& ) : assigns a new graph as a
84  *starting
85  * point to the generator of graph change operators
86  * - void modifyGraph ( const GraphChange& ) : indicate to the operator
87  *set
88  * that the graph has been changed and that we need to compute the new
89  * operators that result from this change
90  * - void clearChanges () : empty the set of possible operators
91  * - methods begin () and end () that return iterators allowing to parse
92  *the
93  * available set of operators.
94  *
95  * Basically, the idea is to use method setGraph at the beginning of the
96  * structure learning in order to initialize the possible set of operators.
97  * Then, parse this set using a for ( auto iter = operator_set.begin ();
98  * iter != operator_set.end (); ++iter ) loop and compute the scores
99  * induced by these changes. When this is done, flush the operator set by
100  * calling method clearChanges. Then iterate changes and after each new
101  *change
102  * applied, used again the iterator for loop, and so on. Note that, whenever
103  * you execute method modifyGraph, this will automatically flush the current
104  * list of changes and put into the list only the changes that are affected
105  * by the graph modification.
106  *
107  * @ingroup learning_group
108  */
109  template < typename STRUCT_CONSTRAINT >
111  public:
112  /// the iterator for parsing the list of possible graph change operators
113  using iterator = typename Set< GraphChange >::const_iterator;
114 
115  /// the const iterator for parsing the list of graph change operators
117 
118  // ##########################################################################
119  /// @name Constructors / Destructors
120  // ##########################################################################
121  /// @{
122 
123  /// default constructor
124  GraphChangesGenerator4UndiGraph(STRUCT_CONSTRAINT& constraint);
125 
126  /// copy constructor
128  const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& from);
129 
130  /// move operator
132 
133  /// destructor
135 
136  /// @}
137 
138  // ##########################################################################
139  /// @name Operators
140  // ##########################################################################
141  /// @{
142 
143  /// copy operator
144  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
145  operator=(const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& from);
146 
147  /// move operator
148  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
149  operator=(GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&& from);
150 
151  /// @}
152 
153  // ##########################################################################
154  /// @name Iterators
155  // ##########################################################################
156  /// @{
157 
158  /// returns an (unsafe) iterator on the beginning of the list of operators
159  iterator begin() const;
160 
161  /// returns an (unsafe) iterator on the end of the list of operators
162  const iterator& end() const;
163 
164  /// @}
165 
166  // ##########################################################################
167  /// @name Accessors / Modifiers
168  // ##########################################################################
169  /// @{
170 
171  /// returns the constraint that is used by the generator
172  STRUCT_CONSTRAINT& constraint() const noexcept;
173 
174  /// sets a new graph from which the operator will compute possible changes
175  void setGraph(const UndiGraph& graph);
176 
177  /// notify the operator set of a change applied to the graph
178  void modifyGraph(const EdgeAddition& change);
179 
180  /// notify the operator set of a change applied to the graph
181  void modifyGraph(const EdgeDeletion& change);
182 
183  /// notify the operator set of a change applied to the graph
184  void modifyGraph(const GraphChange& change);
185 
186  /// empty the set of possible change operators that can be applied
187  void clearChanges() noexcept;
188 
189  /// notifies the generator that we have parsed all its legal changes
190  void notifyGetCompleted();
191 
192  /// sets the maximum number of threads used to compute the set of changes
193  void setMaxNbThreads(Size nb) noexcept;
194 
195  /// @}
196 
197  protected:
198  /// the graph on which we generate operators
200 
201  /// a reference on the structural constraint used to restrict the changes
202  STRUCT_CONSTRAINT* constraint_;
203 
204  /// the current set of operators
206 
207  /// create the set of legal and illegal changes from a given graph
208  void createChanges_();
209 
210  private:
211 /// the max number of threads authorized
212 #if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
214 #else
216 #endif /* GUM_DEBUG_MODE */
217  };
218 
219  } /* namespace learning */
220 
221 } /* namespace gum */
222 
223 /// always include the templated functions
224 #include <agrum/BN/learning/structureUtils/graphChangesGenerator4UndiGraph_tpl.h>
225 
226 #endif /* GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_UNDIGRAPH_H */
STRUCT_CONSTRAINT * constraint_
a reference on the structural constraint used to restrict the changes
The basic class for computing the next graph changes possible in an undirected structure learning alg...
void notifyGetCompleted()
notifies the generator that we have parsed all its legal changes
void setGraph(const UndiGraph &graph)
sets a new graph from which the operator will compute possible changes
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT > & operator=(GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT > &&from)
move operator
iterator begin() const
returns an (unsafe) iterator on the beginning of the list of operators
void createChanges_()
create the set of legal and illegal changes from a given graph
GraphChangesGenerator4UndiGraph(STRUCT_CONSTRAINT &constraint)
default constructor
void clearChanges() noexcept
empty the set of possible change operators that can be applied
GraphChangesGenerator4UndiGraph(GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT > &&from)
move operator
void modifyGraph(const GraphChange &change)
notify the operator set of a change applied to the graph
void setMaxNbThreads(Size nb) noexcept
sets the maximum number of threads used to compute the set of changes
GraphChangesGenerator4UndiGraph(const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT > &from)
copy constructor
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
UndiGraph graph_
the graph on which we generate operators
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
Set< GraphChange > legal_changes_
the current set of operators
const iterator & end() const
returns an (unsafe) iterator on the end of the list of operators