Ignore:
Timestamp:
Sep 15, 2006 4:55:05 PM (13 years ago)
Author:
forrest
Message:

many changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/devel/Cbc/examples/CbcBranchLink.cpp

    r134 r424  
    2222    numberMembers_(0),
    2323    numberLinks_(0),
    24     first_(-1)
     24    which_(NULL)
    2525{
    2626}
     
    3232    numberMembers_(numberMembers),
    3333    numberLinks_(numberLinks),
    34     first_(first)
     34    which_(NULL)
    3535{
    3636  id_=identifier;
    3737  if (numberMembers_) {
    3838    weights_ = new double[numberMembers_];
     39    which_ = new int[numberMembers_*numberLinks_];
    3940    if (weights) {
    4041      memcpy(weights_,weights,numberMembers_*sizeof(double));
     
    5051      last=weights_[i];
    5152    }
     53    for (i=0;i<numberMembers_*numberLinks_;i++) {
     54      which_[i]=first+i;
     55    }
     56  } else {
     57    weights_ = NULL;
     58  }
     59}
     60
     61// Useful constructor (which are indices)
     62CbcLink::CbcLink (CbcModel * model,  int numberMembers,
     63           int numberLinks, const int * which , const double * weights, int identifier)
     64  : CbcObject(model),
     65    numberMembers_(numberMembers),
     66    numberLinks_(numberLinks),
     67    which_(NULL)
     68{
     69  id_=identifier;
     70  if (numberMembers_) {
     71    weights_ = new double[numberMembers_];
     72    which_ = new int[numberMembers_*numberLinks_];
     73    if (weights) {
     74      memcpy(weights_,weights,numberMembers_*sizeof(double));
     75    } else {
     76      for (int i=0;i<numberMembers_;i++)
     77        weights_[i]=i;
     78    }
     79    // weights must be increasing
     80    int i;
     81    double last=-COIN_DBL_MAX;
     82    for (i=0;i<numberMembers_;i++) {
     83      assert (weights_[i]>last+1.0e-12);
     84      last=weights_[i];
     85    }
     86    for (i=0;i<numberMembers_*numberLinks_;i++) {
     87      which_[i]= which[i];
     88    }
    5289  } else {
    5390    weights_ = NULL;
     
    6198  numberMembers_ = rhs.numberMembers_;
    6299  numberLinks_ = rhs.numberLinks_;
    63   first_ = rhs.first_;
    64100  if (numberMembers_) {
    65     weights_ = new double[numberMembers_];
    66     memcpy(weights_,rhs.weights_,numberMembers_*sizeof(double));
     101    weights_ = CoinCopyOfArray(rhs.weights_,numberMembers_);
     102    which_ = CoinCopyOfArray(rhs.which_,numberMembers_*numberLinks_);
    67103  } else {
    68104    weights_ = NULL;
     105    which_ = NULL;
    69106  }
    70107}
     
    84121    CbcObject::operator=(rhs);
    85122    delete [] weights_;
     123    delete [] which_;
    86124    numberMembers_ = rhs.numberMembers_;
    87125    numberLinks_ = rhs.numberLinks_;
    88     first_ = rhs.first_;
    89126    if (numberMembers_) {
    90       weights_ = new double[numberMembers_];
    91       memcpy(weights_,rhs.weights_,numberMembers_*sizeof(double));
     127      weights_ = CoinCopyOfArray(rhs.weights_,numberMembers_);
     128      which_ = CoinCopyOfArray(rhs.which_,numberMembers_*numberLinks_);
    92129    } else {
    93130      weights_ = NULL;
     131      which_ = NULL;
    94132    }
    95133  }
     
    101139{
    102140  delete [] weights_;
     141  delete [] which_;
    103142}
    104143
     
    112151  OsiSolverInterface * solver = model_->solver();
    113152  const double * solution = model_->testSolution();
    114   const double * lower = solver->getColLower();
     153  //const double * lower = solver->getColLower();
    115154  const double * upper = solver->getColUpper();
    116155  double integerTolerance =
     
    121160  // check bounds etc
    122161  double lastWeight=-1.0e100;
    123   int base=first_;
     162  int base=0;
    124163  for (j=0;j<numberMembers_;j++) {
    125164    for (int k=0;k<numberLinks_;k++) {
    126       int iColumn = base+k;
    127       if (lower[iColumn])
    128         throw CoinError("Non zero lower bound in CBCLink","infeasibility","CbcLink");
     165      int iColumn = which_[base+k];
     166      //if (lower[iColumn])
     167      //throw CoinError("Non zero lower bound in CBCLink","infeasibility","CbcLink");
    129168      if (lastWeight>=weights_[j]-1.0e-7)
    130169        throw CoinError("Weights too close together in CBCLink","infeasibility","CbcLink");
     
    177216  double sum =0.0;
    178217
    179   int base=first_;
     218  int base=0;
    180219  for (j=0;j<numberMembers_;j++) {
    181220    for (int k=0;k<numberLinks_;k++) {
    182       int iColumn = base+k;
     221      int iColumn = which_[base+k];
    183222      double value = CoinMax(0.0,solution[iColumn]);
    184223      sum += value;
     
    193232  }
    194233  assert (lastNonZero-firstNonZero==0) ;
    195   base=first_;
     234  base=0;
    196235  for (j=0;j<firstNonZero;j++) {
    197236    for (int k=0;k<numberLinks_;k++) {
    198       int iColumn = base+k;
     237      int iColumn = which_[base+k];
    199238      solver->setColUpper(iColumn,0.0);
    200239    }
     
    205244  for (j=lastNonZero+1;j<numberMembers_;j++) {
    206245    for (int k=0;k<numberLinks_;k++) {
    207       int iColumn = base+k;
     246      int iColumn = which_[base+k];
    208247      solver->setColUpper(iColumn,0.0);
    209248    }
     
    229268  double weight = 0.0;
    230269  double sum =0.0;
    231   int base=first_;
     270  int base=0;
    232271  for (j=0;j<numberMembers_;j++) {
    233272    for (int k=0;k<numberLinks_;k++) {
    234       int iColumn = base+k;
     273      int iColumn = which_[base+k];
    235274      if (upper[iColumn]) {
    236275        double value = CoinMax(0.0,solution[iColumn]);
     
    313352  int numberLinks = set_->numberLinks();
    314353  const double * weights = set_->weights();
     354  const int * which = set_->which();
    315355  OsiSolverInterface * solver = model_->solver();
    316356  //const double * lower = solver->getColLower();
     
    324364    }
    325365    assert (i<numberMembers);
    326     int base=set_->first()+i*numberLinks;;
     366    int base=i*numberLinks;;
    327367    for (;i<numberMembers;i++) {
    328368      for (int k=0;k<numberLinks;k++) {
    329         int iColumn = base+k;
     369        int iColumn = which[base+k];
    330370        solver->setColUpper(iColumn,0.0);
    331371      }
     
    335375  } else {
    336376    int i;
    337     int base=set_->first();
     377    int base=0;
    338378    for ( i=0;i<numberMembers;i++) {
    339379      if (weights[i] >= separator_) {
     
    341381      } else {
    342382        for (int k=0;k<numberLinks;k++) {
    343           int iColumn = base+k;
     383          int iColumn = which[base+k];
    344384          solver->setColUpper(iColumn,0.0);
    345385        }
     
    359399  int numberLinks = set_->numberLinks();
    360400  const double * weights = set_->weights();
     401  const int * which = set_->which();
    361402  OsiSolverInterface * solver = model_->solver();
    362403  const double * upper = solver->getColUpper();
     
    366407  int numberOther=0;
    367408  int i;
    368   int base=set_->first();
     409  int base=0;
    369410  for ( i=0;i<numberMembers;i++) {
    370411    for (int k=0;k<numberLinks;k++) {
    371       int iColumn = base+k;
     412      int iColumn = which[base+k];
    372413      double bound = upper[iColumn];
    373414      if (bound) {
     
    379420  }
    380421  // *** for way - up means fix all those in down section
    381   base=set_->first();
     422  base=0;
    382423  if (way_<0) {
    383424    printf("SOS Down");
    384425    for ( i=0;i<numberMembers;i++) {
     426      if (weights[i] > separator_)
     427        break;
    385428      for (int k=0;k<numberLinks;k++) {
    386         int iColumn = base+k;
     429        int iColumn = which[base+k];
    387430        double bound = upper[iColumn];
    388         if (weights[i] > separator_)
    389           break;
    390         else if (bound)
     431        if (bound)
    391432          numberOther++;
    392433      }
     
    396437    for (;i<numberMembers;i++) {
    397438      for (int k=0;k<numberLinks;k++) {
    398         int iColumn = base+k;
     439        int iColumn = which[base+k];
    399440        double bound = upper[iColumn];
    400441        if (bound)
     
    406447    printf("SOS Up");
    407448    for ( i=0;i<numberMembers;i++) {
     449      if (weights[i] >= separator_)
     450        break;
    408451      for (int k=0;k<numberLinks;k++) {
    409         int iColumn = base+k;
     452        int iColumn = which[base+k];
    410453        double bound = upper[iColumn];
    411         if (weights[i] >= separator_)
    412           break;
    413         else if (bound)
     454        if (bound)
    414455          numberFixed++;
    415456      }
     
    419460    for (;i<numberMembers;i++) {
    420461      for (int k=0;k<numberLinks;k++) {
    421         int iColumn = base+k;
     462        int iColumn = which[base+k];
    422463        double bound = upper[iColumn];
    423464        if (bound)
     
    428469  }
    429470  assert ((numberFixed%numberLinks)==0);
    430   assert ((numberFixed%numberOther)==0);
     471  assert ((numberOther%numberLinks)==0);
    431472  printf(" - at %g, free range %d (%g) => %d (%g), %d would be fixed, %d other way\n",
    432473         separator_,first,weights[first],last,weights[last],numberFixed/numberLinks,
Note: See TracChangeset for help on using the changeset viewer.