Changed license from GPL v2+ to FreeBSD (aka BSD 2-clause) license
[libdai.git] / src / clustergraph.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 <set>
10 #include <vector>
11 #include <iostream>
12 #include <dai/varset.h>
13 #include <dai/clustergraph.h>
14
15
16 namespace dai {
17
18
19 using namespace std;
20
21
22 ClusterGraph::ClusterGraph( const std::vector<VarSet> & cls ) : _G(), _vars(), _clusters() {
23 // construct vars, clusters and edge list
24 vector<Edge> edges;
25 foreach( const VarSet &cl, cls ) {
26 if( find( clusters().begin(), clusters().end(), cl ) == clusters().end() ) {
27 // add cluster
28 size_t n2 = nrClusters();
29 _clusters.push_back( cl );
30 for( VarSet::const_iterator n = cl.begin(); n != cl.end(); n++ ) {
31 size_t n1 = find( vars().begin(), vars().end(), *n ) - vars().begin();
32 if( n1 == nrVars() )
33 // add variable
34 _vars.push_back( *n );
35 edges.push_back( Edge( n1, n2 ) );
36 }
37 } // disregard duplicate clusters
38 }
39
40 // Create bipartite graph
41 _G.construct( nrVars(), nrClusters(), edges.begin(), edges.end() );
42 }
43
44
45 ClusterGraph::ClusterGraph( const FactorGraph& fg, bool onlyMaximal ) : _G( fg.bipGraph() ), _vars(), _clusters() {
46 // copy variables
47 _vars.reserve( fg.nrVars() );
48 for( size_t i = 0; i < fg.nrVars(); i++ )
49 _vars.push_back( fg.var(i) );
50
51 // copy clusters
52 _clusters.reserve( fg.nrFactors() );
53 for( size_t I = 0; I < fg.nrFactors(); I++ )
54 _clusters.push_back( fg.factor(I).vars() );
55
56 if( onlyMaximal )
57 eraseNonMaximal();
58 }
59
60
61 size_t sequentialVariableElimination::operator()( const ClusterGraph &cl, const std::set<size_t> &/*remainingVars*/ ) {
62 return cl.findVar( seq.at(i++) );
63 }
64
65
66 size_t greedyVariableElimination::operator()( const ClusterGraph &cl, const std::set<size_t> &remainingVars ) {
67 set<size_t>::const_iterator lowest = remainingVars.end();
68 size_t lowest_cost = -1UL;
69 for( set<size_t>::const_iterator i = remainingVars.begin(); i != remainingVars.end(); i++ ) {
70 size_t cost = heuristic( cl, *i );
71 if( lowest == remainingVars.end() || lowest_cost > cost ) {
72 lowest = i;
73 lowest_cost = cost;
74 }
75 }
76 return *lowest;
77 }
78
79
80 size_t eliminationCost_MinNeighbors( const ClusterGraph &cl, size_t i ) {
81 return cl.bipGraph().delta1( i ).size();
82 }
83
84
85 size_t eliminationCost_MinWeight( const ClusterGraph &cl, size_t i ) {
86 SmallSet<size_t> id_n = cl.bipGraph().delta1( i );
87
88 size_t cost = 1;
89 for( SmallSet<size_t>::const_iterator it = id_n.begin(); it != id_n.end(); it++ )
90 cost *= cl.vars()[*it].states();
91
92 return cost;
93 }
94
95
96 size_t eliminationCost_MinFill( const ClusterGraph &cl, size_t i ) {
97 SmallSet<size_t> id_n = cl.bipGraph().delta1( i );
98
99 size_t cost = 0;
100 // for each unordered pair {i1,i2} adjacent to n
101 for( SmallSet<size_t>::const_iterator it1 = id_n.begin(); it1 != id_n.end(); it1++ )
102 for( SmallSet<size_t>::const_iterator it2 = it1; it2 != id_n.end(); it2++ )
103 if( it1 != it2 ) {
104 // if i1 and i2 are not adjacent, eliminating n would make them adjacent
105 if( !cl.adj(*it1, *it2) )
106 cost++;
107 }
108
109 return cost;
110 }
111
112
113 size_t eliminationCost_WeightedMinFill( const ClusterGraph &cl, size_t i ) {
114 SmallSet<size_t> id_n = cl.bipGraph().delta1( i );
115
116 size_t cost = 0;
117 // for each unordered pair {i1,i2} adjacent to n
118 for( SmallSet<size_t>::const_iterator it1 = id_n.begin(); it1 != id_n.end(); it1++ )
119 for( SmallSet<size_t>::const_iterator it2 = it1; it2 != id_n.end(); it2++ )
120 if( it1 != it2 ) {
121 // if i1 and i2 are not adjacent, eliminating n would make them adjacent
122 if( !cl.adj(*it1, *it2) )
123 cost += cl.vars()[*it1].states() * cl.vars()[*it2].states();
124 }
125
126 return cost;
127 }
128
129
130 } // end of namespace dai