Removed obsolete/deprecated stuff
[libdai.git] / include / dai / varset.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
8 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 * Copyright (C) 2002-2007 Radboud University Nijmegen, The Netherlands
10 */
11
12
13 /// \file
14 /// \brief Defines the VarSet class, which represents a set of random variables.
15
16
17 #ifndef __defined_libdai_varset_h
18 #define __defined_libdai_varset_h
19
20
21 #include <vector>
22 #include <map>
23 #include <ostream>
24 #include <dai/var.h>
25 #include <dai/util.h>
26 #include <dai/smallset.h>
27
28
29 namespace dai {
30
31
32 // Predefine for definitions of calcLinearState() and calcState()
33 class VarSet;
34
35
36 /// Calculates the linear index in the Cartesian product of the variables in \a vs that corresponds to a particular joint assignment of the variables, specified by \a state.
37 /** \param vs Set of variables for which the linear state should be calculated;
38 * \param state Specifies the states of some variables.
39 * \return The linear index in the Cartesian product of the variables in \a vs
40 * corresponding with the joint assignment specified by \a state, where variables
41 * for which no state is specified are assumed to be in state 0.
42 *
43 * The linear index is calculated as follows. The variables in \a vs are
44 * ordered according to their label (in ascending order); say \a vs corresponds with
45 * the set \f$\{x_{l(0)},x_{l(1)},\dots,x_{l(n-1)}\}\f$ with \f$l(0) < l(1) < \dots < l(n-1)\f$,
46 * where variable \f$x_l\f$ has label \a l. Denote by \f$S_l\f$ the number of possible values
47 * ("states") of variable \f$x_l\f$. The argument \a state corresponds
48 * with a mapping \a s that assigns to each variable \f$x_l\f$ a state \f$s(x_l) \in \{0,1,\dots,S_l-1\}\f$,
49 * where \f$s(x_l)=0\f$ if \f$x_l\f$ is not specified in \a state. The linear index \f$S\f$ corresponding
50 * with \a state is now calculated by:
51 * \f{eqnarray*}
52 * S &:=& \sum_{i=0}^{n-1} s(x_{l(i)}) \prod_{j=0}^{i-1} S_{l(j)} \\
53 * &= & s(x_{l(0)}) + s(x_{l(1)}) S_{l(0)} + s(x_{l(2)}) S_{l(0)} S_{l(1)} + \dots + s(x_{l(n-1)}) S_{l(0)} \cdots S_{l(n-2)}.
54 * \f}
55 *
56 * \note If \a vs corresponds with \f$\{x_l\}_{l\in L}\f$, and \a state specifies a state
57 * for each variable \f$x_l\f$ for \f$l\in L\f$, calcLinearState() induces a mapping
58 * \f$\sigma : \prod_{l\in L} X_l \to \{0,1,\dots,\prod_{l\in L} S_l-1\}\f$ that
59 * maps a joint state to a linear index; this is the inverse of the mapping
60 * \f$\sigma^{-1}\f$ induced by calcState().
61 *
62 * \see calcState()
63 */
64 size_t calcLinearState( const VarSet &vs, const std::map<Var, size_t> &state );
65
66
67 /// Calculates the joint assignment of the variables in \a vs corresponding to the linear index \a linearState.
68 /** \param vs Set of variables to which \a linearState refers
69 * \param linearState should be smaller than vs.nrStates().
70 * \return A mapping \f$s\f$ that maps each Var \f$x_l\f$ in \a vs to its state \f$s(x_l)\f$, as specified by \a linearState.
71 *
72 * The variables in \a vs are ordered according to their label (in ascending order); say \a vs corresponds with
73 * the set \f$\{x_{l(0)},x_{l(1)},\dots,x_{l(n-1)}\}\f$ with \f$l(0) < l(1) < \dots < l(n-1)\f$,
74 * where variable \f$x_l\f$ has label \a l. Denote by \f$S_l\f$ the number of possible values
75 * ("states") of variable \f$x_l\f$ with label \a l.
76 * The mapping \f$s\f$ returned by this function is defined as:
77 * \f{eqnarray*}
78 * s(x_{l(i)}) = \left\lfloor\frac{S \mbox { mod } \prod_{j=0}^{i} S_{l(j)}}{\prod_{j=0}^{i-1} S_{l(j)}}\right\rfloor \qquad \mbox{for all $i=0,\dots,n-1$}.
79 * \f}
80 * where \f$S\f$ denotes the value of \a linearState.
81 *
82 * \note If \a vs corresponds with \f$\{x_l\}_{l\in L}\f$, calcState() induces a mapping
83 * \f$\sigma^{-1} : \{0,1,\dots,\prod_{l\in L} S_l-1\} \to \prod_{l\in L} X_l\f$ that
84 * maps a linear index to a joint state; this is the inverse of the mapping \f$\sigma\f$
85 * induced by calcLinearState().
86 *
87 * \see calcLinearState()
88 */
89 std::map<Var, size_t> calcState( const VarSet &vs, size_t linearState );
90
91
92 /// Represents a set of variables.
93 /** \note A VarSet is implemented using a SmallSet<Var> instead
94 * of the more natural std::set<Var> because of efficiency reasons.
95 * That is, internally, the variables in the set are sorted ascendingly
96 * according to their labels.
97 */
98 class VarSet : public SmallSet<Var> {
99 public:
100 /// \name Constructors and destructors
101 //@{
102 /// Default constructor (constructs an empty set)
103 VarSet() : SmallSet<Var>() {}
104
105 /// Construct from \link SmallSet \endlink<\link Var \endlink> \a x
106 VarSet( const SmallSet<Var> &x ) : SmallSet<Var>(x) {}
107
108 /// Construct a VarSet with one element, \a v
109 VarSet( const Var &v ) : SmallSet<Var>(v) {}
110
111 /// Construct a VarSet with two elements, \a v1 and \a v2
112 VarSet( const Var &v1, const Var &v2 ) : SmallSet<Var>(v1,v2) {}
113
114 /// Construct a VarSet from the range between \a begin and \a end.
115 /** \tparam VarIterator Iterates over instances of type Var.
116 * \param begin Points to first Var to be added.
117 * \param end Points just beyond last Var to be added.
118 * \param sizeHint For efficiency, the number of elements can be speficied by \a sizeHint.
119 */
120 template <typename VarIterator>
121 VarSet( VarIterator begin, VarIterator end, size_t sizeHint=0 ) : SmallSet<Var>(begin,end,sizeHint) {}
122 //@}
123
124 /// \name Queries
125 //@{
126 /// Calculates the number of states of this VarSet, which is simply the number of possible joint states of the variables in \c *this.
127 /** The number of states of the Cartesian product of the variables in this VarSet
128 * is simply the product of the number of states of each variable in this VarSet.
129 * If \c *this corresponds with the set \f$\{x_l\}_{l\in L}\f$,
130 * where variable \f$x_l\f$ has label \f$l\f$, and denoting by \f$S_l\f$ the
131 * number of possible values ("states") of variable \f$x_l\f$, the number of
132 * joint configurations of the variables in \f$\{x_l\}_{l\in L}\f$ is given by \f$\prod_{l\in L} S_l\f$.
133 */
134 size_t nrStates() const {
135 size_t states = 1;
136 for( VarSet::const_iterator n = begin(); n != end(); n++ )
137 states *= n->states();
138 return states;
139 }
140 //@}
141
142 /// \name Input and output
143 //@{
144 /// Writes a VarSet to an output stream
145 friend std::ostream& operator<<( std::ostream &os, const VarSet &vs ) {
146 os << "{";
147 for( VarSet::const_iterator v = vs.begin(); v != vs.end(); v++ )
148 os << (v != vs.begin() ? ", " : "") << *v;
149 os << "}";
150 return( os );
151 }
152 //@}
153 };
154
155
156 } // end of namespace dai
157
158
159 /** \example example_varset.cpp
160 * This example shows how to use the Var, VarSet and State classes. It also explains the concept of "states" for VarSets.
161 *
162 * \section Output
163 * \verbinclude examples/example_varset.out
164 *
165 * \section Source
166 */
167
168
169 #endif