File: CbcNWay.cpp

package info (click to toggle)
coinor-cbc 2.9.9+repack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (425 lines) | stat: -rw-r--r-- 12,579 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
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
// $Id: CbcNWay.cpp 1899 2013-04-09 18:12:08Z stefan $
// Copyright (C) 2002, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

// Edwin 11/9/2009-- carved out of CbcBranchActual

#if defined(_MSC_VER)
// Turn off compiler warning about long names
#  pragma warning(disable:4786)
#endif
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <cfloat>
//#define CBC_DEBUG

#include "CoinTypes.hpp"
#include "OsiSolverInterface.hpp"
#include "OsiSolverBranch.hpp"
#include "CbcModel.hpp"
#include "CbcMessage.hpp"
#include "CbcNWay.hpp"
#include "CbcBranchActual.hpp"
#include "CoinSort.hpp"
#include "CoinError.hpp"

//##############################################################################

// Default Constructor
CbcNWay::CbcNWay ()
        : CbcObject(),
        numberMembers_(0),
        members_(NULL),
        consequence_(NULL)
{
}

// Useful constructor (which are integer indices)
CbcNWay::CbcNWay (CbcModel * model, int numberMembers,
                  const int * which, int identifier)
        : CbcObject(model)
{
    id_ = identifier;
    numberMembers_ = numberMembers;
    if (numberMembers_) {
        members_ = new int[numberMembers_];
        memcpy(members_, which, numberMembers_*sizeof(int));
    } else {
        members_ = NULL;
    }
    consequence_ = NULL;
}

// Copy constructor
CbcNWay::CbcNWay ( const CbcNWay & rhs)
        : CbcObject(rhs)
{
    numberMembers_ = rhs.numberMembers_;
    consequence_ = NULL;
    if (numberMembers_) {
        members_ = new int[numberMembers_];
        memcpy(members_, rhs.members_, numberMembers_*sizeof(int));
        if (rhs.consequence_) {
            consequence_ = new CbcConsequence * [numberMembers_];
            for (int i = 0; i < numberMembers_; i++) {
                if (rhs.consequence_[i])
                    consequence_[i] = rhs.consequence_[i]->clone();
                else
                    consequence_[i] = NULL;
            }
        }
    } else {
        members_ = NULL;
    }
}

// Clone
CbcObject *
CbcNWay::clone() const
{
    return new CbcNWay(*this);
}

// Assignment operator
CbcNWay &
CbcNWay::operator=( const CbcNWay & rhs)
{
    if (this != &rhs) {
        CbcObject::operator=(rhs);
        delete [] members_;
        numberMembers_ = rhs.numberMembers_;
        if (consequence_) {
            for (int i = 0; i < numberMembers_; i++)
                delete consequence_[i];
            delete [] consequence_;
            consequence_ = NULL;
        }
        if (numberMembers_) {
            members_ = new int[numberMembers_];
            memcpy(members_, rhs.members_, numberMembers_*sizeof(int));
        } else {
            members_ = NULL;
        }
        if (rhs.consequence_) {
            consequence_ = new CbcConsequence * [numberMembers_];
            for (int i = 0; i < numberMembers_; i++) {
                if (rhs.consequence_[i])
                    consequence_[i] = rhs.consequence_[i]->clone();
                else
                    consequence_[i] = NULL;
            }
        }
    }
    return *this;
}

// Destructor
CbcNWay::~CbcNWay ()
{
    delete [] members_;
    if (consequence_) {
        for (int i = 0; i < numberMembers_; i++)
            delete consequence_[i];
        delete [] consequence_;
    }
}
// Set up a consequence for a single member
void
CbcNWay::setConsequence(int iColumn, const CbcConsequence & consequence)
{
    if (!consequence_) {
        consequence_ = new CbcConsequence * [numberMembers_];
        for (int i = 0; i < numberMembers_; i++)
            consequence_[i] = NULL;
    }
    for (int i = 0; i < numberMembers_; i++) {
        if (members_[i] == iColumn) {
            consequence_[i] = consequence.clone();
            break;
        }
    }
}

// Applies a consequence for a single member
void
CbcNWay::applyConsequence(int iSequence, int state) const
{
    assert (state == -9999 || state == 9999);
    if (consequence_) {
        CbcConsequence * consequence = consequence_[iSequence];
        if (consequence)
            consequence->applyToSolver(model_->solver(), state);
    }
}
double
CbcNWay::infeasibility(const OsiBranchingInformation * /*info*/,
                       int &preferredWay) const
{
    int numberUnsatis = 0;
    int j;
    OsiSolverInterface * solver = model_->solver();
    const double * solution = model_->testSolution();
    const double * lower = solver->getColLower();
    const double * upper = solver->getColUpper();
    double largestValue = 0.0;

    double integerTolerance =
        model_->getDblParam(CbcModel::CbcIntegerTolerance);

    for (j = 0; j < numberMembers_; j++) {
        int iColumn = members_[j];
        double value = solution[iColumn];
        value = CoinMax(value, lower[iColumn]);
        value = CoinMin(value, upper[iColumn]);
        double distance = CoinMin(value - lower[iColumn], upper[iColumn] - value);
        if (distance > integerTolerance) {
            numberUnsatis++;
            largestValue = CoinMax(distance, largestValue);
        }
    }
    preferredWay = 1;
    if (numberUnsatis) {
        return largestValue;
    } else {
        return 0.0; // satisfied
    }
}

// This looks at solution and sets bounds to contain solution
void
CbcNWay::feasibleRegion()
{
    int j;
    OsiSolverInterface * solver = model_->solver();
    const double * solution = model_->testSolution();
    const double * lower = solver->getColLower();
    const double * upper = solver->getColUpper();
    double integerTolerance =
        model_->getDblParam(CbcModel::CbcIntegerTolerance);
    for (j = 0; j < numberMembers_; j++) {
        int iColumn = members_[j];
        double value = solution[iColumn];
        value = CoinMax(value, lower[iColumn]);
        value = CoinMin(value, upper[iColumn]);
        if (value >= upper[iColumn] - integerTolerance) {
            solver->setColLower(iColumn, upper[iColumn]);
        } else {
            assert (value <= lower[iColumn] + integerTolerance);
            solver->setColUpper(iColumn, lower[iColumn]);
        }
    }
}
// Redoes data when sequence numbers change
void
CbcNWay::redoSequenceEtc(CbcModel * model, int numberColumns, const int * originalColumns)
{
    model_ = model;
    int n2 = 0;
    for (int j = 0; j < numberMembers_; j++) {
        int iColumn = members_[j];
        int i;
        for (i = 0; i < numberColumns; i++) {
            if (originalColumns[i] == iColumn)
                break;
        }
        if (i < numberColumns) {
            members_[n2] = i;
            consequence_[n2++] = consequence_[j];
        } else {
            delete consequence_[j];
        }
    }
    if (n2 < numberMembers_) {
        printf("** NWay number of members reduced from %d to %d!\n", numberMembers_, n2);
        numberMembers_ = n2;
    }
}
CbcBranchingObject *
CbcNWay::createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * /*info*/, int /*way*/)
{
    int numberFree = 0;
    int j;

    //OsiSolverInterface * solver = model_->solver();
    const double * solution = model_->testSolution();
    const double * lower = solver->getColLower();
    const double * upper = solver->getColUpper();
    int * list = new int[numberMembers_];
    double * sort = new double[numberMembers_];

    for (j = 0; j < numberMembers_; j++) {
        int iColumn = members_[j];
        double value = solution[iColumn];
        value = CoinMax(value, lower[iColumn]);
        value = CoinMin(value, upper[iColumn]);
        if (upper[iColumn] > lower[iColumn]) {
            double distance = upper[iColumn] - value;
            list[numberFree] = j;
            sort[numberFree++] = distance;
        }
    }
    assert (numberFree);
    // sort
    CoinSort_2(sort, sort + numberFree, list);
    // create object
    CbcBranchingObject * branch;
    branch = new CbcNWayBranchingObject(model_, this, numberFree, list);
    branch->setOriginalObject(this);
    delete [] list;
    delete [] sort;
    return branch;
}

// Default Constructor
CbcNWayBranchingObject::CbcNWayBranchingObject()
        : CbcBranchingObject()
{
    order_ = NULL;
    object_ = NULL;
    numberInSet_ = 0;
    way_ = 0;
}

// Useful constructor
CbcNWayBranchingObject::CbcNWayBranchingObject (CbcModel * model,
        const CbcNWay * nway,
        int number, const int * order)
        : CbcBranchingObject(model, nway->id(), -1, 0.5)
{
    numberBranches_ = number;
    order_ = new int [number];
    object_ = nway;
    numberInSet_ = number;
    memcpy(order_, order, number*sizeof(int));
}

// Copy constructor
CbcNWayBranchingObject::CbcNWayBranchingObject ( const CbcNWayBranchingObject & rhs) : CbcBranchingObject(rhs)
{
    numberInSet_ = rhs.numberInSet_;
    object_ = rhs.object_;
    if (numberInSet_) {
        order_ = new int [numberInSet_];
        memcpy(order_, rhs.order_, numberInSet_*sizeof(int));
    } else {
        order_ = NULL;
    }
}

// Assignment operator
CbcNWayBranchingObject &
CbcNWayBranchingObject::operator=( const CbcNWayBranchingObject & rhs)
{
    if (this != &rhs) {
        CbcBranchingObject::operator=(rhs);
        object_ = rhs.object_;
        delete [] order_;
        numberInSet_ = rhs.numberInSet_;
        if (numberInSet_) {
            order_ = new int [numberInSet_];
            memcpy(order_, rhs.order_, numberInSet_*sizeof(int));
        } else {
            order_ = NULL;
        }
    }
    return *this;
}
CbcBranchingObject *
CbcNWayBranchingObject::clone() const
{
    return (new CbcNWayBranchingObject(*this));
}


// Destructor
CbcNWayBranchingObject::~CbcNWayBranchingObject ()
{
    delete [] order_;
}
double
CbcNWayBranchingObject::branch()
{
    int which = branchIndex_;
    branchIndex_++;
    assert (numberBranchesLeft() >= 0);
    if (which == 0) {
        // first branch so way_ may mean something
        assert (way_ == -1 || way_ == 1);
        if (way_ == -1)
            which++;
    } else if (which == 1) {
        // second branch so way_ may mean something
        assert (way_ == -1 || way_ == 1);
        if (way_ == -1)
            which--;
        // switch way off
        way_ = 0;
    }
    const double * lower = model_->solver()->getColLower();
    const double * upper = model_->solver()->getColUpper();
    const int * members = object_->members();
    for (int j = 0; j < numberInSet_; j++) {
        int iSequence = order_[j];
        int iColumn = members[iSequence];
        if (j != which) {
            model_->solver()->setColUpper(iColumn, lower[iColumn]);
            //model_->solver()->setColLower(iColumn,lower[iColumn]);
            assert (lower[iColumn] > -1.0e20);
            // apply any consequences
            object_->applyConsequence(iSequence, -9999);
        } else {
            model_->solver()->setColLower(iColumn, upper[iColumn]);
            //model_->solver()->setColUpper(iColumn,upper[iColumn]);
#ifdef FULL_PRINT
            printf("Up Fix %d to %g\n", iColumn, upper[iColumn]);
#endif
            assert (upper[iColumn] < 1.0e20);
            // apply any consequences
            object_->applyConsequence(iSequence, 9999);
        }
    }
    return 0.0;
}
void
CbcNWayBranchingObject::print()
{
    printf("NWay - Up Fix ");
    const int * members = object_->members();
    for (int j = 0; j < way_; j++) {
        int iColumn = members[order_[j]];
        printf("%d ", iColumn);
    }
    printf("\n");
}

/** Compare the original object of \c this with the original object of \c
    brObj. Assumes that there is an ordering of the original objects.
    This method should be invoked only if \c this and brObj are of the same
    type.
    Return negative/0/positive depending on whether \c this is
    smaller/same/larger than the argument.
*/
int
CbcNWayBranchingObject::compareOriginalObject
(const CbcBranchingObject* /*brObj*/) const
{
    throw("must implement");
}

/** Compare the \c this with \c brObj. \c this and \c brObj must be os the
    same type and must have the same original object, but they may have
    different feasible regions.
    Return the appropriate CbcRangeCompare value (first argument being the
    sub/superset if that's the case). In case of overlap (and if \c
    replaceIfOverlap is true) replace the current branching object with one
    whose feasible region is the overlap.
*/
CbcRangeCompare
CbcNWayBranchingObject::compareBranchingObject
(const CbcBranchingObject* /*brObj*/, const bool /*replaceIfOverlap*/)
{
    throw("must implement");
}