aGrUM 3.1.1
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-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
44
45#ifdef GUM_NO_INLINE
47#endif
48
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51namespace gum {
52
54 // for speed issues
55 const Sequence< const DiscreteVariable* >& v = master->variablesSequence();
56 _vars_.resize(v.size());
57 _vals_.reserve(v.size());
58 // fill the SetInst
59
60 for (const auto var: v)
61 _add_(*var);
62
63 // if ( master ) actAsSlave( master->getMasterRef() );
64 }
65
66 // constructor for a SetInst contained into a MultiDimInterface
67
68 SetInst::SetInst(MultiDimAdressable& d) : /* _master_( 0 ),*/ _overflow_(false) {
69 // for debugging purposes
70 GUM_CONSTRUCTOR(SetInst);
71 _init_(&d);
72 }
73
74 SetInst::SetInst(const MultiDimAdressable& d) : /* _master_( 0 ),*/ _overflow_(false) {
75 // for debugging purposes
76 GUM_CONSTRUCTOR(SetInst);
77 _init_(const_cast< MultiDimAdressable* >(&d));
78 }
79
80 // constructor for a SetInst contained into a MultiDimInterface
81
82 SetInst::SetInst(MultiDimAdressable* d) : /* _master_( 0 ),*/ _overflow_(false) {
83 // for debugging purposes
84 GUM_CONSTRUCTOR(SetInst);
85
86 if (d) _init_(d);
87 }
88
89 // constructor for a SetInst contained into a MultiDimInterface
92
93 SetInst::SetInst(const MultiDimAdressable* const_d) : /* _master_( 0 ),*/ _overflow_(false) {
94 // for debugging purposes
95 GUM_CONSTRUCTOR(SetInst);
96
97 if (const_d) _init_(const_cast< MultiDimAdressable* >(const_d));
98 }
99
100 // copy constructor
101
102 SetInst::SetInst(const SetInst& aI) :
103 /* MultiDimInterface(), _master_( 0 ),*/ _overflow_(false) {
104 // for debugging purposes
105 GUM_CONS_CPY(SetInst);
106 // copy the content of aI
107 _vars_ = aI._vars_;
108 _vals_ = aI._vals_;
109 _overflow_ = aI._overflow_;
110
111 // if ( aI. _master_ && notifyMaster ) actAsSlave( *aI. _master_ );
112 }
113
114 SetInst::SetInst(SetInst&& aI) noexcept :
115 _vars_(std::move(aI._vars_)), _vals_(std::move(aI._vals_)), _overflow_(aI._overflow_) {
116 GUM_CONS_MOV(SetInst);
117 }
118
119 SetInst::SetInst(const Instantiation& aI) :
120 /* MultiDimInterface(), _master_( 0 ),*/ _overflow_(false) {
121 // for debugging purposes
122 GUM_CONS_CPY(SetInst);
123 const Sequence< const DiscreteVariable* >& v = aI.variablesSequence();
124 _vars_.resize(v.size());
125 // __vals.reserve( v.size() );
126 // fill the SetInst
127
128 for (const auto var: v) {
129 _add_(*var);
130 _vals_[_vars_.pos(var)] = (Size(1) << (aI.val(*var)));
131 }
132 }
133
134 // operator=
135 SetInst& SetInst::operator=(const SetInst& aI) {
136 // copy the content of aI
137 _vars_ = aI._vars_;
138 _vals_ = aI._vals_;
139 _overflow_ = aI._overflow_;
140 GUM_OP_CPY(SetInst);
141
142 return *this;
143 }
144
145 // move operator=
146 SetInst& SetInst::operator=(SetInst&& aI) noexcept {
147 _vars_ = std::move(aI._vars_);
148 _vals_ = std::move(aI._vals_);
149 _overflow_ = aI._overflow_;
150 GUM_OP_MOV(SetInst);
151
152 return *this;
153 }
154
155 // Gives a string version of a SetInst
156 std::string SetInst::toString() const {
157 std::string result;
158 // check if the value of the SetInst is correct
159
160 if (_overflow_) { result += "<invalid>"; }
161
162 result += "<";
163
164 Sequence< const DiscreteVariable* >::iterator_safe iter = _vars_.begin();
165
166 if (iter != _vars_.end()) {
167 Size si = variable(iter.pos()).domainSize();
168 Size valb = vals(iter.pos());
169 std::string bits;
170
171 while (si-- != 0) {
172 bits = std::string(1, (valb & 1) ? '1' : '0') + bits;
173 valb >>= 1;
174 }
175
176 result += variable(iter.pos()).name() + ":" + bits;
177 ++iter;
178
179 while (iter != _vars_.end()) {
180 si = variable(iter.pos()).domainSize();
181 valb = vals(iter.pos());
182 bits = "";
183
184 while (si-- != 0) {
185 bits = std::string(1, (valb & 1) ? '1' : '0') + bits;
186 valb >>= 1;
187 }
188
189 result += "|" + variable(iter.pos()).name() + ":" + bits;
190 ++iter;
191 }
192 }
193
194 result += ">";
195
196 return result;
197 }
198
199 // an operator for user-friendly displaying the content of a SetInst
200 std::ostream& operator<<(std::ostream& aStream, const SetInst& i) {
201 aStream << i.toString();
202 return aStream;
203 }
204
206 inst.add(i);
207 return inst;
208 }
209
211 inst.erase(i);
212 return inst;
213 }
214
217 const gum::SetInst& i,
218 gum::SetInst& j) {
219 try {
220 for (const auto var: i.variablesSequence())
221 j.chgVal(bij.second(var), i.val(var));
222 } catch (gum::NotFound&) {
223 GUM_ERROR(gum::NotFound, "missing variable in bijection or SetInst")
224 }
225 }
226
227} /* namespace gum */
228
229#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:1640
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:873
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:876
SetInst & chgVal(const DiscreteVariable &v, Idx newVal)
Assign newVal to variable v in the SetInst.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
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