aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphChangesGeneratorOnSubDiGraph.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 class for computing the set of graph changes (over a subgraph)
24  * transmitted to learning algorithms
25  *
26  * Structure learning algorithm try different modifications of the graph. Class
27  * GraphChangesGeneratorOnSubDiGraph provides a simple way to compute those that
28  * we wish to perform when we wish to limit the set of changes over a subgraph
29  *of
30  * the graph we learn. For instance, we may wish to relearn an already partially
31  * learnt structure just around nodes A,B,C. In this case, we can set the set of
32  * target nodes of the GraphChangesGeneratorOnSubDiGraph to {A,B,C} and the set
33  * of starting nodes as all the nodes of the graph. In this case, only the arcs
34  * whose heads are A, B or C will be trnasmitted to the learning algorithm.
35  *
36  * Basically, the idea is to use method setGraph at the beginning of the
37  * structure learning in order to initialize the possible set of operators.
38  * Then, parse this set using a for ( auto iter = operator_set.begin ();
39  * iter != operator_set.end (); ++iter ) loop and compute the scores
40  * induced by these changes. When this is done, flush the operator set by
41  * calling method clearChanges. Then iterate changes and after each new change
42  * applied, used again the iterator for loop, and so on. Note that, whenever
43  * you execute method modifyGraph, this will automatically flush the current
44  * list of changes and put into the list only the changes that are affected
45  * by the graph modification.
46  *
47  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
48  */
49 #ifndef GUM_LEARNING_GRAPH_CHANGES_GENERATOR_ON_SUBDIGRAPH_H
50 #define GUM_LEARNING_GRAPH_CHANGES_GENERATOR_ON_SUBDIGRAPH_H
51 
52 #include <agrum/agrum.h>
53 #include <agrum/tools/core/OMPThreads.h>
54 #include <agrum/tools/core/set.h>
55 #include <agrum/tools/graphs/diGraph.h>
56 #include <agrum/BN/learning/structureUtils/IGraphChangesGenerator4DiGraph.h>
57 #include <agrum/BN/learning/structureUtils/graphChange.h>
58 
59 namespace gum {
60 
61  namespace learning {
62 
63  /** @class GraphChangesGeneratorOnSubDiGraph
64  * @brief The basic class for computing the next graph changes possible in a
65  * structure learning algorithm
66  *
67  * Structure learning algorithm try different modifications of the graph.
68  *Class
69  * GraphChangesGeneratorOnSubDiGraph provides a simple way to compute those
70  * that we wish to perform when we wish to limit the set of changes over a
71  * subgraph of the graph we learn. For instance, we may wish to relearn an
72  * already partially learnt structure just around nodes A,B,C. In this case,
73  * we can set the set of target nodes of the
74  *GraphChangesGeneratorOnSubDiGraph
75  * to {A,B,C} and the set of "tail" nodes as all the nodes of the graph. In
76  * this case, only the arcs whose heads are A, B or C will be trnasmitted to
77  * the learning algorithm.
78  *
79  * Basically, the idea is to use method setGraph at the beginning of the
80  * structure learning in order to initialize the possible set of operators.
81  * Then, parse this set using a for ( auto iter = operator_set.begin ();
82  * iter != operator_set.end (); ++iter ) loop and compute the scores
83  * induced by these changes. When this is done, flush the operator set by
84  * calling method clearChanges. Then iterate changes and after each new
85  *change
86  * applied, used again the iterator for loop, and so on. Note that, whenever
87  * you execute method modifyGraph, this will automatically flush the current
88  * list of changes and put into the list only the changes that are affected
89  * by the graph modification.
90  *
91  * @ingroup learning_group
92  */
93  template < typename STRUCT_CONSTRAINT >
96  public:
97  /// the iterator for parsing the list of possible graph change operators
98  using iterator = typename Set< GraphChange >::const_iterator;
99 
100  /// the const iterator for parsing the list of graph change operators
102 
103  // ##########################################################################
104  /// @name Constructors / Destructors
105  // ##########################################################################
106  /// @{
107 
108  /// default constructor
109  GraphChangesGeneratorOnSubDiGraph(STRUCT_CONSTRAINT& constraint);
110 
111  /// copy constructor
113  const GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >& from);
114 
115  /// move operator
117  GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >&& from);
118 
119  /// destructor
121 
122  /// @}
123 
124  // ##########################################################################
125  /// @name Operators
126  // ##########################################################################
127  /// @{
128 
129  /// copy operator
130  GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >& operator=(
131  const GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >& from);
132 
133  /// move operator
134  GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >&
135  operator=(GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT >&& from);
136 
137  /// @}
138 
139  // ##########################################################################
140  /// @name Iterators
141  // ##########################################################################
142  /// @{
143 
144  /// returns an (unsafe) iterator on the beginning of the list of operators
145  iterator begin() const;
146 
147  /// returns an (unsafe) iterator on the end of the list of operators
148  const iterator& end() const;
149 
150  /// @}
151 
152  // ##########################################################################
153  /// @name Accessors / Modifiers
154  // ##########################################################################
155  /// @{
156 
157  /// returns the constraint that is used by the generator
158  STRUCT_CONSTRAINT& constraint() const noexcept;
159 
160  /// sets a new graph from which the operator will compute possible changes
161  void setGraph(const DiGraph& graph);
162 
163  /// assign a set of target nodes
164  void setTargets(const NodeSet& nodes);
165 
166  /// adds a new target node
167  void addTarget(NodeId node);
168 
169  /// removes a target
170  void eraseTarget(NodeId node);
171 
172  /// assign a set of "tail" nodes
173  void setTails(const NodeSet& nodes);
174 
175  /// assign a set of "tail" nodes from 0 to nb_nodes - 1
176  void setTails(Size nb_nodes);
177 
178  /// adds a new "tail" node
179  void addTail(NodeId node);
180 
181  /// removes a tail node
182  void eraseTail(NodeId node);
183 
184  /// notify the operator set of a change applied to the graph
185  void modifyGraph(const ArcAddition& change);
186 
187  /// notify the operator set of a change applied to the graph
188  void modifyGraph(const ArcDeletion& change);
189 
190  /// notify the operator set of a change applied to the graph
191  void modifyGraph(const ArcReversal& change);
192 
193  /// notify the operator set of a change applied to the graph
194  void modifyGraph(const GraphChange& change);
195 
196  /// empty the set of possible change operators that can be applied
197  void clearChanges() noexcept;
198 
199  /// notifies the generator that we have parsed all its legal changes
200  void notifyGetCompleted();
201 
202  /// sets the maximum number of threads used to compute the set of changes
203  void setMaxNbThreads(Size nb) noexcept;
204 
205  /// @}
206 
207  protected:
208  /// a reference on the structural constraint used to restrict the changes
209  STRUCT_CONSTRAINT* constraint_;
210 
211  /// the set of target nodes
213 
214  /// the tail nodes (other extremities than the targets)
216 
217  /// the current set of operators
219 
220  /// create the set of legal and illegal changes from a given graph
221  void createChanges_();
222 
223  private:
224 /// the max number of threads authorized
225 #if defined(_OPENMP) && !defined(GUM_DEBUG_MODE)
227 #else
229 #endif /* GUM_DEBUG_MODE */
230  };
231 
232  } /* namespace learning */
233 
234 } /* namespace gum */
235 
236 /// always include the templated functions
237 #include <agrum/BN/learning/structureUtils/graphChangesGeneratorOnSubDiGraph_tpl.h>
238 
239 #endif /* GUM_LEARNING_GRAPH_CHANGES_GENERATOR_ON_SUBDIGRAPH_H */
GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > & operator=(const GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > &from)
copy operator
GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > & operator=(GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > &&from)
move operator
STRUCT_CONSTRAINT & constraint() const noexcept
returns the constraint that is used by the generator
The basic class for computing the next graph changes possible in a structure learning algorithm...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void eraseTarget(NodeId node)
removes a target
GraphChangesGeneratorOnSubDiGraph(STRUCT_CONSTRAINT &constraint)
default constructor
void eraseTail(NodeId node)
removes a tail node
const iterator & end() const
returns an (unsafe) iterator on the end of the list of operators
void setTails(const NodeSet &nodes)
assign a set of "tail" nodes
void clearChanges() noexcept
empty the set of possible change operators that can be applied
Set< GraphChange > legal_changes_
the current set of operators
void setGraph(const DiGraph &graph)
sets a new graph from which the operator will compute possible changes
iterator begin() const
returns an (unsafe) iterator on the beginning of the list of operators
void modifyGraph(const GraphChange &change)
notify the operator set of a change applied to the graph
NodeSet tail_nodes_
the tail nodes (other extremities than the targets)
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 setTails(Size nb_nodes)
assign a set of "tail" nodes from 0 to nb_nodes - 1
void setTargets(const NodeSet &nodes)
assign a set of target nodes
Size max_threads_number__
the max number of threads authorized
void addTarget(NodeId node)
adds a new target node
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
STRUCT_CONSTRAINT * constraint_
a reference on the structural constraint used to restrict the changes
void addTail(NodeId node)
adds a new "tail" node
GraphChangesGeneratorOnSubDiGraph(const GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > &from)
copy constructor
void notifyGetCompleted()
notifies the generator that we have parsed all its legal changes
GraphChangesGeneratorOnSubDiGraph(GraphChangesGeneratorOnSubDiGraph< STRUCT_CONSTRAINT > &&from)
move operator