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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2025, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* As an exception, when this program is distributed through (i) the *
* App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or *
* (iii) Google Play by Google Inc., then that store may impose any *
* digital rights management, device limits and/or redistribution *
* restrictions that are required by its terms of service. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include <cstdlib>
#include "triangulation/dim4.h"
#include "utilities/randutils.h"
namespace regina {
template <Triangulation<4>::SimplifyContext context>
bool Triangulation<4>::simplifyToLocalMinimumInternal(bool perform,
ProgressTrackerObjective* tracker) {
if (! perform) {
// In this scenario there should be no progress tracker.
ensureSkeleton();
if constexpr (context != SimplifyContext::UpDownDescent) {
// Crush edges if we can.
if (countVertices() > countComponents() &&
countVertices() > countBoundaryComponents())
for (Edge<4>* e : edges())
if (hasCollapseEdge(e))
return true;
}
// Look for internal simplifications.
// Our order of tests follows the case where perform is true.
for (Edge<4>* e : edges())
if (has20(e))
return true;
for (Triangle<4>* t : triangles())
if (has20(t))
return true;
if constexpr (context == SimplifyContext::UpDownDescent) {
// In this context, we are not allowed to try any other moves.
return false;
}
for (Vertex<4>* v : vertices())
if (has20(v))
return true;
for (Edge<4>* e : edges())
if (hasPachner(e))
return true;
// Look for boundary simplifications.
if (hasBoundaryTetrahedra()) {
for (BoundaryComponent<4>* bc : boundaryComponents())
for (Tetrahedron<4>* f : bc->facets())
if (hasShellBoundary(f->front().pentachoron()))
return true;
}
return false;
}
bool changed = false; // Has anything changed ever (for return value)?
bool changedNow = true; // Did we just change something (for loop control)?
{ // Begin scope for change event span.
PacketChangeGroup span(*this);
while (changedNow) {
changedNow = false;
ensureSkeleton();
if (tracker && tracker->isCancelled())
return changed;
if constexpr (context != SimplifyContext::UpDownDescent) {
// Crush edges if we can.
if (countVertices() > countComponents() &&
countVertices() > countBoundaryComponents()) {
for (Edge<4>* e : edges()) {
if (collapseEdge(e)) {
changedNow = changed = true;
if (tracker)
tracker->setObjective(size());
break;
}
}
if (changedNow) {
if (perform)
continue;
else
return true;
}
}
}
// Look for internal simplifications.
// Experience suggests that 2-0 moves are more important to
// "unblock" other moves, and we should leave the simpler
// 4-2 moves until last.
//
// We prioritise edge moves, since in general we are trying to
// reduce the number of edges.
for (Edge<4>* e : edges()) {
if (move20(e)) {
changedNow = changed = true;
if (tracker)
tracker->setObjective(size());
break;
}
}
if (changedNow) {
if (perform)
continue;
else
return true;
}
for (Triangle<4>* t : triangles()) {
if (move20(t)) {
changedNow = changed = true;
if (tracker)
tracker->setObjective(size());
break;
}
}
if (changedNow) {
if (perform)
continue;
else
return true;
}
if constexpr (context == SimplifyContext::UpDownDescent) {
// In this context, we are not allowed to try any other moves.
break;
}
for (Vertex<4>* v : vertices()) {
if (move20(v)) {
changedNow = changed = true;
if (tracker)
tracker->setObjective(size());
break;
}
}
if (changedNow) {
if (perform)
continue;
else
return true;
}
for (Edge<4>* e : edges()) {
if (pachner(e)) {
changedNow = changed = true;
if (tracker)
tracker->setObjective(size());
break;
}
}
if (changedNow) {
if (perform)
continue;
else
return true;
}
// Look for boundary simplifications.
if (hasBoundaryTetrahedra()) {
for (BoundaryComponent<4>* bc : boundaryComponents()) {
// Run through facets of this boundary component looking
// for shell boundary moves.
for (Tetrahedron<4>* f : bc->facets())
if (shellBoundary(f->front().pentachoron())) {
changedNow = changed = true;
if (tracker)
tracker->setObjective(size());
break;
}
if (changedNow)
break;
}
if (changedNow) {
if (perform)
continue;
else
return true;
}
}
}
} // End scope for change event span.
return changed;
}
// Instantiate all variants of simplifyToLocalMinimumInternal().
template bool Triangulation<4>::simplifyToLocalMinimumInternal<
Triangulation<4>::SimplifyContext::Best>(
bool, ProgressTrackerObjective*);
template bool Triangulation<4>::simplifyToLocalMinimumInternal<
Triangulation<4>::SimplifyContext::UpDownDescent>(
bool, ProgressTrackerObjective*);
template <Triangulation<4>::SimplifyContext context>
bool Triangulation<4>::simplifyGreedyInternal(
ProgressTrackerObjective* tracker) {
bool changed = false;
PacketChangeGroup span(*this);
// ---------- Reduce to local minimum ----------
if (tracker)
tracker->newStage("Reducing to local minimum");
if (simplifyToLocalMinimumInternal<context>(true, tracker)) {
if (tracker)
if (! tracker->setObjective(size()))
return true; // cancelled, but triangulation was improved
changed = true;
} else if (tracker && tracker->isCancelled())
return false; // cancelled, and triangulation was not improved
// ---------- Try random "sideways" moves ----------
// The pool of available 3-3 moves that we will randomly select from.
std::vector<Triangle<4>*> threeThreeAvailable;
bool allowOpenBook;
if constexpr (context == SimplifyContext::UpDownDescent) {
// In this context, we are not allowed to use book-opening moves.
allowOpenBook = false;
} else {
// We need boundary tetrahedra for book-opening moves to make sense.
// Note: whether boundary tetrahedra exist should remain the same as
// the simplification algorithm runs, so precomputing this now is fine.
allowOpenBook = hasBoundaryTetrahedra();
}
if (tracker) {
if (allowOpenBook)
tracker->newStage("Trying 3-3 and open-book moves");
else
tracker->newStage("Trying 3-3 moves");
}
while (true) {
// We will try (1) 3-3 moves always, and (2) open-book moves if allowed.
// --- Random 3-3 moves ---
// Clone the triangulation and start making changes that might or
// might not lead to a simplification.
// If we've already simplified then there's no need to use a
// separate clone since we won't need to undo further changes.
//
// If we _are_ cloning the triangulation, ensure we clone the locks
// also.
Triangulation<4>* use = (changed ? this :
new Triangulation<4>(*this, false, true));
// Make random 3-3 moves.
static constexpr int COEFF_3_3 =
(context == SimplifyContext::UpDownDescent ? 200 : 10);
size_t threeThreeAttempts = 0;
size_t threeThreeCap = 0;
while (true) {
// Calculate the list of available 3-3 moves.
threeThreeAvailable.clear();
// Use triangles() to ensure the skeleton has been calculated.
for (Triangle<4>* triangle : use->triangles())
if (use->hasPachner(triangle))
threeThreeAvailable.push_back(triangle);
// Increment threeThreeCap if needed.
if (threeThreeCap < COEFF_3_3 * threeThreeAvailable.size())
threeThreeCap = COEFF_3_3 * threeThreeAvailable.size();
// Have we tried enough 3-3 moves?
if (threeThreeAttempts >= threeThreeCap)
break;
// Perform a random 3-3 move on the clone.
Triangle<4>* threeThreeChoice = threeThreeAvailable[
RandomEngine::rand(threeThreeAvailable.size())];
use->pachner(threeThreeChoice, regina::unprotected);
// See if we can simplify now.
if (use->simplifyToLocalMinimumInternal<context>(true, tracker)) {
// We have successfully simplified!
if (tracker)
if (! tracker->setObjective(use->size())) {
// The operation was cancelled: stop making 3-3 moves,
// but don't return until we collect results from *use.
break;
}
// Start all over again.
threeThreeAttempts = threeThreeCap = 0;
} else {
if (tracker && tracker->isCancelled())
break; // as above: don't return until we collect results
threeThreeAttempts++;
}
}
// Sync the real triangulation with the clone if appropriate.
if (use != this) {
// At this point, changed == false.
if (use->size() < size()) {
// The 3-3 moves were successful; accept them.
swap(*use);
changed = true;
}
delete use;
}
// Now that we have collected results from *use, we can finally return
// if the operation was cancelled.
if (tracker && tracker->isCancelled())
return changed;
// --- Open book moves ---
if (allowOpenBook) {
// Clone again, always -- we don't want to create gratuitous
// boundary facets if they won't be of any help.
//
// Again, don't clone properties, but do clone locks.
Triangulation<4> working(*this, false, true);
// Perform every book opening move we can find.
bool opened = false;
bool openedNow = true;
while (openedNow) {
openedNow = false;
for (Tetrahedron<4>* tet : working.tetrahedra())
if (working.openBook(tet)) {
opened = openedNow = true;
break;
}
}
// If we're lucky, we can now simplify further.
if (opened) {
if (working.simplifyToLocalMinimumInternal<context>(
true, tracker)) {
// Yay!
swap(working);
if (tracker)
if (! tracker->setObjective(size()))
return true; // cancelled, and improved
changed = true;
} else {
// No good.
// Throw away working, and ignore our open books.
if (tracker && tracker->isCancelled())
return changed;
opened = false;
}
} else if (tracker && tracker->isCancelled())
return changed;
// If we did any book opening stuff, start all over again.
if (opened)
continue;
}
// Nothing more we can do here.
break;
}
return changed;
}
// Instantiate all variants of simplifyGreedyInternal().
template bool Triangulation<4>::simplifyGreedyInternal<
Triangulation<4>::SimplifyContext::Best>(ProgressTrackerObjective*);
template bool Triangulation<4>::simplifyGreedyInternal<
Triangulation<4>::SimplifyContext::UpDownDescent>(ProgressTrackerObjective*);
bool Triangulation<4>::simplifyUpDownInternal(ssize_t max24, ssize_t max33,
bool alwaysModify, ProgressTrackerObjective* tracker) {
if ((! alwaysModify) && size() <= 2)
return false;
// Set up some sensible default arguments.
if (max24 < 0)
max24 = 10;
if (max33 < 0)
max33 = max24 * 3;
size_t initSize = size();
// Set up a temporary working triangulation, just in case we end up making
// things worse, not better.
Triangulation<4> working(*this, false, true);
for (ssize_t attempts = 1; attempts <= max24; ++attempts) {
if (tracker)
tracker->newStage("Trying run of " + std::to_string(attempts)
+ "× 2-4 moves");
// Do attempts successive 2-4 moves.
for (ssize_t i=0; i<attempts; i++)
for (auto tet : working.tetrahedra())
if (working.pachner(tet))
break;
// Simplify using only 2-0 edge/triangle moves and 3-3 moves.
// Do not bother with the tracker for this.
working.simplifyGreedyInternal<SimplifyContext::UpDownDescent>(nullptr);
if (working.size() < initSize) {
// We simplified!
swap(working);
if (tracker)
tracker->setObjective(size());
return true;
} else if (tracker && tracker->isCancelled())
return false;
// Make the requested number of 3-3 moves.
for (int i=0; i<max33; i++)
for (auto tri : working.triangles())
if (working.pachner(tri))
break;
if (tracker && tracker->isCancelled())
return false;
}
// We never reduced the number of pentachora.
if (alwaysModify)
swap(working);
return false;
}
bool Triangulation<4>::simplify(ProgressTrackerObjective* tracker) {
// For now, we try our greedy heuristics followed by one round of up-down
// simplify (regardless of whether the greedy heuritics worked).
// Regarding progress trackers: our internal functions will manage the
// stages and objective values. We just need to check for cancellation.
bool changed;
{
PacketChangeGroup span(*this);
// Stage 1: greedy heuristics
changed = simplifyGreedyInternal<SimplifyContext::Best>(tracker);
// Stage 2: up-down simplify
if (! (tracker && tracker->isCancelled()))
changed |= simplifyUpDownInternal(-1, -1, false, tracker);
}
// Now that the packet change span is closed, no more actions or updates
// will be triggered from our end. We can safely mark the progress
// tracker as finished.
if (tracker)
tracker->setFinished();
return changed;
}
} // namespace regina
|