Ignore:
Timestamp:
Oct 21, 2012 9:34:48 AM (7 years ago)
Author:
forrest
Message:

make maximization simpler

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/src/CbcModel.cpp

    r1791 r1798  
    16071607    dblParam_[CbcLargestChange] = 0.0;
    16081608    intParam_[CbcNumberBranches] = 0;
    1609     // Double check optimization directions line up
    1610     dblParam_[CbcOptimizationDirection] = solver_->getObjSense();
     1609    // Force minimization !!!!
     1610    bool flipObjective = (solver_->getObjSense()<0.0);
     1611    if (flipObjective)
     1612      flipModel();
     1613    dblParam_[CbcOptimizationDirection] = 1.0; // was solver_->getObjSense();
    16111614    strongInfo_[0] = 0;
    16121615    strongInfo_[1] = 0;
     
    16351638            CbcDisasterHandler handler(this);
    16361639            clpSolver->passInDisasterHandler(&handler);
    1637             // Initialise solvers seed
    1638             clpSolver->getModelPtr()->setRandomSeed(1234567);
     1640            // Initialise solvers seed (unless users says not)
     1641            if ((specialOptions_&4194304)==0)
     1642              clpSolver->getModelPtr()->setRandomSeed(1234567);
    16391643#ifdef JJF_ZERO
    16401644            // reduce factorization frequency
     
    16731677                secondaryStatus_ = 1;
    16741678                originalContinuousObjective_ = COIN_DBL_MAX;
     1679                if (flipObjective)
     1680                  flipModel();
    16751681                return ;
    16761682            } else if (numberObjects_ && object_) {
     
    20222028        originalContinuousObjective_ = COIN_DBL_MAX;
    20232029        solverCharacteristics_ = NULL;
     2030        if (flipObjective)
     2031          flipModel();
    20242032        return ;
    20252033    } else if (!numberObjects_ && (!strategy_ || strategy_->preProcessState() <= 0)) {
    20262034        // nothing to do
     2035        if (flipObjective)
     2036          flipModel();
    20272037        solverCharacteristics_ = NULL;
    20282038        bestObjective_ = solver_->getObjValue() * solver_->getObjSense();
     
    27892799        delete [] upperBefore;
    27902800        delete saveSolver;
     2801        if (flipObjective)
     2802          flipModel();
    27912803        return;
    27922804    }
     
    43324344        }
    43334345    }
     4346    if (flipObjective)
     4347      flipModel();
    43344348#ifdef COIN_HAS_CLP
    43354349    {
     
    1564415658    specialOptions_ |= setFlag;
    1564515659}
     15660static void flipSolver(OsiSolverInterface * solver, double newCutoff)
     15661{
     15662  if (solver) {
     15663    double objValue = solver->getObjValue();
     15664    double objectiveOffset;
     15665    solver->setObjSense(-solver->getObjSense());
     15666    solver->getDblParam(OsiObjOffset,objectiveOffset);
     15667    solver->setDblParam(OsiObjOffset,-objectiveOffset);
     15668    int numberColumns = solver->getNumCols();
     15669    double * array = CoinCopyOfArray(solver->getObjCoefficients(),numberColumns);
     15670    for (int i=0;i<numberColumns;i++)
     15671      array[i] = - array[i];
     15672    solver->setObjective(array);
     15673    delete [] array;
     15674    solver->setDblParam(OsiDualObjectiveLimit,newCutoff);
     15675#ifdef COIN_HAS_CLP
     15676    OsiClpSolverInterface * clpSolver
     15677      = dynamic_cast<OsiClpSolverInterface *> (solver);
     15678    if (clpSolver) {
     15679      double * dj = clpSolver->getModelPtr()->dualColumnSolution();
     15680      for (int i=0;i<numberColumns;i++)
     15681        dj[i] = - dj[i];
     15682      int numberRows=clpSolver->getNumRows();
     15683      double * pi = clpSolver->getModelPtr()->dualRowSolution();
     15684      for (int i=0;i<numberRows;i++)
     15685        pi[i] = - pi[i];
     15686      clpSolver->getModelPtr()->setObjectiveValue(-objValue);
     15687    } else {
     15688#endif
     15689      // update values
     15690      solver->resolve();
     15691#ifdef COIN_HAS_CLP
     15692    }
     15693#endif
     15694  }
     15695}
     15696/*
     15697  Flip direction of optimization on all models
     15698*/
     15699void
     15700CbcModel::flipModel()
     15701{
     15702  if (parentModel_)
     15703    return;
     15704  // I think cutoff is always minimization
     15705  double cutoff=getCutoff();
     15706  flipSolver(referenceSolver_,cutoff);
     15707  flipSolver(continuousSolver_,cutoff);
     15708  flipSolver(solver_,cutoff);
     15709}
    1564615710#ifdef CBC_KEEP_DEPRECATED
    1564715711/* preProcess problem - replacing solver
Note: See TracChangeset for help on using the changeset viewer.