// Copyright (C) 2006, International Business Machines // Corporation and others. All Rights Reserved. #if defined(_MSC_VER) // Turn off compiler warning about long names # pragma warning(disable:4786) #endif #include // For Branch and bound #include "OsiClpSolverInterface.hpp" #include "CbcModel.hpp" #include "CbcCutGenerator.hpp" #include "CoinHelperFunctions.hpp" #include "CbcStrategy.hpp" // Need stored cuts #include "CglStored.hpp" // For saying about solution validity #include "OsiAuxInfo.hpp" // Time #include "CoinTime.hpp" // Class to disallow strong branching solutions #include "CbcFeasibilityBase.hpp" class CbcFeasibilityNoStrong : public CbcFeasibilityBase{ public: // Default Constructor CbcFeasibilityNoStrong () {} virtual ~CbcFeasibilityNoStrong() {} // Copy constructor CbcFeasibilityNoStrong ( const CbcFeasibilityNoStrong &rhs) {} // Assignment operator CbcFeasibilityNoStrong & operator=( const CbcFeasibilityNoStrong& rhs) { return * this;} /// Clone virtual CbcFeasibilityBase * clone() const { return new CbcFeasibilityNoStrong();} /** On input mode: 0 - called after a solve but before any cuts -1 - called after strong branching Returns : 0 - no opinion -1 pretend infeasible 1 pretend integer solution */ virtual int feasible(CbcModel * model, int mode) {return mode;} }; /************************************************************************ This main program solves the following 0-1 problem: min -x0 - 2x1 - 3x2 - 4x3 subject to x0 + x1 + x2 + x3 <= 2 and quadratic constraints with positive random numbers It does it creating extra yij variables and constraints xi + xj -1 <= yij and putting quadratic elements on y The extra constraints are treated as stored cuts. This is to show how to keep branching even if we have a solution ************************************************************************/ int main (int argc, const char *argv[]) { // Define a Solver which inherits from OsiClpsolverInterface -> OsiSolverInterface OsiClpSolverInterface solver1; int nX=4; int nY = (nX * (nX-1)/2); // All columns double * obj = new double [nX+nY]; double * clo = new double[nX+nY]; double * cup = new double[nX+nY]; int i; for (i=0;isetMustCallAgain(true); // Say cuts needed at continuous (b) OsiBabSolver oddCuts; oddCuts.setSolverType(4); model.passInSolverCharacteristics(&oddCuts); // Say no to all solutions by strong branching (c) CbcFeasibilityNoStrong noStrong; model.setProblemFeasibility(noStrong); // Say don't recompute solution d) model.setSpecialOptions(4); double time1 = CoinCpuTime(); // Do complete search model.branchAndBound(); std::cout<getNumCols(); //const double * solution = model.bestSolution(); const double * solution = model.solver()->getColSolution(); int iColumn; for (iColumn=0;iColumn1.0e-7&&model.solver()->isInteger(iColumn)) printf("Column %d has value %g\n",iColumn,value); } } return 0; }