Limbo
3.5.4
Toggle main menu visibility
Loading...
Searching...
No Matches
limbo
algorithms
partition
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
16
namespace
limbo
17
{
19
namespace
algorithms
20
{
22
namespace
partition
23
{
24
30
template
<
typename
GainCalcType>
31
class
FMMultiWay
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
;
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)
60
,
m_num_partitions
(tp)
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
;
101
int
m_num_vertice
;
102
int
m_num_partitions
;
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
{
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
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>
150
void
FMMultiWay<GainCalcType>::revert_to_kth_move
(
int
k)
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>
157
void
FMMultiWay<GainCalcType>::reset
()
158
{
159
m_vFixed
.assign(
m_vFixed
.size(),
false
);
160
m_vVertexMove
.clear();
161
}
162
163
template
<
typename
GainCalcType>
164
void
FMMultiWay<GainCalcType>::run
()
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 > 1e-3);
188
}
189
190
}
// namespace partition
191
}
// namespace algorithms
192
}
// namespace limbo
193
194
#endif
limbo::algorithms::partition::FMMultiWay::operator()
void operator()()
API to run the algorithm.
Definition
FMMultiWay.h:81
limbo::algorithms::partition::FMMultiWay::m_vPartition
std::vector< signed char > m_vPartition
an array storing partition of each vertex
Definition
FMMultiWay.h:103
limbo::algorithms::partition::FMMultiWay::set_partition
void set_partition(int v, signed char p)
set vertex v to partition p
Definition
FMMultiWay.h:69
limbo::algorithms::partition::FMMultiWay::run
void run()
kernel function to run the algorithm
Definition
FMMultiWay.h:164
limbo::algorithms::partition::FMMultiWay::set_partitions
void set_partitions(Iterator first, Iterator last)
set partitions
Definition
FMMultiWay.h:74
limbo::algorithms::partition::FMMultiWay::m_num_partitions
int m_num_partitions
total number of partitions
Definition
FMMultiWay.h:102
limbo::algorithms::partition::FMMultiWay::m_gain_calc
gain_calc_type const & m_gain_calc
function object to calculate gains
Definition
FMMultiWay.h:100
limbo::algorithms::partition::FMMultiWay::m_vVertexMove
std::vector< VertexMove > m_vVertexMove
record vertex movement during each iteration
Definition
FMMultiWay.h:105
limbo::algorithms::partition::FMMultiWay::reset
void reset()
reset movements and fixed flags
Definition
FMMultiWay.h:157
limbo::algorithms::partition::FMMultiWay::FMMultiWay
FMMultiWay(gain_calc_type const &gc, int tn, signed char tp)
constructor
Definition
FMMultiWay.h:57
limbo::algorithms::partition::FMMultiWay::revert_to_kth_move
void revert_to_kth_move(int k)
Definition
FMMultiWay.h:150
limbo::algorithms::partition::FMMultiWay::m_vFixed
std::vector< bool > m_vFixed
whehter fixed during current iteration
Definition
FMMultiWay.h:104
limbo::algorithms::partition::FMMultiWay::best_kth_move
void best_kth_move(int &k, gain_value_type &improve) const
Definition
FMMultiWay.h:133
limbo::algorithms::partition::FMMultiWay::find_candidate
void find_candidate(int &cv, signed char &cp, gain_value_type &max_gain) const
Definition
FMMultiWay.h:109
limbo::algorithms::partition::FMMultiWay::m_num_vertice
int m_num_vertice
total number of vertices
Definition
FMMultiWay.h:101
limbo::algorithms::partition::FMMultiWay::partition
signed char partition(int v) const
get partition of a vertex
Definition
FMMultiWay.h:78
limbo::algorithms::partition
namespace for Limbo.Algorithms.Partition
Definition
FM.h:41
limbo::algorithms
namespace for Limbo.algorithms
Definition
GraphUtility.h:26
limbo
namespace for Limbo
Definition
GraphUtility.h:23
limbo::sum
std::iterator_traits< Iterator >::value_type sum(Iterator first, Iterator last)
get summation of an array
Definition
Math.h:31
limbo::algorithms::partition::FMMultiWay::VertexMove
a class denotes movement of vertex
Definition
FMMultiWay.h:42
limbo::algorithms::partition::FMMultiWay::VertexMove::gain
gain_value_type gain
Definition
FMMultiWay.h:45
limbo::algorithms::partition::FMMultiWay::VertexMove::orig_partition
signed char orig_partition
original partition
Definition
FMMultiWay.h:44
limbo::algorithms::partition::FMMultiWay::VertexMove::vertex
int vertex
vertex id
Definition
FMMultiWay.h:43
limbo::algorithms::partition::FMMultiWay::VertexMove::VertexMove
VertexMove(int v, int op, gain_value_type g)
constructor
Definition
FMMultiWay.h:50
Generated on
for Limbo by
1.17.0