aGrUM  0.14.2
PRMScalarAttribute_tpl.h
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY || FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #include <sstream>
28 
29 #include <agrum/PRM/utils_prm.h>
30 
33 
34 namespace gum {
35  namespace prm {
36 
37  template < typename GUM_SCALAR >
39  const std::string& name,
40  const PRMType& type,
42  PRMAttribute< GUM_SCALAR >(name),
43  __type(new PRMType(type)), __cpf(new Potential< GUM_SCALAR >(impl)) {
44  GUM_CONSTRUCTOR(PRMScalarAttribute);
45  __cpf->add(__type->variable());
46 
47  this->_safeName =
49  }
50 
51  template < typename GUM_SCALAR >
53  const PRMScalarAttribute< GUM_SCALAR >& source) :
54  PRMAttribute< GUM_SCALAR >(source),
55  __type(0), __cpf(0) {
56  GUM_CONS_CPY(PRMScalarAttribute);
58  "Illegal call to the copy constructor of gum::ScalarAttribute");
59  }
60 
61  template < typename GUM_SCALAR >
63  GUM_DESTRUCTOR(PRMScalarAttribute);
64  delete __cpf;
65  delete __type;
66  }
67 
68  template < typename GUM_SCALAR >
70  const PRMClass< GUM_SCALAR >& c) const {
71  auto impl = static_cast< MultiDimImplementation< GUM_SCALAR >* >(
72  this->cpf().content()->newFactory());
74  this->name(), this->type(), impl);
75  }
76 
77  template < typename GUM_SCALAR >
80  auto copy = new PRMScalarAttribute< GUM_SCALAR >(this->name(), this->type());
81 
82  if (!bij.existsFirst(&(type().variable()))) {
83  bij.insert(&(type().variable()), &(copy->type().variable()));
84  }
85 
86  delete copy->__cpf;
87  copy->__cpf = copyPotential(bij, cpf());
88 
89  return copy;
90  }
91 
92  template < typename GUM_SCALAR >
95  const PRMAttribute< GUM_SCALAR >& source) {
96  delete __cpf;
98 
99  for (auto var : source.cpf().variablesSequence()) {
100  __cpf->add(*(bij.second(var)));
101  }
102 
103  Instantiation inst(*__cpf), jnst(source.cpf());
104 
105  for (inst.setFirst(), jnst.setFirst(); !(inst.end() || jnst.end());
106  inst.inc(), jnst.inc()) {
107  __cpf->set(inst, source.cpf().get(jnst));
108  }
109 
110  GUM_ASSERT(inst.end() && jnst.end());
111  GUM_ASSERT(__cpf->contains(__type->variable()));
112  GUM_ASSERT(!__cpf->contains(source.type().variable()));
113  }
114 
115  template < typename GUM_SCALAR >
119  "Illegal call to the copy operator of gum::ScalarAttribute");
120  }
121 
122  template < typename GUM_SCALAR >
125  return this->prm_attribute;
126  }
127 
128  template < typename GUM_SCALAR >
130  return *__type;
131  }
132 
133  template < typename GUM_SCALAR >
135  return *__type;
136  }
137 
138  template < typename GUM_SCALAR >
139  INLINE const Potential< GUM_SCALAR >&
141  return *__cpf;
142  }
143 
144  template < typename GUM_SCALAR >
146  const PRMClassElement< GUM_SCALAR >& elt) {
147  try {
148  __cpf->add(elt.type().variable());
149  } catch (DuplicateElement&) {
151  elt.name() << " as parent of " << this->name());
152  } catch (OperationNotAllowed&) {
154  elt.name() << " of wrong type as parent of " << this->name());
155  }
156  }
157 
158  // See gum::PRMClassElement<GUM_SCALAR>::_addChild().
159  template < typename GUM_SCALAR >
161  const PRMClassElement< GUM_SCALAR >& elt) {}
162 
163  template < typename GUM_SCALAR >
167 
168  try {
169  cast =
171  } catch (NotFound&) {
173  "this ScalarAttribute can not have cast descendant");
174  }
175 
176  cast->addParent(*this);
177  const DiscreteVariable& my_var = type().variable();
178  DiscreteVariable& cast_var = cast->type().variable();
179  Instantiation inst(cast->cpf());
180 
181  for (inst.setFirst(); !inst.end(); inst.inc()) {
182  if (type().label_map()[inst.val(my_var)] == inst.val(cast_var)) {
183  cast->cpf().set(inst, 1);
184  } else {
185  cast->cpf().set(inst, 0);
186  }
187  }
188 
189  return cast;
190  }
191 
192  template < typename GUM_SCALAR >
195  try {
196  type().setSuper(cast->type());
197  } catch (OperationNotAllowed&) {
199  "this ScalarAttribute can not have cast descendant");
200  } catch (WrongType&) {
201  std::stringstream msg;
202  msg << type().name() << " is not a subtype of " << cast->type().name();
203  GUM_ERROR(WrongType, msg.str());
204  }
205  cast->becomeCastDescendant(type());
206  }
207 
208  template < typename GUM_SCALAR >
210  delete __cpf;
212  __cpf->add(type().variable());
213  __cpf->add(subtype.variable());
214 
215  Instantiation inst(*__cpf);
216 
217  for (inst.setFirst(); !inst.end(); inst.inc()) {
218  auto my_pos = inst.pos(subtype.variable());
219  if (subtype.label_map()[my_pos] == inst.pos(type().variable())) {
220  __cpf->set(inst, 1);
221  } else {
222  __cpf->set(inst, 0);
223  }
224  }
225  }
226 
227  template < typename GUM_SCALAR >
229  const PRMType& new_type) {
230  if (&(old_type) == __type) {
231  GUM_ERROR(OperationNotAllowed, "Cannot replace attribute own type");
232  }
233  if (old_type->domainSize() != new_type->domainSize()) {
235  "Cannot replace types with difference domain size");
236  }
237  if (!__cpf->contains(old_type.variable())) {
238  GUM_ERROR(NotFound, "could not find variable " + old_type.name());
239  }
240 
241  auto old = __cpf;
242 
244 
245  for (auto var : old->variablesSequence()) {
246  if (var != &(old_type.variable())) {
247  __cpf->add(*var);
248  } else {
249  __cpf->add(new_type.variable());
250  }
251  }
252 
253  Instantiation inst(__cpf), jnst(old);
254 
255  for (inst.setFirst(), jnst.setFirst(); !(inst.end() || jnst.end());
256  inst.inc(), jnst.inc()) {
257  __cpf->set(inst, old->get(jnst));
258  }
259 
260  delete old;
261 
262  GUM_ASSERT(inst.end() && jnst.end());
263  GUM_ASSERT(__cpf->contains(__type->variable()));
264  GUM_ASSERT(__cpf->contains(new_type.variable()));
265  GUM_ASSERT(!__cpf->contains(old_type.variable()));
266  }
267 
268  template < typename GUM_SCALAR >
270  return __type;
271  }
272 
273  template < typename GUM_SCALAR >
275  if (__type->variable().domainSize() != t->variable().domainSize()) {
277  "Cannot replace types with difference domain size");
278  }
279  auto old = __cpf;
280 
282 
283  for (auto var : old->variablesSequence()) {
284  if (var != &(__type->variable())) {
285  __cpf->add(*var);
286  } else {
287  __cpf->add(t->variable());
288  }
289  }
290 
291  Instantiation inst(__cpf), jnst(old);
292 
293  for (inst.setFirst(), jnst.setFirst(); !(inst.end() || jnst.end());
294  inst.inc(), jnst.inc()) {
295  __cpf->set(inst, old->get(jnst));
296  }
297 
298  delete old;
299 
300  __type = t;
301 
302  GUM_ASSERT(__cpf->contains(__type->variable()));
303  GUM_ASSERT(inst.end() && jnst.end());
304  }
305 
306  } /* namespace prm */
307 } /* namespace gum */
virtual void addChild(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::_addChild().
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
const std::string & name() const
Returns the name of this object.
Definition: PRMType_inl.h:65
virtual void becomeCastDescendant(PRMType &subtype)
See gum::PRMClassElement::elt_type().
PRMType & superType()
Returns the super type of this type.
Definition: PRMType_inl.h:33
void setSuper(PRMType &t)
Changes the PRMType of this PRMType super.
Definition: PRMType_inl.h:77
virtual ~PRMScalarAttribute()
Destructor.
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:42
virtual void swap(const PRMType &old_type, const PRMType &new_type)
Swap old_type with new_type in the PRMClassElement cpt.
Idx pos(const DiscreteVariable &v) const final
Returns the position of the variable v.
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:32
virtual PRMType & type()=0
See gum::PRMClassElement::type().
Abstract class representing an element of PRM class.
Base class for discrete random variable.
virtual PRMType & type()
See gum::PRMClassElement::type().
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
virtual PRMAttribute< GUM_SCALAR > * newFactory(const PRMClass< GUM_SCALAR > &c) const
See gum::PRMClassElement::elt_type().
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const
See gum::PRMClassElement::elt_type().
virtual void copyCpf(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bif, const PRMAttribute< GUM_SCALAR > &source)
See gum::PRMClassElement::elt_type().
virtual void becomeCastDescendant(PRMType &subtype)=0
Change this attribute to be a cast descendant of a an attribute with type subtype.
virtual std::string cast(const PRMType &t) const
Returns the name of the cast descendant with PRMType t of this PRMClassElement.
virtual Size domainSize() const =0
bool existsFirst(const T1 &first) const
Returns true if first is the first element in a pair in the gum::Bijection.
virtual const Potential< GUM_SCALAR > & cpf() const
See gum::PRMClassElement::cpf().
void inc()
Operator increment.
PRMType * __type
The random variable type of this attribute.
virtual const Potential< GUM_SCALAR > & cpf() const =0
See gum::PRMClassElement::cpf().
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
Definition: utils_prm_tpl.h:27
This is a decoration of the DiscreteVariable class.
Definition: PRMType.h:60
virtual PRMType & type()=0
Return a reference over the gum::PRMType of this class element.
virtual PRMAttribute< GUM_SCALAR > * copy(Bijection< const DiscreteVariable *, const DiscreteVariable * > bij) const
See gum::PRMClassElement::elt_type().
Headers of Class.
Headers of gum::PRMAttribute.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
static std::string LEFT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition: PRMObject.h:76
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:63
virtual void setAsCastDescendant(PRMAttribute< GUM_SCALAR > *attr)
See gum::PRMClassElement::elt_type().
Potential< GUM_SCALAR > * __cpf
A pointer on the Potential of this attribute.
void setFirst()
Assign the first values to the tuple of the Instantiation.
const std::vector< Idx > & label_map() const
Returns the vector in which the i-th element is the Idx of the super type&#39;s label for the i-th label ...
Definition: PRMType_inl.h:91
virtual PRMAttribute< GUM_SCALAR > * getCastDescendant() const
See gum::PRMClassElement::elt_type().
<agrum/multidim/multiDimImplementation.h>
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:58
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::_addParent().
const std::string & name() const
returns the name of the variable
static std::string RIGHT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition: PRMObject.h:77
<agrum/PRM/elements/scalarAttribute.h>
PRMScalarAttribute(const std::string &name, const PRMType &type, MultiDimImplementation< GUM_SCALAR > *impl=new MultiDimArray< GUM_SCALAR >())
Constructor used by gum::Class.
std::string _safeName
The safe name of this PRMClassElement.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
PRMScalarAttribute & operator=(const PRMScalarAttribute &from)
Copy operator. Don&#39;t use it.
bool end() const
Returns true if the Instantiation reached the end.
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.