Merge branch 'joris'
[libdai.git] / include / dai / varset.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
6 Radboud University Nijmegen, The Netherlands
7
8 This file is part of libDAI.
9
10 libDAI is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 libDAI is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with libDAI; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25
26 /// \file
27 /// \brief Defines VarSet class
28
29
30 #ifndef __defined_libdai_varset_h
31 #define __defined_libdai_varset_h
32
33
34 #include <vector>
35 #include <map>
36 #include <cassert>
37 #include <ostream>
38 #include <dai/var.h>
39 #include <dai/util.h>
40 #include <dai/smallset.h>
41
42
43 namespace dai {
44
45
46 /// Represents a set of variables.
47 /** \note A VarSet is implemented using a SmallSet<Var> instead
48 * of the more natural std::set<Var> because of efficiency reasons.
49 * That is, internally, the variables in the set are sorted according
50 * to their labels: the set of variables \f$\{x_l\}_{l\in L}\f$ is
51 * represented as a vector \f$(x_{l(0)},x_{l(1)},\dots,x_{l(|L|-1)})\f$
52 * where \f$l(0) < l(1) < \dots < l(|L|-1)\f$
53 * and \f$L = \{l(0),l(1),\dots,l(|L|-1)\}\f$.
54 */
55 class VarSet : public SmallSet<Var> {
56 public:
57 /// Default constructor
58 VarSet() : SmallSet<Var>() {}
59
60 /// Copy constructor
61 VarSet( const VarSet &x ) : SmallSet<Var>(x) {}
62
63 /// Assignment operator
64 VarSet& operator=( const VarSet &x ) {
65 if( this != &x ) {
66 SmallSet<Var>::operator=( x );
67 }
68 return *this;
69 }
70
71 /// Construct from SmallSet<Var>
72 VarSet( const SmallSet<Var> &x ) : SmallSet<Var>(x) {}
73
74 /// Calculates the number of states of this VarSet.
75 /** The number of states of the Cartesian product of the variables in this VarSet
76 * is simply the product of the number of states of each variable in this VarSet.
77 * If *this corresponds with the set \f$\{x_l\}_{l\in L}\f$,
78 * where variable \f$x_l\f$ has label \f$l\f$, and denoting by \f$S_l\f$ the
79 * number of possible values ("states") of variable \f$x_l\f$, the number of
80 * joint configurations of the variables in \f$\{x_l\}_{l\in L}\f$ is given by \f$\prod_{l\in L} S_l\f$.
81 */
82 size_t nrStates() {
83 size_t states = 1;
84 for( VarSet::const_iterator n = begin(); n != end(); n++ )
85 states *= n->states();
86 return states;
87 }
88
89 /// Construct a VarSet with one element
90 VarSet( const Var &n ) : SmallSet<Var>(n) {}
91
92 /// Construct a VarSet with two elements
93 VarSet( const Var &n1, const Var &n2 ) : SmallSet<Var>(n1,n2) {}
94
95 /// Construct a VarSet from a range.
96 /** \tparam VarIterator Iterates over instances of type Var.
97 * \param begin Points to first Var to be added.
98 * \param end Points just beyond last Var to be added.
99 * \param sizeHint For efficiency, the number of elements can be speficied by sizeHint.
100 */
101 template <typename VarIterator>
102 VarSet( VarIterator begin, VarIterator end, size_t sizeHint=0 ) : SmallSet<Var>(begin,end,sizeHint) {}
103
104 /// Calculates the linear index in the Cartesian product of the variables in *this, which corresponds to a particular joint assignment of the variables specified by \a states.
105 /** \param states Specifies the states of some variables.
106 * \return The linear index in the Cartesian product of the variables in *this
107 * corresponding with the joint assignment specified by \a states, where it is
108 * assumed that \a states[\a m]==0 for all \a m in *this which are not in \a states.
109 *
110 * The linear index is calculated as follows. The variables in *this are
111 * ordered according to their label (in ascending order); say *this corresponds with
112 * 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$,
113 * where variable \f$x_l\f$ has label \a l. Denote by \f$S_l\f$ the number of possible values
114 * ("states") of variable \f$x_l\f$. The argument \a states corresponds
115 * 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$,
116 * where \f$s(x_l)=0\f$ if \f$x_l\f$ is not specified in \a states. The linear index \a S corresponding
117 * with \a states is now calculated as:
118 * \f{eqnarray*}
119 * S &:=& \sum_{i=0}^{n-1} s(x_{l(i)}) \prod_{j=0}^{i-1} S_{l(j)} \\
120 * &= & 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)}.
121 * \f}
122 *
123 * \note If *this corresponds with \f$\{x_l\}_{l\in L}\f$, and \a states specifies a state
124 * for each variable \f$x_l\f$ for \f$l\in L\f$, calcState(const std::map<Var,size_t> &) induces a mapping
125 * \f$\sigma : \prod_{l\in L} X_l \to \{0,1,\dots,\prod_{l\in L} S_l-1\}\f$ that
126 * maps a joint state to a linear index; this is the inverse of the mapping
127 * \f$\sigma^{-1}\f$ induced by calcStates(size_t).
128 */
129 size_t calcState( const std::map<Var, size_t> &states ) {
130 size_t prod = 1;
131 size_t state = 0;
132 for( VarSet::const_iterator n = begin(); n != end(); n++ ) {
133 std::map<Var, size_t>::const_iterator m = states.find( *n );
134 if( m != states.end() )
135 state += prod * m->second;
136 prod *= n->states();
137 }
138 return state;
139 }
140
141 /// Calculates the joint assignment of the variables in *this corresponding to the linear index \a linearState.
142 /** \param linearState should be smaller than nrStates().
143 * \return A mapping \f$s\f$ that maps each Var \f$x_l\f$ in *this to its state \f$s(x_l)\f$, as specified by \a linearState.
144 *
145 * The variables in *this are ordered according to their label (in ascending order); say *this corresponds with
146 * 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$,
147 * where variable \f$x_l\f$ has label \a l. Denote by \f$S_l\f$ the number of possible values
148 * ("states") of variable \f$x_l\f$ with label \a l.
149 * The mapping \a s returned by this function is defined as:
150 * \f{eqnarray*}
151 * 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$}.
152 * \f}
153 * where \f$S\f$ denotes the value of \a linearState.
154 *
155 * \note If *this corresponds with \f$\{x_l\}_{l\in L}\f$, calcStates(size_t) induces a mapping
156 * \f$\sigma^{-1} : \{0,1,\dots,\prod_{l\in L} S_l-1\} \to \prod_{l\in L} X_l\f$ that
157 * maps a linear index to a joint state; this is the inverse of the mapping \f$\sigma\f$
158 * induced by calcState(const std::map<Var,size_t> &).
159 */
160 std::map<Var, size_t> calcStates( size_t linearState ) {
161 std::map<Var, size_t> states;
162 for( VarSet::const_iterator n = begin(); n != end(); n++ ) {
163 states[*n] = linearState % n->states();
164 linearState /= n->states();
165 }
166 assert( linearState == 0 );
167 return states;
168 }
169
170 /// Writes a VarSet to an output stream
171 friend std::ostream& operator<< (std::ostream &os, const VarSet& ns) {
172 os << "{";
173 for( VarSet::const_iterator n = ns.begin(); n != ns.end(); n++ )
174 os << (n != ns.begin() ? "," : "") << *n;
175 os << "}";
176 return( os );
177 }
178 };
179
180
181 } // end of namespace dai
182
183
184 /** \example example_varset.cpp
185 * This example shows how to use the Var and VarSet classes. It also explains the concept of "states" for VarSets.
186 *
187 * \section Output
188 * \verbinclude examples/example_varset.out
189 *
190 * \section Source
191 */
192
193
194 #endif