aGrUM  0.16.0
multiDimNoisyAND_tpl.h
Go to the documentation of this file.
1 
30 #include <agrum/core/exceptions.h>
32 
33 namespace gum {
34 
35  // Default constructor
36  template < typename GUM_SCALAR >
37  INLINE
39  GUM_SCALAR default_weight) :
40  MultiDimICIModel< GUM_SCALAR >(external_weight, default_weight) {
41  if (external_weight == 0) {
42  GUM_ERROR(InvalidArgument, "external weight can not be null for a NoisyAND");
43  }
44 
45  GUM_CONSTRUCTOR(MultiDimNoisyAND);
46  }
47 
48  // Default constructor
49  template < typename GUM_SCALAR >
51  const MultiDimNoisyAND< GUM_SCALAR >& from) :
52  MultiDimICIModel< GUM_SCALAR >(from) {
53  GUM_CONS_CPY(MultiDimNoisyAND);
54  }
55 
56  // Copy constructor using a bijection to replace variables from source.
57  template < typename GUM_SCALAR >
60  const MultiDimNoisyAND< GUM_SCALAR >& from) :
61  MultiDimICIModel< GUM_SCALAR >(bij, from) {
62  GUM_CONSTRUCTOR(MultiDimNoisyAND);
63  }
64 
65  // destructor
66  template < typename GUM_SCALAR >
68  GUM_DESTRUCTOR(MultiDimNoisyAND);
69  }
70 
71  template < typename GUM_SCALAR >
73  if (this->nbrDim() < 1) {
74  GUM_ERROR(OperationNotAllowed, "Not enough variable for a NoisyAND ");
75  }
76 
77  const DiscreteVariable& C = this->variable((Idx)0);
78 
79  if (i.val(C) > 1) return (GUM_SCALAR)0.0;
80 
81  GUM_SCALAR fact = this->externalWeight();
82 
83  for (Idx j = 1; j < this->nbrDim(); j++) {
84  const DiscreteVariable& v = this->variable(j);
85 
86  if (i.val(v) == 0) {
87  fact *= ((GUM_SCALAR)1.0 - this->causalWeight(v));
88  } else {
89  fact *= this->causalWeight(v);
90  }
91  }
92 
93  return (i.val(C) == 1) ? fact : (GUM_SCALAR)1.0 - fact;
94  }
95 
96  template < typename GUM_SCALAR >
97  const std::string MultiDimNoisyAND< GUM_SCALAR >::toString() const {
98  std::stringstream s;
99  s << MultiDimImplementation< GUM_SCALAR >::variable(0) << "=noisyAND(["
100  << this->externalWeight() << "],";
101 
102  for (Idx i = 1; i < MultiDimImplementation< GUM_SCALAR >::nbrDim(); i++) {
103  s << MultiDimImplementation< GUM_SCALAR >::variable(i) << "["
105  << "]";
106  }
107 
108  s << ")";
109 
110  std::string res;
111  s >> res;
112  return res;
113  }
114 
115  // For friendly displaying the content of the variable.
116 
117  template < typename GUM_SCALAR >
118  INLINE std::ostream& operator<<(std::ostream& s,
119  const MultiDimNoisyAND< GUM_SCALAR >& ag) {
120  return s << ag.toString();
121  }
122 
123  template < typename GUM_SCALAR >
127  this->__default_weight);
128  }
129 
130  // returns the name of the implementation
131  template < typename GUM_SCALAR >
132  INLINE const std::string& MultiDimNoisyAND< GUM_SCALAR >::name() const {
133  static const std::string str = "MultiDimNoisyAND";
134  return str;
135  }
136 
137 } /* namespace gum */
virtual const std::string & name() const
Returns the real name of the multiDimArray.
MultiDimNoisyAND(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
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.
GUM_SCALAR __external_weight
in Henrion (89).
Idx val(Idx i) const
Returns the current value of the variable at position i.
GUM_SCALAR externalWeight() const
Copy of a multiDimICIModel.
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the real name of the multiDimArray.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
abstract class for Conditional Indepency Models
const std::string toString() const
Returns the real name of the multiDimArray.
GUM_SCALAR __default_weight
in Henrion (89) in a hashtable with a default_value.
virtual ~MultiDimNoisyAND()
Destructor.
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
Noisy AND representation.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
This method creates a clone of this object, withouth its content (including variable), you must use this method if you want to ensure that the generated object has the same type than the object containing the called newFactory()
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:53
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55