aGrUM  0.14.2
multiDimNoisyAND_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  ***************************************************************************/
27 #include <agrum/core/exceptions.h>
29 
30 namespace gum {
31 
32  // Default constructor
33  template < typename GUM_SCALAR >
34  INLINE
36  GUM_SCALAR default_weight) :
37  MultiDimICIModel< GUM_SCALAR >(external_weight, default_weight) {
38  if (external_weight == 0) {
39  GUM_ERROR(InvalidArgument, "external weight can not be null for a NoisyAND");
40  }
41 
42  GUM_CONSTRUCTOR(MultiDimNoisyAND);
43  }
44 
45  // Default constructor
46  template < typename GUM_SCALAR >
48  const MultiDimNoisyAND< GUM_SCALAR >& from) :
49  MultiDimICIModel< GUM_SCALAR >(from) {
50  GUM_CONS_CPY(MultiDimNoisyAND);
51  }
52 
53  // Copy constructor using a bijection to replace variables from source.
54  template < typename GUM_SCALAR >
57  const MultiDimNoisyAND< GUM_SCALAR >& from) :
58  MultiDimICIModel< GUM_SCALAR >(bij, from) {
59  GUM_CONSTRUCTOR(MultiDimNoisyAND);
60  }
61 
62  // destructor
63  template < typename GUM_SCALAR >
65  GUM_DESTRUCTOR(MultiDimNoisyAND);
66  }
67 
68  template < typename GUM_SCALAR >
70  if (this->nbrDim() < 1) {
71  GUM_ERROR(OperationNotAllowed, "Not enough variable for a NoisyAND ");
72  }
73 
74  const DiscreteVariable& C = this->variable((Idx)0);
75 
76  if (i.val(C) > 1) return (GUM_SCALAR)0.0;
77 
78  GUM_SCALAR fact = this->externalWeight();
79 
80  for (Idx j = 1; j < this->nbrDim(); j++) {
81  const DiscreteVariable& v = this->variable(j);
82 
83  if (i.val(v) == 0) {
84  fact *= ((GUM_SCALAR)1.0 - this->causalWeight(v));
85  } else {
86  fact *= this->causalWeight(v);
87  }
88  }
89 
90  return (i.val(C) == 1) ? fact : (GUM_SCALAR)1.0 - fact;
91  }
92 
93  template < typename GUM_SCALAR >
94  const std::string MultiDimNoisyAND< GUM_SCALAR >::toString() const {
95  std::stringstream s;
96  s << MultiDimImplementation< GUM_SCALAR >::variable(0) << "=noisyAND(["
97  << this->externalWeight() << "],";
98 
99  for (Idx i = 1; i < MultiDimImplementation< GUM_SCALAR >::nbrDim(); i++) {
100  s << MultiDimImplementation< GUM_SCALAR >::variable(i) << "["
102  << "]";
103  }
104 
105  s << ")";
106 
107  std::string res;
108  s >> res;
109  return res;
110  }
111 
112  // For friendly displaying the content of the variable.
113 
114  template < typename GUM_SCALAR >
115  INLINE std::ostream& operator<<(std::ostream& s,
116  const MultiDimNoisyAND< GUM_SCALAR >& ag) {
117  return s << ag.toString();
118  }
119 
120  template < typename GUM_SCALAR >
124  this->__default_weight);
125  }
126 
127  // returns the name of the implementation
128  template < typename GUM_SCALAR >
129  INLINE const std::string& MultiDimNoisyAND< GUM_SCALAR >::name() const {
130  static const std::string str = "MultiDimNoisyAND";
131  return str;
132  }
133 
134 } /* 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.
gum is the global namespace for all aGrUM entities
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:583
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the real name of the multiDimArray.
aGrUM&#39;s exceptions
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:1803
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:80
class for NoisyAND-net implementation as multiDim
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52