File: CbcFixVariable.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 (196 lines) | stat: -rw-r--r-- 6,039 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
// $Id: CbcFixVariable.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/10/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 "CbcFixVariable.hpp"
#include "CbcBranchActual.hpp"
#include "CoinSort.hpp"
#include "CoinError.hpp"


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

// Default Constructor
CbcFixVariable::CbcFixVariable ()
        : CbcConsequence(),
        numberStates_(0),
        states_(NULL),
        startLower_(NULL),
        startUpper_(NULL),
        newBound_(NULL),
        variable_(NULL)
{
}

// One useful Constructor
CbcFixVariable::CbcFixVariable (int numberStates, const int * states, const int * numberNewLower,
                                const int ** newLowerValue,
                                const int ** lowerColumn,
                                const int * numberNewUpper, const int ** newUpperValue,
                                const int ** upperColumn)
        : CbcConsequence(),
        states_(NULL),
        startLower_(NULL),
        startUpper_(NULL),
        newBound_(NULL),
        variable_(NULL)
{
    // How much space
    numberStates_ = numberStates;
    if (numberStates_) {
        states_ = new int[numberStates_];
        memcpy(states_, states, numberStates_*sizeof(int));
        int i;
        int n = 0;
        startLower_ = new int[numberStates_+1];
        startUpper_ = new int[numberStates_+1];
        startLower_[0] = 0;
        //count
        for (i = 0; i < numberStates_; i++) {
            n += numberNewLower[i];
            startUpper_[i] = n;
            n += numberNewUpper[i];
            startLower_[i+1] = n;
        }
        newBound_ = new double [n];
        variable_ = new int [n];
        n = 0;
        for (i = 0; i < numberStates_; i++) {
            int j;
            int k;
            const int * bound;
            const int * variable;
            k = numberNewLower[i];
            bound = newLowerValue[i];
            variable = lowerColumn[i];
            for (j = 0; j < k; j++) {
                newBound_[n] = bound[j];
                variable_[n++] = variable[j];
            }
            k = numberNewUpper[i];
            bound = newUpperValue[i];
            variable = upperColumn[i];
            for (j = 0; j < k; j++) {
                newBound_[n] = bound[j];
                variable_[n++] = variable[j];
            }
        }
    }
}

// Copy constructor
CbcFixVariable::CbcFixVariable ( const CbcFixVariable & rhs)
        : CbcConsequence(rhs)
{
    numberStates_ = rhs.numberStates_;
    states_ = NULL;
    startLower_ = NULL;
    startUpper_ = NULL;
    newBound_ = NULL;
    variable_ = NULL;
    if (numberStates_) {
        states_ = CoinCopyOfArray(rhs.states_, numberStates_);
        startLower_ = CoinCopyOfArray(rhs.startLower_, numberStates_ + 1);
        startUpper_ = CoinCopyOfArray(rhs.startUpper_, numberStates_ + 1);
        int n = startLower_[numberStates_];
        newBound_ = CoinCopyOfArray(rhs.newBound_, n);
        variable_ = CoinCopyOfArray(rhs.variable_, n);
    }
}

// Clone
CbcConsequence *
CbcFixVariable::clone() const
{
    return new CbcFixVariable(*this);
}

// Assignment operator
CbcFixVariable &
CbcFixVariable::operator=( const CbcFixVariable & rhs)
{
    if (this != &rhs) {
        CbcConsequence::operator=(rhs);
        delete [] states_;
        delete [] startLower_;
        delete [] startUpper_;
        delete [] newBound_;
        delete [] variable_;
        states_ = NULL;
        startLower_ = NULL;
        startUpper_ = NULL;
        newBound_ = NULL;
        variable_ = NULL;
        numberStates_ = rhs.numberStates_;
        if (numberStates_) {
            states_ = CoinCopyOfArray(rhs.states_, numberStates_);
            startLower_ = CoinCopyOfArray(rhs.startLower_, numberStates_ + 1);
            startUpper_ = CoinCopyOfArray(rhs.startUpper_, numberStates_ + 1);
            int n = startLower_[numberStates_];
            newBound_ = CoinCopyOfArray(rhs.newBound_, n);
            variable_ = CoinCopyOfArray(rhs.variable_, n);
        }
    }
    return *this;
}

// Destructor
CbcFixVariable::~CbcFixVariable ()
{
    delete [] states_;
    delete [] startLower_;
    delete [] startUpper_;
    delete [] newBound_;
    delete [] variable_;
}
// Set up a startLower for a single member
void
CbcFixVariable::applyToSolver(OsiSolverInterface * solver, int state) const
{
    assert (state == -9999 || state == 9999);
    // Find state
    int find;
    for (find = 0; find < numberStates_; find++)
        if (states_[find] == state)
            break;
    if (find == numberStates_)
        return;
    int i;
    // Set new lower bounds
    for (i = startLower_[find]; i < startUpper_[find]; i++) {
        int iColumn = variable_[i];
        double value = newBound_[i];
        double oldValue = solver->getColLower()[iColumn];
        //printf("for %d old lower bound %g, new %g",iColumn,oldValue,value);
        solver->setColLower(iColumn, CoinMax(value, oldValue));
        //printf(" => %g\n",solver->getColLower()[iColumn]);
    }
    // Set new upper bounds
    for (i = startUpper_[find]; i < startLower_[find+1]; i++) {
        int iColumn = variable_[i];
        double value = newBound_[i];
        double oldValue = solver->getColUpper()[iColumn];
        //printf("for %d old upper bound %g, new %g",iColumn,oldValue,value);
        solver->setColUpper(iColumn, CoinMin(value, oldValue));
        //printf(" => %g\n",solver->getColUpper()[iColumn]);
    }
}