コード例 #1
0
bool SOS1Handler::isFeasible(ConstSolutionPtr sol, RelaxationPtr rel, bool &,
                             double &)
{
  SOSPtr sos;
  bool isfeas = true;
  int nz;
  const double* x = sol->getPrimal();

  for (SOSConstIterator siter=rel->sos1Begin(); siter!=rel->sos1End();
       ++siter) {
    sos = *siter;
    nz = 0;
    for (VariableConstIterator viter = sos->varsBegin(); viter!=sos->varsEnd();
         ++viter) {
      if (x[(*viter)->getIndex()]>zTol_) {
        ++nz;
        if (nz>1) {
          isfeas = false;
          break;
        }
      }
    }
    if (false == isfeas) {
      break;
    }
  }

#if SPEW
  logger_->msgStream(LogDebug) << me_ << "isFeasible = " << isfeas
                               << std::endl;
#endif
  return isfeas;
}
コード例 #2
0
void SOS1Handler::getBranchingCandidates(RelaxationPtr rel, 
                                         const DoubleVector &x,
                                         ModVector &, BrVarCandSet &,
                                         BrCandVector &gencands,
                                         bool &is_inf)
{
  SOSConstIterator siter;
  VariableConstIterator viter;
  VariablePtr var;
  SOSPtr sos;
  double parsum;
  VarVector lvars, rvars;
  int nz;
  double nzsum;
  double nzval;
  SOSBrCandPtr br_can;

  for (siter=rel->sos1Begin(); siter!=rel->sos1End(); ++siter) {
    sos = *siter;
    getNzNumSum_(sos, x, &nz, &nzsum);
    if (nz>1) {
      parsum = 0.0;
      lvars.clear();
      rvars.clear();
      viter = sos->varsBegin();
      for (; viter!=sos->varsEnd(); ++viter) {
        var = *viter;
        if (var->getUb()-var->getLb()>zTol_) {
          nzval = x[var->getIndex()];
          if ((parsum + nzval) < 0.5*nzsum) {
            lvars.push_back(var);
            parsum += nzval;
          } else if (nzval+parsum - nzsum/2 < nzsum/2 - parsum) {
            lvars.push_back(var);
            parsum += nzval;
            ++viter;
            break;
          } else {
            break;
          }
        }
      } 
      for (; viter!=sos->varsEnd(); ++viter) {
        var = *viter;
        if (var->getUb()-var->getLb()>zTol_) {
          rvars.push_back(*viter);
        }
      }
      br_can = (SOSBrCandPtr) new SOSBrCand(sos, lvars, rvars, parsum,
                                            nzsum-parsum);
      br_can->setDir(DownBranch);
      br_can->setScore(20.0*(lvars.size()-1)*(rvars.size()-1));
      
      gencands.push_back(br_can);

#if SPEW
      logger_->msgStream(LogDebug) << me_ << sos->getName() << " is a "
        << " branching candidate." << std::endl
        << me_ << "left branch has variables ";
      for (viter = lvars.begin(); viter!=lvars.end(); ++viter) {
        logger_->msgStream(LogDebug) << (*viter)->getName() << " ";
      }
      logger_->msgStream(LogDebug) << std::endl
                                   << me_ << "left sum = " << parsum
                                   << std::endl
                                   << me_ << "right branch has variables ";
      for (viter = rvars.begin(); viter!=rvars.end(); ++viter) {
        logger_->msgStream(LogDebug) << (*viter)->getName() << " ";
      }
      logger_->msgStream(LogDebug) << std::endl
                                   << me_ << "right sum = " << nzsum - parsum
                                   << std::endl;
#endif

    } else {
#if SPEW
      logger_->msgStream(LogDebug) << me_ << sos->getName() << " is not a "
                                   << " branching candidate." << std::endl;
#endif
    }
  }
  is_inf = false;
}