Changeset 2475


Ignore:
Timestamp:
Jan 22, 2019 4:42:21 AM (3 months ago)
Author:
forrest
Message:

clean up very odd SOS

File:
1 edited

Legend:

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

    r2467 r2475  
    483483    for (j = 0; j < firstNonZero; j++) {
    484484      int iColumn = members_[j];
     485      assert (lower[iColumn]<=0.0);
     486      assert (upper[iColumn]>=0.0);
    485487      solver->setColLower(iColumn, 0.0);
    486488      solver->setColUpper(iColumn, 0.0);
     
    488490    for (j = lastNonZero + 1; j < numberMembers_; j++) {
    489491      int iColumn = members_[j];
     492      assert (lower[iColumn]<=0.0);
     493      assert (upper[iColumn]>=0.0);
    490494      solver->setColLower(iColumn, 0.0);
    491495      solver->setColUpper(iColumn, 0.0);
     
    534538  const double *lower = solver->getColLower();
    535539  const double *upper = solver->getColUpper();
    536   int firstNonFixed = -1;
    537   int lastNonFixed = -1;
    538540  int firstNonZero = -1;
    539541  int lastNonZero = -1;
     
    542544  for (j = 0; j < numberMembers_; j++) {
    543545    int iColumn = members_[j];
    544     if (upper[iColumn] || oddValues_) {
    545       double value = CoinMax(lower[iColumn], solution[iColumn]);
    546       sum += value;
    547       if (firstNonFixed < 0)
    548         firstNonFixed = j;
    549       lastNonFixed = j;
    550       if (fabs(value) > integerTolerance) {
    551         weight += weights_[j] * value;
    552         if (firstNonZero < 0)
    553           firstNonZero = j;
    554         lastNonZero = j;
    555       }
    556     }
    557   }
    558   assert(lastNonZero - firstNonZero >= sosType_);
     546    double value = CoinMax(lower[iColumn], solution[iColumn]);
     547    value = CoinMin(upper[iColumn], value);
     548    sum += value;
     549    if (fabs(value) > integerTolerance) {
     550      weight += weights_[j] * value;
     551      if (firstNonZero < 0)
     552        firstNonZero = j;
     553      lastNonZero = j;
     554    }
     555  }
     556  assert (lastNonZero - firstNonZero >= sosType_) ;
    559557  // find where to branch
    560558  if (!oddValues_)
    561559    weight /= sum;
    562560  else
    563     weight = 0.5 * (weights_[firstNonZero] + weights_[lastNonZero]);
     561    weight = 0.5*(weights_[firstNonZero]+weights_[lastNonZero]);
    564562  int iWhere;
    565563  double separator = 0.0;
    566564  for (iWhere = firstNonZero; iWhere < lastNonZero; iWhere++)
    567     if (weight < weights_[iWhere + 1])
     565    if (weight < weights_[iWhere+1])
    568566      break;
    569   // If we are dealing with really oddly scaled problems
     567  // If we are dealing with really oddly scaled problems 
    570568  // was assert (iWhere<lastNonZero);
    571   if (iWhere == lastNonZero)
     569  if (iWhere==lastNonZero)
    572570    iWhere--;
    573571  if (sosType_ == 1) {
    574572    // SOS 1
    575     separator = 0.5 * (weights_[iWhere] + weights_[iWhere + 1]);
     573    separator = 0.5 * (weights_[iWhere] + weights_[iWhere+1]);
    576574  } else {
    577575    // SOS 2
    578     if (iWhere == firstNonFixed)
    579       iWhere++;
    580     ;
    581     if (iWhere == lastNonFixed - 1)
    582       iWhere = lastNonFixed - 2;
    583     separator = weights_[iWhere + 1];
    584   }
     576    if (iWhere == firstNonZero)
     577      iWhere++;;
     578    if (iWhere == lastNonZero - 1)
     579      iWhere = lastNonZero - 2;
     580    separator = weights_[iWhere+1];
     581  }
     582#ifndef NDEBUG
     583  double sum1 = 0.0;
     584  double sum2 = 0.0;
     585  bool firstLot=true;
     586  for (j = 0; j < numberMembers_; j++) {
     587    int iColumn = members_[j];
     588    double value = CoinMax(lower[iColumn], solution[iColumn]);
     589    value = CoinMin(upper[iColumn], value);
     590    if (fabs(value) < integerTolerance)
     591      value=0.0;
     592    if (firstLot) {
     593      if (sosType_ == 1 && weights_[j]>separator) {
     594        firstLot=false;
     595      } else if (sosType_ == 2 && weights_[j]==separator) {
     596        firstLot=false;
     597        value = 0.0; // dont count
     598      }
     599    }
     600    if (firstLot)
     601      sum1 += value;
     602    else
     603      sum2 += value;
     604  }
     605  assert (sum1!=0.0 && sum2!=0.0 );
     606#endif
    585607  // create object
    586608  CbcBranchingObject *branch;
     
    719741#endif
    720742  OsiSolverInterface *solver = model_->solver();
    721   const double *upper = solver->getColUpper();
    722   int firstNonFixed = -1;
    723   int lastNonFixed = -1;
     743  const double * lower = solver->getColLower();
     744  const double * upper = solver->getColUpper();
    724745  int firstNonZero = -1;
    725746  int lastNonZero = -1;
    726747  double weight = 0.0;
    727748  double sum = 0.0;
    728   double *fix = new double[numberMembers_];
    729   int *which = new int[numberMembers_];
     749  double * fix = new double[numberMembers_];
     750  int * which = new int[numberMembers_];
    730751  for (j = 0; j < numberMembers_; j++) {
    731752    int iColumn = members_[j];
     
    733754    fix[j] = 0.0;
    734755    which[j] = iColumn;
    735     if (upper[iColumn] || oddValues_) {
    736       double value = CoinMax(0.0, solution[iColumn]);
    737       sum += value;
    738       if (firstNonFixed < 0)
    739         firstNonFixed = j;
    740       lastNonFixed = j;
    741       if (value > integerTolerance) {
    742         weight += weights_[j] * value;
    743         if (firstNonZero < 0)
    744           firstNonZero = j;
    745         lastNonZero = j;
    746       }
    747     }
    748   }
    749   assert(lastNonZero - firstNonZero >= sosType_);
     756    double value = CoinMax(lower[iColumn], solution[iColumn]);
     757    value = CoinMin(upper[iColumn], value);
     758    sum += value;
     759    if (fabs(value) > integerTolerance) {
     760      weight += weights_[j] * value;
     761      if (firstNonZero < 0)
     762        firstNonZero = j;
     763      lastNonZero = j;
     764    }
     765  }
     766  assert (lastNonZero - firstNonZero >= sosType_) ;
    750767  // find where to branch
    751768  if (!oddValues_)
    752769    weight /= sum;
    753770  else
    754     weight = 0.5 * (weights_[firstNonZero] + weights_[lastNonZero]);
     771    weight = 0.5*(weights_[firstNonZero]+weights_[lastNonZero]);
    755772  // down branch fixes ones above weight to 0
    756773  int iWhere;
     
    758775  int iUpEnd = 0;
    759776  for (iWhere = firstNonZero; iWhere < lastNonZero; iWhere++)
    760     if (weight < weights_[iWhere + 1])
     777    if (weight < weights_[iWhere+1])
    761778      break;
    762779  if (sosType_ == 1) {
     
    766783  } else {
    767784    // SOS 2
    768     if (iWhere == firstNonFixed)
    769       iWhere++;
    770     ;
    771     if (iWhere == lastNonFixed - 1)
    772       iWhere = lastNonFixed - 2;
     785    if (iWhere == firstNonZero)
     786      iWhere++;;
     787    if (iWhere == lastNonZero - 1)
     788      iWhere = lastNonZero - 2;
    773789    iUpEnd = iWhere + 1;
    774790    iDownStart = iUpEnd + 1;
     
    899915      printf("%d (%g,%g) ", which[i], weights[i], solution[which[i]]);
    900916#endif
    901       solver->setColLower(which[i], 0.0);
    902       solver->setColUpper(which[i], 0.0);
     917      solver->setColLower(which[i], CoinMin(0.0,upper[which[i]]));
     918      solver->setColUpper(which[i], CoinMax(0.0,lower[which[i]]));
    903919    }
    904920    way_ = 1; // Swap direction
     
    912928        printf("%d (%g,%g) ", which[i], weights[i], solution[which[i]]);
    913929#endif
    914         solver->setColLower(which[i], 0.0);
    915         solver->setColUpper(which[i], 0.0);
     930        solver->setColLower(which[i], CoinMin(0.0,upper[which[i]]));
     931        solver->setColUpper(which[i], CoinMax(0.0,lower[which[i]]));
    916932      }
    917933    }
Note: See TracChangeset for help on using the changeset viewer.