Changeset 844 for stable/2.0
 Timestamp:
 Dec 20, 2007 7:20:12 AM (12 years ago)
 Location:
 stable/2.0/Cbc/src
 Files:

 5 edited
Legend:
 Unmodified
 Added
 Removed

stable/2.0/Cbc/src/CbcHeuristic.cpp
r776 r844 998 998 999 999 // Default Constructor 1000 CbcHeuristicPartial::CbcHeuristicPartial() 1001 :CbcHeuristic() 1002 { 1003 fixPriority_ = 10000; 1004 } 1005 1006 // Constructor from model 1007 CbcHeuristicPartial::CbcHeuristicPartial(CbcModel & model, int fixPriority, int numberNodes) 1008 :CbcHeuristic(model) 1009 { 1010 fixPriority_ = fixPriority; 1011 setNumberNodes(numberNodes); 1012 validate(); 1013 } 1014 1015 // Destructor 1016 CbcHeuristicPartial::~CbcHeuristicPartial () 1017 { 1018 } 1019 1020 // Clone 1021 CbcHeuristic * 1022 CbcHeuristicPartial::clone() const 1023 { 1024 return new CbcHeuristicPartial(*this); 1025 } 1026 // Create C++ lines to get to current state 1027 void 1028 CbcHeuristicPartial::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 1042 CbcHeuristicPartial::CbcHeuristicPartial(const CbcHeuristicPartial & rhs) 1043 : 1044 CbcHeuristic(rhs), 1045 fixPriority_(rhs.fixPriority_) 1046 { 1047 } 1048 1049 // Assignment operator 1050 CbcHeuristicPartial & 1051 CbcHeuristicPartial::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 1061 void 1062 CbcHeuristicPartial::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 1075 int 1076 CbcHeuristicPartial::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(valuefloor(value+0.5))<1.0e8) { 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/5100000000) { 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 01)\n",nFix); 1131 } else { 1132 //printf("\n"); 1133 } 1134 } 1135 fixPriority_=1; // switch off 1136 delete newSolver; 1137 return returnCode; 1138 } 1139 // update model 1140 void 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) 1148 void 1149 CbcHeuristicPartial::validate() 1150 { 1151 if (model_&&when()<10) { 1152 if (model_>numberIntegers()!= 1153 model_>numberObjects()) 1154 setWhen(0); 1155 } 1156 } 1157 1158 // Default Constructor 1000 1159 CbcSerendipity::CbcSerendipity() 1001 1160 :CbcHeuristic() 
stable/2.0/Cbc/src/CbcHeuristic.hpp
r765 r844 190 190 }; 191 191 192 /** Partial solution class 193 If user knows a partial solution this tries to get an integer solution 194 it uses hotstart information 195 */ 196 197 class CbcHeuristicPartial : public CbcHeuristic { 198 public: 199 200 // Default Constructor 201 CbcHeuristicPartial (); 202 203 /** Constructor with model  assumed before cuts 204 Fixes all variables with priority <= given 205 and does given number of nodes 206 */ 207 CbcHeuristicPartial (CbcModel & model, int fixPriority=10000, int numberNodes=200); 208 209 // Copy constructor 210 CbcHeuristicPartial ( const CbcHeuristicPartial &); 211 212 // Destructor 213 ~CbcHeuristicPartial (); 214 215 /// Assignment operator 216 CbcHeuristicPartial & operator=(const CbcHeuristicPartial& rhs); 217 218 /// Clone 219 virtual CbcHeuristic * clone() const; 220 /// Create C++ lines to get to current state 221 virtual void generateCpp( FILE * fp) ; 222 223 /// Resets stuff if model changes 224 virtual void resetModel(CbcModel * model); 225 226 /// update model (This is needed if cliques update matrix etc) 227 virtual void setModel(CbcModel * model); 228 229 using CbcHeuristic::solution ; 230 /** returns 0 if no solution, 1 if valid solution 231 with better objective value than one passed in 232 Sets solution values if good, sets objective value (only if good) 233 This is called after cuts have been added  so can not add cuts 234 */ 235 virtual int solution(double & objectiveValue, 236 double * newSolution); 237 /// Validate model i.e. sets when_ to 0 if necessary (may be NULL) 238 virtual void validate(); 239 240 241 /// Set priority level 242 void setFixPriority(int value) 243 { fixPriority_ = value;} 244 245 protected: 246 // Data 247 248 // All variables with abs priority <= this will be fixed 249 int fixPriority_; 250 }; 251 192 252 /** heuristic  just picks up any good solution 193 253 found by solver  see OsiBabSolver 
stable/2.0/Cbc/src/CbcModel.cpp
r818 r844 9442 9442 int numberColumns = solver_>getNumCols(); 9443 9443 hotstartSolution_ = CoinCopyOfArray(solution,numberColumns); 9444 hotstartPriorities_ = CoinCopyOfArray(priorities,numberColumns); 9444 9445 for (int i=0;i<numberColumns;i++) { 9446 if (hotstartSolution_[i]==COIN_DBL_MAX) { 9447 hotstartSolution_[i]=0.0; 9448 hotstartPriorities_[i]+= 10000; 9449 } 9445 9450 if (solver_>isInteger(i)) 9446 9451 hotstartSolution_[i]=floor(hotstartSolution_[i]+0.5); 9447 9452 } 9448 hotstartPriorities_ = CoinCopyOfArray(priorities,numberColumns);9449 9453 } 9450 9454 } 
stable/2.0/Cbc/src/CbcNode.cpp
r789 r844 1177 1177 int numberObjects = model>numberObjects(); 1178 1178 bool checkFeasibility = numberObjects>model>numberIntegers(); 1179 int maximumStrong = CoinMax(CoinMin( model>numberStrong(),numberObjects),1);1179 int maximumStrong = CoinMax(CoinMin(numberStrong,numberObjects),1); 1180 1180 int numberColumns = model>getNumCols(); 1181 1181 double * saveUpper = new double[numberColumns]; … … 2155 2155 else 2156 2156 depth_ = 0; 2157 // Go to other choose if hot start 2158 if (model>hotstartSolution()) 2159 return 3; 2157 2160 delete branch_; 2158 2161 branch_=NULL; 
stable/2.0/Cbc/src/CbcSolver.cpp
r823 r844 3781 3781 int useCosts=0; 3782 3782 // don't use input solution 3783 int useSolution= 0;3783 int useSolution=1; 3784 3784 3785 3785 // total number of commands read … … 4116 4116 else if (parameters_[iParam].type()==PROCESSTUNE) 4117 4117 tunePreProcess = value; 4118 else if (parameters_[iParam].type()==USESOLUTION) 4119 useSolution = value; 4118 4120 else if (parameters_[iParam].type()==VERBOSE) 4119 4121 verbose = value; … … 4374 4376 preProcess = action; 4375 4377 break; 4376 case USESOLUTION:4377 useSolution = action;4378 break;4379 4378 default: 4380 4379 abort(); … … 5975 5974 #endif 5976 5975 const int * originalColumns = preProcess ? process.originalColumns() : NULL; 5977 if (solutionIn&&useSolution ) {5976 if (solutionIn&&useSolution>=0) { 5978 5977 if (preProcess) { 5979 5978 int numberColumns = babModel_>getNumCols(); … … 6864 6863 return returnCode; 6865 6864 } 6865 if (useSolution>0) { 6866 // use hotstart to try and find solution 6867 CbcHeuristicPartial partial(*babModel_,10000,useSolution); 6868 partial.setHeuristicName("Partial solution given"); 6869 babModel_>addHeuristic(&partial); 6870 } 6866 6871 babModel_>branchAndBound(statistics); 6867 6872 #ifdef NEW_STYLE_SOLVER … … 7765 7770 if (got[0]>=0&&!lpSolver>lengthNames()) 7766 7771 good=false; 7772 int numberFields=99; 7773 if (good&&(strstr(fileName.c_str(),".mst")strstr(fileName.c_str(),".MST"))) { 7774 numberFields=0; 7775 for (i=2;i<(int) (sizeof(got)/sizeof(int));i++) { 7776 if (got[i]>=0) 7777 numberFields++; 7778 } 7779 if (!numberFields) { 7780 // Like Cplex format 7781 order[nAcross]=6; 7782 got[6]=nAcross++; 7783 } 7784 } 7767 7785 if (good) { 7768 7786 char ** columnNames = new char * [numberColumns]; … … 7778 7796 if (got[6]>=0) { 7779 7797 solutionIn = (double *) malloc(numberColumns*sizeof(double)); 7780 CoinZeroN(solutionIn,numberColumns); 7798 for (iColumn=0;iColumn<numberColumns;iColumn++) 7799 solutionIn[iColumn]=COIN_DBL_MAX; 7781 7800 } 7782 if (got[7]>=0 ) {7801 if (got[7]>=0!numberFields) { 7783 7802 prioritiesIn = (int *) malloc(numberColumns*sizeof(int)); 7784 7803 for (iColumn=0;iColumn<numberColumns;iColumn++) … … 7800 7819 int nLine=0; 7801 7820 while (fgets(line,1000,fp)) { 7821 if (!strncmp(line,"ENDATA",6)) 7822 break; 7802 7823 nLine++; 7803 7824 iColumn = 1; … … 7810 7831 char * pos = line; 7811 7832 char * put = line; 7833 if (!numberFields) { 7834 // put in , 7835 for (i=4;i<100;i++) { 7836 if (line[i]==' 'line[i]=='\t') { 7837 line[i]=','; 7838 break; 7839 } 7840 } 7841 } 7812 7842 while (*pos>=' '&&*pos!='\n') { 7813 7843 if (*pos!=' '&&*pos!='\t') {
Note: See TracChangeset
for help on using the changeset viewer.