aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
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
43
44#ifdef GUM_NO_INLINE
46#endif // GUM_NO_INLINE
47
48namespace gum {
49
55
57 delete _checker_;
58 GUM_DESTRUCTOR(StatesChecker);
59 }
60
61 void StatesChecker::reset(const Instantiation& initialState) {
62 _checker_->clear();
64 = initialState.variablesSequence().beginSafe();
65 varIter != initialState.variablesSequence().endSafe();
66 ++varIter)
67 _checker_->add(**varIter);
68
70 _checkerFalseId_ = _checker_->manager()->addTerminalNode(false);
71 _checkerTrueId_ = _checker_->manager()->addTerminalNode(true);
72
73 _insertState_(initialState, 0, 0);
74 }
75
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
Base class for discrete random variable.
virtual Size domainSize() const =0
Class for assigning/browsing values to tuples of discrete variables.
const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
Idx valFromPtr(const DiscreteVariable *pvar) const
Returns the current value of a given variable.
Class implementingting a function graph.
Safe iterators for Sequence.
Definition sequence.h:1148
~StatesChecker()
Default destructor.
void addState(const Instantiation &)
MultiDimFunctionGraph< bool > * _checker_
void reset(const Instantiation &)
void _insertState_(const Instantiation &, NodeId, Idx)
StatesChecker()
Default constructor.
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Headers of the States Checker class.