source: stable/2.8/Cbc/src/CbcHeuristicVND.cpp @ 2011

Last change on this file since 2011 was 1902, checked in by stefan, 7 years ago

sync with trunk rev 1901

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1// $Id: CbcHeuristicVND.cpp 1902 2013-04-10 16:58:16Z forrest $
2// Copyright (C) 2006, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6// edwin 12/5/09 carved out of CbcHeuristicRINS
7
8#if defined(_MSC_VER)
9// Turn off compiler warning about long names
10#  pragma warning(disable:4786)
11#endif
12#include <cassert>
13#include <cstdlib>
14#include <cmath>
15#include <cfloat>
16
17#include "OsiSolverInterface.hpp"
18#include "CbcModel.hpp"
19#include "CbcMessage.hpp"
20#include "CbcHeuristicVND.hpp"
21#include "CbcBranchActual.hpp"
22#include "CbcStrategy.hpp"
23#include "CglPreProcess.hpp"
24
25
26// Default Constructor
27CbcHeuristicVND::CbcHeuristicVND()
28        : CbcHeuristic()
29{
30    numberSolutions_ = 0;
31    numberSuccesses_ = 0;
32    numberTries_ = 0;
33    lastNode_ = -999999;
34    howOften_ = 100;
35    decayFactor_ = 0.5;
36    baseSolution_ = NULL;
37    whereFrom_ = 1 + 8 + 255 * 256;
38    stepSize_ = 0;
39    k_ = 0;
40    kmax_ = 0;
41    nDifferent_ = 0;
42}
43
44// Constructor with model - assumed before cuts
45
46CbcHeuristicVND::CbcHeuristicVND(CbcModel & model)
47        : CbcHeuristic(model)
48{
49    numberSolutions_ = 0;
50    numberSuccesses_ = 0;
51    numberTries_ = 0;
52    lastNode_ = -999999;
53    howOften_ = 100;
54    decayFactor_ = 0.5;
55    assert(model.solver());
56    int numberColumns = model.solver()->getNumCols();
57    baseSolution_ = new double [numberColumns];
58    memset(baseSolution_, 0, numberColumns*sizeof(double));
59    whereFrom_ = 1 + 8 + 255 * 256;
60    stepSize_ = 0;
61    k_ = 0;
62    kmax_ = 0;
63    nDifferent_ = 0;
64}
65
66// Destructor
67CbcHeuristicVND::~CbcHeuristicVND ()
68{
69    delete [] baseSolution_;
70}
71
72// Clone
73CbcHeuristic *
74CbcHeuristicVND::clone() const
75{
76    return new CbcHeuristicVND(*this);
77}
78
79// Assignment operator
80CbcHeuristicVND &
81CbcHeuristicVND::operator=( const CbcHeuristicVND & rhs)
82{
83    if (this != &rhs) {
84        CbcHeuristic::operator=(rhs);
85        numberSolutions_ = rhs.numberSolutions_;
86        howOften_ = rhs.howOften_;
87        numberSuccesses_ = rhs.numberSuccesses_;
88        numberTries_ = rhs.numberTries_;
89        lastNode_ = rhs.lastNode_;
90        delete [] baseSolution_;
91        if (model_ && rhs.baseSolution_) {
92            int numberColumns = model_->solver()->getNumCols();
93            baseSolution_ = new double [numberColumns];
94            memcpy(baseSolution_, rhs.baseSolution_, numberColumns*sizeof(double));
95        } else {
96            baseSolution_ = NULL;
97        }
98        stepSize_ = rhs.stepSize_;
99        k_ = rhs.k_;
100        kmax_ = rhs.kmax_;
101        nDifferent_ = rhs.nDifferent_;
102    }
103    return *this;
104}
105
106// Create C++ lines to get to current state
107void
108CbcHeuristicVND::generateCpp( FILE * fp)
109{
110    CbcHeuristicVND other;
111    fprintf(fp, "0#include \"CbcHeuristicVND.hpp\"\n");
112    fprintf(fp, "3  CbcHeuristicVND heuristicVND(*cbcModel);\n");
113    CbcHeuristic::generateCpp(fp, "heuristicVND");
114    if (howOften_ != other.howOften_)
115        fprintf(fp, "3  heuristicVND.setHowOften(%d);\n", howOften_);
116    else
117        fprintf(fp, "4  heuristicVND.setHowOften(%d);\n", howOften_);
118    fprintf(fp, "3  cbcModel->addHeuristic(&heuristicVND);\n");
119}
120
121// Copy constructor
122CbcHeuristicVND::CbcHeuristicVND(const CbcHeuristicVND & rhs)
123        :
124        CbcHeuristic(rhs),
125        numberSolutions_(rhs.numberSolutions_),
126        howOften_(rhs.howOften_),
127        numberSuccesses_(rhs.numberSuccesses_),
128        numberTries_(rhs.numberTries_),
129        lastNode_(rhs.lastNode_)
130{
131    if (model_ && rhs.baseSolution_) {
132        int numberColumns = model_->solver()->getNumCols();
133        baseSolution_ = new double [numberColumns];
134        memcpy(baseSolution_, rhs.baseSolution_, numberColumns*sizeof(double));
135    } else {
136        baseSolution_ = NULL;
137    }
138    stepSize_ = rhs.stepSize_;
139    k_ = rhs.k_;
140    kmax_ = rhs.kmax_;
141    nDifferent_ = rhs.nDifferent_;
142}
143// Resets stuff if model changes
144void
145CbcHeuristicVND::resetModel(CbcModel * /*model*/)
146{
147    //CbcHeuristic::resetModel(model);
148    delete [] baseSolution_;
149    if (model_ && baseSolution_) {
150        int numberColumns = model_->solver()->getNumCols();
151        baseSolution_ = new double [numberColumns];
152        memset(baseSolution_, 0, numberColumns*sizeof(double));
153    } else {
154        baseSolution_ = NULL;
155    }
156}
157/*
158  First tries setting a variable to better value.  If feasible then
159  tries setting others.  If not feasible then tries swaps
160  Returns 1 if solution, 0 if not */
161int
162CbcHeuristicVND::solution(double & solutionValue,
163                          double * betterSolution)
164{
165    numCouldRun_++;
166    int returnCode = 0;
167    const double * bestSolution = model_->bestSolution();
168    if (!bestSolution)
169        return 0; // No solution found yet
170    if (numberSolutions_ < model_->getSolutionCount()) {
171        // new solution - add info
172        numberSolutions_ = model_->getSolutionCount();
173
174        int numberIntegers = model_->numberIntegers();
175        const int * integerVariable = model_->integerVariable();
176
177        int i;
178        for (i = 0; i < numberIntegers; i++) {
179            int iColumn = integerVariable[i];
180            const OsiObject * object = model_->object(i);
181            // get original bounds
182            double originalLower;
183            double originalUpper;
184            getIntegerInformation( object, originalLower, originalUpper);
185            double value = bestSolution[iColumn];
186            if (value < originalLower) {
187                value = originalLower;
188            } else if (value > originalUpper) {
189                value = originalUpper;
190            }
191        }
192    }
193    int numberNodes = model_->getNodeCount();
194    if (howOften_ == 100) {
195        if (numberNodes < lastNode_ + 12)
196            return 0;
197        // Do at 50 and 100
198        if ((numberNodes > 40 && numberNodes <= 50) || (numberNodes > 90 && numberNodes < 100))
199            numberNodes = howOften_;
200    }
201    if ((numberNodes % howOften_) == 0 && (model_->getCurrentPassNumber() == 1 ||
202                                           model_->getCurrentPassNumber() == 999999)) {
203        lastNode_ = model_->getNodeCount();
204        OsiSolverInterface * solver = model_->solver();
205
206        int numberIntegers = model_->numberIntegers();
207        const int * integerVariable = model_->integerVariable();
208
209        const double * currentSolution = solver->getColSolution();
210        OsiSolverInterface * newSolver = cloneBut(3); // was model_->continuousSolver()->clone();
211        //const double * colLower = newSolver->getColLower();
212        //const double * colUpper = newSolver->getColUpper();
213
214        double primalTolerance;
215        solver->getDblParam(OsiPrimalTolerance, primalTolerance);
216
217        // Sort on distance
218        double * distance = new double [numberIntegers];
219        int * which = new int [numberIntegers];
220
221        int i;
222        int nFix = 0;
223        double tolerance = 10.0 * primalTolerance;
224        for (i = 0; i < numberIntegers; i++) {
225            int iColumn = integerVariable[i];
226            const OsiObject * object = model_->object(i);
227            // get original bounds
228            double originalLower;
229            double originalUpper;
230            getIntegerInformation( object, originalLower, originalUpper);
231            double valueInt = bestSolution[iColumn];
232            if (valueInt < originalLower) {
233                valueInt = originalLower;
234            } else if (valueInt > originalUpper) {
235                valueInt = originalUpper;
236            }
237            baseSolution_[iColumn] = currentSolution[iColumn];
238            distance[i] = fabs(currentSolution[iColumn] - valueInt);
239            which[i] = i;
240            if (fabs(currentSolution[iColumn] - valueInt) < tolerance)
241                nFix++;
242        }
243        CoinSort_2(distance, distance + numberIntegers, which);
244        nDifferent_ = numberIntegers - nFix;
245        stepSize_ = nDifferent_ / 10;
246        k_ = stepSize_;
247        //nFix = numberIntegers-stepSize_;
248        for (i = 0; i < nFix; i++) {
249            int j = which[i];
250            int iColumn = integerVariable[j];
251            const OsiObject * object = model_->object(i);
252            // get original bounds
253            double originalLower;
254            double originalUpper;
255            getIntegerInformation( object, originalLower, originalUpper);
256            double valueInt = bestSolution[iColumn];
257            if (valueInt < originalLower) {
258                valueInt = originalLower;
259            } else if (valueInt > originalUpper) {
260                valueInt = originalUpper;
261            }
262            double nearest = floor(valueInt + 0.5);
263            newSolver->setColLower(iColumn, nearest);
264            newSolver->setColUpper(iColumn, nearest);
265        }
266        delete [] distance;
267        delete [] which;
268        if (nFix > numberIntegers / 5) {
269            //printf("%d integers have samish value\n",nFix);
270            returnCode = smallBranchAndBound(newSolver, numberNodes_, betterSolution, solutionValue,
271                                             model_->getCutoff(), "CbcHeuristicVND");
272            if (returnCode < 0)
273                returnCode = 0; // returned on size
274            else
275                numRuns_++;
276            if ((returnCode&1) != 0)
277                numberSuccesses_++;
278            //printf("return code %d",returnCode);
279            if ((returnCode&2) != 0) {
280                // could add cut
281                returnCode &= ~2;
282                //printf("could add cut with %d elements (if all 0-1)\n",nFix);
283            } else {
284                //printf("\n");
285            }
286            numberTries_++;
287            if ((numberTries_ % 10) == 0 && numberSuccesses_*3 < numberTries_)
288                howOften_ += static_cast<int> (howOften_ * decayFactor_);
289        }
290
291        delete newSolver;
292    }
293    return returnCode;
294}
295// update model
296void CbcHeuristicVND::setModel(CbcModel * model)
297{
298    model_ = model;
299    // Get a copy of original matrix
300    assert(model_->solver());
301    delete [] baseSolution_;
302    int numberColumns = model->solver()->getNumCols();
303    baseSolution_ = new double [numberColumns];
304    memset(baseSolution_, 0, numberColumns*sizeof(double));
305}
306
Note: See TracBrowser for help on using the repository browser.