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