aGrUM 2.3.2
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-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
46
47 // =================================================================================================
48 //
49 // =================================================================================================
51 GUM_CONSTRUCTOR(StatesCounter);
52 }
53
54 // =================================================================================================
55 //
56 // =================================================================================================
58 delete _counter_;
59 GUM_DESTRUCTOR(StatesCounter);
60 }
61
62 // =================================================================================================
63 //
64 // =================================================================================================
65 void StatesCounter::reset(const Instantiation& initialState) {
66 _counter_->clear();
68 = initialState.variablesSequence().beginSafe();
69 varIter != initialState.variablesSequence().endSafe();
70 ++varIter)
71 _counter_->add(**varIter);
72
73 _counter_->manager()->setRootNode(_counter_->manager()->addTerminalNode(0));
74
75 _incState_(initialState, 0, 0, 0);
76 }
77
78 // =================================================================================================
79 //
80 // =================================================================================================
82 Idx nbVisits = _counter_->get(state);
83
84 NodeId parId = _counter_->root();
85 Idx parModa = state.valFromPtr(_counter_->node(parId)->nodeVar());
86 while (!_counter_->isTerminalNode(_counter_->node(parId)->son(parModa))) {
87 parId = _counter_->node(parId)->son(parModa);
88 parModa = state.valFromPtr(_counter_->node(parId)->nodeVar());
89 }
90 _incState_(state, parId, parModa, nbVisits);
91 }
92
93 // =================================================================================================
94 //
95 // =================================================================================================
97 NodeId parentId,
98 Idx parentModa,
99 Size nbVisits) {
100 Idx varIter = 0;
101 if (parentId) varIter = state.variablesSequence().pos(_counter_->node(parentId)->nodeVar()) + 1;
102
103
104 for (; varIter < state.variablesSequence().size(); ++varIter) {
105 const DiscreteVariable* curVar = state.variablesSequence().atPos(varIter);
106 NodeId varId = _counter_->manager()->addInternalNode(curVar);
107 if (parentId) _counter_->manager()->setSon(parentId, parentModa, varId);
108 else _counter_->manager()->setRootNode(varId);
109 for (Idx moda = 0; moda < curVar->domainSize(); ++moda) {
110 if (moda == state.valFromPtr(curVar)) parentModa = moda;
111 else _counter_->manager()->setSon(varId, moda, _counter_->terminalNodeId(nbVisits));
112 }
113 parentId = varId;
114 }
115 _counter_->manager()->setSon(parentId,
116 parentModa,
117 _counter_->manager()->addTerminalNode(nbVisits + 1));
118 }
119} // 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
void _incState_(const Instantiation &, NodeId, Idx, Size)
StatesCounter()
Default constructor.
void incState(const Instantiation &)
void reset(const Instantiation &)
MultiDimFunctionGraph< Size > * _counter_
~StatesCounter()
Default destructor.
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.