30 template <
typename GUM_SCALAR >
32 if (__layers.size() == 0) {
34 "cannot generate a layered PRM<GUM_SCALAR> without layers");
37 std::vector< MyData > l;
39 std::string type = __generateType(factory);
40 __generateInterfaces(factory, type, l);
41 __generateClasses(factory, type, l);
42 __generateSystem(factory, l);
46 template <
typename GUM_SCALAR >
52 for (
Size i = 0; i < __domain_size; ++i) {
53 std::stringstream sBuff;
62 template <
typename GUM_SCALAR >
65 const std::string& type,
67 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
72 for (
Size a = 0; a < __layers[lvl].a; ++a) {
79 for (
Size g = 0; g < __layers[lvl].g; ++g) {
93 template <
typename GUM_SCALAR >
96 const std::string& type,
101 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
104 for (
Size c = 0; c < __layers[lvl].c; ++c) {
106 __generateCluster(f, type, l, lvl, i);
115 template <
typename GUM_SCALAR >
118 const std::string& type,
123 GUM_SCALAR sum = 0.0;
124 std::string first, second, third;
125 std::vector< std::string >* v = 0;
127 switch (std::rand() % 2) {
131 v =
new std::vector< std::string >();
132 __generateClass(f, type, l, lvl, i);
133 first = l[lvl].c.back();
141 __generateClassDag(lvl, dag, names, l);
144 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
148 s << v->back() <<
"." << l[lvl].a[std::rand() % l[lvl].a.size()];
149 std::vector< std::string > chain(1, s.str()), param(1,
"1");
154 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
158 size = getDomainSize();
163 .
get(names.
first(par))
168 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
170 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
173 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
174 val[idx] = 1 + std::rand();
178 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
179 cpf[norms + idx] = val[idx] / sum;
193 v =
new std::vector< std::string >();
194 __generateClass(f, type, l, lvl, i);
196 first = l[lvl].c.back();
205 __generateClassDag(lvl, dag, names, l);
208 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
212 s << v->back() <<
"." << l[lvl].a[std::rand() % l[lvl].a.size()];
213 std::vector< std::string > chain(1, s.str()), param(1,
"1");
218 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
222 size = getDomainSize();
227 .
get(names.
first(par))
232 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
234 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
237 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
238 val[idx] = 1 + std::rand();
242 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
243 cpf[norms + idx] = val[idx] / sum;
260 __generateClassDag(lvl, dag, names, l);
263 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
267 s << v->back() <<
"." << l[lvl].a[std::rand() % l[lvl].a.size()];
268 std::vector< std::string > chain(1, s.str()), param(1,
"1");
273 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
277 size = getDomainSize();
282 .
get(names.
first(par))
287 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
289 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
292 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
293 val[idx] = 1 + std::rand();
297 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
298 cpf[norms + idx] = val[idx] / sum;
313 __cluster_map.insert(first, v);
316 template <
typename GUM_SCALAR >
319 const std::string& type,
324 GUM_SCALAR sum = 0.0;
332 __generateClassDag(lvl, dag, names, l);
336 for (
const auto agg : l[lvl].g) {
338 s << l[lvl].r <<
"." << l[lvl - 1].a[std::rand() % l[lvl - 1].a.size()];
339 std::vector< std::string > chain(1, s.str()), param(1,
"1");
345 for (
const auto attr : l[lvl].a) {
347 size = getDomainSize();
352 .
get(names.
first(par))
357 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
359 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
362 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
363 val[idx] = 1 + std::rand();
367 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
368 cpf[norms + idx] = val[idx] / sum;
378 template <
typename GUM_SCALAR >
384 float density = __layers[lvl].inner_density * RAND_MAX;
385 std::vector< NodeId > nodes;
389 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
398 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
404 for (std::vector< NodeId >::iterator prnt = nodes.begin();
407 if (std::rand() < density) dag.
addArc(*prnt, names.
second(*a));
415 for (
const auto node : dag.
nodes()) {
417 std::vector< NodeId > v;
419 for (
const auto par : dag.
parents(node))
422 while (dag.
parents(node).
size() > getMaxParents()) {
423 size_t idx = std::rand() % v.size();
424 Arc arc(v[idx], node);
434 template <
typename GUM_SCALAR >
439 std::vector< std::vector< std::string > > o(__layers.size());
440 std::string name, c, first, second, third;
441 std::vector< std::string >* v = 0;
444 for (
size_t lvl = 0; lvl < __layers.size(); ++lvl) {
445 float density = __layers[lvl].outter_density * RAND_MAX;
447 for (
size_t count = 0; count < __layers[lvl].o; ++count) {
448 c = l[lvl].c[std::rand() % l[lvl].c.size()];
450 if (__cluster_map.exists(c)) {
451 v = __cluster_map[c];
459 std::stringstream chain;
460 chain << second <<
"." << v->at(2);
470 std::stringstream chain_1, chain_2;
471 chain_1 << second <<
"." << v->at(2);
475 chain_2 << third <<
"." << v->at(4);
493 o[lvl].push_back(name);
496 std::stringstream chain;
497 chain << name <<
"." << l[lvl].r;
498 std::vector< std::string > ref2add;
500 for (std::vector< std::string >::iterator iter = o[lvl - 1].begin();
501 iter != o[lvl - 1].end();
503 if (std::rand() <= density) ref2add.push_back(*iter);
507 chain.str(), o[lvl - 1][std::rand() % o[lvl - 1].size()]);
509 while (ref2add.size() > getMaxParents()) {
510 idx = std::rand() % ref2add.size();
511 ref2add[idx] = ref2add.back();
515 for (std::vector< std::string >::iterator iter = ref2add.begin();
516 iter != ref2add.end();
526 template <
typename GUM_SCALAR >
528 __layers(), __domain_size(2), __max_parents(INT_MAX),
529 __cluster_ratio(0.0) {
533 template <
typename GUM_SCALAR >
541 template <
typename GUM_SCALAR >
553 template <
typename GUM_SCALAR >
563 template <
typename GUM_SCALAR >
568 template <
typename GUM_SCALAR >
573 template <
typename GUM_SCALAR >
578 template <
typename GUM_SCALAR >
583 template <
typename GUM_SCALAR >
589 template <
typename GUM_SCALAR >
590 INLINE std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
595 template <
typename GUM_SCALAR >
596 INLINE
const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
601 template <
typename GUM_SCALAR >
606 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.
gum is the global namespace for all aGrUM entities
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)