Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • stable/2.8/Cbc/examples/sample4.cpp

    r1675 r1902  
    2626/************************************************************************
    2727
    28 This main program reads in an SOSr model (ltw) from an mps file.
     28This main program reads in an SOS model (rgn) from an mps file.
    2929
    3030It then solves it three ways :-
     
    3232a) As normal
    3333b) SOS 1
    34 c) SOS 2(so answer will be different)
     34c) SOS 2 (so answer will be different)
    3535
    3636************************************************************************/
     
    4646  model.solver()->setHintParam(OsiDoReducePrint,true,OsiHintTry);
    4747
    48   // Read in ltw.mps
    49   // and assert that it is a clean model
    50   int numMpsReadErrors = model.solver()->readMps("./ltw.mps","");
    51   assert(numMpsReadErrors==0);
     48  // Read in rgn.mps
     49  std::string mpsFileName;
     50#if defined(MIPLIB3DIR)
     51  mpsFileName = MIPLIB3DIR "/rgn";
     52#else
     53  if (argc < 2) {
     54    fprintf(stderr, "Do not know where to find miplib3 MPS files.\n");
     55    exit(1);
     56  }
     57#endif
     58  if (argc>=2) mpsFileName = argv[1];
     59  int numMpsReadErrors = model.solver()->readMps(mpsFileName.c_str(),"");
     60  if( numMpsReadErrors != 0 )
     61  {
     62     printf("%d errors reading MPS file\n", numMpsReadErrors);
     63     return numMpsReadErrors;
     64  }
    5265
    5366  // Definition of node choice
     
    7588  }
    7689
    77  
     90  if (numberColumns!=180 || numberIntegers!=100) {
     91    printf("Incorrect model for example\n");
     92    exit(1);
     93  }
     94
    7895  double time1 = CoinCpuTime() ;
    7996
    8097  model.branchAndBound();
    8198
    82   std::cout<<"ltw.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
     99  std::cout<<"rgn.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
    83100           <<model.getNodeCount()<<" nodes with objective "
    84101           <<model.getObjValue()
     
    104121  model = model2;
    105122
     123  // Convert slacks to variables
     124  CoinBigIndex start[5]={0,1,2,3,4};
     125  int row[4]={0,1,2,3};
     126  double element[4]={1.0,1.0,1.0,1.0};
     127  double up[4]={1.0,1.0,1.0,1.0};
     128  model.solver()->addCols(4,start,row,element,NULL,up,NULL);
    106129  // Now use SOS1
    107   int numberSets=8;
    108   int which[28]={20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,
    109                  39,40,41,42,43,44,45,46,47};
    110   double weights[]={1.0,2.0,3.0,4.0,5.0};
    111   int starts[]={0,2,4,6,8,13,18,23,28};
    112   CbcObject ** objects = new CbcObject * [numberSets];
     130  int numberSets=4;
     131  int which[104];
     132  double weights[104];
     133  int starts[5];
     134  // load
     135  int n=0;
     136  starts[0]=0;
     137  for (int iSet=0;iSet<4;iSet++) {
     138    for (int i=0;i<25;i++) {
     139      weights[n]=i+1.0;
     140      which[n]=iSet*25+i;
     141      n++;
     142    }
     143    // slack - make sure first branch is on slack
     144    weights[n]=1000.0;
     145    which[n]=180+iSet;
     146    n++;
     147    starts[iSet+1]=n;
     148  }
    113149  for (i=0;i<numberIntegers;i++) {
    114150    int iColumn = integerVariable[i];
     
    116152    model.solver()->setContinuous(iColumn);
    117153  }
     154  // save model in this state
     155  CbcModel modelSOS = model;
     156  CbcObject ** objects = new CbcObject * [numberSets];
    118157  for (i=0;i<numberSets;i++) {
    119158    objects[i]= new CbcSOS(&model,starts[i+1]-starts[i],which+starts[i],
     
    129168  model.branchAndBound();
    130169
    131   std::cout<<"ltw.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
     170  std::cout<<"rgn.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
    132171           <<model.getNodeCount()<<" nodes with objective "
    133172           <<model.getObjValue()
     
    151190
    152191
    153   // Restore model
    154   model = model2;
     192  // Restore SOS model
     193  model = modelSOS;
    155194
    156195// Now use SOS2
    157196  objects = new CbcObject * [numberSets];
    158   for (i=0;i<numberIntegers;i++) {
    159     int iColumn = integerVariable[i];
    160     // Stop being integer
    161     model.solver()->setContinuous(iColumn);
    162   }
    163197  for (i=0;i<numberSets;i++) {
    164198    objects[i]= new CbcSOS(&model,starts[i+1]-starts[i],which+starts[i],
     
    174208  model.branchAndBound();
    175209
    176   std::cout<<"ltw.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
     210  std::cout<<"rgn.mps"<<" took "<<CoinCpuTime()-time1<<" seconds, "
    177211           <<model.getNodeCount()<<" nodes with objective "
    178212           <<model.getObjValue()
Note: See TracChangeset for help on using the changeset viewer.