1 /* This file is part of libDAI - http://www.libdai.org/
2 *
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]
10 */
13 /// \file
14 /// \brief Defines VarSet class
17 #ifndef __defined_libdai_varset_h
18 #define __defined_libdai_varset_h
21 #include <vector>
22 #include <map>
23 #include <ostream>
24 #include <dai/var.h>
25 #include <dai/util.h>
26 #include <dai/smallset.h>
29 namespace dai {
32 /// Represents a set of variables.
33 /** \note A VarSet is implemented using a SmallSet<Var> instead
34 * of the more natural std::set<Var> because of efficiency reasons.
35 * That is, internally, the variables in the set are sorted according
36 * to their labels: the set of variables \f$\{x_l\}_{l\in L}\f$ is
37 * represented as a vector \f$(x_{l(0)},x_{l(1)},\dots,x_{l(|L|-1)})\f$
38 * where \f$l(0) < l(1) < \dots < l(|L|-1)\f$
39 * and \f$L = \{l(0),l(1),\dots,l(|L|-1)\}\f$.
40 */
41 class VarSet : public SmallSet<Var> {
42 public:
43 /// Default constructor
44 VarSet() : SmallSet<Var>() {}
46 /// Construct from SmallSet<Var>
47 VarSet( const SmallSet<Var> &x ) : SmallSet<Var>(x) {}
49 /// Calculates the number of states of this VarSet.
50 /** The number of states of the Cartesian product of the variables in this VarSet
51 * is simply the product of the number of states of each variable in this VarSet.
52 * If *this corresponds with the set \f$\{x_l\}_{l\in L}\f$,
53 * where variable \f$x_l\f$ has label \f$l\f$, and denoting by \f$S_l\f$ the
54 * number of possible values ("states") of variable \f$x_l\f$, the number of
55 * joint configurations of the variables in \f$\{x_l\}_{l\in L}\f$ is given by \f$\prod_{l\in L} S_l\f$.
56 */
57 size_t nrStates() {
58 size_t states = 1;
59 for( VarSet::const_iterator n = begin(); n != end(); n++ )
60 states *= n->states();
61 return states;
62 }
64 /// Construct a VarSet with one element
65 VarSet( const Var &n ) : SmallSet<Var>(n) {}
67 /// Construct a VarSet with two elements
68 VarSet( const Var &n1, const Var &n2 ) : SmallSet<Var>(n1,n2) {}
70 /// Construct a VarSet from a range.
71 /** \tparam VarIterator Iterates over instances of type Var.
72 * \param begin Points to first Var to be added.
73 * \param end Points just beyond last Var to be added.
74 * \param sizeHint For efficiency, the number of elements can be speficied by sizeHint.
75 */
76 template <typename VarIterator>
77 VarSet( VarIterator begin, VarIterator end, size_t sizeHint=0 ) : SmallSet<Var>(begin,end,sizeHint) {}
79 /// 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.
80 /** \param states Specifies the states of some variables.
81 * \return The linear index in the Cartesian product of the variables in *this
82 * corresponding with the joint assignment specified by \a states, where it is
83 * assumed that \a states[\a m]==0 for all \a m in *this which are not in \a states.
84 *
85 * The linear index is calculated as follows. The variables in *this are
86 * ordered according to their label (in ascending order); say *this corresponds with
87 * 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$,
88 * where variable \f$x_l\f$ has label \a l. Denote by \f$S_l\f$ the number of possible values
89 * ("states") of variable \f$x_l\f$. The argument \a states corresponds
90 * 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$,
91 * where \f$s(x_l)=0\f$ if \f$x_l\f$ is not specified in \a states. The linear index \a S corresponding
92 * with \a states is now calculated as:
93 * \f{eqnarray*}
94 * S &:=& \sum_{i=0}^{n-1} s(x_{l(i)}) \prod_{j=0}^{i-1} S_{l(j)} \\
95 * &= & 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)}.
96 * \f}
97 *
98 * \note If *this corresponds with \f$\{x_l\}_{l\in L}\f$, and \a states specifies a state
99 * for each variable \f$x_l\f$ for \f$l\in L\f$, calcState(const std::map<Var,size_t> &) induces a mapping
100 * \f$\sigma : \prod_{l\in L} X_l \to \{0,1,\dots,\prod_{l\in L} S_l-1\}\f$ that
101 * maps a joint state to a linear index; this is the inverse of the mapping
102 * \f$\sigma^{-1}\f$ induced by calcStates(size_t).
103 */
104 size_t calcState( const std::map<Var, size_t> &states ) {
105 size_t prod = 1;
106 size_t state = 0;
107 for( VarSet::const_iterator n = begin(); n != end(); n++ ) {
108 std::map<Var, size_t>::const_iterator m = states.find( *n );
109 if( m != states.end() )
110 state += prod * m->second;
111 prod *= n->states();
112 }
113 return state;
114 }
116 /// Calculates the joint assignment of the variables in *this corresponding to the linear index \a linearState.
117 /** \param linearState should be smaller than nrStates().
118 * \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.
119 *
120 * The variables in *this are ordered according to their label (in ascending order); say *this corresponds with
121 * 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$,
122 * where variable \f$x_l\f$ has label \a l. Denote by \f$S_l\f$ the number of possible values
123 * ("states") of variable \f$x_l\f$ with label \a l.
124 * The mapping \a s returned by this function is defined as:
125 * \f{eqnarray*}
126 * 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$}.
127 * \f}
128 * where \f$S\f$ denotes the value of \a linearState.
129 *
130 * \note If *this corresponds with \f$\{x_l\}_{l\in L}\f$, calcStates(size_t) induces a mapping
131 * \f$\sigma^{-1} : \{0,1,\dots,\prod_{l\in L} S_l-1\} \to \prod_{l\in L} X_l\f$ that
132 * maps a linear index to a joint state; this is the inverse of the mapping \f$\sigma\f$
133 * induced by calcState(const std::map<Var,size_t> &).
134 */
135 std::map<Var, size_t> calcStates( size_t linearState ) {
136 std::map<Var, size_t> states;
137 for( VarSet::const_iterator n = begin(); n != end(); n++ ) {
138 states[*n] = linearState % n->states();
139 linearState /= n->states();
140 }
141 DAI_ASSERT( linearState == 0 );
142 return states;
143 }
145 /// Writes a VarSet to an output stream
146 friend std::ostream& operator<< (std::ostream &os, const VarSet& ns) {
147 os << "{";
148 for( VarSet::const_iterator n = ns.begin(); n != ns.end(); n++ )
149 os << (n != ns.begin() ? "," : "") << *n;
150 os << "}";
151 return( os );
152 }
153 };
156 } // end of namespace dai
159 /** \example example_varset.cpp
160 * This example shows how to use the Var and VarSet 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 */
169 #endif