Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
aGrUM
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Insights
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
2
Merge Requests
2
Security & Compliance
Security & Compliance
Dependency List
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
agrumery
aGrUM
Commits
a228298e
Commit
a228298e
authored
May 26, 2017
by
Pierre-Henri Wuillemin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[agrum] updating API for marginal targets
parent
9f8d92c5
Changes
5
Pipelines
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
191 additions
and
223 deletions
+191
-223
src/agrum/BN/inference/ShaferShenoyInference_tpl.h
src/agrum/BN/inference/ShaferShenoyInference_tpl.h
+4
-4
src/agrum/BN/inference/lazyPropagation_tpl.h
src/agrum/BN/inference/lazyPropagation_tpl.h
+4
-4
src/agrum/BN/inference/marginalTargetedInference.h
src/agrum/BN/inference/marginalTargetedInference.h
+33
-50
src/agrum/BN/inference/marginalTargetedInference_tpl.h
src/agrum/BN/inference/marginalTargetedInference_tpl.h
+141
-158
src/testunits/module_BN/MarginalTargetedInferenceTestSuite.h
src/testunits/module_BN/MarginalTargetedInferenceTestSuite.h
+9
-7
No files found.
src/agrum/BN/inference/ShaferShenoyInference_tpl.h
View file @
a228298e
...
...
@@ -346,7 +346,7 @@ namespace gum {
// however, note that the nodes that received hard evidence do not belong to
// the graph and, therefore, should not be taken into account
const
auto
&
hard_ev_nodes
=
this
->
hardEvidenceNodes
();
for
(
const
auto
node
:
this
->
allT
argets
()
)
{
for
(
const
auto
node
:
this
->
t
argets
()
)
{
if
(
!
__graph
.
exists
(
node
)
&&
!
hard_ev_nodes
.
exists
(
node
)
)
return
true
;
}
for
(
const
auto
&
nodes
:
this
->
jointTargets
()
)
{
...
...
@@ -412,7 +412,7 @@ namespace gum {
// the BN without altering the inference output
if
(
__barren_nodes_type
==
FindBarrenNodesType
::
FIND_BARREN_NODES
)
{
// identify the barren nodes
NodeSet
target_nodes
=
this
->
allT
argets
();
NodeSet
target_nodes
=
this
->
t
argets
();
for
(
const
auto
&
nodeset
:
this
->
jointTargets
()
)
{
target_nodes
+=
nodeset
;
}
...
...
@@ -999,7 +999,7 @@ namespace gum {
void
ShaferShenoyInference
<
GUM_SCALAR
>::
__computeJoinTreeRoots
()
{
// get the set of cliques in which we can find the targets and joint_targets
NodeSet
clique_targets
;
for
(
const
auto
node
:
this
->
allT
argets
()
)
{
for
(
const
auto
node
:
this
->
t
argets
()
)
{
try
{
clique_targets
.
insert
(
__node_to_clique
[
node
]
);
}
catch
(
Exception
&
)
{
...
...
@@ -1288,7 +1288,7 @@ namespace gum {
template
<
typename
GUM_SCALAR
>
INLINE
void
ShaferShenoyInference
<
GUM_SCALAR
>::
_makeInference
()
{
// collect messages for all single targets
for
(
const
auto
node
:
this
->
allT
argets
()
)
{
for
(
const
auto
node
:
this
->
t
argets
()
)
{
// perform only collects in the join tree for nodes that have
// not received hard evidence (those that received hard evidence were
// not included into the join tree for speed-up reasons)
...
...
src/agrum/BN/inference/lazyPropagation_tpl.h
View file @
a228298e
...
...
@@ -385,7 +385,7 @@ namespace gum {
// however, note that the nodes that received hard evidence do not belong to
// the graph and, therefore, should not be taken into account
const
auto
&
hard_ev_nodes
=
this
->
hardEvidenceNodes
();
for
(
const
auto
node
:
this
->
allT
argets
()
)
{
for
(
const
auto
node
:
this
->
t
argets
()
)
{
if
(
!
__graph
.
exists
(
node
)
&&
!
hard_ev_nodes
.
exists
(
node
)
)
return
true
;
}
for
(
const
auto
&
nodes
:
this
->
jointTargets
()
)
{
...
...
@@ -452,7 +452,7 @@ namespace gum {
// altering the inference output
if
(
__barren_nodes_type
==
FindBarrenNodesType
::
FIND_BARREN_NODES
)
{
// identify the barren nodes
NodeSet
target_nodes
=
this
->
allT
argets
();
NodeSet
target_nodes
=
this
->
t
argets
();
for
(
const
auto
&
nodeset
:
this
->
jointTargets
()
)
{
target_nodes
+=
nodeset
;
}
...
...
@@ -968,7 +968,7 @@ namespace gum {
void
LazyPropagation
<
GUM_SCALAR
>::
__computeJoinTreeRoots
()
{
// get the set of cliques in which we can find the targets and joint_targets
NodeSet
clique_targets
;
for
(
const
auto
node
:
this
->
allT
argets
()
)
{
for
(
const
auto
node
:
this
->
t
argets
()
)
{
try
{
clique_targets
.
insert
(
__node_to_clique
[
node
]
);
}
catch
(
Exception
&
)
{
...
...
@@ -1347,7 +1347,7 @@ namespace gum {
template
<
typename
GUM_SCALAR
>
INLINE
void
LazyPropagation
<
GUM_SCALAR
>::
_makeInference
()
{
// collect messages for all single targets
for
(
const
auto
node
:
this
->
allT
argets
()
)
{
for
(
const
auto
node
:
this
->
t
argets
()
)
{
// perform only collects in the join tree for nodes that have
// not received hard evidence (those that received hard evidence were
// not included into the join tree for speed-up reasons)
...
...
src/agrum/BN/inference/marginalTargetedInference.h
View file @
a228298e
...
...
@@ -50,7 +50,7 @@ namespace gum {
* the current state of the inference. Note that the MarginalTargetedInference
* is designed to be used in incremental inference engines.
*/
template
<
typename
GUM_SCALAR
>
template
<
typename
GUM_SCALAR
>
class
MarginalTargetedInference
:
public
virtual
BayesNetInference
<
GUM_SCALAR
>
{
public:
// ############################################################################
...
...
@@ -59,12 +59,10 @@ namespace gum {
/// @{
/// default constructor
/** @warning By default (when the targets set is empty), all the nodes of
* the Bayes net are considered as targets. Once a first target is added to the
* set, the remaining nodes are not considered as default targets anymore.
/** @warning By default, all the nodes of the Bayes net are targets.
* @warning note that, by aGrUM's rule, the BN is not copied but only
* referenced by the inference algorithm. */
MarginalTargetedInference
(
const
IBayesNet
<
GUM_SCALAR
>*
bn
);
MarginalTargetedInference
(
const
IBayesNet
<
GUM_SCALAR
>
*
bn
);
/// destructor
virtual
~
MarginalTargetedInference
();
...
...
@@ -92,7 +90,7 @@ namespace gum {
*
* @throw UndefinedElement if node is not in the set of targets
*/
virtual
const
Potential
<
GUM_SCALAR
>
&
posterior
(
const
NodeId
node
);
virtual
const
Potential
<
GUM_SCALAR
>
&
posterior
(
const
NodeId
node
);
/// Computes and returns the posterior of a node.
/**
...
...
@@ -110,7 +108,7 @@ namespace gum {
*
* @throw UndefinedElement if node is not in the set of targets
*/
virtual
const
Potential
<
GUM_SCALAR
>
&
posterior
(
const
std
::
string
&
nodeName
);
virtual
const
Potential
<
GUM_SCALAR
>
&
posterior
(
const
std
::
string
&
nodeName
);
/// @}
...
...
@@ -121,62 +119,44 @@ namespace gum {
/// @{
/// Clear all previously defined targets
/// @warning this means that every node is now a target by default
virtual
void
eraseAllTargets
();
/// adds all nodes as targets
/// @warning due to the semantic of targets, this function is an alias of
/// eraseAllTargets()
virtual
void
addAllTargets
()
final
;
/// Add a marginal target to the list of targets
/**
* @throw UndefinedElement if target is not a NodeId in the Bayes net
*/
virtual
void
addTarget
(
const
NodeId
target
)
final
;
virtual
void
addTarget
(
const
NodeId
target
)
final
;
/// Add a marginal target to the list of targets
/**
* @throw UndefinedElement if target is not a NodeId in the Bayes net
*/
virtual
void
addTarget
(
const
std
::
string
&
nodeName
)
final
;
virtual
void
addTarget
(
const
std
::
string
&
nodeName
)
final
;
/// removes an existing (marginal) target
/** @warning If the target does not already exist, the method does nothing.
* In particular, it does not raise any exception.
* @warning Erasing the last target implies that every node is now a target by
* default.
* */
virtual
void
eraseTarget
(
const
NodeId
target
)
final
;
* In particular, it does not raise any exception. */
virtual
void
eraseTarget
(
const
NodeId
target
)
final
;
/// removes an existing (marginal) target
/** @warning If the target does not already exist, the method does nothing.
* In particular, it does not raise any exception.
* @warning Erasing the last target implies that every node is now a target by
* default.*/
virtual
void
eraseTarget
(
const
std
::
string
&
nodeName
)
final
;
* In particular, it does not raise any exception. */
virtual
void
eraseTarget
(
const
std
::
string
&
nodeName
)
final
;
/// return true if variable is a (marginal) target
virtual
bool
isTarget
(
const
NodeId
variable
)
const
final
;
virtual
bool
isTarget
(
const
NodeId
variable
)
const
final
;
/// return true if variable is a (marginal) target
virtual
bool
isTarget
(
const
std
::
string
&
nodeName
)
const
final
;
virtual
bool
isTarget
(
const
std
::
string
&
nodeName
)
const
final
;
/// returns the number of marginal targets.
//// @warning if the result is 0, it means that all the nodes are targets by
/// default.
/// returns the number of marginal targets
virtual
const
Size
nbrTargets
()
const
noexcept
final
;
/// returns the set of marginal targets
//// @warning if the set is empty, it means that all the nodes are targets by
/// default.
virtual
const
NodeSet
&
targets
()
const
noexcept
final
;
/// return all the marginal targets.
/** Particularly, if the targetSet is empty, allTargets will send a copy of the
* nodeSet of the BN
*/
virtual
NodeSet
allTargets
()
const
noexcept
final
;
/// returns the list of marginal targets
virtual
const
NodeSet
&
targets
()
const
noexcept
final
;
/// @}
...
...
@@ -189,13 +169,13 @@ namespace gum {
* Compute Shanon's entropy of a node given the observation
* @see http://en.wikipedia.org/wiki/Information_entropy
*/
virtual
GUM_SCALAR
H
(
const
NodeId
X
)
final
;
virtual
GUM_SCALAR
H
(
const
NodeId
X
)
final
;
/** Entropy
* Compute Shanon's entropy of a node given the observation
* @see http://en.wikipedia.org/wiki/Information_entropy
*/
virtual
GUM_SCALAR
H
(
const
std
::
string
&
nodeName
)
final
;
virtual
GUM_SCALAR
H
(
const
std
::
string
&
nodeName
)
final
;
///@}
...
...
@@ -211,8 +191,8 @@ namespace gum {
* @param evs the vector of nodeId of the observed variables
* @return a Potential
*/
Potential
<
GUM_SCALAR
>
evidenceImpact
(
NodeId
target
,
const
std
::
vector
<
NodeId
>&
evs
);
Potential
<
GUM_SCALAR
>
evidenceImpact
(
NodeId
target
,
const
std
::
vector
<
NodeId
>&
evs
);
/**
* Create a gum::Potential for P(target|evs) (for all instanciation of target
...
...
@@ -224,17 +204,17 @@ namespace gum {
* @param evs the nodeId of the observed variable
* @return a Potential
*/
Potential
<
GUM_SCALAR
>
evidenceImpact
(
const
std
::
string
&
target
,
const
std
::
vector
<
std
::
string
>&
evs
);
Potential
<
GUM_SCALAR
>
evidenceImpact
(
const
std
::
string
&
target
,
const
std
::
vector
<
std
::
string
>&
evs
);
protected:
/// fired after a new marginal target is inserted
/** @param id The target variable's id. */
virtual
void
_onMarginalTargetAdded
(
const
NodeId
id
)
=
0
;
virtual
void
_onMarginalTargetAdded
(
const
NodeId
id
)
=
0
;
/// fired before a marginal target is removed
/** @param id The target variable's id. */
virtual
void
_onMarginalTargetErased
(
const
NodeId
id
)
=
0
;
virtual
void
_onMarginalTargetErased
(
const
NodeId
id
)
=
0
;
/// fired after all the nodes of the BN are added as marginal targets
virtual
void
_onAllMarginalTargetsAdded
()
=
0
;
...
...
@@ -243,16 +223,19 @@ namespace gum {
virtual
void
_onAllMarginalTargetsErased
()
=
0
;
/// fired after a new Bayes net has been assigned to the engine
virtual
void
_onBayesNetChanged
(
const
IBayesNet
<
GUM_SCALAR
>*
bn
);
virtual
void
_onBayesNetChanged
(
const
IBayesNet
<
GUM_SCALAR
>
*
bn
);
/// asks derived classes for the posterior of a given variable
/** @param id The variable's id. */
virtual
const
Potential
<
GUM_SCALAR
>
&
_posterior
(
const
NodeId
id
)
=
0
;
virtual
const
Potential
<
GUM_SCALAR
>
&
_posterior
(
const
NodeId
id
)
=
0
;
private:
/// whether the actual targets are default
bool
__defaultTargets
;
/// the set of marginal targets
NodeSet
__targets
Set
;
NodeSet
__targets
;
/// remove all the marginal posteriors computed
...
...
src/agrum/BN/inference/marginalTargetedInference_tpl.h
View file @
a228298e
This diff is collapsed.
Click to expand it.
src/testunits/module_BN/MarginalTargetedInferenceTestSuite.h
View file @
a228298e
...
...
@@ -44,23 +44,25 @@ namespace gum_tests {
gum
::
BayesNet
<
double
>::
fastPrototype
(
"A->B->C->D;A->E->D;F->B;C->H;"
);
gum
::
LazyPropagation
<
double
>
lazy
(
&
bn
);
TS_ASSERT
(
lazy
.
targets
()
==
gum
::
NodeSet
(
{
}
)
);
TS_ASSERT
(
lazy
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
,
2
,
3
,
4
,
5
,
6
}
)
);
lazy
.
addTarget
(
"A"
);
TS_ASSERT
(
lazy
.
targets
()
==
gum
::
NodeSet
(
{
0
}
)
);
TS_ASSERT
(
lazy
.
targets
()
==
gum
::
NodeSet
(
{
0
}
)
);
lazy
.
addTarget
(
"B"
);
TS_ASSERT
(
lazy
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
}
)
);
TS_ASSERT
(
lazy
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
}
)
);
gum
::
ShaferShenoyInference
<
double
>
shafer
(
&
bn
);
TS_ASSERT
(
shafer
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
,
2
,
3
,
4
,
5
,
6
}
)
);
shafer
.
addTarget
(
"A"
);
TS_ASSERT
(
shafer
.
targets
()
==
gum
::
NodeSet
(
{
0
}
)
);
TS_ASSERT
(
shafer
.
targets
()
==
gum
::
NodeSet
(
{
0
}
)
);
shafer
.
addTarget
(
"B"
);
TS_ASSERT
(
shafer
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
}
)
);
TS_ASSERT
(
shafer
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
}
)
);
gum
::
VariableElimination
<
double
>
ve
(
&
bn
);
TS_ASSERT
(
ve
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
,
2
,
3
,
4
,
5
,
6
}
)
);
ve
.
addTarget
(
"A"
);
TS_ASSERT
(
ve
.
targets
()
==
gum
::
NodeSet
(
{
0
}
)
);
TS_ASSERT
(
ve
.
targets
()
==
gum
::
NodeSet
(
{
0
}
)
);
ve
.
addTarget
(
"B"
);
TS_ASSERT
(
ve
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
}
)
);
TS_ASSERT
(
ve
.
targets
()
==
gum
::
NodeSet
(
{
0
,
1
}
)
);
}
};
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment