Changeset 2033


Ignore:
Timestamp:
May 8, 2014 1:41:51 PM (5 years ago)
Author:
mlubin
Message:

SOS updates and test (not yet working)

Location:
trunk/Cbc
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/src/Cbc_C_Interface.cpp

    r2032 r2033  
    823823    return model;
    824824}
    825 /* Add an SOS constraint to the model */
    826 COINLIBAPI void  COINLINKAGE
    827 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 broken
    835     int i, j;
    836     // I think this is a different model due to overriding = operator
    837     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 objects
    857         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 }
    880825/** Add SOS constraints to the model using row-order matrix */
    881826COINLIBAPI 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;
     827Cbc_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;
    887832    if (VERBOSE > 0) printf("%sbegin\n", prefix);
    888833
    889     int numRows = Cbc_getNumRows(model);
    890834    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 model
    893     assert(numRows == Cbc_getNumRows(model));
    894835
    895836    int row, i;
     
    925866        const int numWeights = rowStarts[row+1] - rowStarts[row];
    926867        if (VERBOSE > 2) printf("%s  numWeights = %i\n", prefix, numWeights);
    927         colIndex    = rowIndices + rowStarts[row];
     868        colIndex    = colIndices + rowStarts[row];
    928869        colWeight   = weights + rowStarts[row];
    929870        if (numWeights > 0) {
  • trunk/Cbc/src/Cbc_C_Interface.h

    r2032 r2033  
    201201    Cbc_setInteger(Cbc_Model * model, int iColumn)
    202202    ;
    203     /** Add SOS constraints to the model using dense matrix */
    204     COINLIBAPI void  COINLINKAGE
    205     Cbc_addSOS_Dense(Cbc_Model * model, int numObjects, const int * len,
    206                      const int * const * which, const double * weights, const int type)
    207     ;
    208203    /** Add SOS constraints to the model using row-order matrix */
    209204    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)
    212207    ;
    213208    /** Print the model */
  • trunk/Cbc/test/CInterfaceTest.c

    r2032 r2033  
    111111}
    112112
     113void 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) < 1e-6));
     160    assert(fabs( Cbc_getBestPossibleObjValue(model)- (2.0) < 1e-6));
     161
     162    sol = Cbc_getColSolution(model);
     163   
     164    assert(fabs(sol[0] - 0.0) < 1e-6);
     165    assert(fabs(sol[1] - 0.0) < 1e-6);
     166    assert(fabs(sol[2] - 1.0) < 1e-6);
     167    assert(fabs(sol[3] - 0.0) < 1e-6);
     168    assert(fabs(sol[4] - 0.0) < 1e-6);
     169
     170    Cbc_deleteModel(model);
     171
     172}
     173
    113174void testIntegerInfeasible() {
    114175
     
    187248int main() {
    188249
     250    printf("Knapsack test\n");
    189251    testKnapsack();
     252    printf("SOS test\n");
     253    testSOS();
     254    printf("Infeasible test\n");
    190255    testIntegerInfeasible();
     256    printf("Unbounded test\n");
    191257    testIntegerUnbounded();
    192258
Note: See TracChangeset for help on using the changeset viewer.