| 1 | // Copyright (C) 2000, International Business Machines |
|---|---|
| 2 | // Corporation and others. All Rights Reserved. |
| 3 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
| 4 | |
| 5 | #if defined(_MSC_VER) |
| 6 | // Turn off compiler warning about long names |
| 7 | # pragma warning(disable:4786) |
| 8 | #endif |
| 9 | |
| 10 | #include "OsiColCut.hpp" |
| 11 | #include <cstdlib> |
| 12 | #include <cstdio> |
| 13 | #include <iostream> |
| 14 | |
| 15 | //------------------------------------------------------------------- |
| 16 | // Default Constructor |
| 17 | //------------------------------------------------------------------- |
| 18 | OsiColCut::OsiColCut() : |
| 19 | OsiCut(), |
| 20 | lbs_(), |
| 21 | ubs_() |
| 22 | { |
| 23 | // nothing to do here |
| 24 | } |
| 25 | //------------------------------------------------------------------- |
| 26 | // Copy constructor |
| 27 | //------------------------------------------------------------------- |
| 28 | OsiColCut::OsiColCut(const OsiColCut & source) : |
| 29 | OsiCut(source), |
| 30 | lbs_(source.lbs_), |
| 31 | ubs_(source.ubs_) |
| 32 | { |
| 33 | // Nothing to do here |
| 34 | } |
| 35 | |
| 36 | |
| 37 | //---------------------------------------------------------------- |
| 38 | // Clone |
| 39 | //---------------------------------------------------------------- |
| 40 | OsiColCut * OsiColCut::clone() const |
| 41 | { return (new OsiColCut(*this)); } |
| 42 | |
| 43 | //------------------------------------------------------------------- |
| 44 | // Destructor |
| 45 | //------------------------------------------------------------------- |
| 46 | OsiColCut::~OsiColCut () |
| 47 | { |
| 48 | // Nothing to do here |
| 49 | } |
| 50 | |
| 51 | //---------------------------------------------------------------- |
| 52 | // Assignment operator |
| 53 | //------------------------------------------------------------------- |
| 54 | OsiColCut & |
| 55 | OsiColCut::operator=(const OsiColCut& rhs) |
| 56 | { |
| 57 | if (this != &rhs) { |
| 58 | |
| 59 | OsiCut::operator=(rhs); |
| 60 | lbs_=rhs.lbs_; |
| 61 | ubs_=rhs.ubs_; |
| 62 | } |
| 63 | return *this; |
| 64 | } |
| 65 | //---------------------------------------------------------------- |
| 66 | |
| 67 | //------------------------------------------------------------------- |
| 68 | |
| 69 | void |
| 70 | OsiColCut::print() const |
| 71 | { |
| 72 | const CoinPackedVector & cutLbs = lbs(); |
| 73 | const CoinPackedVector & cutUbs = ubs(); |
| 74 | int i; |
| 75 | std::cout<<"Column cut has " |
| 76 | <<cutLbs.getNumElements() |
| 77 | <<" lower bound cuts and " |
| 78 | <<cutUbs.getNumElements() |
| 79 | <<" upper bound cuts" |
| 80 | <<std::endl; |
| 81 | for ( i=0; i<cutLbs.getNumElements(); i++ ) { |
| 82 | int colIndx = cutLbs.getIndices()[i]; |
| 83 | double newLb= cutLbs.getElements()[i]; |
| 84 | std::cout<<"[ x"<<colIndx<< " >= "<<newLb<< "] "; |
| 85 | } |
| 86 | for ( i=0; i<cutUbs.getNumElements(); i++ ) { |
| 87 | int colIndx = cutUbs.getIndices()[i]; |
| 88 | double newUb= cutUbs.getElements()[i]; |
| 89 | std::cout<<"[ x"<<colIndx<< " <= "<<newUb<< "] "; |
| 90 | } |
| 91 | std::cout<<std::endl; |
| 92 | } |
| 93 | /* Returns infeasibility of the cut with respect to solution |
| 94 | passed in i.e. is positive if cuts off that solution. |
| 95 | solution is getNumCols() long.. |
| 96 | */ |
| 97 | double |
| 98 | OsiColCut::violated(const double * solution) const |
| 99 | { |
| 100 | const CoinPackedVector & cutLbs = lbs(); |
| 101 | const CoinPackedVector & cutUbs = ubs(); |
| 102 | double sum=0.0; |
| 103 | int i; |
| 104 | const int * column = cutLbs.getIndices(); |
| 105 | int number = cutLbs.getNumElements(); |
| 106 | const double * bound = cutLbs.getElements(); |
| 107 | for ( i=0; i<number; i++ ) { |
| 108 | int colIndx = column[i]; |
| 109 | double newLb = bound[i]; |
| 110 | if (newLb>solution[colIndx]) |
| 111 | sum += newLb - solution[colIndx]; |
| 112 | } |
| 113 | column = cutUbs.getIndices(); |
| 114 | number = cutUbs.getNumElements(); |
| 115 | bound = cutUbs.getElements(); |
| 116 | for ( i=0; i<number; i++ ) { |
| 117 | int colIndx = column[i]; |
| 118 | double newUb = bound[i]; |
| 119 | if (newUb<solution[colIndx]) |
| 120 | sum += solution[colIndx] - newUb; |
| 121 | } |
| 122 | return sum; |
| 123 | } |
| 124 |