33 template <
typename GUM_SCALAR >
35 if (__layers.size() == 0) {
37 "cannot generate a layered PRM<GUM_SCALAR> without layers");
40 std::vector< MyData > l;
42 std::string type = __generateType(factory);
43 __generateInterfaces(factory, type, l);
44 __generateClasses(factory, type, l);
45 __generateSystem(factory, l);
49 template <
typename GUM_SCALAR >
55 for (
Size i = 0; i < __domain_size; ++i) {
56 std::stringstream sBuff;
65 template <
typename GUM_SCALAR >
68 const std::string& type,
70 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
75 for (
Size a = 0; a < __layers[lvl].a; ++a) {
82 for (
Size g = 0; g < __layers[lvl].g; ++g) {
96 template <
typename GUM_SCALAR >
99 const std::string& type,
104 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
107 for (
Size c = 0; c < __layers[lvl].c; ++c) {
109 __generateCluster(f, type, l, lvl, i);
118 template <
typename GUM_SCALAR >
121 const std::string& type,
126 GUM_SCALAR sum = 0.0;
127 std::string first, second, third;
128 std::vector< std::string >* v = 0;
130 switch (std::rand() % 2) {
134 v =
new std::vector< std::string >();
135 __generateClass(f, type, l, lvl, i);
136 first = l[lvl].c.back();
144 __generateClassDag(lvl, dag, names, l);
147 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
151 s << v->back() <<
"." << l[lvl].a[std::rand() % l[lvl].a.size()];
152 std::vector< std::string > chain(1, s.str()), param(1,
"1");
157 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
161 size = getDomainSize();
166 .
get(names.
first(par))
171 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
173 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
176 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
177 val[idx] = 1 + std::rand();
181 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
182 cpf[norms + idx] = val[idx] / sum;
196 v =
new std::vector< std::string >();
197 __generateClass(f, type, l, lvl, i);
199 first = l[lvl].c.back();
208 __generateClassDag(lvl, dag, names, l);
211 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
215 s << v->back() <<
"." << l[lvl].a[std::rand() % l[lvl].a.size()];
216 std::vector< std::string > chain(1, s.str()), param(1,
"1");
221 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
225 size = getDomainSize();
230 .
get(names.
first(par))
235 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
237 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
240 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
241 val[idx] = 1 + std::rand();
245 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
246 cpf[norms + idx] = val[idx] / sum;
263 __generateClassDag(lvl, dag, names, l);
266 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
270 s << v->back() <<
"." << l[lvl].a[std::rand() % l[lvl].a.size()];
271 std::vector< std::string > chain(1, s.str()), param(1,
"1");
276 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
280 size = getDomainSize();
285 .
get(names.
first(par))
290 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
292 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
295 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
296 val[idx] = 1 + std::rand();
300 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
301 cpf[norms + idx] = val[idx] / sum;
318 __cluster_map.insert(first, v);
321 template <
typename GUM_SCALAR >
324 const std::string& type,
329 GUM_SCALAR sum = 0.0;
337 __generateClassDag(lvl, dag, names, l);
341 for (
const auto agg : l[lvl].g) {
343 s << l[lvl].r <<
"." << l[lvl - 1].a[std::rand() % l[lvl - 1].a.size()];
344 std::vector< std::string > chain(1, s.str()), param(1,
"1");
350 for (
const auto attr : l[lvl].a) {
352 size = getDomainSize();
357 .
get(names.
first(par))
362 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
364 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
367 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
368 val[idx] = 1 + std::rand();
372 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
373 cpf[norms + idx] = val[idx] / sum;
383 template <
typename GUM_SCALAR >
389 float density = __layers[lvl].inner_density * RAND_MAX;
390 std::vector< NodeId > nodes;
394 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
403 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
409 for (std::vector< NodeId >::iterator prnt = nodes.begin();
412 if (std::rand() < density) dag.
addArc(*prnt, names.
second(*a));
420 for (
const auto node : dag.
nodes()) {
422 std::vector< NodeId > v;
424 for (
const auto par : dag.
parents(node))
427 while (dag.
parents(node).
size() > getMaxParents()) {
428 size_t idx = std::rand() % v.size();
429 Arc arc(v[idx], node);
439 template <
typename GUM_SCALAR >
444 std::vector< std::vector< std::string > > o(__layers.size());
445 std::string name, c, first, second, third;
446 std::vector< std::string >* v = 0;
449 for (
size_t lvl = 0; lvl < __layers.size(); ++lvl) {
450 float density = __layers[lvl].outter_density * RAND_MAX;
452 for (
size_t count = 0; count < __layers[lvl].o; ++count) {
453 c = l[lvl].c[std::rand() % l[lvl].c.size()];
455 if (__cluster_map.exists(c)) {
456 v = __cluster_map[c];
464 std::stringstream chain;
465 chain << second <<
"." << v->at(2);
475 std::stringstream chain_1, chain_2;
476 chain_1 << second <<
"." << v->at(2);
480 chain_2 << third <<
"." << v->at(4);
498 o[lvl].push_back(name);
501 std::stringstream chain;
502 chain << name <<
"." << l[lvl].r;
503 std::vector< std::string > ref2add;
505 for (std::vector< std::string >::iterator iter = o[lvl - 1].begin();
506 iter != o[lvl - 1].end();
508 if (std::rand() <= density) ref2add.push_back(*iter);
512 chain.str(), o[lvl - 1][std::rand() % o[lvl - 1].size()]);
514 while (ref2add.size() > getMaxParents()) {
515 idx = std::rand() % ref2add.size();
516 ref2add[idx] = ref2add.back();
520 for (std::vector< std::string >::iterator iter = ref2add.begin();
521 iter != ref2add.end();
531 template <
typename GUM_SCALAR >
533 __layers(), __domain_size(2), __max_parents(INT_MAX),
534 __cluster_ratio(0.0) {
538 template <
typename GUM_SCALAR >
546 template <
typename GUM_SCALAR >
558 template <
typename GUM_SCALAR >
568 template <
typename GUM_SCALAR >
573 template <
typename GUM_SCALAR >
578 template <
typename GUM_SCALAR >
583 template <
typename GUM_SCALAR >
588 template <
typename GUM_SCALAR >
594 template <
typename GUM_SCALAR >
595 INLINE std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
600 template <
typename GUM_SCALAR >
601 INLINE
const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
606 template <
typename GUM_SCALAR >
611 template <
typename GUM_SCALAR >
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
Size getMaxParents() const
Returns the max number of parents allowed for any attribute or aggregator.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
<agrum/PRM/generator/clusteredLayerGenerator.h>
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 > ¶ms, std::string type="") override
Add an aggregator in the current declared class.
std::string __generateType(PRMFactory< GUM_SCALAR > &f)
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.
void __generateSystem(PRMFactory< GUM_SCALAR > &factory, std::vector< typename ClusteredLayerGenerator::MyData > &l)
void setLayers(const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData > &v)
Defines the structure of each layers.
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
std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData > & getLayer()
Getters and setters.
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.
virtual void addAttribute(const std::string &type, const std::string &name) override
Add an attribute to an interface.
double getClusterRatio() const
Returns the odds of a given class to be replaced by a cluster.
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.
ClusteredLayerGenerator & operator=(const ClusteredLayerGenerator &source)
Copy operator.
virtual void startSystem(const std::string &name) override
Tells the factory that we started declaring a model.
void __generateInterfaces(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< MyData > &l)
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.
std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData > __layers
Factory which builds a PRM<GUM_SCALAR>.
void __generateClassDag(Size lvl, DAG &dag, Bijection< std::string, NodeId > &names, std::vector< typename ClusteredLayerGenerator::MyData > &l)
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
void __generateClasses(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< typename ClusteredLayerGenerator::MyData > &l)
virtual void endClass(bool checkImplementations=true) override
Tells the factory that we finished a class declaration.
void setRawCPFByLines(const std::vector< GUM_SCALAR > &array)
Gives the factory the CPF in its raw form.
ClusteredLayerGenerator()
Default constructor.
virtual PRM< GUM_SCALAR > * generate()
Proceeds with the generation of the PRM<GUM_SCALAR>.
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
virtual void startDiscreteType(const std::string &name, std::string super="") override
Start a discrete subtype declaration.
void __generateClass(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< typename ClusteredLayerGenerator::MyData > &l, Size lvl, Set< std::string > &i)
virtual ~ClusteredLayerGenerator()
Destructor.
virtual void endDiscreteType() override
End the current discrete type declaration.
void setClusterRatio(double ratio)
Define the odds of a given class to be replaced by a cluster.
void setDomainSize(Size s)
Set the domain size of generated types.
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.
void __generateCluster(PRMFactory< GUM_SCALAR > &f, const std::string &type, std::vector< typename ClusteredLayerGenerator::MyData > &l, Size lvl, Set< std::string > &i)
Size size() const noexcept
Returns the number of elements in the set.
virtual void endSystem() override
Tells the factory that we finished declaring a model.
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Size getDomainSize() const
Getters and setters.
Size NodeId
Type for node ids.
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.
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)