Changed testregression such that it doesn't fail on platform-dependent numerical...
[libdai.git] / examples / example_permute.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <dai/varset.h>
10 #include <dai/index.h>
11 #include <iostream>
12 #include <iomanip>
13
14 using namespace std;
15 using namespace dai;
16
17 int main() {
18 Var x0(0, 2); // Define binary variable x0 (with label 0)
19 Var x1(1, 3); // Define ternary variable x1 (with label 1)
20 Var x2(2, 2); // Define binary variable x2 (with label 2)
21
22 // Define vector V = (x1, x2, x0)
23 vector<Var> V; // Define a vector of variables
24 V.push_back( x1 ); // V[0] = x1;
25 V.push_back( x2 ); // V[1] = x2;
26 V.push_back( x0 ); // V[2] = x0;
27 cout << "V = " << V << endl; // Note that the elements of X are not necessarily ordered according to their labels
28
29 // Define set X = {x0, x1, x2}
30 VarSet X; // empty
31 X |= x2; // X = {x2}
32 X |= x0; // X = {x0, x2}
33 X |= x1; // X = {x0, x1, x2}
34 cout << "X = " << X << endl; // Note that the elements of X are ordered according to their labels
35
36 cout << "Note that the ordering of the variables in X is the canonical ordering" << endl;
37 cout << "(ascendingly according to their labels) but the ordering in V is different." << endl << endl;
38
39 // N = number of variables in V (and X)
40 size_t N = V.size();
41
42 // Define a Permute object based on the variables in V
43 Permute sigma(V);
44 // Each Var in V corresponds with a dimension in a multi-dimensional array.
45 // The permutation sigma permutes these dimensions from the canonical ordering
46 // (sorted ascendingly on the label of the variable, i.e., the same ordering as
47 // in X) into the ordering these variables have in V.
48 cout << "The permutation between both variable orderings is sigma = " << sigma.sigma() << ", or more verbosely:" << endl;
49 for( size_t n = 0; n < N; n++ )
50 cout << " sigma[" << n << "] = " << sigma[n] << endl;
51 cout << "This means that variable V[sigma[n]] should correspond with the n'th variable in X (for n=0,...," << (N-1) << ")...";
52 // Check whether the permutation works as advertised
53 VarSet::const_iterator X_n = X.begin();
54 for( size_t n = 0; n < N; n++, X_n++ )
55 DAI_ASSERT( V[sigma[n]] == *X_n );
56 cout << "OK. " << endl << endl;
57
58 // Iterate over the joint states of the variables, according to the ordering in V
59 cout << "The states of the variables x0,x1,x2 are, according to the ordering in V:" << endl;
60 cout << "SV: x0: x1: x2:" << endl;
61 std::vector<size_t> ranges;
62 for( size_t i = 0; i < V.size(); i++ )
63 ranges.push_back( V[i].states() );
64 for( multifor SV(ranges); SV.valid(); ++SV )
65 cout << setw(2) << (size_t)SV << " " << SV[sigma[0]] << " " << SV[sigma[1]] << " " << SV[sigma[2]] << endl;
66 cout << endl;
67
68 // Iterate over the joint states of the variables, according to the canonical ordering in X
69 cout << "The states of the variables x0,x1,x2 are, according to the canonical ordering in X:" << endl;
70 cout << "SX: x0: x1: x2:" << endl;
71 for( State SX(X); SX.valid(); SX++ )
72 cout << setw(2) << SX << " " << SX(x0) << " " << SX(x1) << " " << SX(x2) << endl;
73 cout << endl;
74
75 // The main functionality of the Permute object is to calculate the induced permutation of linear indices of joint states
76 cout << "The permutation sigma induces the following permutation of linear indices of joint states:" << endl;
77 cout << "SV: SX:" << endl;
78 for( size_t li = 0; li < X.nrStates(); li++ )
79 cout << setw(2) << li << " " << setw(2) << sigma.convertLinearIndex( li ) << endl;
80
81 return 0;
82 }