aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::InfluenceDiagramGenerator< GUM_SCALAR > Class Template Reference

<agrum/ID/generator/influenceDiagramGenerator.h> More...

#include <influenceDiagramGenerator.h>

+ Collaboration diagram for gum::InfluenceDiagramGenerator< GUM_SCALAR >:

Public Member Functions

Constructors / Destructor
 InfluenceDiagramGenerator ()
 Default constructor. More...
 
 InfluenceDiagramGenerator (ICPTGenerator< GUM_SCALAR > *cptGenerator)
 Use this constructor if you want to use a different policy for generating CPT than the default one. More...
 
 InfluenceDiagramGenerator (UTGenerator *utGenerator)
 Use this constructor if you want to use a different policy for generating UT than the default one. More...
 
 InfluenceDiagramGenerator (ICPTGenerator< GUM_SCALAR > *cptGenerator, UTGenerator *utGenerator)
 Use this constructor if you want to use a different policy for generating both CPT & UT than the defaults ones. More...
 
 ~InfluenceDiagramGenerator ()
 Destructor. More...
 
ID generation methods
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. More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::InfluenceDiagramGenerator< GUM_SCALAR >

<agrum/ID/generator/influenceDiagramGenerator.h>

Class for generating influence diagram.

This class randomly generates an influence diagram given four parameters: the number of nodes,the probability of adding an arc between two nodes, the proportion of chance node and the proportion of utility node (the proportion of decision node is deduce from thos two)

Definition at line 56 of file influenceDiagramGenerator.h.

Constructor & Destructor Documentation

◆ InfluenceDiagramGenerator() [1/4]

template<typename GUM_SCALAR >
gum::InfluenceDiagramGenerator< GUM_SCALAR >::InfluenceDiagramGenerator ( )

Default constructor.

Use the SimpleCPTGenerator for generating the IDs CPT. Use the SimpleUTGenerator for generating the IDs UT.

Definition at line 36 of file influenceDiagramGenerator_tpl.h.

36  {
37  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
38  _cptGenerator_ = new SimpleCPTGenerator< GUM_SCALAR >();
39  _utGenerator_ = new SimpleUTGenerator();
40  }
ICPTGenerator< GUM_SCALAR > * _cptGenerator_

◆ InfluenceDiagramGenerator() [2/4]

template<typename GUM_SCALAR >
gum::InfluenceDiagramGenerator< GUM_SCALAR >::InfluenceDiagramGenerator ( ICPTGenerator< GUM_SCALAR > *  cptGenerator)
explicit

Use this constructor if you want to use a different policy for generating CPT than the default one.

The cptGenerator will be erased when the destructor is called.

Parameters
cptGeneratorThe policy used to generate CPT.

Definition at line 47 of file influenceDiagramGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

48  {
49  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
50  _cptGenerator_ = cptGenerator;
51  _utGenerator_ = new SimpleUTGenerator();
52  }
ICPTGenerator< GUM_SCALAR > * _cptGenerator_
+ Here is the call graph for this function:

◆ InfluenceDiagramGenerator() [3/4]

template<typename GUM_SCALAR >
gum::InfluenceDiagramGenerator< GUM_SCALAR >::InfluenceDiagramGenerator ( UTGenerator utGenerator)
explicit

Use this constructor if you want to use a different policy for generating UT than the default one.

The utGenerator will be erased when the destructor is called.

Parameters
utGeneratorThe policy used to generate UT.

Definition at line 59 of file influenceDiagramGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

59  {
60  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
61  _cptGenerator_ = new SimpleCPTGenerator< GUM_SCALAR >();
62  _utGenerator_ = utGenerator;
63  }
ICPTGenerator< GUM_SCALAR > * _cptGenerator_
+ Here is the call graph for this function:

◆ InfluenceDiagramGenerator() [4/4]

template<typename GUM_SCALAR >
gum::InfluenceDiagramGenerator< GUM_SCALAR >::InfluenceDiagramGenerator ( ICPTGenerator< GUM_SCALAR > *  cptGenerator,
UTGenerator utGenerator 
)

Use this constructor if you want to use a different policy for generating both CPT & UT than the defaults ones.

The cptGenerator and utGenerator will be erased when the destructor is called.

Parameters
cptGeneratorThe policy used to generate CPT.
utGeneratorThe policy used to generate UT.

Definition at line 72 of file influenceDiagramGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

74  {
75  GUM_CONSTRUCTOR(InfluenceDiagramGenerator);
76  _cptGenerator_ = cptGenerator;
77  _utGenerator_ = utGenerator;
78  }
ICPTGenerator< GUM_SCALAR > * _cptGenerator_
+ Here is the call graph for this function:

◆ ~InfluenceDiagramGenerator()

template<typename GUM_SCALAR >
gum::InfluenceDiagramGenerator< GUM_SCALAR >::~InfluenceDiagramGenerator ( )

Destructor.

Definition at line 82 of file influenceDiagramGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

82  {
83  GUM_DESTRUCTOR(InfluenceDiagramGenerator);
84  delete _cptGenerator_;
85  delete _utGenerator_;
86  }
ICPTGenerator< GUM_SCALAR > * _cptGenerator_
+ Here is the call graph for this function:

Member Function Documentation

◆ _checkTemporalOrder_()

template<typename GUM_SCALAR >
void gum::InfluenceDiagramGenerator< GUM_SCALAR >::_checkTemporalOrder_ ( InfluenceDiagram< GUM_SCALAR > *  infdiag)
private

Definition at line 154 of file influenceDiagramGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

155  {
156  if (!infdiag->decisionOrderExists()) {
157  Sequence< NodeId > order = infdiag->topologicalOrder(true);
158 
159  auto orderIter = order.begin();
160 
161  while ((orderIter != order.end()) && (!infdiag->isDecisionNode(*orderIter)))
162  ++orderIter;
163 
164  if (orderIter == order.end()) return;
165 
166  NodeId parentDecision = (*orderIter);
167 
168  ++orderIter;
169 
170  for (; orderIter != order.end(); ++orderIter)
171  if (infdiag->isDecisionNode(*orderIter)) {
172  infdiag->addArc(parentDecision, (*orderIter));
173  parentDecision = (*orderIter);
174  }
175  }
176  }
iterator begin() const
Returns an unsafe begin iterator.
Definition: sequence_tpl.h:631
const iterator & end() const noexcept
Returns the unsafe end iterator.
Definition: sequence_tpl.h:638
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ generateID()

template<typename GUM_SCALAR >
InfluenceDiagram< GUM_SCALAR > * gum::InfluenceDiagramGenerator< GUM_SCALAR >::generateID ( Size  nbrNodes,
GUM_SCALAR  arcDensity,
GUM_SCALAR  chanceNodeDensity,
GUM_SCALAR  utilityNodeDensity,
Size  max_modality = 2 
)

Generates an influence diagram using floats.

Parameters
nbrNodesThe number of nodes in the generated ID.
arcDensityThe probability of adding an arc between two nodes.
chanceNodeDensityThe proportion of chance node
utilityNodeDensityThe proportion of utility node
max_modalityEach DRV has from 2 to max_modality modalities
Returns
A IDs randomly generated.

Definition at line 97 of file influenceDiagramGenerator_tpl.h.

References gum::Set< Key, Alloc >::emplace().

101  {
102  InfluenceDiagram< GUM_SCALAR >* influenceDiagram = new InfluenceDiagram< GUM_SCALAR >();
103  // First we add nodes
104  HashTable< Size, NodeId > map;
105  std::stringstream strBuff;
106  Size nb_mod;
107 
108  for (Idx i = 0; i < nbrNodes; ++i) {
109  strBuff << i;
110  nb_mod = (max_modality == 2) ? 2 : 2 + rand() % (max_modality - 1);
111 
112  GUM_SCALAR cnd = chanceNodeDensity * (GUM_SCALAR)RAND_MAX;
113  GUM_SCALAR und = utilityNodeDensity * (GUM_SCALAR)RAND_MAX;
114 
115  GUM_SCALAR d = (GUM_SCALAR)rand();
116 
117  if (d < cnd)
118  map.insert(i,
119  influenceDiagram->addChanceNode(LabelizedVariable(strBuff.str(), "", nb_mod)));
120  else if (d < (cnd + und))
121  map.insert(i, influenceDiagram->addUtilityNode(LabelizedVariable(strBuff.str(), "", 1)));
122  else
123  map.insert(i,
124  influenceDiagram->addDecisionNode(LabelizedVariable(strBuff.str(), "", nb_mod)));
125 
126  strBuff.str("");
127  }
128 
129  // We add arcs
130  GUM_SCALAR p = arcDensity * (GUM_SCALAR)RAND_MAX;
131 
132  for (Size i = 0; i < nbrNodes; ++i)
133  if (!influenceDiagram->isUtilityNode(map[i]))
134  for (Size j = i + 1; j < nbrNodes; ++j)
135  if (((GUM_SCALAR)rand()) < p) { influenceDiagram->addArc(map[i], map[j]); }
136 
137  // And fill the CPTs and UTs
138  for (Size i = 0; i < nbrNodes; ++i)
139  if (influenceDiagram->isChanceNode(map[i]))
140  _cptGenerator_->generateCPT(
141  influenceDiagram->cpt(map[i]).pos(influenceDiagram->variable(map[i])),
142  influenceDiagram->cpt(map[i]));
143  else if (influenceDiagram->isUtilityNode(map[i]))
145  influenceDiagram->utility(map[i]).pos(influenceDiagram->variable(map[i])),
146  influenceDiagram->utility(map[i]));
147 
148  _checkTemporalOrder_(influenceDiagram);
149 
150  return influenceDiagram;
151  }
void _checkTemporalOrder_(InfluenceDiagram< GUM_SCALAR > *infdiag)
ICPTGenerator< GUM_SCALAR > * _cptGenerator_
virtual void generateUT(const Idx &varId, const Potential< float > &ut)=0
Generates a UT using floats.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

Member Data Documentation

◆ _cptGenerator_

template<typename GUM_SCALAR >
ICPTGenerator< GUM_SCALAR >* gum::InfluenceDiagramGenerator< GUM_SCALAR >::_cptGenerator_
private

Definition at line 125 of file influenceDiagramGenerator.h.

◆ _utGenerator_

template<typename GUM_SCALAR >
UTGenerator* gum::InfluenceDiagramGenerator< GUM_SCALAR >::_utGenerator_
private

Definition at line 127 of file influenceDiagramGenerator.h.


The documentation for this class was generated from the following files: