1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* ========================================================================== */
/* === Source/Mongoose_ImproveFM.cpp ======================================== */
/* ========================================================================== */
/* -----------------------------------------------------------------------------
* Mongoose Graph Partitioning Library Copyright (C) 2017-2018,
* Scott P. Kolodziej, Nuri S. Yeralan, Timothy A. Davis, William W. Hager
* Mongoose is licensed under Version 3 of the GNU General Public License.
* Mongoose is also available under other licenses; contact authors for details.
* -------------------------------------------------------------------------- */
#include "Mongoose_ImproveFM.hpp"
#include "Mongoose_BoundaryHeap.hpp"
#include "Mongoose_Debug.hpp"
#include "Mongoose_Internal.hpp"
#include "Mongoose_Logger.hpp"
namespace Mongoose
{
//-----------------------------------------------------------------------------
// Wrapper for Fidducia-Mattheyes cut improvement.
//-----------------------------------------------------------------------------
void improveCutUsingFM(EdgeCutProblem *graph, const EdgeCut_Options *options)
{
Logger::tic(FMTiming);
if (!options->use_FM)
return;
double heuCost = INFINITY;
for (Int i = 0;
i < options->FM_max_num_refinements && graph->heuCost < heuCost; i++)
{
heuCost = graph->heuCost;
fmRefine_worker(graph, options);
}
Logger::toc(FMTiming);
}
//-----------------------------------------------------------------------------
// Make a number of partition moves while considering the impact on problem
// balance.
//-----------------------------------------------------------------------------
void fmRefine_worker(EdgeCutProblem *graph, const EdgeCut_Options *options)
{
double *Gw = graph->w;
double W = graph->W;
Int **bhHeap = graph->bhHeap;
Int *bhSize = graph->bhSize;
Int *externalDegree = graph->externalDegree;
double *gains = graph->vertexGains;
bool *partition = graph->partition;
/* Keep a stack of moved vertices. */
Int *stack = graph->matchmap;
Int head = 0, tail = 0;
/* create & initialize a working cost and a best cost. */
struct CutCost workingCost, bestCost;
workingCost.heuCost = bestCost.heuCost = graph->heuCost;
workingCost.cutCost = bestCost.cutCost = graph->cutCost;
workingCost.W[0] = bestCost.W[0] = graph->W0;
workingCost.W[1] = bestCost.W[1] = graph->W1;
workingCost.imbalance = bestCost.imbalance = graph->imbalance;
/* Tolerance and the linear penalty to assess. */
double tol = options->soft_split_tolerance;
double H = graph->H;
Int fmSearchDepth = options->FM_search_depth;
Int fmConsiderCount = options->FM_consider_count;
Int i = 0;
bool productive = true;
for (; i < fmSearchDepth && productive; i++)
{
productive = false;
/* Look for the best vertex to swap: */
struct SwapCandidate bestCandidate;
for (Int h = 0; h < 2; h++)
{
Int *heap = bhHeap[h];
Int size = bhSize[h];
for (Int c = 0; c < fmConsiderCount && c < size; c++)
{
/* Read the vertex, and if it's marked, try the next one. */
Int v = heap[c];
if (graph->isMarked(v))
{
continue;
}
/* Read the gain for the vertex. */
double gain = gains[v];
/* The balance penalty is the penalty to assess for the move. */
double vertexWeight = (Gw) ? Gw[v] : 1;
double imbalance = workingCost.imbalance
+ (h ? -1.0 : 1.0) * (vertexWeight / W);
double absImbalance = fabs(imbalance);
double imbalanceDelta
= absImbalance - fabs(workingCost.imbalance);
/* If the move hurts the balance past tol, add a penalty. */
double balPenalty = 0.0;
if (imbalanceDelta > 0 && absImbalance > tol)
{
balPenalty = absImbalance * H;
}
/* Heuristic cost is the cut cost reduced by the gain for making
* this move. The gain for the move is amplified by any impact
* to the balance penalty. */
double heuCost = workingCost.cutCost - (gain - balPenalty);
/* If our heuristic value is better than the running one: */
if (heuCost < bestCandidate.heuCost)
{
bestCandidate.vertex = v;
bestCandidate.partition = static_cast<bool>(h);
bestCandidate.vertexWeight = vertexWeight;
bestCandidate.gain = gain;
bestCandidate.bhPosition = c;
bestCandidate.imbalance = imbalance;
bestCandidate.heuCost = heuCost;
}
}
}
/* If we were able to find the best unmoved boundary vertex: */
if (bestCandidate.heuCost < INFINITY)
{
productive = true;
graph->mark(bestCandidate.vertex);
/* Move the vertex from the boundary into the move set. */
bhRemove(graph, options, bestCandidate.vertex, bestCandidate.gain,
bestCandidate.partition, bestCandidate.bhPosition);
stack[tail++] = bestCandidate.vertex;
/* Swap & update the vertex and its neighbors afterwards. */
fmSwap(graph, options, bestCandidate.vertex, bestCandidate.gain,
bestCandidate.partition);
/* Update the cut cost. */
workingCost.cutCost -= 2.0 * bestCandidate.gain;
workingCost.W[bestCandidate.partition]
-= bestCandidate.vertexWeight;
workingCost.W[!bestCandidate.partition]
+= bestCandidate.vertexWeight;
workingCost.imbalance = bestCandidate.imbalance;
double absImbalance = fabs(bestCandidate.imbalance);
workingCost.heuCost
= workingCost.cutCost
+ (absImbalance > tol ? absImbalance * H : 0.0);
/* Commit the cut if it's better. */
if (workingCost.heuCost < bestCost.heuCost)
{
bestCost = workingCost;
head = tail;
i = 0;
}
}
}
/* We've exhausted our search space, so undo all suboptimal moves. */
for (Int u = tail - 1; u >= head; u--)
{
Int vertex = stack[u];
Int bhVertexPosition = graph->BH_getIndex(vertex);
/* Unmark this vertex. */
graph->unmark(vertex);
/* It is possible, although rare, that a vertex may have gone
* from not in the boundary to an undo state that places it in
* the boundary. It is also possible that a previous swap added
* this vertex to the boundary already. */
if (bhVertexPosition != -1)
{
bhRemove(graph, options, vertex, gains[vertex], partition[vertex],
bhVertexPosition);
}
/* Swap the partition and compute the impact on neighbors. */
fmSwap(graph, options, vertex, gains[vertex], partition[vertex]);
if (externalDegree[vertex] > 0)
bhInsert(graph, vertex);
}
// clear the marks from all the vertices
graph->clearMarkArray();
/* Re-add any vertices that were moved that are still on the boundary. */
for (Int k = 0; k < head; k++)
{
Int vertex = stack[k];
if (externalDegree[vertex] > 0 && !graph->BH_inBoundary(vertex))
{
bhInsert(graph, vertex);
}
}
// clear the marks from all the vertices
graph->clearMarkArray();
/* Save the best cost back into the graph. */
graph->heuCost = bestCost.heuCost;
graph->cutCost = bestCost.cutCost;
graph->W0 = bestCost.W[0];
graph->W1 = bestCost.W[1];
graph->imbalance = bestCost.imbalance;
}
//-----------------------------------------------------------------------------
// This function swaps the partition of a vertex
//-----------------------------------------------------------------------------
void fmSwap(EdgeCutProblem *graph, const EdgeCut_Options *options, Int vertex, double gain,
bool oldPartition)
{
Int *Gp = graph->p;
Int *Gi = graph->i;
double *Gx = graph->x;
bool *partition = graph->partition;
double *gains = graph->vertexGains;
Int *externalDegree = graph->externalDegree;
Int **bhHeap = graph->bhHeap;
Int *bhSize = graph->bhSize;
/* Swap partitions */
bool newPartition = !oldPartition;
partition[vertex] = newPartition;
gains[vertex] = -gain;
/* Update neighbors. */
Int exD = 0;
for (Int p = Gp[vertex]; p < Gp[vertex + 1]; p++)
{
Int neighbor = Gi[p];
bool neighborPartition = partition[neighbor];
bool sameSide = (newPartition == neighborPartition);
/* Update the bestCandidate vertex's external degree. */
if (!sameSide)
exD++;
/* Update the neighbor's gain. */
double edgeWeight = (Gx) ? Gx[p] : 1;
double neighborGain = gains[neighbor];
neighborGain += 2 * (sameSide ? -edgeWeight : edgeWeight);
gains[neighbor] = neighborGain;
/* Update the neighbor's external degree. */
Int neighborExD = externalDegree[neighbor];
neighborExD += (sameSide ? -1 : 1);
externalDegree[neighbor] = neighborExD;
Int position = graph->BH_getIndex(neighbor);
/* If the neighbor was in a heap: */
if (position != -1)
{
/* If it had its externalDegree reduced to 0, remove it from the
* heap. */
if (neighborExD == 0)
{
bhRemove(graph, options, neighbor, neighborGain,
neighborPartition, position);
}
/* If the neighbor is in the heap, we touched its gain
* so make sure the heap property is satisfied. */
else
{
Int v = neighbor;
heapifyUp(graph, bhHeap[neighborPartition], gains, v, position,
neighborGain);
v = bhHeap[neighborPartition][position];
heapifyDown(graph, bhHeap[neighborPartition],
bhSize[neighborPartition], gains, v, position,
gains[v]);
}
}
/* Else the neighbor wasn't in the heap so add it. */
else
{
if (!graph->isMarked(neighbor))
{
ASSERT(!graph->BH_inBoundary(neighbor));
bhInsert(graph, neighbor);
}
}
}
externalDegree[vertex] = exD;
}
//-----------------------------------------------------------------------------
// This function computes the gain of a vertex
//-----------------------------------------------------------------------------
void calculateGain(EdgeCutProblem *graph, const EdgeCut_Options *options, Int vertex,
double *out_gain, Int *out_externalDegree)
{
(void)options; // Unused variable
Int *Gp = graph->p;
Int *Gi = graph->i;
double *Gx = graph->x;
bool *partition = graph->partition;
bool vp = partition[vertex];
double gain = 0.0;
Int externalDegree = 0;
for (Int p = Gp[vertex]; p < Gp[vertex + 1]; p++)
{
double ew = (Gx ? Gx[p] : 1.0);
bool sameSide = (partition[Gi[p]] == vp);
gain += (sameSide ? -ew : ew);
if (!sameSide)
externalDegree++;
}
/* Save outputs */
*out_gain = gain;
*out_externalDegree = externalDegree;
}
} // end namespace Mongoose
|