aGrUM  0.16.0
multiDimICIModel_tpl.h
Go to the documentation of this file.
1 
33 #include <agrum/core/bijection.h>
36 
37 namespace gum {
38 
39  // Default constructor
40  template < typename GUM_SCALAR >
41  INLINE
43  GUM_SCALAR default_weight) :
44  MultiDimReadOnly< GUM_SCALAR >(),
45  __external_weight(external_weight), __default_weight(default_weight) {
46  GUM_CONSTRUCTOR(MultiDimICIModel);
47  }
48 
49  // Default constructor
50  template < typename GUM_SCALAR >
52  const MultiDimICIModel< GUM_SCALAR >& from) :
53  MultiDimReadOnly< GUM_SCALAR >(from) {
54  GUM_CONS_CPY(MultiDimICIModel);
58  }
59 
60  // Copy constructor using a bijection to replace variables from source.
61  template < typename GUM_SCALAR >
64  const MultiDimICIModel< GUM_SCALAR >& from) :
65  MultiDimReadOnly< GUM_SCALAR >() {
66  GUM_CONSTRUCTOR(MultiDimICIModel);
69 
71  from.__causal_weights.beginSafe();
72  iter != from.__causal_weights.endSafe();
73  ++iter) {
74  try {
75  causalWeight(*(bij.first(iter.key())), iter.val());
76  } catch (NotFound&) { causalWeight(*(iter.key()), iter.val()); }
77  }
78  }
79 
80  // destructor
81  template < typename GUM_SCALAR >
83  GUM_DESTRUCTOR(MultiDimICIModel);
84  }
85 
86  template < typename GUM_SCALAR >
88  const DiscreteVariable& v) const {
89  return (__causal_weights.exists(&v)) ? __causal_weights[&v] : __default_weight;
90  }
91 
92  template < typename GUM_SCALAR >
93  INLINE void
95  GUM_SCALAR w) const {
96  if (!this->contains(v)) {
97  GUM_ERROR(InvalidArgument, v.name() << " is not a cause for this CI Model");
98  }
99 
100  if (w == (GUM_SCALAR)0) {
101  GUM_ERROR(gum::OutOfBounds, "causal weight in CI Model>0");
102  }
103 
104  __causal_weights.set(&v, w);
105  }
106 
107  template < typename GUM_SCALAR >
109  return __external_weight;
110  }
111 
112  template < typename GUM_SCALAR >
113  INLINE void MultiDimICIModel< GUM_SCALAR >::externalWeight(GUM_SCALAR w) const {
114  __external_weight = w;
115  }
116 
117  template < typename GUM_SCALAR >
118  const std::string MultiDimICIModel< GUM_SCALAR >::toString() const {
119  std::stringstream s;
120  s << this->variable(0) << "=CIModel([" << externalWeight() << "],";
121 
122  for (Idx i = 1; i < this->nbrDim(); i++) {
123  s << this->variable(i) << "[" << causalWeight(this->variable(i)) << "]";
124  }
125 
126  s << ")";
127 
128  std::string res;
129  s >> res;
130  return res;
131  }
132  template < typename GUM_SCALAR >
134  const MultiDimContainer< GUM_SCALAR >& src) const {
135  auto p = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(&src);
136  if (p == nullptr)
138  else {
139  if (src.domainSize() != this->domainSize()) {
140  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit");
141  }
142  __external_weight = p->__external_weight;
143  __default_weight = p->__default_weight;
144  for (Idx i = 1; i < this->nbrDim(); i++) {
145  __causal_weights.set(
146  const_cast< const DiscreteVariable* >(&this->variable(i)),
147  p->causalWeight(this->variable(i)));
148  }
149  }
150  }
151 
152  // returns the name of the implementation
153  template < typename GUM_SCALAR >
154  INLINE const std::string& MultiDimICIModel< GUM_SCALAR >::name() const {
155  static const std::string str = "MultiDimICIModel";
156  return str;
157  }
158 
159  template < typename GUM_SCALAR >
161  const DiscreteVariable* y) {
163  __causal_weights.insert(y, __causal_weights[x]);
164  __causal_weights.erase(x);
165  }
166 
167 } /* namespace gum */
virtual ~MultiDimICIModel()
Destructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< const DiscreteVariable *, GUM_SCALAR > __causal_weights
in Henrion (89) in a hashtable with a default_value.
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
Safe Const Iterators for hashtables.
Definition: hashTable.h:1918
virtual const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
Abstract base class for all multi dimensionnal read only structure.
GUM_SCALAR __external_weight
in Henrion (89).
GUM_SCALAR externalWeight() const
Copy of a multiDimICIModel.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
abstract class for Conditional Indepency Models
virtual Size domainSize() const =0
Returns the product of the variables domain size.
GUM_SCALAR __default_weight
in Henrion (89) in a hashtable with a default_value.
virtual const std::string & name() const override
returns the real name of the multiDimArray
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
virtual void _replace(const DiscreteVariable *x, const DiscreteVariable *y) override
Replace variable x by y.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const override
Copy of a multiDimICIModel.
const std::string toString() const override
Copy of a multiDimICIModel.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
virtual Size domainSize() const override
Returns the product of the variables domain size.
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
MultiDimICIModel(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
Size Idx
Type for indexes.
Definition: types.h:53
virtual bool contains(const DiscreteVariable &v) const override
Returns true if var is in *this.
const std::string & name() const
returns the name of the variable
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55