aGrUM 2.3.2
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-2025 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-2025 *
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
44namespace gum {
45
51
53 delete _checker_;
54 GUM_DESTRUCTOR(StatesChecker);
55 }
56
57 void StatesChecker::reset(const Instantiation& initialState) {
58 _checker_->clear();
60 = initialState.variablesSequence().beginSafe();
61 varIter != initialState.variablesSequence().endSafe();
62 ++varIter)
63 _checker_->add(**varIter);
64
66 _checkerFalseId_ = _checker_->manager()->addTerminalNode(false);
67 _checkerTrueId_ = _checker_->manager()->addTerminalNode(true);
68
69 _insertState_(initialState, 0, 0);
70 }
71
74
75 NodeId parId = _checker_->root();
76 Idx parModa = state.valFromPtr(_checker_->node(parId)->nodeVar());
77 while (_checker_->node(parId)->son(parModa) != _checkerFalseId_) {
78 parId = _checker_->node(parId)->son(parModa);
79 parModa = state.valFromPtr(_checker_->node(parId)->nodeVar());
80 }
81 _insertState_(state, parId, parModa);
82 }
83
84 void StatesChecker::_insertState_(const Instantiation& state, NodeId parentId, Idx parentModa) {
85 Idx varIter = 0;
86 if (parentId) varIter = state.variablesSequence().pos(_checker_->node(parentId)->nodeVar()) + 1;
87
88
89 for (; varIter < state.variablesSequence().size(); ++varIter) {
90 const DiscreteVariable* curVar = state.variablesSequence().atPos(varIter);
91 NodeId varId = _checker_->manager()->addInternalNode(curVar);
92 if (parentId) _checker_->manager()->setSon(parentId, parentModa, varId);
93 else _checker_->manager()->setRootNode(varId);
94 for (Idx moda = 0; moda < curVar->domainSize(); ++moda) {
95 if (moda == state.valFromPtr(curVar)) parentModa = moda;
96 else _checker_->manager()->setSon(varId, moda, _checkerFalseId_);
97 }
98 parentId = varId;
99 }
100 _checker_->manager()->setSon(parentId, parentModa, _checkerTrueId_);
101 }
102} // 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:1134
~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.