aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
influenceDiagramGenerator_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief Source implementation of InfluenceDiagramGenerator
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
26  * GONZALES(@AMU)
27  *
28  */
29 #include <agrum/ID/generator/influenceDiagramGenerator.h>
30 
31 namespace gum {
32 
33  // Default constructor.
34  // Use the SimpleCPTGenerator for generating the IDs CPT.
35  template < typename GUM_SCALAR >
36  InfluenceDiagramGenerator< GUM_SCALAR >::InfluenceDiagramGenerator() {
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 >
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 >
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 >
79  }
80 
81  // Destructor.
82  template < typename GUM_SCALAR >
85  delete cptGenerator__;
86  delete utGenerator__;
87  }
88 
89  // Generates an influence diagram using floats.
90  // @param nbrNodes The number of nodes in the generated ID.
91  // @param arcdensity The probability of adding an arc between two nodes.
92  // @param chanceNodeDensity The proportion of chance node
93  // @param utilityNodeDensity The proportion of utility node
94  // @param max_modality Each DRV has from 2 to max_modality modalities
95  // @return A IDs randomly generated.
96  template < typename GUM_SCALAR >
99  Size nbrNodes,
103  Size max_modality) {
105  = new InfluenceDiagram< GUM_SCALAR >();
106  // First we add nodes
107  HashTable< Size, NodeId > map;
109  Size nb_mod;
110 
111  for (Idx i = 0; i < nbrNodes; ++i) {
112  strBuff << i;
113  nb_mod = (max_modality == 2) ? 2 : 2 + rand() % (max_modality - 1);
114 
117 
119 
120  if (d < cnd)
121  map.insert(i,
124  else if (d < (cnd + und))
125  map.insert(i,
127  LabelizedVariable(strBuff.str(), "", 1)));
128  else
129  map.insert(i,
132 
133  strBuff.str("");
134  }
135 
136  // We add arcs
138 
139  for (Size i = 0; i < nbrNodes; ++i)
141  for (Size j = i + 1; j < nbrNodes; ++j)
142  if (((GUM_SCALAR)rand()) < p) {
144  }
145 
146  // And fill the CPTs and UTs
147  for (Size i = 0; i < nbrNodes; ++i)
152  else if (influenceDiagram->isUtilityNode(map[i]))
156 
158 
159  return influenceDiagram;
160  }
161 
162  template < typename GUM_SCALAR >
165  if (!infdiag->decisionOrderExists()) {
167 
168  auto orderIter = order.begin();
169 
170  while ((orderIter != order.end()) && (!infdiag->isDecisionNode(*orderIter)))
171  ++orderIter;
172 
173  if (orderIter == order.end()) return;
174 
176 
177  ++orderIter;
178 
179  for (; orderIter != order.end(); ++orderIter)
183  }
184  }
185  }
186 
187 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669