aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
localSearchWithTabuList_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
50
51#include <agrum/BN/learning/localSearchWithTabuList.h> // to ease IDE parser
54
55namespace gum {
56
57 namespace learning {
58
60 template < typename GRAPH_CHANGES_SELECTOR >
61 DAG LocalSearchWithTabuList::learnStructure(GRAPH_CHANGES_SELECTOR& selector, DAG dag) {
62 selector.setGraph(dag);
63
64 unsigned int nb_changes_applied = 0;
65 Idx applied_change_with_positive_score = 0;
66 Idx current_N = 0;
67
69
70 // the best dag found so far with its score
71 DAG best_dag = dag;
72 double best_score = 0;
73 double current_score = 0;
74 double delta_score = 0;
75
76 do {
77 applied_change_with_positive_score = 0;
78 delta_score = 0;
79
80 try {
81 const auto& change = selector.bestChange();
82 delta_score = selector.deltaScore(change, true);
83
84 if ((nb_changes_applied > 0) || (delta_score > 0)) {
85 if (delta_score > 0) {
86 ++applied_change_with_positive_score;
87 } else if (current_score > best_score) {
88 best_score = current_score;
89 best_dag = dag;
90 }
91
92 selector.applyChange(change);
93 current_score += delta_score;
94 ++nb_changes_applied;
95 }
96
97 updateApproximationScheme(nb_changes_applied);
98
99 // update current_N
100 if (applied_change_with_positive_score) {
101 current_N = 0;
102 nb_changes_applied = 0;
103 } else {
104 ++current_N;
105 }
106 } catch (NotFound&) { break; }
107 } while ((current_N <= _MaxNbDecreasing_) && continueApproximationScheme(delta_score));
108
109 stopApproximationScheme(); // just to be sure of the approximationScheme
110 // has been notified of the end of loop
111
112 // get the dag that we will return
113 auto& res_dag = current_score > best_score ? dag : best_dag;
114
115 // here, we add to the dag the set of nodes that were removed by method
116 // setGraph because they did not belong to the database
117 selector.finalizeGraph(res_dag);
118
119 return res_dag;
120 }
121
123 template < GUM_Numeric GUM_SCALAR, typename GRAPH_CHANGES_SELECTOR, typename PARAM_ESTIMATOR >
124 BayesNet< GUM_SCALAR > LocalSearchWithTabuList::learnBN(GRAPH_CHANGES_SELECTOR& selector,
125 PARAM_ESTIMATOR& estimator,
126 DAG initial_dag) {
128 learnStructure(selector, initial_dag));
129 }
130
131 } /* namespace learning */
132
133} /* 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)
Size _MaxNbDecreasing_
the max number of changes decreasing the score that we allow to apply
BayesNet< GUM_SCALAR > learnBN(GRAPH_CHANGES_SELECTOR &selector, PARAM_ESTIMATOR &estimator, DAG initial_dag=DAG())
learns the structure and the parameters of a BN
DAG learnStructure(GRAPH_CHANGES_SELECTOR &selector, DAG initial_dag=DAG())
learns the structure of a Bayes net
the classes to account for structure changes in a graph
Size Idx
Type for indexes.
Definition types.h:79
The local search learning with tabu list 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