aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphChangesGenerator4UndiGraph.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 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 >
112  public:
113  /// the iterator for parsing the list of possible graph change operators
114  using iterator = typename Set< GraphChange >::const_iterator;
115 
116  /// the const iterator for parsing the list of graph change operators
118 
119  // ##########################################################################
120  /// @name Constructors / Destructors
121  // ##########################################################################
122  /// @{
123 
124  /// default constructor
125  GraphChangesGenerator4UndiGraph(STRUCT_CONSTRAINT& constraint);
126 
127  /// copy constructor
129  const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& from);
130 
131  /// move operator
133  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&& from);
134 
135  /// destructor
137 
138  /// @}
139 
140  // ##########################################################################
141  /// @name Operators
142  // ##########################################################################
143  /// @{
144 
145  /// copy operator
146  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& operator=(
147  const GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >& from);
148 
149  /// move operator
150  GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&
151  operator=(GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >&& from);
152 
153  /// @}
154 
155  // ##########################################################################
156  /// @name Iterators
157  // ##########################################################################
158  /// @{
159 
160  /// returns an (unsafe) iterator on the beginning of the list of operators
161  iterator begin() const;
162 
163  /// returns an (unsafe) iterator on the end of the list of operators
164  const iterator& end() const;
165 
166  /// @}
167 
168  // ##########################################################################
169  /// @name Accessors / Modifiers
170  // ##########################################################################
171  /// @{
172 
173  /// returns the constraint that is used by the generator
174  STRUCT_CONSTRAINT& constraint() const noexcept;
175 
176  /// sets a new graph from which the operator will compute possible changes
177  void setGraph(const UndiGraph& graph);
178 
179  /// notify the operator set of a change applied to the graph
180  void modifyGraph(const EdgeAddition& change);
181 
182  /// notify the operator set of a change applied to the graph
183  void modifyGraph(const EdgeDeletion& change);
184 
185  /// notify the operator set of a change applied to the graph
186  void modifyGraph(const GraphChange& change);
187 
188  /// empty the set of possible change operators that can be applied
189  void clearChanges() noexcept;
190 
191  /// notifies the generator that we have parsed all its legal changes
192  void notifyGetCompleted();
193 
194  /// sets the maximum number of threads used to compute the set of changes
195  void setMaxNbThreads(Size nb) noexcept;
196 
197  /// @}
198 
199  protected:
200  /// the graph on which we generate operators
202 
203  /// a reference on the structural constraint used to restrict the changes
204  STRUCT_CONSTRAINT* constraint_;
205 
206  /// the current set of operators
208 
209  /// create the set of legal and illegal changes from a given graph
210  void createChanges_();
211 
212  private:
213 /// the max number of threads authorized
214 #if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
216 #else
218 #endif /* GUM_DEBUG_MODE */
219  };
220 
221  } /* namespace learning */
222 
223 } /* namespace gum */
224 
225 /// always include the templated functions
226 #include <agrum/BN/learning/structureUtils/graphChangesGenerator4UndiGraph_tpl.h>
227 
228 #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:669
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
Size max_threads_number__
the max number of threads authorized
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)
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