Changeset 2469


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

formatting

Location:
trunk/Cbc
Files:
58 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/MSVisualStudio/v10/CbcWithInstalledLibraries/main.cpp

    r1989 r2469  
    55#include <cstdio>
    66
    7 int main() {
     7int main()
     8{
    89  const int numcols = 2;
    910  const int numrows = 1;
    10   double obj[] = { 1.0, 1.0}; // obj: Max x0 + x1
    11  
     11  double obj[] = { 1.0, 1.0 }; // obj: Max x0 + x1
     12
    1213  // Column-major sparse "A" matrix: x0 + 2 x1 <= 3.9
    13   int start[] = {0, 1, 2};      // where in index columns start (?)
    14   int index[] = {0, 0};        // row indexs for the columns
    15   double values[] = {1.0, 2.0}; // the values in the sparse matrix
    16   double rowlb[]  = {0.0};
    17   double rowub[]  = {3.9};
     14  int start[] = { 0, 1, 2 }; // where in index columns start (?)
     15  int index[] = { 0, 0 }; // row indexs for the columns
     16  double values[] = { 1.0, 2.0 }; // the values in the sparse matrix
     17  double rowlb[] = { 0.0 };
     18  double rowub[] = { 3.9 };
    1819
    1920  //          0 <= x0 <= 10 and integer
    2021  //          0 <= x1 <= 10
    21   double collb[] = {0.0, 0.0};
    22   double colub[] = {10.0, 10.0};
    23        
     22  double collb[] = { 0.0, 0.0 };
     23  double colub[] = { 10.0, 10.0 };
     24
    2425  OsiClpSolverInterface model;
    25   model.loadProblem(numcols, numrows, start, index, values, 
    26                     collb, colub, obj, rowlb, rowub);
     26  model.loadProblem(numcols, numrows, start, index, values,
     27    collb, colub, obj, rowlb, rowub);
    2728  model.setInteger(0); // Sets x0 to integer
    2829  model.setObjSense(-1.0); // Maximise
  • trunk/Cbc/examples/CbcBranchFollow2.cpp

    r1898 r2469  
    1616#include "CoinSort.hpp"
    1717#include "CoinError.hpp"
    18 // Default Constructor 
    19 CbcFollowOn2::CbcFollowOn2 ()
    20   : CbcObject(),
    21     rhs_(NULL),
    22     maximumRhs_(1)
     18// Default Constructor
     19CbcFollowOn2::CbcFollowOn2()
     20  : CbcObject()
     21  , rhs_(NULL)
     22  , maximumRhs_(1)
    2323{
    2424}
    2525
    2626// Useful constructor
    27 CbcFollowOn2::CbcFollowOn2 (CbcModel * model)
     27CbcFollowOn2::CbcFollowOn2(CbcModel *model)
    2828  : CbcObject(model)
    2929{
    30   assert (model);
    31   OsiSolverInterface * solver = model_->solver();
     30  assert(model);
     31  OsiSolverInterface *solver = model_->solver();
    3232  matrix_ = *solver->getMatrixByCol();
    3333  matrix_.removeGaps();
    3434  matrixByRow_ = *solver->getMatrixByRow();
    3535  int numberRows = matrix_.getNumRows();
    36   maximumRhs_ =1;
    37  
     36  maximumRhs_ = 1;
     37
    3838  rhs_ = new int[numberRows];
    3939  int i;
    40   const double * rowLower = solver->getRowLower();
    41   const double * rowUpper = solver->getRowUpper();
     40  const double *rowLower = solver->getRowLower();
     41  const double *rowUpper = solver->getRowUpper();
    4242  // Row copy
    43   const double * elementByRow = matrixByRow_.getElements();
    44   const int * column = matrixByRow_.getIndices();
    45   const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts();
    46   const int * rowLength = matrixByRow_.getVectorLengths();
    47   for (i=0;i<numberRows;i++) {
    48     rhs_[i]=0;
     43  const double *elementByRow = matrixByRow_.getElements();
     44  const int *column = matrixByRow_.getIndices();
     45  const CoinBigIndex *rowStart = matrixByRow_.getVectorStarts();
     46  const int *rowLength = matrixByRow_.getVectorLengths();
     47  for (i = 0; i < numberRows; i++) {
     48    rhs_[i] = 0;
    4949    double value = rowLower[i];
    50     if (value==rowUpper[i]) {
    51       if (floor(value)==value&&value>=1.0&&value<100.0) {
    52         // check elements
    53         bool good=true;
    54         for (int j=rowStart[i];j<rowStart[i]+rowLength[i];j++) {
    55           int iColumn = column[j];
    56           if (!solver->isInteger(iColumn))
    57             good=false;
    58           double elValue = elementByRow[j];
    59           if (floor(elValue)!=elValue||elValue<1.0)
    60             good=false;
    61         }
    62         if (good)
    63           rhs_[i]=(int) value;
    64       }
    65     }
    66   }
    67 }
    68 
    69 // Copy constructor 
    70 CbcFollowOn2::CbcFollowOn2 ( const CbcFollowOn2 & rhs)
    71   :CbcObject(rhs),
    72    matrix_(rhs.matrix_),
    73    matrixByRow_(rhs.matrixByRow_),
    74   maximumRhs_(rhs.maximumRhs_)
     50    if (value == rowUpper[i]) {
     51      if (floor(value) == value && value >= 1.0 && value < 100.0) {
     52        // check elements
     53        bool good = true;
     54        for (int j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) {
     55          int iColumn = column[j];
     56          if (!solver->isInteger(iColumn))
     57            good = false;
     58          double elValue = elementByRow[j];
     59          if (floor(elValue) != elValue || elValue < 1.0)
     60            good = false;
     61        }
     62        if (good)
     63          rhs_[i] = (int)value;
     64      }
     65    }
     66  }
     67}
     68
     69// Copy constructor
     70CbcFollowOn2::CbcFollowOn2(const CbcFollowOn2 &rhs)
     71  : CbcObject(rhs)
     72  , matrix_(rhs.matrix_)
     73  , matrixByRow_(rhs.matrixByRow_)
     74  , maximumRhs_(rhs.maximumRhs_)
    7575{
    7676  int numberRows = matrix_.getNumRows();
    77   rhs_= CoinCopyOfArray(rhs.rhs_,numberRows);
     77  rhs_ = CoinCopyOfArray(rhs.rhs_, numberRows);
    7878}
    7979
     
    8585}
    8686
    87 // Assignment operator 
    88 CbcFollowOn2 & 
    89 CbcFollowOn2::operator=( const CbcFollowOn2& rhs)
    90 {
    91   if (this!=&rhs) {
     87// Assignment operator
     88CbcFollowOn2 &
     89CbcFollowOn2::operator=(const CbcFollowOn2 &rhs)
     90{
     91  if (this != &rhs) {
    9292    CbcObject::operator=(rhs);
    93     delete [] rhs_;
     93    delete[] rhs_;
    9494    matrix_ = rhs.matrix_;
    9595    matrixByRow_ = rhs.matrixByRow_;
    9696    int numberRows = matrix_.getNumRows();
    97     rhs_= CoinCopyOfArray(rhs.rhs_,numberRows);
     97    rhs_ = CoinCopyOfArray(rhs.rhs_, numberRows);
    9898    maximumRhs_ = rhs.maximumRhs_;
    9999  }
     
    101101}
    102102
    103 // Destructor 
    104 CbcFollowOn2::~CbcFollowOn2 ()
    105 {
    106   delete [] rhs_;
     103// Destructor
     104CbcFollowOn2::~CbcFollowOn2()
     105{
     106  delete[] rhs_;
    107107}
    108108/* As some computation is needed in more than one place - returns row.
    109109   Also returns other row and effective rhs (so we can know if cut)
    110110*/
    111 int
    112 CbcFollowOn2::gutsOfFollowOn2(int & otherRow, int & preferredWay,
    113                               int & effectiveRhs) const
    114 {
    115   int whichRow=-1;
    116   otherRow=-1;
     111int CbcFollowOn2::gutsOfFollowOn2(int &otherRow, int &preferredWay,
     112  int &effectiveRhs) const
     113{
     114  int whichRow = -1;
     115  otherRow = -1;
    117116  int numberRows = matrix_.getNumRows();
    118  
     117
    119118  int i;
    120119  // For sorting
    121   int * sort = new int [numberRows];
    122   int * isort = new int [numberRows];
     120  int *sort = new int[numberRows];
     121  int *isort = new int[numberRows];
    123122  // Column copy
    124123  //const double * element = matrix_.getElements();
    125   const int * row = matrix_.getIndices();
    126   const CoinBigIndex * columnStart = matrix_.getVectorStarts();
    127   const int * columnLength = matrix_.getVectorLengths();
     124  const int *row = matrix_.getIndices();
     125  const CoinBigIndex *columnStart = matrix_.getVectorStarts();
     126  const int *columnLength = matrix_.getVectorLengths();
    128127  // Row copy
    129   const double * elementByRow = matrixByRow_.getElements();
    130   const int * column = matrixByRow_.getIndices();
    131   const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts();
    132   const int * rowLength = matrixByRow_.getVectorLengths();
    133   OsiSolverInterface * solver = model_->solver();
    134   const double * columnLower = solver->getColLower();
    135   const double * columnUpper = solver->getColUpper();
    136   const double * solution = solver->getColSolution();
     128  const double *elementByRow = matrixByRow_.getElements();
     129  const int *column = matrixByRow_.getIndices();
     130  const CoinBigIndex *rowStart = matrixByRow_.getVectorStarts();
     131  const int *rowLength = matrixByRow_.getVectorLengths();
     132  OsiSolverInterface *solver = model_->solver();
     133  const double *columnLower = solver->getColLower();
     134  const double *columnUpper = solver->getColUpper();
     135  const double *solution = solver->getColSolution();
    137136  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
    138   int nSort=0;
    139   for (i=0;i<numberRows;i++) {
     137  int nSort = 0;
     138  for (i = 0; i < numberRows; i++) {
    140139    if (rhs_[i]) {
    141140      // check elements
    142       double smallest=1.0e10;
    143       double largest=0.0;
    144       int rhsValue=rhs_[i];
    145       int number1=0;
    146       int numberOther=0;
    147       int numberUnsatisfied=0;
    148       for (int j=rowStart[i];j<rowStart[i]+rowLength[i];j++) {
    149         int iColumn = column[j];
    150         double value = elementByRow[j];
    151         double solValue = solution[iColumn];
    152         if (columnLower[iColumn]!=columnUpper[iColumn]) {
    153           smallest = CoinMin(smallest,value);
    154           largest = CoinMax(largest,value);
    155           if (value==1.0)
    156             number1++;
     141      double smallest = 1.0e10;
     142      double largest = 0.0;
     143      int rhsValue = rhs_[i];
     144      int number1 = 0;
     145      int numberOther = 0;
     146      int numberUnsatisfied = 0;
     147      for (int j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) {
     148        int iColumn = column[j];
     149        double value = elementByRow[j];
     150        double solValue = solution[iColumn];
     151        if (columnLower[iColumn] != columnUpper[iColumn]) {
     152          smallest = CoinMin(smallest, value);
     153          largest = CoinMax(largest, value);
     154          if (value == 1.0)
     155            number1++;
    157156          else
    158157            numberOther++;
    159           if (fabs(floor(solValue+0.5)-solValue)>integerTolerance)
    160             numberUnsatisfied++;
    161         } else {
    162           rhsValue -= (int)(value*floor(solValue+0.5));
    163         }
    164       }
    165       if (numberUnsatisfied>1) {
    166         if (smallest<largest) {
     158          if (fabs(floor(solValue + 0.5) - solValue) > integerTolerance)
     159            numberUnsatisfied++;
     160        } else {
     161          rhsValue -= (int)(value * floor(solValue + 0.5));
     162        }
     163      }
     164      if (numberUnsatisfied > 1) {
     165        if (smallest < largest) {
    167166#if 0
    168167          if  (largest>rhsValue)
     
    171170            printf("could fix\n");
    172171#endif
    173           if (rhsValue<=maximumRhs_&&0) {
     172          if (rhsValue <= maximumRhs_ && 0) {
    174173            // will mean a cut but worth trying
    175             sort[nSort]=i;
    176             isort[nSort++]=100000-numberUnsatisfied;
    177           }
    178         } else if (largest==rhsValue) {
    179           sort[nSort]=i;
    180           isort[nSort++]=-numberUnsatisfied;
    181         }
    182       }
    183     }
    184   }
    185   if (nSort>1) {
    186     CoinSort_2(isort,isort+nSort,sort);
    187     assert (isort[1]<0);
    188     CoinZeroN(isort,numberRows);
    189     double * other = new double[numberRows];
    190     CoinZeroN(other,numberRows);
    191     int * which = new int[numberRows];
     174            sort[nSort] = i;
     175            isort[nSort++] = 100000 - numberUnsatisfied;
     176          }
     177        } else if (largest == rhsValue) {
     178          sort[nSort] = i;
     179          isort[nSort++] = -numberUnsatisfied;
     180        }
     181      }
     182    }
     183  }
     184  if (nSort > 1) {
     185    CoinSort_2(isort, isort + nSort, sort);
     186    assert(isort[1] < 0);
     187    CoinZeroN(isort, numberRows);
     188    double *other = new double[numberRows];
     189    CoinZeroN(other, numberRows);
     190    int *which = new int[numberRows];
    192191    //#define COUNT
    193192#ifndef COUNT
    194     bool beforeSolution = model_->getSolutionCount()==0;
    195 #endif
    196     for (int k=0;k<nSort-1;k++) {
    197       i=sort[k];
     193    bool beforeSolution = model_->getSolutionCount() == 0;
     194#endif
     195    for (int k = 0; k < nSort - 1; k++) {
     196      i = sort[k];
    198197      int numberUnsatisfied = 0;
    199       int n=0;
     198      int n = 0;
    200199      int j;
    201       for (j=rowStart[i];j<rowStart[i]+rowLength[i];j++) {
    202         int iColumn = column[j];
    203         if (columnLower[iColumn]!=columnUpper[iColumn]) {
    204           double solValue = solution[iColumn]-columnLower[iColumn];
    205           if (fabs(floor(solValue+0.5)-solValue)>integerTolerance) {
    206             numberUnsatisfied++;
    207             for (int jj=columnStart[iColumn];jj<columnStart[iColumn]+columnLength[iColumn];jj++) {
    208               int iRow = row[jj];
    209               if (rhs_[iRow]) {
    210                 other[iRow]+=solValue;
    211                 if (isort[iRow]) {
    212                   isort[iRow]++;
    213                 } else {
    214                   isort[iRow]=1;
    215                   which[n++]=iRow;
    216                 }
    217               }
    218             }
    219           }
    220         }
    221       }
    222       double total=0.0;
     200      for (j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) {
     201        int iColumn = column[j];
     202        if (columnLower[iColumn] != columnUpper[iColumn]) {
     203          double solValue = solution[iColumn] - columnLower[iColumn];
     204          if (fabs(floor(solValue + 0.5) - solValue) > integerTolerance) {
     205            numberUnsatisfied++;
     206            for (int jj = columnStart[iColumn]; jj < columnStart[iColumn] + columnLength[iColumn]; jj++) {
     207              int iRow = row[jj];
     208              if (rhs_[iRow]) {
     209                other[iRow] += solValue;
     210                if (isort[iRow]) {
     211                  isort[iRow]++;
     212                } else {
     213                  isort[iRow] = 1;
     214                  which[n++] = iRow;
     215                }
     216              }
     217            }
     218          }
     219        }
     220      }
     221      double total = 0.0;
    223222      // Take out row
    224       double sumThis=other[i];
    225       other[i]=0.0;
    226       assert (numberUnsatisfied==isort[i]);
     223      double sumThis = other[i];
     224      other[i] = 0.0;
     225      assert(numberUnsatisfied == isort[i]);
    227226      // find one nearest half if solution, one if before solution
    228       int iBest=-1;
    229       double dtarget=0.5*total;
     227      int iBest = -1;
     228      double dtarget = 0.5 * total;
    230229#ifdef COUNT
    231       int target = (numberUnsatisfied+1)>>1;
    232       int best=numberUnsatisfied;
     230      int target = (numberUnsatisfied + 1) >> 1;
     231      int best = numberUnsatisfied;
    233232#else
    234233      double best;
    235234      if (beforeSolution)
    236         best=dtarget;
     235        best = dtarget;
    237236      else
    238         best=1.0e30;
    239 #endif
    240       for (j=0;j<n;j++) {
    241         int iRow = which[j];
    242         double dvalue=other[iRow];
    243         other[iRow]=0.0;
     237        best = 1.0e30;
     238#endif
     239      for (j = 0; j < n; j++) {
     240        int iRow = which[j];
     241        double dvalue = other[iRow];
     242        other[iRow] = 0.0;
    244243#ifdef COUNT
    245         int value = isort[iRow];
    246 #endif
    247         isort[iRow]=0;
    248         if (fabs(dvalue)<1.0e-8||fabs(sumThis-dvalue)<1.0e-8)
     244        int value = isort[iRow];
     245#endif
     246        isort[iRow] = 0;
     247        if (fabs(dvalue) < 1.0e-8 || fabs(sumThis - dvalue) < 1.0e-8)
    249248          continue;
    250         if (fabs(floor(dvalue+0.5)-dvalue)<integerTolerance)
    251           continue;
     249        if (fabs(floor(dvalue + 0.5) - dvalue) < integerTolerance)
     250          continue;
    252251        dvalue -= floor(dvalue);
    253252#ifdef COUNT
    254         if (abs(value-target)<best&&value!=numberUnsatisfied) {
    255           best=abs(value-target);
    256           iBest=iRow;
    257           if (dvalue<dtarget)
    258             preferredWay=1;
    259           else
    260             preferredWay=-1;
    261         }
     253        if (abs(value - target) < best && value != numberUnsatisfied) {
     254          best = abs(value - target);
     255          iBest = iRow;
     256          if (dvalue < dtarget)
     257            preferredWay = 1;
     258          else
     259            preferredWay = -1;
     260        }
    262261#else
    263         if (beforeSolution) {
    264           if (fabs(dvalue-dtarget)>best) {
    265             best = fabs(dvalue-dtarget);
    266             iBest=iRow;
    267             if (dvalue<dtarget)
    268               preferredWay=1;
    269             else
    270               preferredWay=-1;
    271           }
    272         } else {
    273           if (fabs(dvalue-dtarget)<best) {
    274             best = fabs(dvalue-dtarget);
    275             iBest=iRow;
    276             if (dvalue<dtarget)
    277               preferredWay=1;
    278             else
    279               preferredWay=-1;
    280           }
    281         }
    282 #endif
    283       }
    284       if (iBest>=0) {
    285         whichRow=i;
    286         otherRow=iBest;
     262        if (beforeSolution) {
     263          if (fabs(dvalue - dtarget) > best) {
     264            best = fabs(dvalue - dtarget);
     265            iBest = iRow;
     266            if (dvalue < dtarget)
     267              preferredWay = 1;
     268            else
     269              preferredWay = -1;
     270          }
     271        } else {
     272          if (fabs(dvalue - dtarget) < best) {
     273            best = fabs(dvalue - dtarget);
     274            iBest = iRow;
     275            if (dvalue < dtarget)
     276              preferredWay = 1;
     277            else
     278              preferredWay = -1;
     279          }
     280        }
     281#endif
     282      }
     283      if (iBest >= 0) {
     284        whichRow = i;
     285        otherRow = iBest;
    287286        //printf("Rows %d (%d) and %d (%d)\n",whichRow,rhs_[whichRow],
    288287        //     otherRow,rhs_[otherRow]);
    289         break;
    290       }
    291     }
    292     delete [] which;
    293     delete [] other;
    294   }
    295   delete [] sort;
    296   delete [] isort;
     288        break;
     289      }
     290    }
     291    delete[] which;
     292    delete[] other;
     293  }
     294  delete[] sort;
     295  delete[] isort;
    297296  return whichRow;
    298297}
    299298
    300299// Infeasibility - large is 0.5
    301 double 
    302 CbcFollowOn2::infeasibility(int & preferredWay) const
    303 {
    304   int otherRow=0;
     300double
     301CbcFollowOn2::infeasibility(int &preferredWay) const
     302{
     303  int otherRow = 0;
    305304  int effectiveRhs;
    306   int whichRow = gutsOfFollowOn2(otherRow,preferredWay,effectiveRhs);
    307   if (whichRow<0) {
     305  int whichRow = gutsOfFollowOn2(otherRow, preferredWay, effectiveRhs);
     306  if (whichRow < 0) {
    308307    return 0.0;
    309308  } else {
    310     assert (whichRow!=otherRow);
    311     return 2.0* model_->getDblParam(CbcModel::CbcIntegerTolerance);
     309    assert(whichRow != otherRow);
     310    return 2.0 * model_->getDblParam(CbcModel::CbcIntegerTolerance);
    312311  }
    313312}
    314313
    315314// This looks at solution and sets bounds to contain solution
    316 void
    317 CbcFollowOn2::feasibleRegion()
    318 {
    319 }
    320 
     315void CbcFollowOn2::feasibleRegion()
     316{
     317}
    321318
    322319// Creates a branching object
    323 CbcBranchingObject * 
    324 CbcFollowOn2::createBranch(int way) 
    325 {
    326   int otherRow=0;
     320CbcBranchingObject *
     321CbcFollowOn2::createBranch(int way)
     322{
     323  int otherRow = 0;
    327324  int preferredWay;
    328325  int effectiveRhs;
    329   int whichRow = gutsOfFollowOn2(otherRow,preferredWay,effectiveRhs);
    330   assert(way==preferredWay);
    331   assert (whichRow>=0);
     326  int whichRow = gutsOfFollowOn2(otherRow, preferredWay, effectiveRhs);
     327  assert(way == preferredWay);
     328  assert(whichRow >= 0);
    332329  int numberColumns = matrix_.getNumCols();
    333  
     330
    334331  // Column copy
    335332  //const double * element = matrix_.getElements();
    336   const int * row = matrix_.getIndices();
    337   const CoinBigIndex * columnStart = matrix_.getVectorStarts();
    338   const int * columnLength = matrix_.getVectorLengths();
     333  const int *row = matrix_.getIndices();
     334  const CoinBigIndex *columnStart = matrix_.getVectorStarts();
     335  const int *columnLength = matrix_.getVectorLengths();
    339336  // Row copy
    340337  //const double * elementByRow = matrixByRow_.getElements();
    341   const int * column = matrixByRow_.getIndices();
    342   const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts();
    343   const int * rowLength = matrixByRow_.getVectorLengths();
    344   OsiSolverInterface * solver = model_->solver();
    345   const double * columnLower = solver->getColLower();
    346   const double * columnUpper = solver->getColUpper();
     338  const int *column = matrixByRow_.getIndices();
     339  const CoinBigIndex *rowStart = matrixByRow_.getVectorStarts();
     340  const int *rowLength = matrixByRow_.getVectorLengths();
     341  OsiSolverInterface *solver = model_->solver();
     342  const double *columnLower = solver->getColLower();
     343  const double *columnUpper = solver->getColUpper();
    347344  //const double * solution = solver->getColSolution();
    348345#if 0
     
    452449  printf("%d free (but %d implicitly fixed of which %d nonzero), %d out of rhs\n",nFree,nImplicit,nFixedBut,nOut);
    453450#endif
    454   int nUp=0;
    455   int nDown=0;
    456   int * upList = new int[numberColumns];
    457   int * downList = new int[numberColumns];
     451  int nUp = 0;
     452  int nDown = 0;
     453  int *upList = new int[numberColumns];
     454  int *downList = new int[numberColumns];
    458455  int j;
    459   for (j=rowStart[whichRow];j<rowStart[whichRow]+rowLength[whichRow];j++) {
     456  for (j = rowStart[whichRow]; j < rowStart[whichRow] + rowLength[whichRow]; j++) {
    460457    int iColumn = column[j];
    461     if (columnLower[iColumn]!=columnUpper[iColumn]) {
    462       bool up=true;
    463       for (int jj=columnStart[iColumn];jj<columnStart[iColumn]+columnLength[iColumn];jj++) {
    464         int iRow = row[jj];
    465         if (iRow==otherRow) {
    466           up=false;
    467           break;
    468         }
     458    if (columnLower[iColumn] != columnUpper[iColumn]) {
     459      bool up = true;
     460      for (int jj = columnStart[iColumn]; jj < columnStart[iColumn] + columnLength[iColumn]; jj++) {
     461        int iRow = row[jj];
     462        if (iRow == otherRow) {
     463          up = false;
     464          break;
     465        }
    469466      }
    470467      if (up)
    471         upList[nUp++]=iColumn;
     468        upList[nUp++] = iColumn;
    472469      else
    473         downList[nDown++]=iColumn;
     470        downList[nDown++] = iColumn;
    474471    }
    475472  }
    476473  //printf("way %d\n",way);
    477474  // create object
    478   CbcBranchingObject * branch
    479      = new CbcFixingBranchingObject(model_,way,
    480                                          nDown,downList,nUp,upList);
    481   delete [] upList;
    482   delete [] downList;
     475  CbcBranchingObject *branch
     476    = new CbcFixingBranchingObject(model_, way,
     477      nDown, downList, nUp, upList);
     478  delete[] upList;
     479  delete[] downList;
    483480  return branch;
    484481}
  • trunk/Cbc/examples/CbcBranchFollow2.hpp

    r1574 r2469  
    99#include "CbcBranchActual.hpp"
    1010#include "CoinPackedMatrix.hpp"
    11 
    1211
    1312/** Define a follow on class.
     
    2726
    2827public:
    29 
    30   // Default Constructor
    31   CbcFollowOn2 ();
     28  // Default Constructor
     29  CbcFollowOn2();
    3230
    3331  /** Useful constructor
    3432  */
    35   CbcFollowOn2 (CbcModel * model);
    36  
    37   // Copy constructor 
    38   CbcFollowOn2 ( const CbcFollowOn2 &);
    39    
     33  CbcFollowOn2(CbcModel *model);
     34
     35  // Copy constructor
     36  CbcFollowOn2(const CbcFollowOn2 &);
     37
    4038  /// Clone
    41   virtual CbcObject * clone() const;
     39  virtual CbcObject *clone() const;
    4240
    43   // Assignment operator 
    44   CbcFollowOn2 & operator=( const CbcFollowOn2& rhs);
     41  // Assignment operator
     42  CbcFollowOn2 &operator=(const CbcFollowOn2 &rhs);
    4543
    46   // Destructor 
    47   ~CbcFollowOn2 ();
    48  
     44  // Destructor
     45  ~CbcFollowOn2();
     46
    4947  /// Infeasibility - large is 0.5
    50   virtual double infeasibility(int & preferredWay) const;
     48  virtual double infeasibility(int &preferredWay) const;
    5149
    5250  /// This looks at solution and sets bounds to contain solution
    5351  virtual void feasibleRegion();
    5452  /// Creates a branching object
    55   virtual CbcBranchingObject * createBranch(int way) ;
     53  virtual CbcBranchingObject *createBranch(int way);
    5654  /** As some computation is needed in more than one place - returns row.
    5755      Also returns other row and effective rhs (so we can know if cut)
    5856  */
    59   virtual int gutsOfFollowOn2(int & otherRow, int & preferredWay,
    60                               int & effectiveRhs) const;
     57  virtual int gutsOfFollowOn2(int &otherRow, int &preferredWay,
     58    int &effectiveRhs) const;
    6159
    6260  /// get and set for maximum rhws (affects cuts as branch)
    6361  inline int maximumRhs() const
    64   { return maximumRhs_;}
     62  {
     63    return maximumRhs_;
     64  }
    6565  inline void setMaximumRhs(int value)
    66   { maximumRhs_=value;}
     66  {
     67    maximumRhs_ = value;
     68  }
     69
    6770protected:
    6871  /// data
     
    7073  CoinPackedMatrix matrix_;
    7174  /// Matrix by row
    72   CoinPackedMatrix matrixByRow_; 
     75  CoinPackedMatrix matrixByRow_;
    7376  /// Possible rhs (if 0 then not possible)
    74   int * rhs_;
     77  int *rhs_;
    7578  /// If >1 then allow cuts if effective rhs <= this
    7679  int maximumRhs_;
  • trunk/Cbc/examples/CbcBranchLink.cpp

    r1900 r2469  
    1717#include "CoinPackedMatrix.hpp"
    1818
    19 // Default Constructor 
    20 CbcLink::CbcLink ()
    21   : CbcObject(),
    22     weights_(NULL),
    23     numberMembers_(0),
    24     numberLinks_(0),
    25     which_(NULL),
    26     sosType_(1)
     19// Default Constructor
     20CbcLink::CbcLink()
     21  : CbcObject()
     22  , weights_(NULL)
     23  , numberMembers_(0)
     24  , numberLinks_(0)
     25  , which_(NULL)
     26  , sosType_(1)
    2727{
    2828}
    2929
    3030// Useful constructor (which are indices)
    31 CbcLink::CbcLink (CbcModel * model, int numberMembers,
    32            int numberLinks, int first , const double * weights, int identifier)
    33   : CbcObject(model),
    34     numberMembers_(numberMembers),
    35     numberLinks_(numberLinks),
    36     which_(NULL),
    37     sosType_(1)
    38 {
    39   id_=identifier;
     31CbcLink::CbcLink(CbcModel *model, int numberMembers,
     32  int numberLinks, int first, const double *weights, int identifier)
     33  : CbcObject(model)
     34  , numberMembers_(numberMembers)
     35  , numberLinks_(numberLinks)
     36  , which_(NULL)
     37  , sosType_(1)
     38{
     39  id_ = identifier;
    4040  if (numberMembers_) {
    4141    weights_ = new double[numberMembers_];
    42     which_ = new int[numberMembers_*numberLinks_];
     42    which_ = new int[numberMembers_ * numberLinks_];
    4343    if (weights) {
    44       memcpy(weights_,weights,numberMembers_*sizeof(double));
     44      memcpy(weights_, weights, numberMembers_ * sizeof(double));
    4545    } else {
    46       for (int i=0;i<numberMembers_;i++)
    47         weights_[i]=i;
     46      for (int i = 0; i < numberMembers_; i++)
     47        weights_[i] = i;
    4848    }
    4949    // weights must be increasing
    5050    int i;
    51     for (i=0;i<numberMembers_;i++)
    52       assert (i == 0 || weights_[i]>weights_[i-1]+1.0e-12);
    53     for (i=0;i<numberMembers_*numberLinks_;i++) {
    54       which_[i]=first+i;
     51    for (i = 0; i < numberMembers_; i++)
     52      assert(i == 0 || weights_[i] > weights_[i - 1] + 1.0e-12);
     53    for (i = 0; i < numberMembers_ * numberLinks_; i++) {
     54      which_[i] = first + i;
    5555    }
    5656  } else {
     
    6060
    6161// Useful constructor (which are indices)
    62 CbcLink::CbcLink (CbcModel * model, int numberMembers,
    63            int numberLinks, int sosType, const int * which , const double * weights, int identifier)
    64   : CbcObject(model),
    65     numberMembers_(numberMembers),
    66     numberLinks_(numberLinks),
    67     which_(NULL),
    68     sosType_(sosType)
    69 {
    70   id_=identifier;
     62CbcLink::CbcLink(CbcModel *model, int numberMembers,
     63  int numberLinks, int sosType, const int *which, const double *weights, int identifier)
     64  : CbcObject(model)
     65  , numberMembers_(numberMembers)
     66  , numberLinks_(numberLinks)
     67  , which_(NULL)
     68  , sosType_(sosType)
     69{
     70  id_ = identifier;
    7171  if (numberMembers_) {
    7272    weights_ = new double[numberMembers_];
    73     which_ = new int[numberMembers_*numberLinks_];
     73    which_ = new int[numberMembers_ * numberLinks_];
    7474    if (weights) {
    75       memcpy(weights_,weights,numberMembers_*sizeof(double));
     75      memcpy(weights_, weights, numberMembers_ * sizeof(double));
    7676    } else {
    77       for (int i=0;i<numberMembers_;i++)
    78         weights_[i]=i;
     77      for (int i = 0; i < numberMembers_; i++)
     78        weights_[i] = i;
    7979    }
    8080    // weights must be increasing
    8181    int i;
    82     for (i=0;i<numberMembers_;i++)
    83       assert (i == 0 || weights_[i]>weights_[i-1]+1.0e-12);
    84     for (i=0;i<numberMembers_*numberLinks_;i++) {
    85       which_[i]= which[i];
     82    for (i = 0; i < numberMembers_; i++)
     83      assert(i == 0 || weights_[i] > weights_[i - 1] + 1.0e-12);
     84    for (i = 0; i < numberMembers_ * numberLinks_; i++) {
     85      which_[i] = which[i];
    8686    }
    8787  } else {
     
    9090}
    9191
    92 // Copy constructor 
    93 CbcLink::CbcLink ( const CbcLink & rhs)
    94   :CbcObject(rhs)
     92// Copy constructor
     93CbcLink::CbcLink(const CbcLink &rhs)
     94  : CbcObject(rhs)
    9595{
    9696  numberMembers_ = rhs.numberMembers_;
     
    9898  sosType_ = rhs.sosType_;
    9999  if (numberMembers_) {
    100     weights_ = CoinCopyOfArray(rhs.weights_,numberMembers_);
    101     which_ = CoinCopyOfArray(rhs.which_,numberMembers_*numberLinks_);
     100    weights_ = CoinCopyOfArray(rhs.weights_, numberMembers_);
     101    which_ = CoinCopyOfArray(rhs.which_, numberMembers_ * numberLinks_);
    102102  } else {
    103103    weights_ = NULL;
     
    113113}
    114114
    115 // Assignment operator 
    116 CbcLink & 
    117 CbcLink::operator=( const CbcLink& rhs)
    118 {
    119   if (this!=&rhs) {
     115// Assignment operator
     116CbcLink &
     117CbcLink::operator=(const CbcLink &rhs)
     118{
     119  if (this != &rhs) {
    120120    CbcObject::operator=(rhs);
    121     delete [] weights_;
    122     delete [] which_;
     121    delete[] weights_;
     122    delete[] which_;
    123123    numberMembers_ = rhs.numberMembers_;
    124124    numberLinks_ = rhs.numberLinks_;
    125125    sosType_ = rhs.sosType_;
    126126    if (numberMembers_) {
    127       weights_ = CoinCopyOfArray(rhs.weights_,numberMembers_);
    128       which_ = CoinCopyOfArray(rhs.which_,numberMembers_*numberLinks_);
     127      weights_ = CoinCopyOfArray(rhs.weights_, numberMembers_);
     128      which_ = CoinCopyOfArray(rhs.which_, numberMembers_ * numberLinks_);
    129129    } else {
    130130      weights_ = NULL;
     
    135135}
    136136
    137 // Destructor 
    138 CbcLink::~CbcLink ()
    139 {
    140   delete [] weights_;
    141   delete [] which_;
     137// Destructor
     138CbcLink::~CbcLink()
     139{
     140  delete[] weights_;
     141  delete[] which_;
    142142}
    143143
    144144// Infeasibility - large is 0.5
    145 double 
    146 CbcLink::infeasibility(int & preferredWay) const
     145double
     146CbcLink::infeasibility(int &preferredWay) const
    147147{
    148148  int j;
    149   int firstNonZero=-1;
     149  int firstNonZero = -1;
    150150  int lastNonZero = -1;
    151   OsiSolverInterface * solver = model_->solver();
    152   const double * solution = model_->testSolution();
     151  OsiSolverInterface *solver = model_->solver();
     152  const double *solution = model_->testSolution();
    153153  //const double * lower = solver->getColLower();
    154   const double * upper = solver->getColUpper();
    155   double integerTolerance =
    156     model_->getDblParam(CbcModel::CbcIntegerTolerance);
     154  const double *upper = solver->getColUpper();
     155  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
    157156  double weight = 0.0;
    158   double sum =0.0;
     157  double sum = 0.0;
    159158
    160159  // check bounds etc
    161   double lastWeight=-1.0e100;
    162   int base=0;
    163   for (j=0;j<numberMembers_;j++) {
    164     for (int k=0;k<numberLinks_;k++) {
    165       int iColumn = which_[base+k];
     160  double lastWeight = -1.0e100;
     161  int base = 0;
     162  for (j = 0; j < numberMembers_; j++) {
     163    for (int k = 0; k < numberLinks_; k++) {
     164      int iColumn = which_[base + k];
    166165      //if (lower[iColumn])
    167166      //throw CoinError("Non zero lower bound in CBCLink","infeasibility","CbcLink");
    168       if (lastWeight>=weights_[j]-1.0e-7)
    169         throw CoinError("Weights too close together in CBCLink","infeasibility","CbcLink");
    170       double value = CoinMax(0.0,solution[iColumn]);
     167      if (lastWeight >= weights_[j] - 1.0e-7)
     168        throw CoinError("Weights too close together in CBCLink", "infeasibility", "CbcLink");
     169      double value = CoinMax(0.0, solution[iColumn]);
    171170      sum += value;
    172       if (value>integerTolerance&&upper[iColumn]) {
     171      if (value > integerTolerance && upper[iColumn]) {
    173172        // Possibly due to scaling a fixed variable might slip through
    174         if (value>upper[iColumn]+1.0e-8) {
     173        if (value > upper[iColumn] + 1.0e-8) {
    175174          // Could change to #ifdef CBC_DEBUG
    176175#ifndef NDEBUG
    177           if (model_->messageHandler()->logLevel()>1)
     176          if (model_->messageHandler()->logLevel() > 1)
    178177            printf("** Variable %d (%d) has value %g and upper bound of %g\n",
    179                    iColumn,j,value,upper[iColumn]);
     178              iColumn, j, value, upper[iColumn]);
    180179#endif
    181         } 
    182         value = CoinMin(value,upper[iColumn]);
    183         weight += weights_[j]*value;
    184         if (firstNonZero<0)
    185           firstNonZero=j;
    186         lastNonZero=j;
     180        }
     181        value = CoinMin(value, upper[iColumn]);
     182        weight += weights_[j] * value;
     183        if (firstNonZero < 0)
     184          firstNonZero = j;
     185        lastNonZero = j;
    187186      }
    188187    }
     
    190189  }
    191190  double valueInfeasibility;
    192   preferredWay=1;
    193   if (lastNonZero-firstNonZero>=sosType_) {
     191  preferredWay = 1;
     192  if (lastNonZero - firstNonZero >= sosType_) {
    194193    // find where to branch
    195     assert (sum>0.0);
     194    assert(sum > 0.0);
    196195    weight /= sum;
    197     valueInfeasibility = lastNonZero-firstNonZero+1;
    198     valueInfeasibility *= 0.5/((double) numberMembers_);
     196    valueInfeasibility = lastNonZero - firstNonZero + 1;
     197    valueInfeasibility *= 0.5 / ((double)numberMembers_);
    199198    //#define DISTANCE
    200199#ifdef DISTANCE
    201     assert (sosType_==1); // code up
     200    assert(sosType_ == 1); // code up
    202201    /* may still be satisfied.
    203202       For LOS type 2 we might wish to move coding around
     
    205204    */
    206205    int iWhere;
    207     bool possible=false;
    208     for (iWhere=firstNonZero;iWhere<=lastNonZero;iWhere++) {
    209       if (fabs(weight-weights_[iWhere])<1.0e-8) {
    210         possible=true;
    211         break;
     206    bool possible = false;
     207    for (iWhere = firstNonZero; iWhere <= lastNonZero; iWhere++) {
     208      if (fabs(weight - weights_[iWhere]) < 1.0e-8) {
     209        possible = true;
     210        break;
    212211      }
    213212    }
    214213    if (possible) {
    215214      // One could move some of this (+ arrays) into model_
    216       const CoinPackedMatrix * matrix = solver->getMatrixByCol();
    217       const double * element = matrix->getMutableElements();
    218       const int * row = matrix->getIndices();
    219       const CoinBigIndex * columnStart = matrix->getVectorStarts();
    220       const int * columnLength = matrix->getVectorLengths();
    221       const double * rowSolution = solver->getRowActivity();
    222       const double * rowLower = solver->getRowLower();
    223       const double * rowUpper = solver->getRowUpper();
     215      const CoinPackedMatrix *matrix = solver->getMatrixByCol();
     216      const double *element = matrix->getMutableElements();
     217      const int *row = matrix->getIndices();
     218      const CoinBigIndex *columnStart = matrix->getVectorStarts();
     219      const int *columnLength = matrix->getVectorLengths();
     220      const double *rowSolution = solver->getRowActivity();
     221      const double *rowLower = solver->getRowLower();
     222      const double *rowUpper = solver->getRowUpper();
    224223      int numberRows = matrix->getNumRows();
    225       double * array = new double [numberRows];
    226       CoinZeroN(array,numberRows);
    227       int * which = new int [numberRows];
    228       int n=0;
    229       int base=numberLinks_*firstNonZero;
    230       for (j=firstNonZero;j<=lastNonZero;j++) {
    231         for (int k=0;k<numberLinks_;k++) {
    232           int iColumn = which_[base+k];
    233           double value = CoinMax(0.0,solution[iColumn]);
    234           if (value>integerTolerance&&upper[iColumn]) {
    235             value = CoinMin(value,upper[iColumn]);
    236             for (int j=columnStart[iColumn];j<columnStart[iColumn]+columnLength[iColumn];j++) {
    237               int iRow = row[j];
    238               double a = array[iRow];
    239               if (a) {
    240                 a += value*element[j];
    241                 if (!a)
    242                   a = 1.0e-100;
    243               } else {
    244                 which[n++]=iRow;
    245                 a=value*element[j];
    246                 assert (a);
    247               }
    248               array[iRow]=a;
    249             }
    250           }
    251         }
    252         base += numberLinks_;
    253       }
    254       base=numberLinks_*iWhere;
    255       for (int k=0;k<numberLinks_;k++) {
    256         int iColumn = which_[base+k];
    257         const double value = 1.0;
    258         for (int j=columnStart[iColumn];j<columnStart[iColumn]+columnLength[iColumn];j++) {
    259           int iRow = row[j];
    260           double a = array[iRow];
    261           if (a) {
    262             a -= value*element[j];
    263             if (!a)
    264               a = 1.0e-100;
    265           } else {
    266             which[n++]=iRow;
    267             a=-value*element[j];
    268             assert (a);
    269           }
    270           array[iRow]=a;
    271         }
    272       }
    273       for (j=0;j<n;j++) {
    274         int iRow = which[j];
    275         // moving to point will increase row solution by this
    276         double distance = array[iRow];
    277         if (distance>1.0e-8) {
    278           if (distance+rowSolution[iRow]>rowUpper[iRow]+1.0e-8) {
    279             possible=false;
    280             break;
    281           }
    282         } else if (distance<-1.0e-8) {
    283           if (distance+rowSolution[iRow]<rowLower[iRow]-1.0e-8) {
    284             possible=false;
    285             break;
    286           }
    287         }
    288       }
    289       for (j=0;j<n;j++)
    290         array[which[j]]=0.0;
    291       delete [] array;
    292       delete [] which;
     224      double *array = new double[numberRows];
     225      CoinZeroN(array, numberRows);
     226      int *which = new int[numberRows];
     227      int n = 0;
     228      int base = numberLinks_ * firstNonZero;
     229      for (j = firstNonZero; j <= lastNonZero; j++) {
     230        for (int k = 0; k < numberLinks_; k++) {
     231          int iColumn = which_[base + k];
     232          double value = CoinMax(0.0, solution[iColumn]);
     233          if (value > integerTolerance && upper[iColumn]) {
     234            value = CoinMin(value, upper[iColumn]);
     235            for (int j = columnStart[iColumn]; j < columnStart[iColumn] + columnLength[iColumn]; j++) {
     236              int iRow = row[j];
     237              double a = array[iRow];
     238              if (a) {
     239                a += value * element[j];
     240                if (!a)
     241                  a = 1.0e-100;
     242              } else {
     243                which[n++] = iRow;
     244                a = value * element[j];
     245                assert(a);
     246              }
     247              array[iRow] = a;
     248            }
     249          }
     250        }
     251        base += numberLinks_;
     252      }
     253      base = numberLinks_ * iWhere;
     254      for (int k = 0; k < numberLinks_; k++) {
     255        int iColumn = which_[base + k];
     256        const double value = 1.0;
     257        for (int j = columnStart[iColumn]; j < columnStart[iColumn] + columnLength[iColumn]; j++) {
     258          int iRow = row[j];
     259          double a = array[iRow];
     260          if (a) {
     261            a -= value * element[j];
     262            if (!a)
     263              a = 1.0e-100;
     264          } else {
     265            which[n++] = iRow;
     266            a = -value * element[j];
     267            assert(a);
     268          }
     269          array[iRow] = a;
     270        }
     271      }
     272      for (j = 0; j < n; j++) {
     273        int iRow = which[j];
     274        // moving to point will increase row solution by this
     275        double distance = array[iRow];
     276        if (distance > 1.0e-8) {
     277          if (distance + rowSolution[iRow] > rowUpper[iRow] + 1.0e-8) {
     278            possible = false;
     279            break;
     280          }
     281        } else if (distance < -1.0e-8) {
     282          if (distance + rowSolution[iRow] < rowLower[iRow] - 1.0e-8) {
     283            possible = false;
     284            break;
     285          }
     286        }
     287      }
     288      for (j = 0; j < n; j++)
     289        array[which[j]] = 0.0;
     290      delete[] array;
     291      delete[] which;
    293292      if (possible) {
    294         valueInfeasibility=0.0;
    295         printf("possible %d %d %d\n",firstNonZero,lastNonZero,iWhere);
     293        valueInfeasibility = 0.0;
     294        printf("possible %d %d %d\n", firstNonZero, lastNonZero, iWhere);
    296295      }
    297296    }
     
    304303
    305304// This looks at solution and sets bounds to contain solution
    306 void
    307 CbcLink::feasibleRegion()
     305void CbcLink::feasibleRegion()
    308306{
    309307  int j;
    310   int firstNonZero=-1;
     308  int firstNonZero = -1;
    311309  int lastNonZero = -1;
    312   OsiSolverInterface * solver = model_->solver();
    313   const double * solution = model_->testSolution();
    314   const double * upper = solver->getColUpper();
    315   double integerTolerance =
    316     model_->getDblParam(CbcModel::CbcIntegerTolerance);
     310  OsiSolverInterface *solver = model_->solver();
     311  const double *solution = model_->testSolution();
     312  const double *upper = solver->getColUpper();
     313  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
    317314  double weight = 0.0;
    318   double sum =0.0;
    319 
    320   int base=0;
    321   for (j=0;j<numberMembers_;j++) {
    322     for (int k=0;k<numberLinks_;k++) {
    323       int iColumn = which_[base+k];
    324       double value = CoinMax(0.0,solution[iColumn]);
     315  double sum = 0.0;
     316
     317  int base = 0;
     318  for (j = 0; j < numberMembers_; j++) {
     319    for (int k = 0; k < numberLinks_; k++) {
     320      int iColumn = which_[base + k];
     321      double value = CoinMax(0.0, solution[iColumn]);
    325322      sum += value;
    326       if (value>integerTolerance&&upper[iColumn]) {
    327         weight += weights_[j]*value;
    328         if (firstNonZero<0)
    329           firstNonZero=j;
    330         lastNonZero=j;
     323      if (value > integerTolerance && upper[iColumn]) {
     324        weight += weights_[j] * value;
     325        if (firstNonZero < 0)
     326          firstNonZero = j;
     327        lastNonZero = j;
    331328      }
    332329    }
     
    334331  }
    335332#ifdef DISTANCE
    336   if (lastNonZero-firstNonZero>sosType_-1) {
     333  if (lastNonZero - firstNonZero > sosType_ - 1) {
    337334    /* may still be satisfied.
    338335       For LOS type 2 we might wish to move coding around
     
    340337    */
    341338    int iWhere;
    342     bool possible=false;
    343     for (iWhere=firstNonZero;iWhere<=lastNonZero;iWhere++) {
    344       if (fabs(weight-weights_[iWhere])<1.0e-8) {
    345         possible=true;
    346         break;
     339    bool possible = false;
     340    for (iWhere = firstNonZero; iWhere <= lastNonZero; iWhere++) {
     341      if (fabs(weight - weights_[iWhere]) < 1.0e-8) {
     342        possible = true;
     343        break;
    347344      }
    348345    }
    349346    if (possible) {
    350347      // One could move some of this (+ arrays) into model_
    351       const CoinPackedMatrix * matrix = solver->getMatrixByCol();
    352       const double * element = matrix->getMutableElements();
    353       const int * row = matrix->getIndices();
    354       const CoinBigIndex * columnStart = matrix->getVectorStarts();
    355       const int * columnLength = matrix->getVectorLengths();
    356       const double * rowSolution = solver->getRowActivity();
    357       const double * rowLower = solver->getRowLower();
    358       const double * rowUpper = solver->getRowUpper();
     348      const CoinPackedMatrix *matrix = solver->getMatrixByCol();
     349      const double *element = matrix->getMutableElements();
     350      const int *row = matrix->getIndices();
     351      const CoinBigIndex *columnStart = matrix->getVectorStarts();
     352      const int *columnLength = matrix->getVectorLengths();
     353      const double *rowSolution = solver->getRowActivity();
     354      const double *rowLower = solver->getRowLower();
     355      const double *rowUpper = solver->getRowUpper();
    359356      int numberRows = matrix->getNumRows();
    360       double * array = new double [numberRows];
    361       CoinZeroN(array,numberRows);
    362       int * which = new int [numberRows];
    363       int n=0;
    364       int base=numberLinks_*firstNonZero;
    365       for (j=firstNonZero;j<=lastNonZero;j++) {
    366         for (int k=0;k<numberLinks_;k++) {
    367           int iColumn = which_[base+k];
    368           double value = CoinMax(0.0,solution[iColumn]);
    369           if (value>integerTolerance&&upper[iColumn]) {
    370             value = CoinMin(value,upper[iColumn]);
    371             for (int j=columnStart[iColumn];j<columnStart[iColumn]+columnLength[iColumn];j++) {
    372               int iRow = row[j];
    373               double a = array[iRow];
    374               if (a) {
    375                 a += value*element[j];
    376                 if (!a)
    377                   a = 1.0e-100;
    378               } else {
    379                 which[n++]=iRow;
    380                 a=value*element[j];
    381                 assert (a);
    382               }
    383               array[iRow]=a;
    384             }
    385           }
    386         }
    387         base += numberLinks_;
    388       }
    389       base=numberLinks_*iWhere;
    390       for (int k=0;k<numberLinks_;k++) {
    391         int iColumn = which_[base+k];
    392         const double value = 1.0;
    393         for (int j=columnStart[iColumn];j<columnStart[iColumn]+columnLength[iColumn];j++) {
    394           int iRow = row[j];
    395           double a = array[iRow];
    396           if (a) {
    397             a -= value*element[j];
    398             if (!a)
    399               a = 1.0e-100;
    400           } else {
    401             which[n++]=iRow;
    402             a=-value*element[j];
    403             assert (a);
    404           }
    405           array[iRow]=a;
    406         }
    407       }
    408       for (j=0;j<n;j++) {
    409         int iRow = which[j];
    410         // moving to point will increase row solution by this
    411         double distance = array[iRow];
    412         if (distance>1.0e-8) {
    413           if (distance+rowSolution[iRow]>rowUpper[iRow]+1.0e-8) {
    414             possible=false;
    415             break;
    416           }
    417         } else if (distance<-1.0e-8) {
    418           if (distance+rowSolution[iRow]<rowLower[iRow]-1.0e-8) {
    419             possible=false;
    420             break;
    421           }
    422         }
    423       }
    424       for (j=0;j<n;j++)
    425         array[which[j]]=0.0;
    426       delete [] array;
    427       delete [] which;
     357      double *array = new double[numberRows];
     358      CoinZeroN(array, numberRows);
     359      int *which = new int[numberRows];
     360      int n = 0;
     361      int base = numberLinks_ * firstNonZero;
     362      for (j = firstNonZero; j <= lastNonZero; j++) {
     363        for (int k = 0; k < numberLinks_; k++) {
     364          int iColumn = which_[base + k];
     365          double value = CoinMax(0.0, solution[iColumn]);
     366          if (value > integerTolerance && upper[iColumn]) {
     367            value = CoinMin(value, upper[iColumn]);
     368            for (int j = columnStart[iColumn]; j < columnStart[iColumn] + columnLength[iColumn]; j++) {
     369              int iRow = row[j];
     370              double a = array[iRow];
     371              if (a) {
     372                a += value * element[j];
     373                if (!a)
     374                  a = 1.0e-100;
     375              } else {
     376                which[n++] = iRow;
     377                a = value * element[j];
     378                assert(a);
     379              }
     380              array[iRow] = a;
     381            }
     382          }
     383        }
     384        base += numberLinks_;
     385      }
     386      base = numberLinks_ * iWhere;
     387      for (int k = 0; k < numberLinks_; k++) {
     388        int iColumn = which_[base + k];
     389        const double value = 1.0;
     390        for (int j = columnStart[iColumn]; j < columnStart[iColumn] + columnLength[iColumn]; j++) {
     391          int iRow = row[j];
     392          double a = array[iRow];
     393          if (a) {
     394            a -= value * element[j];
     395            if (!a)
     396              a = 1.0e-100;
     397          } else {
     398            which[n++] = iRow;
     399            a = -value * element[j];
     400            assert(a);
     401          }
     402          array[iRow] = a;
     403        }
     404      }
     405      for (j = 0; j < n; j++) {
     406        int iRow = which[j];
     407        // moving to point will increase row solution by this
     408        double distance = array[iRow];
     409        if (distance > 1.0e-8) {
     410          if (distance + rowSolution[iRow] > rowUpper[iRow] + 1.0e-8) {
     411            possible = false;
     412            break;
     413          }
     414        } else if (distance < -1.0e-8) {
     415          if (distance + rowSolution[iRow] < rowLower[iRow] - 1.0e-8) {
     416            possible = false;
     417            break;
     418          }
     419        }
     420      }
     421      for (j = 0; j < n; j++)
     422        array[which[j]] = 0.0;
     423      delete[] array;
     424      delete[] which;
    428425      if (possible) {
    429         printf("possible feas region %d %d %d\n",firstNonZero,lastNonZero,iWhere);
    430         firstNonZero=iWhere;
    431         lastNonZero=iWhere;
     426        printf("possible feas region %d %d %d\n", firstNonZero, lastNonZero, iWhere);
     427        firstNonZero = iWhere;
     428        lastNonZero = iWhere;
    432429      }
    433430    }
    434431  }
    435432#else
    436   assert (lastNonZero-firstNonZero<sosType_) ;
     433  assert(lastNonZero - firstNonZero < sosType_);
    437434#endif
    438   base=0;
    439   for (j=0;j<firstNonZero;j++) {
    440     for (int k=0;k<numberLinks_;k++) {
    441       int iColumn = which_[base+k];
    442       solver->setColUpper(iColumn,0.0);
     435  base = 0;
     436  for (j = 0; j < firstNonZero; j++) {
     437    for (int k = 0; k < numberLinks_; k++) {
     438      int iColumn = which_[base + k];
     439      solver->setColUpper(iColumn, 0.0);
    443440    }
    444441    base += numberLinks_;
     
    446443  // skip
    447444  base += numberLinks_;
    448   for (j=lastNonZero+1;j<numberMembers_;j++) {
    449     for (int k=0;k<numberLinks_;k++) {
    450       int iColumn = which_[base+k];
    451       solver->setColUpper(iColumn,0.0);
     445  for (j = lastNonZero + 1; j < numberMembers_; j++) {
     446    for (int k = 0; k < numberLinks_; k++) {
     447      int iColumn = which_[base + k];
     448      solver->setColUpper(iColumn, 0.0);
    452449    }
    453450    base += numberLinks_;
     
    455452}
    456453
    457 
    458454// Creates a branching object
    459 CbcBranchingObject * 
    460 CbcLink::createCbcBranch(OsiSolverInterface * /*solver*/, const OsiBranchingInformation * /*info*/, int way) 
     455CbcBranchingObject *
     456CbcLink::createCbcBranch(OsiSolverInterface * /*solver*/, const OsiBranchingInformation * /*info*/, int way)
    461457{
    462458  int j;
    463   const double * solution = model_->testSolution();
    464   double integerTolerance =
    465       model_->getDblParam(CbcModel::CbcIntegerTolerance);
    466   OsiSolverInterface * solver = model_->solver();
    467   const double * upper = solver->getColUpper();
    468   int firstNonFixed=-1;
    469   int lastNonFixed=-1;
    470   int firstNonZero=-1;
     459  const double *solution = model_->testSolution();
     460  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
     461  OsiSolverInterface *solver = model_->solver();
     462  const double *upper = solver->getColUpper();
     463  int firstNonFixed = -1;
     464  int lastNonFixed = -1;
     465  int firstNonZero = -1;
    471466  int lastNonZero = -1;
    472467  double weight = 0.0;
    473   double sum =0.0;
    474   int base=0;
    475   for (j=0;j<numberMembers_;j++) {
    476     for (int k=0;k<numberLinks_;k++) {
    477       int iColumn = which_[base+k];
     468  double sum = 0.0;
     469  int base = 0;
     470  for (j = 0; j < numberMembers_; j++) {
     471    for (int k = 0; k < numberLinks_; k++) {
     472      int iColumn = which_[base + k];
    478473      if (upper[iColumn]) {
    479         double value = CoinMax(0.0,solution[iColumn]);
     474        double value = CoinMax(0.0, solution[iColumn]);
    480475        sum += value;
    481         if (firstNonFixed<0)
    482           firstNonFixed=j;
    483         lastNonFixed=j;
    484         if (value>integerTolerance) {
    485           weight += weights_[j]*value;
    486           if (firstNonZero<0)
    487             firstNonZero=j;
    488           lastNonZero=j;
     476        if (firstNonFixed < 0)
     477          firstNonFixed = j;
     478        lastNonFixed = j;
     479        if (value > integerTolerance) {
     480          weight += weights_[j] * value;
     481          if (firstNonZero < 0)
     482            firstNonZero = j;
     483          lastNonZero = j;
    489484        }
    490485      }
     
    492487    base += numberLinks_;
    493488  }
    494   assert (lastNonZero-firstNonZero>=sosType_) ;
     489  assert(lastNonZero - firstNonZero >= sosType_);
    495490  // find where to branch
    496   assert (sum>0.0);
     491  assert(sum > 0.0);
    497492  weight /= sum;
    498493  int iWhere;
    499   double separator=0.0;
    500   for (iWhere=firstNonZero;iWhere<lastNonZero;iWhere++)
    501     if (weight<weights_[iWhere+1])
     494  double separator = 0.0;
     495  for (iWhere = firstNonZero; iWhere < lastNonZero; iWhere++)
     496    if (weight < weights_[iWhere + 1])
    502497      break;
    503   if (sosType_==1) {
     498  if (sosType_ == 1) {
    504499    // SOS 1
    505     separator = 0.5 *(weights_[iWhere]+weights_[iWhere+1]);
     500    separator = 0.5 * (weights_[iWhere] + weights_[iWhere + 1]);
    506501  } else {
    507502    // SOS 2
    508     if (iWhere==firstNonFixed)
    509       iWhere++;;
    510     if (iWhere==lastNonFixed-1)
    511       iWhere = lastNonFixed-2;
    512     separator = weights_[iWhere+1];
     503    if (iWhere == firstNonFixed)
     504      iWhere++;
     505    ;
     506    if (iWhere == lastNonFixed - 1)
     507      iWhere = lastNonFixed - 2;
     508    separator = weights_[iWhere + 1];
    513509  }
    514510  // create object
    515   CbcBranchingObject * branch;
    516   branch = new CbcLinkBranchingObject(model_,this,way,separator);
     511  CbcBranchingObject *branch;
     512  branch = new CbcLinkBranchingObject(model_, this, way, separator);
    517513  return branch;
    518514}
    519515// Useful constructor
    520 CbcLinkBranchingObject::CbcLinkBranchingObject (CbcModel * model,
    521                                               const CbcLink * set,
    522                                               int way ,
    523                                               double separator)
    524   :CbcBranchingObject(model,set->id(),way,0.5)
     516CbcLinkBranchingObject::CbcLinkBranchingObject(CbcModel *model,
     517  const CbcLink *set,
     518  int way,
     519  double separator)
     520  : CbcBranchingObject(model, set->id(), way, 0.5)
    525521{
    526522  set_ = set;
     
    528524}
    529525
    530 // Copy constructor
    531 CbcLinkBranchingObject::CbcLinkBranchingObject ( const CbcLinkBranchingObject & rhs) :CbcBranchingObject(rhs)
    532 {
    533   set_=rhs.set_;
     526// Copy constructor
     527CbcLinkBranchingObject::CbcLinkBranchingObject(const CbcLinkBranchingObject &rhs)
     528  : CbcBranchingObject(rhs)
     529{
     530  set_ = rhs.set_;
    534531  separator_ = rhs.separator_;
    535532}
    536533
    537 // Assignment operator 
    538 CbcLinkBranchingObject & 
    539 CbcLinkBranchingObject::operator=( const CbcLinkBranchingObject& rhs)
     534// Assignment operator
     535CbcLinkBranchingObject &
     536CbcLinkBranchingObject::operator=(const CbcLinkBranchingObject &rhs)
    540537{
    541538  if (this != &rhs) {
    542539    CbcBranchingObject::operator=(rhs);
    543     set_=rhs.set_;
     540    set_ = rhs.set_;
    544541    separator_ = rhs.separator_;
    545542  }
    546543  return *this;
    547544}
    548 CbcBranchingObject * 
     545CbcBranchingObject *
    549546CbcLinkBranchingObject::clone() const
    550 { 
     547{
    551548  return (new CbcLinkBranchingObject(*this));
    552549}
    553550
    554 
    555 // Destructor
    556 CbcLinkBranchingObject::~CbcLinkBranchingObject ()
     551// Destructor
     552CbcLinkBranchingObject::~CbcLinkBranchingObject()
    557553{
    558554}
     
    563559  int numberMembers = set_->numberMembers();
    564560  int numberLinks = set_->numberLinks();
    565   const double * weights = set_->weights();
    566   const int * which = set_->which();
    567   OsiSolverInterface * solver = model_->solver();
     561  const double *weights = set_->weights();
     562  const int *which = set_->which();
     563  OsiSolverInterface *solver = model_->solver();
    568564  //const double * lower = solver->getColLower();
    569565  //const double * upper = solver->getColUpper();
    570566  // *** for way - up means fix all those in down section
    571   if (way_<0) {
     567  if (way_ < 0) {
    572568    int i;
    573     for ( i=0;i<numberMembers;i++) {
     569    for (i = 0; i < numberMembers; i++) {
    574570      if (weights[i] > separator_)
    575         break;
    576     }
    577     assert (i<numberMembers);
    578     int base=i*numberLinks;;
    579     for (;i<numberMembers;i++) {
    580       for (int k=0;k<numberLinks;k++) {
    581         int iColumn = which[base+k];
    582         solver->setColUpper(iColumn,0.0);
     571        break;
     572    }
     573    assert(i < numberMembers);
     574    int base = i * numberLinks;
     575    ;
     576    for (; i < numberMembers; i++) {
     577      for (int k = 0; k < numberLinks; k++) {
     578        int iColumn = which[base + k];
     579        solver->setColUpper(iColumn, 0.0);
    583580      }
    584581      base += numberLinks;
    585582    }
    586     way_=1;      // Swap direction
     583    way_ = 1; // Swap direction
    587584  } else {
    588585    int i;
    589     int base=0;
    590     for ( i=0;i<numberMembers;i++) {
     586    int base = 0;
     587    for (i = 0; i < numberMembers; i++) {
    591588      if (weights[i] >= separator_) {
    592         break;
     589        break;
    593590      } else {
    594         for (int k=0;k<numberLinks;k++) {
    595           int iColumn = which[base+k];
    596           solver->setColUpper(iColumn,0.0);
     591        for (int k = 0; k < numberLinks; k++) {
     592          int iColumn = which[base + k];
     593          solver->setColUpper(iColumn, 0.0);
    597594        }
    598595        base += numberLinks;
    599596      }
    600597    }
    601     assert (i<numberMembers);
    602     way_=-1;      // Swap direction
     598    assert(i < numberMembers);
     599    way_ = -1; // Swap direction
    603600  }
    604601  return 0.0;
    605602}
    606 // Print what would happen 
    607 void
    608 CbcLinkBranchingObject::print()
     603// Print what would happen
     604void CbcLinkBranchingObject::print()
    609605{
    610606  int numberMembers = set_->numberMembers();
    611607  int numberLinks = set_->numberLinks();
    612   const double * weights = set_->weights();
    613   const int * which = set_->which();
    614   OsiSolverInterface * solver = model_->solver();
    615   const double * upper = solver->getColUpper();
    616   int first=numberMembers;
    617   int last=-1;
    618   int numberFixed=0;
    619   int numberOther=0;
     608  const double *weights = set_->weights();
     609  const int *which = set_->which();
     610  OsiSolverInterface *solver = model_->solver();
     611  const double *upper = solver->getColUpper();
     612  int first = numberMembers;
     613  int last = -1;
     614  int numberFixed = 0;
     615  int numberOther = 0;
    620616  int i;
    621   int base=0;
    622   for ( i=0;i<numberMembers;i++) {
    623     for (int k=0;k<numberLinks;k++) {
    624       int iColumn = which[base+k];
     617  int base = 0;
     618  for (i = 0; i < numberMembers; i++) {
     619    for (int k = 0; k < numberLinks; k++) {
     620      int iColumn = which[base + k];
    625621      double bound = upper[iColumn];
    626622      if (bound) {
    627         first = CoinMin(first,i);
    628         last = CoinMax(last,i);
     623        first = CoinMin(first, i);
     624        last = CoinMax(last, i);
    629625      }
    630626    }
     
    632628  }
    633629  // *** for way - up means fix all those in down section
    634   base=0;
    635   if (way_<0) {
     630  base = 0;
     631  if (way_ < 0) {
    636632    printf("SOS Down");
    637     for ( i=0;i<numberMembers;i++) {
    638       if (weights[i] > separator_) 
    639         break;
    640       for (int k=0;k<numberLinks;k++) {
    641         int iColumn = which[base+k];
     633    for (i = 0; i < numberMembers; i++) {
     634      if (weights[i] > separator_)
     635        break;
     636      for (int k = 0; k < numberLinks; k++) {
     637        int iColumn = which[base + k];
    642638        double bound = upper[iColumn];
    643         if (bound)
     639        if (bound)
    644640          numberOther++;
    645641      }
    646642      base += numberLinks;
    647643    }
    648     assert (i<numberMembers);
    649     for (;i<numberMembers;i++) {
    650       for (int k=0;k<numberLinks;k++) {
    651         int iColumn = which[base+k];
     644    assert(i < numberMembers);
     645    for (; i < numberMembers; i++) {
     646      for (int k = 0; k < numberLinks; k++) {
     647        int iColumn = which[base + k];
    652648        double bound = upper[iColumn];
    653649        if (bound)
     
    658654  } else {
    659655    printf("SOS Up");
    660     for ( i=0;i<numberMembers;i++) {
     656    for (i = 0; i < numberMembers; i++) {
    661657      if (weights[i] >= separator_)
    662         break;
    663       for (int k=0;k<numberLinks;k++) {
    664         int iColumn = which[base+k];
     658        break;
     659      for (int k = 0; k < numberLinks; k++) {
     660        int iColumn = which[base + k];
    665661        double bound = upper[iColumn];
    666         if (bound)
     662        if (bound)
    667663          numberFixed++;
    668664      }
    669665      base += numberLinks;
    670666    }
    671     assert (i<numberMembers);
    672     for (;i<numberMembers;i++) {
    673       for (int k=0;k<numberLinks;k++) {
    674         int iColumn = which[base+k];
     667    assert(i < numberMembers);
     668    for (; i < numberMembers; i++) {
     669      for (int k = 0; k < numberLinks; k++) {
     670        int iColumn = which[base + k];
    675671        double bound = upper[iColumn];
    676672        if (bound)
     
    680676    }
    681677  }
    682   assert ((numberFixed%numberLinks)==0);
    683   assert ((numberOther%numberLinks)==0);
     678  assert((numberFixed % numberLinks) == 0);
     679  assert((numberOther % numberLinks) == 0);
    684680  printf(" - at %g, free range %d (%g) => %d (%g), %d would be fixed, %d other way\n",
    685          separator_,first,weights[first],last,weights[last],numberFixed/numberLinks,
    686          numberOther/numberLinks);
     681    separator_, first, weights[first], last, weights[last], numberFixed / numberLinks,
     682    numberOther / numberLinks);
    687683}
    688684/** Compare the \c this with \c brObj. \c this and \c brObj must be os the
     
    695691*/
    696692CbcRangeCompare
    697 CbcLinkBranchingObject::compareBranchingObject
    698 (const CbcBranchingObject* brObj, const bool replaceIfOverlap)
     693CbcLinkBranchingObject::compareBranchingObject(const CbcBranchingObject *brObj, const bool replaceIfOverlap)
    699694{
    700695  throw("must implement");
  • trunk/Cbc/examples/CbcBranchLink.hpp

    r1900 r2469  
    1313*/
    1414
    15 
    1615class CbcLink : public CbcObject {
    1716
    1817public:
    19 
    20   // Default Constructor
    21   CbcLink ();
     18  // Default Constructor
     19  CbcLink();
    2220
    2321  /** Useful constructor - A valid solution is if all variables are zero
     
    2725      starting at first. If weights null then 0,1,2..
    2826  */
    29   CbcLink (CbcModel * model, int numberMembers,
    30            int numberLinks, int first,
    31            const double * weights, int setNumber);
     27  CbcLink(CbcModel *model, int numberMembers,
     28    int numberLinks, int first,
     29    const double *weights, int setNumber);
    3230  /** Useful constructor - A valid solution is if all variables are zero
    3331      apart from k*numberLink to (k+1)*numberLink-1 where k is 0 through
     
    3634      If weights null then 0,1,2..
    3735  */
    38   CbcLink (CbcModel * model, int numberMembers,
    39            int numberLinks, int typeSOS, const int * which,
    40            const double * weights, int setNumber);
    41  
    42   // Copy constructor 
    43   CbcLink ( const CbcLink &);
    44    
     36  CbcLink(CbcModel *model, int numberMembers,
     37    int numberLinks, int typeSOS, const int *which,
     38    const double *weights, int setNumber);
     39
     40  // Copy constructor
     41  CbcLink(const CbcLink &);
     42
    4543  /// Clone
    46   virtual CbcObject * clone() const;
     44  virtual CbcObject *clone() const;
    4745
    48   // Assignment operator 
    49   CbcLink & operator=( const CbcLink& rhs);
     46  // Assignment operator
     47  CbcLink &operator=(const CbcLink &rhs);
    5048
    51   // Destructor 
    52   ~CbcLink ();
    53  
     49  // Destructor
     50  ~CbcLink();
     51
    5452  /// Infeasibility - large is 0.5
    55   virtual double infeasibility(int & preferredWay) const;
     53  virtual double infeasibility(int &preferredWay) const;
    5654
    5755  /// This looks at solution and sets bounds to contain solution
    5856  virtual void feasibleRegion();
    5957  /// Creates a branching object
    60   virtual CbcBranchingObject * createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * info, int way) ;
     58  virtual CbcBranchingObject *createCbcBranch(OsiSolverInterface *solver, const OsiBranchingInformation *info, int way);
    6159
    6260  /// Number of members
    6361  inline int numberMembers() const
    64   {return numberMembers_;}
     62  {
     63    return numberMembers_;
     64  }
    6565
    6666  /// Number of links for each member
    6767  inline int numberLinks() const
    68   {return numberLinks_;}
     68  {
     69    return numberLinks_;
     70  }
    6971
    7072  /// Which variables
    71   inline const int * which() const
    72   {return which_;}
     73  inline const int *which() const
     74  {
     75    return which_;
     76  }
    7377
    7478  /** Array of weights */
    75   inline const double * weights() const
    76   { return weights_;}
     79  inline const double *weights() const
     80  {
     81    return weights_;
     82  }
    7783
    7884private:
     
    8086
    8187  /// Weights
    82   double * weights_;
     88  double *weights_;
    8389
    8490  /// Number of members
    8591  int numberMembers_;
    8692  /// Number of links
    87    int numberLinks_;
     93  int numberLinks_;
    8894  /// Members
    89   int * which_;
     95  int *which_;
    9096  /// Type 1 or 2
    9197  int sosType_;
     
    99105
    100106public:
    101 
    102   // Default Constructor
    103   CbcLinkBranchingObject ();
     107  // Default Constructor
     108  CbcLinkBranchingObject();
    104109
    105110  // Useful constructor
    106   CbcLinkBranchingObject (CbcModel * model,  const CbcLink * set,
    107                           int way,
    108                           double separator);
    109  
    110   // Copy constructor 
    111   CbcLinkBranchingObject ( const CbcLinkBranchingObject &);
    112    
    113   // Assignment operator 
    114   CbcLinkBranchingObject & operator=( const CbcLinkBranchingObject& rhs);
     111  CbcLinkBranchingObject(CbcModel *model, const CbcLink *set,
     112    int way,
     113    double separator);
     114
     115  // Copy constructor
     116  CbcLinkBranchingObject(const CbcLinkBranchingObject &);
     117
     118  // Assignment operator
     119  CbcLinkBranchingObject &operator=(const CbcLinkBranchingObject &rhs);
    115120
    116121  /// Clone
    117   virtual CbcBranchingObject * clone() const;
     122  virtual CbcBranchingObject *clone() const;
    118123
    119   // Destructor 
    120   virtual ~CbcLinkBranchingObject ();
    121  
     124  // Destructor
     125  virtual ~CbcLinkBranchingObject();
     126
    122127  /// Does next branch and updates state
    123128  virtual double branch();
     
    127132  virtual void print();
    128133  /** Return the type (an integer identifier) of \c this */
    129   virtual CbcBranchObjType type() const
    130   { return CbcBranchObjType(0);} /*FIXME what type() should be returned here? */
     134  virtual CbcBranchObjType type() const
     135  {
     136    return CbcBranchObjType(0);
     137  } /*FIXME what type() should be returned here? */
    131138
    132139  /** Compare the \c this with \c brObj. \c this and \c brObj must be os the
     
    138145      whose feasible region is the overlap.
    139146   */
    140   virtual CbcRangeCompare compareBranchingObject
    141   (const CbcBranchingObject* brObj, const bool replaceIfOverlap = false);
     147  virtual CbcRangeCompare compareBranchingObject(const CbcBranchingObject *brObj, const bool replaceIfOverlap = false);
     148
    142149private:
    143150  /// data
    144   const CbcLink * set_;
     151  const CbcLink *set_;
    145152  /// separator
    146153  double separator_;
  • trunk/Cbc/examples/CbcBranchUser.cpp

    r1898 r2469  
    1515#include "CoinSort.hpp"
    1616
    17 // Default Constructor // Default Constructor 
     17// Default Constructor // Default Constructor
    1818CbcBranchUserDecision::CbcBranchUserDecision()
    19   :CbcBranchDecision()
    20 {
    21 }
    22 
    23 // Copy constructor 
    24 CbcBranchUserDecision::CbcBranchUserDecision (
    25                                     const CbcBranchUserDecision & rhs)
    26   :CbcBranchDecision(rhs)
     19  : CbcBranchDecision()
     20{
     21}
     22
     23// Copy constructor
     24CbcBranchUserDecision::CbcBranchUserDecision(
     25  const CbcBranchUserDecision &rhs)
     26  : CbcBranchDecision(rhs)
    2727{
    2828}
     
    3333
    3434// Clone
    35 CbcBranchDecision * 
     35CbcBranchDecision *
    3636CbcBranchUserDecision::clone() const
    3737{
     
    4040
    4141// Initialize i.e. before start of choosing at a node
    42 void
    43 CbcBranchUserDecision::initialize(CbcModel * model)
    44 {
    45 }
    46 
     42void CbcBranchUserDecision::initialize(CbcModel *model)
     43{
     44}
    4745
    4846/* Returns nonzero if branching on first object is "better" than on
     
    5351   
    5452*/
    55 int
    56 CbcBranchUserDecision::betterBranch(CbcBranchingObject * thisOne,
    57                             CbcBranchingObject * bestSoFar,
    58                             double changeUp, int numberInfeasibilitiesUp,
    59                             double changeDown, int numberInfeasibilitiesDown)
     53int CbcBranchUserDecision::betterBranch(CbcBranchingObject *thisOne,
     54  CbcBranchingObject *bestSoFar,
     55  double changeUp, int numberInfeasibilitiesUp,
     56  double changeDown, int numberInfeasibilitiesDown)
    6057{
    6158  printf("Now obsolete CbcBranchUserDecision::betterBranch\n");
     
    7067*/
    7168
    72 int
    73 CbcBranchUserDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects,
    74                                    int numberUnsatisfied,
    75                                    double * changeUp, int * numberInfeasibilitiesUp,
    76                                    double * changeDown, int * numberInfeasibilitiesDown,
    77                                    double objectiveValue)
    78 {
    79 
    80   int bestWay=0;
     69int CbcBranchUserDecision::bestBranch(CbcBranchingObject **objects, int numberObjects,
     70  int numberUnsatisfied,
     71  double *changeUp, int *numberInfeasibilitiesUp,
     72  double *changeDown, int *numberInfeasibilitiesDown,
     73  double objectiveValue)
     74{
     75
     76  int bestWay = 0;
    8177  int whichObject = -1;
    8278  if (numberObjects) {
    83     CbcModel * model = objects[0]->model();
     79    CbcModel *model = objects[0]->model();
    8480    // at continuous
    8581    //double continuousObjective = model->getContinuousObjective();
    8682    //int continuousInfeasibilities = model->getContinuousInfeasibilities();
    87    
     83
    8884    // average cost to get rid of infeasibility
    89     //double averageCostPerInfeasibility = 
     85    //double averageCostPerInfeasibility =
    9086    //(objectiveValue-continuousObjective)/
    9187    //(double) (abs(continuousInfeasibilities-numberUnsatisfied)+1);
     
    9793    int numberSolutions = model->getSolutionCount();
    9894    double cutoff = model->getCutoff();
    99     int method=0;
     95    int method = 0;
    10096    int i;
    10197    if (numberSolutions) {
    10298      int numberHeuristic = model->getNumberHeuristicSolutions();
    103       if (numberHeuristic<numberSolutions) {
    104         method = 1;
     99      if (numberHeuristic < numberSolutions) {
     100        method = 1;
    105101      } else {
    106         method = 2;
    107         // look further
    108         for ( i = 0 ; i < numberObjects ; i++) {
    109           int numberNext = numberInfeasibilitiesUp[i];
    110          
    111           if (numberNext<numberUnsatisfied) {
    112             int numberUp = numberUnsatisfied - numberInfeasibilitiesUp[i];
    113             double perUnsatisfied = changeUp[i]/(double) numberUp;
    114             double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
    115             if (estimatedObjective<cutoff)
    116               method=3;
    117           }
    118           numberNext = numberInfeasibilitiesDown[i];
    119           if (numberNext<numberUnsatisfied) {
    120             int numberDown = numberUnsatisfied - numberInfeasibilitiesDown[i];
    121             double perUnsatisfied = changeDown[i]/(double) numberDown;
    122             double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
    123             if (estimatedObjective<cutoff)
    124               method=3;
    125           }
    126         }
    127       }
    128       method=2;
     102        method = 2;
     103        // look further
     104        for (i = 0; i < numberObjects; i++) {
     105          int numberNext = numberInfeasibilitiesUp[i];
     106
     107          if (numberNext < numberUnsatisfied) {
     108            int numberUp = numberUnsatisfied - numberInfeasibilitiesUp[i];
     109            double perUnsatisfied = changeUp[i] / (double)numberUp;
     110            double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
     111            if (estimatedObjective < cutoff)
     112              method = 3;
     113          }
     114          numberNext = numberInfeasibilitiesDown[i];
     115          if (numberNext < numberUnsatisfied) {
     116            int numberDown = numberUnsatisfied - numberInfeasibilitiesDown[i];
     117            double perUnsatisfied = changeDown[i] / (double)numberDown;
     118            double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
     119            if (estimatedObjective < cutoff)
     120              method = 3;
     121          }
     122        }
     123      }
     124      method = 2;
    129125    } else {
    130126      method = 0;
     
    139135       4 - take cheapest up branch if infeasibilities same
    140136    */
    141     int bestNumber=COIN_INT_MAX;
    142     double bestCriterion=-1.0e50;
     137    int bestNumber = COIN_INT_MAX;
     138    double bestCriterion = -1.0e50;
    143139    double alternativeCriterion = -1.0;
    144140    double bestEstimate = 1.0e100;
     
    146142    case 0:
    147143      // could add in depth as well
    148       for ( i = 0 ; i < numberObjects ; i++) {
    149         int thisNumber = CoinMin(numberInfeasibilitiesUp[i],numberInfeasibilitiesDown[i]);
    150         if (thisNumber<=bestNumber) {
    151           int betterWay=0;
    152           if (numberInfeasibilitiesUp[i]<numberInfeasibilitiesDown[i]) {
    153             if (numberInfeasibilitiesUp[i]<bestNumber) {
    154               betterWay = 1;
    155             } else {
    156               if (changeUp[i]<bestCriterion)
    157                 betterWay=1;
    158             }
    159           } else if (numberInfeasibilitiesUp[i]>numberInfeasibilitiesDown[i]) {
    160             if (numberInfeasibilitiesDown[i]<bestNumber) {
    161               betterWay = -1;
    162             } else {
    163               if (changeDown[i]<bestCriterion)
    164                 betterWay=-1;
    165             }
    166           } else {
    167             // up and down have same number
    168             bool better=false;
    169             if (numberInfeasibilitiesUp[i]<bestNumber) {
    170               better=true;
    171             } else if (numberInfeasibilitiesUp[i]==bestNumber) {
    172               if (CoinMin(changeUp[i],changeDown[i])<bestCriterion)
    173                 better=true;;
    174             }
    175             if (better) {
    176               // see which way
    177               if (changeUp[i]<=changeDown[i])
    178                 betterWay=1;
    179               else
    180                 betterWay=-1;
    181             }
    182           }
    183           if (betterWay) {
    184             bestCriterion = CoinMin(changeUp[i],changeDown[i]);
    185             bestNumber = thisNumber;
    186             whichObject = i;
    187             bestWay = betterWay;
    188           }
    189         }
     144      for (i = 0; i < numberObjects; i++) {
     145        int thisNumber = CoinMin(numberInfeasibilitiesUp[i], numberInfeasibilitiesDown[i]);
     146        if (thisNumber <= bestNumber) {
     147          int betterWay = 0;
     148          if (numberInfeasibilitiesUp[i] < numberInfeasibilitiesDown[i]) {
     149            if (numberInfeasibilitiesUp[i] < bestNumber) {
     150              betterWay = 1;
     151            } else {
     152              if (changeUp[i] < bestCriterion)
     153                betterWay = 1;
     154            }
     155          } else if (numberInfeasibilitiesUp[i] > numberInfeasibilitiesDown[i]) {
     156            if (numberInfeasibilitiesDown[i] < bestNumber) {
     157              betterWay = -1;
     158            } else {
     159              if (changeDown[i] < bestCriterion)
     160                betterWay = -1;
     161            }
     162          } else {
     163            // up and down have same number
     164            bool better = false;
     165            if (numberInfeasibilitiesUp[i] < bestNumber) {
     166              better = true;
     167            } else if (numberInfeasibilitiesUp[i] == bestNumber) {
     168              if (CoinMin(changeUp[i], changeDown[i]) < bestCriterion)
     169                better = true;
     170              ;
     171            }
     172            if (better) {
     173              // see which way
     174              if (changeUp[i] <= changeDown[i])
     175                betterWay = 1;
     176              else
     177                betterWay = -1;
     178            }
     179          }
     180          if (betterWay) {
     181            bestCriterion = CoinMin(changeUp[i], changeDown[i]);
     182            bestNumber = thisNumber;
     183            whichObject = i;
     184            bestWay = betterWay;
     185          }
     186        }
    190187      }
    191188      break;
    192189    case 1:
    193       for ( i = 0 ; i < numberObjects ; i++) {
    194         int betterWay=0;
    195         if (changeUp[i]<=changeDown[i]) {
    196           if (changeUp[i]>bestCriterion)
    197             betterWay=1;
    198         } else {
    199           if (changeDown[i]>bestCriterion)
    200             betterWay=-1;
    201         }
    202         if (betterWay) {
    203           bestCriterion = CoinMin(changeUp[i],changeDown[i]);
    204           whichObject = i;
    205           bestWay = betterWay;
    206         }
     190      for (i = 0; i < numberObjects; i++) {
     191        int betterWay = 0;
     192        if (changeUp[i] <= changeDown[i]) {
     193          if (changeUp[i] > bestCriterion)
     194            betterWay = 1;
     195        } else {
     196          if (changeDown[i] > bestCriterion)
     197            betterWay = -1;
     198        }
     199        if (betterWay) {
     200          bestCriterion = CoinMin(changeUp[i], changeDown[i]);
     201          whichObject = i;
     202          bestWay = betterWay;
     203        }
    207204      }
    208205      break;
    209206    case 2:
    210       for ( i = 0 ; i < numberObjects ; i++) {
    211         double change = CoinMin(changeUp[i],changeDown[i]);
    212         double sum = changeUp[i] + changeDown[i];
    213         bool take=false;
    214         if (change>1.1*bestCriterion)
    215           take=true;
    216         else if (change>0.9*bestCriterion&&sum+change>bestCriterion+alternativeCriterion)
    217           take=true;
    218         if (take) {
    219           if (changeUp[i]<=changeDown[i]) {
    220             if (changeUp[i]>bestCriterion)
    221               bestWay=1;
    222           } else {
    223             if (changeDown[i]>bestCriterion)
    224               bestWay=-1;
    225           }
    226           bestCriterion = change;
    227           alternativeCriterion = sum;
    228           whichObject = i;
    229         }
     207      for (i = 0; i < numberObjects; i++) {
     208        double change = CoinMin(changeUp[i], changeDown[i]);
     209        double sum = changeUp[i] + changeDown[i];
     210        bool take = false;
     211        if (change > 1.1 * bestCriterion)
     212          take = true;
     213        else if (change > 0.9 * bestCriterion && sum + change > bestCriterion + alternativeCriterion)
     214          take = true;
     215        if (take) {
     216          if (changeUp[i] <= changeDown[i]) {
     217            if (changeUp[i] > bestCriterion)
     218              bestWay = 1;
     219          } else {
     220            if (changeDown[i] > bestCriterion)
     221              bestWay = -1;
     222          }
     223          bestCriterion = change;
     224          alternativeCriterion = sum;
     225          whichObject = i;
     226        }
    230227      }
    231228      break;
    232229    case 3:
    233       for ( i = 0 ; i < numberObjects ; i++) {
    234         int numberNext = numberInfeasibilitiesUp[i];
    235        
    236         if (numberNext<numberUnsatisfied) {
    237           int numberUp = numberUnsatisfied - numberInfeasibilitiesUp[i];
    238           double perUnsatisfied = changeUp[i]/(double) numberUp;
    239           double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
    240           if (estimatedObjective<bestEstimate) {
    241             bestEstimate = estimatedObjective;
    242             bestWay=1;
    243             whichObject=i;
    244           }
    245         }
    246         numberNext = numberInfeasibilitiesDown[i];
    247         if (numberNext<numberUnsatisfied) {
    248           int numberDown = numberUnsatisfied - numberInfeasibilitiesDown[i];
    249           double perUnsatisfied = changeDown[i]/(double) numberDown;
    250           double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
    251           if (estimatedObjective<bestEstimate) {
    252             bestEstimate = estimatedObjective;
    253             bestWay=-1;
    254             whichObject=i;
    255           }
    256         }
     230      for (i = 0; i < numberObjects; i++) {
     231        int numberNext = numberInfeasibilitiesUp[i];
     232
     233        if (numberNext < numberUnsatisfied) {
     234          int numberUp = numberUnsatisfied - numberInfeasibilitiesUp[i];
     235          double perUnsatisfied = changeUp[i] / (double)numberUp;
     236          double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
     237          if (estimatedObjective < bestEstimate) {
     238            bestEstimate = estimatedObjective;
     239            bestWay = 1;
     240            whichObject = i;
     241          }
     242        }
     243        numberNext = numberInfeasibilitiesDown[i];
     244        if (numberNext < numberUnsatisfied) {
     245          int numberDown = numberUnsatisfied - numberInfeasibilitiesDown[i];
     246          double perUnsatisfied = changeDown[i] / (double)numberDown;
     247          double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied;
     248          if (estimatedObjective < bestEstimate) {
     249            bestEstimate = estimatedObjective;
     250            bestWay = -1;
     251            whichObject = i;
     252          }
     253        }
    257254      }
    258255      break;
     
    261258      // first get best number or when going down
    262259      // now choose smallest change up amongst equal number infeas
    263       for ( i = 0 ; i < numberObjects ; i++) {
    264         int thisNumber = CoinMin(numberInfeasibilitiesUp[i],numberInfeasibilitiesDown[i]);
    265         if (thisNumber<=bestNumber) {
    266           int betterWay=0;
    267           if (numberInfeasibilitiesUp[i]<numberInfeasibilitiesDown[i]) {
    268             if (numberInfeasibilitiesUp[i]<bestNumber) {
    269               betterWay = 1;
    270             } else {
    271               if (changeUp[i]<bestCriterion)
    272                 betterWay=1;
    273             }
    274           } else if (numberInfeasibilitiesUp[i]>numberInfeasibilitiesDown[i]) {
    275             if (numberInfeasibilitiesDown[i]<bestNumber) {
    276               betterWay = -1;
    277             } else {
    278               if (changeDown[i]<bestCriterion)
    279                 betterWay=-1;
    280             }
    281           } else {
    282             // up and down have same number
    283             bool better=false;
    284             if (numberInfeasibilitiesUp[i]<bestNumber) {
    285               better=true;
    286             } else if (numberInfeasibilitiesUp[i]==bestNumber) {
    287               if (CoinMin(changeUp[i],changeDown[i])<bestCriterion)
    288                 better=true;;
    289             }
    290             if (better) {
    291               // see which way
    292               if (changeUp[i]<=changeDown[i])
    293                 betterWay=1;
    294               else
    295                 betterWay=-1;
    296             }
    297           }
    298           if (betterWay) {
    299             bestCriterion = CoinMin(changeUp[i],changeDown[i]);
    300             bestNumber = thisNumber;
    301             whichObject = i;
    302             bestWay = betterWay;
    303           }
    304         }
    305       }
    306       bestCriterion=1.0e50;
    307       for ( i = 0 ; i < numberObjects ; i++) {
    308         int thisNumber = numberInfeasibilitiesUp[i];
    309         if (thisNumber==bestNumber&&changeUp) {
    310           if (changeUp[i]<bestCriterion) {
    311             bestCriterion = changeUp[i];
    312             whichObject = i;
    313             bestWay = 1;
    314           }
    315         }
     260      for (i = 0; i < numberObjects; i++) {
     261        int thisNumber = CoinMin(numberInfeasibilitiesUp[i], numberInfeasibilitiesDown[i]);
     262        if (thisNumber <= bestNumber) {
     263          int betterWay = 0;
     264          if (numberInfeasibilitiesUp[i] < numberInfeasibilitiesDown[i]) {
     265            if (numberInfeasibilitiesUp[i] < bestNumber) {
     266              betterWay = 1;
     267            } else {
     268              if (changeUp[i] < bestCriterion)
     269                betterWay = 1;
     270            }
     271          } else if (numberInfeasibilitiesUp[i] > numberInfeasibilitiesDown[i]) {
     272            if (numberInfeasibilitiesDown[i] < bestNumber) {
     273              betterWay = -1;
     274            } else {
     275              if (changeDown[i] < bestCriterion)
     276                betterWay = -1;
     277            }
     278          } else {
     279            // up and down have same number
     280            bool better = false;
     281            if (numberInfeasibilitiesUp[i] < bestNumber) {
     282              better = true;
     283            } else if (numberInfeasibilitiesUp[i] == bestNumber) {
     284              if (CoinMin(changeUp[i], changeDown[i]) < bestCriterion)
     285                better = true;
     286              ;
     287            }
     288            if (better) {
     289              // see which way
     290              if (changeUp[i] <= changeDown[i])
     291                betterWay = 1;
     292              else
     293                betterWay = -1;
     294            }
     295          }
     296          if (betterWay) {
     297            bestCriterion = CoinMin(changeUp[i], changeDown[i]);
     298            bestNumber = thisNumber;
     299            whichObject = i;
     300            bestWay = betterWay;
     301          }
     302        }
     303      }
     304      bestCriterion = 1.0e50;
     305      for (i = 0; i < numberObjects; i++) {
     306        int thisNumber = numberInfeasibilitiesUp[i];
     307        if (thisNumber == bestNumber && changeUp) {
     308          if (changeUp[i] < bestCriterion) {
     309            bestCriterion = changeUp[i];
     310            whichObject = i;
     311            bestWay = 1;
     312          }
     313        }
    316314      }
    317315      break;
    318316    }
    319317    // set way in best
    320     if (whichObject>=0)
     318    if (whichObject >= 0)
    321319      objects[whichObject]->way(bestWay);
    322320  }
     
    327325  Equivalent to an unspecified binary variable.
    328326*/
    329 CbcSimpleIntegerFixed::CbcSimpleIntegerFixed ()
     327CbcSimpleIntegerFixed::CbcSimpleIntegerFixed()
    330328  : CbcSimpleInteger()
    331329{
     
    336334  Loads actual upper & lower bounds for the specified variable.
    337335*/
    338 CbcSimpleIntegerFixed::CbcSimpleIntegerFixed (CbcModel * model,
    339                                     int iColumn, double breakEven)
    340   : CbcSimpleInteger(model,iColumn,breakEven)
     336CbcSimpleIntegerFixed::CbcSimpleIntegerFixed(CbcModel *model,
     337  int iColumn, double breakEven)
     338  : CbcSimpleInteger(model, iColumn, breakEven)
    341339{
    342340}
    343341// Constructor from simple
    344 CbcSimpleIntegerFixed::CbcSimpleIntegerFixed (const CbcSimpleInteger & rhs)
     342CbcSimpleIntegerFixed::CbcSimpleIntegerFixed(const CbcSimpleInteger &rhs)
    345343  : CbcSimpleInteger(rhs)
    346344{
    347345}
    348346
    349 // Copy constructor 
    350 CbcSimpleIntegerFixed::CbcSimpleIntegerFixed ( const CbcSimpleIntegerFixed & rhs)
    351   :CbcSimpleInteger(rhs)
     347// Copy constructor
     348CbcSimpleIntegerFixed::CbcSimpleIntegerFixed(const CbcSimpleIntegerFixed &rhs)
     349  : CbcSimpleInteger(rhs)
    352350
    353351{
     
    361359}
    362360
    363 // Assignment operator 
    364 CbcSimpleIntegerFixed & 
    365 CbcSimpleIntegerFixed::operator=( const CbcSimpleIntegerFixed& rhs)
    366 {
    367   if (this!=&rhs) {
     361// Assignment operator
     362CbcSimpleIntegerFixed &
     363CbcSimpleIntegerFixed::operator=(const CbcSimpleIntegerFixed &rhs)
     364{
     365  if (this != &rhs) {
    368366    CbcSimpleInteger::operator=(rhs);
    369367  }
     
    371369}
    372370
    373 // Destructor 
    374 CbcSimpleIntegerFixed::~CbcSimpleIntegerFixed ()
     371// Destructor
     372CbcSimpleIntegerFixed::~CbcSimpleIntegerFixed()
    375373{
    376374}
    377375
    378376// Infeasibility - large is 0.5
    379 double 
    380 CbcSimpleIntegerFixed::infeasibility(int & preferredWay) const
    381 {
    382   OsiSolverInterface * solver = model_->solver();
    383   const double * solution = model_->testSolution();
    384   const double * lower = solver->getColLower();
    385   const double * upper = solver->getColUpper();
     377double
     378CbcSimpleIntegerFixed::infeasibility(int &preferredWay) const
     379{
     380  OsiSolverInterface *solver = model_->solver();
     381  const double *solution = model_->testSolution();
     382  const double *lower = solver->getColLower();
     383  const double *upper = solver->getColUpper();
    386384  double value = solution[columnNumber_];
    387385  value = CoinMax(value, lower[columnNumber_]);
     
    389387  /*printf("%d %g %g %g %g\n",columnNumber_,value,lower[columnNumber_],
    390388    solution[columnNumber_],upper[columnNumber_]);*/
    391   double nearest = floor(value+(1.0-breakEven_));
    392   assert (breakEven_>0.0&&breakEven_<1.0);
    393   double integerTolerance =
    394     model_->getDblParam(CbcModel::CbcIntegerTolerance);
    395   if (nearest>value)
    396     preferredWay=1;
     389  double nearest = floor(value + (1.0 - breakEven_));
     390  assert(breakEven_ > 0.0 && breakEven_ < 1.0);
     391  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
     392  if (nearest > value)
     393    preferredWay = 1;
    397394  else
    398     preferredWay=-1;
     395    preferredWay = -1;
    399396  if (preferredWay_)
    400     preferredWay=preferredWay_;
    401   double weight = fabs(value-nearest);
     397    preferredWay = preferredWay_;
     398  double weight = fabs(value - nearest);
    402399  // normalize so weight is 0.5 at break even
    403   if (nearest<value)
    404     weight = (0.5/breakEven_)*weight;
     400  if (nearest < value)
     401    weight = (0.5 / breakEven_) * weight;
    405402  else
    406     weight = (0.5/(1.0-breakEven_))*weight;
    407   if (fabs(value-nearest)<=integerTolerance) {
    408     if (upper[columnNumber_]==lower[columnNumber_])
     403    weight = (0.5 / (1.0 - breakEven_)) * weight;
     404  if (fabs(value - nearest) <= integerTolerance) {
     405    if (upper[columnNumber_] == lower[columnNumber_])
    409406      return 0.0;
    410407    else
     
    415412}
    416413// Creates a branching object
    417 CbcBranchingObject * 
    418 CbcSimpleIntegerFixed::createBranch(OsiSolverInterface * solver,
    419                                             const OsiBranchingInformation * info, int way) 
    420 {
    421   const double * solution = model_->testSolution();
    422   const double * lower = solver->getColLower();
    423   const double * upper = solver->getColUpper();
     414CbcBranchingObject *
     415CbcSimpleIntegerFixed::createBranch(OsiSolverInterface *solver,
     416  const OsiBranchingInformation *info, int way)
     417{
     418  const double *solution = model_->testSolution();
     419  const double *lower = solver->getColLower();
     420  const double *upper = solver->getColUpper();
    424421  double value = solution[columnNumber_];
    425422  value = CoinMax(value, lower[columnNumber_]);
    426423  value = CoinMin(value, upper[columnNumber_]);
    427   assert (upper[columnNumber_]>lower[columnNumber_]);
     424  assert(upper[columnNumber_] > lower[columnNumber_]);
    428425  if (!model_->hotstartSolution()) {
    429     double nearest = floor(value+0.5);
    430     double integerTolerance =
    431     model_->getDblParam(CbcModel::CbcIntegerTolerance);
    432     if (fabs(value-nearest)<integerTolerance) {
     426    double nearest = floor(value + 0.5);
     427    double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
     428    if (fabs(value - nearest) < integerTolerance) {
    433429      // adjust value
    434       if (nearest!=upper[columnNumber_])
    435         value = nearest+2.0*integerTolerance;
     430      if (nearest != upper[columnNumber_])
     431        value = nearest + 2.0 * integerTolerance;
    436432      else
    437         value = nearest-2.0*integerTolerance;
     433        value = nearest - 2.0 * integerTolerance;
    438434    }
    439435  } else {
    440     const double * hotstartSolution = model_->hotstartSolution();
     436    const double *hotstartSolution = model_->hotstartSolution();
    441437    double targetValue = hotstartSolution[columnNumber_];
    442     if (way>0)
    443       value = targetValue-0.1;
     438    if (way > 0)
     439      value = targetValue - 0.1;
    444440    else
    445       value = targetValue+0.1;
     441      value = targetValue + 0.1;
    446442  }
    447   CbcBranchingObject * branch = new CbcIntegerBranchingObject(model_,columnNumber_,way,
    448                                              value);
     443  CbcBranchingObject *branch = new CbcIntegerBranchingObject(model_, columnNumber_, way,
     444    value);
    449445  branch->setOriginalObject(this);
    450446  return branch;
  • trunk/Cbc/examples/CbcBranchUser.hpp

    r1574 r2469  
    1414class CbcBranchUserDecision : public CbcBranchDecision {
    1515public:
    16   // Default Constructor 
    17   CbcBranchUserDecision ();
     16  // Default Constructor
     17  CbcBranchUserDecision();
    1818
    19   // Copy constructor 
    20   CbcBranchUserDecision ( const CbcBranchUserDecision &);
     19  // Copy constructor
     20  CbcBranchUserDecision(const CbcBranchUserDecision &);
    2121
    2222  virtual ~CbcBranchUserDecision();
    2323
    24  /// Clone
    25   virtual CbcBranchDecision * clone() const;
     24  /// Clone
     25  virtual CbcBranchDecision *clone() const;
    2626
    27     /// Initialize i.e. before start of choosing at a node
    28   virtual void initialize(CbcModel * model);
     27  /// Initialize i.e. before start of choosing at a node
     28  virtual void initialize(CbcModel *model);
    2929
    3030  /** Returns nonzero if branching on first object is "better" than on
     
    3535     
    3636 */
    37   virtual int betterBranch(CbcBranchingObject * thisOne,
    38                             CbcBranchingObject * bestSoFar,
    39                             double changeUp, int numberInfeasibilitiesUp,
    40                             double changeDown, int numberInfeasibilitiesDown);
     37  virtual int betterBranch(CbcBranchingObject *thisOne,
     38    CbcBranchingObject *bestSoFar,
     39    double changeUp, int numberInfeasibilitiesUp,
     40    double changeDown, int numberInfeasibilitiesDown);
    4141
    4242  /** \brief Compare N branching objects. Return index of best
     
    4848
    4949  virtual int
    50   bestBranch (CbcBranchingObject ** objects, int numberObjects, int numberUnsatisfied,
    51               double * changeUp, int * numberInfeasibilitiesUp,
    52               double * changeDown, int * numberInfeasibilitiesDown,
    53               double objectiveValue) ;
     50  bestBranch(CbcBranchingObject **objects, int numberObjects, int numberUnsatisfied,
     51    double *changeUp, int *numberInfeasibilitiesUp,
     52    double *changeDown, int *numberInfeasibilitiesDown,
     53    double objectiveValue);
     54
    5455private:
    55  
    56   /// Illegal Assignment operator
    57   CbcBranchUserDecision & operator=(const CbcBranchUserDecision& rhs);
    58 
     56  /// Illegal Assignment operator
     57  CbcBranchUserDecision &operator=(const CbcBranchUserDecision &rhs);
    5958};
    6059
    6160/// Define a single integer class where branching is forced until fixed
    6261
    63 
    6462class CbcSimpleIntegerFixed : public CbcSimpleInteger {
    6563
    6664public:
    67 
    68   // Default Constructor
    69   CbcSimpleIntegerFixed ();
     65  // Default Constructor
     66  CbcSimpleIntegerFixed();
    7067
    7168  // Useful constructor - passed integer index and model index
    72   CbcSimpleIntegerFixed (CbcModel * model, int iColumn, double breakEven=0.5);
    73  
     69  CbcSimpleIntegerFixed(CbcModel *model, int iColumn, double breakEven = 0.5);
     70
    7471  // Constructor from simple
    75   CbcSimpleIntegerFixed (const CbcSimpleInteger & simple);
    76  
    77   // Copy constructor 
    78   CbcSimpleIntegerFixed ( const CbcSimpleIntegerFixed &);
    79    
     72  CbcSimpleIntegerFixed(const CbcSimpleInteger &simple);
     73
     74  // Copy constructor
     75  CbcSimpleIntegerFixed(const CbcSimpleIntegerFixed &);
     76
    8077  /// Clone
    81   virtual CbcObject * clone() const;
     78  virtual CbcObject *clone() const;
    8279
    83   // Assignment operator 
    84   CbcSimpleIntegerFixed & operator=( const CbcSimpleIntegerFixed& rhs);
     80  // Assignment operator
     81  CbcSimpleIntegerFixed &operator=(const CbcSimpleIntegerFixed &rhs);
    8582
    86   // Destructor 
    87   ~CbcSimpleIntegerFixed ();
    88  
     83  // Destructor
     84  ~CbcSimpleIntegerFixed();
     85
    8986  /// Infeasibility - large is 0.5
    90   virtual double infeasibility(int & preferredWay) const;
     87  virtual double infeasibility(int &preferredWay) const;
    9188
    9289  /** Creates a branching object
     
    10097      variables, etc.)
    10198  */
    102   virtual CbcBranchingObject * createBranch(OsiSolverInterface * solver,
    103                                             const OsiBranchingInformation * info, int way) ;
     99  virtual CbcBranchingObject *createBranch(OsiSolverInterface *solver,
     100    const OsiBranchingInformation *info, int way);
    104101
    105102protected:
  • trunk/Cbc/examples/CbcCompareUser.cpp

    r1898 r2469  
    2020
    2121*/
    22 CbcCompareUser::CbcCompareUser ()
    23   : CbcCompareBase(),
    24     weight_(-1.0),
    25     saveWeight_(0.0),
    26     numberSolutions_(0),
    27     count_(0),
    28     treeSize_(0)
    29 {
    30   test_=this;
     22CbcCompareUser::CbcCompareUser()
     23  : CbcCompareBase()
     24  , weight_(-1.0)
     25  , saveWeight_(0.0)
     26  , numberSolutions_(0)
     27  , count_(0)
     28  , treeSize_(0)
     29{
     30  test_ = this;
    3131}
    3232
    3333// Constructor with weight
    34 CbcCompareUser::CbcCompareUser (double weight)
    35   : CbcCompareBase(),
    36     weight_(weight) ,
    37     saveWeight_(0.0),
    38     numberSolutions_(0),
    39     count_(0),
    40     treeSize_(0)
    41 {
    42   test_=this;
    43 }
    44 
    45 
    46 // Copy constructor
    47 CbcCompareUser::CbcCompareUser ( const CbcCompareUser & rhs)
    48   :CbcCompareBase(rhs)
    49 
    50 {
    51   weight_=rhs.weight_;
     34CbcCompareUser::CbcCompareUser(double weight)
     35  : CbcCompareBase()
     36  , weight_(weight)
     37  , saveWeight_(0.0)
     38  , numberSolutions_(0)
     39  , count_(0)
     40  , treeSize_(0)
     41{
     42  test_ = this;
     43}
     44
     45// Copy constructor
     46CbcCompareUser::CbcCompareUser(const CbcCompareUser &rhs)
     47  : CbcCompareBase(rhs)
     48
     49{
     50  weight_ = rhs.weight_;
    5251  saveWeight_ = rhs.saveWeight_;
    53   numberSolutions_=rhs.numberSolutions_;
     52  numberSolutions_ = rhs.numberSolutions_;
    5453  count_ = rhs.count_;
    5554  treeSize_ = rhs.treeSize_;
     
    6362}
    6463
    65 // Assignment operator 
    66 CbcCompareUser & 
    67 CbcCompareUser::operator=( const CbcCompareUser& rhs)
    68 {
    69   if (this!=&rhs) {
     64// Assignment operator
     65CbcCompareUser &
     66CbcCompareUser::operator=(const CbcCompareUser &rhs)
     67{
     68  if (this != &rhs) {
    7069    CbcCompareBase::operator=(rhs);
    71     weight_=rhs.weight_;
     70    weight_ = rhs.weight_;
    7271    saveWeight_ = rhs.saveWeight_;
    73     numberSolutions_=rhs.numberSolutions_;
     72    numberSolutions_ = rhs.numberSolutions_;
    7473    count_ = rhs.count_;
    7574    treeSize_ = rhs.treeSize_;
     
    7877}
    7978
    80 // Destructor 
    81 CbcCompareUser::~CbcCompareUser ()
     79// Destructor
     80CbcCompareUser::~CbcCompareUser()
    8281{
    8382}
     
    184183
    185184// Returns true if y better than x
    186 bool
    187 CbcCompareUser::test (CbcNode * x, CbcNode * y)
    188 {
    189   if (weight_==-1.0&&(y->depth()>7||x->depth()>7)) {
     185bool CbcCompareUser::test(CbcNode *x, CbcNode *y)
     186{
     187  if (weight_ == -1.0 && (y->depth() > 7 || x->depth() > 7)) {
    190188    // before solution
    191189    /* printf("x %d %d %g, y %d %d %g\n",
     
    199197      int testX = x->depth();
    200198      int testY = y->depth();
    201       if (testX!=testY)
    202         return testX < testY;
     199      if (testX != testY)
     200        return testX < testY;
    203201      else
    204         return equalityTest(x,y); // so ties will be broken in consistent manner
     202        return equalityTest(x, y); // so ties will be broken in consistent manner
    205203    }
    206204  } else {
    207205    // after solution
    208     double weight = CoinMax(weight_,0.0);
    209     double testX =  x->objectiveValue()+ weight*x->numberUnsatisfied();
    210     double testY = y->objectiveValue() + weight*y->numberUnsatisfied();
    211     if (testX!=testY)
     206    double weight = CoinMax(weight_, 0.0);
     207    double testX = x->objectiveValue() + weight * x->numberUnsatisfied();
     208    double testY = y->objectiveValue() + weight * y->numberUnsatisfied();
     209    if (testX != testY)
    212210      return testX > testY;
    213211    else
    214       return equalityTest(x,y); // so ties will be broken in consistent manner
     212      return equalityTest(x, y); // so ties will be broken in consistent manner
    215213  }
    216214}
    217215// This allows method to change behavior as it is called
    218216// after each solution
    219 bool
    220 CbcCompareUser::newSolution(CbcModel * model,
    221                                double objectiveAtContinuous,
    222                                int numberInfeasibilitiesAtContinuous)
    223 {
    224   if (model->getSolutionCount()==model->getNumberHeuristicSolutions()&&
    225       model->getSolutionCount()<5&&model->getNodeCount()<500)
    226     return (false) ; // solution was got by rounding
     217bool CbcCompareUser::newSolution(CbcModel *model,
     218  double objectiveAtContinuous,
     219  int numberInfeasibilitiesAtContinuous)
     220{
     221  if (model->getSolutionCount() == model->getNumberHeuristicSolutions() && model->getSolutionCount() < 5 && model->getNodeCount() < 500)
     222    return (false); // solution was got by rounding
    227223  // set to get close to this solution
    228   double costPerInteger =
    229     (model->getObjValue()-objectiveAtContinuous)/
    230     ((double) numberInfeasibilitiesAtContinuous);
    231   weight_ = 0.95*costPerInteger;
    232   saveWeight_ = 0.95*weight_;
     224  double costPerInteger = (model->getObjValue() - objectiveAtContinuous) / ((double)numberInfeasibilitiesAtContinuous);
     225  weight_ = 0.95 * costPerInteger;
     226  saveWeight_ = 0.95 * weight_;
    233227  numberSolutions_++;
    234   if (numberSolutions_>5)
    235     weight_ =0.0; // this searches on objective
    236   return (true) ;
    237 }
    238 // This allows method to change behavior
    239 bool
    240 CbcCompareUser::every1000Nodes(CbcModel * model, int numberNodes)
    241 {
    242   double saveWeight=weight_;
    243   int numberNodes1000 = numberNodes/1000;
    244   if (numberNodes>10000) {
    245     weight_ =0.0; // this searches on objective
     228  if (numberSolutions_ > 5)
     229    weight_ = 0.0; // this searches on objective
     230  return (true);
     231}
     232// This allows method to change behavior
     233bool CbcCompareUser::every1000Nodes(CbcModel *model, int numberNodes)
     234{
     235  double saveWeight = weight_;
     236  int numberNodes1000 = numberNodes / 1000;
     237  if (numberNodes > 10000) {
     238    weight_ = 0.0; // this searches on objective
    246239    // but try a bit of other stuff
    247     if ((numberNodes1000%4)==1)
    248       weight_=saveWeight_;
    249   } else if (numberNodes==1000&&weight_==-2.0) {
    250     weight_=-1.0; // Go to depth first
     240    if ((numberNodes1000 % 4) == 1)
     241      weight_ = saveWeight_;
     242  } else if (numberNodes == 1000 && weight_ == -2.0) {
     243    weight_ = -1.0; // Go to depth first
    251244  }
    252245  // get size of tree
    253246  treeSize_ = model->tree()->size();
    254   if (treeSize_>10000) {
    255     int n1 = model->solver()->getNumRows()+model->solver()->getNumCols();
     247  if (treeSize_ > 10000) {
     248    int n1 = model->solver()->getNumRows() + model->solver()->getNumCols();
    256249    int n2 = model->numberObjects();
    257     double size = n1*0.1 + n2*2.0;
     250    double size = n1 * 0.1 + n2 * 2.0;
    258251    // set weight to reduce size most of time
    259     if (treeSize_*size>5.0e7)
    260       weight_=-1.0;
    261     else if ((numberNodes1000%4)==0&&treeSize_*size>1.0e6)
    262       weight_=-1.0;
    263     else if ((numberNodes1000%4)==1)
    264       weight_=0.0;
     252    if (treeSize_ * size > 5.0e7)
     253      weight_ = -1.0;
     254    else if ((numberNodes1000 % 4) == 0 && treeSize_ * size > 1.0e6)
     255      weight_ = -1.0;
     256    else if ((numberNodes1000 % 4) == 1)
     257      weight_ = 0.0;
    265258    else
    266       weight_=saveWeight_;
    267   }
    268   return (weight_!=saveWeight);
     259      weight_ = saveWeight_;
     260  }
     261  return (weight_ != saveWeight);
    269262}
    270263// Returns true if wants code to do scan with alternate criterion
    271 bool
    272 CbcCompareUser::fullScan() const
     264bool CbcCompareUser::fullScan() const
    273265{
    274266  return false;
    275267}
    276268// This is alternate test function
    277 bool
    278 CbcCompareUser::alternateTest (CbcNode * x, CbcNode * y)
     269bool CbcCompareUser::alternateTest(CbcNode *x, CbcNode *y)
    279270{
    280271  // not used
  • trunk/Cbc/examples/CbcCompareUser.hpp

    r1574 r2469  
    1515   less 2%
    1616*/
    17 class CbcCompareUser  : public CbcCompareBase {
     17class CbcCompareUser : public CbcCompareBase {
    1818public:
    19   // Default Constructor 
    20   CbcCompareUser () ;
     19  // Default Constructor
     20  CbcCompareUser();
    2121  // Constructor with weight
    22   CbcCompareUser (double weight);
     22  CbcCompareUser(double weight);
    2323
    24   // Copy constructor 
    25   CbcCompareUser ( const CbcCompareUser &rhs);
    26    
    27   // Assignment operator 
    28   CbcCompareUser & operator=( const CbcCompareUser& rhs);
     24  // Copy constructor
     25  CbcCompareUser(const CbcCompareUser &rhs);
     26
     27  // Assignment operator
     28  CbcCompareUser &operator=(const CbcCompareUser &rhs);
    2929
    3030  /// Clone
    31   virtual CbcCompareBase * clone() const;
     31  virtual CbcCompareBase *clone() const;
    3232
    33   ~CbcCompareUser() ;
     33  ~CbcCompareUser();
    3434  /* This returns true if weighted value of node y is less than
    3535     weighted value of node x */
    36   virtual bool test (CbcNode * x, CbcNode * y) ;
     36  virtual bool test(CbcNode *x, CbcNode *y);
    3737  /// This is alternate test function
    38   virtual bool alternateTest (CbcNode * x, CbcNode * y);
     38  virtual bool alternateTest(CbcNode *x, CbcNode *y);
    3939  // This allows method to change behavior as it is called
    4040  // after each solution
    41   virtual bool newSolution(CbcModel * model,
    42                            double objectiveAtContinuous,
    43                            int numberInfeasibilitiesAtContinuous) ;
     41  virtual bool newSolution(CbcModel *model,
     42    double objectiveAtContinuous,
     43    int numberInfeasibilitiesAtContinuous);
    4444  /// Returns true if wants code to do scan with alternate criterion
    4545  virtual bool fullScan() const;
    46   // This allows method to change behavior 
     46  // This allows method to change behavior
    4747  // Return true if want tree re-sorted
    48   virtual bool every1000Nodes(CbcModel * model,int numberNodes);
     48  virtual bool every1000Nodes(CbcModel *model, int numberNodes);
    4949
    5050  /* if weight == -1.0 then depth first (before solution)
     
    5252  */
    5353  inline double getWeight() const
    54   { return weight_;}
     54  {
     55    return weight_;
     56  }
    5557  inline void setWeight(double weight)
    56   { weight_ = weight;}
     58  {
     59    weight_ = weight;
     60  }
     61
    5762protected:
    5863  // Weight for each infeasibility
  • trunk/Cbc/examples/CbcSolver2.cpp

    r1574 r2469  
    1818#include "CoinModel.hpp"
    1919
    20 static int timesBad_=0;
    21 static int iterationsBad_=0;
     20static int timesBad_ = 0;
     21static int iterationsBad_ = 0;
    2222//#############################################################################
    2323// Solve methods
     
    2626{
    2727  modelPtr_->scaling(0);
    28   setBasis(basis_,modelPtr_);
     28  setBasis(basis_, modelPtr_);
    2929  // Do long thin by sprint
    3030  ClpSolve options;
    3131  options.setSolveType(ClpSolve::usePrimalorSprint);
    3232  options.setPresolveType(ClpSolve::presolveOff);
    33   options.setSpecialOption(1,3,30);
     33  options.setSpecialOption(1, 3, 30);
    3434  modelPtr_->initialSolve(options);
    3535  basis_ = getBasis(modelPtr_);
     
    4141{
    4242  int numberColumns = modelPtr_->numberColumns();
    43   if ((count_<10&&algorithm_==2)||!algorithm_) {
     43  if ((count_ < 10 && algorithm_ == 2) || !algorithm_) {
    4444    OsiClpSolverInterface::resolve();
    45     if (modelPtr_->status()==0) {
     45    if (modelPtr_->status() == 0) {
    4646      count_++;
    47       double * solution = modelPtr_->primalColumnSolution();
     47      double *solution = modelPtr_->primalColumnSolution();
    4848      int i;
    49       for (i=0;i<numberColumns;i++) {
    50         if (solution[i]>1.0e-6||modelPtr_->getStatus(i)==ClpSimplex::basic) {
    51           node_[i]=CoinMax(count_,node_[i]);
    52           howMany_[i]++;
    53         }
     49      for (i = 0; i < numberColumns; i++) {
     50        if (solution[i] > 1.0e-6 || modelPtr_->getStatus(i) == ClpSimplex::basic) {
     51          node_[i] = CoinMax(count_, node_[i]);
     52          howMany_[i]++;
     53        }
    5454      }
    5555    } else {
    56       if (!algorithm_==2)
    57         printf("infeasible early on\n");
     56      if (!algorithm_ == 2)
     57        printf("infeasible early on\n");
    5858    }
    5959  } else {
    6060    // use counts
    61     int numberRows=modelPtr_->numberRows();
    62     int * whichRow = new int[numberRows];
    63     int * whichColumn = new int [numberColumns];
     61    int numberRows = modelPtr_->numberRows();
     62    int *whichRow = new int[numberRows];
     63    int *whichColumn = new int[numberColumns];
    6464    int i;
    65     const double * lower = modelPtr_->columnLower();
    66     const double * upper = modelPtr_->columnUpper();
    67     const double * rowUpper = modelPtr_->rowUpper();
    68     bool equality=false;
    69     for (i=0;i<numberRows;i++) {
    70       if (rowUpper[i]==1.0) {
    71         equality=true;
     65    const double *lower = modelPtr_->columnLower();
     66    const double *upper = modelPtr_->columnUpper();
     67    const double *rowUpper = modelPtr_->rowUpper();
     68    bool equality = false;
     69    for (i = 0; i < numberRows; i++) {
     70      if (rowUpper[i] == 1.0) {
     71        equality = true;
    7272        break;
    7373      }
    7474    }
    75     setBasis(basis_,modelPtr_);
    76     int nNewCol=0;
     75    setBasis(basis_, modelPtr_);
     76    int nNewCol = 0;
    7777    // Column copy
    7878    //const double * element = modelPtr_->matrix()->getElements();
    79     const int * row = modelPtr_->matrix()->getIndices();
    80     const CoinBigIndex * columnStart = modelPtr_->matrix()->getVectorStarts();
    81     const int * columnLength = modelPtr_->matrix()->getVectorLengths();
    82    
    83     int * rowActivity = new int[numberRows];
    84     memset(rowActivity,0,numberRows*sizeof(int));
    85     int * rowActivity2 = new int[numberRows];
    86     memset(rowActivity2,0,numberRows*sizeof(int));
    87     char * mark = new char[numberColumns];
    88     memset(mark,0,numberColumns);
     79    const int *row = modelPtr_->matrix()->getIndices();
     80    const CoinBigIndex *columnStart = modelPtr_->matrix()->getVectorStarts();
     81    const int *columnLength = modelPtr_->matrix()->getVectorLengths();
     82
     83    int *rowActivity = new int[numberRows];
     84    memset(rowActivity, 0, numberRows * sizeof(int));
     85    int *rowActivity2 = new int[numberRows];
     86    memset(rowActivity2, 0, numberRows * sizeof(int));
     87    char *mark = new char[numberColumns];
     88    memset(mark, 0, numberColumns);
    8989    // Get rows which are satisfied
    90     for (i=0;i<numberColumns;i++) {
    91       if (lower[i]>0.0) {
     90    for (i = 0; i < numberColumns; i++) {
     91      if (lower[i] > 0.0) {
    9292        CoinBigIndex j;
    93         for (j=columnStart[i];
    94              j<columnStart[i]+columnLength[i];j++) {
    95           int iRow=row[j];
    96           rowActivity2[iRow] ++;
     93        for (j = columnStart[i];
     94             j < columnStart[i] + columnLength[i]; j++) {
     95          int iRow = row[j];
     96          rowActivity2[iRow]++;
    9797        }
    9898      } else if (!upper[i]) {
    99         mark[i]=2; // no good
     99        mark[i] = 2; // no good
    100100      }
    101101    }
    102102    // If equality - check not infeasible
    103103    if (equality) {
    104       bool feasible=true;
    105       for (i=0;i<numberRows;i++) {
    106         if (rowActivity2[i]>1) {
    107           feasible=false;
     104      bool feasible = true;
     105      for (i = 0; i < numberRows; i++) {
     106        if (rowActivity2[i] > 1) {
     107          feasible = false;
    108108          break;
    109109        }
    110110      }
    111111      if (!feasible) {
    112         delete [] rowActivity;
    113         delete [] rowActivity2;
     112        delete[] rowActivity;
     113        delete[] rowActivity2;
    114114        modelPtr_->setProblemStatus(1);
    115         delete [] whichRow;
    116         delete [] whichColumn;
    117         delete [] mark;
     115        delete[] whichRow;
     116        delete[] whichColumn;
     117        delete[] mark;
    118118        printf("infeasible by inspection (over)\n");
    119119        return;
    120120      }
    121121    }
    122     int nNoGood=0;
    123     for (i=0;i<numberColumns;i++) {
    124       if (mark[i]==2) {
     122    int nNoGood = 0;
     123    for (i = 0; i < numberColumns; i++) {
     124      if (mark[i] == 2) {
    125125        nNoGood++;
    126126        continue;
    127127      }
    128128      bool choose;
    129       if (algorithm_==1)
     129      if (algorithm_ == 1)
    130130        choose = true;
    131131      else
    132         choose = (node_[i]>count_-memory_&&node_[i]>0);
     132        choose = (node_[i] > count_ - memory_ && node_[i] > 0);
    133133      bool any;
    134134      if (equality) {
    135135        // See if forced to be zero
    136136        CoinBigIndex j;
    137         any=true;
    138         for (j=columnStart[i];
    139              j<columnStart[i]+columnLength[i];j++) {
    140           int iRow=row[j];
     137        any = true;
     138        for (j = columnStart[i];
     139             j < columnStart[i] + columnLength[i]; j++) {
     140          int iRow = row[j];
    141141          if (rowActivity2[iRow])
    142             any=false; // can't be in
     142            any = false; // can't be in
    143143        }
    144144      } else {
    145145        // See if not useful
    146146        CoinBigIndex j;
    147         any=false;
    148         for (j=columnStart[i];
    149              j<columnStart[i]+columnLength[i];j++) {
    150           int iRow=row[j];
     147        any = false;
     148        for (j = columnStart[i];
     149             j < columnStart[i] + columnLength[i]; j++) {
     150          int iRow = row[j];
    151151          if (!rowActivity2[iRow])
    152             any=true; // useful
    153         }
    154       }
    155       if (!any&&!lower[i]) {
    156         choose=false;
     152            any = true; // useful
     153        }
     154      }
     155      if (!any && !lower[i]) {
     156        choose = false;
    157157        // and say can't be useful
    158         mark[i]=2;
     158        mark[i] = 2;
    159159        nNoGood++;
    160160      }
    161       if (strategy_&&modelPtr_->getColumnStatus(i)==ClpSimplex::basic)
    162         choose=true;
    163       if (choose||lower[i]>0.0) {
    164         mark[i]=1;
    165         whichColumn[nNewCol++]=i;
     161      if (strategy_ && modelPtr_->getColumnStatus(i) == ClpSimplex::basic)
     162        choose = true;
     163      if (choose || lower[i] > 0.0) {
     164        mark[i] = 1;
     165        whichColumn[nNewCol++] = i;
    166166        CoinBigIndex j;
    167167        double value = upper[i];
    168168        if (value) {
    169           for (j=columnStart[i];
    170                j<columnStart[i]+columnLength[i];j++) {
    171             int iRow=row[j];
    172             rowActivity[iRow] ++;
     169          for (j = columnStart[i];
     170               j < columnStart[i] + columnLength[i]; j++) {
     171            int iRow = row[j];
     172            rowActivity[iRow]++;
    173173          }
    174174        }
     
    178178    CoinModel build;
    179179    if (equality) {
    180       int row=0;
    181       for (i=0;i<numberRows;i++) {
     180      int row = 0;
     181      for (i = 0; i < numberRows; i++) {
    182182        // put in all rows if wanted
    183         if(strategy_)
    184           rowActivity2[i]=0;
     183        if (strategy_)
     184          rowActivity2[i] = 0;
    185185        if (!rowActivity2[i]) {
    186           double element=1.0;
    187           build.addColumn(1,&row,&element,0.0,1.0,1.0e8); // large cost
     186          double element = 1.0;
     187          build.addColumn(1, &row, &element, 0.0, 1.0, 1.0e8); // large cost
    188188          row++;
    189189        }
    190190      }
    191191    }
    192     int nOK=0;
    193     int nNewRow=0;
    194     for (i=0;i<numberRows;i++) {
     192    int nOK = 0;
     193    int nNewRow = 0;
     194    for (i = 0; i < numberRows; i++) {
    195195      if (rowActivity[i])
    196196        nOK++;
    197197      if (!rowActivity2[i])
    198         whichRow[nNewRow++]=i; // not satisfied
     198        whichRow[nNewRow++] = i; // not satisfied
    199199      else
    200         modelPtr_->setRowStatus(i,ClpSimplex::basic); // make slack basic
    201     }
    202     if (nOK<numberRows) {
    203       for (i=0;i<numberColumns;i++) {
     200        modelPtr_->setRowStatus(i, ClpSimplex::basic); // make slack basic
     201    }
     202    if (nOK < numberRows) {
     203      for (i = 0; i < numberColumns; i++) {
    204204        if (!mark[i]) {
    205205          CoinBigIndex j;
    206           int good=0;
    207           for (j=columnStart[i];
    208                j<columnStart[i]+columnLength[i];j++) {
    209             int iRow=row[j];
     206          int good = 0;
     207          for (j = columnStart[i];
     208               j < columnStart[i] + columnLength[i]; j++) {
     209            int iRow = row[j];
    210210            if (!rowActivity[iRow]) {
    211               rowActivity[iRow] ++;
     211              rowActivity[iRow]++;
    212212              good++;
    213213            }
    214214          }
    215215          if (good) {
    216             nOK+=good;
    217             whichColumn[nNewCol++]=i;
     216            nOK += good;
     217            whichColumn[nNewCol++] = i;
    218218          }
    219219        }
    220220      }
    221221    }
    222     delete [] rowActivity;
    223     delete [] rowActivity2;
    224     if (nOK<numberRows) {
     222    delete[] rowActivity;
     223    delete[] rowActivity2;
     224    if (nOK < numberRows) {
    225225      modelPtr_->setProblemStatus(1);
    226       delete [] whichRow;
    227       delete [] whichColumn;
    228       delete [] mark;
     226      delete[] whichRow;
     227      delete[] whichColumn;
     228      delete[] mark;
    229229      printf("infeasible by inspection\n");
    230230      return;
    231231    }
    232     bool allIn=false;
    233     if (nNewCol+nNoGood+numberRows>numberColumns) {
     232    bool allIn = false;
     233    if (nNewCol + nNoGood + numberRows > numberColumns) {
    234234      // add in all
    235       allIn=true;
    236       for (i=0;i<numberColumns;i++) {
     235      allIn = true;
     236      for (i = 0; i < numberColumns; i++) {
    237237        if (!mark[i]) {
    238           whichColumn[nNewCol++]=i;
    239         }
    240       }
    241     }
    242     ClpSimplex *  temp = new ClpSimplex(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
     238          whichColumn[nNewCol++] = i;
     239        }
     240      }
     241    }
     242    ClpSimplex *temp = new ClpSimplex(modelPtr_, nNewRow, whichRow, nNewCol, whichColumn);
    243243    if (equality)
    244244      temp->addColumns(build);
    245245    temp->setLogLevel(1);
    246246    printf("small has %d rows and %d columns (%d impossible to help) %s\n",
    247            nNewRow,nNewCol,nNoGood,allIn ? "all in" : "");
    248     temp->setSpecialOptions(128+512);
     247      nNewRow, nNewCol, nNoGood, allIn ? "all in" : "");
     248    temp->setSpecialOptions(128 + 512);
    249249    temp->setDualObjectiveLimit(1.0e50);
    250250    temp->dual();
    251     assert (!temp->status());
    252     double * solution = modelPtr_->primalColumnSolution();
    253     const double * solution2 = temp->primalColumnSolution();
    254     memset(solution,0,numberColumns*sizeof(double));
    255     for (i=0;i<nNewCol;i++) {
     251    assert(!temp->status());
     252    double *solution = modelPtr_->primalColumnSolution();
     253    const double *solution2 = temp->primalColumnSolution();
     254    memset(solution, 0, numberColumns * sizeof(double));
     255    for (i = 0; i < nNewCol; i++) {
    256256      int iColumn = whichColumn[i];
    257       solution[iColumn]=solution2[i];
    258       modelPtr_->setStatus(iColumn,temp->getStatus(i));
    259     }
    260     double * rowSolution = modelPtr_->primalRowSolution();
    261     const double * rowSolution2 = temp->primalRowSolution();
    262     double * dual = modelPtr_->dualRowSolution();
    263     const double * dual2 = temp->dualRowSolution();
    264     memset(dual,0,numberRows*sizeof(double));
    265     for (i=0;i<nNewRow;i++) {
    266       int iRow=whichRow[i];
    267       modelPtr_->setRowStatus(iRow,temp->getRowStatus(i));
    268       rowSolution[iRow]=rowSolution2[i];
    269       dual[iRow]=dual2[i];
     257      solution[iColumn] = solution2[i];
     258      modelPtr_->setStatus(iColumn, temp->getStatus(i));
     259    }
     260    double *rowSolution = modelPtr_->primalRowSolution();
     261    const double *rowSolution2 = temp->primalRowSolution();
     262    double *dual = modelPtr_->dualRowSolution();
     263    const double *dual2 = temp->dualRowSolution();
     264    memset(dual, 0, numberRows * sizeof(double));
     265    for (i = 0; i < nNewRow; i++) {
     266      int iRow = whichRow[i];
     267      modelPtr_->setRowStatus(iRow, temp->getRowStatus(i));
     268      rowSolution[iRow] = rowSolution2[i];
     269      dual[iRow] = dual2[i];
    270270    }
    271271    // See if optimal
    272     double * dj = modelPtr_->dualColumnSolution();
     272    double *dj = modelPtr_->dualColumnSolution();
    273273    // get reduced cost for large problem
    274274    // this assumes minimization
    275     memcpy(dj,modelPtr_->objective(),numberColumns*sizeof(double));
    276     modelPtr_->transposeTimes(-1.0,dual,dj);
     275    memcpy(dj, modelPtr_->objective(), numberColumns * sizeof(double));
     276    modelPtr_->transposeTimes(-1.0, dual, dj);
    277277    modelPtr_->setObjectiveValue(temp->objectiveValue());
    278278    modelPtr_->setProblemStatus(0);
    279     int nBad=0;
    280     for (i=0;i<numberColumns;i++) {
    281       if (modelPtr_->getStatus(i)==ClpSimplex::atLowerBound
    282           &&upper[i]>lower[i]&&dj[i]<-1.0e-5)
     279    int nBad = 0;
     280    for (i = 0; i < numberColumns; i++) {
     281      if (modelPtr_->getStatus(i) == ClpSimplex::atLowerBound
     282        && upper[i] > lower[i] && dj[i] < -1.0e-5)
    283283        nBad++;
    284284    }
     
    286286    //temp->writeMps("badb.mps");
    287287    delete temp;
    288     if (nBad&&!allIn) {
    289       assert (algorithm_==2);
     288    if (nBad && !allIn) {
     289      assert(algorithm_ == 2);
    290290      //printf("%d bad\n",nBad);
    291291      timesBad_++;
    292292      // just non mark==2
    293       int nAdded=0;
    294       for (i=0;i<numberColumns;i++) {
     293      int nAdded = 0;
     294      for (i = 0; i < numberColumns; i++) {
    295295        if (!mark[i]) {
    296           whichColumn[nNewCol++]=i;
     296          whichColumn[nNewCol++] = i;
    297297          nAdded++;
    298298        }
    299299      }
    300       assert (nAdded);
     300      assert(nAdded);
    301301      {
    302         temp = new ClpSimplex(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
     302        temp = new ClpSimplex(modelPtr_, nNewRow, whichRow, nNewCol, whichColumn);
    303303        if (equality)
    304304          temp->addColumns(build);
    305305        temp->setLogLevel(2);
    306         temp->setSpecialOptions(128+512);
     306        temp->setSpecialOptions(128 + 512);
    307307        temp->setDualObjectiveLimit(1.0e50);
    308308        temp->primal(1);
    309         assert (!temp->status());
    310         double * solution = modelPtr_->primalColumnSolution();
    311         const double * solution2 = temp->primalColumnSolution();
    312         memset(solution,0,numberColumns*sizeof(double));
    313         for (i=0;i<nNewCol;i++) {
     309        assert(!temp->status());
     310        double *solution = modelPtr_->primalColumnSolution();
     311        const double *solution2 = temp->primalColumnSolution();
     312        memset(solution, 0, numberColumns * sizeof(double));
     313        for (i = 0; i < nNewCol; i++) {
    314314          int iColumn = whichColumn[i];
    315           solution[iColumn]=solution2[i];
    316           modelPtr_->setStatus(iColumn,temp->getStatus(i));
    317         }
    318         double * rowSolution = modelPtr_->primalRowSolution();
    319         const double * rowSolution2 = temp->primalRowSolution();
    320         double * dual = modelPtr_->dualRowSolution();
    321         const double * dual2 = temp->dualRowSolution();
    322         memset(dual,0,numberRows*sizeof(double));
    323         for (i=0;i<nNewRow;i++) {
    324           int iRow=whichRow[i];
    325           modelPtr_->setRowStatus(iRow,temp->getRowStatus(i));
    326           rowSolution[iRow]=rowSolution2[i];
    327           dual[iRow]=dual2[i];
     315          solution[iColumn] = solution2[i];
     316          modelPtr_->setStatus(iColumn, temp->getStatus(i));
     317        }
     318        double *rowSolution = modelPtr_->primalRowSolution();
     319        const double *rowSolution2 = temp->primalRowSolution();
     320        double *dual = modelPtr_->dualRowSolution();
     321        const double *dual2 = temp->dualRowSolution();
     322        memset(dual, 0, numberRows * sizeof(double));
     323        for (i = 0; i < nNewRow; i++) {
     324          int iRow = whichRow[i];
     325          modelPtr_->setRowStatus(iRow, temp->getRowStatus(i));
     326          rowSolution[iRow] = rowSolution2[i];
     327          dual[iRow] = dual2[i];
    328328        }
    329329        modelPtr_->setObjectiveValue(temp->objectiveValue());
    330330        modelPtr_->setProblemStatus(0);
    331331        iterationsBad_ += temp->numberIterations();
    332         printf("clean %d\n",temp->numberIterations());
     332        printf("clean %d\n", temp->numberIterations());
    333333        delete temp;
    334334      }
    335335    }
    336     delete [] mark;
    337     delete [] whichRow;
    338     delete [] whichColumn;
     336    delete[] mark;
     337    delete[] whichRow;
     338    delete[] whichColumn;
    339339    basis_ = getBasis(modelPtr_);
    340340    modelPtr_->setSpecialOptions(0);
    341341    count_++;
    342     if ((count_%100)==0&&algorithm_==2)
    343       printf("count %d, bad %d - iterations %d\n",count_,timesBad_,iterationsBad_);
    344     for (i=0;i<numberColumns;i++) {
    345       if (solution[i]>1.0e-6||modelPtr_->getStatus(i)==ClpSimplex::basic) {
    346         node_[i]=CoinMax(count_,node_[i]);
    347         howMany_[i]++;
    348       }
    349     }
    350     if (modelPtr_->objectiveValue()>=modelPtr_->dualObjectiveLimit())
     342    if ((count_ % 100) == 0 && algorithm_ == 2)
     343      printf("count %d, bad %d - iterations %d\n", count_, timesBad_, iterationsBad_);
     344    for (i = 0; i < numberColumns; i++) {
     345      if (solution[i] > 1.0e-6 || modelPtr_->getStatus(i) == ClpSimplex::basic) {
     346        node_[i] = CoinMax(count_, node_[i]);
     347        howMany_[i]++;
     348      }
     349    }
     350    if (modelPtr_->objectiveValue() >= modelPtr_->dualObjectiveLimit())
    351351      modelPtr_->setProblemStatus(1);
    352352  }
     
    358358
    359359//-------------------------------------------------------------------
    360 // Default Constructor 
    361 //-------------------------------------------------------------------
    362 CbcSolver2::CbcSolver2 ()
     360// Default Constructor
     361//-------------------------------------------------------------------
     362CbcSolver2::CbcSolver2()
    363363  : OsiClpSolverInterface()
    364364{
    365   node_=NULL;
    366   howMany_=NULL;
    367   count_=0;
     365  node_ = NULL;
     366  howMany_ = NULL;
     367  count_ = 0;
    368368  model_ = NULL;
    369   memory_=300;
    370   algorithm_=0;
    371   strategy_=0;
     369  memory_ = 300;
     370  algorithm_ = 0;
     371  strategy_ = 0;
    372372}
    373373
     
    375375// Clone
    376376//-------------------------------------------------------------------
    377 OsiSolverInterface * 
     377OsiSolverInterface *
    378378CbcSolver2::clone(bool CopyData) const
    379379{
     
    386386}
    387387
    388 
    389 //-------------------------------------------------------------------
    390 // Copy constructor
    391 //-------------------------------------------------------------------
    392 CbcSolver2::CbcSolver2 (
    393                   const CbcSolver2 & rhs)
     388//-------------------------------------------------------------------
     389// Copy constructor
     390//-------------------------------------------------------------------
     391CbcSolver2::CbcSolver2(
     392  const CbcSolver2 &rhs)
    394393  : OsiClpSolverInterface(rhs)
    395394{
    396395  model_ = rhs.model_;
    397396  int numberColumns = modelPtr_->numberColumns();
    398   node_=CoinCopyOfArray(rhs.node_,numberColumns);
    399   howMany_=CoinCopyOfArray(rhs.howMany_,numberColumns);
    400   count_=rhs.count_;
    401   memory_=rhs.memory_;
    402   algorithm_=rhs.algorithm_;
    403   strategy_=rhs.strategy_;
    404 }
    405 
    406 //-------------------------------------------------------------------
    407 // Destructor 
    408 //-------------------------------------------------------------------
    409 CbcSolver2::~CbcSolver2 ()
    410 {
    411   delete [] node_;
    412   delete [] howMany_;
    413 }
    414 
    415 //-------------------------------------------------------------------
    416 // Assignment operator 
     397  node_ = CoinCopyOfArray(rhs.node_, numberColumns);
     398  howMany_ = CoinCopyOfArray(rhs.howMany_, numberColumns);
     399  count_ = rhs.count_;
     400  memory_ = rhs.memory_;
     401  algorithm_ = rhs.algorithm_;
     402  strategy_ = rhs.strategy_;
     403}
     404
     405//-------------------------------------------------------------------
     406// Destructor
     407//-------------------------------------------------------------------
     408CbcSolver2::~CbcSolver2()
     409{
     410  delete[] node_;
     411  delete[] howMany_;
     412}
     413
     414//-------------------------------------------------------------------
     415// Assignment operator
    417416//-------------------------------------------------------------------
    418417CbcSolver2 &
    419 CbcSolver2::operator=(const CbcSolver2& rhs)
    420 {
    421   if (this != &rhs) { 
     418CbcSolver2::operator=(const CbcSolver2 &rhs)
     419{
     420  if (this != &rhs) {
    422421    OsiClpSolverInterface::operator=(rhs);
    423     delete [] node_;
    424     delete [] howMany_;
     422    delete[] node_;
     423    delete[] howMany_;
    425424    model_ = rhs.model_;
    426425    int numberColumns = modelPtr_->numberColumns();
    427     node_=CoinCopyOfArray(rhs.node_,numberColumns);
    428     howMany_=CoinCopyOfArray(rhs.howMany_,numberColumns);
    429     count_=rhs.count_;
    430     memory_=rhs.memory_;
    431     algorithm_=rhs.algorithm_;
    432     strategy_=rhs.strategy_;
     426    node_ = CoinCopyOfArray(rhs.node_, numberColumns);
     427    howMany_ = CoinCopyOfArray(rhs.howMany_, numberColumns);
     428    count_ = rhs.count_;
     429    memory_ = rhs.memory_;
     430    algorithm_ = rhs.algorithm_;
     431    strategy_ = rhs.strategy_;
    433432  }
    434433  return *this;
     
    437436// Real initializer
    438437//-------------------------------------------------------------------
    439 void
    440 CbcSolver2::initialize (CbcModel * model, const char * keep)
    441 {
    442   model_=model;
     438void CbcSolver2::initialize(CbcModel *model, const char *keep)
     439{
     440  model_ = model;
    443441  int numberColumns = modelPtr_->numberColumns();
    444442  if (numberColumns) {
    445443    node_ = new int[numberColumns];
    446444    howMany_ = new int[numberColumns];
    447     for (int i=0;i<numberColumns;i++) {
    448       if (keep&&keep[i])
    449         node_[i]=COIN_INT_MAX;
     445    for (int i = 0; i < numberColumns; i++) {
     446      if (keep && keep[i])
     447        node_[i] = COIN_INT_MAX;
    450448      else
    451         node_[i]=0;
    452       howMany_[i]=0;
     449        node_[i] = 0;
     450      howMany_[i] = 0;
    453451    }
    454452  } else {
    455     node_=NULL;
    456     howMany_=NULL;
     453    node_ = NULL;
     454    howMany_ = NULL;
    457455  }
    458456}
  • trunk/Cbc/examples/CbcSolver2.hpp

    r1574 r2469  
    2626  /**@name Solve methods */
    2727  //@{
    28     /// Solve initial LP relaxation
    29     virtual void initialSolve();
     28  /// Solve initial LP relaxation
     29  virtual void initialSolve();
    3030
    31     /// Resolve an LP relaxation after problem modification
    32     virtual void resolve();
     31  /// Resolve an LP relaxation after problem modification
     32  virtual void resolve();
    3333
    3434  //@}
    3535
    36 
    3736  /**@name Constructors and destructors */
    3837  //@{
    39     /// Default Constructor
    40     CbcSolver2 ();
    41    
    42     /// Clone
    43     virtual OsiSolverInterface * clone(bool CopyData=true) const;
    44    
    45     /// Copy constructor
    46     CbcSolver2 (const CbcSolver2 &);
    47    
    48     /// Assignment operator
    49     CbcSolver2 & operator=(const CbcSolver2& rhs);
    50    
    51     /// Destructor
    52     virtual ~CbcSolver2 ();
     38  /// Default Constructor
     39  CbcSolver2();
     40
     41  /// Clone
     42  virtual OsiSolverInterface *clone(bool CopyData = true) const;
     43
     44  /// Copy constructor
     45  CbcSolver2(const CbcSolver2 &);
     46
     47  /// Assignment operator
     48  CbcSolver2 &operator=(const CbcSolver2 &rhs);
     49
     50  /// Destructor
     51  virtual ~CbcSolver2();
    5352
    5453  //@}
    55 
    5654
    5755  /**@name Sets and Getss */
    5856  //@{
    5957  /// Setup arrays - ones in keep will always be in
    60   void initialize(CbcModel * model, const char * keep);
     58  void initialize(CbcModel *model, const char *keep);
    6159  /// get which ones have been used
    62   inline const int * when() const
    63   { return node_;}
     60  inline const int *when() const
     61  {
     62    return node_;
     63  }
    6464  /// Get memory (i.e. how recent use should be)
    6565  inline int getMemory() const
    66   { return memory_;}
     66  {
     67    return memory_;
     68  }
    6769  /// Get current count
    6870  inline int getCount() const
    69   { return count_;}
     71  {
     72    return count_;
     73  }
    7074  /// Set memory (i.e. how recent use should be)
    7175  inline void setMemory(int value)
    72   { memory_=value;}
     76  {
     77    memory_ = value;
     78  }
    7379  /// Say whether to just count usage
    7480  inline void setAlgorithm(int value)
    75   { algorithm_=value;}
     81  {
     82    algorithm_ = value;
     83  }
    7684  /// Say whether to just count usage
    7785  inline int getAlgorithm() const
    78   { return algorithm_;}
     86  {
     87    return algorithm_;
     88  }
    7989  /// Strategy
    8090  inline void setStrategy(int value)
    81   { strategy_=value;}
     91  {
     92    strategy_ = value;
     93  }
    8294  /// Strategy
    8395  inline int getStrategy() const
    84   { return strategy_;}
     96  {
     97    return strategy_;
     98  }
    8599  //@}
    86100
     
    88102
    89103private:
    90  
    91104  /**@name Private member data */
    92105  //@{
    93106  /// Node number when variable last in problem
    94   int * node_;
     107  int *node_;
    95108  /// How many times in problem
    96   int * howMany_;
     109  int *howMany_;
    97110  /// Pointer back to model
    98   CbcModel * model_;
     111  CbcModel *model_;
    99112  /// Counter
    100113  int count_;
  • trunk/Cbc/examples/CbcSolver3.cpp

    r1898 r2469  
    3333#include "CglTwomir.hpp"
    3434
    35 static int timesBad_=0;
     35static int timesBad_ = 0;
    3636//#############################################################################
    3737// Solve methods
     
    4040{
    4141  modelPtr_->scaling(0);
    42   setBasis(basis_,modelPtr_);
     42  setBasis(basis_, modelPtr_);
    4343  // Do long thin by sprint
    4444  ClpSolve options;
    4545  options.setSolveType(ClpSolve::usePrimalorSprint);
    4646  options.setPresolveType(ClpSolve::presolveOff);
    47   options.setSpecialOption(1,3,30);
     47  options.setSpecialOption(1, 3, 30);
    4848  modelPtr_->initialSolve(options);
    4949  basis_ = getBasis(modelPtr_);
     
    5454void CbcSolver3::resolve()
    5555{
    56   int * whichRow = NULL;
    57   int * whichColumn = NULL;
     56  int *whichRow = NULL;
     57  int *whichColumn = NULL;
    5858  // problem may be small enough to do nested search
    59   const double * colLower = modelPtr_->columnLower();
    60   const double * colUpper = modelPtr_->columnUpper();
    61  
     59  const double *colLower = modelPtr_->columnLower();
     60  const double *colUpper = modelPtr_->columnUpper();
     61
    6262  int numberIntegers = model_->numberIntegers();
    63   const int * integerVariable = model_->integerVariable();
    64   int numberRows=modelPtr_->numberRows();
     63  const int *integerVariable = model_->integerVariable();
     64  int numberRows = modelPtr_->numberRows();
    6565  int numberColumns = modelPtr_->numberColumns();
    66  
     66
    6767  int i;
    68   int nFix=0;
    69   int nNewRow=0;
    70   int nNewCol=0;
    71   int smallOriginalNumberRows=0;
    72   if (algorithm_==0) {
    73     for (i=0;i<numberIntegers;i++) {
    74       int iColumn=integerVariable[i];
    75       if (colLower[iColumn]==colUpper[iColumn])
     68  int nFix = 0;
     69  int nNewRow = 0;
     70  int nNewCol = 0;
     71  int smallOriginalNumberRows = 0;
     72  if (algorithm_ == 0) {
     73    for (i = 0; i < numberIntegers; i++) {
     74      int iColumn = integerVariable[i];
     75      if (colLower[iColumn] == colUpper[iColumn])
    7676        nFix++;
    7777    }
    7878  } else {
    7979    whichRow = new int[numberRows];
    80     whichColumn = new int [numberColumns];
    81     for (i=0;i<numberRows;i++)
    82       whichRow[i]=i;
    83     nNewRow=numberRows;
    84     smallOriginalNumberRows=nNewRow;
    85     for (i=0;i<numberIntegers;i++) {
    86       int iColumn=integerVariable[i];
    87       if (colLower[iColumn]==colUpper[iColumn])
     80    whichColumn = new int[numberColumns];
     81    for (i = 0; i < numberRows; i++)
     82      whichRow[i] = i;
     83    nNewRow = numberRows;
     84    smallOriginalNumberRows = nNewRow;
     85    for (i = 0; i < numberIntegers; i++) {
     86      int iColumn = integerVariable[i];
     87      if (colLower[iColumn] == colUpper[iColumn])
    8888        nFix++;
    8989      bool choose;
    90       if (algorithm_==1)
     90      if (algorithm_ == 1)
    9191        choose = true;
    9292      else
    93         choose = (node_[i]>count_-memory_&&node_[i]>0);
    94       if ((choose&&colUpper[i])
    95           ||(modelPtr_->getStatus(i)!=ClpSimplex::atLowerBound&&
    96              modelPtr_->getStatus(i)!=ClpSimplex::isFixed)
    97           ||colLower[i]>0.0)
    98         whichColumn[nNewCol++]=i;
    99     }
    100   }
    101   if (nestedSearch_<1.0&&model_&&model_->phase()==2) {
    102     if (nFix>nestedSearch_*numberIntegers) {
     93        choose = (node_[i] > count_ - memory_ && node_[i] > 0);
     94      if ((choose && colUpper[i])
     95        || (modelPtr_->getStatus(i) != ClpSimplex::atLowerBound && modelPtr_->getStatus(i) != ClpSimplex::isFixed)
     96        || colLower[i] > 0.0)
     97        whichColumn[nNewCol++] = i;
     98    }
     99  }
     100  if (nestedSearch_ < 1.0 && model_ && model_->phase() == 2) {
     101    if (nFix > nestedSearch_ * numberIntegers) {
    103102      // Do nested search
    104103      // back to original number of rows
    105104      nNewRow = smallOriginalNumberRows;
    106105      // and get rid of any basics
    107       int nNewCol=0;
    108       for (i=0;i<numberColumns;i++) {
    109         if (colUpper[i]||colLower[i]>0.0)
    110           whichColumn[nNewCol++]=i;
     106      int nNewCol = 0;
     107      for (i = 0; i < numberColumns; i++) {
     108        if (colUpper[i] || colLower[i] > 0.0)
     109          whichColumn[nNewCol++] = i;
    111110      }
    112111      // start again very simply
    113       ClpSimplex temp(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
     112      ClpSimplex temp(modelPtr_, nNewRow, whichRow, nNewCol, whichColumn);
    114113      int returnCode;
    115114      OsiClpSolverInterface temp2(&temp);
    116115      temp2.setupForRepeatedUse(2);
    117116      int numberColumns2 = temp.numberColumns();
    118       const double * colUpper2 = temp2.getColUpper();
    119       const double * colLower2 = temp2.getColLower();
    120       const double * solution2 = temp.getColSolution();
    121       double * cleanSolution2 = new double [numberColumns2];
    122       for (i=0;i<numberColumns2;i++) {
     117      const double *colUpper2 = temp2.getColUpper();
     118      const double *colLower2 = temp2.getColLower();
     119      const double *solution2 = temp.getColSolution();
     120      double *cleanSolution2 = new double[numberColumns2];
     121      for (i = 0; i < numberColumns2; i++) {
    123122        temp2.setInteger(i);
    124123        double value = solution2[i];
    125         value = CoinMin(CoinMax(value,colLower2[i]),colUpper2[i]);
     124        value = CoinMin(CoinMax(value, colLower2[i]), colUpper2[i]);
    126125        cleanSolution2[i] = value;
    127126      }
    128127      temp2.setColSolution(cleanSolution2);
    129       delete [] cleanSolution2;
     128      delete[] cleanSolution2;
    130129      CbcModel modelSmall(temp2);
    131130      modelSmall.setNumberStrong(0);
     
    136135      generator1.setMaxLook(50);
    137136      generator1.setRowCuts(3);
    138      
     137
    139138      CglGomory generator2;
    140139      // try larger limit
    141140      generator2.setLimit(3000);
    142      
     141
    143142      CglKnapsackCover generator3;
    144      
     143
    145144      CglOddHole generator4;
    146145      generator4.setMinimumViolation(0.005);
     
    148147      // try larger limit
    149148      generator4.setMaximumEntries(200);
    150      
     149
    151150      CglClique generator5;
    152151      generator5.setStarCliqueReport(false);
    153152      generator5.setRowCliqueReport(false);
    154      
     153
    155154      CglMixedIntegerRounding mixedGen;
    156155      CglFlowCover flowGen;
    157156      // Add in generators (just at root)
    158       modelSmall.addCutGenerator(&generator1,-99,"Probing",true,false,false,-1);
    159       modelSmall.addCutGenerator(&generator2,-99,"Gomory",true,false,false,-99);
    160       modelSmall.addCutGenerator(&generator3,-99,"Knapsack",true,false,false,-99);
    161       modelSmall.addCutGenerator(&generator4,-99,"OddHole",true,false,false,-99);
    162       modelSmall.addCutGenerator(&generator5,-99,"Clique",true,false,false,-99);
    163       modelSmall.addCutGenerator(&flowGen,-99,"FlowCover",true,false,false,-99);
    164       modelSmall.addCutGenerator(&mixedGen,-99,"MixedIntegerRounding",true,false,false,-100);
     157      modelSmall.addCutGenerator(&generator1, -99, "Probing", true, false, false, -1);
     158      modelSmall.addCutGenerator(&generator2, -99, "Gomory", true, false, false, -99);
     159      modelSmall.addCutGenerator(&generator3, -99, "Knapsack", true, false, false, -99);
     160      modelSmall.addCutGenerator(&generator4, -99, "OddHole", true, false, false, -99);
     161      modelSmall.addCutGenerator(&generator5, -99, "Clique", true, false, false, -99);
     162      modelSmall.addCutGenerator(&flowGen, -99, "FlowCover", true, false, false, -99);
     163      modelSmall.addCutGenerator(&mixedGen, -99, "MixedIntegerRounding", true, false, false, -100);
    165164#if 1
    166       const CoinPackedMatrix * matrix = temp2.getMatrixByCol();
    167       const int * columnLength = matrix->getVectorLengths();
    168       int * priority = new int [numberColumns2+1];
     165      const CoinPackedMatrix *matrix = temp2.getMatrixByCol();
     166      const int *columnLength = matrix->getVectorLengths();
     167      int *priority = new int[numberColumns2 + 1];
    169168      // do pseudo costs and priorities - take a reasonable guess
    170       CbcObject ** objects = new CbcObject * [numberColumns2+1];
    171       int n=0;
    172       const double * objective = modelSmall.getObjCoefficients();
    173       for (i=0;i<numberColumns2;i++) {
    174         CbcSimpleIntegerPseudoCost * newObject =
    175           new CbcSimpleIntegerPseudoCost(&modelSmall,n,i,objective[i],0.5*objective[i]);
     169      CbcObject **objects = new CbcObject *[numberColumns2 + 1];
     170      int n = 0;
     171      const double *objective = modelSmall.getObjCoefficients();
     172      for (i = 0; i < numberColumns2; i++) {
     173        CbcSimpleIntegerPseudoCost *newObject = new CbcSimpleIntegerPseudoCost(&modelSmall, n, i, objective[i], 0.5 * objective[i]);
    176174        newObject->setMethod(3);
    177         objects[n]= newObject;
    178         priority[n++]=10000-columnLength[i];
    179       }
    180       modelSmall.addObjects(n,objects);
    181       for (i=0;i<n;i++)
     175        objects[n] = newObject;
     176        priority[n++] = 10000 - columnLength[i];
     177      }
     178      modelSmall.addObjects(n, objects);
     179      for (i = 0; i < n; i++)
    182180        delete objects[i];
    183       delete [] objects;
    184       modelSmall.passInPriorities(priority,false);
    185       delete [] priority;
     181      delete[] objects;
     182      modelSmall.passInPriorities(priority, false);
     183      delete[] priority;
    186184#endif
    187185      modelSmall.setCutoff(model_->getCutoff());
    188186      modelSmall.messageHandler()->setLogLevel(1);
    189187      modelSmall.solver()->messageHandler()->setLogLevel(0);
    190       modelSmall.messagesPointer()->setDetailMessage(3,9);
    191       modelSmall.messagesPointer()->setDetailMessage(3,6);
    192       modelSmall.messagesPointer()->setDetailMessage(3,4);
    193       modelSmall.messagesPointer()->setDetailMessage(3,13);
    194       modelSmall.messagesPointer()->setDetailMessage(3,14);
    195       modelSmall.messagesPointer()->setDetailMessage(3,1);
    196       modelSmall.messagesPointer()->setDetailMessage(3,3007);
     188      modelSmall.messagesPointer()->setDetailMessage(3, 9);
     189      modelSmall.messagesPointer()->setDetailMessage(3, 6);
     190      modelSmall.messagesPointer()->setDetailMessage(3, 4);
     191      modelSmall.messagesPointer()->setDetailMessage(3, 13);
     192      modelSmall.messagesPointer()->setDetailMessage(3, 14);
     193      modelSmall.messagesPointer()->setDetailMessage(3, 1);
     194      modelSmall.messagesPointer()->setDetailMessage(3, 3007);
    197195      // Definition of node choice
    198196      CbcCompareObjective compare;
     
    207205      if (modelSmall.bestSolution()) {
    208206        double objValue = 0.0;
    209         const double * solution2 = modelSmall.bestSolution();
    210         double * solution = modelPtr_->primalColumnSolution();
    211         const double * objective = modelPtr_->objective();
    212         for (i=0;i<numberColumns;i++)
    213           solution[i]=colLower[i];
    214         for (i=0;i<nNewCol;i++) {
     207        const double *solution2 = modelSmall.bestSolution();
     208        double *solution = modelPtr_->primalColumnSolution();
     209        const double *objective = modelPtr_->objective();
     210        for (i = 0; i < numberColumns; i++)
     211          solution[i] = colLower[i];
     212        for (i = 0; i < nNewCol; i++) {
    215213          int iColumn = whichColumn[i];
    216           solution[iColumn]=solution2[i];
     214          solution[iColumn] = solution2[i];
    217215        }
    218         for (i=0;i<numberColumns;i++)
    219           objValue += solution[i]*objective[i];
    220         assert (objValue<model_->getCutoff());
    221         if (objValue<model_->getCutoff()) {
     216        for (i = 0; i < numberColumns; i++)
     217          objValue += solution[i] * objective[i];
     218        assert(objValue < model_->getCutoff());
     219        if (objValue < model_->getCutoff()) {
    222220          //printf("good solution \n");
    223           model_->setBestSolution(CBC_TREE_SOL,objValue,solution);
     221          model_->setBestSolution(CBC_TREE_SOL, objValue, solution);
    224222          returnCode = 0;
    225223        } else {
    226           returnCode=2;
     224          returnCode = 2;
    227225        }
    228226      } else {
    229         returnCode=2;
    230       }
    231       if (returnCode!=0&&returnCode!=2) {
    232         printf("pretending entire search done\n");
    233         returnCode=0;
    234       }
    235       if (returnCode==0||returnCode==2) {
    236         modelPtr_->setProblemStatus(1);
    237         delete [] whichRow;
    238         delete [] whichColumn;
    239         return;
    240      }
    241     }
    242   }
    243   if ((count_<100&&algorithm_==2)||!algorithm_) {
    244     delete [] whichRow;
    245     delete [] whichColumn;
     227        returnCode = 2;
     228      }
     229      if (returnCode != 0 && returnCode != 2) {
     230        printf("pretending entire search done\n");
     231        returnCode = 0;
     232      }
     233      if (returnCode == 0 || returnCode == 2) {
     234        modelPtr_->setProblemStatus(1);
     235        delete[] whichRow;
     236        delete[] whichColumn;
     237        return;
     238      }
     239    }
     240  }
     241  if ((count_ < 100 && algorithm_ == 2) || !algorithm_) {
     242    delete[] whichRow;
     243    delete[] whichColumn;
    246244    assert(!modelPtr_->specialOptions());
    247245    int saveOptions = modelPtr_->specialOptions();
    248246    bool takeHint;
    249247    OsiHintStrength strength;
    250     getHintParam(OsiDoInBranchAndCut,takeHint,strength);
    251     if (strength!=OsiHintIgnore&&takeHint) {
     248    getHintParam(OsiDoInBranchAndCut, takeHint, strength);
     249    if (strength != OsiHintIgnore && takeHint) {
    252250      // could do something - think about it
    253251      //printf("thin hint %d %c\n",strength,takeHint ? 'T' :'F');
    254252    }
    255     if((specialOptions_&1)==0) {
    256       modelPtr_->setSpecialOptions(saveOptions|(64|1024));
     253    if ((specialOptions_ & 1) == 0) {
     254      modelPtr_->setSpecialOptions(saveOptions | (64 | 1024));
    257255    } else {
    258       if((specialOptions_&4)==0)
    259         modelPtr_->setSpecialOptions(saveOptions|(64|128|512|1024|4096));
     256      if ((specialOptions_ & 4) == 0)
     257        modelPtr_->setSpecialOptions(saveOptions | (64 | 128 | 512 | 1024 | 4096));
    260258      else
    261         modelPtr_->setSpecialOptions(saveOptions|(64|128|512|1024|2048|4096));
     259        modelPtr_->setSpecialOptions(saveOptions | (64 | 128 | 512 | 1024 | 2048 | 4096));
    262260    }
    263261    //printf("thin options %d size %d\n",modelPtr_->specialOptions(),modelPtr_->numberColumns());
    264     setBasis(basis_,modelPtr_);
     262    setBasis(basis_, modelPtr_);
    265263    //modelPtr_->setLogLevel(1);
    266     printf("model has %d rows\n",modelPtr_->numberRows());
    267     modelPtr_->dual(0,0);
     264    printf("model has %d rows\n", modelPtr_->numberRows());
     265    modelPtr_->dual(0, 0);
    268266    basis_ = getBasis(modelPtr_);
    269267    modelPtr_->setSpecialOptions(saveOptions);
    270     if (modelPtr_->status()==0) {
     268    if (modelPtr_->status() == 0) {
    271269      count_++;
    272       double * solution = modelPtr_->primalColumnSolution();
     270      double *solution = modelPtr_->primalColumnSolution();
    273271      int i;
    274       for (i=0;i<numberColumns;i++) {
    275         if (solution[i]>1.0e-6||modelPtr_->getStatus(i)==ClpSimplex::basic) {
    276           node_[i]=CoinMax(count_,node_[i]);
    277           howMany_[i]++;
    278         }
     272      for (i = 0; i < numberColumns; i++) {
     273        if (solution[i] > 1.0e-6 || modelPtr_->getStatus(i) == ClpSimplex::basic) {
     274          node_[i] = CoinMax(count_, node_[i]);
     275          howMany_[i]++;
     276        }
    279277      }
    280278    } else {
    281       if (!algorithm_==2)
    282         printf("infeasible early on\n");
     279      if (!algorithm_ == 2)
     280        printf("infeasible early on\n");
    283281    }
    284282  } else {
    285283    // use counts
    286284    int i;
    287     const double * lower = modelPtr_->columnLower();
    288     const double * upper = modelPtr_->columnUpper();
    289     setBasis(basis_,modelPtr_);
    290     ClpSimplex * temp = new ClpSimplex(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
     285    const double *lower = modelPtr_->columnLower();
     286    const double *upper = modelPtr_->columnUpper();
     287    setBasis(basis_, modelPtr_);
     288    ClpSimplex *temp = new ClpSimplex(modelPtr_, nNewRow, whichRow, nNewCol, whichColumn);
    291289    //temp->setLogLevel(2);
    292290    //printf("small has %d rows and %d columns\n",nNewRow,nNewCol);
    293     temp->setSpecialOptions(128+512);
     291    temp->setSpecialOptions(128 + 512);
    294292    temp->setDualObjectiveLimit(1.0e50);
    295     printf("model has %d rows\n",nNewRow);
     293    printf("model has %d rows\n", nNewRow);
    296294    temp->dual();
    297295    if (temp->status()) {
    298296      // In some cases we know that it must be infeasible
    299       if (believeInfeasible_||algorithm_==1) {
    300         modelPtr_->setProblemStatus(1);
    301         printf("assuming infeasible!\n");
    302         //modelPtr_->writeMps("infeas.mps");
    303         //temp->writeMps("infeas2.mps");
    304         //abort();
    305         delete temp;
    306         delete [] whichRow;
    307         delete [] whichColumn;
    308         return;
    309       }
    310     }
    311     double * solution = modelPtr_->primalColumnSolution();
     297      if (believeInfeasible_ || algorithm_ == 1) {
     298        modelPtr_->setProblemStatus(1);
     299        printf("assuming infeasible!\n");
     300        //modelPtr_->writeMps("infeas.mps");
     301        //temp->writeMps("infeas2.mps");
     302        //abort();
     303        delete temp;
     304        delete[] whichRow;
     305        delete[] whichColumn;
     306        return;
     307      }
     308    }
     309    double *solution = modelPtr_->primalColumnSolution();
    312310    if (!temp->status()) {
    313       const double * solution2 = temp->primalColumnSolution();
    314       memset(solution,0,numberColumns*sizeof(double));
    315       for (i=0;i<nNewCol;i++) {
    316         int iColumn = whichColumn[i];
    317         solution[iColumn]=solution2[i];
    318         modelPtr_->setStatus(iColumn,temp->getStatus(i));
    319       }
    320       double * rowSolution = modelPtr_->primalRowSolution();
    321       const double * rowSolution2 = temp->primalRowSolution();
    322       double * dual = modelPtr_->dualRowSolution();
    323       const double * dual2 = temp->dualRowSolution();
    324       memset(dual,0,numberRows*sizeof(double));
    325       for (i=0;i<nNewRow;i++) {
    326         int iRow=whichRow[i];
    327         modelPtr_->setRowStatus(iRow,temp->getRowStatus(i));
    328         rowSolution[iRow]=rowSolution2[i];
    329         dual[iRow]=dual2[i];
     311      const double *solution2 = temp->primalColumnSolution();
     312      memset(solution, 0, numberColumns * sizeof(double));
     313      for (i = 0; i < nNewCol; i++) {
     314        int iColumn = whichColumn[i];
     315        solution[iColumn] = solution2[i];
     316        modelPtr_->setStatus(iColumn, temp->getStatus(i));
     317      }
     318      double *rowSolution = modelPtr_->primalRowSolution();
     319      const double *rowSolution2 = temp->primalRowSolution();
     320      double *dual = modelPtr_->dualRowSolution();
     321      const double *dual2 = temp->dualRowSolution();
     322      memset(dual, 0, numberRows * sizeof(double));
     323      for (i = 0; i < nNewRow; i++) {
     324        int iRow = whichRow[i];
     325        modelPtr_->setRowStatus(iRow, temp->getRowStatus(i));
     326        rowSolution[iRow] = rowSolution2[i];
     327        dual[iRow] = dual2[i];
    330328      }
    331329      // See if optimal
    332       double * dj = modelPtr_->dualColumnSolution();
     330      double *dj = modelPtr_->dualColumnSolution();
    333331      // get reduced cost for large problem
    334332      // this assumes minimization
    335       memcpy(dj,modelPtr_->objective(),numberColumns*sizeof(double));
    336       modelPtr_->transposeTimes(-1.0,dual,dj);
     333      memcpy(dj, modelPtr_->objective(), numberColumns * sizeof(double));
     334      modelPtr_->transposeTimes(-1.0, dual, dj);
    337335      modelPtr_->setObjectiveValue(temp->objectiveValue());
    338336      modelPtr_->setProblemStatus(0);
    339       int nBad=0;
    340      
    341       for (i=0;i<numberColumns;i++) {
    342         if (modelPtr_->getStatus(i)==ClpSimplex::atLowerBound
    343             &&upper[i]>lower[i]&&dj[i]<-1.0e-5)
    344           nBad++;
     337      int nBad = 0;
     338
     339      for (i = 0; i < numberColumns; i++) {
     340        if (modelPtr_->getStatus(i) == ClpSimplex::atLowerBound
     341          && upper[i] > lower[i] && dj[i] < -1.0e-5)
     342          nBad++;
    345343      }
    346344      //modelPtr_->writeMps("bada.mps");
    347345      //temp->writeMps("badb.mps");
    348346      if (nBad) {
    349         assert (algorithm_==2);
    350         //printf("%d bad\n",nBad);
    351         timesBad_++;
    352         modelPtr_->primal();
     347        assert(algorithm_ == 2);
     348        //printf("%d bad\n",nBad);
     349        timesBad_++;
     350        modelPtr_->primal();
    353351      }
    354352    } else {
    355353      // infeasible - do all
    356       modelPtr_->setSpecialOptions(64+128+512);
    357       setBasis(basis_,modelPtr_);
     354      modelPtr_->setSpecialOptions(64 + 128 + 512);
     355      setBasis(basis_, modelPtr_);
    358356      //modelPtr_->setLogLevel(1);
    359       modelPtr_->dual(0,0);
     357      modelPtr_->dual(0, 0);
    360358      basis_ = getBasis(modelPtr_);
    361359      modelPtr_->setSpecialOptions(0);
    362360      if (modelPtr_->status()) {
    363         printf("really infeasible!\n");
    364         delete temp;
    365         delete [] whichRow;
    366         delete [] whichColumn;
    367         return;
     361        printf("really infeasible!\n");
     362        delete temp;
     363        delete[] whichRow;
     364        delete[] whichColumn;
     365        return;
    368366      } else {
    369         printf("initially infeasible\n");
     367        printf("initially infeasible\n");
    370368      }
    371369    }
    372370    delete temp;
    373     delete [] whichRow;
    374     delete [] whichColumn;
     371    delete[] whichRow;
     372    delete[] whichColumn;
    375373    basis_ = getBasis(modelPtr_);
    376374    modelPtr_->setSpecialOptions(0);
    377375    count_++;
    378     if ((count_%100)==0&&algorithm_==2)
    379       printf("count %d, bad %d\n",count_,timesBad_);
    380     for (i=0;i<numberColumns;i++) {
    381       if (solution[i]>1.0e-6||modelPtr_->getStatus(i)==ClpSimplex::basic) {
    382         node_[i]=CoinMax(count_,node_[i]);
    383         howMany_[i]++;
    384       }
    385     }
    386     if (modelPtr_->objectiveValue()>=modelPtr_->dualObjectiveLimit())
     376    if ((count_ % 100) == 0 && algorithm_ == 2)
     377      printf("count %d, bad %d\n", count_, timesBad_);
     378    for (i = 0; i < numberColumns; i++) {
     379      if (solution[i] > 1.0e-6 || modelPtr_->getStatus(i) == ClpSimplex::basic) {
     380        node_[i] = CoinMax(count_, node_[i]);
     381        howMany_[i]++;
     382      }
     383    }
     384    if (modelPtr_->objectiveValue() >= modelPtr_->dualObjectiveLimit())
    387385      modelPtr_->setProblemStatus(1);
    388386  }
     
    394392
    395393//-------------------------------------------------------------------
    396 // Default Constructor 
    397 //-------------------------------------------------------------------
    398 CbcSolver3::CbcSolver3 ()
     394// Default Constructor
     395//-------------------------------------------------------------------
     396CbcSolver3::CbcSolver3()
    399397  : OsiClpSolverInterface()
    400398{
    401   node_=NULL;
    402   howMany_=NULL;
    403   count_=0;
     399  node_ = NULL;
     400  howMany_ = NULL;
     401  count_ = 0;
    404402  model_ = NULL;
    405   memory_=300;
    406   believeInfeasible_=false;
     403  memory_ = 300;
     404  believeInfeasible_ = false;
    407405  nestedSearch_ = 1.0;
    408   algorithm_=0;
     406  algorithm_ = 0;
    409407}
    410408
     
    412410// Clone
    413411//-------------------------------------------------------------------
    414 OsiSolverInterface * 
     412OsiSolverInterface *
    415413CbcSolver3::clone(bool CopyData) const
    416414{
     
    423421}
    424422
    425 
    426 //-------------------------------------------------------------------
    427 // Copy constructor
    428 //-------------------------------------------------------------------
    429 CbcSolver3::CbcSolver3 (
    430                   const CbcSolver3 & rhs)
     423//-------------------------------------------------------------------
     424// Copy constructor
     425//-------------------------------------------------------------------
     426CbcSolver3::CbcSolver3(
     427  const CbcSolver3 &rhs)
    431428  : OsiClpSolverInterface(rhs)
    432429{
    433430  model_ = rhs.model_;
    434431  int numberColumns = modelPtr_->numberColumns();
    435   node_=CoinCopyOfArray(rhs.node_,numberColumns);
    436   howMany_=CoinCopyOfArray(rhs.howMany_,numberColumns);
    437   count_=rhs.count_;
    438   memory_=rhs.memory_;
     432  node_ = CoinCopyOfArray(rhs.node_, numberColumns);
     433  howMany_ = CoinCopyOfArray(rhs.howMany_, numberColumns);
     434  count_ = rhs.count_;
     435  memory_ = rhs.memory_;
    439436  believeInfeasible_ = rhs.believeInfeasible_;
    440437  nestedSearch_ = rhs.nestedSearch_;
    441   algorithm_=rhs.algorithm_;
    442 }
    443 
    444 //-------------------------------------------------------------------
    445 // Destructor 
    446 //-------------------------------------------------------------------
    447 CbcSolver3::~CbcSolver3 ()
    448 {
    449   delete [] node_;
    450   delete [] howMany_;
    451 }
    452 
    453 //-------------------------------------------------------------------
    454 // Assignment operator 
     438  algorithm_ = rhs.algorithm_;
     439}
     440
     441//-------------------------------------------------------------------
     442// Destructor
     443//-------------------------------------------------------------------
     444CbcSolver3::~CbcSolver3()
     445{
     446  delete[] node_;
     447  delete[] howMany_;
     448}
     449
     450//-------------------------------------------------------------------
     451// Assignment operator
    455452//-------------------------------------------------------------------
    456453CbcSolver3 &
    457 CbcSolver3::operator=(const CbcSolver3& rhs)
    458 {
    459   if (this != &rhs) { 
     454CbcSolver3::operator=(const CbcSolver3 &rhs)
     455{
     456  if (this != &rhs) {
    460457    OsiClpSolverInterface::operator=(rhs);
    461     delete [] node_;
    462     delete [] howMany_;
     458    delete[] node_;
     459    delete[] howMany_;
    463460    model_ = rhs.model_;
    464461    int numberColumns = modelPtr_->numberColumns();
    465     node_=CoinCopyOfArray(rhs.node_,numberColumns);
    466     howMany_=CoinCopyOfArray(rhs.howMany_,numberColumns);
    467     count_=rhs.count_;
    468     memory_=rhs.memory_;
     462    node_ = CoinCopyOfArray(rhs.node_, numberColumns);
     463    howMany_ = CoinCopyOfArray(rhs.howMany_, numberColumns);
     464    count_ = rhs.count_;
     465    memory_ = rhs.memory_;
    469466    believeInfeasible_ = rhs.believeInfeasible_;
    470467    nestedSearch_ = rhs.nestedSearch_;
    471     algorithm_=rhs.algorithm_;
     468    algorithm_ = rhs.algorithm_;
    472469  }
    473470  return *this;
     
    476473// Real initializer
    477474//-------------------------------------------------------------------
    478 void
    479 CbcSolver3::initialize (CbcModel * model, const char * keep)
    480 {
    481   model_=model;
     475void CbcSolver3::initialize(CbcModel *model, const char *keep)
     476{
     477  model_ = model;
    482478  int numberColumns = modelPtr_->numberColumns();
    483479  if (numberColumns) {
    484480    node_ = new int[numberColumns];
    485481    howMany_ = new int[numberColumns];
    486     for (int i=0;i<numberColumns;i++) {
    487       if (keep&&keep[i])
    488         node_[i]=COIN_INT_MAX;
     482    for (int i = 0; i < numberColumns; i++) {
     483      if (keep && keep[i])
     484        node_[i] = COIN_INT_MAX;
    489485      else
    490         node_[i]=0;
    491       howMany_[i]=0;
     486        node_[i] = 0;
     487      howMany_[i] = 0;
    492488    }
    493489  } else {
    494     node_=NULL;
    495     howMany_=NULL;
    496   }
    497 }
     490    node_ = NULL;
     491    howMany_ = NULL;
     492  }
     493}
  • trunk/Cbc/examples/CbcSolver3.hpp

    r1574 r2469  
    33// Corporation and others.  All Rights Reserved.
    44// This code is licensed under the terms of the Eclipse Public License (EPL).
    5 
    65
    76#ifndef CbcSolver3_H
     
    2322  /**@name Solve methods */
    2423  //@{
    25     /// Solve initial LP relaxation
    26     virtual void initialSolve();
     24  /// Solve initial LP relaxation
     25  virtual void initialSolve();
    2726
    28     /// Resolve an LP relaxation after problem modification
    29     virtual void resolve();
     27  /// Resolve an LP relaxation after problem modification
     28  virtual void resolve();
    3029
    3130  //@}
    3231
    33 
    3432  /**@name Constructors and destructors */
    3533  //@{
    36     /// Default Constructor
    37     CbcSolver3 ();
    38    
    39     /// Clone
    40     virtual OsiSolverInterface * clone(bool CopyData=true) const;
    41    
    42     /// Copy constructor
    43     CbcSolver3 (const CbcSolver3 &);
    44    
    45     /// Assignment operator
    46     CbcSolver3 & operator=(const CbcSolver3& rhs);
    47    
    48     /// Destructor
    49     virtual ~CbcSolver3 ();
     34  /// Default Constructor
     35  CbcSolver3();
     36
     37  /// Clone
     38  virtual OsiSolverInterface *clone(bool CopyData = true) const;
     39
     40  /// Copy constructor
     41  CbcSolver3(const CbcSolver3 &);
     42
     43  /// Assignment operator
     44  CbcSolver3 &operator=(const CbcSolver3 &rhs);
     45
     46  /// Destructor
     47  virtual ~CbcSolver3();
    5048
    5149  //@}
    52 
    5350
    5451  /**@name Sets and Getss */
    5552  //@{
    5653  /// Setup arrays - ones in keep will always be in
    57   void initialize(CbcModel * model, const char * keep);
     54  void initialize(CbcModel *model, const char *keep);
    5855  /// get which ones have been used
    59   inline const int * when() const
    60   { return node_;}
     56  inline const int *when() const
     57  {
     58    return node_;
     59  }
    6160  /// Get memory (i.e. how recent use should be)
    6261  inline int getMemory() const
    63   { return memory_;}
     62  {
     63    return memory_;
     64  }
    6465  /// Get current count
    6566  inline int getCount() const
    66   { return count_;}
     67  {
     68    return count_;
     69  }
    6770  /// Set memory (i.e. how recent use should be)
    6871  inline void setMemory(int value)
    69   { memory_=value;}
     72  {
     73    memory_ = value;
     74  }
    7075  /// Say whether to believe infeasible
    7176  inline void setBelieveInfeasible(bool yesNo)
    72   { believeInfeasible_=yesNo;}
     77  {
     78    believeInfeasible_ = yesNo;
     79  }
    7380  /// Say whether to just count usage
    7481  inline void setAlgorithm(int value)
    75   { algorithm_=value;}
     82  {
     83    algorithm_ = value;
     84  }
    7685  /// Do nested search if this fraction fixed
    7786  inline void setNested(double value)
    78   { nestedSearch_=value;}
     87  {
     88    nestedSearch_ = value;
     89  }
    7990  /// Say whether to just count usage
    8091  inline int getAlgorithm() const
    81   { return algorithm_;}
     92  {
     93    return algorithm_;
     94  }
    8295  /// Do nested search if this fraction fixed
    8396  inline double getNested() const
    84   { return nestedSearch_;}
     97  {
     98    return nestedSearch_;
     99  }
    85100  //@}
    86101
     
    88103
    89104private:
    90  
    91105  /**@name Private member data */
    92106  //@{
     
    94108  double nestedSearch_;
    95109  /// Node number when variable last in problem
    96   int * node_;
     110  int *node_;
    97111  /// How many times in problem
    98   int * howMany_;
     112  int *howMany_;
    99113  /// Pointer back to model
    100   CbcModel * model_;
     114  CbcModel *model_;
    101115  /// Counter
    102116  int count_;
  • trunk/Cbc/examples/CbcSolverLongThin.cpp

    r1898 r2469  
    3333#include "CbcFathomDynamicProgramming.hpp"
    3434
    35 static int timesBad_=0;
     35static int timesBad_ = 0;
    3636//#############################################################################
    3737// Solve methods
    3838//#############################################################################
    39 static CglDuplicateRow * tryCut=NULL;
     39static CglDuplicateRow *tryCut = NULL;
    4040void CbcSolverLongThin::initialSolve()
    4141{
    4242  modelPtr_->scaling(0);
    43   setBasis(basis_,modelPtr_);
     43  setBasis(basis_, modelPtr_);
    4444  modelPtr_->dual();
    4545  basis_ = getBasis(modelPtr_);
     
    5555void CbcSolverLongThin::resolve()
    5656{
    57   int * whichRow = NULL;
    58   int * whichColumn = NULL;
     57  int *whichRow = NULL;
     58  int *whichColumn = NULL;
    5959  // problem may be small enough to do nested search
    60   const double * colLower = modelPtr_->columnLower();
    61   const double * colUpper = modelPtr_->columnUpper();
    62  
     60  const double *colLower = modelPtr_->columnLower();
     61  const double *colUpper = modelPtr_->columnUpper();
     62
    6363  int numberIntegers = model_->numberIntegers();
    64   const int * integerVariable = model_->integerVariable();
    65   int numberRows=modelPtr_->numberRows();
     64  const int *integerVariable = model_->integerVariable();
     65  int numberRows = modelPtr_->numberRows();
    6666  int numberColumns = modelPtr_->numberColumns();
    67  
     67
    6868  int i;
    69   int nFix=0;
    70   int nNewRow=0;
    71   int nNewCol=0;
     69  int nFix = 0;
     70  int nNewRow = 0;
     71  int nNewCol = 0;
    7272  int sizeDynamic = COIN_INT_MAX;
    73   int smallOriginalNumberRows=0;
    74   if (algorithm_==0) {
    75     for (i=0;i<numberIntegers;i++) {
    76       int iColumn=integerVariable[i];
    77       if (colLower[iColumn]==colUpper[iColumn])
     73  int smallOriginalNumberRows = 0;
     74  if (algorithm_ == 0) {
     75    for (i = 0; i < numberIntegers; i++) {
     76      int iColumn = integerVariable[i];
     77      if (colLower[iColumn] == colUpper[iColumn])
    7878        nFix++;
    7979    }
    8080  } else {
    8181    whichRow = new int[numberRows];
    82     whichColumn = new int [numberColumns];
     82    whichColumn = new int[numberColumns];
    8383    // more sophisticated
    8484    OsiCuts cs;
    85     tryCut->generateCuts(*this,cs);
     85    tryCut->generateCuts(*this, cs);
    8686    int numberCuts = cs.sizeColCuts();
    8787    if (numberCuts) {
    88       for ( i = 0 ; i < numberCuts ; i++) {
    89         const OsiColCut *thisCut = cs.colCutPtr(i) ;
    90         const CoinPackedVector & ubs = thisCut->ubs() ;
    91         int n = ubs.getNumElements() ;
    92         const int * which = ubs.getIndices() ;
    93         const double * values = ubs.getElements() ;
    94         for (int j = 0;j<n;j++) {
    95           int iColumn = which[j] ;
    96           this->setColUpper(iColumn,values[j]) ;
     88      for (i = 0; i < numberCuts; i++) {
     89        const OsiColCut *thisCut = cs.colCutPtr(i);
     90        const CoinPackedVector &ubs = thisCut->ubs();
     91        int n = ubs.getNumElements();
     92        const int *which = ubs.getIndices();
     93        const double *values = ubs.getElements();
     94        for (int j = 0; j < n; j++) {
     95          int iColumn = which[j];
     96          this->setColUpper(iColumn, values[j]);
    9797        }
    9898      }
    9999    }
    100100#if 1
    101     const int * duplicate = tryCut->duplicate();
     101    const int *duplicate = tryCut->duplicate();
    102102    sizeDynamic = tryCut->sizeDynamic();
    103103    int nOrig = tryCut->numberOriginalRows();
    104     for (i=0;i<nOrig;i++) {
    105       if (duplicate[i]==-1)
    106         whichRow[nNewRow++]=i;
     104    for (i = 0; i < nOrig; i++) {
     105      if (duplicate[i] == -1)
     106        whichRow[nNewRow++] = i;
    107107      else
    108         modelPtr_->setRowStatus(i,ClpSimplex::basic);
    109     }
    110     smallOriginalNumberRows=nNewRow;
    111     for (;i<numberRows;i++) {
    112       whichRow[nNewRow++]=i;
     108        modelPtr_->setRowStatus(i, ClpSimplex::basic);
     109    }
     110    smallOriginalNumberRows = nNewRow;
     111    for (; i < numberRows; i++) {
     112      whichRow[nNewRow++] = i;
    113113    }
    114114#else
    115     for (i=0;i<numberRows;i++)
    116       whichRow[i]=i;
    117     nNewRow=numberRows;
     115    for (i = 0; i < numberRows; i++)
     116      whichRow[i] = i;
     117    nNewRow = numberRows;
    118118#endif
    119     for (i=0;i<numberIntegers;i++) {
    120       int iColumn=integerVariable[i];
    121       if (colLower[iColumn]==colUpper[iColumn])
     119    for (i = 0; i < numberIntegers; i++) {
     120      int iColumn = integerVariable[i];
     121      if (colLower[iColumn] == colUpper[iColumn])
    122122        nFix++;
    123123      bool choose;
    124       if (algorithm_==1)
     124      if (algorithm_ == 1)
    125125        choose = true;
    126126      else
    127         choose = (node_[i]>count_-memory_&&node_[i]>0);
    128       if ((choose&&colUpper[i])
    129           ||(modelPtr_->getStatus(i)!=ClpSimplex::atLowerBound&&
    130              modelPtr_->getStatus(i)!=ClpSimplex::isFixed)
    131           ||colLower[i]>0.0)
    132         whichColumn[nNewCol++]=i;
     127        choose = (node_[i] > count_ - memory_ && node_[i] > 0);
     128      if ((choose && colUpper[i])
     129        || (modelPtr_->getStatus(i) != ClpSimplex::atLowerBound && modelPtr_->getStatus(i) != ClpSimplex::isFixed)
     130        || colLower[i] > 0.0)
     131        whichColumn[nNewCol++] = i;
    133132    }
    134133  }
    135   if (nestedSearch_<1.0&&model_&&model_->phase()==2) {
    136     if (((double) sizeDynamic)*((double) nNewCol)<1000000000&&sizeDynamic<10000000) {
     134  if (nestedSearch_ < 1.0 && model_ && model_->phase() == 2) {
     135    if (((double)sizeDynamic) * ((double)nNewCol) < 1000000000 && sizeDynamic < 10000000) {
    137136      // could do Dynamic Programming
    138137      // back to original number of rows
    139138      nNewRow = smallOriginalNumberRows;
    140139      // and get rid of any basics
    141       int nNewCol=0;
    142       for (i=0;i<numberColumns;i++) {
    143         if (colUpper[i]||colLower[i]>0.0)
    144           whichColumn[nNewCol++]=i;
    145       }
    146       ClpSimplex temp(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
     140      int nNewCol = 0;
     141      for (i = 0; i < numberColumns; i++) {
     142        if (colUpper[i] || colLower[i] > 0.0)
     143          whichColumn[nNewCol++] = i;
     144      }
     145      ClpSimplex temp(modelPtr_, nNewRow, whichRow, nNewCol, whichColumn);
    147146      int returnCode;
    148       double * rowLower2 = temp.rowLower();
    149       double * rowUpper2 = temp.rowUpper();
     147      double *rowLower2 = temp.rowLower();
     148      double *rowUpper2 = temp.rowUpper();
    150149      int numberColumns2 = temp.numberColumns();
    151       double * colLower2 = temp.columnLower();
    152       double * colUpper2 = temp.columnUpper();
    153       const CoinPackedMatrix * matrix = temp.matrix();
    154       const double * element = matrix->getElements();
    155       const int * row = matrix->getIndices();
    156       const CoinBigIndex * columnStart = matrix->getVectorStarts();
    157       const int * columnLength = matrix->getVectorLengths();
    158       double offset=0.0;
    159       const double * objective = temp.objective();
    160       bool feasible=true;
    161       for (i=0;i<numberColumns2;i++) {
     150      double *colLower2 = temp.columnLower();
     151      double *colUpper2 = temp.columnUpper();
     152      const CoinPackedMatrix *matrix = temp.matrix();
     153      const double *element = matrix->getElements();
     154      const int *row = matrix->getIndices();
     155      const CoinBigIndex *columnStart = matrix->getVectorStarts();
     156      const int *columnLength = matrix->getVectorLengths();
     157      double offset = 0.0;
     158      const double *objective = temp.objective();
     159      bool feasible = true;
     160      for (i = 0; i < numberColumns2; i++) {
    162161        double value = colLower2[i];
    163162        if (value) {
    164           offset += value*objective[i];
    165           colLower2[i]=0.0;
     163          offset += value * objective[i];
     164          colLower2[i] = 0.0;
    166165          colUpper2[i] -= value;
    167           for (int j=columnStart[i];
    168                j<columnStart[i]+columnLength[i];j++) {
    169             int iRow=row[j];
    170             rowLower2[iRow] -= value*element[j];
    171             rowUpper2[iRow] -= value*element[j];
    172             if (rowUpper2[iRow]<-1.0e-8) {
    173               feasible=false;
     166          for (int j = columnStart[i];
     167               j < columnStart[i] + columnLength[i]; j++) {
     168            int iRow = row[j];
     169            rowLower2[iRow] -= value * element[j];
     170            rowUpper2[iRow] -= value * element[j];
     171            if (rowUpper2[iRow] < -1.0e-8) {
     172              feasible = false;
    174173              printf("odd - problem is infeasible\n");
    175174            }
     
    179178      temp.setObjectiveOffset(-offset);
    180179      OsiClpSolverInterface temp2(&temp);
    181       double * solutionDP = NULL;
     180      double *solutionDP = NULL;
    182181      if (feasible) {
    183         for (i=0;i<numberColumns2;i++)
     182        for (i = 0; i < numberColumns2; i++)
    184183          temp2.setInteger(i);
    185184        CbcModel modelSmall(temp2);
     
    189188        fathom1.setMaximumSize(100000000);
    190189        temp2.writeMps("small");
    191         returnCode=fathom1.fathom(solutionDP);
    192         if (returnCode!=1) {
     190        returnCode = fathom1.fathom(solutionDP);
     191        if (returnCode != 1) {
    193192          printf("probably not enough memory\n");
    194193          abort();
     
    197196      if (solutionDP) {
    198197        double objValue = 0.0;
    199         double * solution = modelPtr_->primalColumnSolution();
    200         const double * objective = modelPtr_->objective();
    201         for (i=0;i<numberColumns;i++)
    202           solution[i]=colLower[i];
    203         for (i=0;i<nNewCol;i++) {
     198        double *solution = modelPtr_->primalColumnSolution();
     199        const double *objective = modelPtr_->objective();
     200        for (i = 0; i < numberColumns; i++)
     201          solution[i] = colLower[i];
     202        for (i = 0; i < nNewCol; i++) {
    204203          int iColumn = whichColumn[i];
    205           solution[iColumn]+=solutionDP[i];
    206         }
    207         for (i=0;i<numberColumns;i++)
    208           objValue += solution[i]*objective[i];
    209         if (objValue<model_->getCutoff()) {
    210           printf("good solution %g by dynamic programming\n",objValue);
     204          solution[iColumn] += solutionDP[i];
     205        }
     206        for (i = 0; i < numberColumns; i++)
     207          objValue += solution[i] * objective[i];
     208        if (objValue < model_->getCutoff()) {
     209          printf("good solution %g by dynamic programming\n", objValue);
    211210          returnCode = 0;
    212211          // paranoid check
    213           double * rowLower = modelPtr_->rowLower();
    214           double * rowUpper = modelPtr_->rowUpper();
     212          double *rowLower = modelPtr_->rowLower();
     213          double *rowUpper = modelPtr_->rowUpper();
    215214          // Column copy
    216           const CoinPackedMatrix * matrix2 = modelPtr_->matrix();
     215          const CoinPackedMatrix *matrix2 = modelPtr_->matrix();
    217216          element = matrix2->getElements();
    218217          row = matrix2->getIndices();
    219218          columnStart = matrix2->getVectorStarts();
    220219          columnLength = matrix2->getVectorLengths();
    221           double * rowActivity = new double [numberRows];
    222           memset(rowActivity,0,numberRows*sizeof(double));
    223           for (i=0;i<numberColumns;i++) {
     220          double *rowActivity = new double[numberRows];
     221          memset(rowActivity, 0, numberRows * sizeof(double));
     222          for (i = 0; i < numberColumns; i++) {
    224223            int j;
    225224            double value = solution[i];
    226             assert (value>=colLower[i]&&value<=colUpper[i]);
     225            assert(value >= colLower[i] && value <= colUpper[i]);
    227226            if (value) {
    228               printf("%d has value %g\n",i,value);
    229               for (j=columnStart[i];
    230                    j<columnStart[i]+columnLength[i];j++) {
    231                 int iRow=row[j];
    232                 rowActivity[iRow] += value*element[j];
     227              printf("%d has value %g\n", i, value);
     228              for (j = columnStart[i];
     229                   j < columnStart[i] + columnLength[i]; j++) {
     230                int iRow = row[j];
     231                rowActivity[iRow] += value * element[j];
    233232              }
    234233            }
    235234          }
    236235          // check was feasible
    237           bool feasible=true;
    238           for (i=0;i<numberRows;i++) {
    239             if(rowActivity[i]<rowLower[i]) {
    240               if (rowActivity[i]<rowLower[i]-1.0e-8)
     236          bool feasible = true;
     237          for (i = 0; i < numberRows; i++) {
     238            if (rowActivity[i] < rowLower[i]) {
     239              if (rowActivity[i] < rowLower[i] - 1.0e-8)
    241240                feasible = false;
    242             } else if(rowActivity[i]>rowUpper[i]) {
    243               if (rowActivity[i]>rowUpper[i]+1.0e-8)
     241            } else if (rowActivity[i] > rowUpper[i]) {
     242              if (rowActivity[i] > rowUpper[i] + 1.0e-8)
    244243                feasible = false;
    245244            }
     
    249248            abort();
    250249          }
    251           delete [] rowActivity;
    252           model_->setBestSolution(CBC_TREE_SOL,objValue,solution);
     250          delete[] rowActivity;
     251          model_->setBestSolution(CBC_TREE_SOL, objValue, solution);
    253252        } else {
    254           returnCode=2;
     253          returnCode = 2;
    255254        }
    256255      } else {
    257         returnCode=2;
     256        returnCode = 2;
    258257      }
    259258      temp2.releaseClp();
    260259      modelPtr_->setProblemStatus(1);
    261       delete [] whichRow;
    262       delete [] whichColumn;
     260      delete[] whichRow;
     261      delete[] whichColumn;
    263262      return;
    264263    }
    265     if (nFix>nestedSearch_*numberIntegers) {
     264    if (nFix > nestedSearch_ * numberIntegers) {
    266265      // Do nested search
    267266      // back to original number of rows
    268267      nNewRow = smallOriginalNumberRows;
    269268      // and get rid of any basics
    270       int nNewCol=0;
    271       for (i=0;i<numberColumns;i++) {
    272         if (colUpper[i]||colLower[i]>0.0)
    273           whichColumn[nNewCol++]=i;
     269      int nNewCol = 0;
     270      for (i = 0; i < numberColumns; i++) {
     271        if (colUpper[i] || colLower[i] > 0.0)
     272          whichColumn[nNewCol++] = i;
    274273      }
    275274#if 0
     
    339338#else
    340339      // start again very simply
    341       ClpSimplex temp(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
     340      ClpSimplex temp(modelPtr_, nNewRow, whichRow, nNewCol, whichColumn);
    342341      int returnCode;
    343342      OsiClpSolverInterface temp2(&temp);
    344343      temp2.setupForRepeatedUse(2);
    345344      int numberColumns2 = temp.numberColumns();
    346       const double * colUpper2 = temp2.getColUpper();
    347       const double * colLower2 = temp2.getColLower();
    348       const double * solution2 = temp.getColSolution();
    349       double * cleanSolution2 = new double [numberColumns2];
    350       for (i=0;i<numberColumns2;i++) {
     345      const double *colUpper2 = temp2.getColUpper();
     346      const double *colLower2 = temp2.getColLower();
     347      const double *solution2 = temp.getColSolution();
     348      double *cleanSolution2 = new double[numberColumns2];
     349      for (i = 0; i < numberColumns2; i++) {
    351350        temp2.setInteger(i);
    352351        double value = solution2[i];
    353         value = CoinMin(CoinMax(value,colLower2[i]),colUpper2[i]);
     352        value = CoinMin(CoinMax(value, colLower2[i]), colUpper2[i]);
    354353        cleanSolution2[i] = value;
    355354      }
    356355      temp2.setColSolution(cleanSolution2);
    357       delete [] cleanSolution2;
     356      delete[] cleanSolution2;
    358357      CbcModel modelSmall(temp2);
    359358      modelSmall.setNumberStrong(0);
     
    364363      generator1.setMaxLook(50);
    365364      generator1.setRowCuts(3);
    366      
     365
    367366      CglGomory generator2;
    368367      // try larger limit
    369368      generator2.setLimit(300);
    370      
     369
    371370      CglKnapsackCover generator3;
    372      
     371
    373372      CglOddHole generator4;
    374373      generator4.setMinimumViolation(0.005);
     
    376375      // try larger limit
    377376      generator4.setMaximumEntries(200);
    378      
     377
    379378      CglClique generator5;
    380379      generator5.setStarCliqueReport(false);
    381380      generator5.setRowCliqueReport(false);
    382      
     381
    383382      CglMixedIntegerRounding mixedGen;
    384383      CglFlowCover flowGen;
    385      
     384
    386385      // Add in generators
    387       modelSmall.addCutGenerator(&generator1,-1,"Probing",true,false,false,-1);
    388       modelSmall.addCutGenerator(&generator2,-99,"Gomory",true,false,false,-99);
    389       modelSmall.addCutGenerator(&generator3,-99,"Knapsack",true,false,false,-99);
    390       modelSmall.addCutGenerator(&generator4,-99,"OddHole",true,false,false,-99);
    391       modelSmall.addCutGenerator(&generator5,-99,"Clique",true,false,false,-99);
    392       modelSmall.addCutGenerator(&flowGen,-99,"FlowCover",true,false,false,-99);
    393       modelSmall.addCutGenerator(&mixedGen,-99,"MixedIntegerRounding",true,false,false,-100);
     386      modelSmall.addCutGenerator(&generator1, -1, "Probing", true, false, false, -1);
     387      modelSmall.addCutGenerator(&generator2, -99, "Gomory", true, false, false, -99);
     388      modelSmall.addCutGenerator(&generator3, -99, "Knapsack", true, false, false, -99);
     389      modelSmall.addCutGenerator(&generator4, -99, "OddHole", true, false, false, -99);
     390      modelSmall.addCutGenerator(&generator5, -99, "Clique", true, false, false, -99);
     391      modelSmall.addCutGenerator(&flowGen, -99, "FlowCover", true, false, false, -99);
     392      modelSmall.addCutGenerator(&mixedGen, -99, "MixedIntegerRounding", true, false, false, -100);
    394393#if 1
    395       const CoinPackedMatrix * matrix = temp2.getMatrixByCol();
    396       const int * columnLength = matrix->getVectorLengths();
    397       int * priority = new int [numberColumns2+1];
     394      const CoinPackedMatrix *matrix = temp2.getMatrixByCol();
     395      const int *columnLength = matrix->getVectorLengths();
     396      int *priority = new int[numberColumns2 + 1];
    398397      // do pseudo costs and priorities - take a reasonable guess
    399       CbcObject ** objects = new CbcObject * [numberColumns2+1];
    400       int n=0;
    401       const double * objective = modelSmall.getObjCoefficients();
    402       for (i=0;i<numberColumns2;i++) {
    403         CbcSimpleIntegerPseudoCost * newObject =
    404           new CbcSimpleIntegerPseudoCost(&modelSmall,n,i,objective[i],0.5*objective[i]);
     398      CbcObject **objects = new CbcObject *[numberColumns2 + 1];
     399      int n = 0;
     400      const double *objective = modelSmall.getObjCoefficients();
     401      for (i = 0; i < numberColumns2; i++) {
     402        CbcSimpleIntegerPseudoCost *newObject = new CbcSimpleIntegerPseudoCost(&modelSmall, n, i, objective[i], 0.5 * objective[i]);
    405403        newObject->setMethod(3);
    406         objects[n]= newObject;
    407         priority[n++]=10000-columnLength[i];
    408       }
    409       priority[n]=1;
    410       objects[n++]=new CbcFollowOn2(&modelSmall);
    411       modelSmall.addObjects(n,objects);
    412       for (i=0;i<n;i++)
     404        objects[n] = newObject;
     405        priority[n++] = 10000 - columnLength[i];
     406      }
     407      priority[n] = 1;
     408      objects[n++] = new CbcFollowOn2(&modelSmall);
     409      modelSmall.addObjects(n, objects);
     410      for (i = 0; i < n; i++)
    413411        delete objects[i];
    414       delete [] objects;
    415       modelSmall.passInPriorities(priority,false);
    416       delete [] priority;
     412      delete[] objects;
     413      modelSmall.passInPriorities(priority, false);
     414      delete[] priority;
    417415#endif
    418416      modelSmall.setCutoff(model_->getCutoff());
     
    422420      modelSmall.messageHandler()->setLogLevel(1);
    423421      modelSmall.solver()->messageHandler()->setLogLevel(0);
    424       modelSmall.messagesPointer()->setDetailMessage(3,9);
    425       modelSmall.messagesPointer()->setDetailMessage(3,6);
    426       modelSmall.messagesPointer()->setDetailMessage(3,4);
    427       modelSmall.messagesPointer()->setDetailMessage(3,13);
    428       modelSmall.messagesPointer()->setDetailMessage(3,14);
    429       modelSmall.messagesPointer()->setDetailMessage(3,1);
    430       modelSmall.messagesPointer()->setDetailMessage(3,3007);
     422      modelSmall.messagesPointer()->setDetailMessage(3, 9);
     423      modelSmall.messagesPointer()->setDetailMessage(3, 6);
     424      modelSmall.messagesPointer()->setDetailMessage(3, 4);
     425      modelSmall.messagesPointer()->setDetailMessage(3, 13);
     426      modelSmall.messagesPointer()->setDetailMessage(3, 14);
     427      modelSmall.messagesPointer()->setDetailMessage(3, 1);
     428      modelSmall.messagesPointer()->setDetailMessage(3, 3007);
    431429      modelSmall.branchAndBound();
    432430      temp2.releaseClp();
    433431      if (modelSmall.bestSolution()) {
    434432        double objValue = 0.0;
    435         const double * solution2 = modelSmall.bestSolution();
    436         double * solution = modelPtr_->primalColumnSolution();
    437         const double * objective = modelPtr_->objective();
    438         for (i=0;i<numberColumns;i++)
    439           solution[i]=colLower[i];
    440         for (i=0;i<nNewCol;i++) {
     433        const double *solution2 = modelSmall.bestSolution();
     434        double *solution = modelPtr_->primalColumnSolution();
     435        const double *objective = modelPtr_->objective();
     436        for (i = 0; i < numberColumns; i++)
     437          solution[i] = colLower[i];
     438        for (i = 0; i < nNewCol; i++) {
    441439          int iColumn = whichColumn[i];
    442           solution[iColumn]=solution2[i];
    443         }
    444         for (i=0;i<numberColumns;i++)
    445           objValue += solution[i]*objective[i];
    446         assert (objValue<model_->getCutoff());
    447         if (objValue<model_->getCutoff()) {
     440          solution[iColumn] = solution2[i];
     441        }
     442        for (i = 0; i < numberColumns; i++)
     443          objValue += solution[i] * objective[i];
     444        assert(objValue < model_->getCutoff());
     445        if (objValue < model_->getCutoff()) {
    448446          //printf("good solution \n");
    449           model_->setBestSolution(CBC_TREE_SOL,objValue,solution);
     447          model_->setBestSolution(CBC_TREE_SOL, objValue, solution);
    450448          returnCode = 0;
    451449        } else {
    452           returnCode=2;
     450          returnCode = 2;
    453451        }
    454452      } else {
    455         returnCode=2;
     453        returnCode = 2;
    456454      }
    457455#endif
    458       if (returnCode!=0&&returnCode!=2) {
    459         printf("pretending entire search done\n");
    460         returnCode=0;
    461       }
    462       if (returnCode==0||returnCode==2) {
    463         modelPtr_->setProblemStatus(1);
    464         delete [] whichRow;
    465         delete [] whichColumn;
    466         return;
    467      }
     456      if (returnCode != 0 && returnCode != 2) {
     457        printf("pretending entire search done\n");
     458        returnCode = 0;
     459      }
     460      if (returnCode == 0 || returnCode == 2) {
     461        modelPtr_->setProblemStatus(1);
     462        delete[] whichRow;
     463        delete[] whichColumn;
     464        return;
     465      }
    468466    }
    469467  }
    470   if ((count_<100&&algorithm_==2)||!algorithm_) {
    471     delete [] whichRow;
    472     delete [] whichColumn;
     468  if ((count_ < 100 && algorithm_ == 2) || !algorithm_) {
     469    delete[] whichRow;
     470    delete[] whichColumn;
    473471    assert(!modelPtr_->specialOptions());
    474472    int saveOptions = modelPtr_->specialOptions();
    475473    bool takeHint;
    476474    OsiHintStrength strength;
    477     getHintParam(OsiDoInBranchAndCut,takeHint,strength);
    478     if (strength!=OsiHintIgnore&&takeHint) {
     475    getHintParam(OsiDoInBranchAndCut, takeHint, strength);
     476    if (strength != OsiHintIgnore && takeHint) {
    479477      // could do something - think about it
    480478      //printf("thin hint %d %c\n",strength,takeHint ? 'T' :'F');
    481479    }
    482     if((specialOptions_&1)==0) {
    483       modelPtr_->setSpecialOptions(saveOptions|(64|1024));
     480    if ((specialOptions_ & 1) == 0) {
     481      modelPtr_->setSpecialOptions(saveOptions | (64 | 1024));
    484482    } else {
    485       if((specialOptions_&4)==0)
    486         modelPtr_->setSpecialOptions(saveOptions|(64|128|512|1024|4096));
     483      if ((specialOptions_ & 4) == 0)
     484        modelPtr_->setSpecialOptions(saveOptions | (64 | 128 | 512 | 1024 | 4096));
    487485      else
    488         modelPtr_->setSpecialOptions(saveOptions|(64|128|512|1024|2048|4096));
     486        modelPtr_->setSpecialOptions(saveOptions | (64 | 128 | 512 | 1024 | 2048 | 4096));
    489487    }
    490488    //printf("thin options %d size %d\n",modelPtr_->specialOptions(),modelPtr_->numberColumns());
    491     setBasis(basis_,modelPtr_);
     489    setBasis(basis_, modelPtr_);
    492490    //modelPtr_->setLogLevel(1);
    493     modelPtr_->dual(0,0);
     491    modelPtr_->dual(0, 0);
    494492    basis_ = getBasis(modelPtr_);
    495493    modelPtr_->setSpecialOptions(saveOptions);
    496     if (modelPtr_->status()==0) {
     494    if (modelPtr_->status() == 0) {
    497495      count_++;
    498       double * solution = modelPtr_->primalColumnSolution();
     496      double *solution = modelPtr_->primalColumnSolution();
    499497      int i;
    500       for (i=0;i<numberColumns;i++) {
    501         if (solution[i]>1.0e-6||modelPtr_->getStatus(i)==ClpSimplex::basic) {
    502           node_[i]=CoinMax(count_,node_[i]);
    503           howMany_[i]++;
    504         }
     498      for (i = 0; i < numberColumns; i++) {
     499        if (solution[i] > 1.0e-6 || modelPtr_->getStatus(i) == ClpSimplex::basic) {
     500          node_[i] = CoinMax(count_, node_[i]);
     501          howMany_[i]++;
     502        }
    505503      }
    506504    } else {
    507       if (!algorithm_==2)
    508         printf("infeasible early on\n");
     505      if (!algorithm_ == 2)
     506        printf("infeasible early on\n");
    509507    }
    510508  } else {
    511509    // use counts
    512510    int i;
    513     const double * lower = modelPtr_->columnLower();
    514     const double * upper = modelPtr_->columnUpper();
    515     setBasis(basis_,modelPtr_);
    516     ClpSimplex * temp = new ClpSimplex(modelPtr_,nNewRow,whichRow,nNewCol,whichColumn);
     511    const double *lower = modelPtr_->columnLower();
     512    const double *upper = modelPtr_->columnUpper();
     513    setBasis(basis_, modelPtr_);
     514    ClpSimplex *temp = new ClpSimplex(modelPtr_, nNewRow, whichRow, nNewCol, whichColumn);
    517515    //temp->setLogLevel(2);
    518516    //printf("small has %d rows and %d columns\n",nNewRow,nNewCol);
    519     temp->setSpecialOptions(128+512);
     517    temp->setSpecialOptions(128 + 512);
    520518    temp->setDualObjectiveLimit(1.0e50);
    521519    temp->dual();
    522520    if (temp->status()) {
    523521      // In some cases we know that it must be infeasible
    524       if (believeInfeasible_||algorithm_==1) {
    525         modelPtr_->setProblemStatus(1);
    526         printf("assuming infeasible!\n");
    527         //modelPtr_->writeMps("infeas.mps");
    528         //temp->writeMps("infeas2.mps");
    529         //abort();
    530         delete temp;
    531         delete [] whichRow;
    532         delete [] whichColumn;
    533         return;
    534       }
    535     }
    536     double * solution = modelPtr_->primalColumnSolution();
     522      if (believeInfeasible_ || algorithm_ == 1) {
     523        modelPtr_->setProblemStatus(1);
     524        printf("assuming infeasible!\n");
     525        //modelPtr_->writeMps("infeas.mps");
     526        //temp->writeMps("infeas2.mps");
     527        //abort();
     528        delete temp;
     529        delete[] whichRow;
     530        delete[] whichColumn;
     531        return;
     532      }
     533    }
     534    double *solution = modelPtr_->primalColumnSolution();
    537535    if (!temp->status()) {
    538       const double * solution2 = temp->primalColumnSolution();
    539       memset(solution,0,numberColumns*sizeof(double));
    540       for (i=0;i<nNewCol;i++) {
    541         int iColumn = whichColumn[i];
    542         solution[iColumn]=solution2[i];
    543         modelPtr_->setStatus(iColumn,temp->getStatus(i));
    544       }
    545       double * rowSolution = modelPtr_->primalRowSolution();
    546       const double * rowSolution2 = temp->primalRowSolution();
    547       double * dual = modelPtr_->dualRowSolution();
    548       const double * dual2 = temp->dualRowSolution();
    549       memset(dual,0,numberRows*sizeof(double));
    550       for (i=0;i<nNewRow;i++) {
    551         int iRow=whichRow[i];
    552         modelPtr_->setRowStatus(iRow,temp->getRowStatus(i));
    553         rowSolution[iRow]=rowSolution2[i];
    554         dual[iRow]=dual2[i];
     536      const double *solution2 = temp->primalColumnSolution();
     537      memset(solution, 0, numberColumns * sizeof(double));
     538      for (i = 0; i < nNewCol; i++) {
     539        int iColumn = whichColumn[i];
     540        solution[iColumn] = solution2[i];
     541        modelPtr_->setStatus(iColumn, temp->getStatus(i));
     542      }
     543      double *rowSolution = modelPtr_->primalRowSolution();
     544      const double *rowSolution2 = temp->primalRowSolution();
     545      double *dual = modelPtr_->dualRowSolution();
     546      const double *dual2 = temp->dualRowSolution();
     547      memset(dual, 0, numberRows * sizeof(double));
     548      for (i = 0; i < nNewRow; i++) {
     549        int iRow = whichRow[i];
     550        modelPtr_->setRowStatus(iRow, temp->getRowStatus(i));
     551        rowSolution[iRow] = rowSolution2[i];
     552        dual[iRow] = dual2[i];
    555553      }
    556554      // See if optimal
    557       double * dj = modelPtr_->dualColumnSolution();
     555      double *dj = modelPtr_->dualColumnSolution();
    558556      // get reduced cost for large problem
    559557      // this assumes minimization
    560       memcpy(dj,modelPtr_->objective(),numberColumns*sizeof(double));
    561       modelPtr_->transposeTimes(-1.0,dual,dj);
     558      memcpy(dj, modelPtr_->objective(), numberColumns * sizeof(double));
     559      modelPtr_->transposeTimes(-1.0, dual, dj);
    562560      modelPtr_->setObjectiveValue(temp->objectiveValue());
    563561      modelPtr_->setProblemStatus(0);
    564       int nBad=0;
    565      
    566       for (i=0;i<numberColumns;i++) {
    567         if (modelPtr_->getStatus(i)==ClpSimplex::atLowerBound
    568             &&upper[i]>lower[i]&&dj[i]<-1.0e-5)
    569           nBad++;
     562