aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
setInst.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
42#include <sstream>
43
46
47#ifdef GUM_NO_INLINE
49#endif
50
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53namespace gum {
54
56 // for speed issues
57 const Sequence< const DiscreteVariable* >& v = master->variablesSequence();
58 _vars_.resize(v.size());
59 _vals_.reserve(v.size());
60 // fill the SetInst
61
62 for (const auto var: v)
63 _add_(*var);
64
65 // if ( master ) actAsSlave( master->getMasterRef() );
66 }
67
68 // constructor for a SetInst contained into a MultiDimInterface
69
70 SetInst::SetInst(MultiDimAdressable& d) : /* _master_( 0 ),*/ _overflow_(false) {
71 // for debugging purposes
72 GUM_CONSTRUCTOR(SetInst);
73 _init_(&d);
74 }
75
76 SetInst::SetInst(const MultiDimAdressable& d) : /* _master_( 0 ),*/ _overflow_(false) {
77 // for debugging purposes
78 GUM_CONSTRUCTOR(SetInst);
79 _init_(const_cast< MultiDimAdressable* >(&d));
80 }
81
82 // constructor for a SetInst contained into a MultiDimInterface
83
84 SetInst::SetInst(MultiDimAdressable* d) : /* _master_( 0 ),*/ _overflow_(false) {
85 // for debugging purposes
86 GUM_CONSTRUCTOR(SetInst);
87
88 if (d) _init_(d);
89 }
90
91 // constructor for a SetInst contained into a MultiDimInterface
94
95 SetInst::SetInst(const MultiDimAdressable* const_d) : /* _master_( 0 ),*/ _overflow_(false) {
96 // for debugging purposes
97 GUM_CONSTRUCTOR(SetInst);
98
99 if (const_d) _init_(const_cast< MultiDimAdressable* >(const_d));
100 }
101
102 // copy constructor
103
104 SetInst::SetInst(const SetInst& aI) :
105 /* MultiDimInterface(), _master_( 0 ),*/ _overflow_(false) {
106 // for debugging purposes
107 GUM_CONS_CPY(SetInst);
108 // copy the content of aI
109 _vars_ = aI._vars_;
110 _vals_ = aI._vals_;
111 _overflow_ = aI._overflow_;
112
113 // if ( aI. _master_ && notifyMaster ) actAsSlave( *aI. _master_ );
114 }
115
116 SetInst::SetInst(const Instantiation& aI) :
117 /* MultiDimInterface(), _master_( 0 ),*/ _overflow_(false) {
118 // for debugging purposes
119 GUM_CONS_CPY(SetInst);
120 const Sequence< const DiscreteVariable* >& v = aI.variablesSequence();
121 _vars_.resize(v.size());
122 // __vals.reserve( v.size() );
123 // fill the SetInst
124
125 for (const auto var: v) {
126 _add_(*var);
127 _vals_[_vars_.pos(var)] = (Size(1) << (aI.val(*var)));
128 }
129 }
130
131 // operator=
132 SetInst& SetInst::operator=(const SetInst& aI) {
133 // copy the content of aI
134 _vars_ = aI._vars_;
135 _vals_ = aI._vals_;
136 _overflow_ = aI._overflow_;
137
138 return *this;
139 }
140
141 // Gives a string version of a SetInst
142 std::string SetInst::toString() const {
143 std::stringstream sstr;
144 // check if the value of the SetInst is correct
145
146 if (_overflow_) { sstr << "<invalid>"; }
147
148 sstr << "<";
149
150 Sequence< const DiscreteVariable* >::iterator_safe iter = _vars_.begin();
151
152 if (iter != _vars_.end()) {
153 std::stringstream sstr2;
154 sstr2.str("");
155 Size si = variable(iter.pos()).domainSize();
156 Size valb = vals(iter.pos());
157
158 while (si-- != 0) {
159 std::stringstream sstr4;
160 sstr4 << ((valb & 1) ? "1" : "0") << sstr2.str();
161 valb >>= 1;
162 sstr2.str("");
163 ;
164 sstr2 << sstr4.str();
165 }
166
167 sstr << variable(iter.pos()).name() << ":" << sstr2.str();
168 ++iter;
169
170 while (iter != _vars_.end()) {
171 std::stringstream sstr3;
172 sstr3 << "";
173 si = variable(iter.pos()).domainSize();
174
175 valb = vals(iter.pos());
176
177 while (si-- != 0) {
178 std::stringstream sstr4;
179
180 sstr4 << ((valb & 1) ? "1" : "0") << sstr3.str();
181 valb >>= 1;
182 sstr3.str("");
183 sstr3 << sstr4.str();
184 }
185
186 sstr << "|" << variable(iter.pos()).name() << ":" << sstr3.str();
187 ++iter;
188 }
189 }
190
191 sstr << ">";
192
193 return sstr.str();
194 }
195
196 // an operator for user-friendly displaying the content of a SetInst
197 std::ostream& operator<<(std::ostream& aStream, const SetInst& i) {
198 aStream << i.toString();
199 return aStream;
200 }
201
203 inst.add(i);
204 return inst;
205 }
206
208 inst.erase(i);
209 return inst;
210 }
211
214 const gum::SetInst& i,
215 gum::SetInst& j) {
216 try {
217 for (const auto var: i.variablesSequence())
218 j.chgVal(bij.second(var), i.val(var));
219 } catch (gum::NotFound&) {
220 GUM_ERROR(gum::NotFound, "missing variable in bijection or SetInst")
221 }
222 }
223
224} /* namespace gum */
225
226#endif // DOXYGEN_SHOULD_SKIP_THIS
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
Base class for discrete random variable.
Abstract base class for all multi dimensionnal addressable.
Class for assigning/browsing values to tuples of discrete variables.
Definition setInst.h:101
SetInst()
Default constructor: creates an empty tuple.
void erase(const DiscreteVariable &v)
Removes a variable from the SetInst.
Sequence< const DiscreteVariable * > _vars_
The tuple of variables to be instantiated.
Definition setInst.h:864
static void assign_values(Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const SetInst &i, SetInst &j)
Assign the values of i in j, using bij as a bijection between i and j variables.
const Sequence< const DiscreteVariable * > & variablesSequence() const
Returns the sequence of DiscreteVariable of this SetInst.
void _add_(const DiscreteVariable &v)
Adds a new var to the sequence of vars.
Idx val(Idx i) const
Returns the current value of a variable at a given position.
void _init_(MultiDimAdressable *master)
Intialize the SetInst.
void add(const DiscreteVariable &v)
Adds a new variable in the SetInst.
std::vector< Size > _vals_
The current SetInst: the value of the tuple.
Definition setInst.h:867
SetInst & chgVal(const DiscreteVariable &v, Idx newVal)
Assign newVal to variable v in the SetInst.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Headers for the abstract base class for all multi dimensionnal containers.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Headers of SetInst.
std::istream & operator>>(std::istream &in, TiXmlNode &base)
Definition tinyxml.cpp:1505
std::ostream & operator<<(std::ostream &out, const TiXmlNode &base)
Definition tinyxml.cpp:1516