aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
clusteredLayerGenerator.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 ClusteredLayerGenerator.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_CLUSTERED_LAYER_GENERATOR_H
30 #define GUM_CLUSTERED_LAYER_GENERATOR_H
31 
32 #include <cstdlib>
33 #include <ctime>
34 #include <list>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 
39 #include <agrum/BN/generator/simpleBayesNetGenerator.h>
40 
41 #include <agrum/PRM/PRMFactory.h>
42 #include <agrum/PRM/generator/PRMGenerator.h>
43 #include <agrum/PRM/generator/layerGenerator.h>
44 
45 namespace gum {
46  namespace prm {
47 
48  /**
49  * @class ClusteredLayerGenerator clusteredLayerGenerator.h
50  *<agrum/PRM/generator/clusteredLayerGenerator.h>
51  * @brief This class generates PRMs with a clustered layer structure.
52  *
53  * @ingroup prm_group
54  */
55  template < typename GUM_SCALAR >
56  class ClusteredLayerGenerator: public PRMGenerator< GUM_SCALAR > {
57  public:
58  // ========================================================================
59  /// @name Constructors and destructor.
60  // ========================================================================
61  /// @{
62 
63  /// Default constructor.
64  ClusteredLayerGenerator();
65  /// Copy constructor.
66  ClusteredLayerGenerator(const ClusteredLayerGenerator& source);
67  /// Copy operator.
68  ClusteredLayerGenerator& operator=(const ClusteredLayerGenerator& source);
69  /// Destructor.
70  virtual ~ClusteredLayerGenerator();
71 
72  /// @}
73  // ========================================================================
74  /// Getters and setters.
75  // ========================================================================
76  /// @{
77 
78  /// Returns the domain size of generated types.
79  Size getDomainSize() const;
80 
81  /// Set the domain size of generated types.
82  void setDomainSize(Size s);
83 
84  /// Returns the max number of parents allowed for any attribute or
85  /// aggregator
86  Size getMaxParents() const;
87 
88  /// Returns the max number of parents allowed for any attribute or
89  /// aggregator
90  void setMaxParents(Size s);
91 
92  /// Returns the odds of a given class to be replaced by a cluster.
93  double getClusterRatio() const;
94 
95  /// Define the odds of a given class to be replaced by a cluster.
96  void setClusterRatio(double ratio);
97 
98  /**
99  * @brief Defines the structure of each layers.
100  * Each value in v defines a layer, were v[i].first is the number of
101  * different classes in the i-th layer and v[i].second is the number of
102  * instances in the i-th layer.
103  * @param v A vector describing each layer.
104  */
105  void setLayers(
106  const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >& v);
107 
108  std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >& getLayer();
109  const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >&
110  getLayer() const;
111 
112  /// Proceeds with the generation of the PRM<GUM_SCALAR>.
113  virtual PRM< GUM_SCALAR >* generate();
114 
115  /// @}
116  private:
117  std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData > layers__;
118  Size domain_size__;
119  Size max_parents__;
120  double cluster_ratio__;
121  HashTable< std::string, std::vector< std::string >* > cluster_map__;
122 
123  struct MyData {
124  // interface name
125  std::string i;
126  std::vector< std::string > a;
127  std::vector< std::string > g;
128  std::string r;
129  std::vector< std::string > c;
130  };
131 
132  std::string generateType__(PRMFactory< GUM_SCALAR >& f);
133 
134  void generateInterfaces__(PRMFactory< GUM_SCALAR >& f,
135  const std::string& type,
136  std::vector< MyData >& l);
137 
138  void generateClasses__(
139  PRMFactory< GUM_SCALAR >& f,
140  const std::string& type,
141  std::vector< typename ClusteredLayerGenerator::MyData >& l);
142 
143  void generateCluster__(
144  PRMFactory< GUM_SCALAR >& f,
145  const std::string& type,
146  std::vector< typename ClusteredLayerGenerator::MyData >& l,
147  Size lvl,
148  Set< std::string >& i);
149 
150  void generateClass__(
151  PRMFactory< GUM_SCALAR >& f,
152  const std::string& type,
153  std::vector< typename ClusteredLayerGenerator::MyData >& l,
154  Size lvl,
155  Set< std::string >& i);
156 
157  void generateClassDag__(
158  Size lvl,
159  DAG& dag,
160  Bijection< std::string, NodeId >& names,
161  std::vector< typename ClusteredLayerGenerator::MyData >& l);
162 
163  void generateSystem__(
164  PRMFactory< GUM_SCALAR >& factory,
165  std::vector< typename ClusteredLayerGenerator::MyData >& l);
166  };
167 
168  } /* namespace prm */
169 } /* namespace gum */
170 
171 #include <agrum/PRM/generator/clusteredLayerGenerator_tpl.h>
172 
173 #endif /* GUM_CLUSTERED_LAYER_GENERATOR_H */