aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
greedyHillClimbing_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by 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 greedy hill learning algorithm (for directed graphs)
24
*
25
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26
*/
27
28
#
include
<
agrum
/
BN
/
learning
/
paramUtils
/
DAG2BNLearner
.
h
>
29
#
include
<
agrum
/
BN
/
learning
/
structureUtils
/
graphChange
.
h
>
30
31
namespace
gum
{
32
33
namespace
learning
{
34
35
/// learns the structure of a Bayes net
36
template
<
typename
GRAPH_CHANGES_SELECTOR
>
37
DAG
GreedyHillClimbing
::
learnStructure
(
GRAPH_CHANGES_SELECTOR
&
selector
,
DAG
dag
) {
38
selector
.
setGraph
(
dag
);
39
40
unsigned
int
nb_changes_applied
= 1;
41
double
delta_score
;
42
43
initApproximationScheme
();
44
45
// a vector that indicates which queues have valid scores, i.e., scores
46
// that were not invalidated by previously applied changes
47
std
::
vector
<
bool
>
impacted_queues
(
dag
.
size
(),
false
);
48
49
do
{
50
nb_changes_applied
= 0;
51
delta_score
= 0;
52
53
std
::
vector
<
std
::
pair
<
NodeId
,
double
> >
ordered_queues
54
=
selector
.
nodesSortedByBestScore
();
55
56
for
(
Idx
j
= 0;
j
<
dag
.
size
(); ++
j
) {
57
Idx
i
=
ordered_queues
[
j
].
first
;
58
59
if
(!(
selector
.
empty
(
i
)) && (
selector
.
bestScore
(
i
) > 0)) {
60
// pick up the best change
61
const
GraphChange
&
change
=
selector
.
bestChange
(
i
);
62
63
// perform the change
64
switch
(
change
.
type
()) {
65
case
GraphChangeType
::
ARC_ADDITION
:
66
if
(!
impacted_queues
[
change
.
node2
()] &&
selector
.
isChangeValid
(
change
)) {
67
delta_score
+=
selector
.
bestScore
(
i
);
68
dag
.
addArc
(
change
.
node1
(),
change
.
node2
());
69
impacted_queues
[
change
.
node2
()] =
true
;
70
selector
.
applyChangeWithoutScoreUpdate
(
change
);
71
++
nb_changes_applied
;
72
}
73
74
break
;
75
76
case
GraphChangeType
::
ARC_DELETION
:
77
if
(!
impacted_queues
[
change
.
node2
()] &&
selector
.
isChangeValid
(
change
)) {
78
delta_score
+=
selector
.
bestScore
(
i
);
79
dag
.
eraseArc
(
Arc
(
change
.
node1
(),
change
.
node2
()));
80
impacted_queues
[
change
.
node2
()] =
true
;
81
selector
.
applyChangeWithoutScoreUpdate
(
change
);
82
++
nb_changes_applied
;
83
}
84
85
break
;
86
87
case
GraphChangeType
::
ARC_REVERSAL
:
88
if
((!
impacted_queues
[
change
.
node1
()]) && (!
impacted_queues
[
change
.
node2
()])
89
&&
selector
.
isChangeValid
(
change
)) {
90
delta_score
+=
selector
.
bestScore
(
i
);
91
dag
.
eraseArc
(
Arc
(
change
.
node1
(),
change
.
node2
()));
92
dag
.
addArc
(
change
.
node2
(),
change
.
node1
());
93
impacted_queues
[
change
.
node1
()] =
true
;
94
impacted_queues
[
change
.
node2
()] =
true
;
95
selector
.
applyChangeWithoutScoreUpdate
(
change
);
96
++
nb_changes_applied
;
97
}
98
99
break
;
100
101
default
:
102
GUM_ERROR
(
OperationNotAllowed
,
103
"edge modifications are not supported by local search"
)
104
}
105
}
106
}
107
108
selector
.
updateScoresAfterAppliedChanges
();
109
110
// reset the impacted queue and applied changes structures
111
for
(
auto
iter
=
impacted_queues
.
begin
();
iter
!=
impacted_queues
.
end
(); ++
iter
) {
112
*
iter
=
false
;
113
}
114
115
updateApproximationScheme
(
nb_changes_applied
);
116
117
}
while
(
nb_changes_applied
&&
continueApproximationScheme
(
delta_score
));
118
119
stopApproximationScheme
();
// just to be sure of the approximationScheme
120
// has
121
// been notified of the end of looop
122
123
return
dag
;
124
}
125
126
/// learns the structure and the parameters of a BN
127
template
<
typename
GUM_SCALAR
,
typename
GRAPH_CHANGES_SELECTOR
,
typename
PARAM_ESTIMATOR
>
128
BayesNet
<
GUM_SCALAR
>
GreedyHillClimbing
::
learnBN
(
GRAPH_CHANGES_SELECTOR
&
selector
,
129
PARAM_ESTIMATOR
&
estimator
,
130
DAG
initial_dag
) {
131
return
DAG2BNLearner
<>::
createBN
<
GUM_SCALAR
>(
estimator
,
132
learnStructure
(
selector
,
initial_dag
));
133
}
134
135
}
/* namespace learning */
136
137
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643
gum::learning::genericBNLearner::Database::Database
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Definition:
genericBNLearner_tpl.h:31