aGrUM  0.16.0
layerGenerator_tpl.h
Go to the documentation of this file.
1 
30 
31 namespace gum {
32  namespace prm {
33 
34  template < typename GUM_SCALAR >
36  if (__layers.size() == 0) {
38  "cannot generate a layered PRM<GUM_SCALAR> without layers");
39  }
40 
41  std::vector< MyData > l;
43  std::string type = __generateType(factory);
44  __generateInterfaces(factory, type, l);
45  __generateClasses(factory, type, l);
46  __generateSystem(factory, l);
47  return factory.prm();
48  }
49 
50  template < typename GUM_SCALAR >
52  PRMFactory< GUM_SCALAR >& factory) {
53  std::string name = this->_name_gen.nextName(PRMObject::prm_type::TYPE);
54  factory.startDiscreteType(name);
55 
56  for (Size i = 0; i < __domain_size; ++i) {
57  std::stringstream sBuff;
58  sBuff << i;
59  factory.addLabel(sBuff.str());
60  }
61 
62  factory.endDiscreteType();
63  return name;
64  }
65 
66  template < typename GUM_SCALAR >
69  const std::string& type,
70  std::vector< typename LayerGenerator< GUM_SCALAR >::MyData >& l) {
71  for (Size lvl = 0; lvl < __layers.size(); ++lvl) {
73  l[lvl].i = this->_name_gen.nextName(PRMObject::prm_type::PRM_INTERFACE);
74  f.startInterface(l[lvl].i);
75 
76  for (Size a = 0; a < __layers[lvl].a; ++a) {
77  l[lvl].a.push_back(
78  this->_name_gen.nextName(PRMObject::prm_type::CLASS_ELT));
79  f.addAttribute(type, l[lvl].a.back());
80  }
81 
82  if (lvl) {
83  for (Size g = 0; g < __layers[lvl].g; ++g) {
84  l[lvl].g.push_back(
85  this->_name_gen.nextName(PRMObject::prm_type::CLASS_ELT));
86  f.addAttribute("boolean", l[lvl].g.back());
87  }
88 
89  l[lvl].r = this->_name_gen.nextName(PRMObject::prm_type::CLASS_ELT);
90  f.addReferenceSlot(l[lvl - 1].i, l[lvl].r, true);
91  }
92 
93  f.endInterface();
94  }
95  }
96 
97  template < typename GUM_SCALAR >
100  const std::string& type,
101  std::vector< typename LayerGenerator< GUM_SCALAR >::MyData >& l) {
102  Size size = 0;
103  GUM_SCALAR sum = 0.0;
105 
106  for (Size lvl = 0; lvl < __layers.size(); ++lvl) {
107  i.insert(l[lvl].i);
108 
109  for (Size c = 0; c < __layers[lvl].c; ++c) {
110  l[lvl].c.push_back(this->_name_gen.nextName(PRMObject::prm_type::CLASS));
111  f.startClass(l[lvl].c.back(), "", &i);
112 
113  if (lvl) f.addReferenceSlot(l[lvl - 1].i, l[lvl].r, true);
114 
115  DAG dag;
117  __generateClassDag(lvl, dag, names, l);
118 
119  // Adding aggregates
120  if (lvl) {
121  for (std::vector< std::string >::iterator g = l[lvl].g.begin();
122  g != l[lvl].g.end();
123  ++g) {
124  std::stringstream s;
125  s << l[lvl].r << "."
126  << l[lvl - 1].a[std::rand() % l[lvl - 1].a.size()];
127  std::vector< std::string > chain(1, s.str()), param(1, "1");
128  f.addAggregator(*g, "exists", chain, param);
129  }
130  }
131 
132  // Adding attributes
133  for (std::vector< std::string >::iterator a = l[lvl].a.begin();
134  a != l[lvl].a.end();
135  ++a) {
136  f.startAttribute(type, *a, true);
137  size = getDomainSize();
138 
139  for (const auto par : dag.parents(names.second(*a))) {
140  f.addParent(names.first(par));
141  size *= f.retrieveClass(l[lvl].c.back())
142  .get(names.first(par))
143  .type()
144  ->domainSize();
145  }
146 
147  std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
148 
149  for (size_t norms = 0; norms < size; norms += getDomainSize()) {
150  sum = 0.0;
151 
152  for (size_t idx = 0; idx < getDomainSize(); ++idx) {
153  val[idx] = 1 + std::rand();
154  sum += val[idx];
155  }
156 
157  for (size_t idx = 0; idx < getDomainSize(); ++idx)
158  cpf[norms + idx] = val[idx] / sum;
159  }
160 
161  f.setRawCPFByLines(cpf);
162  f.endAttribute();
163  }
164 
165  f.endClass();
166  }
167 
168  i.erase(l[lvl].i);
169  }
170  }
171 
172  template < typename GUM_SCALAR >
174  Size lvl,
175  DAG& dag,
177  std::vector< typename LayerGenerator< GUM_SCALAR >::MyData >& l) {
178  float density = __layers[lvl].inner_density * RAND_MAX;
179  std::vector< NodeId > nodes;
180  NodeId id = 0;
181 
182  if (lvl) {
183  for (const auto agg : l[lvl].g) {
184  id = dag.addNode();
185  names.insert(agg, id);
186  nodes.push_back(id);
187  }
188  }
189 
190  for (const auto attr : l[lvl].a) {
191  id = dag.addNode();
192  names.insert(attr, id);
193 
194  for (const auto node : nodes)
195  if (std::rand() < density) dag.addArc(node, names.second(attr));
196 
197  nodes.push_back(id);
198  }
199 
200  // For each nodes with #parents > __max_parents we randomly remove parents
201  // until
202  // #parents <= __max_parents
203  for (const auto node : dag.nodes()) {
204  if (dag.parents(node).size() > getMaxParents()) {
205  std::vector< NodeId > v;
206 
207  for (const auto par : dag.parents(node))
208  v.push_back(par);
209 
210  while (dag.parents(node).size() > getMaxParents()) {
211  size_t idx = std::rand() % v.size();
212  Arc arc(v[idx], node);
213  GUM_ASSERT(dag.existsArc(arc));
214  dag.eraseArc(arc);
215  v[idx] = v.back();
216  v.pop_back();
217  }
218  }
219  }
220  }
221 
222  template < typename GUM_SCALAR >
224  PRMFactory< GUM_SCALAR >& factory,
225  std::vector< typename LayerGenerator< GUM_SCALAR >::MyData >& l) {
226  factory.startSystem(this->_name_gen.nextName(PRMObject::prm_type::SYSTEM));
227  std::vector< std::vector< std::string > > o(__layers.size());
228  std::string name;
229  size_t idx = 0;
230 
231  for (size_t lvl = 0; lvl < __layers.size(); ++lvl) {
232  float density = __layers[lvl].outter_density * RAND_MAX;
233 
234  for (size_t count = 0; count < __layers[lvl].o; ++count) {
235  name = this->_name_gen.nextName(PRMObject::prm_type::PRM_INTERFACE);
236  factory.addInstance(l[lvl].c[std::rand() % l[lvl].c.size()], name);
237  o[lvl].push_back(name);
238 
239  if (lvl) {
240  std::stringstream chain;
241  chain << name << "." << l[lvl].r;
242  std::vector< std::string > ref2add;
243 
244  for (std::vector< std::string >::iterator iter = o[lvl - 1].begin();
245  iter != o[lvl - 1].end();
246  ++iter)
247  if (std::rand() <= density) ref2add.push_back(*iter);
248 
249  if (ref2add.empty())
250  factory.setReferenceSlot(
251  chain.str(), 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  }
269 
270  template < typename GUM_SCALAR >
272  __layers(), __domain_size(2), __max_parents(INT_MAX) {
273  GUM_CONSTRUCTOR(LayerGenerator);
274  }
275 
276  template < typename GUM_SCALAR >
278  const LayerGenerator< GUM_SCALAR >& source) :
279  __layers(source.__layers),
281  GUM_CONS_CPY(LayerGenerator);
282  }
283 
284  template < typename GUM_SCALAR >
286  GUM_DESTRUCTOR(LayerGenerator);
287  }
288 
289  template < typename GUM_SCALAR >
292  __layers = source.__layers;
293  __domain_size = source.__domain_size;
294  __max_parents = source.__max_parents;
295  return *this;
296  }
297 
298  template < typename GUM_SCALAR >
300  return __domain_size;
301  }
302 
303  template < typename GUM_SCALAR >
305  __domain_size = s;
306  }
307 
308  template < typename GUM_SCALAR >
310  return __max_parents;
311  }
312 
313  template < typename GUM_SCALAR >
315  __max_parents = s;
316  }
317 
318  template < typename GUM_SCALAR >
320  const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >& v) {
321  __layers = v;
322  }
323 
324  template < typename GUM_SCALAR >
325  INLINE std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
327  return __layers;
328  }
329 
330  template < typename GUM_SCALAR >
331  INLINE const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
333  return __layers;
334  }
335 
336  } /* namespace prm */
337 } /* namespace gum */
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.
virtual void endInterface() override
Tells the factory that we finished an interface declaration.
virtual void addAggregator(const std::string &name, const std::string &agg_type, const std::vector< std::string > &chains, const std::vector< std::string > &params, std::string type="") override
Add an aggregator in the current declared class.
virtual void setReferenceSlot(const std::string &left_instance, const std::string &left_reference, const std::string &right_instance) override
Instantiate a reference in the current model.
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
virtual void addInstance(const std::string &type, const std::string &name) override
Add an instance to the model.
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
PRMClass< GUM_SCALAR > & retrieveClass(const std::string &name)
Returns a reference over a Class<GUM_SCALAR> given its name.
virtual void endAttribute() override
Tells the factory that we finished declaring an attribute.
virtual void startInterface(const std::string &i, const std::string &ext="", bool delayInheritance=false) override
Tells the factory that we start an interface declaration.
virtual void addReferenceSlot(const std::string &type, const std::string &name, bool isArray) override
Tells the factory that we started declaring a slot.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void __generateInterfaces(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< MyData > &l)
virtual void addAttribute(const std::string &type, const std::string &name) override
Add an attribute to an interface.
virtual NodeId addNode()
insert a new node and return its id
virtual void startClass(const std::string &c, const std::string &ext="", const Set< std::string > *implements=nullptr, bool delayInheritance=false) override
Tells the factory that we start a class declaration.
std::vector< LayerData > __layers
virtual void startSystem(const std::string &name) override
Tells the factory that we started declaring a model.
LayerGenerator()
Default constructor.
virtual void startAttribute(const std::string &type, const std::string &name, bool scalar_atttr=false) override
Tells the factory that we start an attribute declaration.
void __generateClasses(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< typename LayerGenerator::MyData > &l)
void setDomainSize(Size s)
Set the domain size of generated types.
Factory which builds a PRM<GUM_SCALAR>.
Definition: PRMType.h:50
Size getMaxParents() const
Returns the max number of parents allowed for any attribute or aggregator.
Size getDomainSize() const
Returns the domain size of generated types.
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:43
void __generateSystem(PRMFactory< GUM_SCALAR > &factory, std::vector< typename LayerGenerator::MyData > &l)
<agrum/PRM/generator/layerGenerator.h>
virtual void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
void setLayers(const std::vector< LayerData > &v)
Defines the structure of each layers.
virtual PRM< GUM_SCALAR > * generate()
Proceeds with the generation of the PRM<GUM_SCALAR>.
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
virtual void startDiscreteType(const std::string &name, std::string super="") override
Start a discrete subtype declaration.
virtual ~LayerGenerator()
Destructor.
std::vector< LayerData > & getLayer()
Returns the domain size of generated types.
virtual void endDiscreteType() override
End the current discrete type declaration.
void setMaxParents(Size s)
Returns the max number of parents allowed for any attribute or aggregator.
virtual void addLabel(const std::string &l, std::string ext="") override
Add a label to the current discrete type.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
virtual void endSystem() override
Tells the factory that we finished declaring a model.
Base class for dag.
Definition: DAG.h:102
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Size NodeId
Type for node ids.
Definition: graphElements.h:98
PRM< GUM_SCALAR > * prm() const
Returns a pointer on the PRM<GUM_SCALAR> created by this factory.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
LayerGenerator & operator=(const LayerGenerator &source)
Copy operator.
virtual void addParent(const std::string &name) override
Tells the factory that we add a parent to the current declared attribute.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
void __generateClassDag(Size lvl, DAG &dag, Bijection< std::string, NodeId > &names, std::vector< typename LayerGenerator::MyData > &l)
std::string __generateType(PRMFactory< GUM_SCALAR > &f)