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

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Clp/src/ClpConstraintQuadratic.cpp
r2271 r2385 17 17 // Default Constructor 18 18 // 19 ClpConstraintQuadratic::ClpConstraintQuadratic 20 21 { 22 23 24 25 26 27 28 19 ClpConstraintQuadratic::ClpConstraintQuadratic() 20 : ClpConstraint() 21 { 22 type_ = 0; 23 start_ = NULL; 24 column_ = NULL; 25 coefficient_ = NULL; 26 numberColumns_ = 0; 27 numberCoefficients_ = 0; 28 numberQuadraticColumns_ = 0; 29 29 } 30 30 … … 32 32 // Useful Constructor 33 33 // 34 ClpConstraintQuadratic::ClpConstraintQuadratic (int row, int numberQuadraticColumns,35 int numberColumns, const CoinBigIndex *start,36 const int * column, const double *coefficient)37 38 { 39 40 41 42 43 44 45 46 47 char * mark = new char[numberQuadraticColumns_];48 49 50 51 52 for (j = start_[iColumn]; j < start_[iColumn+1]; j++) {53 54 55 assert(jColumn < numberQuadraticColumns_);56 57 58 59 60 61 62 63 64 65 66 delete[] mark;34 ClpConstraintQuadratic::ClpConstraintQuadratic(int row, int numberQuadraticColumns, 35 int numberColumns, const CoinBigIndex *start, 36 const int *column, const double *coefficient) 37 : ClpConstraint() 38 { 39 type_ = 0; 40 rowNumber_ = row; 41 numberColumns_ = numberColumns; 42 numberQuadraticColumns_ = numberQuadraticColumns; 43 start_ = CoinCopyOfArray(start, numberQuadraticColumns + 1); 44 CoinBigIndex numberElements = start_[numberQuadraticColumns_]; 45 column_ = CoinCopyOfArray(column, numberElements); 46 coefficient_ = CoinCopyOfArray(coefficient, numberElements); 47 char *mark = new char[numberQuadraticColumns_]; 48 memset(mark, 0, numberQuadraticColumns_); 49 int iColumn; 50 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 51 CoinBigIndex j; 52 for (j = start_[iColumn]; j < start_[iColumn + 1]; j++) { 53 int jColumn = column_[j]; 54 if (jColumn >= 0) { 55 assert(jColumn < numberQuadraticColumns_); 56 mark[jColumn] = 1; 57 } 58 mark[iColumn] = 1; 59 } 60 } 61 numberCoefficients_ = 0; 62 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 63 if (mark[iColumn]) 64 numberCoefficients_++; 65 } 66 delete[] mark; 67 67 } 68 68 … … 70 70 // Copy constructor 71 71 // 72 ClpConstraintQuadratic::ClpConstraintQuadratic (const ClpConstraintQuadratic & rhs) 73 : ClpConstraint(rhs) 74 { 75 numberColumns_ = rhs.numberColumns_; 76 numberCoefficients_ = rhs.numberCoefficients_; 77 numberQuadraticColumns_ = rhs.numberQuadraticColumns_; 78 start_ = CoinCopyOfArray(rhs.start_, numberQuadraticColumns_ + 1); 79 CoinBigIndex numberElements = start_[numberQuadraticColumns_]; 80 column_ = CoinCopyOfArray(rhs.column_, numberElements); 81 coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberElements); 82 } 83 72 ClpConstraintQuadratic::ClpConstraintQuadratic(const ClpConstraintQuadratic &rhs) 73 : ClpConstraint(rhs) 74 { 75 numberColumns_ = rhs.numberColumns_; 76 numberCoefficients_ = rhs.numberCoefficients_; 77 numberQuadraticColumns_ = rhs.numberQuadraticColumns_; 78 start_ = CoinCopyOfArray(rhs.start_, numberQuadraticColumns_ + 1); 79 CoinBigIndex numberElements = start_[numberQuadraticColumns_]; 80 column_ = CoinCopyOfArray(rhs.column_, numberElements); 81 coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberElements); 82 } 84 83 85 84 // 86 85 // Destructor 87 86 // 88 ClpConstraintQuadratic::~ClpConstraintQuadratic 89 { 90 delete[] start_;91 delete[] column_;92 delete[] coefficient_;87 ClpConstraintQuadratic::~ClpConstraintQuadratic() 88 { 89 delete[] start_; 90 delete[] column_; 91 delete[] coefficient_; 93 92 } 94 93 … … 97 96 // 98 97 ClpConstraintQuadratic & 99 ClpConstraintQuadratic::operator=(const ClpConstraintQuadratic &rhs)100 { 101 102 delete[] start_;103 delete[] column_;104 delete[] coefficient_;105 106 107 108 109 110 111 112 113 98 ClpConstraintQuadratic::operator=(const ClpConstraintQuadratic &rhs) 99 { 100 if (this != &rhs) { 101 delete[] start_; 102 delete[] column_; 103 delete[] coefficient_; 104 numberColumns_ = rhs.numberColumns_; 105 numberCoefficients_ = rhs.numberCoefficients_; 106 numberQuadraticColumns_ = rhs.numberQuadraticColumns_; 107 start_ = CoinCopyOfArray(rhs.start_, numberQuadraticColumns_ + 1); 108 CoinBigIndex numberElements = start_[numberQuadraticColumns_]; 109 column_ = CoinCopyOfArray(rhs.column_, numberElements); 110 coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberElements); 111 } 112 return *this; 114 113 } 115 114 // 116 115 // Clone 117 116 // 118 ClpConstraint * 119 { 120 117 ClpConstraint *ClpConstraintQuadratic::clone() const 118 { 119 return new ClpConstraintQuadratic(*this); 121 120 } 122 121 123 122 // Returns gradient 124 int 125 ClpConstraintQuadratic::gradient(const ClpSimplex * model, 126 const double * solution, 127 double * gradient, 128 double & functionValue, 129 double & offset, 130 bool useScaling, 131 bool refresh) const 132 { 133 if (refresh  !lastGradient_) { 134 offset_ = 0.0; 135 functionValue_ = 0.0; 136 if (!lastGradient_) 137 lastGradient_ = new double[numberColumns_]; 138 CoinZeroN(lastGradient_, numberColumns_); 139 bool scaling = (model && model>rowScale() && useScaling); 140 if (!scaling) { 141 int iColumn; 142 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 143 double valueI = solution[iColumn]; 144 CoinBigIndex j; 145 for (j = start_[iColumn]; j < start_[iColumn+1]; j++) { 146 int jColumn = column_[j]; 147 if (jColumn >= 0) { 148 double valueJ = solution[jColumn]; 149 double elementValue = coefficient_[j]; 150 if (iColumn != jColumn) { 151 offset_ = valueI * valueJ * elementValue; 152 double gradientI = valueJ * elementValue; 153 double gradientJ = valueI * elementValue; 154 lastGradient_[iColumn] += gradientI; 155 lastGradient_[jColumn] += gradientJ; 156 } else { 157 offset_ = 0.5 * valueI * valueI * elementValue; 158 double gradientI = valueI * elementValue; 159 lastGradient_[iColumn] += gradientI; 160 } 161 } else { 162 // linear part 163 lastGradient_[iColumn] += coefficient_[j]; 164 functionValue_ += valueI * coefficient_[j]; 165 } 166 } 167 } 168 functionValue_ = offset_; 123 int ClpConstraintQuadratic::gradient(const ClpSimplex *model, 124 const double *solution, 125 double *gradient, 126 double &functionValue, 127 double &offset, 128 bool useScaling, 129 bool refresh) const 130 { 131 if (refresh  !lastGradient_) { 132 offset_ = 0.0; 133 functionValue_ = 0.0; 134 if (!lastGradient_) 135 lastGradient_ = new double[numberColumns_]; 136 CoinZeroN(lastGradient_, numberColumns_); 137 bool scaling = (model && model>rowScale() && useScaling); 138 if (!scaling) { 139 int iColumn; 140 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 141 double valueI = solution[iColumn]; 142 CoinBigIndex j; 143 for (j = start_[iColumn]; j < start_[iColumn + 1]; j++) { 144 int jColumn = column_[j]; 145 if (jColumn >= 0) { 146 double valueJ = solution[jColumn]; 147 double elementValue = coefficient_[j]; 148 if (iColumn != jColumn) { 149 offset_ = valueI * valueJ * elementValue; 150 double gradientI = valueJ * elementValue; 151 double gradientJ = valueI * elementValue; 152 lastGradient_[iColumn] += gradientI; 153 lastGradient_[jColumn] += gradientJ; 154 } else { 155 offset_ = 0.5 * valueI * valueI * elementValue; 156 double gradientI = valueI * elementValue; 157 lastGradient_[iColumn] += gradientI; 158 } 169 159 } else { 170 abort(); 171 // do scaling 172 const double * columnScale = model>columnScale(); 173 for (int i = 0; i < numberCoefficients_; i++) { 174 int iColumn = column_[i]; 175 double value = solution[iColumn]; // already scaled 176 double coefficient = coefficient_[i] * columnScale[iColumn]; 177 functionValue_ += value * coefficient; 178 lastGradient_[iColumn] = coefficient; 179 } 160 // linear part 161 lastGradient_[iColumn] += coefficient_[j]; 162 functionValue_ += valueI * coefficient_[j]; 180 163 } 181 } 182 functionValue = functionValue_; 183 offset = offset_; 184 CoinMemcpyN(lastGradient_, numberColumns_, gradient); 185 return 0; 164 } 165 } 166 functionValue_ = offset_; 167 } else { 168 abort(); 169 // do scaling 170 const double *columnScale = model>columnScale(); 171 for (int i = 0; i < numberCoefficients_; i++) { 172 int iColumn = column_[i]; 173 double value = solution[iColumn]; // already scaled 174 double coefficient = coefficient_[i] * columnScale[iColumn]; 175 functionValue_ += value * coefficient; 176 lastGradient_[iColumn] = coefficient; 177 } 178 } 179 } 180 functionValue = functionValue_; 181 offset = offset_; 182 CoinMemcpyN(lastGradient_, numberColumns_, gradient); 183 return 0; 186 184 } 187 185 // Resize constraint 188 void 189 ClpConstraintQuadratic::resize(int newNumberColumns) 190 { 191 if (numberColumns_ != newNumberColumns) { 192 abort(); 186 void ClpConstraintQuadratic::resize(int newNumberColumns) 187 { 188 if (numberColumns_ != newNumberColumns) { 189 abort(); 193 190 #ifndef NDEBUG 194 int lastColumn = column_[numberCoefficients_1];191 int lastColumn = column_[numberCoefficients_  1]; 195 192 #endif 196 assert(newNumberColumns > lastColumn);197 delete[] lastGradient_;198 199 200 193 assert(newNumberColumns > lastColumn); 194 delete[] lastGradient_; 195 lastGradient_ = NULL; 196 numberColumns_ = newNumberColumns; 197 } 201 198 } 202 199 // Delete columns in constraint 203 void 204 ClpConstraintQuadratic::deleteSome(int numberToDelete, const int * which) 205 { 206 if (numberToDelete) { 207 abort(); 208 int i ; 209 char * deleted = new char[numberColumns_]; 210 memset(deleted, 0, numberColumns_ * sizeof(char)); 211 for (i = 0; i < numberToDelete; i++) { 212 int j = which[i]; 213 if (j >= 0 && j < numberColumns_ && !deleted[j]) { 214 deleted[j] = 1; 215 } 216 } 217 int n = 0; 218 for (i = 0; i < numberCoefficients_; i++) { 219 int iColumn = column_[i]; 220 if (!deleted[iColumn]) { 221 column_[n] = iColumn; 222 coefficient_[n++] = coefficient_[i]; 223 } 224 } 225 numberCoefficients_ = n; 226 } 200 void ClpConstraintQuadratic::deleteSome(int numberToDelete, const int *which) 201 { 202 if (numberToDelete) { 203 abort(); 204 int i; 205 char *deleted = new char[numberColumns_]; 206 memset(deleted, 0, numberColumns_ * sizeof(char)); 207 for (i = 0; i < numberToDelete; i++) { 208 int j = which[i]; 209 if (j >= 0 && j < numberColumns_ && !deleted[j]) { 210 deleted[j] = 1; 211 } 212 } 213 int n = 0; 214 for (i = 0; i < numberCoefficients_; i++) { 215 int iColumn = column_[i]; 216 if (!deleted[iColumn]) { 217 column_[n] = iColumn; 218 coefficient_[n++] = coefficient_[i]; 219 } 220 } 221 numberCoefficients_ = n; 222 } 227 223 } 228 224 // Scale constraint 229 void 230 ClpConstraintQuadratic::reallyScale(const double * ) 231 { 232 abort(); 225 void ClpConstraintQuadratic::reallyScale(const double *) 226 { 227 abort(); 233 228 } 234 229 /* Given a zeroed array sets nonquadratic columns to 1. 235 230 Returns number of nonlinear columns 236 231 */ 237 int 238 ClpConstraintQuadratic::markNonlinear(char * which) const 239 { 240 int iColumn; 241 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 242 CoinBigIndex j; 243 for (j = start_[iColumn]; j < start_[iColumn+1]; j++) { 244 int jColumn = column_[j]; 245 if (jColumn >= 0) { 246 assert (jColumn < numberQuadraticColumns_); 247 which[jColumn] = 1; 248 which[iColumn] = 1; 249 } 250 } 251 } 252 int numberCoefficients = 0; 253 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 254 if (which[iColumn]) 255 numberCoefficients++; 256 } 257 return numberCoefficients; 232 int ClpConstraintQuadratic::markNonlinear(char *which) const 233 { 234 int iColumn; 235 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 236 CoinBigIndex j; 237 for (j = start_[iColumn]; j < start_[iColumn + 1]; j++) { 238 int jColumn = column_[j]; 239 if (jColumn >= 0) { 240 assert(jColumn < numberQuadraticColumns_); 241 which[jColumn] = 1; 242 which[iColumn] = 1; 243 } 244 } 245 } 246 int numberCoefficients = 0; 247 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 248 if (which[iColumn]) 249 numberCoefficients++; 250 } 251 return numberCoefficients; 258 252 } 259 253 /* Given a zeroed array sets possible nonzero coefficients to 1. 260 254 Returns number of nonzeros 261 255 */ 262 int 263 ClpConstraintQuadratic::markNonzero(char * which) const 264 { 265 int iColumn; 266 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 267 CoinBigIndex j; 268 for (j = start_[iColumn]; j < start_[iColumn+1]; j++) { 269 int jColumn = column_[j]; 270 if (jColumn >= 0) { 271 assert (jColumn < numberQuadraticColumns_); 272 which[jColumn] = 1; 273 } 274 which[iColumn] = 1; 275 } 276 } 277 int numberCoefficients = 0; 278 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 279 if (which[iColumn]) 280 numberCoefficients++; 281 } 282 return numberCoefficients; 256 int ClpConstraintQuadratic::markNonzero(char *which) const 257 { 258 int iColumn; 259 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 260 CoinBigIndex j; 261 for (j = start_[iColumn]; j < start_[iColumn + 1]; j++) { 262 int jColumn = column_[j]; 263 if (jColumn >= 0) { 264 assert(jColumn < numberQuadraticColumns_); 265 which[jColumn] = 1; 266 } 267 which[iColumn] = 1; 268 } 269 } 270 int numberCoefficients = 0; 271 for (iColumn = 0; iColumn < numberQuadraticColumns_; iColumn++) { 272 if (which[iColumn]) 273 numberCoefficients++; 274 } 275 return numberCoefficients; 283 276 } 284 277 // Number of coefficients 285 int 286 ClpConstraintQuadratic::numberCoefficients() const 287 { 288 return numberCoefficients_; 289 } 278 int ClpConstraintQuadratic::numberCoefficients() const 279 { 280 return numberCoefficients_; 281 } 282 283 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 284 */
Note: See TracChangeset
for help on using the changeset viewer.