aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
clusteredLayerGenerator.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 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(const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >& v);
106 
107  std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >& getLayer();
108  const std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData >& getLayer() const;
109 
110  /// Proceeds with the generation of the PRM<GUM_SCALAR>.
111  virtual PRM< GUM_SCALAR >* generate();
112 
113  /// @}
114  private:
115  std::vector< typename LayerGenerator< GUM_SCALAR >::LayerData > _layers_;
116  Size _domain_size_;
117  Size _max_parents_;
118  double _cluster_ratio_;
119  HashTable< std::string, std::vector< std::string >* > _cluster_map_;
120 
121  struct MyData {
122  // interface name
123  std::string i;
124  std::vector< std::string > a;
125  std::vector< std::string > g;
126  std::string r;
127  std::vector< std::string > c;
128  };
129 
130  std::string _generateType_(PRMFactory< GUM_SCALAR >& f);
131 
132  void _generateInterfaces_(PRMFactory< GUM_SCALAR >& f,
133  const std::string& type,
134  std::vector< MyData >& l);
135 
136  void _generateClasses_(PRMFactory< GUM_SCALAR >& f,
137  const std::string& type,
138  std::vector< typename ClusteredLayerGenerator::MyData >& l);
139 
140  void _generateCluster_(PRMFactory< GUM_SCALAR >& f,
141  const std::string& type,
142  std::vector< typename ClusteredLayerGenerator::MyData >& l,
143  Size lvl,
144  Set< std::string >& i);
145 
146  void _generateClass_(PRMFactory< GUM_SCALAR >& f,
147  const std::string& type,
148  std::vector< typename ClusteredLayerGenerator::MyData >& l,
149  Size lvl,
150  Set< std::string >& i);
151 
152  void _generateClassDag_(Size lvl,
153  DAG& dag,
154  Bijection< std::string, NodeId >& names,
155  std::vector< typename ClusteredLayerGenerator::MyData >& l);
156 
157  void _generateSystem_(PRMFactory< GUM_SCALAR >& factory,
158  std::vector< typename ClusteredLayerGenerator::MyData >& l);
159  };
160 
161  } /* namespace prm */
162 } /* namespace gum */
163 
164 #include <agrum/PRM/generator/clusteredLayerGenerator_tpl.h>
165 
166 #endif /* GUM_CLUSTERED_LAYER_GENERATOR_H */