| 1 |
// (C) Copyright International Business Machines Corporation 2007 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, International Business Machines Corporation |
| 7 |
// |
| 8 |
// Date : 10/06/2007 |
| 9 |
|
| 10 |
#include "BonQuadCut.hpp" |
| 11 |
|
| 12 |
namespace Bonmin { |
| 13 |
|
| 14 |
QuadCut::QuadCut(): |
| 15 |
OsiRowCut(), |
| 16 |
c_(0), |
| 17 |
Q_(), |
| 18 |
type_(Upper) |
| 19 |
{} |
| 20 |
|
| 21 |
QuadCut::QuadCut(const QuadCut & other): |
| 22 |
OsiRowCut(other), |
| 23 |
c_(other.c_), |
| 24 |
Q_(other.Q_), |
| 25 |
type_(other.type_){ |
| 26 |
} |
| 27 |
|
| 28 |
QuadCut & |
| 29 |
QuadCut::operator=(const QuadCut & rhs){ |
| 30 |
if(this != &rhs){ |
| 31 |
OsiRowCut::operator=(rhs); |
| 32 |
c_ = rhs.c_; |
| 33 |
Q_ = rhs.Q_; |
| 34 |
type_ = rhs.type_; |
| 35 |
} |
| 36 |
return *this; |
| 37 |
} |
| 38 |
|
| 39 |
OsiRowCut * |
| 40 |
QuadCut::clone() const { |
| 41 |
return new QuadCut(*this); |
| 42 |
} |
| 43 |
|
| 44 |
QuadCut::~QuadCut(){ |
| 45 |
} |
| 46 |
|
| 47 |
/// Compute cut violation |
| 48 |
double |
| 49 |
QuadCut::violated(const double * solution) const{ |
| 50 |
double rhs = c_; |
| 51 |
rhs += row().dotProduct(solution); |
| 52 |
const int * indice = Q_.getIndices(); |
| 53 |
const double * val = Q_.getElements(); |
| 54 |
const int * start = Q_.getVectorStarts(); |
| 55 |
const int * length = Q_.getVectorLengths(); |
| 56 |
int n = Q_.getMajorDim(); |
| 57 |
|
| 58 |
for(int i = 0 ; i < n ; i++){ |
| 59 |
int s=start[i]; |
| 60 |
int l=length[i]; |
| 61 |
for(int k = s ; k < s+l ; k++){ |
| 62 |
if(i == indice[k]) rhs += solution[i] * solution[indice[k]] * val[k]; |
| 63 |
else rhs += 2*solution[i] * solution[indice[k]] * val[k]; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#if 0 |
| 68 |
for(int i = 0 ; i < n ; i++){ |
| 69 |
for(int k = 0 ; k < length[i] ; k++){ |
| 70 |
if(i == indice[k]) rhs += solution[i] * solution[indice[k]] * val[k]; |
| 71 |
else rhs += 2*solution[i] * solution[indice[k]] * val[k]; |
| 72 |
} |
| 73 |
start += length[i]; |
| 74 |
} |
| 75 |
#endif |
| 76 |
return std::max(lb() - rhs, rhs - ub()); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
void |
| 82 |
QuadCut::print() const{ |
| 83 |
std::cout<<"Quadratic cut has lower bound "<<lb()<<" and upper bound "<<ub() |
| 84 |
<<std::endl; |
| 85 |
|
| 86 |
std::cout<<"Linear part has "<<row().getNumElements()<<" non zeroes:" |
| 87 |
<<std::endl; |
| 88 |
|
| 89 |
const int& nElem = row().getNumElements(); |
| 90 |
const int * indices = row().getIndices(); |
| 91 |
const double * elements = row().getElements(); |
| 92 |
|
| 93 |
for(int i = 0 ; i < nElem ; i++){ |
| 94 |
if(i > 0 && elements[i] > 0.) |
| 95 |
std::cout<<"+ "; |
| 96 |
std::cout<< elements[i] <<" x["<<indices[i]<<"]\t"; |
| 97 |
if(i > 0 && i % 5 == 0) std::cout<<std::endl; |
| 98 |
} |
| 99 |
std::cout<<std::endl; |
| 100 |
if(Q_.getNumElements()){ |
| 101 |
std::cout<<"Quadratic part is given by the matrix:"<<std::endl; |
| 102 |
Q_.dumpMatrix(); |
| 103 |
} |
| 104 |
} |
| 105 |
Cuts::Cuts(): |
| 106 |
OsiCuts(), |
| 107 |
quadCuts_(0){ |
| 108 |
} |
| 109 |
|
| 110 |
Cuts::Cuts(const Cuts & other): |
| 111 |
OsiCuts(other), |
| 112 |
quadCuts_(other.quadCuts_.size()){ |
| 113 |
for(unsigned int i = 0 ; i < quadCuts_.size() ; i++){ |
| 114 |
quadCuts_[i] = new QuadCut(*other.quadCuts_[i]); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
Cuts & |
| 119 |
Cuts::operator=(const Cuts & rhs){ |
| 120 |
if(this != &rhs){ |
| 121 |
OsiCuts::operator=(rhs); |
| 122 |
for(unsigned int i = 0 ; i < quadCuts_.size() ; i++) |
| 123 |
{ |
| 124 |
delete quadCuts_[i]; |
| 125 |
} |
| 126 |
quadCuts_.resize(rhs.quadCuts_.size()); |
| 127 |
for(unsigned int i = 0 ; i < quadCuts_.size() ; i++){ |
| 128 |
quadCuts_[i] = new QuadCut(*rhs.quadCuts_[i]); |
| 129 |
} |
| 130 |
} |
| 131 |
return *this; |
| 132 |
} |
| 133 |
|
| 134 |
Cuts::~Cuts(){ |
| 135 |
for(unsigned int i = 0 ; i < quadCuts_.size() ; i++) |
| 136 |
{ |
| 137 |
delete quadCuts_[i]; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
Cuts::printCuts() const { |
| 143 |
OsiCuts::printCuts(); |
| 144 |
std::cout<<quadCuts_.size()<<" quadratic cuts."<<std::endl; |
| 145 |
for(unsigned int i = 0 ; i < quadCuts_.size() ; i++){ |
| 146 |
quadCuts_[i]->print(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
} /* Ends Bonmin namespace.*/ |