aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
statesCounter.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
50
51 // =================================================================================================
52 //
53 // =================================================================================================
55 GUM_CONSTRUCTOR(StatesCounter);
56 }
57
58 // =================================================================================================
59 //
60 // =================================================================================================
62 delete _counter_;
63 GUM_DESTRUCTOR(StatesCounter);
64 }
65
66 // =================================================================================================
67 //
68 // =================================================================================================
69 void StatesCounter::reset(const Instantiation& initialState) {
70 _counter_->clear();
72 = initialState.variablesSequence().beginSafe();
73 varIter != initialState.variablesSequence().endSafe();
74 ++varIter)
75 _counter_->add(**varIter);
76
77 _counter_->manager()->setRootNode(_counter_->manager()->addTerminalNode(0));
78
79 _incState_(initialState, 0, 0, 0);
80 }
81
82 // =================================================================================================
83 //
84 // =================================================================================================
86 Idx nbVisits = _counter_->get(state);
87
88 NodeId parId = _counter_->root();
89 Idx parModa = state.valFromPtr(_counter_->node(parId)->nodeVar());
90 while (!_counter_->isTerminalNode(_counter_->node(parId)->son(parModa))) {
91 parId = _counter_->node(parId)->son(parModa);
92 parModa = state.valFromPtr(_counter_->node(parId)->nodeVar());
93 }
94 _incState_(state, parId, parModa, nbVisits);
95 }
96
97 // =================================================================================================
98 //
99 // =================================================================================================
101 NodeId parentId,
102 Idx parentModa,
103 Size nbVisits) {
104 Idx varIter = 0;
105 if (parentId) varIter = state.variablesSequence().pos(_counter_->node(parentId)->nodeVar()) + 1;
106
107
108 for (; varIter < state.variablesSequence().size(); ++varIter) {
109 const DiscreteVariable* curVar = state.variablesSequence().atPos(varIter);
110 NodeId varId = _counter_->manager()->addInternalNode(curVar);
111 if (parentId) _counter_->manager()->setSon(parentId, parentModa, varId);
112 else _counter_->manager()->setRootNode(varId);
113 for (Idx moda = 0; moda < curVar->domainSize(); ++moda) {
114 if (moda == state.valFromPtr(curVar)) parentModa = moda;
115 else _counter_->manager()->setSon(varId, moda, _counter_->terminalNodeId(nbVisits));
116 }
117 parentId = varId;
118 }
119 _counter_->manager()->setSon(parentId,
120 parentModa,
121 _counter_->manager()->addTerminalNode(nbVisits + 1));
122 }
123} // 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
void _incState_(const Instantiation &, NodeId, Idx, Size)
StatesCounter()
Default constructor.
void incState(const Instantiation &)
~StatesCounter() override
Default destructor.
void reset(const Instantiation &)
MultiDimFunctionGraph< Size > * _counter_
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
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 Counter class.