[1573] | 1 | // $Id: CbcFixVariable.cpp 1899 2013-04-09 18:12:08Z stefan $ |
---|
| 2 | // Copyright (C) 2002, International Business Machines |
---|
| 3 | // Corporation and others. All Rights Reserved. |
---|
| 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
---|
| 5 | |
---|
[1357] | 6 | // Edwin 11/10/2009-- carved out of CbcBranchActual |
---|
[1573] | 7 | |
---|
[1357] | 8 | #if defined(_MSC_VER) |
---|
| 9 | // Turn off compiler warning about long names |
---|
| 10 | # pragma warning(disable:4786) |
---|
| 11 | #endif |
---|
| 12 | #include <cassert> |
---|
| 13 | #include <cstdlib> |
---|
| 14 | #include <cmath> |
---|
| 15 | #include <cfloat> |
---|
| 16 | //#define CBC_DEBUG |
---|
| 17 | |
---|
| 18 | #include "CoinTypes.hpp" |
---|
| 19 | #include "OsiSolverInterface.hpp" |
---|
| 20 | #include "OsiSolverBranch.hpp" |
---|
| 21 | #include "CbcModel.hpp" |
---|
| 22 | #include "CbcMessage.hpp" |
---|
| 23 | #include "CbcFixVariable.hpp" |
---|
| 24 | #include "CbcBranchActual.hpp" |
---|
| 25 | #include "CoinSort.hpp" |
---|
| 26 | #include "CoinError.hpp" |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | //############################################################################## |
---|
| 30 | |
---|
| 31 | // Default Constructor |
---|
| 32 | CbcFixVariable::CbcFixVariable () |
---|
| 33 | : CbcConsequence(), |
---|
| 34 | numberStates_(0), |
---|
| 35 | states_(NULL), |
---|
| 36 | startLower_(NULL), |
---|
| 37 | startUpper_(NULL), |
---|
| 38 | newBound_(NULL), |
---|
| 39 | variable_(NULL) |
---|
| 40 | { |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // One useful Constructor |
---|
| 44 | CbcFixVariable::CbcFixVariable (int numberStates, const int * states, const int * numberNewLower, |
---|
| 45 | const int ** newLowerValue, |
---|
| 46 | const int ** lowerColumn, |
---|
| 47 | const int * numberNewUpper, const int ** newUpperValue, |
---|
| 48 | const int ** upperColumn) |
---|
| 49 | : CbcConsequence(), |
---|
| 50 | states_(NULL), |
---|
| 51 | startLower_(NULL), |
---|
| 52 | startUpper_(NULL), |
---|
| 53 | newBound_(NULL), |
---|
| 54 | variable_(NULL) |
---|
| 55 | { |
---|
| 56 | // How much space |
---|
| 57 | numberStates_ = numberStates; |
---|
| 58 | if (numberStates_) { |
---|
| 59 | states_ = new int[numberStates_]; |
---|
| 60 | memcpy(states_, states, numberStates_*sizeof(int)); |
---|
| 61 | int i; |
---|
| 62 | int n = 0; |
---|
| 63 | startLower_ = new int[numberStates_+1]; |
---|
| 64 | startUpper_ = new int[numberStates_+1]; |
---|
| 65 | startLower_[0] = 0; |
---|
| 66 | //count |
---|
| 67 | for (i = 0; i < numberStates_; i++) { |
---|
| 68 | n += numberNewLower[i]; |
---|
| 69 | startUpper_[i] = n; |
---|
| 70 | n += numberNewUpper[i]; |
---|
| 71 | startLower_[i+1] = n; |
---|
| 72 | } |
---|
| 73 | newBound_ = new double [n]; |
---|
| 74 | variable_ = new int [n]; |
---|
| 75 | n = 0; |
---|
| 76 | for (i = 0; i < numberStates_; i++) { |
---|
| 77 | int j; |
---|
| 78 | int k; |
---|
| 79 | const int * bound; |
---|
| 80 | const int * variable; |
---|
| 81 | k = numberNewLower[i]; |
---|
| 82 | bound = newLowerValue[i]; |
---|
| 83 | variable = lowerColumn[i]; |
---|
| 84 | for (j = 0; j < k; j++) { |
---|
| 85 | newBound_[n] = bound[j]; |
---|
| 86 | variable_[n++] = variable[j]; |
---|
| 87 | } |
---|
| 88 | k = numberNewUpper[i]; |
---|
| 89 | bound = newUpperValue[i]; |
---|
| 90 | variable = upperColumn[i]; |
---|
| 91 | for (j = 0; j < k; j++) { |
---|
| 92 | newBound_[n] = bound[j]; |
---|
| 93 | variable_[n++] = variable[j]; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | // Copy constructor |
---|
| 100 | CbcFixVariable::CbcFixVariable ( const CbcFixVariable & rhs) |
---|
| 101 | : CbcConsequence(rhs) |
---|
| 102 | { |
---|
| 103 | numberStates_ = rhs.numberStates_; |
---|
| 104 | states_ = NULL; |
---|
| 105 | startLower_ = NULL; |
---|
| 106 | startUpper_ = NULL; |
---|
| 107 | newBound_ = NULL; |
---|
| 108 | variable_ = NULL; |
---|
| 109 | if (numberStates_) { |
---|
| 110 | states_ = CoinCopyOfArray(rhs.states_, numberStates_); |
---|
| 111 | startLower_ = CoinCopyOfArray(rhs.startLower_, numberStates_ + 1); |
---|
| 112 | startUpper_ = CoinCopyOfArray(rhs.startUpper_, numberStates_ + 1); |
---|
| 113 | int n = startLower_[numberStates_]; |
---|
| 114 | newBound_ = CoinCopyOfArray(rhs.newBound_, n); |
---|
| 115 | variable_ = CoinCopyOfArray(rhs.variable_, n); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | // Clone |
---|
| 120 | CbcConsequence * |
---|
| 121 | CbcFixVariable::clone() const |
---|
| 122 | { |
---|
| 123 | return new CbcFixVariable(*this); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | // Assignment operator |
---|
| 127 | CbcFixVariable & |
---|
| 128 | CbcFixVariable::operator=( const CbcFixVariable & rhs) |
---|
| 129 | { |
---|
| 130 | if (this != &rhs) { |
---|
| 131 | CbcConsequence::operator=(rhs); |
---|
| 132 | delete [] states_; |
---|
| 133 | delete [] startLower_; |
---|
| 134 | delete [] startUpper_; |
---|
| 135 | delete [] newBound_; |
---|
| 136 | delete [] variable_; |
---|
| 137 | states_ = NULL; |
---|
| 138 | startLower_ = NULL; |
---|
| 139 | startUpper_ = NULL; |
---|
| 140 | newBound_ = NULL; |
---|
| 141 | variable_ = NULL; |
---|
| 142 | numberStates_ = rhs.numberStates_; |
---|
| 143 | if (numberStates_) { |
---|
| 144 | states_ = CoinCopyOfArray(rhs.states_, numberStates_); |
---|
| 145 | startLower_ = CoinCopyOfArray(rhs.startLower_, numberStates_ + 1); |
---|
| 146 | startUpper_ = CoinCopyOfArray(rhs.startUpper_, numberStates_ + 1); |
---|
| 147 | int n = startLower_[numberStates_]; |
---|
| 148 | newBound_ = CoinCopyOfArray(rhs.newBound_, n); |
---|
| 149 | variable_ = CoinCopyOfArray(rhs.variable_, n); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | return *this; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | // Destructor |
---|
| 156 | CbcFixVariable::~CbcFixVariable () |
---|
| 157 | { |
---|
| 158 | delete [] states_; |
---|
| 159 | delete [] startLower_; |
---|
| 160 | delete [] startUpper_; |
---|
| 161 | delete [] newBound_; |
---|
| 162 | delete [] variable_; |
---|
| 163 | } |
---|
| 164 | // Set up a startLower for a single member |
---|
| 165 | void |
---|
| 166 | CbcFixVariable::applyToSolver(OsiSolverInterface * solver, int state) const |
---|
| 167 | { |
---|
| 168 | assert (state == -9999 || state == 9999); |
---|
| 169 | // Find state |
---|
| 170 | int find; |
---|
| 171 | for (find = 0; find < numberStates_; find++) |
---|
| 172 | if (states_[find] == state) |
---|
| 173 | break; |
---|
| 174 | if (find == numberStates_) |
---|
| 175 | return; |
---|
| 176 | int i; |
---|
| 177 | // Set new lower bounds |
---|
| 178 | for (i = startLower_[find]; i < startUpper_[find]; i++) { |
---|
| 179 | int iColumn = variable_[i]; |
---|
| 180 | double value = newBound_[i]; |
---|
| 181 | double oldValue = solver->getColLower()[iColumn]; |
---|
| 182 | //printf("for %d old lower bound %g, new %g",iColumn,oldValue,value); |
---|
| 183 | solver->setColLower(iColumn, CoinMax(value, oldValue)); |
---|
| 184 | //printf(" => %g\n",solver->getColLower()[iColumn]); |
---|
| 185 | } |
---|
| 186 | // Set new upper bounds |
---|
| 187 | for (i = startUpper_[find]; i < startLower_[find+1]; i++) { |
---|
| 188 | int iColumn = variable_[i]; |
---|
| 189 | double value = newBound_[i]; |
---|
| 190 | double oldValue = solver->getColUpper()[iColumn]; |
---|
| 191 | //printf("for %d old upper bound %g, new %g",iColumn,oldValue,value); |
---|
| 192 | solver->setColUpper(iColumn, CoinMin(value, oldValue)); |
---|
| 193 | //printf(" => %g\n",solver->getColUpper()[iColumn]); |
---|
| 194 | } |
---|
| 195 | } |
---|
[1432] | 196 | |
---|