Changeset 2033
 Timestamp:
 May 8, 2014 1:41:51 PM (5 years ago)
 Location:
 trunk/Cbc
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/src/Cbc_C_Interface.cpp
r2032 r2033 823 823 return model; 824 824 } 825 /* Add an SOS constraint to the model */826 COINLIBAPI void COINLINKAGE827 Cbc_addSOS_Dense(Cbc_Model * model, int numObjects, const int * len,828 const int * const* which, const double * weights, const int type)829 {830 const char prefix[] = "Cbc_C_Interface::Cbc_addSOS_Dense(): ";831 // const int VERBOSE = 2;832 if (VERBOSE > 0) printf("%sbegin\n", prefix);833 834 assert(1 > 0);// this is probably broken835 int i, j;836 // I think this is a different model due to overriding = operator837 CbcModel m = *(model>model_);838 839 CbcObject ** objects = new CbcObject * [numObjects];840 841 if (VERBOSE > 1) printf("%s numObjects = %i\n", prefix, numObjects);842 for (i = 0; i < numObjects; i++) {843 if (VERBOSE > 1) {844 printf("%s len[%i] = %i, identifier = %i, type = %i\n",845 prefix, i, len[i], i, type);846 fflush(stdout);847 for (j = 0; j < len[i]; j++) {848 if (VERBOSE > 2  j == 0  j == (len[i]  1)) {849 printf("%s which[%i][%i] = %d, weights[%i] = %g\n",850 prefix, i, j, which[i][j], j, weights[j]);851 fflush(stdout);852 }853 }854 }855 856 // Make a CbcSOS and assign it to objects857 if (VERBOSE > 1) printf("%s len[%i] = %i\n", prefix, i, len[i]);858 if (VERBOSE > 1) printf("%s new CbcSOS()\n", prefix);859 // ***860 objects[i] = new CbcSOS(model>model_, (int)(len[i]),861 (const int*)which[i], (const double*)weights, (int)i, (int)type);862 // ***863 if (objects[i] == NULL) {864 printf("%s ERROR: objects[%i] == NULL\n", prefix, i);865 fflush(stdout);866 assert(objects[i] != NULL);867 }868 }869 if (VERBOSE > 1) printf("%s calling addObjects()\n", prefix);870 fflush(stdout);871 model>model_>addObjects(numObjects, objects);872 if (VERBOSE > 1) printf("%s finished addObjects()\n", prefix);873 874 for (i = 0; i < numObjects; i++) delete objects[i];875 delete [] objects;876 877 if (VERBOSE > 0) printf("%sreturn\n", prefix);878 return;879 }880 825 /** Add SOS constraints to the model using roworder matrix */ 881 826 COINLIBAPI void COINLINKAGE 882 Cbc_addSOS _Sparse(Cbc_Model * model, const int * rowStarts,883 const int * rowIndices, const double * weights, const int type)884 { 885 const char prefix[] = "Cbc_C_Interface::Cbc_addSOS _Sparse(): ";886 // const int VERBOSE = 1;827 Cbc_addSOS(Cbc_Model * model, int numRows, const int * rowStarts, 828 const int * colIndices, const double * weights, const int type) 829 { 830 const char prefix[] = "Cbc_C_Interface::Cbc_addSOS(): "; 831 //const int VERBOSE = 4; 887 832 if (VERBOSE > 0) printf("%sbegin\n", prefix); 888 833 889 int numRows = Cbc_getNumRows(model);890 834 if (VERBOSE > 0) printf("%s numRows = %i\n", prefix, numRows); 891 892 // The passed sparse matrix must have the same number of rows as the model893 assert(numRows == Cbc_getNumRows(model));894 835 895 836 int row, i; … … 925 866 const int numWeights = rowStarts[row+1]  rowStarts[row]; 926 867 if (VERBOSE > 2) printf("%s numWeights = %i\n", prefix, numWeights); 927 colIndex = rowIndices + rowStarts[row];868 colIndex = colIndices + rowStarts[row]; 928 869 colWeight = weights + rowStarts[row]; 929 870 if (numWeights > 0) { 
trunk/Cbc/src/Cbc_C_Interface.h
r2032 r2033 201 201 Cbc_setInteger(Cbc_Model * model, int iColumn) 202 202 ; 203 /** Add SOS constraints to the model using dense matrix */204 COINLIBAPI void COINLINKAGE205 Cbc_addSOS_Dense(Cbc_Model * model, int numObjects, const int * len,206 const int * const * which, const double * weights, const int type)207 ;208 203 /** Add SOS constraints to the model using roworder matrix */ 209 204 COINLIBAPI void COINLINKAGE 210 Cbc_addSOS _Sparse(Cbc_Model * model, const int * rowStarts,211 const int * rowIndices, const double * weights, const int type)205 Cbc_addSOS(Cbc_Model * model, int numRows, const int * rowStarts, 206 const int * colIndices, const double * weights, const int type) 212 207 ; 213 208 /** Print the model */ 
trunk/Cbc/test/CInterfaceTest.c
r2032 r2033 111 111 } 112 112 113 void testSOS() { 114 115 Cbc_Model *model = Cbc_newModel(); 116 117 /* 118 Minimize 5x[1] + 3x[2] + 2x[3] + 7x[4] + 4x[5] 119 s.t. x[1] + x[2] + x[3] + x[4] + x[5] == 1 120 All x binary 121 */ 122 123 CoinBigIndex start[] = {0, 0, 0, 0, 0, 0, 0}; 124 /*int rowindex[] = {};*/ 125 /*double value[] = {};*/ 126 double collb[] = {0,0,0,0,0}; 127 double colub[] = {1,1,1,1,1}; 128 double obj[] = {5, 3, 2, 7, 4}; 129 /*double rowlb[] = {};*/ 130 /*double rowub[] = {};*/ 131 int sosrowstart[] = {0,5}; 132 int soscolindex[] = {0,1,2,3,4}; 133 const double *sol; 134 int i; 135 136 Cbc_loadProblem(model, 5, 0, start, NULL, NULL, collb, colub, obj, NULL, NULL); 137 138 assert(Cbc_getNumCols(model) == 5); 139 assert(Cbc_getNumRows(model) == 0); 140 141 for (i = 0; i < 5; i++) { 142 Cbc_setInteger(model, i); 143 assert(Cbc_isInteger(model,i)); 144 } 145 146 assert(Cbc_getObjSense(model) == 1); 147 148 Cbc_addSOS(model,1,sosrowstart,soscolindex,obj,1); 149 150 Cbc_solve(model); 151 152 assert(Cbc_isProvenOptimal(model)); 153 assert(!Cbc_isAbandoned(model)); 154 assert(!Cbc_isProvenInfeasible(model)); 155 assert(!Cbc_isContinuousUnbounded(model)); 156 assert(!Cbc_isNodeLimitReached(model)); 157 assert(!Cbc_isSecondsLimitReached(model)); 158 assert(!Cbc_isSolutionLimitReached(model)); 159 assert(fabs( Cbc_getObjValue(model) (2.0) < 1e6)); 160 assert(fabs( Cbc_getBestPossibleObjValue(model) (2.0) < 1e6)); 161 162 sol = Cbc_getColSolution(model); 163 164 assert(fabs(sol[0]  0.0) < 1e6); 165 assert(fabs(sol[1]  0.0) < 1e6); 166 assert(fabs(sol[2]  1.0) < 1e6); 167 assert(fabs(sol[3]  0.0) < 1e6); 168 assert(fabs(sol[4]  0.0) < 1e6); 169 170 Cbc_deleteModel(model); 171 172 } 173 113 174 void testIntegerInfeasible() { 114 175 … … 187 248 int main() { 188 249 250 printf("Knapsack test\n"); 189 251 testKnapsack(); 252 printf("SOS test\n"); 253 testSOS(); 254 printf("Infeasible test\n"); 190 255 testIntegerInfeasible(); 256 printf("Unbounded test\n"); 191 257 testIntegerUnbounded(); 192 258
Note: See TracChangeset
for help on using the changeset viewer.