@@ -200,6 +200,109 @@ void SinkClustering::run(const unsigned groupSize,
200200 }
201201}
202202
203+ void SinkClustering::repairClusteringSolution (
204+ const unsigned groupSize,
205+ vector<vector<Point<double >>>& solutionPoints,
206+ vector<vector<unsigned >>& solutionPointsIdx,
207+ vector<vector<unsigned >>& solutions,
208+ int & single_cluster_count,
209+ int & solved_cluster_count)
210+ {
211+ int cluster_num = solutions.size ();
212+ for (int c = 0 ; c < cluster_num; ++c) {
213+ // only clusters with a single sink
214+ if (solutionPoints[c].size () == 1 ) {
215+ single_cluster_count++;
216+ // get info of the single sink
217+ const unsigned idx = solutionPointsIdx[c][0 ];
218+ const Point<double >& p = points_[idx];
219+ // init variables to repair
220+ double min_dist_to_add = maxInternalDiameter_;
221+ int min_sink_idx_to_add = -1 , min_cluster_idx_to_add = -1 ;
222+ double min_dist_to_merge = maxInternalDiameter_;
223+ int cluster_idx_to_merge = -1 ;
224+ // Find cluster to add one sink
225+ for (int k = 0 ; k < cluster_num; ++k) {
226+ if (k == c) {
227+ continue ;
228+ }
229+ // iter all sinks of candidate cluster
230+ unsigned pointIdx = 0 ;
231+ double distanceCost = 0 ;
232+ double capCost = pointsCap_[idx];
233+ for (const auto & comparisonPoint : solutionPoints[k]) {
234+ const double cost = HTree_->computeDist (p, comparisonPoint);
235+ const double cap_cost
236+ = pointsCap_[idx]
237+ + (cost * capPerUnit_
238+ + pointsCap_[solutionPointsIdx[k][pointIdx]]);
239+ if (solutionPoints[k].size () > 2 && cost < min_dist_to_add
240+ && !isLimitExceeded (2 , cost, cap_cost, groupSize)) {
241+ min_dist_to_add = cost;
242+ min_sink_idx_to_add = pointIdx;
243+ min_cluster_idx_to_add = k;
244+ }
245+ if (useMaxCapLimit_) {
246+ capCost += cost * capPerUnit_
247+ + pointsCap_[solutionPointsIdx[k][pointIdx]];
248+ }
249+ pointIdx++;
250+ distanceCost = std::max (cost, distanceCost);
251+ }
252+
253+ if (!isLimitExceeded (
254+ solutionPoints[k].size (), distanceCost, capCost, groupSize)
255+ && distanceCost < min_dist_to_merge) {
256+ cluster_idx_to_merge = k;
257+ min_dist_to_merge = distanceCost;
258+ }
259+ }
260+ // cluster found to merge the single cluster
261+ if (cluster_idx_to_merge != -1 ) {
262+ solved_cluster_count++;
263+ // Add sink to found cluster
264+ solutionPoints[cluster_idx_to_merge].push_back (p);
265+ solutionPointsIdx[cluster_idx_to_merge].push_back (idx);
266+ solutions[cluster_idx_to_merge].push_back (idx);
267+ // move cluster to the last to remove it from the vectors
268+ std::swap (solutionPoints[c], solutionPoints[cluster_num - 1 ]);
269+ solutionPoints.pop_back ();
270+ std::swap (solutionPointsIdx[c], solutionPointsIdx[cluster_num - 1 ]);
271+ solutionPointsIdx.pop_back ();
272+ std::swap (solutions[c], solutions[cluster_num - 1 ]);
273+ solutions.pop_back ();
274+ cluster_num--;
275+ if (c < cluster_num && solutionPoints[c].size () == 1 ) {
276+ c--;
277+ }
278+ }
279+ // if it can not be added on cluster near and there is a sink near, then
280+ // add the sink on cluster
281+ else if (min_cluster_idx_to_add != -1 ) {
282+ solved_cluster_count++;
283+ // add the more nearby sink on cluster
284+ const unsigned idx_p
285+ = solutionPointsIdx[min_cluster_idx_to_add][min_sink_idx_to_add];
286+ solutionPoints[c].push_back (points_[idx_p]);
287+ solutionPointsIdx[c].push_back (idx_p);
288+ solutions[c].push_back (idx_p);
289+ // delete the sink of old cluster
290+ const unsigned old_size = solutionPoints[min_cluster_idx_to_add].size ();
291+ std::swap (solutionPoints[min_cluster_idx_to_add][min_sink_idx_to_add],
292+ solutionPoints[min_cluster_idx_to_add][old_size - 1 ]);
293+ solutionPoints[min_cluster_idx_to_add].pop_back ();
294+ std::swap (
295+ solutionPointsIdx[min_cluster_idx_to_add][min_sink_idx_to_add],
296+ solutionPointsIdx[min_cluster_idx_to_add][old_size - 1 ]);
297+ solutionPointsIdx[min_cluster_idx_to_add].pop_back ();
298+ std::swap (solutions[min_cluster_idx_to_add][min_sink_idx_to_add],
299+ solutions[min_cluster_idx_to_add][old_size - 1 ]);
300+ solutions[min_cluster_idx_to_add].pop_back ();
301+ }
302+ }
303+ }
304+ }
305+
203306bool SinkClustering::findBestMatching (const unsigned groupSize)
204307{
205308 // Counts how many clusters are in each solution.
@@ -330,6 +433,22 @@ bool SinkClustering::findBestMatching(const unsigned groupSize)
330433 bestSolutionCost_);
331434 // Save the solution for the Tree Builder.
332435 if (bestSolutionFound) {
436+ // Repair single sink on cluster
437+ int single_cluster_count = 0 ;
438+ int solved_cluster_count = 0 ;
439+ repairClusteringSolution (groupSize,
440+ solutionPoints[bestSolution],
441+ solutionPointsIdx[bestSolution],
442+ solutions[bestSolution],
443+ single_cluster_count,
444+ solved_cluster_count);
445+ if (logger_->debugCheck (CTS , " clustering" , 2 )) {
446+ logger_->report (
447+ " Best solution has {} clusters with single sink, {} clusters move to "
448+ " nearby cluster" ,
449+ single_cluster_count,
450+ solved_cluster_count);
451+ }
333452 bestSolution_ = solutions[bestSolution];
334453 // Calculate max diameter to save
335454 double span;
0 commit comments