| 1 |
// (C) Copyright International Business Machines 2007 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Andreas Waechter IBM 2007-09-01 |
| 7 |
|
| 8 |
#include "BonGuessHeuristic.hpp" |
| 9 |
#include "CoinHelperFunctions.hpp" |
| 10 |
#include "CbcModel.hpp" |
| 11 |
#include "BonChooseVariable.hpp" |
| 12 |
|
| 13 |
//#include "OsiAuxInfo.hpp" |
| 14 |
namespace Bonmin |
| 15 |
{ |
| 16 |
/// Default constructor |
| 17 |
GuessHeuristic::GuessHeuristic(CbcModel &model) |
| 18 |
: |
| 19 |
CbcHeuristic(model) |
| 20 |
{} |
| 21 |
|
| 22 |
/// heuristic method |
| 23 |
int |
| 24 |
GuessHeuristic::solution(double &solutionValue, double *betterSolution) |
| 25 |
{ |
| 26 |
// Get pointer to pseudo costs object |
| 27 |
const BonChooseVariable* chooseMethod = dynamic_cast<BonChooseVariable*>(model_->branchingMethod()->chooseMethod()); |
| 28 |
|
| 29 |
if (!chooseMethod) { |
| 30 |
(*model_->messageHandler()) << "Can't get pseudo costs!!!\n"; |
| 31 |
solutionValue = model_->getCurrentMinimizationObjValue(); |
| 32 |
return -1; |
| 33 |
} |
| 34 |
const OsiPseudoCosts& pseudoCosts = chooseMethod->pseudoCosts(); |
| 35 |
int numberObjects = pseudoCosts.numberObjects(); |
| 36 |
assert(numberObjects == model_->numberObjects()); |
| 37 |
const double* upTotalChange = pseudoCosts.upTotalChange(); |
| 38 |
const double* downTotalChange = pseudoCosts.downTotalChange(); |
| 39 |
const int* upNumber = pseudoCosts.upNumber(); |
| 40 |
const int* downNumber = pseudoCosts.downNumber(); |
| 41 |
|
| 42 |
double sumUpTot = 0.; |
| 43 |
int numberUpTot = 0; |
| 44 |
double sumDownTot = 0.; |
| 45 |
int numberDownTot = 0; |
| 46 |
for (int i=0;i<numberObjects;i++) { |
| 47 |
sumUpTot += upTotalChange[i]; |
| 48 |
numberUpTot += upNumber[i]; |
| 49 |
sumDownTot += downTotalChange[i]; |
| 50 |
numberDownTot += downNumber[i]; |
| 51 |
} |
| 52 |
if (!numberUpTot || !numberDownTot) { |
| 53 |
// don't have ANY pseudo-costs information yet |
| 54 |
solutionValue = COIN_DBL_MAX; |
| 55 |
return -1; |
| 56 |
} |
| 57 |
double upAvrg=sumUpTot/numberUpTot; |
| 58 |
double downAvrg=sumDownTot/numberDownTot; |
| 59 |
|
| 60 |
OsiObject** object = model_->objects(); |
| 61 |
|
| 62 |
solutionValue = model_->getCurrentMinimizationObjValue(); |
| 63 |
for (int iObj = 0; iObj < numberObjects; iObj++) { |
| 64 |
//printf("%3d upest=%e uptot=%e upnum=%d downest=%e downtot=%e downnum=%d ", iObj, object[iObj]->upEstimate(), upTotalChange[iObj], upNumber[iObj], object[iObj]->downEstimate(), downTotalChange[iObj], downNumber[iObj]); |
| 65 |
|
| 66 |
double upEstimate = upNumber[iObj] ? object[iObj]->upEstimate()*upTotalChange[iObj]/upNumber[iObj] : object[iObj]->upEstimate()*upAvrg; |
| 67 |
double downEstimate = downNumber[iObj] ? object[iObj]->downEstimate()*downTotalChange[iObj]/downNumber[iObj] : object[iObj]->downEstimate()*downAvrg; |
| 68 |
//printf("up=%e down=%e\n", upEstimate, downEstimate); |
| 69 |
solutionValue += CoinMin(upEstimate,downEstimate); |
| 70 |
} |
| 71 |
//printf("solutionValue = %e\n", solutionValue); |
| 72 |
return -1; |
| 73 |
} |
| 74 |
|
| 75 |
} |