aGrUM  0.14.2
multiDimNoisyORNet_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  ***************************************************************************/
28 
29 namespace gum {
30 
31  // Default constructor
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(MultiDimNoisyORNet);
37  }
38 
39  // Default constructor
40  template < typename GUM_SCALAR >
43  MultiDimICIModel< GUM_SCALAR >(from) {
44  GUM_CONS_CPY(MultiDimNoisyORNet);
45  }
46 
47  // Copy constructor using a bijection to replace variables from source.
48  template < typename GUM_SCALAR >
52  MultiDimICIModel< GUM_SCALAR >(bij, from) {
53  GUM_CONSTRUCTOR(MultiDimNoisyORNet);
54  }
55 
56  // destructor
57  template < typename GUM_SCALAR >
59  GUM_DESTRUCTOR(MultiDimNoisyORNet);
60  }
61 
62  template < typename GUM_SCALAR >
64  if (this->nbrDim() < 1) {
65  GUM_ERROR(OperationNotAllowed, "Not enough variable for a NoisyOr ");
66  }
67 
68  const DiscreteVariable& C = this->variable((Idx)0);
69 
70  if (i.val(C) > 1) return (GUM_SCALAR)0.0;
71 
72  GUM_SCALAR ratio = (GUM_SCALAR)1.0 - this->externalWeight();
73 
74  GUM_SCALAR fact = (GUM_SCALAR)ratio;
75 
76  if (fact != (GUM_SCALAR)0) {
77  for (Idx j = 1; j < this->nbrDim(); j++) {
78  const DiscreteVariable& v = this->variable(j);
79 
80  if (i.val(v) == 1) {
81  GUM_SCALAR pr = (1 - this->causalWeight(v));
82 
83  if (pr == (GUM_SCALAR)0.0) {
84  fact = (GUM_SCALAR)0.0;
85  break;
86  } else {
87  fact *= pr;
88  }
89  }
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 MultiDimNoisyORNet< GUM_SCALAR >::toString() const {
98  std::stringstream s;
99  s << MultiDimImplementation< GUM_SCALAR >::variable(0) << "=noisyORNet(["
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  template < typename GUM_SCALAR >
117  INLINE std::ostream& operator<<(std::ostream& s,
119  return s << ag.toString();
120  }
121 
122  template < typename GUM_SCALAR >
126  this->__default_weight);
127  }
128 
129  // returns the name of the implementation
130  template < typename GUM_SCALAR >
131  INLINE const std::string& MultiDimNoisyORNet< GUM_SCALAR >::name() const {
132  static const std::string str = "MultiDimNoisyORNet";
133  return str;
134  }
135 
136 } /* namespace gum */
Noisy OR representation.
class for NoisyOR-net implementation as multiDim
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the real name of the multiDimArray.
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
abstract class for Conditional Indepency Models
const std::string toString() const
Returns the real name of the multiDimArray.
virtual const std::string & name() const
Returns the real name of the multiDimArray.
GUM_SCALAR __default_weight
in Henrion (89) in a hashtable with a default_value.
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()
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
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
GUM_SCALAR causalWeight(const DiscreteVariable &v) const
Copy of a multiDimICIModel.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:50
MultiDimNoisyORNet(GUM_SCALAR external_weight, GUM_SCALAR default_weight=(GUM_SCALAR) 1.0)
Default constructor.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
virtual ~MultiDimNoisyORNet()
Destructor.