aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphChangesGenerator4K2.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_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
140  const GraphChangesGenerator4K2< STRUCT_CONSTRAINT >& from);
141 
142  /// move operator
144  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&& from);
145 
146  /// destructor
147  virtual ~GraphChangesGenerator4K2();
148 
149  /// @}
150 
151  // ##########################################################################
152  /// @name Operators
153  // ##########################################################################
154  /// @{
155 
156  /// copy operator
157  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
158  operator=(const GraphChangesGenerator4K2< STRUCT_CONSTRAINT >& from);
159 
160  /// move operator
161  GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&
162  operator=(GraphChangesGenerator4K2< STRUCT_CONSTRAINT >&& from);
163 
164  /// @}
165 
166  // ##########################################################################
167  /// @name Iterators
168  // ##########################################################################
169  /// @{
170 
171  /// returns an (unsafe) iterator on the beginning of the list of operators
172  iterator begin() const;
173 
174  /// returns an (unsafe) iterator on the end of the list of operators
175  const iterator& end() const;
176 
177  /// @}
178 
179  // ##########################################################################
180  /// @name Accessors / Modifiers
181  // ##########################################################################
182  /// @{
183 
184  /// returns the constraint that is used by the generator
185  STRUCT_CONSTRAINT& constraint() const noexcept;
186 
187  /// sets a new graph from which the generator will compute possible
188  /// changes
189  void setGraph(const DiGraph& graph);
190 
191  /// notify the generator of a change applied to the graph
192  void modifyGraph(const ArcAddition& change);
193 
194  /// notify the generator of a change applied to the graph
195  void modifyGraph(const ArcDeletion& change);
196 
197  /// notify the generator of a change applied to the graph
198  void modifyGraph(const ArcReversal& change);
199 
200  /// notify the generator of a change applied to the graph
201  void modifyGraph(const GraphChange& change);
202 
203  /// empty the set of possible change operators that can be applied
204  void clearChanges() noexcept;
205 
206  /// notifies the generator that we have parsed all its legal changes
207  void notifyGetCompleted();
208 
209  /// sets the maximum number of threads used to compute the set of changes
210  void setMaxNbThreads(Size nb) noexcept;
211 
212  /// set a new order on the random variables
213  void setOrder(const Sequence< NodeId >& order);
214 
215  /// set a new order on the random variables
216  void setOrder(const std::vector< NodeId >& order);
217 
218  /// @}
219 
220  protected:
221  /// the graph on which we generate operators
223 
224  /// the structural constraint used to restrict the changes
225  STRUCT_CONSTRAINT* constraint_;
226 
227  /// the order on the variables
229 
230  /// the current set of graph changes
232 
233  /// create the set of legal and illegal changes from a given graph
234  void createChanges_();
235 
236  private:
237 /// the max number of threads authorized
238 #if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
240 #else
242 #endif /* GUM_DEBUG_MODE */
243  };
244 
245  } /* namespace learning */
246 
247 } /* namespace gum */
248 
249 /// always include the templated functions
250 #include <agrum/BN/learning/structureUtils/graphChangesGenerator4K2_tpl.h>
251 
252 #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:669
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
Size max_threads_number__
the max number of threads authorized
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
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