aGrUM  0.14.2
influenceDiagramGenerator_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
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 
30  // Default constructor.
31  // Use the SimpleCPTGenerator for generating the IDs CPT.
32  template < typename GUM_SCALAR >
34  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
35  __cptGenerator = new SimpleCPTGenerator< GUM_SCALAR >();
36  __utGenerator = new SimpleUTGenerator();
37  }
38 
39  // Use this constructor if you want to use a different policy for generating
40  // CPT than the default one.
41  // The cptGenerator will be erased when the destructor is called.
42  // @param cptGenerator The policy used to generate CPT.
43  template < typename GUM_SCALAR >
45  ICPTGenerator< GUM_SCALAR >* cptGenerator) {
46  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
47  __cptGenerator = cptGenerator;
48  __utGenerator = new SimpleUTGenerator();
49  }
50 
51  // Use this constructor if you want to use a different policy for generating
52  // UT than the default one.
53  // The utGenerator will be erased when the destructor is called.
54  // @param utGenerator The policy used to generate UT.
55  template < typename GUM_SCALAR >
57  UTGenerator* utGenerator) {
58  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
59  __cptGenerator = new SimpleCPTGenerator< GUM_SCALAR >();
60  __utGenerator = utGenerator;
61  }
62 
63  // Use this constructor if you want to use a different policy for generating
64  // both CPT & UT than the defaults ones.
65  // The cptGenerator and utGenerator will be erased when the destructor is
66  // called.
67  // @param cptGenerator The policy used to generate CPT.
68  // @param utGenerator The policy used to generate UT.
69  template < typename GUM_SCALAR >
71  ICPTGenerator< GUM_SCALAR >* cptGenerator, UTGenerator* utGenerator) {
72  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
73  __cptGenerator = cptGenerator;
74  __utGenerator = utGenerator;
75  }
76 
77  // Destructor.
78  template < typename GUM_SCALAR >
80  GUM_DESTRUCTOR(InfluenceDiagramGenerator);
81  delete __cptGenerator;
82  delete __utGenerator;
83  }
84 
85  // Generates an influence diagram using floats.
86  // @param nbrNodes The number of nodes in the generated ID.
87  // @param arcdensity The probability of adding an arc between two nodes.
88  // @param chanceNodeDensity The proportion of chance node
89  // @param utilityNodeDensity The proportion of utility node
90  // @param max_modality Each DRV has from 2 to max_modality modalities
91  // @return A IDs randomly generated.
92  template < typename GUM_SCALAR >
95  Size nbrNodes,
96  GUM_SCALAR arcDensity,
97  GUM_SCALAR chanceNodeDensity,
98  GUM_SCALAR utilityNodeDensity,
99  Size max_modality) {
100  InfluenceDiagram< GUM_SCALAR >* influenceDiagram =
102  // First we add nodes
104  std::stringstream strBuff;
105  Size nb_mod;
106 
107  for (Idx i = 0; i < nbrNodes; ++i) {
108  strBuff << i;
109  nb_mod = (max_modality == 2) ? 2 : 2 + rand() % (max_modality - 1);
110 
111  GUM_SCALAR cnd = chanceNodeDensity * (GUM_SCALAR)RAND_MAX;
112  GUM_SCALAR und = utilityNodeDensity * (GUM_SCALAR)RAND_MAX;
113 
114  GUM_SCALAR d = (GUM_SCALAR)rand();
115 
116  if (d < cnd)
117  map.insert(i,
118  influenceDiagram->addChanceNode(
119  LabelizedVariable(strBuff.str(), "", nb_mod)));
120  else if (d < (cnd + und))
121  map.insert(i,
122  influenceDiagram->addUtilityNode(
123  LabelizedVariable(strBuff.str(), "", 1)));
124  else
125  map.insert(i,
126  influenceDiagram->addDecisionNode(
127  LabelizedVariable(strBuff.str(), "", nb_mod)));
128 
129  strBuff.str("");
130  }
131 
132  // We add arcs
133  GUM_SCALAR p = arcDensity * (GUM_SCALAR)RAND_MAX;
134 
135  for (Size i = 0; i < nbrNodes; ++i)
136  if (!influenceDiagram->isUtilityNode(map[i]))
137  for (Size j = i + 1; j < nbrNodes; ++j)
138  if (((GUM_SCALAR)rand()) < p) {
139  influenceDiagram->addArc(map[i], map[j]);
140  }
141 
142  // And fill the CPTs and UTs
143  for (Size i = 0; i < nbrNodes; ++i)
144  if (influenceDiagram->isChanceNode(map[i]))
145  __cptGenerator->generateCPT(
146  influenceDiagram->cpt(map[i]).pos(influenceDiagram->variable(map[i])),
147  influenceDiagram->cpt(map[i]));
148  else if (influenceDiagram->isUtilityNode(map[i]))
149  __utGenerator->generateUT(influenceDiagram->utility(map[i]).pos(
150  influenceDiagram->variable(map[i])),
151  influenceDiagram->utility(map[i]));
152 
153  __checkTemporalOrder(influenceDiagram);
154 
155  return influenceDiagram;
156  }
157 
158  template < typename GUM_SCALAR >
161  if (!infdiag->decisionOrderExists()) {
162  Sequence< NodeId > order = infdiag->topologicalOrder(true);
163 
164  auto orderIter = order.begin();
165 
166  while ((orderIter != order.end()) && (!infdiag->isDecisionNode(*orderIter)))
167  ++orderIter;
168 
169  if (orderIter == order.end()) return;
170 
171  NodeId parentDecision = (*orderIter);
172 
173  ++orderIter;
174 
175  for (; orderIter != order.end(); ++orderIter)
176  if (infdiag->isDecisionNode(*orderIter)) {
177  infdiag->addArc(parentDecision, (*orderIter));
178  parentDecision = (*orderIter);
179  }
180  }
181  }
182 
183 } /* namespace gum */
InfluenceDiagram< GUM_SCALAR > * generateID(Size nbrNodes, GUM_SCALAR arcDensity, GUM_SCALAR chanceNodeDensity, GUM_SCALAR utilityNodeDensity, Size max_modality=2)
Generates an influence diagram using floats.
Class for generating Utility Tables.
iterator begin() const
Returns an unsafe begin iterator.
Definition: sequence_tpl.h:654
void addArc(NodeId tail, NodeId head)
Add an arc in the ID, and update diagram&#39;s potential nodes cpt if necessary.
bool isChanceNode(NodeId varId) const
Returns true if node is a chance one.
NodeId addChanceNode(const DiscreteVariable &variable, NodeId id=0)
Add a chance variable, it&#39;s associate node and it&#39;s CPT.
class LabelizedVariable
virtual const DiscreteVariable & variable(NodeId id) const
Returns a constant reference over a variabe given it&#39;s node id.
void __checkTemporalOrder(InfluenceDiagram< GUM_SCALAR > *infdiag)
Class for generating bayesian networks.
virtual const Potential< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
<agrum/ID/generator/influenceDiagramGenerator.h>
bool isDecisionNode(NodeId varId) const
Returns true if node is a decision one.
bool isUtilityNode(NodeId varId) const
Returns true if node is a utility one.
const Sequence< NodeId > & topologicalOrder(bool clear=true) const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
Definition: DAGmodel.cpp:115
<agrum/BN/generator/simpleCPTGenerator.h>
bool decisionOrderExists() const
True if a directed path exist with all decison nodes.
NodeId addUtilityNode(const DiscreteVariable &variable, NodeId id=0)
Add a utility variable, it&#39;s associate node and it&#39;s UT.
Size Idx
Type for indexes.
Definition: types.h:50
NodeId addDecisionNode(const DiscreteVariable &variable, NodeId id=0)
Add a decision variable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
const iterator & end() const noexcept
Returns the unsafe end iterator.
Definition: sequence_tpl.h:661
Abstract class for generating Utility Tables.
Definition: UTGenerator.h:40
Size NodeId
Type for node ids.
Definition: graphElements.h:97
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a potential variable.
Class representing an Influence Diagram.