aGrUM  0.14.2
graphChangesSelector4DiGraph.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}@lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it wil be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef GUM_LEARNING_GRAPH_CHANGES_SELECTOR_4_DIGRAPH_H
27 #define GUM_LEARNING_GRAPH_CHANGES_SELECTOR_4_DIGRAPH_H
28 
29 #include <vector>
30 
31 #include <agrum/agrum.h>
33 #include <agrum/core/sequence.h>
34 #include <agrum/core/set.h>
35 #include <agrum/graphs/diGraph.h>
38 
39 namespace gum {
40 
41  namespace learning {
42 
48  template < typename STRUCTURAL_CONSTRAINT,
49  typename GRAPH_CHANGES_GENERATOR,
50  template < typename > class ALLOC = std::allocator >
52  public:
54  using GeneratorType = GRAPH_CHANGES_GENERATOR;
55 
56  // ##########################################################################
58  // ##########################################################################
60 
63  STRUCTURAL_CONSTRAINT& constraint,
64  GRAPH_CHANGES_GENERATOR& changes_generator);
65 
68  const GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT,
69  GRAPH_CHANGES_GENERATOR,
70  ALLOC >& from);
71 
74  GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT,
75  GRAPH_CHANGES_GENERATOR,
76  ALLOC >&& from);
77 
80 
82 
83  // ##########################################################################
85  // ##########################################################################
87 
89  GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT,
90  GRAPH_CHANGES_GENERATOR,
91  ALLOC >&
92  operator=(const GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT,
93  GRAPH_CHANGES_GENERATOR,
94  ALLOC >& from);
95 
97  GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT,
98  GRAPH_CHANGES_GENERATOR,
99  ALLOC >&
100  operator=(GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT,
101  GRAPH_CHANGES_GENERATOR,
102  ALLOC >&& from);
103 
105 
106  // ##########################################################################
108  // ##########################################################################
110 
112  GeneratorType& graphChangeGenerator() const noexcept;
113 
115  bool empty();
116 
120  bool empty(const NodeId i);
121 
123 
124  const GraphChange& bestChange();
125 
127 
132  const GraphChange& bestChange(const NodeId i);
133 
135 
136  double bestScore();
137 
139 
144  double bestScore(const NodeId i);
145 
147  void applyChange(const GraphChange& change);
148 
150 
156  void applyChangeWithoutScoreUpdate(const GraphChange& change);
157 
159 
164 
166  bool isChangeValid(const GraphChange& change) const;
167 
169  void setGraph(DiGraph& graph);
170 
172  std::vector< std::pair< NodeId, double > > nodesSortedByBestScore() const;
173 
175  std::vector< std::pair< NodeId, double > > nodesUnsortedWithScore() const;
176 
178 
179  private:
181  Score< ALLOC >* __score;
182 
184  STRUCTURAL_CONSTRAINT* __constraint;
185 
187  GRAPH_CHANGES_GENERATOR* __changes_generator;
188 
191 
193 
194  std::vector< std::pair< double, double > > __change_scores;
195 
197 
199  NodeProperty< PriorityQueue< std::size_t, double, std::greater< double > > >
201 
203  PriorityQueue< NodeId, double, std::greater< double > > __node_queue;
204 
206 
209 
212 
214  NodeProperty< std::vector< NodeId, ALLOC< NodeId > > > __parents;
215 
217  bool __queues_valid{false};
218 
221 
223  bool __isChangeValid(const std::size_t index) const;
224 
226  void __invalidateChange(const std::size_t change_index);
227 
229  void __illegal2LegalChanges(Set< std::size_t >& changes_to_recompute);
230 
232  void
234  const NodeId target_node);
235 
237  void __updateScores(const Set< std::size_t >& changes_to_recompute);
238 
240  void __getNewChanges();
241  };
242 
243  } /* namespace learning */
244 
245 } /* namespace gum */
246 
249 
250 #endif /* GUM_LEARNING_GRAPH_CHANGES_SELECTOR_4_DIGRAPH_H */
Base classes for oriented graphs.
NodeProperty< double > __node_current_scores
the current score of each node
Header file of gum::Sequence, a class for storing (ordered) sequences of objects. ...
bool empty()
indicates whether the selector still contains graph changes
The base class for all the scores used for learning (BIC, BDeu, etc)
Definition: score.h:49
Sets of elements (i.e.
GeneratorType & graphChangeGenerator() const noexcept
returns the generator used by the selector
the classes to account for structure changes in a graph
void __updateScores(const Set< std::size_t > &changes_to_recompute)
perform the necessary updates of the scores
STRUCTURAL_CONSTRAINT * __constraint
the set of constraints used to determine valid changes
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR, ALLOC > & operator=(const GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR, ALLOC > &from)
copy operator
const GraphChange & bestChange()
returns the best graph change to examine
STL namespace.
void applyChange(const GraphChange &change)
indicate to the selector that a change has been applied
Set< std::size_t > __illegal_changes
the set of changes known to be currently illegal (due to the constraints)
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
PriorityQueue< NodeId, double, std::greater< double > > __node_queue
a global priority queue indicating for each node its best score
The class for generic Hash Tables.
Definition: hashTable.h:676
The mecanism to compute the next available graph changes for directed structure learning search algor...
std::vector< std::pair< double, double > > __change_scores
the scores for the head and tail of all the changes
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
The mecanism to compute the next available graph changes for directed structure learning search algor...
void applyChangeWithoutScoreUpdate(const GraphChange &change)
indicate to the selector that one of serveral changes has been applied
A priorityQueue is a heap in which each element has a mutable priorityA priority queue is quite simil...
Definition: priorityQueue.h:48
bool __isChangeValid(const std::size_t index) const
indicates whether a given change is valid or not
NodeProperty< std::vector< NodeId, ALLOC< NodeId > > > __parents
the set of parents of each node (speeds-up score computations)
Base class for all oriented graphs.
Definition: diGraph.h:108
Set< NodeId > __queues_to_update
the set of queues to update when applying several changes
double bestScore()
return the score of the best graph change
priority queues (in which an element cannot appear more than once)
bool __queues_valid
indicates whether we need to recompute whether the queue is empty or not
the base class for all the scores used for learning (BIC, BDeu, etc)
GraphChangesSelector4DiGraph(Score< ALLOC > &score, STRUCTURAL_CONSTRAINT &constraint, GRAPH_CHANGES_GENERATOR &changes_generator)
default constructor
void updateScoresAfterAppliedChanges()
recompute all the scores after the application of several changes
std::vector< std::pair< NodeId, double > > nodesUnsortedWithScore() const
returns the set of queues top priorities
Sequence< GraphChange > __changes
a sequence containing all the possible changes
std::vector< std::pair< NodeId, double > > nodesSortedByBestScore() const
returns the set of queues sorted by decreasing top priority
void __invalidateChange(const std::size_t change_index)
put a change into the illegal set
void __findLegalChangesNeedingUpdate(Set< std::size_t > &changes_to_recompute, const NodeId target_node)
finds the changes that are affected by a given node modification
void __illegal2LegalChanges(Set< std::size_t > &changes_to_recompute)
remove the now legal changes from the illegal set
void setGraph(DiGraph &graph)
sets the graph from which scores are computed
GRAPH_CHANGES_GENERATOR GeneratorType
the type of the generator
GRAPH_CHANGES_GENERATOR * __changes_generator
the generator that returns the set of possible changes
void __getNewChanges()
get from the graph change generator a new set of changes
bool isChangeValid(const GraphChange &change) const
indicates whether a given change is valid or not
Size NodeId
Type for node ids.
Definition: graphElements.h:97
NodeProperty< PriorityQueue< std::size_t, double, std::greater< double > > > __change_queue_per_node
for each node, a priority queue sorting GraphChanges by decreasing score