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 Headers of the Observation class.
25  *
26  * @author Pierre-Henri WUILLEMIN(_at_LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(_at_AMU)
28  */
29 
30 // #define  TRACE_ALGO
31 // =========================================================================
32 #ifndef GUM_OBSERVATION_H
33 #define GUM_OBSERVATION_H
34 // =========================================================================
35 #include <agrum/tools/core/hashTable.h>
36 #include <agrum/tools/core/smallobjectallocator/smallObjectAllocator.h>
37 // =========================================================================
38 #include <agrum/tools/variables/discreteVariable.h>
39 // =========================================================================
40 
41 namespace gum {
42 
43   /**
44    * @class Observation
45    * @headerfile observation.h <agrum/FMDP/learning/observation.h>
46    * @brief
47    * @ingroup fmdp_group
48    *
49    *
50    *
51    */
52 
53   class Observation {
54     public:
55     // ==========================================================================
56     /// @name Constructor & destructor.
57     // ==========================================================================
58     /// @{
59 
60     // ###################################################################
61     /// Default constructor
62     // ###################################################################
Observation()63     Observation() {
64       GUM_CONSTRUCTOR(Observation);
65       ;
66     }
67 
68     // ###################################################################
69     /// Default destructor
70     // ###################################################################
~Observation()71     ~Observation() {
72       GUM_DESTRUCTOR(Observation);
73       ;
74     }
75 
76     // ============================================================================
77     /// Allocators and Deallocators redefinition
78     // ============================================================================
new(size_t s)79     void* operator new(size_t s) { return SmallObjectAllocator::instance().allocate(s); }
delete(void * p)80     void  operator delete(void* p) {
81       SmallObjectAllocator::instance().deallocate(p, sizeof(Observation));
82     }
83 
84     /// @}
85 
86     // ==========================================================================
87     /// @name Observation Handlers
88     // ==========================================================================
89     /// @{
90 
91     // ###################################################################
92     /**
93      * Returns the modality assumed by the given variable in this observation
94      *
95      * @throws NotFound if variable is not in this observation
96      */
97     // ###################################################################
modality(const DiscreteVariable * var)98     INLINE Idx modality(const DiscreteVariable* var) const { return _varInst_[var]; }
rModality(const DiscreteVariable * var)99     INLINE Idx rModality(const DiscreteVariable* var) const { return _rInst_[var]; }
100 
101     // ###################################################################
102     /**
103      * Sets the modality assumed by the given variable in this observation
104      *
105      * @throws DuplicateElement if a value has already be assigned to
106      * this variable
107      */
108     // ###################################################################
setModality(const DiscreteVariable * var,Idx modality)109     INLINE void setModality(const DiscreteVariable* var, Idx modality) {
110       _varInst_.insert(var, modality);
111     }
setRModality(const DiscreteVariable * var,Idx modality)112     INLINE void setRModality(const DiscreteVariable* var, Idx modality) {
113       _rInst_.insert(var, modality);
114     }
115 
116     // ###################################################################
117     // Returns the reward obtained during this observation
118     // ###################################################################
reward()119     double reward() const { return _reward_; }
120 
121     // ###################################################################
122     // Sets the reward obtained during this observation
123     // ###################################################################
setReward(double reward)124     void setReward(double reward) { _reward_ = reward; }
125 
126     /// @}
127     ///
128     std::string toString() const;
129 
130     // ==========================================================================
131     /// @name Iterators on Variables
132     // ==========================================================================
133     /// @{
134 
135     // ###################################################################
136     /// Returns an const safe iterator on the beginning of the list of
137     /// variables in this observation
138     // ###################################################################
cbeginVariablesSafe()139     HashTableConstIteratorSafe< const DiscreteVariable*, Idx > cbeginVariablesSafe() const {
140       return _varInst_.cbeginSafe();
141     }
142 
143     // ###################################################################
144     /// Returns an const safe iterator on the end of the list of
145     /// variables in this observation
146     // ###################################################################
cendVariablesSafe()147     HashTableConstIteratorSafe< const DiscreteVariable*, Idx > cendVariablesSafe() const {
148       return _varInst_.cendSafe();
149     }
150 
151     /// @}
152 
153     private:
154     /// Table giving for every variables its instantiation
155     HashTable< const DiscreteVariable*, Idx > _varInst_;
156     HashTable< const DiscreteVariable*, Idx > _rInst_;
157 
158     /// The reward associated to this transition
159     double _reward_;
160   };
161 
162 } /* namespace gum */
163 
164 
165 #endif   // GUM_OBSERVATION_H
166