34 template <
typename GUM_SCALAR >
36 if (__layers.size() == 0) {
38 "cannot generate a layered PRM<GUM_SCALAR> without layers");
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);
50 template <
typename GUM_SCALAR >
56 for (
Size i = 0; i < __domain_size; ++i) {
57 std::stringstream sBuff;
66 template <
typename GUM_SCALAR >
69 const std::string& type,
71 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
76 for (
Size a = 0; a < __layers[lvl].a; ++a) {
83 for (
Size g = 0; g < __layers[lvl].g; ++g) {
97 template <
typename GUM_SCALAR >
100 const std::string& type,
103 GUM_SCALAR sum = 0.0;
106 for (
Size lvl = 0; lvl < __layers.size(); ++lvl) {
109 for (
Size c = 0; c < __layers[lvl].c; ++c) {
117 __generateClassDag(lvl, dag, names, l);
121 for (std::vector< std::string >::iterator g = l[lvl].g.begin();
126 << l[lvl - 1].a[std::rand() % l[lvl - 1].a.size()];
127 std::vector< std::string > chain(1, s.str()), param(1,
"1");
133 for (std::vector< std::string >::iterator a = l[lvl].a.begin();
137 size = getDomainSize();
142 .
get(names.
first(par))
147 std::vector< GUM_SCALAR > cpf(size), val(getDomainSize());
149 for (
size_t norms = 0; norms < size; norms += getDomainSize()) {
152 for (
size_t idx = 0; idx < getDomainSize(); ++idx) {
153 val[idx] = 1 + std::rand();
157 for (
size_t idx = 0; idx < getDomainSize(); ++idx)
158 cpf[norms + idx] = val[idx] / sum;
172 template <
typename GUM_SCALAR >
178 float density = __layers[lvl].inner_density * RAND_MAX;
179 std::vector< NodeId > nodes;
183 for (
const auto agg : l[lvl].g) {
190 for (
const auto attr : l[lvl].a) {
194 for (
const auto node : nodes)
195 if (std::rand() < density) dag.
addArc(node, names.
second(attr));
203 for (
const auto node : dag.
nodes()) {
205 std::vector< NodeId > v;
207 for (
const auto par : dag.
parents(node))
210 while (dag.
parents(node).
size() > getMaxParents()) {
211 size_t idx = std::rand() % v.size();
212 Arc arc(v[idx], node);
222 template <
typename GUM_SCALAR >
227 std::vector< std::vector< std::string > > o(__layers.size());
231 for (
size_t lvl = 0; lvl < __layers.size(); ++lvl) {
232 float density = __layers[lvl].outter_density * RAND_MAX;
234 for (
size_t count = 0; count < __layers[lvl].o; ++count) {
236 factory.
addInstance(l[lvl].c[std::rand() % l[lvl].c.size()], name);
237 o[lvl].push_back(name);
240 std::stringstream chain;
241 chain << name <<
"." << l[lvl].r;
242 std::vector< std::string > ref2add;
244 for (std::vector< std::string >::iterator iter = o[lvl - 1].begin();
245 iter != o[lvl - 1].end();
247 if (std::rand() <= density) ref2add.push_back(*iter);
251 chain.str(), o[lvl - 1][std::rand() % o[lvl - 1].size()]);
253 while (ref2add.size() > getMaxParents()) {
254 idx = std::rand() % ref2add.size();
255 ref2add[idx] = ref2add.back();
259 for (std::vector< std::string >::iterator iter = ref2add.begin();
260 iter != ref2add.end();
270 template <
typename GUM_SCALAR >
272 __layers(), __domain_size(2), __max_parents(INT_MAX) {
276 template <
typename GUM_SCALAR >
284 template <
typename GUM_SCALAR >
289 template <
typename GUM_SCALAR >
298 template <
typename GUM_SCALAR >
303 template <
typename GUM_SCALAR >
308 template <
typename GUM_SCALAR >
313 template <
typename GUM_SCALAR >
318 template <
typename GUM_SCALAR >
324 template <
typename GUM_SCALAR >
325 INLINE std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
330 template <
typename GUM_SCALAR >
331 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
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.
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)