aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
graphChangesSelector4DiGraph.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48#ifndef GUM_LEARNING_GRAPH_CHANGES_SELECTOR_4_DIGRAPH_H
49#define GUM_LEARNING_GRAPH_CHANGES_SELECTOR_4_DIGRAPH_H
50
51#include <vector>
52
53#include <agrum/agrum.h>
54
56
57namespace gum {
58
59 namespace learning {
60
66 template < typename STRUCTURAL_CONSTRAINT, typename GRAPH_CHANGES_GENERATOR >
68 public:
70 using GeneratorType = GRAPH_CHANGES_GENERATOR;
71
72 // ##########################################################################
74 // ##########################################################################
76
79 STRUCTURAL_CONSTRAINT& constraint,
80 GRAPH_CHANGES_GENERATOR& changes_generator);
81
85 from);
86
90
93
95
96 // ##########################################################################
98 // ##########################################################################
100
103 operator=(const GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT,
104 GRAPH_CHANGES_GENERATOR >& from);
105
109
111
112 // ##########################################################################
114 // ##########################################################################
116
119
121 bool empty();
122
126 bool empty(const NodeId i);
127
129
131
133
139
141
142 double bestScore();
143
145
150 double bestScore(const NodeId i);
151
153 void applyChange(const GraphChange& change);
154
156
163
165
170
172 bool isChangeValid(const GraphChange& change) const;
173
175 void setGraph(DiGraph& graph);
176
178 std::vector< std::pair< NodeId, double > > nodesSortedByBestScore() const;
179
181 std::vector< std::pair< NodeId, double > > nodesUnsortedWithScore() const;
182
184
185 private:
188
190 STRUCTURAL_CONSTRAINT* _constraint_;
191
193 GRAPH_CHANGES_GENERATOR* _changes_generator_;
194
197
199
200 std::vector< std::pair< double, double > > _change_scores_;
201
203
205 NodeProperty< PriorityQueue< std::size_t, double, std::greater< double > > >
207
209 PriorityQueue< NodeId, double, std::greater< double > > _node_queue_;
210
212
215
218
221
223 bool _queues_valid_{false};
224
227
229 bool _isChangeValid_(const std::size_t index) const;
230
232 void _invalidateChange_(const std::size_t change_index);
233
235 void _illegal2LegalChanges_(Set< std::size_t >& changes_to_recompute);
236
239 const NodeId target_node);
240
242 void _updateScores_(const Set< std::size_t >& changes_to_recompute);
243
246 };
247
248 } /* namespace learning */
249
250} /* namespace gum */
251
254
255#endif /* GUM_LEARNING_GRAPH_CHANGES_SELECTOR_4_DIGRAPH_H */
Base class for all oriented graphs.
Definition diGraph.h:130
A priorityQueue is a heap in which each element has a mutable priority.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Representation of a set.
Definition set.h:131
double bestScore()
return the score of the best graph change
bool isChangeValid(const GraphChange &change) const
indicates whether a given change is valid or not
Set< NodeId > _queues_to_update_
the set of queues to update when applying several changes
Sequence< GraphChange > _changes_
a sequence containing all the possible changes
void updateScoresAfterAppliedChanges()
recompute all the scores after the application of several changes
GeneratorType & graphChangeGenerator() const noexcept
returns the generator used by the selector
void setGraph(DiGraph &graph)
sets the graph from which scores are computed
void _illegal2LegalChanges_(Set< std::size_t > &changes_to_recompute)
remove the now legal changes from the illegal set
bool empty()
indicates whether the selector still contains graph changes
std::vector< std::pair< NodeId, double > > nodesSortedByBestScore() const
returns the set of queues sorted by decreasing top priority
std::vector< std::pair< double, double > > _change_scores_
the scores for the head and tail of all the changes
GraphChangesSelector4DiGraph(const GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR > &from)
copy constructor
NodeProperty< double > _node_current_scores_
the current score of each node
GraphChangesSelector4DiGraph(Score &score, STRUCTURAL_CONSTRAINT &constraint, GRAPH_CHANGES_GENERATOR &changes_generator)
default constructor
STRUCTURAL_CONSTRAINT * _constraint_
the set of constraints used to determine valid changes
void _findLegalChangesNeedingUpdate_(Set< std::size_t > &changes_to_recompute, const NodeId target_node)
finds the changes that are affected by a given node modification
GRAPH_CHANGES_GENERATOR * _changes_generator_
the generator that returns the set of possible changes
void _updateScores_(const Set< std::size_t > &changes_to_recompute)
perform the necessary updates of the scores
std::vector< std::pair< NodeId, double > > nodesUnsortedWithScore() const
returns the set of queues top priorities
NodeProperty< std::vector< NodeId > > _parents_
the set of parents of each node (speeds-up score computations)
Set< std::size_t > _illegal_changes_
the set of changes known to be currently illegal (due to the constraints)
void applyChangeWithoutScoreUpdate(const GraphChange &change)
indicate to the selector that one of serveral changes has been applied
PriorityQueue< NodeId, double, std::greater< double > > _node_queue_
a global priority queue indicating for each node its best score
NodeProperty< PriorityQueue< std::size_t, double, std::greater< double > > > _change_queue_per_node_
for each node, a priority queue sorting GraphChanges by decreasing score
void _getNewChanges_()
get from the graph change generator a new set of changes
bool _isChangeValid_(const std::size_t index) const
indicates whether a given change is valid or not
GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR > & operator=(const GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR > &from)
copy operator
GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR > & operator=(GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR > &&from)
move operator
void _invalidateChange_(const std::size_t change_index)
put a change into the illegal set
void applyChange(const GraphChange &change)
indicate to the selector that a change has been applied
GRAPH_CHANGES_GENERATOR GeneratorType
the type of the generator
const GraphChange & bestChange()
returns the best graph change to examine
bool _queues_valid_
indicates whether we need to recompute whether the queue is empty or not
GraphChangesSelector4DiGraph(GraphChangesSelector4DiGraph< STRUCTURAL_CONSTRAINT, GRAPH_CHANGES_GENERATOR > &&from)
move constructor
The base class for all the scores used for learning (BIC, BDeu, etc).
Definition score.h:68
The mecanism to compute the next available graph changes for directed structure learning search algor...
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
the base class for all the scores used for learning (BIC, BDeu, etc)