Changeset 2469 for trunk/Cbc/examples/qmip2.cpp
 Timestamp:
 Jan 6, 2019 6:17:46 PM (19 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/examples/qmip2.cpp
r1898 r2469 10 10 // For Branch and bound 11 11 #include "OsiClpSolverInterface.hpp" 12 #include "CbcModel.hpp" 12 #include "CbcModel.hpp" 13 13 #include "CbcCutGenerator.hpp" 14 14 #include "CoinHelperFunctions.hpp" … … 21 21 // For saying about solution validity 22 22 #include "OsiAuxInfo.hpp" 23 24 23 25 24 // Time … … 27 26 // Class to disallow strong branching solutions 28 27 #include "CbcFeasibilityBase.hpp" 29 class CbcFeasibilityNoStrong : public CbcFeasibilityBase {28 class CbcFeasibilityNoStrong : public CbcFeasibilityBase { 30 29 public: 31 // Default Constructor 32 CbcFeasibilityNoStrong 30 // Default Constructor 31 CbcFeasibilityNoStrong() {} 33 32 34 33 virtual ~CbcFeasibilityNoStrong() {} 35 // Copy constructor 36 CbcFeasibilityNoStrong ( const CbcFeasibilityNoStrong &rhs) {} 37 38 // Assignment operator 39 CbcFeasibilityNoStrong & operator=( const CbcFeasibilityNoStrong& rhs) 40 { return * this;} 34 // Copy constructor 35 CbcFeasibilityNoStrong(const CbcFeasibilityNoStrong &rhs) {} 36 37 // Assignment operator 38 CbcFeasibilityNoStrong &operator=(const CbcFeasibilityNoStrong &rhs) 39 { 40 return *this; 41 } 41 42 42 43 /// Clone 43 virtual CbcFeasibilityBase * clone() const 44 { return new CbcFeasibilityNoStrong();} 44 virtual CbcFeasibilityBase *clone() const 45 { 46 return new CbcFeasibilityNoStrong(); 47 } 45 48 46 49 /** … … 53 56 1 pretend integer solution 54 57 */ 55 virtual int feasible(CbcModel * model, int mode) 56 {return mode;} 58 virtual int feasible(CbcModel *model, int mode) 59 { 60 return mode; 61 } 57 62 }; 58 59 63 60 64 /************************************************************************ … … 79 83 ************************************************************************/ 80 84 81 int main 85 int main(int argc, const char *argv[]) 82 86 { 83 87 84 88 // Define a Solver which inherits from OsiClpsolverInterface > OsiSolverInterface 85 89 86 90 OsiClpSolverInterface solver1; 87 91 88 int nX =4;89 90 int nY = (nX * (nX 1)/2);92 int nX = 4; 93 94 int nY = (nX * (nX  1) / 2); 91 95 // All columns 92 double * obj = new double [nX+nY];93 double * clo = new double[nX+nY];94 double * cup = new double[nX+nY];96 double *obj = new double[nX + nY]; 97 double *clo = new double[nX + nY]; 98 double *cup = new double[nX + nY]; 95 99 int i; 96 for (i =0;i<nX;i++) {97 obj[i] = (i +1);98 clo[i] =0.0;99 cup[i] =1.0;100 } 101 for (i =nX;i<nX+nY;i++) {100 for (i = 0; i < nX; i++) { 101 obj[i] = (i + 1); 102 clo[i] = 0.0; 103 cup[i] = 1.0; 104 } 105 for (i = nX; i < nX + nY; i++) { 102 106 obj[i] = 0.0; 103 clo[i] =0.0;104 cup[i] =1.0;107 clo[i] = 0.0; 108 cup[i] = 1.0; 105 109 } 106 110 // Just ordinary rows 107 int nRow = 1 +nX;108 double * 109 double * 110 for (i =0;i<nRow;i++) {111 rlo[i] =COIN_DBL_MAX;112 rup[i] =1.0;111 int nRow = 1 + nX; 112 double *rlo = new double[nRow]; 113 double *rup = new double[nRow]; 114 for (i = 0; i < nRow; i++) { 115 rlo[i] = COIN_DBL_MAX; 116 rup[i] = 1.0; 113 117 } 114 118 // and first row 115 rup[0] =nX/2.0;119 rup[0] = nX / 2.0; 116 120 // Matrix 117 int nEl = nX +nX*nX;118 int * 119 int * 120 double * 121 int nEl = nX + nX * nX; 122 int *row = new int[nEl]; 123 int *col = new int[nEl]; 124 double *el = new double[nEl]; 121 125 // X 122 nEl =0;126 nEl = 0; 123 127 // May need scale to make plausible 124 128 double scaleFactor = 1.0; 125 for (i =0;i<nX;i++) {126 row[nEl] =0;127 col[nEl] =i;128 el[nEl++] =1.0;129 for (i = 0; i < nX; i++) { 130 row[nEl] = 0; 131 col[nEl] = i; 132 el[nEl++] = 1.0; 129 133 // and diagonal 130 row[nEl] =i+1;131 col[nEl] =i;132 double value = CoinDrand48() *scaleFactor;134 row[nEl] = i + 1; 135 col[nEl] = i; 136 double value = CoinDrand48() * scaleFactor; 133 137 // make reasonable (so multiples of 0.000001) 134 138 value *= 1.0e6; 135 int iValue = (int) (value+1.0);139 int iValue = (int)(value + 1.0); 136 140 value = iValue; 137 141 value *= 1.0e6; 138 el[nEl++] =value;142 el[nEl++] = value; 139 143 } 140 144 // Y … … 142 146 // And stored cuts 143 147 CglStored stored; 144 double cutEls[3] ={1.0,1.0,1.0};148 double cutEls[3] = { 1.0, 1.0, 1.0 }; 145 149 int cutIndices[3]; 146 for (i =0;i<nX;i++) {147 cutIndices[0] =i;148 for (int j =i+1;j<nX;j++) {149 cutIndices[1] =j;150 cutIndices[2] =nY;150 for (i = 0; i < nX; i++) { 151 cutIndices[0] = i; 152 for (int j = i + 1; j < nX; j++) { 153 cutIndices[1] = j; 154 cutIndices[2] = nY; 151 155 // add cut 152 stored.addCut(COIN_DBL_MAX, 1.0,3,cutIndices,cutEls);153 row[nEl] =i+1;154 col[nEl] =nY;155 double value = CoinDrand48() *scaleFactor;156 stored.addCut(COIN_DBL_MAX, 1.0, 3, cutIndices, cutEls); 157 row[nEl] = i + 1; 158 col[nEl] = nY; 159 double value = CoinDrand48() * scaleFactor; 156 160 // multiply to make ones with most negative objective violated 157 161 // make reasonable (so multiples of 0.000001) 158 value *= 1.0e6 +1.0e6*j;159 int iValue = (int) (value+1.0);162 value *= 1.0e6 + 1.0e6 * j; 163 int iValue = (int)(value + 1.0); 160 164 value = iValue; 161 165 value *= 1.0e6; 162 el[nEl++] =value;166 el[nEl++] = value; 163 167 // and other 164 if (i !=j) {165 row[nEl] =j+1;166 col[nEl] =nY;167 el[nEl++] =value;168 if (i != j) { 169 row[nEl] = j + 1; 170 col[nEl] = nY; 171 el[nEl++] = value; 168 172 } 169 173 nY++; … … 173 177 CoinPackedMatrix matrix(true, row, col, el, nEl); 174 178 solver1.loadProblem(matrix, clo, cup, obj, rlo, rup); 175 delete 176 delete 177 delete 178 delete 179 delete 180 delete 181 delete 182 delete 179 delete[] obj; 180 delete[] clo; 181 delete[] cup; 182 delete[] rlo; 183 delete[] rup; 184 delete[] row; 185 delete[] col; 186 delete[] el; 183 187 // Integers 184 for (i =0;i<nX;i++)188 for (i = 0; i < nX; i++) 185 189 solver1.setInteger(i); 186 190 // Reduce printout 187 solver1.setHintParam(OsiDoReducePrint, true,OsiHintTry);188 // This clones solver 191 solver1.setHintParam(OsiDoReducePrint, true, OsiHintTry); 192 // This clones solver 189 193 CbcModel model(solver1); 190 194 // Add stored cuts (making sure at all depths) 191 model.addCutGenerator(&stored, 1,"Stored",true,false,false,100,1,1);195 model.addCutGenerator(&stored, 1, "Stored", true, false, false, 100, 1, 1); 192 196 /* You need the next few lines  193 197 a) so that cut generator will always be called again if it generated cuts … … 208 212 // Say don't recompute solution d) 209 213 model.setSpecialOptions(4); 210 214 211 215 double time1 = CoinCpuTime(); 212 216 213 217 // Do complete search 214 218 215 219 model.branchAndBound(); 216 220 217 std::cout <<argv[1]<<" took "<<CoinCpuTime()time1<<" seconds, "218 <<model.getNodeCount()<<" nodes with objective "219 <<model.getObjValue()220 <<(!model.status() ? " Finished" : " Not finished")221 <<std::endl;221 std::cout << argv[1] << " took " << CoinCpuTime()  time1 << " seconds, " 222 << model.getNodeCount() << " nodes with objective " 223 << model.getObjValue() 224 << (!model.status() ? " Finished" : " Not finished") 225 << std::endl; 222 226 223 227 // Print solution if finished  we can't get names from Osi! 224 228 225 if (!model.status() &&model.getMinimizationObjValue()<1.0e50) {229 if (!model.status() && model.getMinimizationObjValue() < 1.0e50) { 226 230 int numberColumns = model.solver()>getNumCols(); 227 231 228 232 //const double * solution = model.bestSolution(); 229 const double * 230 233 const double *solution = model.solver()>getColSolution(); 234 231 235 int iColumn; 232 for (iColumn =0;iColumn<numberColumns;iColumn++) {233 double value =solution[iColumn];234 if (fabs(value) >1.0e7&&model.solver()>isInteger(iColumn))235 printf("Column %d has value %g\n",iColumn,value);236 for (iColumn = 0; iColumn < numberColumns; iColumn++) { 237 double value = solution[iColumn]; 238 if (fabs(value) > 1.0e7 && model.solver()>isInteger(iColumn)) 239 printf("Column %d has value %g\n", iColumn, value); 236 240 } 237 241 } 238 242 return 0; 239 } 243 }
Note: See TracChangeset
for help on using the changeset viewer.