aGrUM  0.14.2
multiDimNoisyORCompound_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 
28 namespace gum {
29 
31 
32  template < typename GUM_SCALAR >
34  GUM_SCALAR external_weight, GUM_SCALAR default_weight) :
35  MultiDimICIModel< GUM_SCALAR >(external_weight, default_weight) {
36  GUM_CONSTRUCTOR(MultiDimNoisyORCompound);
37  }
38 
40 
41  template < typename GUM_SCALAR >
44  MultiDimICIModel< GUM_SCALAR >(from) {
45  GUM_CONS_CPY(MultiDimNoisyORCompound);
46  }
47 
49 
50  template < typename GUM_SCALAR >
54  MultiDimICIModel< GUM_SCALAR >(bij, from) {
55  GUM_CONSTRUCTOR(MultiDimNoisyORCompound);
56  }
57 
59 
60  template < typename GUM_SCALAR >
62  GUM_DESTRUCTOR(MultiDimNoisyORCompound);
63  }
64 
65  template < typename GUM_SCALAR >
66  GUM_SCALAR
68  if (this->nbrDim() < 1) {
69  GUM_ERROR(OperationNotAllowed, "Not enough variable for a NoisyOr ");
70  }
71 
72  const DiscreteVariable& C = this->variable((Idx)0);
73 
74  if (i.val(C) > 1) return (GUM_SCALAR)0.0;
75 
76  GUM_SCALAR ratio = (GUM_SCALAR)1.0 - this->externalWeight();
77 
78  GUM_SCALAR fact = (GUM_SCALAR)ratio;
79 
80  if (fact != (GUM_SCALAR)0) {
81  for (Idx j = 1; j < this->nbrDim(); j++) {
82  const DiscreteVariable& v = this->variable(j);
83 
84  if (i.val(v) == 1) {
85  GUM_SCALAR pr = (1 - this->causalWeight(v)) / ratio;
86 
87  if (pr == (GUM_SCALAR)0.0) {
88  fact = (GUM_SCALAR)0.0;
89  break;
90  } else {
91  fact *= pr;
92  }
93  }
94  }
95  }
96 
97  return (i.val(C) != 1) ? fact : (GUM_SCALAR)1.0 - fact;
98  }
99 
100  template < typename GUM_SCALAR >
102  std::stringstream s;
103  s << MultiDimImplementation< GUM_SCALAR >::variable(0) << "=noisyORCompound(["
104  << this->externalWeight() << "],";
105 
106  for (Idx i = 1; i < MultiDimImplementation< GUM_SCALAR >::nbrDim(); i++) {
107  s << MultiDimImplementation< GUM_SCALAR >::variable(i) << "["
109  << "]";
110  }
111 
112  s << ")";
113 
114  std::string res;
115  s >> res;
116  return res;
117  }
118 
119  // For friendly displaying the content of the variable.
120 
121  template < typename GUM_SCALAR >
122  INLINE std::ostream&
123  operator<<(std::ostream& s, const MultiDimNoisyORCompound< GUM_SCALAR >& ag) {
124  return s << ag.toString();
125  }
126 
127  template < typename GUM_SCALAR >
131  this->__default_weight);
132  }
133 
134  // returns the name of the implementation
135  template < typename GUM_SCALAR >
136  INLINE const std::string& MultiDimNoisyORCompound< GUM_SCALAR >::name() const {
137  static const std::string str = "MultiDimNoisyORCompound";
138  return str;
139  }
140 
141  // ==================================================
142 } /* namespace gum */
virtual const std::string & name() const
returns the real name of the multiDimArray
virtual const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
class for multiDimNoisyORCompound
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.
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() For example : MultiDimArray<double> y; MultiDimContainer<double>* x = y.newFactory(); Then x is a MultiDimArray<double>*.
abstract class for Conditional Indepency Models
GUM_SCALAR __default_weight
in Henrion (89) in a hashtable with a default_value.
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
virtual ~MultiDimNoisyORCompound()
Destructor.
const std::string toString() const
returns the real name of the multiDimArray
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
MultiDimNoisyORCompound(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
virtual GUM_SCALAR get(const Instantiation &i) const
returns the real name of the multiDimArray
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52