aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::prm::LayerGenerator< GUM_SCALAR > Class Template Reference

<agrum/PRM/generator/layerGenerator.h> More...

#include <layerGenerator.h>

+ Inheritance diagram for gum::prm::LayerGenerator< GUM_SCALAR >:
+ Collaboration diagram for gum::prm::LayerGenerator< GUM_SCALAR >:

Public Member Functions

Constructors and destructor.
 LayerGenerator ()
 Default constructor. More...
 
 LayerGenerator (const LayerGenerator &source)
 Copy constructor. More...
 
LayerGeneratoroperator= (const LayerGenerator &source)
 Copy operator. More...
 
virtual ~LayerGenerator ()
 Destructor. More...
 
Size getDomainSize () const
 Returns the domain size of generated types. More...
 
void setDomainSize (Size s)
 Set the domain size of generated types. More...
 
Size getMaxParents () const
 Returns the max number of parents allowed for any attribute or aggregator. More...
 
void setMaxParents (Size s)
 Returns the max number of parents allowed for any attribute or aggregator. More...
 
void setLayers (const std::vector< LayerData > &v)
 Defines the structure of each layers. More...
 
std::vector< LayerData > & getLayer ()
 Returns the domain size of generated types. More...
 
const std::vector< LayerData > & getLayer () const
 Returns the domain size of generated types. More...
 
virtual PRM< GUM_SCALAR > * generate ()
 Proceeds with the generation of the PRM<GUM_SCALAR>. More...
 
void setNameGenerator (const NameGenerator &name_gen)
 Getters and setters. More...
 
const NameGeneratorgetNameGenerator () const
 Returns the NameGenerator used by this generator. More...
 

Protected Attributes

NameGenerator name_gen_
 The name generator used by this class. More...
 

Classes

struct  LayerData
 Getters and setters. More...
 
struct  MyData
 

Detailed Description

template<typename GUM_SCALAR>
class gum::prm::LayerGenerator< GUM_SCALAR >

<agrum/PRM/generator/layerGenerator.h>

This class generates PRMs with a layer structure.

Definition at line 55 of file layerGenerator.h.

Constructor & Destructor Documentation

◆ LayerGenerator() [1/2]

template<typename GUM_SCALAR >
INLINE gum::prm::LayerGenerator< GUM_SCALAR >::LayerGenerator ( )

Default constructor.

Definition at line 271 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

271  :
272  layers__(), domain_size__(2), max_parents__(INT_MAX) {
273  GUM_CONSTRUCTOR(LayerGenerator);
274  }
LayerGenerator()
Default constructor.
std::vector< LayerData > layers__
+ Here is the call graph for this function:

◆ LayerGenerator() [2/2]

template<typename GUM_SCALAR >
INLINE gum::prm::LayerGenerator< GUM_SCALAR >::LayerGenerator ( const LayerGenerator< GUM_SCALAR > &  source)

Copy constructor.

Definition at line 277 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

278  :
279  layers__(source.layers__),
280  domain_size__(source.domain_size__), max_parents__(source.max_parents__) {
281  GUM_CONS_CPY(LayerGenerator);
282  }
LayerGenerator()
Default constructor.
std::vector< LayerData > layers__
+ Here is the call graph for this function:

◆ ~LayerGenerator()

template<typename GUM_SCALAR >
INLINE gum::prm::LayerGenerator< GUM_SCALAR >::~LayerGenerator ( )
virtual

Destructor.

Definition at line 285 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

285  {
286  GUM_DESTRUCTOR(LayerGenerator);
287  }
LayerGenerator()
Default constructor.
+ Here is the call graph for this function:

Member Function Documentation

◆ generate()

template<typename GUM_SCALAR >
PRM< GUM_SCALAR > * gum::prm::LayerGenerator< GUM_SCALAR >::generate ( )
virtual

Proceeds with the generation of the PRM<GUM_SCALAR>.

Implements gum::prm::PRMGenerator< GUM_SCALAR >.

Definition at line 34 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

34  {
35  if (layers__.size() == 0) {
36  GUM_ERROR(OperationNotAllowed,
37  "cannot generate a layered PRM<GUM_SCALAR> without layers");
38  }
39 
40  std::vector< MyData > l;
41  PRMFactory< GUM_SCALAR > factory;
42  std::string type = generateType__(factory);
43  generateInterfaces__(factory, type, l);
44  generateClasses__(factory, type, l);
45  generateSystem__(factory, l);
46  return factory.prm();
47  }
std::vector< LayerData > layers__
std::string generateType__(PRMFactory< GUM_SCALAR > &f)
void generateInterfaces__(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< MyData > &l)
void generateClasses__(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< typename LayerGenerator::MyData > &l)
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
void generateSystem__(PRMFactory< GUM_SCALAR > &factory, std::vector< typename LayerGenerator::MyData > &l)
+ Here is the call graph for this function:

◆ generateClassDag__()

template<typename GUM_SCALAR >
void gum::prm::LayerGenerator< GUM_SCALAR >::generateClassDag__ ( Size  lvl,
DAG dag,
Bijection< std::string, NodeId > &  names,
std::vector< typename LayerGenerator< GUM_SCALAR >::MyData > &  l 
)
private

Definition at line 172 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

176  {
177  float density = layers__[lvl].inner_density * RAND_MAX;
178  std::vector< NodeId > nodes;
179  NodeId id = 0;
180 
181  if (lvl) {
182  for (const auto agg: l[lvl].g) {
183  id = dag.addNode();
184  names.insert(agg, id);
185  nodes.push_back(id);
186  }
187  }
188 
189  for (const auto attr: l[lvl].a) {
190  id = dag.addNode();
191  names.insert(attr, id);
192 
193  for (const auto node: nodes)
194  if (std::rand() < density) dag.addArc(node, names.second(attr));
195 
196  nodes.push_back(id);
197  }
198 
199  // For each nodes with #parents > max_parents__ we randomly remove parents
200  // until
201  // #parents <= max_parents__
202  for (const auto node: dag.nodes()) {
203  if (dag.parents(node).size() > getMaxParents()) {
204  std::vector< NodeId > v;
205 
206  for (const auto par: dag.parents(node))
207  v.push_back(par);
208 
209  while (dag.parents(node).size() > getMaxParents()) {
210  size_t idx = std::rand() % v.size();
211  Arc arc(v[idx], node);
212  GUM_ASSERT(dag.existsArc(arc));
213  dag.eraseArc(arc);
214  v[idx] = v.back();
215  v.pop_back();
216  }
217  }
218  }
219  }
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
Size getMaxParents() const
Returns the max number of parents allowed for any attribute or aggregator.
std::vector< LayerData > layers__
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ generateClasses__()

template<typename GUM_SCALAR >
void gum::prm::LayerGenerator< GUM_SCALAR >::generateClasses__ ( PRMFactory< GUM_SCALAR > &  f,
const std::string &  type,
std::vector< typename LayerGenerator< GUM_SCALAR >::MyData > &  l 
)
private

Definition at line 97 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

100  {
101  Size size = 0;
102  GUM_SCALAR sum = 0.0;
104 
105  for (Size lvl = 0; lvl < layers__.size(); ++lvl) {
106  i.insert(l[lvl].i);
107 
108  for (Size c = 0; c < layers__[lvl].c; ++c) {
109  l[lvl].c.push_back(this->name_gen_.nextName(PRMObject::prm_type::CLASS));
110  f.startClass(l[lvl].c.back(), "", &i);
111 
112  if (lvl) f.addReferenceSlot(l[lvl - 1].i, l[lvl].r, true);
113 
114  DAG dag;
116  generateClassDag__(lvl, dag, names, l);
117 
118  // Adding aggregates
119  if (lvl) {
120  for (std::vector< std::string >::iterator g = l[lvl].g.begin();
121  g != l[lvl].g.end();
122  ++g) {
123  std::stringstream s;
124  s << l[lvl].r << "."
125  << l[lvl - 1].a[std::rand() % l[lvl - 1].a.size()];
126  std::vector< std::string > chain(1, s.str()), param(1, "1");
127  f.addAggregator(*g, "exists", chain, param);
128  }
129  }
130 
131  // Adding attributes
132  for (std::vector< std::string >::iterator a = l[lvl].a.begin();
133  a != l[lvl].a.end();
134  ++a) {
135  f.startAttribute(type, *a, true);
136  size = getDomainSize();
137 
138  for (const auto par: dag.parents(names.second(*a))) {
139  f.addParent(names.first(par));
140  size *= f.retrieveClass(l[lvl].c.back())
141  .get(names.first(par))
142  .type()
143  ->domainSize();
144  }
145 
146  std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
147 
148  for (size_t norms = 0; norms < size; norms += getDomainSize()) {
149  sum = 0.0;
150 
151  for (size_t idx = 0; idx < getDomainSize(); ++idx) {
152  val[idx] = 1 + std::rand();
153  sum += val[idx];
154  }
155 
156  for (size_t idx = 0; idx < getDomainSize(); ++idx)
157  cpf[norms + idx] = val[idx] / sum;
158  }
159 
160  f.setRawCPFByLines(cpf);
161  f.endAttribute();
162  }
163 
164  f.endClass();
165  }
166 
167  i.erase(l[lvl].i);
168  }
169  }
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
void generateClassDag__(Size lvl, DAG &dag, Bijection< std::string, NodeId > &names, std::vector< typename LayerGenerator::MyData > &l)
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
Size getDomainSize() const
Returns the domain size of generated types.
std::vector< LayerData > layers__
std::string nextName(PRMObject::prm_type type)
Getters and setters.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
NameGenerator name_gen_
The name generator used by this class.
Definition: PRMGenerator.h:80
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
+ Here is the call graph for this function:

◆ generateInterfaces__()

template<typename GUM_SCALAR >
void gum::prm::LayerGenerator< GUM_SCALAR >::generateInterfaces__ ( PRMFactory< GUM_SCALAR > &  f,
const std::string &  type,
std::vector< MyData > &  l 
)
private

Definition at line 66 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

69  {
70  for (Size lvl = 0; lvl < layers__.size(); ++lvl) {
71  l.push_back(LayerGenerator< GUM_SCALAR >::MyData());
73  f.startInterface(l[lvl].i);
74 
75  for (Size a = 0; a < layers__[lvl].a; ++a) {
76  l[lvl].a.push_back(
78  f.addAttribute(type, l[lvl].a.back());
79  }
80 
81  if (lvl) {
82  for (Size g = 0; g < layers__[lvl].g; ++g) {
83  l[lvl].g.push_back(
85  f.addAttribute("boolean", l[lvl].g.back());
86  }
87 
89  f.addReferenceSlot(l[lvl - 1].i, l[lvl].r, true);
90  }
91 
92  f.endInterface();
93  }
94  }
std::vector< LayerData > layers__
std::string nextName(PRMObject::prm_type type)
Getters and setters.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
NameGenerator name_gen_
The name generator used by this class.
Definition: PRMGenerator.h:80
+ Here is the call graph for this function:

◆ generateSystem__()

template<typename GUM_SCALAR >
void gum::prm::LayerGenerator< GUM_SCALAR >::generateSystem__ ( PRMFactory< GUM_SCALAR > &  factory,
std::vector< typename LayerGenerator< GUM_SCALAR >::MyData > &  l 
)
private

Definition at line 222 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

224  {
225  factory.startSystem(this->name_gen_.nextName(PRMObject::prm_type::SYSTEM));
226  std::vector< std::vector< std::string > > o(layers__.size());
227  std::string name;
228  size_t idx = 0;
229 
230  for (size_t lvl = 0; lvl < layers__.size(); ++lvl) {
231  float density = layers__[lvl].outter_density * RAND_MAX;
232 
233  for (size_t count = 0; count < layers__[lvl].o; ++count) {
235  factory.addInstance(l[lvl].c[std::rand() % l[lvl].c.size()], name);
236  o[lvl].push_back(name);
237 
238  if (lvl) {
239  std::stringstream chain;
240  chain << name << "." << l[lvl].r;
241  std::vector< std::string > ref2add;
242 
243  for (std::vector< std::string >::iterator iter = o[lvl - 1].begin();
244  iter != o[lvl - 1].end();
245  ++iter)
246  if (std::rand() <= density) ref2add.push_back(*iter);
247 
248  if (ref2add.empty())
249  factory.setReferenceSlot(
250  chain.str(),
251  o[lvl - 1][std::rand() % o[lvl - 1].size()]);
252 
253  while (ref2add.size() > getMaxParents()) {
254  idx = std::rand() % ref2add.size();
255  ref2add[idx] = ref2add.back();
256  ref2add.pop_back();
257  }
258 
259  for (std::vector< std::string >::iterator iter = ref2add.begin();
260  iter != ref2add.end();
261  ++iter)
262  factory.setReferenceSlot(chain.str(), *iter);
263  }
264  }
265  }
266 
267  factory.endSystem();
268  }
Size getMaxParents() const
Returns the max number of parents allowed for any attribute or aggregator.
std::vector< LayerData > layers__
std::string nextName(PRMObject::prm_type type)
Getters and setters.
NameGenerator name_gen_
The name generator used by this class.
Definition: PRMGenerator.h:80
+ Here is the call graph for this function:

◆ generateType__()

template<typename GUM_SCALAR >
std::string gum::prm::LayerGenerator< GUM_SCALAR >::generateType__ ( PRMFactory< GUM_SCALAR > &  f)
private

Definition at line 50 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

51  {
52  std::string name = this->name_gen_.nextName(PRMObject::prm_type::TYPE);
53  factory.startDiscreteType(name);
54 
55  for (Size i = 0; i < domain_size__; ++i) {
56  std::stringstream sBuff;
57  sBuff << i;
58  factory.addLabel(sBuff.str());
59  }
60 
61  factory.endDiscreteType();
62  return name;
63  }
std::string nextName(PRMObject::prm_type type)
Getters and setters.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
NameGenerator name_gen_
The name generator used by this class.
Definition: PRMGenerator.h:80
+ Here is the call graph for this function:

◆ getDomainSize()

template<typename GUM_SCALAR >
INLINE Size gum::prm::LayerGenerator< GUM_SCALAR >::getDomainSize ( ) const

Returns the domain size of generated types.

Definition at line 299 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

299  {
300  return domain_size__;
301  }
+ Here is the call graph for this function:

◆ getLayer() [1/2]

template<typename GUM_SCALAR >
INLINE std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData > & gum::prm::LayerGenerator< GUM_SCALAR >::getLayer ( )

Returns the domain size of generated types.

Definition at line 326 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

326  {
327  return layers__;
328  }
std::vector< LayerData > layers__
+ Here is the call graph for this function:

◆ getLayer() [2/2]

template<typename GUM_SCALAR >
INLINE const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData > & gum::prm::LayerGenerator< GUM_SCALAR >::getLayer ( ) const

Returns the domain size of generated types.

Definition at line 332 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

332  {
333  return layers__;
334  }
std::vector< LayerData > layers__
+ Here is the call graph for this function:

◆ getMaxParents()

template<typename GUM_SCALAR >
INLINE Size gum::prm::LayerGenerator< GUM_SCALAR >::getMaxParents ( ) const

Returns the max number of parents allowed for any attribute or aggregator.

Definition at line 309 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

309  {
310  return max_parents__;
311  }
+ Here is the call graph for this function:

◆ getNameGenerator()

template<typename GUM_SCALAR >
INLINE const NameGenerator & gum::prm::PRMGenerator< GUM_SCALAR >::getNameGenerator ( ) const
inherited

Returns the NameGenerator used by this generator.

Definition at line 57 of file PRMGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

57  {
58  return name_gen_;
59  }
NameGenerator name_gen_
The name generator used by this class.
Definition: PRMGenerator.h:80
+ Here is the call graph for this function:

◆ operator=()

template<typename GUM_SCALAR >
INLINE LayerGenerator< GUM_SCALAR > & gum::prm::LayerGenerator< GUM_SCALAR >::operator= ( const LayerGenerator< GUM_SCALAR > &  source)

Copy operator.

Definition at line 290 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

291  {
292  layers__ = source.layers__;
293  domain_size__ = source.domain_size__;
294  max_parents__ = source.max_parents__;
295  return *this;
296  }
std::vector< LayerData > layers__
+ Here is the call graph for this function:

◆ setDomainSize()

template<typename GUM_SCALAR >
INLINE void gum::prm::LayerGenerator< GUM_SCALAR >::setDomainSize ( Size  s)

Set the domain size of generated types.

Definition at line 304 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

304  {
305  domain_size__ = s;
306  }
+ Here is the call graph for this function:

◆ setLayers()

template<typename GUM_SCALAR >
INLINE void gum::prm::LayerGenerator< GUM_SCALAR >::setLayers ( const std::vector< LayerData > &  v)

Defines the structure of each layers.

Each value in v defines a layer, were v[i].first is the number of different classes in the i-th layer and v[i].second is the number of instances in the i-th layer.

Parameters
vA vector describing each layer.

Definition at line 319 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

320  {
321  layers__ = v;
322  }
std::vector< LayerData > layers__
+ Here is the call graph for this function:

◆ setMaxParents()

template<typename GUM_SCALAR >
INLINE void gum::prm::LayerGenerator< GUM_SCALAR >::setMaxParents ( Size  s)

Returns the max number of parents allowed for any attribute or aggregator.

Definition at line 314 of file layerGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

314  {
315  max_parents__ = s;
316  }
+ Here is the call graph for this function:

◆ setNameGenerator()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMGenerator< GUM_SCALAR >::setNameGenerator ( const NameGenerator name_gen)
inherited

Getters and setters.

Defines the NameGenerator used by this generator.

Definition at line 50 of file PRMGenerator_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

51  {
52  name_gen_ = name_gen;
53  }
NameGenerator name_gen_
The name generator used by this class.
Definition: PRMGenerator.h:80
+ Here is the call graph for this function:

Member Data Documentation

◆ domain_size__

template<typename GUM_SCALAR >
Size gum::prm::LayerGenerator< GUM_SCALAR >::domain_size__
private

Definition at line 127 of file layerGenerator.h.

◆ layers__

template<typename GUM_SCALAR >
std::vector< LayerData > gum::prm::LayerGenerator< GUM_SCALAR >::layers__
private

Definition at line 126 of file layerGenerator.h.

◆ max_parents__

template<typename GUM_SCALAR >
Size gum::prm::LayerGenerator< GUM_SCALAR >::max_parents__
private

Definition at line 128 of file layerGenerator.h.

◆ name_gen_

template<typename GUM_SCALAR >
NameGenerator gum::prm::PRMGenerator< GUM_SCALAR >::name_gen_
protectedinherited

The name generator used by this class.

Definition at line 80 of file PRMGenerator.h.


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