Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FMMultiWay.h
Go to the documentation of this file.
1 
12 #ifndef LIMBO_ALGORITHMS_PARTITION_FMMULTIWAY_H
13 #define LIMBO_ALGORITHMS_PARTITION_FMMULTIWAY_H
14 
16 namespace limbo
17 {
19 namespace algorithms
20 {
22 namespace partition
23 {
24 
30 template <typename GainCalcType>
32 {
33  public:
35  typedef GainCalcType gain_calc_type;
36  typedef typename gain_calc_type::value_type gain_value_type;
38 
41  struct VertexMove
42  {
43  int vertex;
44  signed char orig_partition;
45  gain_value_type gain;
46  VertexMove(int v, int op, gain_value_type g) : vertex(v), orig_partition(op), gain(g) {}
51  };
52 
57  FMMultiWay(gain_calc_type const& gc, int tn, signed char tp)
58  : m_gain_calc(gc)
59  , m_num_vertice(tn)
60  , m_num_partitions(tp)
61  , m_vPartition(tn, -1)
62  , m_vFixed(tn, false)
63  {
64  m_vVertexMove.reserve(tn);
65  }
69  void set_partition(int v, signed char p) {m_vPartition[v] = p;}
73  template <typename Iterator>
74  void set_partitions(Iterator first, Iterator last) {m_vPartition.assign(first, last);}
78  signed char partition(int v) const {return m_vPartition[v];}
79 
81  void operator()() {return run();}
82  protected:
84  void run();
89  void find_candidate(int& cv, signed char& cp, gain_value_type& max_gain) const;
93  void best_kth_move(int& k, gain_value_type& improve) const;
96  void revert_to_kth_move(int k);
98  void reset();
99 
100  gain_calc_type const& m_gain_calc;
103  std::vector<signed char> m_vPartition;
104  std::vector<bool> m_vFixed;
105  std::vector<VertexMove> m_vVertexMove;
106 };
107 
108 template <typename GainCalcType>
109 void FMMultiWay<GainCalcType>::find_candidate(int& cv, signed char& cp, FMMultiWay<GainCalcType>::gain_value_type& max_gain) const
110 {
112 
113  for (int v = 0; v != m_num_vertice; ++v)
114  {
115  // skip fixed vertex
116  if (m_vFixed[v]) continue;
117  for (signed char p = 0; p != m_num_partitions; ++p)
118  {
119  // skip its own partition
120  if (p == m_vPartition[v]) continue;
121  gain_value_type cur_gain = m_gain_calc(v, m_vPartition[v], p, m_vPartition);
122  if (cur_gain > max_gain)
123  {
124  cv = v;
125  cp = p;
126  max_gain = cur_gain;
127  }
128  }
129  }
130 }
131 
132 template <typename GainCalcType>
133 void FMMultiWay<GainCalcType>::best_kth_move(int& k, FMMultiWay<GainCalcType>::gain_value_type& improve) const
134 {
135  k = -1;
136  improve = 0;
137  gain_value_type sum = 0;
138  for (int i = 0; i != m_num_vertice; ++i)
139  {
140  sum += m_vVertexMove[i].gain;
141  if (sum > improve)
142  {
143  improve = sum;
144  k = i;
145  }
146  }
147 }
148 
149 template <typename GainCalcType>
151 {
152  for (int i = m_num_vertice-1; i > k; --i)
153  m_vPartition[m_vVertexMove[i].vertex] = m_vVertexMove[i].orig_partition;
154 }
155 
156 template <typename GainCalcType>
158 {
159  m_vFixed.assign(m_vFixed.size(), false);
160  m_vVertexMove.clear();
161 }
162 
163 template <typename GainCalcType>
165 {
166  gain_value_type improve = 0;
167  do
168  {
169  for (int i = 0; i != m_num_vertice; ++i)
170  {
171  int v = -1;
172  signed char p = -1;
173  gain_value_type gain = 0;
174  find_candidate(v, p, gain);
175  assert(v >= 0);
176 
177  // collect move
178  m_vVertexMove.push_back(VertexMove(v, m_vPartition[v], gain));
179  // apply
180  m_vPartition[v] = p;
181  m_vFixed[v] = true;
182  }
183  int k = -1;
184  best_kth_move(k, improve);
185  revert_to_kth_move(k);
186  reset();
187  } while (improve > 0);
188 }
189 
190 } // namespace partition
191 } // namespace algorithms
192 } // namespace limbo
193 
194 #endif
signed char orig_partition
original partition
Definition: FMMultiWay.h:44
signed char partition(int v) const
get partition of a vertex
Definition: FMMultiWay.h:78
std::vector< bool > m_vFixed
whehter fixed during current iteration
Definition: FMMultiWay.h:104
std::vector< signed char > m_vPartition
an array storing partition of each vertex
Definition: FMMultiWay.h:103
int m_num_vertice
total number of vertices
Definition: FMMultiWay.h:101
void operator()()
API to run the algorithm.
Definition: FMMultiWay.h:81
void set_partition(int v, signed char p)
set vertex v to partition p
Definition: FMMultiWay.h:69
gain_calc_type const & m_gain_calc
function object to calculate gains
Definition: FMMultiWay.h:100
void run()
kernel function to run the algorithm
Definition: FMMultiWay.h:164
std::vector< VertexMove > m_vVertexMove
record vertex movement during each iteration
Definition: FMMultiWay.h:105
std::iterator_traits< Iterator >::value_type sum(Iterator first, Iterator last)
get summation of an array
Definition: Math.h:31
void best_kth_move(int &k, gain_value_type &improve) const
Definition: FMMultiWay.h:133
namespace for Limbo
Definition: GraphUtility.h:22
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition: Math.h:61
void set_partitions(Iterator first, Iterator last)
set partitions
Definition: FMMultiWay.h:74
a class denotes movement of vertex
Definition: FMMultiWay.h:41
void find_candidate(int &cv, signed char &cp, gain_value_type &max_gain) const
Definition: FMMultiWay.h:109
void reset()
reset movements and fixed flags
Definition: FMMultiWay.h:157
VertexMove(int v, int op, gain_value_type g)
constructor
Definition: FMMultiWay.h:50
int m_num_partitions
total number of partitions
Definition: FMMultiWay.h:102
FMMultiWay(gain_calc_type const &gc, int tn, signed char tp)
constructor
Definition: FMMultiWay.h:57