aGrUM  0.14.2
multiDimICIModel_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
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 or 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  ***************************************************************************/
30 #include <agrum/core/bijection.h>
33 
34 namespace gum {
35 
36  // Default constructor
37  template < typename GUM_SCALAR >
38  INLINE
40  GUM_SCALAR default_weight) :
41  MultiDimReadOnly< GUM_SCALAR >(),
42  __external_weight(external_weight), __default_weight(default_weight) {
43  GUM_CONSTRUCTOR(MultiDimICIModel);
44  }
45 
46  // Default constructor
47  template < typename GUM_SCALAR >
49  const MultiDimICIModel< GUM_SCALAR >& from) :
50  MultiDimReadOnly< GUM_SCALAR >(from) {
51  GUM_CONS_CPY(MultiDimICIModel);
55  }
56 
57  // Copy constructor using a bijection to replace variables from source.
58  template < typename GUM_SCALAR >
61  const MultiDimICIModel< GUM_SCALAR >& from) :
62  MultiDimReadOnly< GUM_SCALAR >() {
63  GUM_CONSTRUCTOR(MultiDimICIModel);
66 
68  from.__causal_weights.beginSafe();
69  iter != from.__causal_weights.endSafe();
70  ++iter) {
71  try {
72  causalWeight(*(bij.first(iter.key())), iter.val());
73  } catch (NotFound&) { causalWeight(*(iter.key()), iter.val()); }
74  }
75  }
76 
77  // destructor
78  template < typename GUM_SCALAR >
80  GUM_DESTRUCTOR(MultiDimICIModel);
81  }
82 
83  template < typename GUM_SCALAR >
85  const DiscreteVariable& v) const {
86  return (__causal_weights.exists(&v)) ? __causal_weights[&v] : __default_weight;
87  }
88 
89  template < typename GUM_SCALAR >
90  INLINE void
92  GUM_SCALAR w) const {
93  if (!this->contains(v)) {
94  GUM_ERROR(InvalidArgument, v.name() << " is not a cause for this CI Model");
95  }
96 
97  if (w == (GUM_SCALAR)0) {
98  GUM_ERROR(gum::OutOfBounds, "causal weight in CI Model>0");
99  }
100 
101  __causal_weights.set(&v, w);
102  }
103 
104  template < typename GUM_SCALAR >
106  return __external_weight;
107  }
108 
109  template < typename GUM_SCALAR >
110  INLINE void MultiDimICIModel< GUM_SCALAR >::externalWeight(GUM_SCALAR w) const {
111  __external_weight = w;
112  }
113 
114  template < typename GUM_SCALAR >
115  const std::string MultiDimICIModel< GUM_SCALAR >::toString() const {
116  std::stringstream s;
117  s << this->variable(0) << "=CIModel([" << externalWeight() << "],";
118 
119  for (Idx i = 1; i < this->nbrDim(); i++) {
120  s << this->variable(i) << "[" << causalWeight(this->variable(i)) << "]";
121  }
122 
123  s << ")";
124 
125  std::string res;
126  s >> res;
127  return res;
128  }
129  template < typename GUM_SCALAR >
131  const MultiDimContainer< GUM_SCALAR >& src) const {
132  auto p = dynamic_cast< const MultiDimICIModel< GUM_SCALAR >* >(&src);
133  if (p == nullptr)
135  else {
136  if (src.domainSize() != this->domainSize()) {
137  GUM_ERROR(OperationNotAllowed, "Domain sizes do not fit");
138  }
139  __external_weight = p->__external_weight;
140  __default_weight = p->__default_weight;
141  for (Idx i = 1; i < this->nbrDim(); i++) {
142  __causal_weights.set(
143  const_cast< const DiscreteVariable* >(&this->variable(i)),
144  p->causalWeight(this->variable(i)));
145  }
146  }
147  }
148 
149  // returns the name of the implementation
150  template < typename GUM_SCALAR >
151  INLINE const std::string& MultiDimICIModel< GUM_SCALAR >::name() const {
152  static const std::string str = "MultiDimICIModel";
153  return str;
154  }
155 
156  template < typename GUM_SCALAR >
158  const DiscreteVariable* y) {
160  __causal_weights.insert(y, __causal_weights[x]);
161  __causal_weights.erase(x);
162  }
163 
164 } /* namespace gum */
virtual ~MultiDimICIModel()
Destructor.
Header of the MultiDimReadOnly class.
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:1915
virtual const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
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.
Abstract base class for all multi dimensionnal Causal Independency models.
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:1803
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:50
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
Set of pairs of elements with fast search for both elements.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52