Changeset 1092


Ignore:
Timestamp:
Oct 24, 2008 4:43:52 PM (11 years ago)
Author:
rlh
Message:

various changes, including making it only run once

File:
1 edited

Legend:

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

    r1077 r1092  
    3131
    3232// Constructor with model - assumed before cuts
    33 
    3433CbcHeuristicRandRound::CbcHeuristicRandRound(CbcModel & model)
    3534  :CbcHeuristic(model)
    3635{
     36  model_=&model;
     37  setWhen(1);
    3738}
    3839
     
    4849  return new CbcHeuristicRandRound(*this);
    4950}
     51
    5052// Create C++ lines to get to current state
    5153void
     
    8284  //CbcHeuristic::resetModel(model);
    8385}
     86
    8487/*
    8588  Randomized Rounding Heuristic
    86   Returns 1 if solution, 0 if not */
     89  Returns 1 if solution, 0 if not
     90*/
    8791int
    8892CbcHeuristicRandRound::solution(double & solutionValue,
    8993                         double * betterSolution)
    9094{
    91   std::cout << "Lucky you! You're in the Randomized Rounding Heuristic" << std::endl;
    92   // The struct should be moved to member data
    93 
     95  // rlh: Todo: Memory Cleanup
     96
     97  setWhen(1);  // setWhen(1) didn't have the effect I expected (e.g., run once).
     98             
     99  // Run only once.
     100  //
     101  //    See if at root node
     102  bool atRoot = model_->getNodeCount()==0;
     103  int passNumber = model_->getCurrentPassNumber();
     104  //    Just do once
     105  if (!atRoot||passNumber!=1)
     106    return 0;
     107
     108  std::cout << "Entering the Randomized Rounding Heuristic" << std::endl;
     109 
    94110  typedef struct {
    95111    int numberSolutions;
     
    101117
    102118  double start = CoinCpuTime();
    103   numCouldRun_++; // Todo: Ask JJHF what this for.
    104 
    105   OsiClpSolverInterface * clpSolver
    106     = dynamic_cast<OsiClpSolverInterface *> (model_->solver());
     119  numCouldRun_++; //
     120                  // Todo: Ask JJHF what "number of times
     121                  // the heuristic could run" means.
     122
     123  OsiSolverInterface * solver= model_->solver()->clone();
     124  OsiClpSolverInterface * clpSolver = dynamic_cast<OsiClpSolverInterface *> (solver);
    107125  assert (clpSolver);
    108126  ClpSimplex * simplex = clpSolver->getModelPtr();
    109127
    110   // Initialize the structure holding the solutions
     128  // Initialize the structure holding the solutions for the Simplex iterations
    111129  clpSolution solutions;
    112   // Set typeStruct field of ClpTrustedData struct to one.
    113   // This tells Clp it's "Mahdi!"
     130  // Set typeStruct field of ClpTrustedData struct to 1 to indicate
     131  // desired behavior for  RandRound heuristic (which is what?)
    114132  ClpTrustedData trustedSolutions;
    115133  trustedSolutions.typeStruct = 1;
     
    120138  solutions.solution=NULL;
    121139  solutions.numberUnsatisfied=NULL;
    122   simplex->setTrustedUserPointer(&trustedSolutions); // rlh: old was
    123                                               // "userPointer"
    124    // Solve from all slack to get some points
     140  simplex->setTrustedUserPointer(&trustedSolutions);
     141
     142  // Solve from all slack to get some points
    125143  simplex->allSlackBasis();
     144 
     145  // Calling primal invalidates pointers to some rim vectors,
     146  // like...row sense (!)
    126147  simplex->primal();
    127148
    128   // rlh: Mahdi's code
    129   // -------------------------------------------------
    130   // Get the problem information
    131  
    132   // - Get the number of cols and rows
     149  // 1. Okay - so a workaround would be to copy the data I want BEFORE
     150  // calling primal. 
     151  // 2. Another approach is to ask the simplex solvers NOT to mess up my
     152  // rims.
     153  // 3. See freeCachedResults() for what is getting
     154  // deleted. Everything else points into the structure.
     155  // ...or use collower and colupper rather than rowsense.
     156  // ..store address of where one of these
     157
     158  // Store the basic problem information
     159  // -Get the number of columns, rows and rhs vector
    133160  int numCols = clpSolver->getNumCols();
    134161  int numRows = clpSolver->getNumRows();
    135162
    136   // - Get the right hand side of the rows
    137   const double * rhs = clpSolver->getRightHandSide();
    138 
    139   // Find the integer variables
    140   // rlh: consider using columnType
    141   // One if not continuous (i.e., bin or gen int)
     163  // Find the integer variables (use columnType(?))
     164  // One if not continuous, that is binary or general integer)
    142165  bool * varClassInt = new bool[numCols];
    143166  for(int i=0; i<numCols; i++)
     
    154177
    155178  // -Get the objective coefficients
    156   const double *objCoefficients = clpSolver->getObjCoefficients();
    157   double *originalObjCoeff = new double [numCols];
    158   for(int i=0; i<numCols;i++)
    159     originalObjCoeff[i] = objCoefficients[i];
     179  double *originalObjCoeff = CoinCopyOfArray(clpSolver->getObjCoefficients(), numCols);
    160180
    161181  // -Get the matrix of the problem
    162   double ** matrix = new double * [numRows];
     182  // rlh: look at using sparse representation
     183 double ** matrix = new double * [numRows];
    163184  for(int i = 0; i < numRows; i++)
    164185    {
     
    200221 
    201222  // Start finding corner points by iteratively doing the following:
    202   // - find randomly tilted objective
     223  // - contruct a randomly tilted objective
    203224  // - solve
    204225  for(int i=0; i<numRows; i++)
     
    212233          // for row i and column j vary the coefficient "a bit"
    213234          if(randNum == 1)             
    214             // if the element is zero, then round the coefficient down to 0.1
     235            // if the element is zero, then set the new obj
     236            // coefficient to 0.1 (i.e., round up)
    215237            if(fabs(matrix[index[i]][j]) < 1e-6)
    216238              newObj[j] = 0.1;
    217239            else
    218               // if the element is nonzero, then increase it "a bit"
     240              // if the element is nonzero, then increase the new obj
     241              // coefficient "a bit"
    219242              newObj[j] = matrix[index[i]][j] * 1.1;
    220243          else
    221244            // if randnum is 2, then
    222             // if the element is zero, then round the coefficient down
    223             // to NEGATIVE 0.1
     245            // if the element is zero, then set the new obj coeffient
     246            // to NEGATIVE 0.1 (i.e., round down)
    224247            if(fabs(matrix[index[i]][j]) < 1e-6)
    225248              newObj[j] = -0.1;
    226249            else
    227               // if the element is nonzero, then DEcrease it "a bit"
     250              // if the element is nonzero, then DEcrease the new obj coeffienct "a bit"
    228251              newObj[j] = matrix[index[i]][j] * 0.9;
    229252        }
     
    238261
    239262      // Solve with primal simplex
    240       clpSolver->getModelPtr()->primal(1);
     263      simplex->primal(1);
     264      // rlh+ll: This was the original code. But we already have the
     265      // model pointer (it's in simplex). And, calling getModelPtr()
     266      // invalidates the cached data in the OsiClpSolverInterface
     267      // object, which means our precious rowsens is lost. So let's
     268      // not use the line below...
     269      /******* clpSolver->getModelPtr()->primal(1); */
    241270      printf("---------------------------------------------------------------- %d\n", i);
    242271    }
     
    260289 
    261290  bool feasibility = 1;
     291  // rlh: use some COIN max instead of 1e30 (?)
    262292  double bestObj = 1e30;
    263293  std::vector< std::vector <double> > feasibles;
     
    267297  int numCornerPoints = numberSolutions;
    268298
    269   rhs = clpSolver->getRightHandSide();
     299  const double * rhs = clpSolver->getRightHandSide();
     300  // rlh: row sense hasn't changed. why a fresh copy?
     301  // Delete next line.
    270302  rowSense = clpSolver->getRowSense();
    271303 
     
    279311      if(objValue < bestObj)
    280312        {
     313          // check integer feasibility
    281314          feasibility = 1;
    282315          for(int j=0; j<numCols; j++)
     
    292325                }
    293326            }
    294           if(feasibility)
     327          // check all constraints satisfied
     328          if (feasibility)
    295329              {
    296330                for(int irow = 0; irow < numRows; irow++)
     
    393427     
    394428      //SOFT ROUNDING
    395       // Look at original files for the "how to" on soft rounding 
    396      
    397      
    398       //check the feasibility of the rounded random point
     429      // Look at original files for the "how to" on soft rounding;
     430      // Soft rounding omitted here. 
     431           
     432      //Check the feasibility of the rounded random point
    399433      // -Check the feasibility
    400434      // -Get the rows sense
     
    460494    betterSolution[k] =  feasibles[numFeasibles-1][k];
    461495  } 
    462   std::cout << "See you soon! You're leaving the Randomized Rounding Heuristic" << std::endl;
     496  std::cout << "Leaving the Randomized Rounding Heuristic" << std::endl;
    463497  return 1;
    464498
Note: See TracChangeset for help on using the changeset viewer.