aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
instantiation.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
48
51
52#ifdef GUM_NO_INLINE
54#endif /* GUM_NO_INLINE */
55
56namespace gum {
57
58 // Default constructor
60 GUM_CONSTRUCTOR(Instantiation);
61 }
62
63 // destructor
65 GUM_DESTRUCTOR(Instantiation);
66 // unregister the Instantiation from its _master_
67
68 if (_master_) _master_->unregisterSlave(*this);
69 }
70
72 // for speed issues
73 GUM_ASSERT(master != nullptr);
74
76 _vars_.resize(v.size());
77 _vals_.reserve(v.size());
78 // fill the instantiation
79
80 for (const auto var: v)
81 _add_(*var);
82
83 actAsSlave(master->getMasterRef());
84 }
85
86 // constructor for a Instantiation contained into a MultiDimInterface
88 // for debugging purposes
89 GUM_CONSTRUCTOR(Instantiation);
90 _init_(&d);
91 }
92
94 // for debugging purposes
95 GUM_CONSTRUCTOR(Instantiation);
96 _init_(const_cast< MultiDimAdressable* >(&d));
97 }
98
99 // constructor for a Instantiation contained into a MultiDimInterface
101 // for debugging purposes
102 GUM_CONSTRUCTOR(Instantiation);
103
104 if (d) _init_(d);
105 }
106
107 // constructor for a Instantiation contained into a MultiDimInterface this
108 // constructor is needed in order to allow creation of Instantiation(this) in
109 // MultiDimAdressable and below
111 // for debugging purposes
112 GUM_CONSTRUCTOR(Instantiation);
113
114 if (const_d) _init_(const_cast< MultiDimAdressable* >(const_d));
115 }
116
117 // copy constructor
118 Instantiation::Instantiation(const Instantiation& aI, const bool notifyMaster) :
120 // for debugging purposes
121 GUM_CONS_CPY(Instantiation);
122 // copy the content of aI
123 _vars_ = aI._vars_;
124 _vals_ = aI._vals_;
126
127 if (aI._master_ && notifyMaster) actAsSlave(*aI._master_);
128 }
129
130 // operator=
132 if (_master_) {
133 if (!aI.isMaster(_master_)) { // aI as the same master.
134 if (nbrDim() != aI.nbrDim()) { GUM_ERROR(OperationNotAllowed, "in slave Instantiation") }
135
136 for (Idx i = 0; i < nbrDim(); i++) {
137 if ((!contains(aI.variable(i))) || (!aI.contains(variable(i)))) {
138 GUM_ERROR(OperationNotAllowed, "in slave Instantiation")
139 }
140 }
141 }
142
143 setVals(aI);
144 } else {
145 // copy the content of aI
146 _vars_ = aI._vars_;
147 _vals_ = aI._vals_;
149
150 if (aI._master_) actAsSlave(*aI._master_);
151 }
152
153 return *this;
154 }
155
156 // Gives a string version of a Instantiation
157 std::string Instantiation::toString() const {
158 std::stringstream sstr;
159 // check if the value of the instantiation is correct
160
161 if (_overflow_) { sstr << "<invalid>"; }
162
163 sstr << "<";
164
165 bool first = true;
166
167 for (const auto var: _vars_) {
168 if (!first) sstr << "|";
169
170 first = false;
171 sstr << var->name() << ":" << var->label(val(*var));
172 }
173
174 sstr << ">";
175
176 return sstr.str();
177 }
178
179 // give a Id value for Hamming distance
181 Idx res = 0;
182
183 for (const auto var: _vars_)
184 res += val(*var);
185
186 return res;
187 }
188
191 const Instantiation& external) {
192 for (const auto& elt: map) {
193 const DiscreteVariable& var = *elt.second;
194
195 try {
196 Idx val = external.val(*elt.first);
197
198 try {
199 chgVal(var, val);
200 } catch (NotFound const&) {
201 GUM_ERROR(NotFound, var.name() << " : missing variable in instantiation")
202 }
203 } catch (NotFound const&) {
204 GUM_ERROR(NotFound, var.name() << " : missing variable in external instantiation")
205 }
206 }
207 }
208
209 void Instantiation::_masterChangeNotification_(Idx varPos, Idx newVal, Idx oldVal) const {
210 if (_master_) _master_->changeNotification(*this, _vars_[varPos], oldVal, newVal);
211 }
212
214 if (_master_) _master_->setFirstNotification(*this);
215 }
216
218 if (_master_) _master_->setIncNotification(*this);
219 }
220
222 if (_master_) _master_->setLastNotification(*this);
223 }
224
226 if (_master_) _master_->setDecNotification(*this);
227 }
228
229 // deassociate the master MultiDimAdressable, if any
231 if (_master_) {
232 _master_->unregisterSlave(*this);
233 _master_ = nullptr;
234 }
235 return true;
236 }
237
238 // force the variables sequence order to be the same as the master one
240 if (m != _master_) { GUM_ERROR(OperationNotAllowed, "only master can do this") }
241
242 _reorder_(_master_->variablesSequence());
243 }
244
245 // erase new dim by master
247 if (m != _master_) { GUM_ERROR(OperationNotAllowed, "only master can do this") }
248
249 _erase_(v);
250
251 if (_master_) _master_->setChangeNotification(*this);
252 }
253
254 // tries to register the Instantiation to a MultiDimAdressable
256 // if _master_ : not allowed
257 if (_master_ != nullptr) { GUM_ERROR(OperationNotAllowed, "in slave Instantiation") }
258
259 _master_ = &aMD;
260
261 // perform the registration
262 if (aMD.registerSlave(*this)) {
263 return true;
264 } else {
265 _master_ = nullptr;
266 return false;
267 }
268 }
269
270 // an operator for user-friendly displaying the content of a Instantiation
271 std::ostream& operator<<(std::ostream& aStream, const Instantiation& i) {
272 aStream << i.toString();
273 return aStream;
274 }
275
276} /* namespace gum */
Base class for discrete random variable.
The class for generic Hash Tables.
Definition hashTable.h:637
Class for assigning/browsing values to tuples of discrete variables.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
void synchronizeWithMaster(const MultiDimAdressable *m)
Force the variables sequence to be the same as the master one.
void _masterIncNotification_() const
Sequence< const DiscreteVariable * > _vars_
The tuple of variables to be instantiated.
bool actAsSlave(MultiDimAdressable &aMD)
Tries to register the Instantiation to a MultiDimAdressable.
bool isMaster(const MultiDimAdressable *m) const
Indicates whether m is the master of this instantiation.
void _masterFirstNotification_() const
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
std::vector< Idx > _vals_
The current instantiation: the value of the tuple.
bool _overflow_
Indicates whether the current value of the tuple is valid when we loop sufficiently over values of th...
void _reorder_(const Sequence< const DiscreteVariable * > &v)
Reorder vars of this instantiation giving the order in v.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
Instantiation()
Default constructor: creates an empty tuple.
MultiDimAdressable * _master_
The master, if any, contains precisely the set of variables to be instantiated.
void _init_(MultiDimAdressable *master)
Initialize this Instantiation.
~Instantiation()
Destructor.
Idx hamming() const
Returns the hamming distance of this instantiation.
void _erase_(const DiscreteVariable &v)
Removes a variable from the sequence of vars.
void _masterLastNotification_() const
void _add_(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
void _masterDecNotification_() const
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setValsFrom(const HashTable< const DiscreteVariable *, const DiscreteVariable * > &map, const Instantiation &external)
Assign the values of external in *this, using map as a bijection between external and this variables.
void _masterChangeNotification_(Idx varPos, Idx newVal, Idx oldVal) const
std::string toString() const
Give a string version of instantiation.
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
bool forgetMaster()
Deassociate the master MultiDimAdressable, if any.
void eraseWithMaster(const MultiDimAdressable *m, const DiscreteVariable &v)
Call Instantiation:: erase(const DiscreteVariable&) by master.
Instantiation & operator=(const Instantiation &aI)
Copy operator.
Abstract base class for all multi dimensionnal addressable.
virtual bool registerSlave(Instantiation &i)=0
Register i as a slave of this MultiDimAdressable.
virtual MultiDimAdressable & getMasterRef()=0
In order to insure the dereference for decorators, we need to virtualize the access to master pointer...
Interface for all classes addressing in a multiDim fashion.
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const =0
Returns a const ref to the sequence of DiscreteVariable*.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
Size size() const noexcept
Returns the size of the sequence.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79
Header files of gum::Instantiation.
Inline implemenation of gum::Instantiation.
Headers for the abstract base class for all multi dimensionnal containers.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Definition AVLTree.h:913