aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
layerGenerator.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of LayerGenerator.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_LAYER_GENERATOR_H
30 #define GUM_LAYER_GENERATOR_H
31 #include <cstdlib>
32 #include <ctime>
33 #include <list>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37 
38 #include <agrum/BN/generator/simpleBayesNetGenerator.h>
39 
40 #include <agrum/PRM/PRMFactory.h>
41 #include <agrum/PRM/generator/PRMGenerator.h>
42 #include <agrum/PRM/generator/nameGenerator.h>
43 
44 namespace gum {
45  namespace prm {
46 
47  /**
48  * @class LayerGenerator layerGenerator.h
49  *<agrum/PRM/generator/layerGenerator.h>
50  * @brief This class generates PRMs with a layer structure.
51  *
52  * @ingroup prm_group
53  */
54  template < typename GUM_SCALAR >
55  class LayerGenerator: public PRMGenerator< GUM_SCALAR > {
56  public:
57  // ========================================================================
58  /// @name Constructors and destructor.
59  // ========================================================================
60  /// @{
61 
62  /// Default constructor.
63  LayerGenerator();
64  /// Copy constructor.
65  LayerGenerator(const LayerGenerator& source);
66  /// Copy operator.
67  LayerGenerator& operator=(const LayerGenerator& source);
68  /// Destructor.
69  virtual ~LayerGenerator();
70 
71  /// @}
72  // ========================================================================
73  /// Getters and setters.
74  // ========================================================================
75  /// @{
76 
77  /// Inner structure used to describe a Layer of the generated
78  /// PRM<GUM_SCALAR>.
79  struct LayerData {
80  // Number of attributes for the layer's interface.
81  Size a;
82  // Number of aggregates for the layer's interface.
83  Size g;
84  // Number of classes for this layer.
85  Size c;
86  // Number of instances for this layer.
87  Size o;
88  // Density of arcs between attributes of the same classe in this layer.
89  float inner_density;
90  // The odds of an instance of the precedent layer to be added to a
91  // reference slot of a class of this layer.
92  float outter_density;
93  };
94 
95  /// Returns the domain size of generated types.
96  Size getDomainSize() const;
97 
98  /// Set the domain size of generated types.
99  void setDomainSize(Size s);
100 
101  /// Returns the max number of parents allowed for any attribute or
102  /// aggregator
103  Size getMaxParents() const;
104 
105  /// Returns the max number of parents allowed for any attribute or
106  /// aggregator
107  void setMaxParents(Size s);
108 
109  /**
110  * @brief Defines the structure of each layers.
111  * Each value in v defines a layer, were v[i].first is the number of
112  * different classes in the i-th layer and v[i].second is the number of
113  * instances in the i-th layer.
114  * @param v A vector describing each layer.
115  */
116  void setLayers(const std::vector< LayerData >& v);
117 
118  std::vector< LayerData >& getLayer();
119  const std::vector< LayerData >& getLayer() const;
120 
121  /// Proceeds with the generation of the PRM<GUM_SCALAR>.
122  virtual PRM< GUM_SCALAR >* generate();
123 
124  /// @}
125  private:
126  std::vector< LayerData > _layers_;
127  Size _domain_size_;
128  Size _max_parents_;
129 
130  struct MyData {
131  // interface name
132  std::string i;
133  std::vector< std::string > a;
134  std::vector< std::string > g;
135  std::string r;
136  std::vector< std::string > c;
137  };
138 
139  std::string _generateType_(PRMFactory< GUM_SCALAR >& f);
140 
141  void _generateInterfaces_(PRMFactory< GUM_SCALAR >& f,
142  const std::string& type,
143  std::vector< MyData >& l);
144 
145  void _generateClasses_(PRMFactory< GUM_SCALAR >& f,
146  const std::string& type,
147  std::vector< typename LayerGenerator::MyData >& l);
148 
149  void _generateClassDag_(Size lvl,
150  DAG& dag,
151  Bijection< std::string, NodeId >& names,
152  std::vector< typename LayerGenerator::MyData >& l);
153 
154  void _generateSystem_(PRMFactory< GUM_SCALAR >& factory,
155  std::vector< typename LayerGenerator::MyData >& l);
156  };
157 
158  } /* namespace prm */
159 } /* namespace gum */
160 
161 #include <agrum/PRM/generator/layerGenerator_tpl.h>
162 
163 #endif /* GUM_LAYER_GENERATOR_H */