Ignore:
Timestamp:
Jan 6, 2019 6:17:46 PM (4 months ago)
Author:
unxusr
Message:

formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/examples/qmip2.cpp

    r1898 r2469  
    1010// For Branch and bound
    1111#include "OsiClpSolverInterface.hpp"
    12 #include "CbcModel.hpp" 
     12#include "CbcModel.hpp"
    1313#include "CbcCutGenerator.hpp"
    1414#include "CoinHelperFunctions.hpp"
     
    2121// For saying about solution validity
    2222#include "OsiAuxInfo.hpp"
    23 
    2423
    2524// Time
     
    2726// Class to disallow strong branching solutions
    2827#include "CbcFeasibilityBase.hpp"
    29 class CbcFeasibilityNoStrong : public CbcFeasibilityBase{
     28class CbcFeasibilityNoStrong : public CbcFeasibilityBase {
    3029public:
    31   // Default Constructor 
    32   CbcFeasibilityNoStrong () {}
     30  // Default Constructor
     31  CbcFeasibilityNoStrong() {}
    3332
    3433  virtual ~CbcFeasibilityNoStrong() {}
    35   // Copy constructor
    36   CbcFeasibilityNoStrong ( const CbcFeasibilityNoStrong &rhs) {}
    37    
    38   // Assignment operator
    39   CbcFeasibilityNoStrong & operator=( const CbcFeasibilityNoStrong& rhs)
    40   { return * this;}
     34  // Copy constructor
     35  CbcFeasibilityNoStrong(const CbcFeasibilityNoStrong &rhs) {}
     36
     37  // Assignment operator
     38  CbcFeasibilityNoStrong &operator=(const CbcFeasibilityNoStrong &rhs)
     39  {
     40    return *this;
     41  }
    4142
    4243  /// Clone
    43   virtual CbcFeasibilityBase * clone() const
    44   { return new CbcFeasibilityNoStrong();}
     44  virtual CbcFeasibilityBase *clone() const
     45  {
     46    return new CbcFeasibilityNoStrong();
     47  }
    4548
    4649  /**
     
    5356     1 pretend integer solution
    5457  */
    55   virtual int feasible(CbcModel * model, int mode)
    56   {return mode;}
     58  virtual int feasible(CbcModel *model, int mode)
     59  {
     60    return mode;
     61  }
    5762};
    58 
    5963
    6064/************************************************************************
     
    7983************************************************************************/
    8084
    81 int main (int argc, const char *argv[])
     85int main(int argc, const char *argv[])
    8286{
    8387
    8488  // Define a Solver which inherits from OsiClpsolverInterface -> OsiSolverInterface
    85  
     89
    8690  OsiClpSolverInterface solver1;
    8791
    88   int nX=4;
    89 
    90   int nY = (nX * (nX-1)/2);
     92  int nX = 4;
     93
     94  int nY = (nX * (nX - 1) / 2);
    9195  // All columns
    92   double * obj = new double [nX+nY];
    93   double * clo = new double[nX+nY];
    94   double * cup = new double[nX+nY];
     96  double *obj = new double[nX + nY];
     97  double *clo = new double[nX + nY];
     98  double *cup = new double[nX + nY];
    9599  int i;
    96   for (i=0;i<nX;i++) {
    97     obj[i] = -(i+1);
    98     clo[i]=0.0;
    99     cup[i]=1.0;
    100   }
    101   for (i=nX;i<nX+nY;i++) {
     100  for (i = 0; i < nX; i++) {
     101    obj[i] = -(i + 1);
     102    clo[i] = 0.0;
     103    cup[i] = 1.0;
     104  }
     105  for (i = nX; i < nX + nY; i++) {
    102106    obj[i] = 0.0;
    103     clo[i]=0.0;
    104     cup[i]=1.0;
     107    clo[i] = 0.0;
     108    cup[i] = 1.0;
    105109  }
    106110  // Just ordinary rows
    107   int nRow = 1+nX;
    108   double * rlo = new double[nRow];
    109   double * rup = new double[nRow];
    110   for (i=0;i<nRow;i++) {
    111     rlo[i]=-COIN_DBL_MAX;
    112     rup[i]=1.0;
     111  int nRow = 1 + nX;
     112  double *rlo = new double[nRow];
     113  double *rup = new double[nRow];
     114  for (i = 0; i < nRow; i++) {
     115    rlo[i] = -COIN_DBL_MAX;
     116    rup[i] = 1.0;
    113117  }
    114118  // and first row
    115   rup[0]=nX/2.0;
     119  rup[0] = nX / 2.0;
    116120  // Matrix
    117   int nEl = nX+nX*nX;
    118   int * row = new int[nEl];
    119   int * col = new int[nEl];
    120   double * el = new double[nEl];
     121  int nEl = nX + nX * nX;
     122  int *row = new int[nEl];
     123  int *col = new int[nEl];
     124  double *el = new double[nEl];
    121125  // X
    122   nEl=0;
     126  nEl = 0;
    123127  // May need scale to make plausible
    124128  double scaleFactor = 1.0;
    125   for (i=0;i<nX;i++) {
    126     row[nEl]=0;
    127     col[nEl]=i;
    128     el[nEl++]=1.0;
     129  for (i = 0; i < nX; i++) {
     130    row[nEl] = 0;
     131    col[nEl] = i;
     132    el[nEl++] = 1.0;
    129133    // and diagonal
    130     row[nEl]=i+1;
    131     col[nEl]=i;
    132     double value = CoinDrand48()*scaleFactor;
     134    row[nEl] = i + 1;
     135    col[nEl] = i;
     136    double value = CoinDrand48() * scaleFactor;
    133137    // make reasonable (so multiples of 0.000001)
    134138    value *= 1.0e6;
    135     int iValue = (int) (value+1.0);
     139    int iValue = (int)(value + 1.0);
    136140    value = iValue;
    137141    value *= 1.0e-6;
    138     el[nEl++]=value;
     142    el[nEl++] = value;
    139143  }
    140144  // Y
     
    142146  // And stored cuts
    143147  CglStored stored;
    144   double cutEls[3]={1.0,1.0,-1.0};
     148  double cutEls[3] = { 1.0, 1.0, -1.0 };
    145149  int cutIndices[3];
    146   for (i=0;i<nX;i++) {
    147     cutIndices[0]=i;
    148     for (int j=i+1;j<nX;j++) {
    149       cutIndices[1]=j;
    150       cutIndices[2]=nY;
     150  for (i = 0; i < nX; i++) {
     151    cutIndices[0] = i;
     152    for (int j = i + 1; j < nX; j++) {
     153      cutIndices[1] = j;
     154      cutIndices[2] = nY;
    151155      // add cut
    152       stored.addCut(-COIN_DBL_MAX,1.0,3,cutIndices,cutEls);
    153       row[nEl]=i+1;
    154       col[nEl]=nY;
    155       double value = CoinDrand48()*scaleFactor;
     156      stored.addCut(-COIN_DBL_MAX, 1.0, 3, cutIndices, cutEls);
     157      row[nEl] = i + 1;
     158      col[nEl] = nY;
     159      double value = CoinDrand48() * scaleFactor;
    156160      // multiply to make ones with most negative objective violated
    157161      // make reasonable (so multiples of 0.000001)
    158       value *= 1.0e6+1.0e6*j;
    159       int iValue = (int) (value+1.0);
     162      value *= 1.0e6 + 1.0e6 * j;
     163      int iValue = (int)(value + 1.0);
    160164      value = iValue;
    161165      value *= 1.0e-6;
    162       el[nEl++]=value;
     166      el[nEl++] = value;
    163167      // and other
    164       if (i!=j) {
    165         row[nEl]=j+1;
    166         col[nEl]=nY;
    167         el[nEl++]=value;
     168      if (i != j) {
     169        row[nEl] = j + 1;
     170        col[nEl] = nY;
     171        el[nEl++] = value;
    168172      }
    169173      nY++;
     
    173177  CoinPackedMatrix matrix(true, row, col, el, nEl);
    174178  solver1.loadProblem(matrix, clo, cup, obj, rlo, rup);
    175   delete [] obj;
    176   delete [] clo;
    177   delete [] cup;
    178   delete [] rlo;
    179   delete [] rup;
    180   delete [] row;
    181   delete [] col;
    182   delete [] el;
     179  delete[] obj;
     180  delete[] clo;
     181  delete[] cup;
     182  delete[] rlo;
     183  delete[] rup;
     184  delete[] row;
     185  delete[] col;
     186  delete[] el;
    183187  // Integers
    184   for (i=0;i<nX;i++)
     188  for (i = 0; i < nX; i++)
    185189    solver1.setInteger(i);
    186190  // Reduce printout
    187   solver1.setHintParam(OsiDoReducePrint,true,OsiHintTry);
    188   // This clones solver 
     191  solver1.setHintParam(OsiDoReducePrint, true, OsiHintTry);
     192  // This clones solver
    189193  CbcModel model(solver1);
    190194  // Add stored cuts (making sure at all depths)
    191   model.addCutGenerator(&stored,1,"Stored",true,false,false,-100,1,-1);
     195  model.addCutGenerator(&stored, 1, "Stored", true, false, false, -100, 1, -1);
    192196  /*  You need the next few lines -
    193197      a) so that cut generator will always be called again if it generated cuts
     
    208212  // Say don't recompute solution d)
    209213  model.setSpecialOptions(4);
    210  
     214
    211215  double time1 = CoinCpuTime();
    212216
    213217  // Do complete search
    214  
     218
    215219  model.branchAndBound();
    216220
    217   std::cout<<argv[1]<<" took "<<CoinCpuTime()-time1<<" seconds, "
    218            <<model.getNodeCount()<<" nodes with objective "
    219            <<model.getObjValue()
    220            <<(!model.status() ? " Finished" : " Not finished")
    221            <<std::endl;
     221  std::cout << argv[1] << " took " << CoinCpuTime() - time1 << " seconds, "
     222            << model.getNodeCount() << " nodes with objective "
     223            << model.getObjValue()
     224            << (!model.status() ? " Finished" : " Not finished")
     225            << std::endl;
    222226
    223227  // Print solution if finished - we can't get names from Osi!
    224228
    225   if (!model.status()&&model.getMinimizationObjValue()<1.0e50) {
     229  if (!model.status() && model.getMinimizationObjValue() < 1.0e50) {
    226230    int numberColumns = model.solver()->getNumCols();
    227    
     231
    228232    //const double * solution = model.bestSolution();
    229     const double * solution = model.solver()->getColSolution();
    230    
     233    const double *solution = model.solver()->getColSolution();
     234
    231235    int iColumn;
    232     for (iColumn=0;iColumn<numberColumns;iColumn++) {
    233       double value=solution[iColumn];
    234       if (fabs(value)>1.0e-7&&model.solver()->isInteger(iColumn))
    235         printf("Column %d has value %g\n",iColumn,value);
     236    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
     237      double value = solution[iColumn];
     238      if (fabs(value) > 1.0e-7 && model.solver()->isInteger(iColumn))
     239        printf("Column %d has value %g\n", iColumn, value);
    236240    }
    237241  }
    238242  return 0;
    239 }   
     243}
Note: See TracChangeset for help on using the changeset viewer.