aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
graphChangesGenerator4K2.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 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_K2_H
61 #define GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_K2_H
62 
63 #include <agrum/agrum.h>
64 #include <agrum/tools/core/OMPThreads.h>
65 #include <agrum/tools/core/sequence.h>
66 #include <agrum/tools/core/set.h>
67 
68 #include <agrum/tools/graphs/diGraph.h>
69 #include <agrum/BN/learning/structureUtils/IGraphChangesGenerator4DiGraph.h>
70 #include <agrum/BN/learning/structureUtils/graphChange.h>
71 
72 namespace gum {
73 
74  namespace learning {
75 
76  // a dummy class used to check that the generator is adapted to K2
78 
79  /** @class GraphChangesGenerator4K2
80  * @brief The basic class for computing the next graph changes possible in a
81  * structure learning algorithm
82  *
83  * Structure learning algorithm try different modifications of the graph.
84  *Class
85  * GraphChangesGenerator4K2 provides a simple way to compute those that
86  * we wish to perform. For instance, in the basic LocalSearch algorithm for
87  * learning directed graphs, one may expect that all possible arc additions,
88  * deletions and reversals can be applied and GraphChangesGenerator4K2
89  * provides exactly this set of operations. However, there may be cases
90  *where
91  * we would like to apply these operators, say, only on a subgraph. In this
92  * case, we should use the derived class of GraphChangesGenerator4K2 named
93  * GraphChangesGeneratorOnSubDiGraph. Anyway, all the search generators
94  * should have the following minimal methods:
95  * - void setGraph ( const DiGraph& ) : assigns a new graph as a starting
96  * point to the generator of graph change operators
97  * - void modifyGraph ( const GraphChange& ) : indicate to the generator
98  * that the graph has been changed and that we need to compute the new
99  * operators that result from this change
100  * - void clearChanges () : empty the set of possible changes
101  * - methods begin () and end () that return iterators allowing to parse
102  *the
103  * available set of changes.
104  *
105  * Basically, the idea is to use method setGraph at the beginning of the
106  * structure learning in order to initialize the possible set of operators.
107  * Then, parse this set using a for ( auto iter = generator.begin ();
108  * iter != generator.end (); ++iter ) loop and compute the scores
109  * induced by these changes. When this is done, flush the generator by
110  * calling method clearChanges. Then iterate changes and after each new
111  *change
112  * applied, used again the iterator for loop, and so on. Note that, whenever
113  * you execute method modifyGraph, this will automatically flush the current
114  * list of changes and put into the list only the changes that are affected
115  * by the graph modification.
116  *
117  * @ingroup learning_group
118  */
119  template < typename STRUCT_CONSTRAINT >
123  public:
124  /// the iterator for parsing the list of possible graph change operators
125  using iterator = typename Set< GraphChange >::const_iterator;
126 
127  /// the const iterator for parsing the list of graph change operators
129 
130  // ##########################################################################
131  /// @name Constructors / Destructors
132  // ##########################################################################
133  /// @{
134 
135  /// default constructor
136  GraphChangesGenerator4K2(STRUCT_CONSTRAINT& constraint);
137 
138  /// copy constructor
139  GraphChangesGenerator4K2(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT >& from);
140 
141  /// move operator
142  GraphChangesGenerator4K2(GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&& from);
143 
144  /// destructor
145  virtual ~GraphChangesGenerator4K2();
146 
147  /// @}
148 
149  // ##########################################################################
150  /// @name Operators
151  // ##########################################################################
152  /// @{
153 
154  /// copy operator
155  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
156  operator=(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT >& from);
157 
158  /// move operator
159  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
160  operator=(GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&& from);
161 
162  /// @}
163 
164  // ##########################################################################
165  /// @name Iterators
166  // ##########################################################################
167  /// @{
168 
169  /// returns an (unsafe) iterator on the beginning of the list of operators
170  iterator begin() const;
171 
172  /// returns an (unsafe) iterator on the end of the list of operators
173  const iterator& end() const;
174 
175  /// @}
176 
177  // ##########################################################################
178  /// @name Accessors / Modifiers
179  // ##########################################################################
180  /// @{
181 
182  /// returns the constraint that is used by the generator
183  STRUCT_CONSTRAINT& constraint() const noexcept;
184 
185  /// sets a new graph from which the generator will compute possible
186  /// changes
187  void setGraph(const DiGraph& graph);
188 
189  /// notify the generator of a change applied to the graph
190  void modifyGraph(const ArcAddition& change);
191 
192  /// notify the generator of a change applied to the graph
193  void modifyGraph(const ArcDeletion& change);
194 
195  /// notify the generator of a change applied to the graph
196  void modifyGraph(const ArcReversal& change);
197 
198  /// notify the generator of a change applied to the graph
199  void modifyGraph(const GraphChange& change);
200 
201  /// empty the set of possible change operators that can be applied
202  void clearChanges() noexcept;
203 
204  /// notifies the generator that we have parsed all its legal changes
205  void notifyGetCompleted();
206 
207  /// sets the maximum number of threads used to compute the set of changes
208  void setMaxNbThreads(Size nb) noexcept;
209 
210  /// set a new order on the random variables
211  void setOrder(const Sequence< NodeId >& order);
212 
213  /// set a new order on the random variables
214  void setOrder(const std::vector< NodeId >& order);
215 
216  /// @}
217 
218  protected:
219  /// the graph on which we generate operators
221 
222  /// the structural constraint used to restrict the changes
223  STRUCT_CONSTRAINT* constraint_;
224 
225  /// the order on the variables
227 
228  /// the current set of graph changes
230 
231  /// create the set of legal and illegal changes from a given graph
232  void createChanges_();
233 
234  private:
235 /// the max number of threads authorized
236 #if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
238 #else
240 #endif /* GUM_DEBUG_MODE */
241  };
242 
243  } /* namespace learning */
244 
245 } /* namespace gum */
246 
247 /// always include the templated functions
248 #include <agrum/BN/learning/structureUtils/graphChangesGenerator4K2_tpl.h>
249 
250 #endif /* GUM_LEARNING_GRAPH_CHANGES_GENERATOR_4_K2_H */
GraphChangesGenerator4K2(GraphChangesGenerator4K2< STRUCT_CONSTRAINT > &&from)
move operator
GraphChangesGenerator4K2(STRUCT_CONSTRAINT &constraint)
default constructor
STRUCT_CONSTRAINT * constraint_
the structural constraint used to restrict the changes
void notifyGetCompleted()
notifies the generator that we have parsed all its legal changes
const iterator & end() const
returns an (unsafe) iterator on the end of the list of operators
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
iterator begin() const
returns an (unsafe) iterator on the beginning of the list of operators
void setGraph(const DiGraph &graph)
sets a new graph from which the generator will compute possible changes
GraphChangesGenerator4K2(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT > &from)
copy constructor
STRUCT_CONSTRAINT & constraint() const noexcept
returns the constraint that is used by the generator
Sequence< NodeId > order_
the order on the variables
void setMaxNbThreads(Size nb) noexcept
sets the maximum number of threads used to compute the set of changes
GraphChangesGenerator4K2< STRUCT_CONSTRAINT > & operator=(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT > &from)
copy operator
Size _max_threads_number_
the max number of threads authorized
void modifyGraph(const GraphChange &change)
notify the generator of a change applied to the graph
GraphChangesGenerator4K2< STRUCT_CONSTRAINT > & operator=(GraphChangesGenerator4K2< STRUCT_CONSTRAINT > &&from)
move operator
Set< GraphChange > legal_changes_
the current set of graph changes
virtual ~GraphChangesGenerator4K2()
destructor
void clearChanges() noexcept
empty the set of possible change operators that can be applied
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
The basic class for computing the next graph changes possible in a structure learning algorithm...
void setOrder(const std::vector< NodeId > &order)
set a new order on the random variables
void createChanges_()
create the set of legal and illegal changes from a given graph
DiGraph graph_
the graph on which we generate operators