aGrUM  0.16.0
influenceDiagramGenerator_tpl.h
Go to the documentation of this file.
1 
30 
31 namespace gum {
32 
33  // Default constructor.
34  // Use the SimpleCPTGenerator for generating the IDs CPT.
35  template < typename GUM_SCALAR >
37  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
38  __cptGenerator = new SimpleCPTGenerator< GUM_SCALAR >();
39  __utGenerator = new SimpleUTGenerator();
40  }
41 
42  // Use this constructor if you want to use a different policy for generating
43  // CPT than the default one.
44  // The cptGenerator will be erased when the destructor is called.
45  // @param cptGenerator The policy used to generate CPT.
46  template < typename GUM_SCALAR >
48  ICPTGenerator< GUM_SCALAR >* cptGenerator) {
49  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
50  __cptGenerator = cptGenerator;
51  __utGenerator = new SimpleUTGenerator();
52  }
53 
54  // Use this constructor if you want to use a different policy for generating
55  // UT than the default one.
56  // The utGenerator will be erased when the destructor is called.
57  // @param utGenerator The policy used to generate UT.
58  template < typename GUM_SCALAR >
60  UTGenerator* utGenerator) {
61  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
62  __cptGenerator = new SimpleCPTGenerator< GUM_SCALAR >();
63  __utGenerator = utGenerator;
64  }
65 
66  // Use this constructor if you want to use a different policy for generating
67  // both CPT & UT than the defaults ones.
68  // The cptGenerator and utGenerator will be erased when the destructor is
69  // called.
70  // @param cptGenerator The policy used to generate CPT.
71  // @param utGenerator The policy used to generate UT.
72  template < typename GUM_SCALAR >
74  ICPTGenerator< GUM_SCALAR >* cptGenerator, UTGenerator* utGenerator) {
75  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
76  __cptGenerator = cptGenerator;
77  __utGenerator = utGenerator;
78  }
79 
80  // Destructor.
81  template < typename GUM_SCALAR >
83  GUM_DESTRUCTOR(InfluenceDiagramGenerator);
84  delete __cptGenerator;
85  delete __utGenerator;
86  }
87 
88  // Generates an influence diagram using floats.
89  // @param nbrNodes The number of nodes in the generated ID.
90  // @param arcdensity The probability of adding an arc between two nodes.
91  // @param chanceNodeDensity The proportion of chance node
92  // @param utilityNodeDensity The proportion of utility node
93  // @param max_modality Each DRV has from 2 to max_modality modalities
94  // @return A IDs randomly generated.
95  template < typename GUM_SCALAR >
98  Size nbrNodes,
99  GUM_SCALAR arcDensity,
100  GUM_SCALAR chanceNodeDensity,
101  GUM_SCALAR utilityNodeDensity,
102  Size max_modality) {
103  InfluenceDiagram< GUM_SCALAR >* influenceDiagram =
105  // First we add nodes
107  std::stringstream strBuff;
108  Size nb_mod;
109 
110  for (Idx i = 0; i < nbrNodes; ++i) {
111  strBuff << i;
112  nb_mod = (max_modality == 2) ? 2 : 2 + rand() % (max_modality - 1);
113 
114  GUM_SCALAR cnd = chanceNodeDensity * (GUM_SCALAR)RAND_MAX;
115  GUM_SCALAR und = utilityNodeDensity * (GUM_SCALAR)RAND_MAX;
116 
117  GUM_SCALAR d = (GUM_SCALAR)rand();
118 
119  if (d < cnd)
120  map.insert(i,
121  influenceDiagram->addChanceNode(
122  LabelizedVariable(strBuff.str(), "", nb_mod)));
123  else if (d < (cnd + und))
124  map.insert(i,
125  influenceDiagram->addUtilityNode(
126  LabelizedVariable(strBuff.str(), "", 1)));
127  else
128  map.insert(i,
129  influenceDiagram->addDecisionNode(
130  LabelizedVariable(strBuff.str(), "", nb_mod)));
131 
132  strBuff.str("");
133  }
134 
135  // We add arcs
136  GUM_SCALAR p = arcDensity * (GUM_SCALAR)RAND_MAX;
137 
138  for (Size i = 0; i < nbrNodes; ++i)
139  if (!influenceDiagram->isUtilityNode(map[i]))
140  for (Size j = i + 1; j < nbrNodes; ++j)
141  if (((GUM_SCALAR)rand()) < p) {
142  influenceDiagram->addArc(map[i], map[j]);
143  }
144 
145  // And fill the CPTs and UTs
146  for (Size i = 0; i < nbrNodes; ++i)
147  if (influenceDiagram->isChanceNode(map[i]))
148  __cptGenerator->generateCPT(
149  influenceDiagram->cpt(map[i]).pos(influenceDiagram->variable(map[i])),
150  influenceDiagram->cpt(map[i]));
151  else if (influenceDiagram->isUtilityNode(map[i]))
152  __utGenerator->generateUT(influenceDiagram->utility(map[i]).pos(
153  influenceDiagram->variable(map[i])),
154  influenceDiagram->utility(map[i]));
155 
156  __checkTemporalOrder(influenceDiagram);
157 
158  return influenceDiagram;
159  }
160 
161  template < typename GUM_SCALAR >
164  if (!infdiag->decisionOrderExists()) {
165  Sequence< NodeId > order = infdiag->topologicalOrder(true);
166 
167  auto orderIter = order.begin();
168 
169  while ((orderIter != order.end()) && (!infdiag->isDecisionNode(*orderIter)))
170  ++orderIter;
171 
172  if (orderIter == order.end()) return;
173 
174  NodeId parentDecision = (*orderIter);
175 
176  ++orderIter;
177 
178  for (; orderIter != order.end(); ++orderIter)
179  if (infdiag->isDecisionNode(*orderIter)) {
180  infdiag->addArc(parentDecision, (*orderIter));
181  parentDecision = (*orderIter);
182  }
183  }
184  }
185 
186 } /* 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:657
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)
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual const Potential< GUM_SCALAR > & utility(NodeId varId) const
Returns the utility table of a utility node.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:117
<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:53
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:48
const iterator & end() const noexcept
Returns the unsafe end iterator.
Definition: sequence_tpl.h:664
Abstract class for generating Utility Tables.
Definition: UTGenerator.h:43
Size NodeId
Type for node ids.
Definition: graphElements.h:98
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a potential variable.
Class representing an Influence Diagram.