aGrUM  0.13.2
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 53 of file layerGenerator.h.

Constructor & Destructor Documentation

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

Default constructor.

Definition at line 268 of file layerGenerator_tpl.h.

268  :
269  __layers(), __domain_size(2), __max_parents(INT_MAX) {
270  GUM_CONSTRUCTOR(LayerGenerator);
271  }
std::vector< LayerData > __layers
LayerGenerator()
Default constructor.
template<typename GUM_SCALAR >
INLINE gum::prm::LayerGenerator< GUM_SCALAR >::LayerGenerator ( const LayerGenerator< GUM_SCALAR > &  source)

Copy constructor.

Definition at line 274 of file layerGenerator_tpl.h.

275  :
276  __layers(source.__layers),
277  __domain_size(source.__domain_size), __max_parents(source.__max_parents) {
278  GUM_CONS_CPY(LayerGenerator);
279  }
std::vector< LayerData > __layers
LayerGenerator()
Default constructor.
template<typename GUM_SCALAR >
INLINE gum::prm::LayerGenerator< GUM_SCALAR >::~LayerGenerator ( )
virtual

Destructor.

Definition at line 282 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::operator=().

282  {
283  GUM_DESTRUCTOR(LayerGenerator);
284  }
LayerGenerator()
Default constructor.

+ Here is the call graph for this function:

Member Function Documentation

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 170 of file layerGenerator_tpl.h.

References gum::DAG::addArc(), gum::NodeGraphPart::addNode(), gum::ArcGraphPart::eraseArc(), gum::ArcGraphPart::existsArc(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::insert(), gum::NodeGraphPart::nodes(), gum::ArcGraphPart::parents(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::second(), and gum::Set< Key, Alloc >::size().

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

+ Here is the call graph for this function:

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 95 of file layerGenerator_tpl.h.

References gum::prm::PRMFactory< GUM_SCALAR >::addAggregator(), gum::prm::PRMFactory< GUM_SCALAR >::addParent(), gum::prm::PRMFactory< GUM_SCALAR >::addReferenceSlot(), gum::prm::PRMObject::CLASS, gum::prm::PRMFactory< GUM_SCALAR >::endAttribute(), gum::prm::PRMFactory< GUM_SCALAR >::endClass(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::first(), gum::Set< Key, Alloc >::insert(), gum::ArcGraphPart::parents(), gum::prm::PRMFactory< GUM_SCALAR >::retrieveClass(), gum::BijectionImplementation< T1, T2, Alloc, Gen >::second(), gum::prm::PRMFactory< GUM_SCALAR >::setRawCPFByLines(), gum::prm::PRMFactory< GUM_SCALAR >::startAttribute(), and gum::prm::PRMFactory< GUM_SCALAR >::startClass().

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

+ Here is the call graph for this function:

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::PRMFactory< GUM_SCALAR >::addAttribute(), gum::prm::PRMFactory< GUM_SCALAR >::addReferenceSlot(), gum::prm::PRMObject::CLASS_ELT, gum::prm::PRMFactory< GUM_SCALAR >::endInterface(), gum::prm::PRMObject::PRM_INTERFACE, and gum::prm::PRMFactory< GUM_SCALAR >::startInterface().

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(
76  f.addAttribute(type, l[lvl].a.back());
77  }
78 
79  if (lvl) {
80  for (Size g = 0; g < __layers[lvl].g; ++g) {
81  l[lvl].g.push_back(
83  f.addAttribute("boolean", l[lvl].g.back());
84  }
85 
87  f.addReferenceSlot(l[lvl - 1].i, l[lvl].r, true);
88  }
89 
90  f.endInterface();
91  }
92  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
std::vector< LayerData > __layers
NameGenerator _name_gen
The name generator used by this class.
Definition: PRMGenerator.h:78
std::string nextName(PRMObject::prm_type type)
Getters and setters.

+ Here is the call graph for this function:

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 220 of file layerGenerator_tpl.h.

References gum::prm::PRMFactory< GUM_SCALAR >::addInstance(), gum::prm::PRMFactory< GUM_SCALAR >::endSystem(), gum::prm::PRMObject::PRM_INTERFACE, gum::prm::PRMFactory< GUM_SCALAR >::setReferenceSlot(), gum::prm::PRMFactory< GUM_SCALAR >::startSystem(), and gum::prm::PRMObject::SYSTEM.

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

+ Here is the call graph for this function:

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

Definition at line 48 of file layerGenerator_tpl.h.

References gum::prm::PRMFactory< GUM_SCALAR >::addLabel(), gum::prm::PRMFactory< GUM_SCALAR >::endDiscreteType(), gum::prm::PRMFactory< GUM_SCALAR >::startDiscreteType(), and gum::prm::PRMObject::TYPE.

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  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
NameGenerator _name_gen
The name generator used by this class.
Definition: PRMGenerator.h:78
std::string nextName(PRMObject::prm_type type)
Getters and setters.

+ Here is the call graph for this function:

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 32 of file layerGenerator_tpl.h.

References GUM_ERROR, and gum::prm::PRMFactory< GUM_SCALAR >::prm().

32  {
33  if (__layers.size() == 0) {
34  GUM_ERROR(OperationNotAllowed,
35  "cannot generate a layered PRM<GUM_SCALAR> without layers");
36  }
37 
38  std::vector< MyData > l;
39  PRMFactory< GUM_SCALAR > factory;
40  std::string type = __generateType(factory);
41  __generateInterfaces(factory, type, l);
42  __generateClasses(factory, type, l);
43  __generateSystem(factory, l);
44  return factory.prm();
45  }
void __generateInterfaces(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< MyData > &l)
std::vector< LayerData > __layers
void __generateClasses(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< typename LayerGenerator::MyData > &l)
void __generateSystem(PRMFactory< GUM_SCALAR > &factory, std::vector< typename LayerGenerator::MyData > &l)
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
std::string __generateType(PRMFactory< GUM_SCALAR > &f)

+ Here is the call graph for this function:

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

Returns the domain size of generated types.

Definition at line 296 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__domain_size.

296  {
297  return __domain_size;
298  }
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 323 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__layers.

323  {
324  return __layers;
325  }
std::vector< LayerData > __layers
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 329 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__layers.

329  {
330  return __layers;
331  }
std::vector< LayerData > __layers
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 306 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__max_parents.

306  {
307  return __max_parents;
308  }
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::PRMGenerator< GUM_SCALAR >::_name_gen.

55  {
56  return _name_gen;
57  }
NameGenerator _name_gen
The name generator used by this class.
Definition: PRMGenerator.h:78
template<typename GUM_SCALAR >
INLINE LayerGenerator< GUM_SCALAR > & gum::prm::LayerGenerator< GUM_SCALAR >::operator= ( const LayerGenerator< GUM_SCALAR > &  source)

Copy operator.

Definition at line 288 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__domain_size, gum::prm::LayerGenerator< GUM_SCALAR >::__layers, and gum::prm::LayerGenerator< GUM_SCALAR >::__max_parents.

Referenced by gum::prm::LayerGenerator< GUM_SCALAR >::~LayerGenerator().

288  {
289  __layers = source.__layers;
290  __domain_size = source.__domain_size;
291  __max_parents = source.__max_parents;
292  return *this;
293  }
std::vector< LayerData > __layers

+ Here is the caller graph for this function:

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

Set the domain size of generated types.

Definition at line 301 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__domain_size.

301  {
302  __domain_size = s;
303  }
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 316 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__layers.

317  {
318  __layers = v;
319  }
std::vector< LayerData > __layers
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 311 of file layerGenerator_tpl.h.

References gum::prm::LayerGenerator< GUM_SCALAR >::__max_parents.

311  {
312  __max_parents = s;
313  }
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 49 of file PRMGenerator_tpl.h.

References gum::prm::PRMGenerator< GUM_SCALAR >::_name_gen.

49  {
50  _name_gen = name_gen;
51  }
NameGenerator _name_gen
The name generator used by this class.
Definition: PRMGenerator.h:78

Member Data Documentation

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

The name generator used by this class.

Definition at line 78 of file PRMGenerator.h.

Referenced by gum::prm::PRMGenerator< GUM_SCALAR >::getNameGenerator(), and gum::prm::PRMGenerator< GUM_SCALAR >::setNameGenerator().


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