1 /** 2 * 3 * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_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 Class used to store optimum IBayesNet during some inference 25 * algorithms. 26 * @author Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(_at_LIP6) 27 */ 28 29 #ifndef __VAR_MOD_2_BNS_MAP__H__ 30 #define __VAR_MOD_2_BNS_MAP__H__ 31 32 #include <list> 33 34 //#include <tr1/functional> 35 36 #include <agrum/tools/core/hashTable.h> 37 #include <agrum/tools/core/math/pow.h> 38 39 #include <agrum/CN/credalNet.h> 40 41 namespace gum { 42 namespace credal { 43 44 /** 45 * @class VarMod2BNsMap 46 * @headerfile varMod2BNsMap.h <agrum/CN/varMod2BNsMap.h> 47 * @brief Class used to store optimum IBayesNet during some inference 48 * algorithms. 49 * @ingroup cn_group 50 * @tparam GUM_SCALAR A floating type ( float, double, long double ... ). 51 * @author Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(_at_LIP6) 52 */ 53 54 template < typename GUM_SCALAR > 55 class VarMod2BNsMap { 56 private: 57 typedef std::vector< bool > dBN; 58 59 typedef typename std::vector< Size > varKey; 60 61 typedef typename gum::HashTable< Size, dBN > hashNet; 62 63 typedef typename gum::HashTable< varKey, std::list< Size > > varHashs; 64 65 typedef typename gum::HashTable< Size, std::list< varKey > > hashVars; 66 67 protected: 68 /** Map id - dBN : get a net from it's id. */ 69 hashNet myHashNet_; 70 /** Map varKey - list(id) : get all optimal nets id from the given key : 71 * variable, modality and min=0 ( or max=1 ). */ 72 varHashs myVarHashs_; 73 /** Map id - list(varKey) : get all variables, modalities associated to 74 * this 75 * optimal net id. */ 76 hashVars myHashVars_; 77 78 /** Since all samples have the same structure, this will be used as 79 * default 80 * initialization (copy constructor) for any sample. */ 81 std::vector< std::vector< std::vector< bool > > > sampleDef_; 82 83 /** The current sampled IBayesNet. */ 84 dBN currentSample_; 85 /** The current sampled IBayesNet hash. */ 86 Size currentHash_; 87 /** The hash fuction. Requires c++11/0x. */ 88 std::hash< std::vector< bool > > vectHash_; 89 90 /** A pointer to the CredalNet to be used. */ 91 const CredalNet< GUM_SCALAR >* cnet; 92 93 public: 94 /// @name Constructors / Destructors 95 /// @{ 96 97 /** 98 * Thread constructor. 99 * Initialize _sampleDef_ from the CredalNet. 100 * 101 * @param cn The CredalNet to be used. 102 */ 103 explicit VarMod2BNsMap(const CredalNet< GUM_SCALAR >& cn); 104 105 /** Default constructor that should be used only by InferenceEngine since 106 * it 107 * has a member variable. */ 108 VarMod2BNsMap(); 109 110 /** Destructor. */ 111 ~VarMod2BNsMap(); 112 113 /// @} 114 115 /** 116 * Insert for thread fusion. All inserted nets are optimums and none will 117 * be 118 * deleted because a better one is being inserted. 119 * @param bn The constant reference to the net to be inserted. 120 * @param key The constant reference to the key at which we will insert 121 * the 122 * net. 123 */ 124 bool insert(const std::vector< bool >& bn, const std::vector< Size >& key); 125 126 /** 127 * Thread insert. 128 * @param key The key at which the net will be inserted. 129 * @param isBetter \c True if the net is a better one, i.e. it gave better 130 * marginals. 131 * @return \c True if the net was inserted, \c false otherwise. 132 */ 133 bool insert(const std::vector< Size >& key, const bool isBetter); 134 135 /// @name Getters and setters 136 /// @{ 137 138 /** 139 * Initialize _sampleDef_ from the CredalNet. 140 * 141 * Shoud only be used by InferenceEngine to initialize it's member 142 *variable. 143 * @param cn The CredalNet to be used. 144 */ 145 void setCNet(const CredalNet< GUM_SCALAR >& cn); 146 147 /** 148 * %Set the current thread sample and it's hash. 149 * @param sample The constant reference to the sample with structure. 150 */ 151 void setCurrentSample(const std::vector< std::vector< std::vector< bool > > >& sample); 152 153 /** 154 * Get the current sample as a vector of bits without structure. 155 * @return The constant reference to the sampled IBayesNet without 156 * structure. 157 */ 158 const dBN& getCurrentSample(); 159 160 /** 161 * Get the sample structure. 162 * @return The constant reference to the sample structure. 163 */ 164 const std::vector< std::vector< std::vector< bool > > >& getSampleDef(); 165 166 /** 167 * Get optimum IBayesNet (s) without structure of the given variable, 168 * modality 169 * for min or max. 170 * @param key The constant reference to the variable, modality, min or 171 * max. 172 * @return The constant vector of not yet constant pointers to the nets. 173 */ 174 const std::vector< dBN* > getBNOptsFromKey(const std::vector< Size >& key); 175 176 /** 177 * Get optimum IBayesNet (s) with structure of the given variable, modality 178 * for 179 * min or max. 180 * @param key The constant reference to the variable, modality, min or max. 181 * @return The vector of not yet constant pointers to the nets. 182 */ 183 std::vector< std::vector< std::vector< std::vector< bool > > > > 184 getFullBNOptsFromKey(const std::vector< Size >& key); 185 186 /** Get the number of IBayesNet stored. */ 187 Size getEntrySize() const; 188 189 /// @} 190 191 }; // end of OptBN 192 193 194 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 195 extern template class VarMod2BNsMap< double >; 196 #endif 197 198 } // namespace credal 199 } // namespace gum 200 201 #include <agrum/CN/tools/varMod2BNsMap_tpl.h> 202 203 #endif 204