Limbo 3.5.4
Loading...
Searching...
No Matches
FMMultiWay.h
Go to the documentation of this file.
1
11
12#ifndef LIMBO_ALGORITHMS_PARTITION_FMMULTIWAY_H
13#define LIMBO_ALGORITHMS_PARTITION_FMMULTIWAY_H
14
16namespace limbo
17{
19namespace algorithms
20{
22namespace partition
23{
24
30template <typename GainCalcType>
32{
33 public:
35 typedef GainCalcType gain_calc_type;
36 typedef typename gain_calc_type::value_type gain_value_type;
38
42 {
43 int vertex;
44 signed char orig_partition;
45 gain_value_type gain;
50 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)
61 , m_vPartition(tn, -1)
62 , m_vFixed(tn, false)
63 {
64 m_vVertexMove.reserve(tn);
65 }
66
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
108template <typename GainCalcType>
109void FMMultiWay<GainCalcType>::find_candidate(int& cv, signed char& cp, FMMultiWay<GainCalcType>::gain_value_type& max_gain) const
110{
111 max_gain = -std::numeric_limits<gain_value_type>::max();
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
132template <typename GainCalcType>
133void 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
149template <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
156template <typename GainCalcType>
158{
159 m_vFixed.assign(m_vFixed.size(), false);
160 m_vVertexMove.clear();
161}
162
163template <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);
186 reset();
187 } while (improve > 1e-3);
188}
189
190} // namespace partition
191} // namespace algorithms
192} // namespace limbo
193
194#endif
void operator()()
API to run the algorithm.
Definition FMMultiWay.h:81
std::vector< signed char > m_vPartition
an array storing partition of each vertex
Definition FMMultiWay.h:103
void set_partition(int v, signed char p)
set vertex v to partition p
Definition FMMultiWay.h:69
void run()
kernel function to run the algorithm
Definition FMMultiWay.h:164
void set_partitions(Iterator first, Iterator last)
set partitions
Definition FMMultiWay.h:74
int m_num_partitions
total number of partitions
Definition FMMultiWay.h:102
gain_calc_type const & m_gain_calc
function object to calculate gains
Definition FMMultiWay.h:100
std::vector< VertexMove > m_vVertexMove
record vertex movement during each iteration
Definition FMMultiWay.h:105
void reset()
reset movements and fixed flags
Definition FMMultiWay.h:157
FMMultiWay(gain_calc_type const &gc, int tn, signed char tp)
constructor
Definition FMMultiWay.h:57
std::vector< bool > m_vFixed
whehter fixed during current iteration
Definition FMMultiWay.h:104
void best_kth_move(int &k, gain_value_type &improve) const
Definition FMMultiWay.h:133
void find_candidate(int &cv, signed char &cp, gain_value_type &max_gain) const
Definition FMMultiWay.h:109
int m_num_vertice
total number of vertices
Definition FMMultiWay.h:101
signed char partition(int v) const
get partition of a vertex
Definition FMMultiWay.h:78
namespace for Limbo.Algorithms.Partition
Definition FM.h:41
namespace for Limbo.algorithms
namespace for Limbo
std::iterator_traits< Iterator >::value_type sum(Iterator first, Iterator last)
get summation of an array
Definition Math.h:31
a class denotes movement of vertex
Definition FMMultiWay.h:42
VertexMove(int v, int op, gain_value_type g)
constructor
Definition FMMultiWay.h:50