File: simplifyglobal.cpp

package info (click to toggle)
regina-normal 4.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,824 kB
  • ctags: 7,862
  • sloc: cpp: 63,296; ansic: 12,913; sh: 10,556; perl: 3,294; makefile: 947; python: 188
file content (282 lines) | stat: -rw-r--r-- 11,072 bytes parent folder | download
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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2008, 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.                       *
 *                                                                        *
 *  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, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

#include <cstdlib>
#include "triangulation/ntriangulation.h"

// Affects the number of random 4-4 moves attempted during simplification.
#define COEFF_4_4 3

namespace regina {

bool NTriangulation::intelligentSimplify() {
    bool changed;

    // Don't automatically fire a change event - we don't know in
    // advance if changes will be made or not.

    { // Begin scope for change event block.
        ChangeEventBlock block(this, false);

        // Reduce to a local minimum.
        changed = simplifyToLocalMinimum(true);

        // Clone to work with when we might want to roll back changes.
        NTriangulation* use;

        // Variables used for selecting random 4-4 moves.
        std::vector<std::pair<NEdge*, int> > fourFourAvailable;
        std::pair<NEdge*, int> fourFourChoice;

        unsigned long fourFourAttempts;
        unsigned long fourFourCap;

        NEdge* edge;
        EdgeIterator eit;
        int axis;

        while (true) {
            // --- Random 4-4 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.
            use = (changed ? this : new NTriangulation(*this));

            // Make random 4-4 moves.
            fourFourAttempts = fourFourCap = 0;
            while (true) {
                // Calculate the list of available 4-4 moves.
                fourFourAvailable.clear();
                // Use getEdges() to ensure the skeleton has been calculated.
                for (eit = use->getEdges().begin();
                        eit != use->getEdges().end(); eit++) {
                    edge = *eit;
                    for (axis = 0; axis < 2; axis++)
                        if (use->fourFourMove(edge, axis, true, false))
                            fourFourAvailable.push_back(
                                std::make_pair(edge, axis));
                }

                // Increment fourFourCap if needed.
                if (fourFourCap < COEFF_4_4 * fourFourAvailable.size())
                    fourFourCap = COEFF_4_4 * fourFourAvailable.size();

                // Have we tried enough 4-4 moves?
                if (fourFourAttempts >= fourFourCap)
                    break;

                // Perform a random 4-4 move on the clone.
                fourFourChoice = fourFourAvailable[
                    static_cast<unsigned>(rand()) % fourFourAvailable.size()];
                use->fourFourMove(fourFourChoice.first, fourFourChoice.second,
                    false, true);

                // See if we can simplify now.
                if (use->simplifyToLocalMinimum(true)) {
                    // We have successfully simplified!
                    // Start all over again.
                    fourFourAttempts = fourFourCap = 0;
                } else
                    fourFourAttempts++;
            }

            // Sync the real triangulation with the clone if appropriate.
            if (use != this) {
                // At this point, changed == false.
                if (use->getNumberOfTetrahedra() < getNumberOfTetrahedra()) {
                    // The 4-4 moves were successful; accept them.
                    cloneFrom(*use);
                    changed = true;
                }
                delete use;
            }

            // At this point we have decided that 4-4 moves will help us
            // no more.

            // --- TODO: Open book moves ---

            // If we did any book opening stuff, move back to the beginning
            // of the loop.  Otherwise exit.
            break;
        }
    } // End scope for change event block.

    if (changed)
        fireChangedEvent();
    return changed;
}

bool NTriangulation::simplifyToLocalMinimum(bool perform) {
    EdgeIterator eit;
    VertexIterator vit;
    BoundaryComponentIterator bit;
    NEdge* edge;
    NBoundaryComponent* bc;
    unsigned long nFaces;
    unsigned long iFace;
    // unsigned long nEdges;
    // unsigned long iEdge;
    // std::deque<NEdgeEmbedding>::const_iterator embit, embbeginit, embendit;

    bool changed = false;   // Has anything changed ever (for return value)?
    bool changedNow = true; // Did we just change something (for loop control)?

    // Don't automatically fire a change event - we don't know in
    // advance if changes will be made or not.

    { // Begin scope for change event block.
        ChangeEventBlock block(this, false);

        while (changedNow) {
            changedNow = false;
            if (! calculatedSkeleton) {
                calculateSkeleton();
            }

            // Crush a maximal skeleton.
            /* Don't crush a maximal skeleton until we know what the
             * routine does!
            if (vertices.size() > components.size()) {
                if (crushMaximalForest()) {
                    if (! calculatedSkeleton)
                        calculateSkeleton();
                    // Keep trying to simplify in this iteration of the loop.
                    // Thus changedNow will remain false.
                    changed = true;
                }
            }
            */

            // Look for internal simplifications.
            for (eit = edges.begin(); eit != edges.end(); eit++) {
                edge = *eit;
                if (threeTwoMove(edge, true, perform)) {
                    changedNow = changed = true;
                    break;
                }
                if (twoZeroMove(edge, true, perform)) {
                    changedNow = changed = true;
                    break;
                }
                if (twoOneMove(edge, 0, true, perform)) {
                    changedNow = changed = true;
                    break;
                }
                if (twoOneMove(edge, 1, true, perform)) {
                    changedNow = changed = true;
                    break;
                }
            }
            if (changedNow) {
                if (perform)
                    continue;
                else
                    return true;
            }
            for (vit = vertices.begin(); vit != vertices.end(); vit++) {
                if (twoZeroMove(*vit, true, perform)) {
                    changedNow = changed = true;
                    break;
                }
            }
            if (changedNow) {
                if (perform)
                    continue;
                else
                    return true;
            }

            // Look for boundary simplifications.
            if (hasBoundaryFaces()) {
                for (bit = boundaryComponents.begin();
                        bit != boundaryComponents.end(); bit++) {
                    bc = *bit;

                    // Run through faces of this boundary component looking
                    // for shell boundary moves.
                    nFaces = (*bit)->getNumberOfFaces();
                    for (iFace = 0; iFace < nFaces; iFace++) {
                        if (shellBoundary((*bit)->getFace(iFace)->
                                getEmbedding(0).getTetrahedron(),
                                true, perform)) {
                            changedNow = changed = true;
                            break;
                        }
                    }
                    if (changedNow)
                        break;

                    /**
                     * Do NOT do open book moves here since they don't reduce
                     * the triangulation per se.  We'll do this in
                     * intelligentSimplify() instead.
                     */
                    /*
                    // Run through edges of this boundary component looking
                    // for open book moves.
                    nEdges = (*bit)->getNumberOfEdges();
                    for (iEdge = 0; iEdge < nEdges; iEdge++) {
                        embbeginit = (*bit)->getEdge(iEdge)->
                            getEmbeddings().begin();
                        embendit = (*bit)->getEdge(iEdge)->
                            getEmbeddings().end();
                        for (embit = embbeginit; embit != embendit; embit++)
                            if (openBook((*embit).getTetrahedron()->getFace(
                                    ((*embit).getVertices())[2]),
                                    true, perform)) {
                                changedNow = true;
                                changed = true;
                                break;
                            }
                        if (changedNow)
                            break;
                    }
                    if (changedNow)
                        break;
                    */
                }
                if (changedNow) {
                    if (perform)
                        continue;
                    else
                        return true;
                }
            }
        }
    } // End scope for change event block.

    if (changed)
        fireChangedEvent();
    return changed;
}

} // namespace regina