aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
statesChecker.cpp
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
42
#include <
agrum/FMDP/simulation/statesChecker.h
>
43
44
#ifdef GUM_NO_INLINE
45
# include <
agrum/FMDP/simulation/statesChecker_inl.h
>
46
#endif
// GUM_NO_INLINE
47
48
namespace
gum
{
49
50
StatesChecker::StatesChecker
() :
51
_nbVisitedStates_
(0),
_checker_
(
MultiDimFunctionGraph
<
bool
>::getTreeInstance()),
52
_checkerTrueId_
(0),
_checkerFalseId_
(0) {
53
GUM_CONSTRUCTOR(
StatesChecker
);
54
}
55
56
StatesChecker::~StatesChecker
() {
57
delete
_checker_
;
58
GUM_DESTRUCTOR(
StatesChecker
);
59
}
60
61
void
StatesChecker::reset
(
const
Instantiation
& initialState) {
62
_checker_
->
clear
();
63
for
(
SequenceIteratorSafe< const DiscreteVariable* >
varIter
64
= initialState.
variablesSequence
().beginSafe();
65
varIter != initialState.
variablesSequence
().endSafe();
66
++varIter)
67
_checker_
->
add
(**varIter);
68
69
_nbVisitedStates_
= 1;
70
_checkerFalseId_
=
_checker_
->
manager
()->
addTerminalNode
(
false
);
71
_checkerTrueId_
=
_checker_
->
manager
()->
addTerminalNode
(
true
);
72
73
_insertState_
(initialState, 0, 0);
74
}
75
76
void
StatesChecker::addState
(
const
Instantiation
& state) {
77
_nbVisitedStates_
++;
78
79
NodeId
parId =
_checker_
->
root
();
80
Idx
parModa = state.
valFromPtr
(
_checker_
->
node
(parId)->
nodeVar
());
81
while
(
_checker_
->
node
(parId)->
son
(parModa) !=
_checkerFalseId_
) {
82
parId =
_checker_
->
node
(parId)->
son
(parModa);
83
parModa = state.
valFromPtr
(
_checker_
->
node
(parId)->
nodeVar
());
84
}
85
_insertState_
(state, parId, parModa);
86
}
87
88
void
StatesChecker::_insertState_
(
const
Instantiation
& state,
NodeId
parentId,
Idx
parentModa) {
89
Idx
varIter = 0;
90
if
(parentId) varIter = state.
variablesSequence
().pos(
_checker_
->
node
(parentId)->
nodeVar
()) + 1;
91
92
93
for
(; varIter < state.
variablesSequence
().size(); ++varIter) {
94
const
DiscreteVariable
* curVar = state.
variablesSequence
().atPos(varIter);
95
NodeId
varId =
_checker_
->
manager
()->
addInternalNode
(curVar);
96
if
(parentId)
_checker_
->
manager
()->
setSon
(parentId, parentModa, varId);
97
else
_checker_
->
manager
()->
setRootNode
(varId);
98
for
(
Idx
moda = 0; moda < curVar->
domainSize
(); ++moda) {
99
if
(moda == state.
valFromPtr
(curVar)) parentModa = moda;
100
else
_checker_
->
manager
()->
setSon
(varId, moda,
_checkerFalseId_
);
101
}
102
parentId = varId;
103
}
104
_checker_
->
manager
()->
setSon
(parentId, parentModa,
_checkerTrueId_
);
105
}
106
}
// End of namespace gum
bool
gum::DiscreteVariable
Base class for discrete random variable.
Definition
discreteVariable.h:80
gum::DiscreteVariable::domainSize
virtual Size domainSize() const =0
gum::Instantiation
Class for assigning/browsing values to tuples of discrete variables.
Definition
instantiation.h:102
gum::Instantiation::variablesSequence
const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
Definition
instantiation_inl.h:425
gum::Instantiation::valFromPtr
Idx valFromPtr(const DiscreteVariable *pvar) const
Returns the current value of a given variable.
Definition
instantiation_inl.h:183
gum::InternalNode::nodeVar
const DiscreteVariable * nodeVar() const
Returns the node variable.
Definition
internalNode_inl.h:91
gum::InternalNode::son
NodeId son(Idx modality) const
Returns the son at a given index.
Definition
internalNode_inl.h:97
gum::MultiDimFunctionGraphManager::addInternalNode
NodeId addInternalNode(const DiscreteVariable *var)
Inserts a new non terminal node in graph.
Definition
multiDimFunctionGraphManager_tpl.h:95
gum::MultiDimFunctionGraphManager::setRootNode
void setRootNode(const NodeId &root)
Sets root node of decision diagram.
Definition
multiDimFunctionGraphManager_tpl.h:75
gum::MultiDimFunctionGraphManager::addTerminalNode
NodeId addTerminalNode(const GUM_ELEMENT &value)
Adds a value to the MultiDimFunctionGraph.
Definition
multiDimFunctionGraphManager_tpl.h:122
gum::MultiDimFunctionGraphManager::setSon
void setSon(const NodeId &node, const Idx &modality, const NodeId &sonNode)
Sets nodes son for given modality to designated son node.
Definition
multiDimFunctionGraphManager_tpl.h:181
gum::MultiDimFunctionGraph
Class implementingting a function graph.
Definition
multiDimFunctionGraph.h:91
gum::MultiDimFunctionGraph::clear
void clear()
Clears the function graph.
Definition
multiDimFunctionGraph_tpl.h:425
gum::MultiDimFunctionGraph::add
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
Definition
multiDimFunctionGraph_tpl.h:164
gum::MultiDimFunctionGraph::root
const NodeId & root() const
Returns the id of the root node from the diagram.
Definition
multiDimFunctionGraph_tpl.h:546
gum::MultiDimFunctionGraph::node
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
Definition
multiDimFunctionGraph_tpl.h:576
gum::MultiDimFunctionGraph::manager
MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
Definition
multiDimFunctionGraph_tpl.h:534
gum::SequenceIteratorSafe
Safe iterators for Sequence.
Definition
sequence.h:1148
gum::StatesChecker::_checkerFalseId_
NodeId _checkerFalseId_
Definition
statesChecker.h:109
gum::StatesChecker::~StatesChecker
~StatesChecker()
Default destructor.
Definition
statesChecker.cpp:56
gum::StatesChecker::addState
void addState(const Instantiation &)
Definition
statesChecker.cpp:76
gum::StatesChecker::_checker_
MultiDimFunctionGraph< bool > * _checker_
Definition
statesChecker.h:108
gum::StatesChecker::reset
void reset(const Instantiation &)
Definition
statesChecker.cpp:61
gum::StatesChecker::_insertState_
void _insertState_(const Instantiation &, NodeId, Idx)
Definition
statesChecker.cpp:88
gum::StatesChecker::_nbVisitedStates_
Idx _nbVisitedStates_
Definition
statesChecker.h:106
gum::StatesChecker::StatesChecker
StatesChecker()
Default constructor.
Definition
statesChecker.cpp:50
gum::StatesChecker::_checkerTrueId_
NodeId _checkerTrueId_
Definition
statesChecker.h:109
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
gum::NodeId
Size NodeId
Type for node ids.
Definition
graphElements.h:117
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
statesChecker.h
Headers of the States Checker class.
statesChecker_inl.h
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0