31 template <
typename GUM_SCALAR >
33 if (__layers.size() == 0) {
35 "cannot generate a layered PRM<GUM_SCALAR> without layers");
38 std::vector< MyData > l;
40 std::string type = __generateType(factory);
41 __generateInterfaces(factory, type, l);
42 __generateClasses(factory, type, l);
43 __generateSystem(factory, l);
47 template <
typename GUM_SCALAR >
53 for (
Size i = 0; i < __domain_size; ++i) {
54 std::stringstream sBuff;
63 template <
typename GUM_SCALAR >
66 const std::string& type,
68 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
73 for (
Size a = 0; a < __layers[lvl].a; ++a) {
80 for (
Size g = 0; g < __layers[lvl].g; ++g) {
94 template <
typename GUM_SCALAR >
97 const std::string& type,
100 GUM_SCALAR sum = 0.0;
103 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
106 for (
Size c = 0; c < __layers[lvl].c; ++c) {
114 __generateClassDag(lvl, dag, names, l);
118 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
123 << l[lvl - 1].a[std::rand() % l[lvl - 1].a.size()];
124 std::vector< std::string > chain(1, s.str()), param(1,
"1");
130 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
134 size = getDomainSize();
139 .
get(names.
first(par))
144 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
146 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
149 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
150 val[idx] = 1 + std::rand();
154 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
155 cpf[norms + idx] = val[idx] / sum;
169 template <
typename GUM_SCALAR >
175 float density = __layers[lvl].inner_density * RAND_MAX;
176 std::vector< NodeId > nodes;
180 for (
const auto agg : l[lvl].g) {
187 for (
const auto attr : l[lvl].a) {
191 for (
const auto node : nodes)
192 if (std::rand() < density) dag.
addArc(node, names.
second(attr));
200 for (
const auto node : dag.
nodes()) {
202 std::vector< NodeId > v;
204 for (
const auto par : dag.
parents(node))
207 while (dag.
parents(node).
size() > getMaxParents()) {
208 size_t idx = std::rand() % v.size();
209 Arc arc(v[idx], node);
219 template <
typename GUM_SCALAR >
224 std::vector< std::vector< std::string > > o(__layers.size());
228 for (
size_t lvl = 0; lvl < __layers.size(); ++lvl) {
229 float density = __layers[lvl].outter_density * RAND_MAX;
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);
237 std::stringstream chain;
238 chain << name <<
"." << l[lvl].r;
239 std::vector< std::string > ref2add;
241 for (std::vector< std::string >::iterator iter = o[lvl - 1].begin();
242 iter != o[lvl - 1].end();
244 if (std::rand() <= density) ref2add.push_back(*iter);
248 chain.str(), o[lvl - 1][std::rand() % o[lvl - 1].size()]);
250 while (ref2add.size() > getMaxParents()) {
251 idx = std::rand() % ref2add.size();
252 ref2add[idx] = ref2add.back();
256 for (std::vector< std::string >::iterator iter = ref2add.begin();
257 iter != ref2add.end();
267 template <
typename GUM_SCALAR >
269 __layers(), __domain_size(2), __max_parents(INT_MAX) {
273 template <
typename GUM_SCALAR >
281 template <
typename GUM_SCALAR >
286 template <
typename GUM_SCALAR >
295 template <
typename GUM_SCALAR >
300 template <
typename GUM_SCALAR >
305 template <
typename GUM_SCALAR >
310 template <
typename GUM_SCALAR >
315 template <
typename GUM_SCALAR >
321 template <
typename GUM_SCALAR >
322 INLINE std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
327 template <
typename GUM_SCALAR >
328 INLINE
const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
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 > ¶ms, 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
Headers of LayerGenerator.
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
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>.
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
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>.
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.
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 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.
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)
void __generateClassDag(Size lvl, DAG &dag, Bijection< std::string, NodeId > &names, std::vector< typename LayerGenerator::MyData > &l)
std::string __generateType(PRMFactory< GUM_SCALAR > &f)