//------------------------------------------------------------------- // Generate Stored cuts //------------------------------------------------------------------- void CglStored::generateCuts(const OsiSolverInterface & si, OsiCuts & cs, const CglTreeInfo /*info*/) const { // Get basic problem information const double * solution = si.getColSolution(); int numberRowCuts = cuts_.sizeRowCuts(); for (int i=0;i<numberRowCuts;i++) { const OsiRowCut * rowCutPointer = cuts_.rowCutPtr(i); double violation = rowCutPointer->violated(solution); if (violation>=requiredViolation_) cs.insert(*rowCutPointer); } if (probingInfo_) { int number01 = probingInfo_->numberIntegers(); const cliqueEntry * entry = probingInfo_->fixEntries(); const int * toZero = probingInfo_->toZero(); const int * toOne = probingInfo_->toOne(); const int * integerVariable = probingInfo_->integerVariable(); const double * lower = si.getColLower(); const double * upper = si.getColUpper(); OsiRowCut cut; int column[2]; double element[2]; for (int i=0;i<number01;i++) { int iColumn=integerVariable[i]; if (upper[iColumn]==lower[iColumn]) continue; double value1 = solution[iColumn]; for (int j=toZero[i];j<toOne[i];j++) { int jColumn=sequenceInCliqueEntry(entry[j]); if (jColumn<number01) { jColumn=integerVariable[jColumn]; assert (jColumn>=0); double value2 = solution[jColumn]; if (oneFixesInCliqueEntry(entry[j])) { double violation = 1.0-value1-value2; if (violation>requiredViolation_) { //printf("XXX can do %d + %d >=1\n",iColumn,jColumn); cut.setLb(1.0); cut.setUb(COIN_DBL_MAX); column[0]=iColumn; element[0]=1.0; column[1]=jColumn; element[1]= 1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } else { double violation = value2-value1; if (violation>requiredViolation_) { //printf("XXX can do %d >= %d\n",iColumn,jColumn); cut.setLb(0.0); cut.setUb(COIN_DBL_MAX); column[0]=iColumn; element[0]=1.0; column[1]=jColumn; element[1]= -1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } } else { jColumn -= number01; // not 0-1 double value2 = solution[jColumn]; double lowerValue = lower[jColumn]; double upperValue = upper[jColumn]; if (oneFixesInCliqueEntry(entry[j])) { double violation = upperValue-value1*(upperValue-lowerValue)-value2; if (violation>requiredViolation_) { //printf("XXX can do %g*%d + %d >=%g\n",(upperValue-lowerValue),iColumn,jColumn,upperValue); cut.setLb(upperValue); cut.setUb(COIN_DBL_MAX); column[0]=iColumn; element[0]=upperValue-lowerValue; column[1]=jColumn; element[1]= 1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } else { double violation = value2-value1*(upperValue-lowerValue)-lowerValue; if (violation>requiredViolation_) { //printf("XXX can do %g*%d >= %d -%g\n",(upperValue-lowerValue),iColumn,jColumn,lowerValue); cut.setLb(-lowerValue); cut.setUb(COIN_DBL_MAX); column[0]=iColumn; element[0]=upperValue-lowerValue; column[1]=jColumn; element[1]= -1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } } } for (int j=toOne[i];j<toZero[i+1];j++) { int jColumn=sequenceInCliqueEntry(entry[j]); if (jColumn<number01) { jColumn=integerVariable[jColumn]; assert (jColumn>=0); double value2 = solution[jColumn]; if (oneFixesInCliqueEntry(entry[j])) { double violation = value1-value2; if (violation>requiredViolation_) { //printf("XXX can do %d <= %d\n",iColumn,jColumn); cut.setLb(-COIN_DBL_MAX); cut.setUb(0.0); column[0]=iColumn; element[0]=1.0; column[1]=jColumn; element[1]= -1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } else { double violation = value1+value2-1.0; if (violation>requiredViolation_) { //printf("XXX can do %d + %d <=1\n",iColumn,jColumn); cut.setLb(-COIN_DBL_MAX); cut.setUb(1.0); column[0]=iColumn; element[0]=1.0; column[1]=jColumn; element[1]= 1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } } else { jColumn -= number01; // not 0-1 double value2 = solution[jColumn]; double lowerValue = lower[jColumn]; double upperValue = upper[jColumn]; if (oneFixesInCliqueEntry(entry[j])) { double violation = lowerValue +(upperValue-lowerValue)*value1-value2; if (violation>requiredViolation_) { //printf("XXX can do %g*%d <= %d -%g\n",(upperValue-lowerValue),iColumn,jColumn,lowerValue); cut.setLb(-COIN_DBL_MAX); cut.setUb(-lowerValue); column[0]=iColumn; element[0]=upperValue-lowerValue; column[1]=jColumn; element[1]= -1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } else { double violation = (upperValue-lowerValue)*value1+value2-upperValue; if (violation>requiredViolation_) { //printf("XXX can do %g*%d + %d <=%g\n",(upperValue-lowerValue),iColumn,jColumn,upperValue); cut.setLb(-COIN_DBL_MAX); cut.setUb(upperValue); column[0]=iColumn; element[0]=upperValue-lowerValue; column[1]=jColumn; element[1]= 1.0; cut.setEffectiveness(violation); cut.setRow(2,column,element,false); cs.insert(cut); } } } } } } }
//------------------------------------------------------------------- // Given the model data, a row of the model, and a LP solution, // this function tries to generate a violated lifted simple generalized // flow cover. //------------------------------------------------------------------- bool CglFlowCover::generateOneFlowCut( const OsiSolverInterface & si, const int rowLen, int* ind, double* coef, char sense, double rhs, OsiRowCut& flowCut, double& violation ) const { bool generated = false; const double* xlp = si.getColSolution(); const int numCols = si.getNumCols(); double* up = new double [rowLen]; double* x = new double [rowLen]; double* y = new double [rowLen]; CglFlowColType* sign = new CglFlowColType [rowLen]; int i, j; double value, LB, UB; CglFlowVLB VLB; CglFlowVUB VUB; static int count=0; ++count; CGLFLOW_DEBUG=false; doLift=true; // Get integer types const char * columnType = si.getColType (); for (i = 0; i < rowLen; ++i) { if ( xlp[ind[i]] - floor(xlp[ind[i]]) > EPSILON_ && ceil(xlp[ind[i]]) - xlp[ind[i]] > EPSILON_ ) break; } if (i == rowLen) { delete [] sign; delete [] up; delete [] x; delete [] y; return generated; } //------------------------------------------------------------------------- if (sense == 'G') flipRow(rowLen, coef, rhs); // flips everything, // but the sense if(CGLFLOW_DEBUG) { std::cout << "***************************" << std::endl; std::cout << "Generate Flow cover -- initial constraint, converted to L sense..." << std::endl; std::cout << "Rhs = " << rhs << std::endl; std::cout << "coef [var_index]" << " -- " << "xlp[var_index]" << '\t' << "vub_coef[vub_index] vub_lp_value OR var_index_col_ub" << std::endl; for(int iD = 0; iD < rowLen; ++iD) { VUB = getVubs(ind[iD]); std::cout << std::setw(5) << coef[iD] << "[" << std::setw(5) << ind[iD] << "] -- " << std::setw(20) << xlp[ind[iD]] << '\t'; if (VUB.getVar() != UNDEFINED_) { std::cout << std::setw(20) << VUB.getVal() << "[" << std::setw(5) << VUB.getVar() << "]" << std::setw(20) << xlp[VUB.getVar()] << std::endl; } else std::cout << std::setw(20) << si.getColUpper()[ind[iD]] << " " << std::setw(20) << 1.0 << std::endl; } } //------------------------------------------------------------------------- // Generate conservation inequality and capacity equalities from // the given row. for (i = 0; i < rowLen; ++i) { VLB = getVlbs(ind[i]); LB = ( VLB.getVar() != UNDEFINED_ ) ? VLB.getVal() : si.getColLower()[ind[i]]; VUB = getVubs(ind[i]); UB = ( VUB.getVar() != UNDEFINED_ ) ? VUB.getVal() : si.getColUpper()[ind[i]]; if (LB < -EPSILON_) { // Only consider rows whose variables are all delete [] sign; // non-negative (LB>= 0). delete [] up; delete [] x; delete [] y; return generated; } if ( columnType[ind[i]]==1 ) { // Binary variable value = coef[i]; if (value > EPSILON_) sign[i] = CGLFLOW_COL_BINPOS; else { sign[i] = CGLFLOW_COL_BINNEG; value = -value; } up[i] = value; x[i] = xlp[ind[i]]; y[i] = value * x[i]; } else { value = coef[i]; if (value > EPSILON_) sign[i] = CGLFLOW_COL_CONTPOS; else { sign[i] = CGLFLOW_COL_CONTNEG; value = -value; } up[i] = value* UB; x[i] = (VUB.getVar() != UNDEFINED_) ? xlp[VUB.getVar()] : 1.0; y[i] = value * xlp[ind[i]]; } } //------------------------------------------------------------------------- // Find a initial cover (C+, C-) in (N+, N-) double knapRHS = rhs; double tempSum = 0.0; double tempMin = INFTY_; CglFlowColCut * candidate = new CglFlowColCut [rowLen]; CglFlowColCut * label = new CglFlowColCut [rowLen]; double* ratio = new double [rowLen]; int t = -1; for (i = 0; i < rowLen; ++i) { candidate[i] = label[i] = CGLFLOW_COL_OUTCUT; ratio[i] = INFTY_; switch(sign[i]) { case CGLFLOW_COL_CONTPOS: case CGLFLOW_COL_BINPOS: if( y[i] > EPSILON_ ) { ratio[i] = (1.0 - x[i]) / up[i]; if( y[i] > up[i] * x[i] - EPSILON_ ) { // Violated candidate[i] = CGLFLOW_COL_PRIME; tempSum += up[i]; } else { candidate[i] = CGLFLOW_COL_SECONDARY; } } break; case CGLFLOW_COL_CONTNEG: case CGLFLOW_COL_BINNEG: if( up[i] > ( (1.0 - EPSILON_) * INFTY_ ) ) { // UB is infty label[i] = CGLFLOW_COL_INCUT; } else { knapRHS += up[i]; if( y[i] < up[i] ) { candidate[i] = CGLFLOW_COL_PRIME; ratio[i] = x[i] / up[i]; tempSum += up[i]; } } break; } } double diff, tempD, lambda; int xID = -1; if (knapRHS >1.0e10) { if(CGLFLOW_DEBUG) { std::cout << "knapsack RHS too large. RETURN." << std::endl; } delete [] sign; delete [] up; delete [] x; delete [] y; delete [] candidate; delete [] label; delete [] ratio; return generated; } while (tempSum < knapRHS + EPSILON_) { // Not a cover yet diff = INFTY_; for (i = 0; i < rowLen; ++i) { if (candidate[i] == CGLFLOW_COL_SECONDARY) { tempD = up[i] * x[i] - y[i]; if (tempD < diff - EPSILON_) { diff = tempD; xID = i; } } } if( diff > (1.0 - EPSILON_) * INFTY_ ) { // NO cover exits. delete [] sign; delete [] up; delete [] x; delete [] y; delete [] candidate; delete [] label; delete [] ratio; return generated; } else { tempSum += up[xID]; candidate[xID] = CGLFLOW_COL_PRIME; } } // Solve the knapsack problem to get an initial cover tempSum = 0.0; for (i = 0; i < rowLen; ++i) { if (candidate[i] == CGLFLOW_COL_PRIME && ratio[i] < EPSILON_) { //Zero ratio label[i] = CGLFLOW_COL_INCUT; tempSum += up[i]; } } while (tempSum < knapRHS + EPSILON_) { tempMin = INFTY_; xID=-1; for (i = 0; i < rowLen; i++) { // Search the col with minimum ratio if (candidate[i] == CGLFLOW_COL_PRIME && label[i] == 0 && ratio[i] < tempMin) { tempMin = ratio[i]; xID = i; } } if (xID>=0) { label[xID] = CGLFLOW_COL_INCUT; tempSum += up[xID]; } else { if(CGLFLOW_DEBUG) { std::cout << "knapsack RHS too large B. RETURN." << std::endl; } delete [] sign; delete [] up; delete [] x; delete [] y; delete [] candidate; delete [] label; delete [] ratio; return generated; } } // Reduce to a minimal cover for (i = 0; i < rowLen; ++i) { if (label[i] == CGLFLOW_COL_INCUT && ratio[i] > EPSILON_) { if (tempSum - up[i] > knapRHS + EPSILON_) { label[i] = CGLFLOW_COL_OUTCUT; tempSum -= up[i]; } } } for (i = 0; i < rowLen; ++i) { if (label[i] == CGLFLOW_COL_INCUT && ratio[i] < EPSILON_) { if (tempSum - up[i] > knapRHS + EPSILON_) { label[i] = CGLFLOW_COL_OUTCUT; tempSum -= up[i]; } } } // Due to the way to handle N- for(i = 0; i < rowLen; ++i) { if( sign[i] < 0 ) label[i] = label[i]==CGLFLOW_COL_OUTCUT?CGLFLOW_COL_INCUT:CGLFLOW_COL_OUTCUT; } // No cover, no cut. bool emptyCover = true; for (i = 0; i < rowLen; ++i) { if (label[i] == CGLFLOW_COL_INCUT) { emptyCover = false; break; } } if (emptyCover) { if(CGLFLOW_DEBUG) { std::cout << "No cover. RETURN." << std::endl; } delete [] sign; delete [] up; delete [] x; delete [] y; delete [] candidate; delete [] label; delete [] ratio; return generated; } lambda = tempSum - knapRHS; if(CGLFLOW_DEBUG) { double sum_mj_Cplus = 0.0; double sum_mj_Cminus= 0.0; // double checkLambda; // variable not used anywhere (LL) // print out the knapsack variables std::cout << "Knapsack Cover: C+" << std::endl; for (i = 0; i < rowLen; ++i) { if ( label[i] == CGLFLOW_COL_INCUT && sign[i] > 0 ) { std::cout << ind[i] << '\t' << up[i] << std::endl; sum_mj_Cplus += up[i]; } } std::cout << "Knapsack Cover: C-" << std::endl; for (i = 0; i < rowLen; ++i) { if ( label[i] == CGLFLOW_COL_INCUT && sign[i] < 0 ) { std::cout << ind[i] << '\t' << up[i] << std::endl; sum_mj_Cminus += up[i]; } } // rlh: verified "lambda" is lambda in the paper. // lambda = (sum coefficients in C+) - (sum of VUB // coefficients in C-) - rhs-orig-constraint std::cout << "lambda = " << lambda << std::endl; } //------------------------------------------------------------------------- // Generate a violated SGFC int numCMinus = 0; int numPlusPlus = 0; double* rho = new double [rowLen]; double* xCoef = new double [rowLen]; double* yCoef = new double [rowLen]; double cutRHS = rhs; double temp = 0.0; double sum = 0.0; double minPlsM = INFTY_; double minNegM = INFTY_; for(i = 0; i < rowLen; ++i) { rho[i] = 0.0; xCoef[i] = 0.0; yCoef[i] = 0.0; } // Project out variables in C- // d^' = d + sum_{i in C^-} m_i. Now cutRHS = d^' for (i = 0; i < rowLen; ++i) { if ( label[i] == CGLFLOW_COL_INCUT && sign[i] < 0 ) { cutRHS += up[i]; ++numCMinus; } } // (1) Compute the coefficients of the simple generalized flow cover // (2) Compute minPlsM, minNegM and sum // // sum = sum_{i in C+\C++} m_i + sum_{i in L--} m_i = m. Page 15. // minPlsM = min_{i in C++} m_i // minNegM = min_{i in L-} m_i temp = cutRHS; for (i = 0; i < rowLen; ++i) { if (label[i] == CGLFLOW_COL_INCUT && sign[i] > 0) { // C+ yCoef[i] = 1.0; if ( up[i] > lambda + EPSILON_ ) { // C++ ++numPlusPlus; xCoef[i] = lambda - up[i]; cutRHS += xCoef[i]; if( up[i] < minPlsM ) { minPlsM = up[i]; } } else { // C+\C++ xCoef[i] = 0.0; // rlh: is this necesarry? (xCoef initialized to zero) sum += up[i]; } } if (label[i] != CGLFLOW_COL_INCUT && sign[i] < 0) { // N-\C- temp += up[i]; if ( up[i] > lambda) { // L- if(CGLFLOW_DEBUG) { std::cout << "Variable " << ind[i] << " is in L-" << std::endl; } yCoef[i] = 0.0; xCoef[i] = -lambda; label[i] = CGLFLOW_COL_INLMIN; if ( up[i] < minNegM ) { minNegM = up[i]; } } else { // L-- if(CGLFLOW_DEBUG) { std::cout << "Variable " << ind[i] << " is in L-- " << std::endl; } yCoef[i] = -1.0; xCoef[i] = 0.0; // rlh: is this necesarry? (xCoef initialized to zero) label[i] = CGLFLOW_COL_INLMINMIN; sum += up[i]; } } } // Sort the upper bounds (m_i) of variables in C++ and L-. int ix; int index = 0; double temp1 = 0.0; double* mt = new double [rowLen]; double* M = new double [rowLen + 1]; // order to look at variables int * order = new int [rowLen]; int nLook=0; for (int i = 0; i < rowLen; ++i) { if ( (label[i] == CGLFLOW_COL_INCUT && sign[i] > 0) || label[i] == CGLFLOW_COL_INLMIN ) { // C+ || L- // possible M[nLook]=-up[i]; order[nLook++]=i; } } CoinSort_2(M,M+nLook,order); int kLook=0; while (kLook<nLook) { ix = UNDEFINED_; i = order[kLook]; kLook++; if ( (label[i] == CGLFLOW_COL_INCUT && sign[i] > 0) || label[i] == CGLFLOW_COL_INLMIN ) { // C+ || L- if ( up[i] > lambda ) { // C++ || L-(up[i] > lambda) ix = i; } } if( ix == UNDEFINED_ ) break; mt[index++] = up[ix]; // Record m_i in C++ and L-(not all) in descending order. if( label[ix] == CGLFLOW_COL_INLMIN ) label[ix] = CGLFLOW_COL_INLMINDONE; else label[ix] = CGLFLOW_COL_INCUTDONE; } //printf("mins %g %g\n",minNegM,minPlsM); if( index == 0 || numPlusPlus == 0) { // No column in C++ and L-(not all). RETURN. if(CGLFLOW_DEBUG) { std::cout << "index = 0. RETURN." << std::endl; } delete [] sign; delete [] up; delete [] x; delete [] y; delete [] candidate; delete [] label; delete [] ratio; delete [] rho; delete [] xCoef; delete [] yCoef; delete [] mt; delete [] M; delete [] order; return generated; } for ( i = 0; i < rowLen; i++ ) { switch( label[i] ) { case CGLFLOW_COL_INCUTDONE: label[i] = CGLFLOW_COL_INCUT; break; case CGLFLOW_COL_INLMIN: case CGLFLOW_COL_INLMINDONE: case CGLFLOW_COL_INLMINMIN: label[i] = CGLFLOW_COL_OUTCUT; break; case CGLFLOW_COL_INCUT: case CGLFLOW_COL_OUTCUT: case CGLFLOW_COL_PRIME: case CGLFLOW_COL_SECONDARY: break; } } temp1 = temp; /* Get t */ t = 0; for ( i = 0; i < index; ++i ) { if ( mt[i] < minPlsM ) { t = i; break; } } if (i == index) { t = index; } /* Compute M_i */ M[0] = 0.0; for ( i = 1; i <= index; ++i ) { M[i] = M[(i-1)] + mt[(i-1)]; if(CGLFLOW_DEBUG) { std::cout << "t = " << t << std::endl; std::cout << "mt[" << std::setw(5) << (i-1) << "]=" << std::setw(2) << ", M[" << std::setw(5) << i << "]=" << std::setw(20) << M[i] << std::endl; } } // Exit if very big M if (M[index]>1.0e30) { // rlh: should test for huge col UB earler // no sense doing all this work in that case. if(CGLFLOW_DEBUG) { std::cout << "M[index]>1.0e30. RETURN." << std::endl; delete [] sign; delete [] up; delete [] x; delete [] y; delete [] candidate; delete [] label; delete [] ratio; delete [] rho; delete [] xCoef; delete [] yCoef; delete [] mt; delete [] M; delete [] order; return generated; } } /* Get ml */ double ml = CoinMin(sum, lambda); if(CGLFLOW_DEBUG) { // sum = sum_{i in C+\C++} m_i + sum_{i in L--} m_i = m. Page 15. std::cout << "ml = CoinMin(m, lambda) = CoinMin(" << sum << ", " << lambda << ") =" << ml << std::endl; } /* rho_i = max[0, m_i - (minPlsM - lamda) - ml */ if (t < index ) { /* rho exits only for t <= index-1 */ value = (minPlsM - lambda) + ml; for (i = t; i < index; ++i) { rho[i] = CoinMax(0.0, mt[i] - value); if(CGLFLOW_DEBUG) { std::cout << "rho[" << std::setw(5) << i << "]=" << std::setw(20) << rho[i] << std::endl; } } } // Calculate the violation violation = -cutRHS; for ( i = 0; i < rowLen; ++i ) { #ifdef CGLFLOW_DEBUG2 if(CGLFLOW_DEBUG) { std::cout << "i = " << i << " ind = " << ind[i] << " sign = " << sign[i] << " coef = " << coef[i] << " x = " << x[i] << " xCoef = " << xCoef[i] << " y = " << y[i] << " yCoef = " << yCoef[i] << " up = " << up[i] << " label = " << label[i] << std::endl; } #endif violation += y[i] * yCoef[i] + x[i] * xCoef[i]; } if(CGLFLOW_DEBUG) { std::cout << "violation = " << violation << std::endl; } // double violationBeforeLift=violation; // variable not used anywhere (LL) if(doLift && fabs(violation) > TOLERANCE_ ) { // LIFTING double estY, estX; double movement = 0.0; double dPrimePrime = temp + cutRHS; bool lifted = false; for( i = 0; i < rowLen; ++i ) { if ( (label[i] != CGLFLOW_COL_INCUT) && (sign[i] > 0) ) {/* N+\C+*/ lifted = liftPlus(estY, estX, index, up[i], lambda, y[i], x[i], dPrimePrime, M); xCoef[i] = -estX; yCoef[i] = estY; if(CGLFLOW_DEBUG) { if (lifted) { printf("Success: Lifted col %i (up_i=%f,yCoef[i]=%f,xCoef[i]=%f) in N+\\C+\n", ind[i], up[i], yCoef[i], xCoef[i]); } else { printf("Failed to Lift col %i (m_i=%f) in N+\\C+\n", ind[i], up[i]); } } } if (label[i] == CGLFLOW_COL_INCUT && sign[i] < 0) { /* C- */ liftMinus(movement, t, index, up[i], dPrimePrime, lambda, ml, M, rho); if(movement > EPSILON_) { if(CGLFLOW_DEBUG) { printf("Success: Lifted col %i in C-, movement=%f\n", ind[i], movement); } lifted = true; xCoef[i] = -movement; cutRHS -= movement; } else { if(CGLFLOW_DEBUG) { printf("Failed to Lift col %i in C-, g=%f\n", ind[i], movement); } } } } } //------------------------------------------------------------------- // Calculate the violation violation = -cutRHS; for ( i = 0; i < rowLen; ++i ) { #ifdef CGLFLOW_DEBUG2 if(CGLFLOW_DEBUG) { std::cout << "i = " << i << " ind = " << ind[i] << " sign = " << sign[i] << " coef = " << coef[i] << " x = " << x[i] << " xCoef = " << xCoef[i] << " y = " << y[i] << " yCoef = " << yCoef[i] << " up = " << up[i] << " label = " << label[i] << std::endl; } #endif violation += y[i] * yCoef[i] + x[i] * xCoef[i]; } if(CGLFLOW_DEBUG) { std::cout << "violation = " << violation << std::endl; } int cutLen = 0; char cutSense = 'L'; int* cutInd = 0; double* cutCoef = 0; // If violated, transform the inequality back to original system if ( violation > TOLERANCE_ ) { cutLen = 0; cutSense = 'L'; cutInd = new int [numCols]; cutCoef = new double [numCols]; for ( i = 0; i < rowLen; ++i ) { VUB = getVubs(ind[i]); if ( ( sign[i] == CGLFLOW_COL_CONTPOS ) || ( sign[i] == CGLFLOW_COL_CONTNEG ) ) { if ( fabs( yCoef[i] ) > EPSILON_ ) { if ( sign[i] == CGLFLOW_COL_CONTPOS ) cutCoef[cutLen] = coef[i] * yCoef[i]; else cutCoef[cutLen] = -coef[i] * yCoef[i]; cutInd[cutLen++] = ind[i]; } if ( fabs( xCoef[i] ) > EPSILON_ ) { if ( VUB.getVar() != UNDEFINED_ ) { cutCoef[cutLen] = xCoef[i]; cutInd[cutLen++] = VUB.getVar(); } else cutRHS -= xCoef[i]; } } if ( ( sign[i] == CGLFLOW_COL_BINPOS ) || ( sign[i] == CGLFLOW_COL_BINNEG ) ) { if (fabs(yCoef[i]) > EPSILON_ || fabs(xCoef[i]) > EPSILON_) { if (sign[i] == CGLFLOW_COL_BINPOS) cutCoef[cutLen] = coef[i] * yCoef[i] + xCoef[i]; else cutCoef[cutLen] = -coef[i] * yCoef[i] + xCoef[i]; cutInd[cutLen++] = ind[i]; } } } for ( i = 0; i < cutLen; ++i ) { for ( j = 0; j < i; j++ ) { if ( cutInd[j] == cutInd[i] ) { /* Duplicate*/ cutCoef[j] += cutCoef[i]; cutInd[i] = -1; } } } for ( j = 0, i = 0; i < cutLen; ++i ) { if ( ( cutInd[i] == -1 ) || ( fabs( cutCoef[i]) < EPSILON_ ) ){ /* Small coeff*/ } else { cutCoef[j] = cutCoef[i]; cutInd[j] = cutInd[i]; j++; } } cutLen = j; // Skip if no elements ? - bug somewhere assert (cutLen); // Recheck the violation. violation = 0.0; for (i = 0; i < cutLen; ++i) violation += cutCoef[i] * xlp[cutInd[i]]; violation -= cutRHS; if ( violation > TOLERANCE_ ) { flowCut.setRow(cutLen, cutInd, cutCoef); flowCut.setLb(-1.0 * si.getInfinity()); flowCut.setUb(cutRHS); flowCut.setEffectiveness(violation); generated = true; if(CGLFLOW_DEBUG) { std::cout << "generateOneFlowCover(): Found a cut" << std::endl; } } else { if(CGLFLOW_DEBUG) { std::cout << "generateOneFlowCover(): Lost a cut" << std::endl; } } } //------------------------------------------------------------------------- delete [] sign; delete [] up; delete [] x; delete [] y; delete [] candidate; delete [] label; delete [] ratio; delete [] rho; delete [] xCoef; delete [] yCoef; delete [] mt; delete [] M; delete [] order; delete [] cutInd; delete [] cutCoef; return generated; }
//----------------------------------------------------------------------- // Test XPRESS-MP solution methods. void OsiXprSolverInterfaceUnitTest(const std::string & mpsDir, const std::string & netlibDir) { #if 0 // Test to at least see if licence managment is working { int iret = initlz(NULL, 0); if ( iret != 0 ) getipv(N_ERRNO, &iret); assert(iret == 0); } #endif // Test default constructor { assert( OsiXprSolverInterface::getNumInstances()==0 ); OsiXprSolverInterface m; // assert( m.xprSaved_ == false ); // assert( m.xprMatrixId_ = -1 ); assert( m.xprProbname_ == "" ); assert( m.matrixByRow_ == NULL ); assert( m.colupper_ == NULL ); assert( m.collower_ == NULL ); assert( m.rowupper_ == NULL ); assert( m.rowlower_ == NULL ); assert( m.rowsense_ == NULL ); assert( m.rhs_ == NULL ); assert( m.rowrange_ == NULL ); assert( m.colsol_ == NULL ); assert( m.rowprice_ == NULL ); assert( m.ivarind_ == NULL ); assert( m.ivartype_ == NULL ); assert( m.vartype_ == NULL ); assert( OsiXprSolverInterface::getNumInstances() == 1 ); // assert( OsiXprSolverInterface::xprCurrentProblem_ == NULL ); assert( m.getApplicationData() == NULL ); int i = 2346; m.setApplicationData(&i); assert( *((int *)(m.getApplicationData())) == i ); } assert( OsiXprSolverInterface::getNumInstances() == 0 ); { CoinRelFltEq eq; OsiXprSolverInterface m; assert( OsiXprSolverInterface::getNumInstances() == 1 ); std::string fn = mpsDir+"exmip1"; m.readMps(fn.c_str()); // assert( OsiXprSolverInterface::xprCurrentProblem_ == &m ); // This assert fails on windows because fn is mixed case and xprProbname_is uppercase. //assert( m.xprProbname_ == fn ); int ad = 13579; m.setApplicationData(&ad); assert( *((int *)(m.getApplicationData())) == ad ); { OsiXprSolverInterface im; // assert( im.modelPtr_==NULL ); assert( im.getNumCols() == 0 ); // assert( im.modelPtr()!=NULL ); // assert( im.mutableModelPtr()!=NULL ); // assert( im.modelPtr() == im.mutableModelPtr() ); } // Test copy constructor and assignment operator { OsiXprSolverInterface lhs; { assert( *((int *)(m.getApplicationData())) == ad ); OsiXprSolverInterface im(m); assert( *((int *)(im.getApplicationData())) == ad ); OsiXprSolverInterface imC1(im); // assert( imC1.mutableModelPtr()!=im.mutableModelPtr() ); // assert( imC1.modelPtr()!=im.modelPtr() ); assert( imC1.getNumCols() == im.getNumCols() ); assert( imC1.getNumRows() == im.getNumRows() ); assert( *((int *)(imC1.getApplicationData())) == ad ); //im.setModelPtr(m); OsiXprSolverInterface imC2(im); // assert( imC2.mutableModelPtr()!=im.mutableModelPtr() ); // assert( imC2.modelPtr()!=im.modelPtr() ); assert( imC2.getNumCols() == im.getNumCols() ); assert( imC2.getNumRows() == im.getNumRows() ); assert( *((int *)(imC2.getApplicationData())) == ad ); // assert( imC2.mutableModelPtr()!=imC1.mutableModelPtr() ); // assert( imC2.modelPtr()!=imC1.modelPtr() ); lhs=imC2; } // Test that lhs has correct values even though rhs has gone out of scope // assert( lhs.mutableModelPtr() != m.mutableModelPtr() ); // assert( lhs.modelPtr() != m.modelPtr() ); assert( lhs.getNumCols() == m.getNumCols() ); assert( lhs.getNumRows() == m.getNumRows() ); assert( *((int *)(lhs.getApplicationData())) == ad ); } // Test clone { OsiXprSolverInterface xprSi(m); OsiSolverInterface * siPtr = &xprSi; OsiSolverInterface * siClone = siPtr->clone(); OsiXprSolverInterface * xprClone = dynamic_cast<OsiXprSolverInterface*>(siClone); assert( xprClone != NULL ); // assert( xprClone->modelPtr() != xprSi.modelPtr() ); // assert( xprClone->modelPtr() != m.modelPtr() ); assert( xprClone->getNumRows() == xprSi.getNumRows() ); assert( xprClone->getNumCols() == m.getNumCols() ); assert( *((int *)(xprClone->getApplicationData())) == ad ); delete siClone; } // Test infinity { OsiXprSolverInterface si; assert( eq(si.getInfinity(), XPRS_PLUSINFINITY) ); } // Test setting solution { OsiXprSolverInterface m1(m); int i; double * cs = new double[m1.getNumCols()]; for ( i = 0; i < m1.getNumCols(); i++ ) cs[i] = i + .5; m1.setColSolution(cs); for ( i = 0; i < m1.getNumCols(); i++ ) assert(m1.getColSolution()[i] == i + .5); double * rs = new double[m1.getNumRows()]; for ( i = 0; i < m1.getNumRows(); i++ ) rs[i] = i - .5; m1.setRowPrice(rs); for ( i = 0; i < m1.getNumRows(); i++ ) assert(m1.getRowPrice()[i] == i - .5); delete [] cs; delete [] rs; } // Test fraction Indices { OsiXprSolverInterface fim; std::string fn = mpsDir+"exmip1"; fim.readMps(fn.c_str()); //fim.setModelPtr(m); // exmip1.mps has 2 integer variables with index 2 & 3 assert( fim.isContinuous(0) ); assert( fim.isContinuous(1) ); assert( !fim.isContinuous(2) ); assert( !fim.isContinuous(3) ); assert( fim.isContinuous(4) ); assert( !fim.isInteger(0) ); assert( !fim.isInteger(1) ); assert( fim.isInteger(2) ); assert( fim.isInteger(3) ); assert( !fim.isInteger(4) ); // XPRESS-MP incorrectly treats unbounded integer variables as // general integers instead of binary (as in MPSX standard) assert( !fim.isBinary(0) ); assert( !fim.isBinary(1) ); assert( fim.isBinary(2) ); assert( fim.isBinary(3) ); assert( !fim.isBinary(4) ); assert( !fim.isIntegerNonBinary(0) ); assert( !fim.isIntegerNonBinary(1) ); assert( !fim.isIntegerNonBinary(2) ); assert( !fim.isIntegerNonBinary(3) ); assert( !fim.isIntegerNonBinary(4) ); // Test fractionalIndices { // Set a solution vector double * cs = new double[fim.getNumCols()]; for ( int i = 0; i < fim.getNumCols(); cs[i++] = 0.0 ); cs[2] = 2.9; cs[3] = 3.0; fim.setColSolution(cs); OsiVectorInt fi = fim.getFractionalIndices(); assert( fi.size() == 1 ); assert( fi[0]==2 ); // Set integer variables very close to integer values cs[2] = 5 + .00001/2.; cs[3] = 8 - .00001/2.; fim.setColSolution(cs); fi = fim.getFractionalIndices(1e-5); assert( fi.size() == 0 ); // Set integer variables close, but beyond tolerances cs[2] = 5 + .00001*2.; cs[3] = 8 - .00001*2.; fim.setColSolution(cs); fi = fim.getFractionalIndices(1e-5); assert( fi.size() == 2 ); assert( fi[0]==2 ); assert( fi[1]==3 ); delete [] cs; } // Change data so column 2 & 3 are integerNonBinary fim.setColUpper(2, 5); fim.setColUpper(3, 6.0); assert( !fim.isBinary(0) ); assert( !fim.isBinary(1) ); assert( !fim.isBinary(2) ); assert( !fim.isBinary(3) ); assert( !fim.isBinary(4) ); assert( !fim.isIntegerNonBinary(0) ); assert( !fim.isIntegerNonBinary(1) ); assert( fim.isIntegerNonBinary(2) ); assert( fim.isIntegerNonBinary(3) ); assert( !fim.isIntegerNonBinary(4) ); } // Test apply cut method { OsiXprSolverInterface im(m); OsiCuts cuts; // Generate some cuts //cg.generateCuts(im,cuts); { // Get number of rows and columns in model int nr=im.getNumRows(); int nc=im.getNumCols(); assert ( nr == 5 ); assert ( nc == 8 ); // Generate a valid row cut from thin air int c; { int *inx = new int[nc]; for (c=0;c<nc;c++) inx[c]=c; double *el = new double[nc]; for (c=0;c<nc;c++) el[c]=((double)c)*((double)c); OsiRowCut rc; rc.setRow(nc,inx,el); rc.setLb(-100.); rc.setUb(100.); rc.setEffectiveness(22); cuts.insert(rc); delete[]el; delete[]inx; } // Generate valid col cut from thin air { const double * xprColLB = im.getColLower(); const double * xprColUB = im.getColUpper(); int *inx = new int[nc]; for (c=0;c<nc;c++) inx[c]=c; double *lb = new double[nc]; double *ub = new double[nc]; for (c=0;c<nc;c++) lb[c]=xprColLB[c]+0.001; for (c=0;c<nc;c++) ub[c]=xprColUB[c]-0.001; OsiColCut cc; cc.setLbs(nc,inx,lb); cc.setUbs(nc,inx,ub); cuts.insert(cc); delete [] ub; delete [] lb; delete [] inx; } { // Generate a row and column cut which have are ineffective OsiRowCut * rcP= new OsiRowCut; rcP->setEffectiveness(-1.); cuts.insert(rcP); assert(rcP==NULL); OsiColCut * ccP= new OsiColCut; ccP->setEffectiveness(-12.); cuts.insert(ccP); assert(ccP==NULL); } { //Generate inconsistent Row cut OsiRowCut rc; const int ne=1; int inx[ne]={-10}; double el[ne]={2.5}; rc.setRow(ne,inx,el); rc.setLb(3.); rc.setUb(4.); assert(!rc.consistent()); cuts.insert(rc); } { //Generate inconsistent col cut OsiColCut cc; const int ne=1; int inx[ne]={-10}; double el[ne]={2.5}; cc.setUbs(ne,inx,el); assert(!cc.consistent()); cuts.insert(cc); } { // Generate row cut which is inconsistent for model m OsiRowCut rc; const int ne=1; int inx[ne]={10}; double el[ne]={2.5}; rc.setRow(ne,inx,el); assert(rc.consistent()); assert(!rc.consistent(im)); cuts.insert(rc); } { // Generate col cut which is inconsistent for model m OsiColCut cc; const int ne=1; int inx[ne]={30}; double el[ne]={2.0}; cc.setLbs(ne,inx,el); assert(cc.consistent()); assert(!cc.consistent(im)); cuts.insert(cc); } { // Generate col cut which is infeasible OsiColCut cc; const int ne=1; int inx[ne]={0}; double el[ne]={2.0}; cc.setUbs(ne,inx,el); cc.setEffectiveness(1000.); assert(cc.consistent()); assert(cc.consistent(im)); assert(cc.infeasible(im)); cuts.insert(cc); } } assert(cuts.sizeRowCuts()==4); assert(cuts.sizeColCuts()==5); OsiSolverInterface::ApplyCutsReturnCode rc = im.applyCuts(cuts); assert( rc.getNumIneffective() == 2 ); assert( rc.getNumApplied() == 2 ); assert( rc.getNumInfeasible() == 1 ); assert( rc.getNumInconsistentWrtIntegerModel() == 2 ); assert( rc.getNumInconsistent() == 2 ); assert( cuts.sizeCuts() == rc.getNumIneffective() + rc.getNumApplied() + rc.getNumInfeasible() + rc.getNumInconsistentWrtIntegerModel() + rc.getNumInconsistent() ); } { OsiXprSolverInterface xprSi(m); int nc = xprSi.getNumCols(); int nr = xprSi.getNumRows(); assert( nc == 8 ); assert( nr == 5 ); assert( eq(xprSi.getColLower()[0],2.5) ); assert( eq(xprSi.getColLower()[1],0.0) ); assert( eq(xprSi.getColUpper()[1],4.1) ); assert( eq(xprSi.getRowLower()[0],2.5) ); assert( eq(xprSi.getRowLower()[4],3.0) ); assert( eq(xprSi.getRowUpper()[1],2.1) ); assert( eq(xprSi.getRowUpper()[4],15.0) ); // const double * cs = xprSi.getColSolution(); // assert( eq(cs[0],2.5) ); // assert( eq(cs[7],0.0) ); assert( !eq(xprSi.getColLower()[3],1.2345) ); xprSi.setColLower( 3, 1.2345 ); assert( eq(xprSi.getColLower()[3],1.2345) ); assert( !eq(xprSi.getColUpper()[4],10.2345) ); xprSi.setColUpper( 4, 10.2345 ); assert( eq(xprSi.getColUpper()[4],10.2345) ); //assert( eq(xprSi.getObjValue(),0.0) ); assert( eq( xprSi.getObjCoefficients()[0], 1.0) ); assert( eq( xprSi.getObjCoefficients()[1], 0.0) ); assert( eq( xprSi.getObjCoefficients()[2], 0.0) ); assert( eq( xprSi.getObjCoefficients()[3], 0.0) ); assert( eq( xprSi.getObjCoefficients()[4], 2.0) ); assert( eq( xprSi.getObjCoefficients()[5], 0.0) ); assert( eq( xprSi.getObjCoefficients()[6], 0.0) ); assert( eq( xprSi.getObjCoefficients()[7], -1.0) ); } // Test matrixByRow method { const OsiXprSolverInterface si(m); const CoinPackedMatrix * smP = si.getMatrixByRow(); CoinRelFltEq eq; const double * ev = smP->getElements(); assert( eq(ev[0], 3.0) ); assert( eq(ev[1], 1.0) ); assert( eq(ev[2], -2.0) ); assert( eq(ev[3], -1.0) ); assert( eq(ev[4], -1.0) ); assert( eq(ev[5], 2.0) ); assert( eq(ev[6], 1.1) ); assert( eq(ev[7], 1.0) ); assert( eq(ev[8], 1.0) ); assert( eq(ev[9], 2.8) ); assert( eq(ev[10], -1.2) ); assert( eq(ev[11], 5.6) ); assert( eq(ev[12], 1.0) ); assert( eq(ev[13], 1.9) ); const int * mi = smP->getVectorStarts(); assert( mi[0]==0 ); assert( mi[1]==5 ); assert( mi[2]==7 ); assert( mi[3]==9 ); assert( mi[4]==11 ); assert( mi[5]==14 ); const int * ei = smP->getIndices(); assert( ei[0] == 0 ); assert( ei[1] == 1 ); assert( ei[2] == 3 ); assert( ei[3] == 4 ); assert( ei[4] == 7 ); assert( ei[5] == 1 ); assert( ei[6] == 2 ); assert( ei[7] == 2 ); assert( ei[8] == 5 ); assert( ei[9] == 3 ); assert( ei[10] == 6 ); assert( ei[11] == 0 ); assert( ei[12] == 4 ); assert( ei[13] == 7 ); assert( smP->getMajorDim() == 5 ); assert( smP->getMinorDim() == 8 ); assert( smP->getNumElements() == 14 ); assert( smP->getSizeVectorStarts()==6 ); } //-------------- // Test rowsense, rhs, rowrange, matrixByRow { OsiXprSolverInterface lhs; { assert( m.rowrange_==NULL ); assert( m.rowsense_==NULL ); assert( m.rhs_==NULL ); OsiXprSolverInterface siC1(m); assert( siC1.rowrange_==NULL ); assert( siC1.rowsense_==NULL ); assert( siC1.rhs_==NULL ); const char * siC1rs = siC1.getRowSense(); assert( siC1rs[0] == 'G' ); assert( siC1rs[1] == 'L' ); assert( siC1rs[2] == 'E' ); assert( siC1rs[3] == 'R' ); assert( siC1rs[4] == 'R' ); const double * siC1rhs = siC1.getRightHandSide(); assert( eq(siC1rhs[0], 2.5) ); assert( eq(siC1rhs[1], 2.1) ); assert( eq(siC1rhs[2], 4.0) ); assert( eq(siC1rhs[3], 5.0) ); assert( eq(siC1rhs[4], 15.0) ); const double * siC1rr = siC1.getRowRange(); assert( eq(siC1rr[0], 0.0) ); assert( eq(siC1rr[1], 0.0) ); assert( eq(siC1rr[2], 0.0) ); assert( eq(siC1rr[3], 5.0 - 1.8) ); assert( eq(siC1rr[4], 15.0 - 3.0) ); const CoinPackedMatrix * siC1mbr = siC1.getMatrixByRow(); assert( siC1mbr != NULL ); const double * ev = siC1mbr->getElements(); assert( eq(ev[0], 3.0) ); assert( eq(ev[1], 1.0) ); assert( eq(ev[2], -2.0) ); assert( eq(ev[3], -1.0) ); assert( eq(ev[4], -1.0) ); assert( eq(ev[5], 2.0) ); assert( eq(ev[6], 1.1) ); assert( eq(ev[7], 1.0) ); assert( eq(ev[8], 1.0) ); assert( eq(ev[9], 2.8) ); assert( eq(ev[10], -1.2) ); assert( eq(ev[11], 5.6) ); assert( eq(ev[12], 1.0) ); assert( eq(ev[13], 1.9) ); const int * mi = siC1mbr->getVectorStarts(); assert( mi[0]==0 ); assert( mi[1]==5 ); assert( mi[2]==7 ); assert( mi[3]==9 ); assert( mi[4]==11 ); assert( mi[5]==14 ); const int * ei = siC1mbr->getIndices(); assert( ei[0] == 0 ); assert( ei[1] == 1 ); assert( ei[2] == 3 ); assert( ei[3] == 4 ); assert( ei[4] == 7 ); assert( ei[5] == 1 ); assert( ei[6] == 2 ); assert( ei[7 ] == 2 ); assert( ei[8 ] == 5 ); assert( ei[9 ] == 3 ); assert( ei[10] == 6 ); assert( ei[11] == 0 ); assert( ei[12] == 4 ); assert( ei[13] == 7 ); assert( siC1mbr->getMajorDim() == 5 ); assert( siC1mbr->getMinorDim() == 8 ); assert( siC1mbr->getNumElements() == 14 ); assert( siC1mbr->getSizeVectorStarts()==6 ); assert( siC1rs == siC1.getRowSense() ); assert( siC1rhs == siC1.getRightHandSide() ); assert( siC1rr == siC1.getRowRange() ); // Change XPRESS Model by adding free row OsiRowCut rc; rc.setLb(-COIN_DBL_MAX); rc.setUb(COIN_DBL_MAX); OsiCuts cuts; cuts.insert(rc); siC1.applyCuts(cuts); // Since model was changed, test that cached // data is now freed. assert( siC1.rowrange_ == NULL ); assert( siC1.rowsense_ == NULL ); assert( siC1.rhs_ == NULL ); assert( siC1.matrixByRow_ == NULL ); siC1rs = siC1.getRowSense(); assert( siC1rs[0] == 'G' ); assert( siC1rs[1] == 'L' ); assert( siC1rs[2] == 'E' ); assert( siC1rs[3] == 'R' ); assert( siC1rs[4] == 'R' ); assert( siC1rs[5] == 'N' ); siC1rhs = siC1.getRightHandSide(); assert( eq(siC1rhs[0],2.5) ); assert( eq(siC1rhs[1],2.1) ); assert( eq(siC1rhs[2],4.0) ); assert( eq(siC1rhs[3],5.0) ); assert( eq(siC1rhs[4],15.0) ); assert( eq(siC1rhs[5],0.0) ); siC1rr = siC1.getRowRange(); assert( eq(siC1rr[0], 0.0) ); assert( eq(siC1rr[1], 0.0) ); assert( eq(siC1rr[2], 0.0) ); assert( eq(siC1rr[3], 5.0 - 1.8) ); assert( eq(siC1rr[4], 15.0 - 3.0) ); assert( eq(siC1rr[5], 0.0) ); lhs=siC1; } // Test that lhs has correct values even though siC1 has gone out of scope assert( lhs.rowrange_ == NULL ); assert( lhs.rowsense_ == NULL ); assert( lhs.rhs_ == NULL ); assert( lhs.matrixByRow_ == NULL ); const char * lhsrs = lhs.getRowSense(); assert( lhsrs[0] == 'G' ); assert( lhsrs[1] == 'L' ); assert( lhsrs[2] == 'E' ); assert( lhsrs[3] == 'R' ); assert( lhsrs[4] == 'R' ); assert( lhsrs[5] == 'N' ); const double * lhsrhs = lhs.getRightHandSide(); assert( eq(lhsrhs[0], 2.5) ); assert( eq(lhsrhs[1], 2.1) ); assert( eq(lhsrhs[2], 4.0) ); assert( eq(lhsrhs[3], 5.0) ); assert( eq(lhsrhs[4], 15.0) ); assert( eq(lhsrhs[5], 0.0) ); const double *lhsrr = lhs.getRowRange(); assert( eq(lhsrr[0], 0.0) ); assert( eq(lhsrr[1], 0.0) ); assert( eq(lhsrr[2], 0.0) ); assert( eq(lhsrr[3], 5.0 - 1.8) ); assert( eq(lhsrr[4], 15.0 - 3.0) ); assert( eq(lhsrr[5], 0.0) ); const CoinPackedMatrix * lhsmbr = lhs.getMatrixByRow(); assert( lhsmbr != NULL ); const double * ev = lhsmbr->getElements(); assert( eq(ev[0], 3.0) ); assert( eq(ev[1], 1.0) ); assert( eq(ev[2], -2.0) ); assert( eq(ev[3], -1.0) ); assert( eq(ev[4], -1.0) ); assert( eq(ev[5], 2.0) ); assert( eq(ev[6], 1.1) ); assert( eq(ev[7], 1.0) ); assert( eq(ev[8], 1.0) ); assert( eq(ev[9], 2.8) ); assert( eq(ev[10], -1.2) ); assert( eq(ev[11], 5.6) ); assert( eq(ev[12], 1.0) ); assert( eq(ev[13], 1.9) ); const int * mi = lhsmbr->getVectorStarts(); assert( mi[0]==0 ); assert( mi[1]==5 ); assert( mi[2]==7 ); assert( mi[3]==9 ); assert( mi[4]==11 ); assert( mi[5]==14 ); const int * ei = lhsmbr->getIndices(); assert( ei[0] == 0 ); assert( ei[1] == 1 ); assert( ei[2] == 3 ); assert( ei[3] == 4 ); assert( ei[4] == 7 ); assert( ei[5] == 1 ); assert( ei[6] == 2 ); assert( ei[7] == 2 ); assert( ei[8] == 5 ); assert( ei[9] == 3 ); assert( ei[10] == 6 ); assert( ei[11] == 0 ); assert( ei[12] == 4 ); assert( ei[13] == 7 ); assert( lhsmbr->getMajorDim() == 6 ); assert( lhsmbr->getMinorDim() == 8 ); assert( lhsmbr->getNumElements() == 14 ); assert( lhsmbr->getSizeVectorStarts()==7 ); } //-------------- // Test load problem { OsiXprSolverInterface base(m); base.initialSolve(); assert(m.getNumRows() == base.getNumRows()); OsiXprSolverInterface si1,si2,si3,si4; si1.loadProblem( *base.getMatrixByCol(), base.getColLower(),base.getColUpper(),base.getObjCoefficients(), base.getRowSense(),base.getRightHandSide(),base.getRowRange()); si1.initialSolve(); assert(eq(base.getObjValue(), si1.getObjValue())); assert(m.getNumRows() == si1.getNumRows()); si2.loadProblem( *base.getMatrixByRow(), base.getColLower(),base.getColUpper(),base.getObjCoefficients(), base.getRowSense(),base.getRightHandSide(),base.getRowRange()); si2.initialSolve(); assert(eq(base.getObjValue(), si2.getObjValue())); assert(m.getNumRows() == si2.getNumRows()); si3.loadProblem( *base.getMatrixByCol(), base.getColLower(),base.getColUpper(),base.getObjCoefficients(), base.getRowLower(),base.getRowUpper() ); si3.initialSolve(); assert(eq(base.getObjValue(), si3.getObjValue())); assert(m.getNumRows() == si3.getNumRows()); si4.loadProblem( *base.getMatrixByCol(), base.getColLower(),base.getColUpper(),base.getObjCoefficients(), base.getRowLower(),base.getRowUpper() ); si4.initialSolve(); assert(eq(base.getObjValue(), si4.getObjValue())); assert(m.getNumRows() == si4.getNumRows()); base.initialSolve(); si1.initialSolve(); si2.initialSolve(); si3.initialSolve(); si4.initialSolve(); // Create an indices vector assert(base.getNumCols()<10); assert(base.getNumRows()<10); int indices[10]; int i; for (i=0; i<10; i++) indices[i]=i; // Test collower CoinPackedVector basePv,pv; basePv.setVector(base.getNumCols(),indices,base.getColLower()); pv.setVector( si1.getNumCols(),indices, si1.getColLower()); assert(basePv.isEquivalent(pv)); pv.setVector( si2.getNumCols(),indices, si2.getColLower()); assert(basePv.isEquivalent(pv)); pv.setVector( si3.getNumCols(),indices, si3.getColLower()); assert(basePv.isEquivalent(pv)); pv.setVector( si4.getNumCols(),indices, si4.getColLower()); assert(basePv.isEquivalent(pv)); // Test colupper basePv.setVector(base.getNumCols(),indices,base.getColUpper()); pv.setVector( si1.getNumCols(),indices, si1.getColUpper()); assert(basePv.isEquivalent(pv)); pv.setVector( si2.getNumCols(),indices, si2.getColUpper()); assert(basePv.isEquivalent(pv)); pv.setVector( si3.getNumCols(),indices, si3.getColUpper()); assert(basePv.isEquivalent(pv)); pv.setVector( si4.getNumCols(),indices, si4.getColUpper()); assert(basePv.isEquivalent(pv)); // Test getObjCoefficients basePv.setVector(base.getNumCols(),indices,base.getObjCoefficients()); pv.setVector( si1.getNumCols(),indices, si1.getObjCoefficients()); assert(basePv.isEquivalent(pv)); pv.setVector( si2.getNumCols(),indices, si2.getObjCoefficients()); assert(basePv.isEquivalent(pv)); pv.setVector( si3.getNumCols(),indices, si3.getObjCoefficients()); assert(basePv.isEquivalent(pv)); pv.setVector( si4.getNumCols(),indices, si4.getObjCoefficients()); assert(basePv.isEquivalent(pv)); // Test rowlower basePv.setVector(base.getNumRows(),indices,base.getRowLower()); pv.setVector( si1.getNumRows(),indices, si1.getRowLower()); assert( eq(base.getRowLower()[3],si1.getRowLower()[3]) ); assert(basePv.isEquivalent(pv)); pv.setVector( si2.getNumRows(),indices, si2.getRowLower()); assert(basePv.isEquivalent(pv)); pv.setVector( si3.getNumRows(),indices, si3.getRowLower()); assert(basePv.isEquivalent(pv)); pv.setVector( si4.getNumRows(),indices, si4.getRowLower()); assert(basePv.isEquivalent(pv)); // Test rowupper basePv.setVector(base.getNumRows(),indices,base.getRowUpper()); pv.setVector( si1.getNumRows(),indices, si1.getRowUpper()); assert(basePv.isEquivalent(pv)); pv.setVector( si2.getNumRows(),indices, si2.getRowUpper()); assert(basePv.isEquivalent(pv)); pv.setVector( si3.getNumRows(),indices, si3.getRowUpper()); assert(basePv.isEquivalent(pv)); pv.setVector( si4.getNumRows(),indices, si4.getRowUpper()); assert(basePv.isEquivalent(pv)); // Test Constraint Matrix assert( base.getMatrixByCol()->isEquivalent(*si1.getMatrixByCol()) ); assert( base.getMatrixByRow()->isEquivalent(*si1.getMatrixByRow()) ); assert( base.getMatrixByCol()->isEquivalent(*si2.getMatrixByCol()) ); assert( base.getMatrixByRow()->isEquivalent(*si2.getMatrixByRow()) ); assert( base.getMatrixByCol()->isEquivalent(*si3.getMatrixByCol()) ); assert( base.getMatrixByRow()->isEquivalent(*si3.getMatrixByRow()) ); assert( base.getMatrixByCol()->isEquivalent(*si4.getMatrixByCol()) ); assert( base.getMatrixByRow()->isEquivalent(*si4.getMatrixByRow()) ); // Test Objective Value assert( eq(base.getObjValue(),si1.getObjValue()) ); assert( eq(base.getObjValue(),si2.getObjValue()) ); assert( eq(base.getObjValue(),si3.getObjValue()) ); assert( eq(base.getObjValue(),si4.getObjValue()) ); } //-------------- assert(OsiXprSolverInterface::getNumInstances()==1); } assert(OsiXprSolverInterface::getNumInstances()==0); // Do common solverInterface testing by calling the // base class testing method. { OsiXprSolverInterface m; OsiSolverInterfaceCommonUnitTest(&m, mpsDir,netlibDir); } }
//-------------------------------------------------------------------------- void OsiCpxSolverInterfaceUnitTest( const std::string & mpsDir, const std::string & netlibDir ) { // Test default constructor { OsiCpxSolverInterface m; assert( m.obj_==NULL ); assert( m.collower_==NULL ); assert( m.colupper_==NULL ); assert( m.coltype_==NULL ); assert( m.rowsense_==NULL ); assert( m.rhs_==NULL ); assert( m.rowrange_==NULL ); assert( m.rowlower_==NULL ); assert( m.rowupper_==NULL ); assert( m.colsol_==NULL ); assert( m.rowsol_==NULL ); assert( m.matrixByRow_==NULL ); assert( m.matrixByCol_==NULL ); assert( m.coltype_==NULL ); assert( m.coltypesize_==0 ); assert( m.getApplicationData() == NULL ); int i=2346; m.setApplicationData(&i); assert( *((int *)(m.getApplicationData())) == i ); } { CoinRelFltEq eq; OsiCpxSolverInterface m; std::string fn = mpsDir+"exmip1"; m.readMps(fn.c_str(),"mps"); int ad = 13579; m.setApplicationData(&ad); assert( *((int *)(m.getApplicationData())) == ad ); { assert( m.getNumCols()==8 ); const CoinPackedMatrix * colCopy = m.getMatrixByCol(); assert( colCopy->getNumCols() == 8 ); assert( colCopy->getMajorDim() == 8 ); assert( colCopy->getNumRows() == 5 ); assert( colCopy->getMinorDim() == 5 ); assert (colCopy->getVectorLengths()[7] == 2 ); CoinPackedMatrix revColCopy; revColCopy.reverseOrderedCopyOf(*colCopy); CoinPackedMatrix rev2ColCopy; rev2ColCopy.reverseOrderedCopyOf(revColCopy); assert( rev2ColCopy.getNumCols() == 8 ); assert( rev2ColCopy.getMajorDim() == 8 ); assert( rev2ColCopy.getNumRows() == 5 ); assert( rev2ColCopy.getMinorDim() == 5 ); assert( rev2ColCopy.getVectorLengths()[7] == 2 ); } { OsiCpxSolverInterface im; assert( im.getNumCols() == 0 ); } // Test copy constructor and assignment operator { OsiCpxSolverInterface lhs; { assert( *((int *)(m.getApplicationData())) == ad ); OsiCpxSolverInterface im(m); assert( *((int *)(im.getApplicationData())) == ad ); OsiCpxSolverInterface imC1(im); assert( imC1.lp_ != im.lp_ ); assert( imC1.getNumCols() == im.getNumCols() ); assert( imC1.getNumRows() == im.getNumRows() ); assert( *((int *)(imC1.getApplicationData())) == ad ); //im.setModelPtr(m); OsiCpxSolverInterface imC2(im); assert( imC2.lp_ != im.lp_ ); assert( imC2.getNumCols() == im.getNumCols() ); assert( imC2.getNumRows() == im.getNumRows() ); assert( *((int *)(imC2.getApplicationData())) == ad ); assert( imC2.lp_ != imC1.lp_ ); lhs=imC2; } // Test that lhs has correct values even though rhs has gone out of scope assert( lhs.lp_ != m.lp_ ); assert( lhs.getNumCols() == m.getNumCols() ); assert( lhs.getNumRows() == m.getNumRows() ); assert( *((int *)(lhs.getApplicationData())) == ad ); } // Test clone { OsiCpxSolverInterface cplexSi(m); OsiSolverInterface * siPtr = &cplexSi; OsiSolverInterface * siClone = siPtr->clone(); OsiCpxSolverInterface * cplexClone = dynamic_cast<OsiCpxSolverInterface*>(siClone); assert( cplexClone != NULL ); assert( cplexClone->lp_ != cplexSi.lp_ ); assert( cplexClone->getNumRows() == cplexSi.getNumRows() ); assert( cplexClone->getNumCols() == m.getNumCols() ); assert( *((int *)(cplexClone->getApplicationData())) == ad ); delete siClone; } // test infinity { OsiCpxSolverInterface si; assert( eq( si.getInfinity(), CPX_INFBOUND ) ); } // Test setting solution { OsiCpxSolverInterface m1(m); int i; double * cs = new double[m1.getNumCols()]; for ( i = 0; i < m1.getNumCols(); i++ ) cs[i] = i + .5; m1.setColSolution(cs); for ( i = 0; i < m1.getNumCols(); i++ ) assert(m1.getColSolution()[i] == i + .5); double * rs = new double[m1.getNumRows()]; for ( i = 0; i < m1.getNumRows(); i++ ) rs[i] = i - .5; m1.setRowPrice(rs); for ( i = 0; i < m1.getNumRows(); i++ ) assert(m1.getRowPrice()[i] == i - .5); delete [] cs; delete [] rs; } // Test fraction Indices { OsiCpxSolverInterface fim; std::string fn = mpsDir+"exmip1"; fim.readMps(fn.c_str(),"mps"); //fim.setModelPtr(m); // exmip1.mps has 2 integer variables with index 2 & 3 assert( fim.isContinuous(0) ); assert( fim.isContinuous(1) ); assert( !fim.isContinuous(2) ); assert( !fim.isContinuous(3) ); assert( fim.isContinuous(4) ); assert( !fim.isInteger(0) ); assert( !fim.isInteger(1) ); assert( fim.isInteger(2) ); assert( fim.isInteger(3) ); assert( !fim.isInteger(4) ); assert( !fim.isBinary(0) ); assert( !fim.isBinary(1) ); assert( fim.isBinary(2) ); assert( fim.isBinary(3) ); assert( !fim.isBinary(4) ); assert( !fim.isIntegerNonBinary(0) ); assert( !fim.isIntegerNonBinary(1) ); assert( !fim.isIntegerNonBinary(2) ); assert( !fim.isIntegerNonBinary(3) ); assert( !fim.isIntegerNonBinary(4) ); // Test fractionalIndices { // Set a solution vector double * cs = new double[fim.getNumCols()]; for ( int i = 0; i < fim.getNumCols(); cs[i++] = 0.0 ); cs[2] = 2.9; cs[3] = 3.0; fim.setColSolution(cs); OsiVectorInt fi = fim.getFractionalIndices(); assert( fi.size() == 1 ); assert( fi[0]==2 ); // Set integer variables very close to integer values cs[2] = 5 + .00001/2.; cs[3] = 8 - .00001/2.; fim.setColSolution(cs); fi = fim.getFractionalIndices(1e-5); assert( fi.size() == 0 ); // Set integer variables close, but beyond tolerances cs[2] = 5 + .00001*2.; cs[3] = 8 - .00001*2.; fim.setColSolution(cs); fi = fim.getFractionalIndices(1e-5); assert( fi.size() == 2 ); assert( fi[0]==2 ); assert( fi[1]==3 ); delete [] cs; } // Change data so column 2 & 3 are integerNonBinary fim.setColUpper(2, 5); fim.setColUpper(3, 6.0); assert( !fim.isBinary(0) ); assert( !fim.isBinary(1) ); assert( !fim.isBinary(2) ); assert( !fim.isBinary(3) ); assert( !fim.isBinary(4) ); assert( !fim.isIntegerNonBinary(0) ); assert( !fim.isIntegerNonBinary(1) ); assert( fim.isIntegerNonBinary(2) ); assert( fim.isIntegerNonBinary(3) ); assert( !fim.isIntegerNonBinary(4) ); } // Test apply cuts method { OsiCpxSolverInterface im(m); OsiCuts cuts; // Generate some cuts { // Get number of rows and columns in model int nr=im.getNumRows(); int nc=im.getNumCols(); assert( nr == 5 ); assert( nc == 8 ); // Generate a valid row cut from thin air int c; { int *inx = new int[nc]; for (c=0;c<nc;c++) inx[c]=c; double *el = new double[nc]; for (c=0;c<nc;c++) el[c]=((double)c)*((double)c); OsiRowCut rc; rc.setRow(nc,inx,el); rc.setLb(-100.); rc.setUb(100.); rc.setEffectiveness(22); cuts.insert(rc); delete[]el; delete[]inx; } // Generate valid col cut from thin air { const double * cplexColLB = im.getColLower(); const double * cplexColUB = im.getColUpper(); int *inx = new int[nc]; for (c=0;c<nc;c++) inx[c]=c; double *lb = new double[nc]; double *ub = new double[nc]; for (c=0;c<nc;c++) lb[c]=cplexColLB[c]+0.001; for (c=0;c<nc;c++) ub[c]=cplexColUB[c]-0.001; OsiColCut cc; cc.setLbs(nc,inx,lb); cc.setUbs(nc,inx,ub); cuts.insert(cc); delete [] ub; delete [] lb; delete [] inx; } { // Generate a row and column cut which have are ineffective OsiRowCut * rcP= new OsiRowCut; rcP->setEffectiveness(-1.); cuts.insert(rcP); assert(rcP==NULL); OsiColCut * ccP= new OsiColCut; ccP->setEffectiveness(-12.); cuts.insert(ccP); assert(ccP==NULL); } { //Generate inconsistent Row cut OsiRowCut rc; const int ne=1; int inx[ne]={-10}; double el[ne]={2.5}; rc.setRow(ne,inx,el); rc.setLb(3.); rc.setUb(4.); assert(!rc.consistent()); cuts.insert(rc); } { //Generate inconsistent col cut OsiColCut cc; const int ne=1; int inx[ne]={-10}; double el[ne]={2.5}; cc.setUbs(ne,inx,el); assert(!cc.consistent()); cuts.insert(cc); } { // Generate row cut which is inconsistent for model m OsiRowCut rc; const int ne=1; int inx[ne]={10}; double el[ne]={2.5}; rc.setRow(ne,inx,el); assert(rc.consistent()); assert(!rc.consistent(im)); cuts.insert(rc); } { // Generate col cut which is inconsistent for model m OsiColCut cc; const int ne=1; int inx[ne]={30}; double el[ne]={2.0}; cc.setLbs(ne,inx,el); assert(cc.consistent()); assert(!cc.consistent(im)); cuts.insert(cc); } { // Generate col cut which is infeasible OsiColCut cc; const int ne=1; int inx[ne]={0}; double el[ne]={2.0}; cc.setUbs(ne,inx,el); cc.setEffectiveness(1000.); assert(cc.consistent()); assert(cc.consistent(im)); assert(cc.infeasible(im)); cuts.insert(cc); } } assert(cuts.sizeRowCuts()==4); assert(cuts.sizeColCuts()==5); OsiSolverInterface::ApplyCutsReturnCode rc = im.applyCuts(cuts); assert( rc.getNumIneffective() == 2 ); assert( rc.getNumApplied() == 2 ); assert( rc.getNumInfeasible() == 1 ); assert( rc.getNumInconsistentWrtIntegerModel() == 2 ); assert( rc.getNumInconsistent() == 2 ); assert( cuts.sizeCuts() == rc.getNumIneffective() + rc.getNumApplied() + rc.getNumInfeasible() + rc.getNumInconsistentWrtIntegerModel() + rc.getNumInconsistent() ); } { OsiCpxSolverInterface cplexSi(m); int nc = cplexSi.getNumCols(); int nr = cplexSi.getNumRows(); const double * cl = cplexSi.getColLower(); const double * cu = cplexSi.getColUpper(); const double * rl = cplexSi.getRowLower(); const double * ru = cplexSi.getRowUpper(); assert( nc == 8 ); assert( nr == 5 ); assert( eq(cl[0],2.5) ); assert( eq(cl[1],0.0) ); assert( eq(cu[1],4.1) ); assert( eq(cu[2],1.0) ); assert( eq(rl[0],2.5) ); assert( eq(rl[4],3.0) ); assert( eq(ru[1],2.1) ); assert( eq(ru[4],15.0) ); double newCs[8] = {1., 2., 3., 4., 5., 6., 7., 8.}; cplexSi.setColSolution(newCs); const double * cs = cplexSi.getColSolution(); assert( eq(cs[0],1.0) ); assert( eq(cs[7],8.0) ); { OsiCpxSolverInterface solnSi(cplexSi); const double * cs = solnSi.getColSolution(); assert( eq(cs[0],1.0) ); assert( eq(cs[7],8.0) ); } assert( !eq(cl[3],1.2345) ); cplexSi.setColLower( 3, 1.2345 ); assert( eq(cplexSi.getColLower()[3],1.2345) ); assert( !eq(cu[4],10.2345) ); cplexSi.setColUpper( 4, 10.2345 ); assert( eq(cplexSi.getColUpper()[4],10.2345) ); assert( eq(cplexSi.getObjValue(),0.0) ); assert( eq( cplexSi.getObjCoefficients()[0], 1.0) ); assert( eq( cplexSi.getObjCoefficients()[1], 0.0) ); assert( eq( cplexSi.getObjCoefficients()[2], 0.0) ); assert( eq( cplexSi.getObjCoefficients()[3], 0.0) ); assert( eq( cplexSi.getObjCoefficients()[4], 2.0) ); assert( eq( cplexSi.getObjCoefficients()[5], 0.0) ); assert( eq( cplexSi.getObjCoefficients()[6], 0.0) ); assert( eq( cplexSi.getObjCoefficients()[7], -1.0) ); } // Test getMatrixByRow method { const OsiCpxSolverInterface si(m); const CoinPackedMatrix * smP = si.getMatrixByRow(); //const CoinPackedMatrix * osmP = dynamic_cast(const OsiCpxPackedMatrix*)(smP); //assert( osmP!=NULL ); CoinRelFltEq eq; const double * ev = smP->getElements(); assert( eq(ev[0], 3.0) ); assert( eq(ev[1], 1.0) ); assert( eq(ev[2], -2.0) ); assert( eq(ev[3], -1.0) ); assert( eq(ev[4], -1.0) ); assert( eq(ev[5], 2.0) ); assert( eq(ev[6], 1.1) ); assert( eq(ev[7], 1.0) ); assert( eq(ev[8], 1.0) ); assert( eq(ev[9], 2.8) ); assert( eq(ev[10], -1.2) ); assert( eq(ev[11], 5.6) ); assert( eq(ev[12], 1.0) ); assert( eq(ev[13], 1.9) ); const int * mi = smP->getVectorStarts(); assert( mi[0]==0 ); assert( mi[1]==5 ); assert( mi[2]==7 ); assert( mi[3]==9 ); assert( mi[4]==11 ); assert( mi[5]==14 ); const int * ei = smP->getIndices(); assert( ei[0] == 0 ); assert( ei[1] == 1 ); assert( ei[2] == 3 ); assert( ei[3] == 4 ); assert( ei[4] == 7 ); assert( ei[5] == 1 ); assert( ei[6] == 2 ); assert( ei[7] == 2 ); assert( ei[8] == 5 ); assert( ei[9] == 3 ); assert( ei[10] == 6 ); assert( ei[11] == 0 ); assert( ei[12] == 4 ); assert( ei[13] == 7 ); assert( smP->getMajorDim() == 5 ); assert( smP->getNumElements() == 14 ); } //-------------- // Test rowsense, rhs, rowrange, getMatrixByRow { OsiCpxSolverInterface lhs; { #if 0 assert( m.obj_==NULL ); assert( m.collower_==NULL ); assert( m.colupper_==NULL ); assert( m.rowrange_==NULL ); assert( m.rowsense_==NULL ); assert( m.rhs_==NULL ); assert( m.rowlower_==NULL ); assert( m.rowupper_==NULL ); assert( m.colsol_==NULL ); assert( m.rowsol_==NULL ); assert( m.getMatrixByRow_==NULL ); #endif OsiCpxSolverInterface siC1(m); assert( siC1.obj_==NULL ); assert( siC1.collower_==NULL ); assert( siC1.colupper_==NULL ); // assert( siC1.coltype_==NULL ); assert( siC1.rowrange_==NULL ); assert( siC1.rowsense_==NULL ); assert( siC1.rhs_==NULL ); assert( siC1.rowlower_==NULL ); assert( siC1.rowupper_==NULL ); assert( siC1.colsol_!=NULL ); assert( siC1.rowsol_!=NULL ); assert( siC1.matrixByRow_==NULL ); const char * siC1rs = siC1.getRowSense(); assert( siC1rs[0]=='G' ); assert( siC1rs[1]=='L' ); assert( siC1rs[2]=='E' ); assert( siC1rs[3]=='R' ); assert( siC1rs[4]=='R' ); const double * siC1rhs = siC1.getRightHandSide(); assert( eq(siC1rhs[0],2.5) ); assert( eq(siC1rhs[1],2.1) ); assert( eq(siC1rhs[2],4.0) ); assert( eq(siC1rhs[3],5.0) ); assert( eq(siC1rhs[4],15.) ); const double * siC1rr = siC1.getRowRange(); assert( eq(siC1rr[0],0.0) ); assert( eq(siC1rr[1],0.0) ); assert( eq(siC1rr[2],0.0) ); assert( eq(siC1rr[3],5.0-1.8) ); assert( eq(siC1rr[4],15.0-3.0) ); const CoinPackedMatrix * siC1mbr = siC1.getMatrixByRow(); assert( siC1mbr != NULL ); const double * ev = siC1mbr->getElements(); assert( eq(ev[0], 3.0) ); assert( eq(ev[1], 1.0) ); assert( eq(ev[2], -2.0) ); assert( eq(ev[3], -1.0) ); assert( eq(ev[4], -1.0) ); assert( eq(ev[5], 2.0) ); assert( eq(ev[6], 1.1) ); assert( eq(ev[7], 1.0) ); assert( eq(ev[8], 1.0) ); assert( eq(ev[9], 2.8) ); assert( eq(ev[10], -1.2) ); assert( eq(ev[11], 5.6) ); assert( eq(ev[12], 1.0) ); assert( eq(ev[13], 1.9) ); const int * mi = siC1mbr->getVectorStarts(); assert( mi[0]==0 ); assert( mi[1]==5 ); assert( mi[2]==7 ); assert( mi[3]==9 ); assert( mi[4]==11 ); assert( mi[5]==14 ); const int * ei = siC1mbr->getIndices(); assert( ei[0] == 0 ); assert( ei[1] == 1 ); assert( ei[2] == 3 ); assert( ei[3] == 4 ); assert( ei[4] == 7 ); assert( ei[5] == 1 ); assert( ei[6] == 2 ); assert( ei[7] == 2 ); assert( ei[8] == 5 ); assert( ei[9] == 3 ); assert( ei[10] == 6 ); assert( ei[11] == 0 ); assert( ei[12] == 4 ); assert( ei[13] == 7 ); assert( siC1mbr->getMajorDim() == 5 ); assert( siC1mbr->getNumElements() == 14 ); assert( siC1rs == siC1.getRowSense() ); assert( siC1rhs == siC1.getRightHandSide() ); assert( siC1rr == siC1.getRowRange() ); // Change CPLEX Model by adding free row OsiRowCut rc; rc.setLb(-COIN_DBL_MAX); rc.setUb( COIN_DBL_MAX); OsiCuts cuts; cuts.insert(rc); siC1.applyCuts(cuts); // Since model was changed, test that cached // data is now freed. assert( siC1.obj_==NULL ); assert( siC1.collower_==NULL ); assert( siC1.colupper_==NULL ); // assert( siC1.coltype_==NULL ); assert( siC1.rowrange_==NULL ); assert( siC1.rowsense_==NULL ); assert( siC1.rhs_==NULL ); assert( siC1.rowlower_==NULL ); assert( siC1.rowupper_==NULL ); assert( siC1.colsol_==NULL ); assert( siC1.rowsol_==NULL ); assert( siC1.matrixByRow_==NULL ); siC1rs = siC1.getRowSense(); siC1rhs = siC1.getRightHandSide(); siC1rr = siC1.getRowRange(); assert( siC1rs[0]=='G' ); assert( siC1rs[1]=='L' ); assert( siC1rs[2]=='E' ); assert( siC1rs[3]=='R' ); assert( siC1rs[4]=='R' ); assert( siC1rs[5]=='N' ); assert( eq(siC1rhs[0],2.5) ); assert( eq(siC1rhs[1],2.1) ); assert( eq(siC1rhs[2],4.0) ); assert( eq(siC1rhs[3],5.0) ); assert( eq(siC1rhs[4],15.) ); assert( eq(siC1rhs[5],0.0) ); assert( eq(siC1rr[0],0.0) ); assert( eq(siC1rr[1],0.0) ); assert( eq(siC1rr[2],0.0) ); assert( eq(siC1rr[3],5.0-1.8) ); assert( eq(siC1rr[4],15.0-3.0) ); assert( eq(siC1rr[5],0.0) ); lhs=siC1; } // Test that lhs has correct values even though siC1 has gone out of scope assert( lhs.obj_==NULL ); assert( lhs.collower_==NULL ); assert( lhs.colupper_==NULL ); // assert( lhs.coltype_==NULL ); assert( lhs.rowrange_==NULL ); assert( lhs.rowsense_==NULL ); assert( lhs.rhs_==NULL ); assert( lhs.rowlower_==NULL ); assert( lhs.rowupper_==NULL ); assert( lhs.colsol_!=NULL ); assert( lhs.rowsol_!=NULL ); assert( lhs.matrixByRow_==NULL ); const char * lhsrs = lhs.getRowSense(); assert( lhsrs[0]=='G' ); assert( lhsrs[1]=='L' ); assert( lhsrs[2]=='E' ); assert( lhsrs[3]=='R' ); assert( lhsrs[4]=='R' ); assert( lhsrs[5]=='N' ); const double * lhsrhs = lhs.getRightHandSide(); assert( eq(lhsrhs[0],2.5) ); assert( eq(lhsrhs[1],2.1) ); assert( eq(lhsrhs[2],4.0) ); assert( eq(lhsrhs[3],5.0) ); assert( eq(lhsrhs[4],15.) ); assert( eq(lhsrhs[5],0.0) ); const double *lhsrr = lhs.getRowRange(); assert( eq(lhsrr[0],0.0) ); assert( eq(lhsrr[1],0.0) ); assert( eq(lhsrr[2],0.0) ); assert( eq(lhsrr[3],5.0-1.8) ); assert( eq(lhsrr[4],15.0-3.0) ); assert( eq(lhsrr[5],0.0) ); const CoinPackedMatrix * lhsmbr = lhs.getMatrixByRow(); assert( lhsmbr != NULL ); const double * ev = lhsmbr->getElements(); assert( eq(ev[0], 3.0) ); assert( eq(ev[1], 1.0) ); assert( eq(ev[2], -2.0) ); assert( eq(ev[3], -1.0) ); assert( eq(ev[4], -1.0) ); assert( eq(ev[5], 2.0) ); assert( eq(ev[6], 1.1) ); assert( eq(ev[7], 1.0) ); assert( eq(ev[8], 1.0) ); assert( eq(ev[9], 2.8) ); assert( eq(ev[10], -1.2) ); assert( eq(ev[11], 5.6) ); assert( eq(ev[12], 1.0) ); assert( eq(ev[13], 1.9) ); const int * mi = lhsmbr->getVectorStarts(); assert( mi[0]==0 ); assert( mi[1]==5 ); assert( mi[2]==7 ); assert( mi[3]==9 ); assert( mi[4]==11 ); assert( mi[5]==14 ); const int * ei = lhsmbr->getIndices(); assert( ei[0] == 0 ); assert( ei[1] == 1 ); assert( ei[2] == 3 ); assert( ei[3] == 4 ); assert( ei[4] == 7 ); assert( ei[5] == 1 ); assert( ei[6] == 2 ); assert( ei[7] == 2 ); assert( ei[8] == 5 ); assert( ei[9] == 3 ); assert( ei[10] == 6 ); assert( ei[11] == 0 ); assert( ei[12] == 4 ); assert( ei[13] == 7 ); int md = lhsmbr->getMajorDim(); assert( md == 6 ); assert( lhsmbr->getNumElements() == 14 ); } } // Do common solverInterface testing by calling the // base class testing method. { OsiCpxSolverInterface m; OsiSolverInterfaceCommonUnitTest(&m, mpsDir,netlibDir); } }
CbcBranchingObject * CbcBranchToFixLots::createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * /*info*/, int /*way*/) { // by default way must be -1 //assert (way==-1); //OsiSolverInterface * solver = model_->solver(); const double * solution = model_->testSolution(); const double * lower = solver->getColLower(); const double * upper = solver->getColUpper(); const double * dj = solver->getReducedCost(); int i; int numberIntegers = model_->numberIntegers(); const int * integerVariable = model_->integerVariable(); double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); // make smaller ? double tolerance = CoinMin(1.0e-8, integerTolerance); // How many fixed are we aiming at int wantedFixed = static_cast<int> (static_cast<double>(numberIntegers) * fractionFixed_); int nSort = 0; int numberFixed = 0; int numberColumns = solver->getNumCols(); int * sort = new int[numberColumns]; double * dsort = new double[numberColumns]; if (djTolerance_ != -1.234567) { int type = shallWe(); assert (type); // Take clean first if (type == 1) { for (i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; if (upper[iColumn] > lower[iColumn]) { if (!mark_ || !mark_[iColumn]) { if (solution[iColumn] < lower[iColumn] + tolerance) { if (dj[iColumn] > djTolerance_) { dsort[nSort] = -dj[iColumn]; sort[nSort++] = iColumn; } } else if (solution[iColumn] > upper[iColumn] - tolerance) { if (dj[iColumn] < -djTolerance_) { dsort[nSort] = dj[iColumn]; sort[nSort++] = iColumn; } } } } else { numberFixed++; } } // sort CoinSort_2(dsort, dsort + nSort, sort); nSort = CoinMin(nSort, wantedFixed - numberFixed); } else if (type < 10) { int i; //const double * rowLower = solver->getRowLower(); const double * rowUpper = solver->getRowUpper(); // Row copy const double * elementByRow = matrixByRow_.getElements(); const int * column = matrixByRow_.getIndices(); const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts(); const int * rowLength = matrixByRow_.getVectorLengths(); const double * columnLower = solver->getColLower(); const double * columnUpper = solver->getColUpper(); const double * solution = solver->getColSolution(); int numberColumns = solver->getNumCols(); int numberRows = solver->getNumRows(); for (i = 0; i < numberColumns; i++) { sort[i] = i; if (columnLower[i] != columnUpper[i]) { dsort[i] = 1.0e100; } else { dsort[i] = 1.0e50; numberFixed++; } } for (i = 0; i < numberRows; i++) { double rhsValue = rowUpper[i]; bool oneRow = true; // check elements int numberUnsatisfied = 0; for (int j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) { int iColumn = column[j]; double value = elementByRow[j]; double solValue = solution[iColumn]; if (columnLower[iColumn] != columnUpper[iColumn]) { if (solValue < 1.0 - integerTolerance && solValue > integerTolerance) numberUnsatisfied++; if (value != 1.0) { oneRow = false; break; } } else { rhsValue -= value * floor(solValue + 0.5); } } if (oneRow && rhsValue <= 1.0 + tolerance) { if (!numberUnsatisfied) { for (int j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) { int iColumn = column[j]; if (dsort[iColumn] > 1.0e50) { dsort[iColumn] = 0; nSort++; } } } } } // sort CoinSort_2(dsort, dsort + numberColumns, sort); } else { // new way for (i = 0; i < numberIntegers; i++) { int iColumn = integerVariable[i]; if (upper[iColumn] > lower[iColumn]) { if (!mark_ || !mark_[iColumn]) { double distanceDown = solution[iColumn] - lower[iColumn]; double distanceUp = upper[iColumn] - solution[iColumn]; double distance = CoinMin(distanceDown, distanceUp); if (distance > 0.001 && distance < 0.5) { dsort[nSort] = distance; sort[nSort++] = iColumn; } } } } // sort CoinSort_2(dsort, dsort + nSort, sort); int n = 0; double sum = 0.0; for (int k = 0; k < nSort; k++) { sum += dsort[k]; if (sum <= djTolerance_) n = k; else break; } nSort = CoinMin(n, numberClean_ / 1000000); } } else { #define FIX_IF_LESS -0.1 // 3 in same row and sum <FIX_IF_LESS? int numberRows = matrixByRow_.getNumRows(); const double * solution = model_->testSolution(); const int * column = matrixByRow_.getIndices(); const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts(); const int * rowLength = matrixByRow_.getVectorLengths(); double bestSum = 1.0; int nBest = -1; int kRow = -1; OsiSolverInterface * solver = model_->solver(); for (int i = 0; i < numberRows; i++) { int numberUnsatisfied = 0; double sum = 0.0; for (int j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) { int iColumn = column[j]; if (solver->isInteger(iColumn)) { double solValue = solution[iColumn]; if (solValue > 1.0e-5 && solValue < FIX_IF_LESS) { numberUnsatisfied++; sum += solValue; } } } if (numberUnsatisfied >= 3 && sum < FIX_IF_LESS) { // possible if (numberUnsatisfied > nBest || (numberUnsatisfied == nBest && sum < bestSum)) { nBest = numberUnsatisfied; bestSum = sum; kRow = i; } } } assert (nBest > 0); for (int j = rowStart[kRow]; j < rowStart[kRow] + rowLength[kRow]; j++) { int iColumn = column[j]; if (solver->isInteger(iColumn)) { double solValue = solution[iColumn]; if (solValue > 1.0e-5 && solValue < FIX_IF_LESS) { sort[nSort++] = iColumn; } } } } OsiRowCut down; down.setLb(-COIN_DBL_MAX); double rhs = 0.0; for (i = 0; i < nSort; i++) { int iColumn = sort[i]; double distanceDown = solution[iColumn] - lower[iColumn]; double distanceUp = upper[iColumn] - solution[iColumn]; if (distanceDown < distanceUp) { rhs += lower[iColumn]; dsort[i] = 1.0; } else { rhs -= upper[iColumn]; dsort[i] = -1.0; } } down.setUb(rhs); down.setRow(nSort, sort, dsort); down.setEffectiveness(COIN_DBL_MAX); // so will persist delete [] sort; delete [] dsort; // up is same - just with rhs changed OsiRowCut up = down; up.setLb(rhs + 1.0); up.setUb(COIN_DBL_MAX); // Say can fix one way CbcCutBranchingObject * newObject = new CbcCutBranchingObject(model_, down, up, true); if (model_->messageHandler()->logLevel() > 1) printf("creating cut in CbcBranchCut\n"); return newObject; }