Changeset 2385 for trunk/Clp/src/ClpLinearObjective.cpp
 Timestamp:
 Jan 6, 2019 2:43:06 PM (4 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Clp/src/ClpLinearObjective.cpp
r1665 r2385 18 18 // Default Constructor 19 19 // 20 ClpLinearObjective::ClpLinearObjective 21 22 { 23 24 25 20 ClpLinearObjective::ClpLinearObjective() 21 : ClpObjective() 22 { 23 type_ = 1; 24 objective_ = NULL; 25 numberColumns_ = 0; 26 26 } 27 27 … … 29 29 // Useful Constructor 30 30 // 31 ClpLinearObjective::ClpLinearObjective (const double * objective,32 33 34 { 35 36 37 31 ClpLinearObjective::ClpLinearObjective(const double *objective, 32 int numberColumns) 33 : ClpObjective() 34 { 35 type_ = 1; 36 numberColumns_ = numberColumns; 37 objective_ = CoinCopyOfArray(objective, numberColumns_, 0.0); 38 38 } 39 39 … … 41 41 // Copy constructor 42 42 // 43 ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective &rhs)44 45 { 46 47 43 ClpLinearObjective::ClpLinearObjective(const ClpLinearObjective &rhs) 44 : ClpObjective(rhs) 45 { 46 numberColumns_ = rhs.numberColumns_; 47 objective_ = CoinCopyOfArray(rhs.objective_, numberColumns_); 48 48 } 49 49 /* Subset constructor. Duplicates are allowed 50 50 and order is as given. 51 51 */ 52 ClpLinearObjective::ClpLinearObjective (const ClpLinearObjective &rhs, 53 int numberColumns, 54 const int * whichColumn) 55 : ClpObjective(rhs) 56 { 57 objective_ = NULL; 58 numberColumns_ = 0; 59 if (numberColumns > 0) { 60 // check valid lists 61 int numberBad = 0; 62 int i; 63 for (i = 0; i < numberColumns; i++) 64 if (whichColumn[i] < 0  whichColumn[i] >= rhs.numberColumns_) 65 numberBad++; 66 if (numberBad) 67 throw CoinError("bad column list", "subset constructor", 68 "ClpLinearObjective"); 69 numberColumns_ = numberColumns; 70 objective_ = new double[numberColumns_]; 71 for (i = 0; i < numberColumns_; i++) 72 objective_[i] = rhs.objective_[whichColumn[i]]; 73 } 74 } 75 52 ClpLinearObjective::ClpLinearObjective(const ClpLinearObjective &rhs, 53 int numberColumns, 54 const int *whichColumn) 55 : ClpObjective(rhs) 56 { 57 objective_ = NULL; 58 numberColumns_ = 0; 59 if (numberColumns > 0) { 60 // check valid lists 61 int numberBad = 0; 62 int i; 63 for (i = 0; i < numberColumns; i++) 64 if (whichColumn[i] < 0  whichColumn[i] >= rhs.numberColumns_) 65 numberBad++; 66 if (numberBad) 67 throw CoinError("bad column list", "subset constructor", 68 "ClpLinearObjective"); 69 numberColumns_ = numberColumns; 70 objective_ = new double[numberColumns_]; 71 for (i = 0; i < numberColumns_; i++) 72 objective_[i] = rhs.objective_[whichColumn[i]]; 73 } 74 } 76 75 77 76 // 78 77 // Destructor 79 78 // 80 ClpLinearObjective::~ClpLinearObjective 81 { 82 delete[] objective_;79 ClpLinearObjective::~ClpLinearObjective() 80 { 81 delete[] objective_; 83 82 } 84 83 … … 87 86 // 88 87 ClpLinearObjective & 89 ClpLinearObjective::operator=(const ClpLinearObjective &rhs)90 { 91 92 93 94 delete[] objective_;95 96 97 88 ClpLinearObjective::operator=(const ClpLinearObjective &rhs) 89 { 90 if (this != &rhs) { 91 ClpObjective::operator=(rhs); 92 numberColumns_ = rhs.numberColumns_; 93 delete[] objective_; 94 objective_ = CoinCopyOfArray(rhs.objective_, numberColumns_); 95 } 96 return *this; 98 97 } 99 98 … … 101 100 double * 102 101 ClpLinearObjective::gradient(const ClpSimplex * /*model*/, 103 const double * /*solution*/, double &offset,104 105 106 { 107 108 109 110 111 102 const double * /*solution*/, double &offset, 103 bool /*refresh*/, 104 int /*includeLinear*/) 105 { 106 // not sure what to do about scaling 107 //assert (!model); 108 //assert (includeLinear==2); //otherwise need to return all zeros 109 offset = 0.0; 110 return objective_; 112 111 } 113 112 … … 115 114 */ 116 115 double 117 ClpLinearObjective::reducedGradient(ClpSimplex * model, double *region,118 119 { 120 121 122 CoinIndexedVector *workSpace = model>rowArray(0);123 124 125 126 127 116 ClpLinearObjective::reducedGradient(ClpSimplex *model, double *region, 117 bool /*useFeasibleCosts*/) 118 { 119 int numberRows = model>numberRows(); 120 //work space 121 CoinIndexedVector *workSpace = model>rowArray(0); 122 123 CoinIndexedVector arrayVector; 124 arrayVector.reserve(numberRows + 1); 125 126 int iRow; 128 127 #ifdef CLP_DEBUG 129 128 workSpace>checkClear(); 130 129 #endif 131 double *array = arrayVector.denseVector();132 int *index = arrayVector.getIndices();133 134 const double *cost = model>costRegion();135 136 const int *pivotVariable = model>pivotVariable();137 138 139 140 141 142 143 144 145 146 147 148 149 150 double *work = workSpace>denseVector();151 152 153 154 double *rowReducedCost = region + numberColumns;155 double *dual = rowReducedCost;156 double *rowCost = model>costRegion(0);157 158 159 160 double *dj = region;161 162 163 164 165 166 167 168 169 130 double *array = arrayVector.denseVector(); 131 int *index = arrayVector.getIndices(); 132 int number = 0; 133 const double *cost = model>costRegion(); 134 //assert (!useFeasibleCosts); 135 const int *pivotVariable = model>pivotVariable(); 136 for (iRow = 0; iRow < numberRows; iRow++) { 137 int iPivot = pivotVariable[iRow]; 138 double value = cost[iPivot]; 139 if (value) { 140 array[iRow] = value; 141 index[number++] = iRow; 142 } 143 } 144 arrayVector.setNumElements(number); 145 146 int numberColumns = model>numberColumns(); 147 148 // Btran basic costs 149 double *work = workSpace>denseVector(); 150 model>factorization()>updateColumnTranspose(workSpace, &arrayVector); 151 ClpFillN(work, numberRows, 0.0); 152 // now look at dual solution 153 double *rowReducedCost = region + numberColumns; 154 double *dual = rowReducedCost; 155 double *rowCost = model>costRegion(0); 156 for (iRow = 0; iRow < numberRows; iRow++) { 157 dual[iRow] = array[iRow]; 158 } 159 double *dj = region; 160 ClpDisjointCopyN(model>costRegion(1), numberColumns, dj); 161 model>transposeTimes(1.0, dual, dj); 162 for (iRow = 0; iRow < numberRows; iRow++) { 163 // slack 164 double value = dual[iRow]; 165 value += rowCost[iRow]; 166 rowReducedCost[iRow] = value; 167 } 168 return 0.0; 170 169 } 171 170 /* Returns step length which gives minimum of objective for … … 175 174 */ 176 175 double 177 ClpLinearObjective::stepLength(ClpSimplex * 178 const double *solution,179 const double *change,180 181 double ¤tObj,182 double &predictedObj,183 double &thetaObj)184 { 185 const double *cost = model>costRegion();186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 176 ClpLinearObjective::stepLength(ClpSimplex *model, 177 const double *solution, 178 const double *change, 179 double maximumTheta, 180 double ¤tObj, 181 double &predictedObj, 182 double &thetaObj) 183 { 184 const double *cost = model>costRegion(); 185 double delta = 0.0; 186 int numberRows = model>numberRows(); 187 int numberColumns = model>numberColumns(); 188 currentObj = 0.0; 189 thetaObj = 0.0; 190 for (int iColumn = 0; iColumn < numberColumns + numberRows; iColumn++) { 191 delta += cost[iColumn] * change[iColumn]; 192 currentObj += cost[iColumn] * solution[iColumn]; 193 } 194 thetaObj = currentObj + delta * maximumTheta; 195 predictedObj = currentObj + delta * maximumTheta; 196 if (delta < 0.0) { 197 return maximumTheta; 198 } else { 199 printf("odd linear direction %g\n", delta); 200 return 0.0; 201 } 203 202 } 204 203 // Return objective value (without any ClpModel offset) (model may be NULL) 205 204 double 206 ClpLinearObjective::objectiveValue(const ClpSimplex * model, const double *solution) const207 { 208 const double *cost = objective_;209 210 211 212 213 214 215 205 ClpLinearObjective::objectiveValue(const ClpSimplex *model, const double *solution) const 206 { 207 const double *cost = objective_; 208 if (model && model>costRegion()) 209 cost = model>costRegion(); 210 double currentObj = 0.0; 211 for (int iColumn = 0; iColumn < numberColumns_; iColumn++) { 212 currentObj += cost[iColumn] * solution[iColumn]; 213 } 214 return currentObj; 216 215 } 217 216 // 218 217 // Clone 219 218 // 220 ClpObjective * 221 { 222 219 ClpObjective *ClpLinearObjective::clone() const 220 { 221 return new ClpLinearObjective(*this); 223 222 } 224 223 /* Subset clone. Duplicates are allowed … … 226 225 */ 227 226 ClpObjective * 228 ClpLinearObjective::subsetClone 229 const int *whichColumns) const230 { 231 227 ClpLinearObjective::subsetClone(int numberColumns, 228 const int *whichColumns) const 229 { 230 return new ClpLinearObjective(*this, numberColumns, whichColumns); 232 231 } 233 232 // Resize objective 234 void 235 ClpLinearObjective::resize(int newNumberColumns) 236 { 237 if (numberColumns_ != newNumberColumns) { 238 int i; 239 double * newArray = new double[newNumberColumns]; 240 if (objective_) 241 CoinMemcpyN(objective_, CoinMin(newNumberColumns, numberColumns_), newArray); 242 delete [] objective_; 243 objective_ = newArray; 244 for (i = numberColumns_; i < newNumberColumns; i++) 245 objective_[i] = 0.0; 246 numberColumns_ = newNumberColumns; 247 } 248 233 void ClpLinearObjective::resize(int newNumberColumns) 234 { 235 if (numberColumns_ != newNumberColumns) { 236 int i; 237 double *newArray = new double[newNumberColumns]; 238 if (objective_) 239 CoinMemcpyN(objective_, CoinMin(newNumberColumns, numberColumns_), newArray); 240 delete[] objective_; 241 objective_ = newArray; 242 for (i = numberColumns_; i < newNumberColumns; i++) 243 objective_[i] = 0.0; 244 numberColumns_ = newNumberColumns; 245 } 249 246 } 250 247 // Delete columns in objective 251 void 252 ClpLinearObjective::deleteSome(int numberToDelete, const int * which) 253 { 254 if (objective_) { 255 int i ; 256 char * deleted = new char[numberColumns_]; 257 int numberDeleted = 0; 258 CoinZeroN(deleted, numberColumns_); 259 for (i = 0; i < numberToDelete; i++) { 260 int j = which[i]; 261 if (j >= 0 && j < numberColumns_ && !deleted[j]) { 262 numberDeleted++; 263 deleted[j] = 1; 264 } 265 } 266 int newNumberColumns = numberColumns_  numberDeleted; 267 double * newArray = new double[newNumberColumns]; 268 int put = 0; 269 for (i = 0; i < numberColumns_; i++) { 270 if (!deleted[i]) { 271 newArray[put++] = objective_[i]; 272 } 273 } 274 delete [] objective_; 275 objective_ = newArray; 276 delete [] deleted; 277 numberColumns_ = newNumberColumns; 278 } 248 void ClpLinearObjective::deleteSome(int numberToDelete, const int *which) 249 { 250 if (objective_) { 251 int i; 252 char *deleted = new char[numberColumns_]; 253 int numberDeleted = 0; 254 CoinZeroN(deleted, numberColumns_); 255 for (i = 0; i < numberToDelete; i++) { 256 int j = which[i]; 257 if (j >= 0 && j < numberColumns_ && !deleted[j]) { 258 numberDeleted++; 259 deleted[j] = 1; 260 } 261 } 262 int newNumberColumns = numberColumns_  numberDeleted; 263 double *newArray = new double[newNumberColumns]; 264 int put = 0; 265 for (i = 0; i < numberColumns_; i++) { 266 if (!deleted[i]) { 267 newArray[put++] = objective_[i]; 268 } 269 } 270 delete[] objective_; 271 objective_ = newArray; 272 delete[] deleted; 273 numberColumns_ = newNumberColumns; 274 } 279 275 } 280 276 // Scale objective 281 void 282 ClpLinearObjective::reallyScale(const double * columnScale) 283 { 284 for (int iColumn = 0; iColumn < numberColumns_; iColumn++) { 285 objective_[iColumn] *= columnScale[iColumn]; 286 } 287 } 288 277 void ClpLinearObjective::reallyScale(const double *columnScale) 278 { 279 for (int iColumn = 0; iColumn < numberColumns_; iColumn++) { 280 objective_[iColumn] *= columnScale[iColumn]; 281 } 282 } 283 284 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 285 */
Note: See TracChangeset
for help on using the changeset viewer.