File: Mongoose_Debug.cpp

package info (click to toggle)
suitesparse 1%3A5.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 138,928 kB
  • sloc: ansic: 389,614; cpp: 24,213; makefile: 5,965; fortran: 1,927; java: 1,808; csh: 1,750; ruby: 725; sh: 226; perl: 225; python: 209; sed: 164; awk: 60
file content (397 lines) | stat: -rw-r--r-- 10,377 bytes parent folder | download | duplicates (3)
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
/* ========================================================================== */
/* === Source/Mongoose_Debug.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_Debug.hpp"

namespace Mongoose
{

#ifndef NDEBUG

// print a CSparse matrix
void print(cs *G)
{
    ASSERT(G);
    Int *Gp    = G->p;
    Int *Gi    = G->i;
    double *Gx = G->x;
    for (Int j = 0; j < G->n; j++)
    {
        for (int p = Gp[j]; p < Gp[j + 1]; p++)
        {
            PR(("G(%ld,%ld) = %g\n", Gi[p], j, (Gx) ? Gx[p] : 1));
        }
    }
}

// print a Mongoose graph
void print(Graph *G)
{
    ASSERT(G);
    Int *Gp    = G->p;
    Int *Gi    = G->i;
    double *Gx = G->x;
    for (Int j = 0; j < G->n; j++)
    {
        for (int p = Gp[j]; p < Gp[j + 1]; p++)
        {
            PR(("G(%ld,%ld) = %g\n", Gi[p], j, (Gx) ? Gx[p] : 1));
        }
    }
}

//------------------------------------------------------------------------------
// QPcheckCom
//------------------------------------------------------------------------------

// Check that the QPcom data structure is consistent and cost decreases

#define ERROR                                                                  \
    {                                                                          \
        FFLUSH;                                                                \
        ASSERT(0);                                                             \
    }

void QPcheckCom(EdgeCutProblem *G, const EdgeCut_Options *O, QPDelta *QP, bool check_b,
                Int nFreeSet, // use this instead of QP->nFreeSet
                double b      // use this instead of QP->b
)
{
    Int i, j, k, l;
    double s, t;

    ASSERT(G);
    ASSERT(O);
    ASSERT(QP);

    //--- FreeSet
    //  Int nFreeSet = QP->nFreeSet ;  /* number of i such that 0 < x_i < 1 */
    Int *FreeSet_list   = QP->FreeSet_list; /* list for free indices */
    Int *FreeSet_status = QP->FreeSet_status;
    /* FreeSet_status [i] = +1, -1, or 0 if x_i = 1, 0, or 0 < x_i < 1*/
    //---

    double *x = QP->x; /* current estimate of solution */

    /* problem specification */
    Int n      = G->n; /* problem dimension */
    double *Ex = G->x; /* numerical values for edge weights */
    Int *Ei    = G->i; /* adjacent vertices for each vertex */
    Int *Ep    = G->p; /* points into Ex or Ei */
    double *a  = G->w; /* a'x = b, lo <= b <= hi */

    double lo    = QP->lo;
    double hi    = QP->hi;
    double *D    = QP->D;        /* diagonal of quadratic */
    double *grad = QP->gradient; /* gradient at current x */
    double tol   = std::max(log10(O->gradproj_tolerance * G->worstCaseRatio),
                          O->gradproj_tolerance);

    // get workspace
    Int *w0       = (Int *)calloc(n + 1, sizeof(Int));          // [
    double *gtemp = (double *)malloc((n + 1) * sizeof(double)); // [

    ASSERT(w0);
    ASSERT(gtemp);

    /* check that lo <= hi */
    if (lo > hi)
    {
        PR(("lo %e > hi %e\n", lo, hi));
        ERROR;
    }

    /* check feasibility */
    if (a == NULL)
    {
        // a is implicitly all 1's
        s = (double)n;
        t = 0.;
    }
    else
    {
        s = 0.;
        t = 0.;
        for (j = 0; j < n; j++)
        {
            if (a[j] > 0)
                s += a[j];
            else
                t += a[j];
        }
    }

    if (s < lo)
    {
        PR(("lo %e > largest possible value %e\n", lo, s));
        ERROR;
    }
    if (t > hi)
    {
        PR(("hi %e < smallest possible value %e\n", hi, t));
        ERROR;
    }

    Int *ix = FreeSet_status;

    /* check that nFreeSet = number of zeroes in ix, and ix agrees with x */
    i = 0;
    for (j = 0; j < n; j++)
    {
        if (ix[j] > 1)
        {
            PR(("ix [%ld] = %ld (> 1)\n", j, ix[j]));
            ERROR;
        }
        else if (ix[j] < -1)
        {
            PR(("ix [%ld] = %ld (< -1)\n", j, ix[j]));
            ERROR;
        }
        else if (ix[j] == 0)
            i++;
        k = 0;
        if (ix[j] == 1)
        {
            if (x[j] != 1.)
                k = 1;
        }
        else if (ix[j] == -1)
        {
            if (x[j] != 0.)
                k = 1;
        }
        if (k)
        {
            PR(("ix [%ld] = %ld while x = %e\n", j, ix[j], x[j]));
            ERROR;
        }
        if ((x[j] > 1 + tol) || (x[j] < -tol))
        {
            PR(("x [%ld] = %e outside range [0, 1]", j, x[j]));
            ERROR;
        }
    }

    PR(("i %ld nFreeSet %ld\n", i, nFreeSet));

    if (i != nFreeSet)
    {
        PR(("free indices in ix is %ld, nFreeSet = %ld\n", i, nFreeSet));
        ERROR;
    }

    /* check that FreeSet is valid */
    for (Int ifree = 0; ifree < nFreeSet; ifree++)
    {
        i = FreeSet_list[ifree];
        if ((i < 0) || (i > n))
        {
            PR(("FreeSet_list [%ld] = %ld, out of range [0, %ld]\n", ifree, i,
                n));
            ERROR;
        }
        if (w0[i] != 0)
        {
            PR(("FreeSet_list [%ld] = %ld, repeats\n", ifree, i));
            ERROR;
        }
        if (ix[i] != 0)
        {
            PR(("FreeSet_list [%ld] = %ld, is not free\n", ifree, i));
            ERROR;
        }
        w0[i] = 1;
    }

    for (Int ifree = 0; ifree < nFreeSet; ifree++)
    {
        i     = FreeSet_list[ifree];
        w0[i] = 0;
    }

    /* check that b is correct */

    s = 0.;
    if (a == NULL)
        for (j = 0; j < n; j++)
            s += x[j];
    else
        for (j = 0; j < n; j++)
            s += x[j] * a[j];
    // PR (("CHECK BOUNDS: lo %g s=a'x %g hi %g\n", lo, s, hi)) ;
    if (check_b)
    {
        if (fabs(b - s) > tol)
        {
            PR(("QP->b = %e while a'x = %e\n", b, s));
            ERROR;
        }
    }
    if (s < lo - tol)
    {
        PR(("a'x TOO LO: a'x = %e < lo = %e\n", s, lo));
        ERROR;
    }
    if (s > hi + tol)
    {
        PR(("a'x TOO HI: a'x = %e > hi = %e\n", s, hi));
        ERROR;
    }

    /* check that grad is correct */

    for (j = 0; j < n; j++)
        gtemp[j] = (.5 - x[j]) * D[j];
    double newcost = 0.;
    if (Ex == NULL)
    {
        for (j = 0; j < n; j++)
        {
            s = .5 - x[j];
            t = 0.;
            for (k = Ep[j]; k < Ep[j + 1]; k++)
            {
                gtemp[Ei[k]] += s;
                t += x[Ei[k]];
            }
            newcost += (t + x[j] * D[j]) * (1. - x[j]);
        }
    }
    else
    {
        for (j = 0; j < n; j++)
        {
            s = .5 - x[j];
            t = 0.;
            for (k = Ep[j]; k < Ep[j + 1]; k++)
            {
                /* Ex is not NULL */
                gtemp[Ei[k]] += s * Ex[k];
                t += Ex[k] * x[Ei[k]];
            }
            newcost += (t + x[j] * D[j]) * (1. - x[j]);
        }
    }
    s = 0.;
    for (j = 0; j < n; j++)
        s = std::max(s, fabs(gtemp[j] - grad[j]));
    if (s > tol)
    {
        PR(("error (%e) in grad: current grad, true grad, x:\n", s));
        for (j = 0; j < n; j++)
        {
            double ack = fabs(gtemp[j] - grad[j]);
            PR(("j: %5ld grad: %15.6e gtemp: %15.6e err: %15.6e "
                "x: %15.6e",
                j, grad[j], gtemp[j], ack, x[j]));
            if (ack > tol)
                PR((" ACK!"));
            PR(("\n"));
        }
        PR(("tol = %g\n", tol));
        ERROR;
    }

    /* check that cost decreases */

    if (newcost / QP->check_cost > (1 + tol))
    {
        PR(("cost increases, old %30.15e new %30.15e\n", QP->check_cost,
            newcost));
        ERROR;
    }
    QP->check_cost = newcost;
    PR(("cost: %30.15e\n", newcost));

    free(gtemp); // ]
    free(w0);    // ]
}

//------------------------------------------------------------------------------
// FreeSet_dump
//------------------------------------------------------------------------------

void FreeSet_dump(const char *where, Int n, Int *FreeSet_list, Int nFreeSet,
                  Int *FreeSet_status, Int verbose, double *x)
{
    Int death = 0;

    if (verbose)
    {
        PR(("\ndump FreeSet (%s): nFreeSet %ld n %ld jfirst %ld\n", where,
            nFreeSet, n, (nFreeSet == 0) ? -1 : FreeSet_list[0]));
    }

    for (Int kfree = 0; kfree < nFreeSet; kfree++)
    {
        Int k = FreeSet_list[kfree];
        if (verbose)
        {
            PR(("    k %ld \n", k));
            ASSERT(k >= 0 && k < n);
            PR((" FreeSet_status %ld\n", FreeSet_status[k]));
            ASSERT(FreeSet_status[k] == 0);
            if (x != NULL)
            {
                PR((" x: %g\n", x[k]));
            }
            PR(("\n"));
        }
    }

    Int nFree2 = 0;
    Int nHi    = 0;
    Int nLo    = 0;
    for (Int j = 0; j < n; j++)
    {
        FFLUSH;
        if (FreeSet_status[j] == 0)
        {
            // note that in rare cases, x[j] can be 0 or 1 yet
            // still be in the FreeSet.
            if (x != NULL)
                ASSERT(-1E-8 <= x[j] && x[j] <= 1. + 1E-8);
            nFree2++;
        }
        else if (FreeSet_status[j] == 1)
        {
            if (x != NULL)
                ASSERT(x[j] >= 1.);
            nHi++;
        }
        else if (FreeSet_status[j] == -1)
        {
            if (x != NULL)
                ASSERT(x[j] <= 0.);
            nLo++;
        }
        else
        {
            ASSERT(0);
        }
    }
    if (verbose)
    {
        PR(("    # that have FreeSet_status of zero: %ld\n", nFree2));
        PR(("    # that have FreeSet_status of one:  %ld\n", nHi));
        PR(("    # that have FreeSet_status of -1    %ld\n", nLo));
    }
    if (nFreeSet != nFree2)
    {
        PR(("ERROR nFree2 (%ld) nFreeSet %ld\n", nFree2, nFreeSet));
        ASSERT(0);
    }
    PR(("bye\n"));
}
#endif

} // end namespace Mongoose