aGrUM  0.20.3
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 258 of file layerGenerator_tpl.h.

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

258  :
259  _layers_(), _domain_size_(2), _max_parents_(INT_MAX) {
260  GUM_CONSTRUCTOR(LayerGenerator);
261  }
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 265 of file layerGenerator_tpl.h.

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

265  :
266  _layers_(source._layers_),
267  _domain_size_(source._domain_size_), _max_parents_(source._max_parents_) {
268  GUM_CONS_CPY(LayerGenerator);
269  }
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 272 of file layerGenerator_tpl.h.

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

272  {
273  GUM_DESTRUCTOR(LayerGenerator);
274  }
LayerGenerator()
Default constructor.
+ Here is the call graph for this function:

Member Function Documentation

◆ _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 162 of file layerGenerator_tpl.h.

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

166  {
167  float density = _layers_[lvl].inner_density * RAND_MAX;
168  std::vector< NodeId > nodes;
169  NodeId id = 0;
170 
171  if (lvl) {
172  for (const auto agg: l[lvl].g) {
173  id = dag.addNode();
174  names.insert(agg, id);
175  nodes.push_back(id);
176  }
177  }
178 
179  for (const auto attr: l[lvl].a) {
180  id = dag.addNode();
181  names.insert(attr, id);
182 
183  for (const auto node: nodes)
184  if (std::rand() < density) dag.addArc(node, names.second(attr));
185 
186  nodes.push_back(id);
187  }
188 
189  // For each nodes with #parents > _max_parents_ we randomly remove parents
190  // until
191  // #parents <= _max_parents_
192  for (const auto node: dag.nodes()) {
193  if (dag.parents(node).size() > getMaxParents()) {
194  std::vector< NodeId > v;
195 
196  for (const auto par: dag.parents(node))
197  v.push_back(par);
198 
199  while (dag.parents(node).size() > getMaxParents()) {
200  size_t idx = std::rand() % v.size();
201  Arc arc(v[idx], node);
202  GUM_ASSERT(dag.existsArc(arc));
203  dag.eraseArc(arc);
204  v[idx] = v.back();
205  v.pop_back();
206  }
207  }
208  }
209  }
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.
std::vector< LayerData > _layers_
Size getMaxParents() const
Returns the max number of parents allowed for any attribute or aggregator.
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 93 of file layerGenerator_tpl.h.

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

96  {
97  Size size = 0;
98  GUM_SCALAR sum = 0.0;
100 
101  for (Size lvl = 0; lvl < _layers_.size(); ++lvl) {
102  i.insert(l[lvl].i);
103 
104  for (Size c = 0; c < _layers_[lvl].c; ++c) {
105  l[lvl].c.push_back(this->name_gen_.nextName(PRMObject::prm_type::CLASS));
106  f.startClass(l[lvl].c.back(), "", &i);
107 
108  if (lvl) f.addReferenceSlot(l[lvl - 1].i, l[lvl].r, true);
109 
110  DAG dag;
112  _generateClassDag_(lvl, dag, names, l);
113 
114  // Adding aggregates
115  if (lvl) {
116  for (std::vector< std::string >::iterator g = l[lvl].g.begin(); g != l[lvl].g.end();
117  ++g) {
118  std::stringstream s;
119  s << l[lvl].r << "." << l[lvl - 1].a[std::rand() % l[lvl - 1].a.size()];
120  std::vector< std::string > chain(1, s.str()), param(1, "1");
121  f.addAggregator(*g, "exists", chain, param);
122  }
123  }
124 
125  // Adding attributes
126  for (std::vector< std::string >::iterator a = l[lvl].a.begin(); a != l[lvl].a.end();
127  ++a) {
128  f.startAttribute(type, *a, true);
129  size = getDomainSize();
130 
131  for (const auto par: dag.parents(names.second(*a))) {
132  f.addParent(names.first(par));
133  size *= f.retrieveClass(l[lvl].c.back()).get(names.first(par)).type()->domainSize();
134  }
135 
136  std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
137 
138  for (size_t norms = 0; norms < size; norms += getDomainSize()) {
139  sum = 0.0;
140 
141  for (size_t idx = 0; idx < getDomainSize(); ++idx) {
142  val[idx] = 1 + std::rand();
143  sum += val[idx];
144  }
145 
146  for (size_t idx = 0; idx < getDomainSize(); ++idx)
147  cpf[norms + idx] = val[idx] / sum;
148  }
149 
150  f.setRawCPFByLines(cpf);
151  f.endAttribute();
152  }
153 
154  f.endClass();
155  }
156 
157  i.erase(l[lvl].i);
158  }
159  }
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
std::vector< LayerData > _layers_
Size getDomainSize() const
Returns the domain size of generated types.
void _generateClassDag_(Size lvl, DAG &dag, Bijection< std::string, NodeId > &names, std::vector< typename LayerGenerator::MyData > &l)
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:606
+ 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 64 of file layerGenerator_tpl.h.

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

67  {
68  for (Size lvl = 0; lvl < _layers_.size(); ++lvl) {
69  l.push_back(LayerGenerator< GUM_SCALAR >::MyData());
71  f.startInterface(l[lvl].i);
72 
73  for (Size a = 0; a < _layers_[lvl].a; ++a) {
74  l[lvl].a.push_back(this->name_gen_.nextName(PRMObject::prm_type::CLASS_ELT));
75  f.addAttribute(type, l[lvl].a.back());
76  }
77 
78  if (lvl) {
79  for (Size g = 0; g < _layers_[lvl].g; ++g) {
80  l[lvl].g.push_back(this->name_gen_.nextName(PRMObject::prm_type::CLASS_ELT));
81  f.addAttribute("boolean", l[lvl].g.back());
82  }
83 
85  f.addReferenceSlot(l[lvl - 1].i, l[lvl].r, true);
86  }
87 
88  f.endInterface();
89  }
90  }
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 212 of file layerGenerator_tpl.h.

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

214  {
215  factory.startSystem(this->name_gen_.nextName(PRMObject::prm_type::SYSTEM));
216  std::vector< std::vector< std::string > > o(_layers_.size());
217  std::string name;
218  size_t idx = 0;
219 
220  for (size_t lvl = 0; lvl < _layers_.size(); ++lvl) {
221  float density = _layers_[lvl].outter_density * RAND_MAX;
222 
223  for (size_t count = 0; count < _layers_[lvl].o; ++count) {
225  factory.addInstance(l[lvl].c[std::rand() % l[lvl].c.size()], name);
226  o[lvl].push_back(name);
227 
228  if (lvl) {
229  std::stringstream chain;
230  chain << name << "." << l[lvl].r;
231  std::vector< std::string > ref2add;
232 
233  for (std::vector< std::string >::iterator iter = o[lvl - 1].begin();
234  iter != o[lvl - 1].end();
235  ++iter)
236  if (std::rand() <= density) ref2add.push_back(*iter);
237 
238  if (ref2add.empty())
239  factory.setReferenceSlot(chain.str(), o[lvl - 1][std::rand() % o[lvl - 1].size()]);
240 
241  while (ref2add.size() > getMaxParents()) {
242  idx = std::rand() % ref2add.size();
243  ref2add[idx] = ref2add.back();
244  ref2add.pop_back();
245  }
246 
247  for (std::vector< std::string >::iterator iter = ref2add.begin(); iter != ref2add.end();
248  ++iter)
249  factory.setReferenceSlot(chain.str(), *iter);
250  }
251  }
252  }
253 
254  factory.endSystem();
255  }
std::vector< LayerData > _layers_
Size getMaxParents() const
Returns the max number of parents allowed for any attribute or aggregator.
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 49 of file layerGenerator_tpl.h.

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

49  {
50  std::string name = this->name_gen_.nextName(PRMObject::prm_type::TYPE);
51  factory.startDiscreteType(name);
52 
53  for (Size i = 0; i < _domain_size_; ++i) {
54  std::stringstream sBuff;
55  sBuff << i;
56  factory.addLabel(sBuff.str());
57  }
58 
59  factory.endDiscreteType();
60  return name;
61  }
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:

◆ 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, "cannot generate a layered PRM<GUM_SCALAR> without layers")
37  }
38 
39  std::vector< MyData > l;
40  PRMFactory< GUM_SCALAR > factory;
41  std::string type = _generateType_(factory);
42  _generateInterfaces_(factory, type, l);
43  _generateClasses_(factory, type, l);
44  _generateSystem_(factory, l);
45  return factory.prm();
46  }
std::vector< LayerData > _layers_
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)
std::string _generateType_(PRMFactory< GUM_SCALAR > &f)
void _generateSystem_(PRMFactory< GUM_SCALAR > &factory, std::vector< typename LayerGenerator::MyData > &l)
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ 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 286 of file layerGenerator_tpl.h.

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

286  {
287  return _domain_size_;
288  }
+ 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 313 of file layerGenerator_tpl.h.

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

313  {
314  return _layers_;
315  }
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 319 of file layerGenerator_tpl.h.

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

319  {
320  return _layers_;
321  }
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 296 of file layerGenerator_tpl.h.

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

296  {
297  return _max_parents_;
298  }
+ 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 55 of file PRMGenerator_tpl.h.

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

55  {
56  return name_gen_;
57  }
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 278 of file layerGenerator_tpl.h.

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

278  {
279  _layers_ = source._layers_;
280  _domain_size_ = source._domain_size_;
281  _max_parents_ = source._max_parents_;
282  return *this;
283  }
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 291 of file layerGenerator_tpl.h.

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

291  {
292  _domain_size_ = s;
293  }
+ 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 306 of file layerGenerator_tpl.h.

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

307  {
308  _layers_ = v;
309  }
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 301 of file layerGenerator_tpl.h.

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

301  {
302  _max_parents_ = s;
303  }
+ 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().

50  {
51  name_gen_ = name_gen;
52  }
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: