File: CbcPartialNodeInfo.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 (286 lines) | stat: -rw-r--r-- 9,361 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
// $Id: CbcPartialNodeInfo.cpp 1951 2013-08-02 14:26:23Z forrest $
// 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/24/09 carved from CbcNode

#include "CbcConfig.h"

#include <string>
//#define CBC_DEBUG 1
//#define CHECK_CUT_COUNTS
//#define CHECK_NODE
//#define CBC_CHECK_BASIS
#include <cassert>
#include <cfloat>
#define CUTS
#include "CoinPragma.hpp"
#include "OsiSolverInterface.hpp"
#include "OsiChooseVariable.hpp"
#include "OsiAuxInfo.hpp"
#include "OsiSolverBranch.hpp"
#include "CoinWarmStartBasis.hpp"
#include "CoinTime.hpp"
#include "CbcModel.hpp"
#include "CbcNode.hpp"
#include "CbcStatistics.hpp"
#include "CbcStrategy.hpp"
#include "CbcBranchActual.hpp"
#include "CbcBranchDynamic.hpp"
#include "OsiRowCut.hpp"
#include "OsiRowCutDebugger.hpp"
#include "OsiCuts.hpp"
#include "CbcCountRowCut.hpp"
#include "CbcFeasibilityBase.hpp"
#include "CbcMessage.hpp"
#ifdef COIN_HAS_CLP
#include "OsiClpSolverInterface.hpp"
#include "ClpSimplexOther.hpp"
#endif
using namespace std;
#include "CglCutGenerator.hpp"

// Default constructor
CbcPartialNodeInfo::CbcPartialNodeInfo()

        : CbcNodeInfo(),
        basisDiff_(NULL),
        variables_(NULL),
        newBounds_(NULL),
        numberChangedBounds_(0)

{ /* this space intentionally left blank */ }

// Constructor from current state
CbcPartialNodeInfo::CbcPartialNodeInfo (CbcNodeInfo *parent, CbcNode *owner,
                                        int numberChangedBounds,
                                        const int *variables,
                                        const double *boundChanges,
                                        const CoinWarmStartDiff *basisDiff)
        : CbcNodeInfo(parent, owner)
{
    basisDiff_ = basisDiff->clone() ;
#ifdef CBC_CHECK_BASIS
    std::cout << "Constructor (" << this << ") " << std::endl ;
#endif

    numberChangedBounds_ = numberChangedBounds;
    size_t size = numberChangedBounds_ * (sizeof(double) + sizeof(int));
    char * temp = new char [size];
    newBounds_ = reinterpret_cast<double *> (temp);
    variables_ = reinterpret_cast<int *> (newBounds_ + numberChangedBounds_);

    int i ;
    for (i = 0; i < numberChangedBounds_; i++) {
        variables_[i] = variables[i];
        newBounds_[i] = boundChanges[i];
    }
}

CbcPartialNodeInfo::CbcPartialNodeInfo (const CbcPartialNodeInfo & rhs)

        : CbcNodeInfo(rhs)

{
    basisDiff_ = rhs.basisDiff_->clone() ;

#ifdef CBC_CHECK_BASIS
    std::cout << "Copy constructor (" << this << ") from " << this << std::endl ;
#endif
    numberChangedBounds_ = rhs.numberChangedBounds_;
    size_t size = numberChangedBounds_ * (sizeof(double) + sizeof(int));
    char * temp = new char [size];
    newBounds_ = reinterpret_cast<double *> (temp);
    variables_ = reinterpret_cast<int *> (newBounds_ + numberChangedBounds_);

    int i ;
    for (i = 0; i < numberChangedBounds_; i++) {
        variables_[i] = rhs.variables_[i];
        newBounds_[i] = rhs.newBounds_[i];
    }
}

CbcNodeInfo *
CbcPartialNodeInfo::clone() const
{
    return (new CbcPartialNodeInfo(*this));
}


CbcPartialNodeInfo::~CbcPartialNodeInfo ()
{
    delete basisDiff_ ;
    delete [] newBounds_;
}


/**
   The basis supplied as a parameter is incrementally modified, and lower and
   upper bounds on variables in the model are incrementally modified. Any
   cuts associated with this node are added to the list in addCuts.
*/

void CbcPartialNodeInfo::applyToModel (CbcModel *model,
                                       CoinWarmStartBasis *&basis,
                                       CbcCountRowCut **addCuts,
                                       int &currentNumberCuts) const

{
    OsiSolverInterface *solver = model->solver();
    if ((active_&4) != 0 && basis) {
        basis->applyDiff(basisDiff_) ;
#ifdef CBC_CHECK_BASIS
        std::cout << "Basis (after applying " << this << ") " << std::endl ;
        basis->print() ;
#endif
    }

    // branch - do bounds
    int i;
    if ((active_&1) != 0) {
        for (i = 0; i < numberChangedBounds_; i++) {
            int variable = variables_[i];
            int k = variable & 0x3fffffff;
            if ((variable&0x80000000) == 0) {
                // lower bound changing
	      //#define CBC_PRINT2
#ifdef CBC_PRINT2
                if (solver->getColLower()[k] != newBounds_[i])
                    printf("lower change for column %d - from %g to %g\n", k, solver->getColLower()[k], newBounds_[i]);
#endif
#ifndef NDEBUG
                if ((variable&0x40000000) == 0 && false) {
                    double oldValue = solver->getColLower()[k];
                    assert (newBounds_[i] > oldValue - 1.0e-8);
                    if (newBounds_[i] < oldValue + 1.0e-8)
                        printf("bad null lower change for column %d - bound %g\n", k, oldValue);
                }
#endif
                solver->setColLower(k, newBounds_[i]);
            } else {
                // upper bound changing
#ifdef CBC_PRINT2
                if (solver->getColUpper()[k] != newBounds_[i])
                    printf("upper change for column %d - from %g to %g\n", k, solver->getColUpper()[k], newBounds_[i]);
#endif
#ifndef NDEBUG
                if ((variable&0x40000000) == 0 && false) {
                    double oldValue = solver->getColUpper()[k];
                    assert (newBounds_[i] < oldValue + 1.0e-8);
                    if (newBounds_[i] > oldValue - 1.0e-8)
                        printf("bad null upper change for column %d - bound %g\n", k, oldValue);
                }
#endif
                solver->setColUpper(k, newBounds_[i]);
            }
        }
    }
    if ((active_&2) != 0) {
        for (i = 0; i < numberCuts_; i++) {
            addCuts[currentNumberCuts+i] = cuts_[i];
            if (cuts_[i] && model->messageHandler()->logLevel() > 4) {
                cuts_[i]->print();
            }
        }

        currentNumberCuts += numberCuts_;
    }
    return ;
}
// Just apply bounds to one variable (1=>infeasible)
int
CbcPartialNodeInfo::applyBounds(int iColumn, double & lower, double & upper, int force)
{
    // branch - do bounds
    int i;
    int found = 0;
    double newLower = -COIN_DBL_MAX;
    double newUpper = COIN_DBL_MAX;
    for (i = 0; i < numberChangedBounds_; i++) {
        int variable = variables_[i];
        int k = variable & 0x3fffffff;
        if (k == iColumn) {
            if ((variable&0x80000000) == 0) {
                // lower bound changing
                found |= 1;
                newLower = CoinMax(newLower, newBounds_[i]);
                if ((force&1) == 0) {
                    if (lower > newBounds_[i])
		      COIN_DETAIL_PRINT(printf("%d odd lower going from %g to %g\n", iColumn, lower, newBounds_[i]));
                    lower = newBounds_[i];
                } else {
                    newBounds_[i] = lower;
                    variables_[i] |= 0x40000000; // say can go odd way
                }
            } else {
                // upper bound changing
                found |= 2;
                newUpper = CoinMin(newUpper, newBounds_[i]);
                if ((force&2) == 0) {
                    if (upper < newBounds_[i])
		      COIN_DETAIL_PRINT(printf("%d odd upper going from %g to %g\n", iColumn, upper, newBounds_[i]));
                    upper = newBounds_[i];
                } else {
                    newBounds_[i] = upper;
                    variables_[i] |= 0x40000000; // say can go odd way
                }
            }
        }
    }
    newLower = CoinMax(newLower, lower);
    newUpper = CoinMin(newUpper, upper);
    int nAdd = 0;
    if ((force&2) != 0 && (found&2) == 0) {
        // need to add new upper
        nAdd++;
    }
    if ((force&1) != 0 && (found&1) == 0) {
        // need to add new lower
        nAdd++;
    }
    if (nAdd) {
        size_t size = (numberChangedBounds_ + nAdd) * (sizeof(double) + sizeof(int));
        char * temp = new char [size];
        double * newBounds = reinterpret_cast<double *> (temp);
        int * variables = reinterpret_cast<int *> (newBounds + numberChangedBounds_ + nAdd);

        int i ;
        for (i = 0; i < numberChangedBounds_; i++) {
            variables[i] = variables_[i];
            newBounds[i] = newBounds_[i];
        }
        delete [] newBounds_;
        newBounds_ = newBounds;
        variables_ = variables;
        if ((force&2) != 0 && (found&2) == 0) {
            // need to add new upper
            int variable = iColumn | 0x80000000;
            variables_[numberChangedBounds_] = variable;
            newBounds_[numberChangedBounds_++] = newUpper;
        }
        if ((force&1) != 0 && (found&1) == 0) {
            // need to add new lower
            int variable = iColumn;
            variables_[numberChangedBounds_] = variable;
            newBounds_[numberChangedBounds_++] = newLower;
        }
    }

    return (newUpper >= newLower) ? 0 : 1;
}

/* Builds up row basis backwards (until original model).
   Returns NULL or previous one to apply .
   Depends on Free being 0 and impossible for cuts
*/

CbcNodeInfo *
CbcPartialNodeInfo::buildRowBasis(CoinWarmStartBasis & basis ) const

{
    basis.applyDiff(basisDiff_) ;

    return parent_ ;
}