1 /* This file is part of libDAI - http://www.libdai.org/
2 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
9 #include <dai/varset.h>
10 #include <dai/index.h>
11 #include <iostream>
13 using namespace std;
14 using namespace dai;
16 int main() {
17 Var x0(0, 2); // Define binary variable x0 (with label 0)
18 Var x1(1, 3); // Define ternary variable x1 (with label 1)
20 // Define set X = {x0, x1}
21 VarSet X; // empty
22 X |= x1; // X = {x1}
23 X |= x0; // X = {x1, x0}
24 cout << "X = " << X << endl << endl; // Note that the elements of X are ordered according to their labels
26 // Output some information about x0, x1 and X
27 cout << "Var " << x0 << " has " << x0.states() << " states (possible values)." << endl;
28 cout << "Var " << x1 << " has " << x1.states() << " states." << endl << endl;
29 cout << "VarSet " << X << " has " << X.nrStates() << " states (joint assignments of its variables)." << endl << endl;
31 cout << "States of VarSets correspond to states of their constituent Vars:" << endl;
32 cout << " state of x0: state of x1: (linear) state of X:" << endl;
33 for( size_t s1 = 0; s1 < x1.states(); s1++ ) // for all states s1 of x1
34 for( size_t s0 = 0; s0 < x0.states(); s0++ ) { // for all states s0 of x0
35 // store s0 and s1 in a map "states"
36 map<Var,size_t> states;
37 states[x0] = s0;
38 states[x1] = s1;
40 // output states of x0, x1 and corresponding state of X
41 cout << " " << s0 << " " << s1 << " " << calcLinearState(X,states) << endl;
43 // calcState() is the inverse of calcLinearState()
44 DAI_ASSERT( calcState(X, calcLinearState(X, states)) == states );
45 }
47 cout << endl << "And vice versa:" << endl;
48 cout << " state of x0: state of x1: (linear) state of X:" << endl;
49 for( size_t S = 0; S < X.nrStates(); S++ ) { // for all (joint) states of X
50 // calculate states of x0 and x1 corresponding to state S of X
51 map<Var,size_t> states = calcState(X,S);
53 // output state of X and corresponding states of x0, x1
54 cout << " " << states[x0] << " " << states[x1] << " " << S << endl;
56 // calcLinearState() is the inverse of calcState()
57 DAI_ASSERT( calcLinearState(X, calcState(X,S)) == S );
58 }
60 cout << endl << "Iterating over all joint states using the State class:" << endl;
61 cout << " state of x0: state of x1: (linear) state of X: state of X (as a map):" << endl;
62 for( State S(X); S.valid(); S++ ) {
63 // output state of X and corresponding states of x0, x1
64 cout << " " << S(x0) << " " << S(x1) << " " << S << " " << S.get() << endl;
65 }
67 return 0;
68 }