aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
greedyThickThinning_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 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-2026 *
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#pragma once
42
43
49
50#include <agrum/BN/learning/greedyThickThinning.h> // to ease IDE parser
53
54namespace gum {
55
56 namespace learning {
57
59 template < typename GRAPH_CHANGES_SELECTOR >
60 DAG GreedyThickThinning::learnStructure(GRAPH_CHANGES_SELECTOR& selector, DAG dag) {
62
63 // Phase 1: thickening — arc additions only
64 selector.useArcAdditions(true);
65 selector.useArcDeletions(false);
66 selector.useArcReversals(false);
67 selector.useArcTriangleDeletions(false);
68 selector.setGraph(dag);
69
70 unsigned int nb_changes_applied = 1;
71 double delta_score;
72
73 do {
74 nb_changes_applied = 0;
75 delta_score = 0;
76
77 try {
78 const auto& change = selector.bestChange();
79 delta_score = selector.deltaScore(change, true);
80
81 if ((delta_score > 0) && continueApproximationScheme(delta_score)) {
82 selector.applyChange(change);
83 nb_changes_applied = 1;
84 updateApproximationScheme(nb_changes_applied);
85 }
86 } catch (NotFound&) {}
87 } while (nb_changes_applied);
88
89 // Phase 2: thinning — arc deletions (+ optional reversals), no additions
90 selector.useArcAdditions(false);
91 selector.useArcDeletions(true);
92 selector.useArcReversals(_allowReversalsInThinPhase_);
93 selector.useArcTriangleDeletions(false);
94 selector.setGraph(dag);
95
96 do {
97 nb_changes_applied = 0;
98 delta_score = 0;
99
100 try {
101 const auto& change = selector.bestChange();
102 delta_score = selector.deltaScore(change, true);
103
104 if ((delta_score > 0) && continueApproximationScheme(delta_score)) {
105 selector.applyChange(change);
106 nb_changes_applied = 1;
107 updateApproximationScheme(nb_changes_applied);
108 }
109 } catch (NotFound&) {}
110 } while (nb_changes_applied);
111
113
114 selector.finalizeGraph(dag);
115
116 return dag;
117 }
118
120 template < GUM_Numeric GUM_SCALAR, typename GRAPH_CHANGES_SELECTOR, typename PARAM_ESTIMATOR >
121 BayesNet< GUM_SCALAR > GreedyThickThinning::learnBN(GRAPH_CHANGES_SELECTOR& selector,
122 PARAM_ESTIMATOR& estimator,
123 DAG initial_dag) {
125 learnStructure(selector, initial_dag));
126 }
127
128 } /* namespace learning */
129
130} /* namespace gum */
A class that, given a structure and a parameter estimator returns a full Bayes net.
void updateApproximationScheme(unsigned int incr=1)
Update the scheme w.r.t the new error and increment steps.
bool continueApproximationScheme(double error)
Update the scheme w.r.t the new error.
void initApproximationScheme()
Initialise the scheme.
void stopApproximationScheme()
Stop the approximation scheme.
Base class for dag.
Definition DAG.h:121
Exception : the element we looked for cannot be found.
static BayesNet< GUM_SCALAR > createBN(ParamEstimator &estimator, const DAG &dag)
create a BN from a DAG using a one pass generator (typically ML)
DAG learnStructure(GRAPH_CHANGES_SELECTOR &selector, DAG initial_dag=DAG())
learns the structure of a Bayes net
BayesNet< GUM_SCALAR > learnBN(GRAPH_CHANGES_SELECTOR &selector, PARAM_ESTIMATOR &estimator, DAG initial_dag=DAG())
learns the structure and the parameters of a BN
the classes to account for structure changes in a graph
The greedy thick-thinning learning algorithm (for directed graphs).
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46