Ignore:
Timestamp:
Dec 20, 2007 7:20:12 AM (12 years ago)
Author:
forrest
Message:

make useSolution work better

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stable/2.0/Cbc/src/CbcHeuristic.cpp

    r776 r844  
    998998
    999999// Default Constructor
     1000CbcHeuristicPartial::CbcHeuristicPartial()
     1001  :CbcHeuristic()
     1002{
     1003  fixPriority_ = 10000;
     1004}
     1005
     1006// Constructor from model
     1007CbcHeuristicPartial::CbcHeuristicPartial(CbcModel & model, int fixPriority, int numberNodes)
     1008  :CbcHeuristic(model)
     1009{
     1010  fixPriority_ = fixPriority;
     1011  setNumberNodes(numberNodes);
     1012  validate();
     1013}
     1014
     1015// Destructor
     1016CbcHeuristicPartial::~CbcHeuristicPartial ()
     1017{
     1018}
     1019
     1020// Clone
     1021CbcHeuristic *
     1022CbcHeuristicPartial::clone() const
     1023{
     1024  return new CbcHeuristicPartial(*this);
     1025}
     1026// Create C++ lines to get to current state
     1027void
     1028CbcHeuristicPartial::generateCpp( FILE * fp)
     1029{
     1030  CbcHeuristicPartial other;
     1031  fprintf(fp,"0#include \"CbcHeuristic.hpp\"\n");
     1032  fprintf(fp,"3  CbcHeuristicPartial partial(*cbcModel);\n");
     1033  CbcHeuristic::generateCpp(fp,"partial");
     1034  if (fixPriority_!=other.fixPriority_)
     1035    fprintf(fp,"3  partial.setFixPriority(%d);\n",fixPriority_);
     1036  else
     1037    fprintf(fp,"4  partial.setFixPriority(%d);\n",fixPriority_);
     1038  fprintf(fp,"3  cbcModel->addHeuristic(&partial);\n");
     1039}
     1040//#define NEW_PARTIAL
     1041// Copy constructor
     1042CbcHeuristicPartial::CbcHeuristicPartial(const CbcHeuristicPartial & rhs)
     1043:
     1044  CbcHeuristic(rhs),
     1045  fixPriority_(rhs.fixPriority_)
     1046{
     1047}
     1048
     1049// Assignment operator
     1050CbcHeuristicPartial &
     1051CbcHeuristicPartial::operator=( const CbcHeuristicPartial& rhs)
     1052{
     1053  if (this!=&rhs) {
     1054    CbcHeuristic::operator=(rhs);
     1055    fixPriority_ = rhs.fixPriority_;
     1056  }
     1057  return *this;
     1058}
     1059
     1060// Resets stuff if model changes
     1061void
     1062CbcHeuristicPartial::resetModel(CbcModel * model)
     1063{
     1064  model_=model;
     1065  // Get a copy of original matrix (and by row for partial);
     1066  assert(model_->solver());
     1067  validate();
     1068}
     1069// See if partial will give solution
     1070// Sets value of solution
     1071// Assumes rhs for original matrix still okay
     1072// At present only works with integers
     1073// Fix values if asked for
     1074// Returns 1 if solution, 0 if not
     1075int
     1076CbcHeuristicPartial::solution(double & solutionValue,
     1077                      double * betterSolution)
     1078{
     1079  // Return if already done
     1080  if (fixPriority_<0)
     1081    return 0; // switched off
     1082  const double * hotstartSolution = model_->hotstartSolution();
     1083  const int * hotstartPriorities = model_->hotstartPriorities();
     1084  if (!hotstartSolution)
     1085    return 0;
     1086  OsiSolverInterface * solver = model_->solver();
     1087 
     1088  int numberIntegers = model_->numberIntegers();
     1089  const int * integerVariable = model_->integerVariable();
     1090 
     1091  OsiSolverInterface * newSolver = model_->continuousSolver()->clone();
     1092  const double * colLower = newSolver->getColLower();
     1093  const double * colUpper = newSolver->getColUpper();
     1094
     1095  double primalTolerance;
     1096  solver->getDblParam(OsiPrimalTolerance,primalTolerance);
     1097   
     1098  int i;
     1099  int numberFixed=0;
     1100  int returnCode=0;
     1101
     1102  for (i=0;i<numberIntegers;i++) {
     1103    int iColumn=integerVariable[i];
     1104    if (abs(hotstartPriorities[iColumn])<=fixPriority_) {
     1105      double value = hotstartSolution[iColumn];
     1106      double lower = colLower[iColumn];
     1107      double upper = colUpper[iColumn];
     1108      value = CoinMax(value,lower);
     1109      value = CoinMin(value,upper);
     1110      if (fabs(value-floor(value+0.5))<1.0e-8) {
     1111        value = floor(value+0.5);
     1112        newSolver->setColLower(iColumn,value);
     1113        newSolver->setColUpper(iColumn,value);
     1114        numberFixed++;
     1115      }
     1116    }
     1117  }
     1118  if (numberFixed>numberIntegers/5-100000000) {
     1119#ifdef COIN_DEVELOP
     1120    printf("%d integers fixed\n",numberFixed);
     1121#endif
     1122    returnCode = smallBranchAndBound(newSolver,numberNodes_,betterSolution,solutionValue,
     1123                                     model_->getCutoff(),"CbcHeuristicPartial");
     1124    if (returnCode<0)
     1125      returnCode=0; // returned on size
     1126    //printf("return code %d",returnCode);
     1127    if ((returnCode&2)!=0) {
     1128      // could add cut
     1129      returnCode &= ~2;
     1130      //printf("could add cut with %d elements (if all 0-1)\n",nFix);
     1131    } else {
     1132      //printf("\n");
     1133    }
     1134  }
     1135  fixPriority_=-1; // switch off
     1136  delete newSolver;
     1137  return returnCode;
     1138}
     1139// update model
     1140void CbcHeuristicPartial::setModel(CbcModel * model)
     1141{
     1142  model_ = model;
     1143  assert(model_->solver());
     1144  // make sure model okay for heuristic
     1145  validate();
     1146}
     1147// Validate model i.e. sets when_ to 0 if necessary (may be NULL)
     1148void
     1149CbcHeuristicPartial::validate()
     1150{
     1151  if (model_&&when()<10) {
     1152    if (model_->numberIntegers()!=
     1153        model_->numberObjects())
     1154      setWhen(0);
     1155  }
     1156}
     1157
     1158// Default Constructor
    10001159CbcSerendipity::CbcSerendipity()
    10011160  :CbcHeuristic()
Note: See TracChangeset for help on using the changeset viewer.