| 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 "BonQuadRow.hpp" |
| 11 |
#include <cfloat> |
| 12 |
//#define DEBUG |
| 13 |
namespace Bonmin{ |
| 14 |
|
| 15 |
QuadRow::QuadRow(): |
| 16 |
c_(0), |
| 17 |
a_(), |
| 18 |
Q_(), |
| 19 |
grad_evaled_(false) |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
QuadRow::QuadRow(const QuadRow &other): |
| 24 |
c_(other.c_), |
| 25 |
a_(other.a_), |
| 26 |
Q_(other.Q_), |
| 27 |
g_(), |
| 28 |
a_grad_idx_(), |
| 29 |
Q_row_grad_idx_(), |
| 30 |
Q_col_grad_idx_(), |
| 31 |
Q_hessian_idx_(), |
| 32 |
grad_evaled_(false) |
| 33 |
{ |
| 34 |
initialize(); |
| 35 |
} |
| 36 |
|
| 37 |
QuadRow & QuadRow::operator=(const QuadRow &rhs){ |
| 38 |
if(this != &rhs){ |
| 39 |
c_ = rhs.c_; |
| 40 |
a_ = rhs.a_; |
| 41 |
Q_ = rhs.Q_; |
| 42 |
Q_hessian_idx_.clear(); |
| 43 |
g_.clear(); |
| 44 |
a_grad_idx_.clear(); |
| 45 |
Q_row_grad_idx_.clear(); |
| 46 |
Q_col_grad_idx_.clear(); |
| 47 |
initialize(); |
| 48 |
//H_Hes_idx_ = rhs.H_Hes_idx_; |
| 49 |
grad_evaled_ = false; |
| 50 |
} |
| 51 |
return (*this); |
| 52 |
} |
| 53 |
|
| 54 |
QuadRow::QuadRow(const QuadCut &cut): |
| 55 |
c_(0), |
| 56 |
a_(cut.row()), |
| 57 |
Q_(cut.Q(), cut.type()) |
| 58 |
{ |
| 59 |
initialize(); |
| 60 |
} |
| 61 |
|
| 62 |
QuadRow& |
| 63 |
QuadRow::operator=(const QuadCut &cut){ |
| 64 |
c_ = cut.c(); |
| 65 |
a_ = cut.row(); |
| 66 |
Q_ = cut.Q(); |
| 67 |
Q_.make_upper_triangular(cut.type()); |
| 68 |
g_.clear(); |
| 69 |
a_grad_idx_.clear(); |
| 70 |
Q_row_grad_idx_.clear(); |
| 71 |
Q_col_grad_idx_.clear(); |
| 72 |
//Q_hessian_idx.clear(); |
| 73 |
//H_Hes_idx_.clear() |
| 74 |
initialize(); |
| 75 |
return (*this); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
QuadRow::QuadRow(const OsiRowCut &cut): |
| 80 |
c_(0), |
| 81 |
a_(cut.row()), |
| 82 |
Q_() |
| 83 |
{ |
| 84 |
initialize(); |
| 85 |
} |
| 86 |
|
| 87 |
QuadRow& |
| 88 |
QuadRow::operator=(const OsiRowCut &cut){ |
| 89 |
c_ = 0; |
| 90 |
a_ = cut.row(); |
| 91 |
Q_ = TMat(); |
| 92 |
g_.clear(); |
| 93 |
a_grad_idx_.clear(); |
| 94 |
Q_row_grad_idx_.clear(); |
| 95 |
Q_col_grad_idx_.clear(); |
| 96 |
//Q_hessian_idx.clear(); |
| 97 |
//H_Hes_idx_.clear() |
| 98 |
initialize(); |
| 99 |
return (*this); |
| 100 |
} |
| 101 |
|
| 102 |
void |
| 103 |
QuadRow::initialize(){ |
| 104 |
//Check that Q_ is upper triangular |
| 105 |
for(int i = 0 ; i < Q_.nnz_ ; i++){ |
| 106 |
assert(Q_.jCol_[i] >= Q_.iRow_[i]);} |
| 107 |
grad_evaled_ = false; |
| 108 |
|
| 109 |
int n; |
| 110 |
// Construct a map to store the non-zero elements of the gradient. |
| 111 |
n = a_.getNumElements(); |
| 112 |
a_grad_idx_.reserve(n); |
| 113 |
|
| 114 |
// Put the linear elements |
| 115 |
const int * indices = a_.getIndices(); |
| 116 |
const double * elems = a_.getElements(); |
| 117 |
|
| 118 |
for(int i = 0 ; i < n ; i++){ |
| 119 |
std::pair<gStore::iterator, bool> res = g_.insert(std::make_pair(indices[i], std::make_pair(elems[i],0.))); |
| 120 |
a_grad_idx_.push_back(res.first); |
| 121 |
} |
| 122 |
// Put the quadratics first rows |
| 123 |
n = Q_.numNonEmptyRows(); |
| 124 |
const TMat::RowS& nonEmptyRows = Q_.nonEmptyRows(); |
| 125 |
Q_row_grad_idx_.reserve(n); |
| 126 |
|
| 127 |
for(TMat::RowS::const_iterator i = nonEmptyRows.begin() ; i != nonEmptyRows.end() ; i++){ |
| 128 |
std::pair<gStore::iterator, bool> res = g_.insert(std::make_pair(i->first, std::make_pair(0.,0.))); |
| 129 |
Q_row_grad_idx_.push_back(res.first); |
| 130 |
} |
| 131 |
|
| 132 |
//Now columns |
| 133 |
n = Q_.numNonEmptyCols(); |
| 134 |
const TMat::RowS& nonEmptyCols = Q_.nonEmptyCols(); |
| 135 |
Q_col_grad_idx_.reserve(n); |
| 136 |
|
| 137 |
for(TMat::RowS::const_iterator i = nonEmptyCols.begin() ; i != nonEmptyCols.end() ; i++){ |
| 138 |
std::pair<gStore::iterator, bool> res = g_.insert(std::make_pair(i->first, std::make_pair(0.,0.))); |
| 139 |
Q_col_grad_idx_.push_back(res.first); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
/** Print quadratic constraint.*/ |
| 145 |
void |
| 146 |
QuadRow::print(){ |
| 147 |
std::cout<<"constant term "<<c_<<std::endl; |
| 148 |
int * a_ind = a_.getIndices(); |
| 149 |
const double * a_el = a_.getElements(); |
| 150 |
int n = a_.getNumElements(); |
| 151 |
std::cout<<"Linear term (size "<<n<<"): "; |
| 152 |
for(int i = 0 ; i < n ; i++) |
| 153 |
{ |
| 154 |
std::cout<<a_el[i]<<" * x["<<a_ind[i]<<"]\t"; |
| 155 |
if(i && ( (i % 5) == 0 ) ) std::cout<<std::endl<<"\t\t"; |
| 156 |
} |
| 157 |
} |
| 158 |
/** Evaluate quadratic form.*/ |
| 159 |
double |
| 160 |
QuadRow::eval_f(const double *x, bool new_x){ |
| 161 |
//if(new_x){ |
| 162 |
internal_eval_grad(x);//} |
| 163 |
double value = c_;// Constant |
| 164 |
|
| 165 |
//Linear part |
| 166 |
int * a_ind = a_.getIndices(); |
| 167 |
const double * a_el = a_.getElements(); |
| 168 |
int n = a_.getNumElements(); |
| 169 |
for(int i = 0 ; i < n ; i++) |
| 170 |
{ |
| 171 |
value += a_el[i] * x[a_ind[i]]; |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
//Quadratic part |
| 176 |
for(gStore::iterator i = g_.begin() ; i != g_.end() ; i++){ |
| 177 |
value += i->second.second * x[i->first]; |
| 178 |
} |
| 179 |
return value; |
| 180 |
} |
| 181 |
|
| 182 |
/** Get number of non-zeroes in the gradiant.*/ |
| 183 |
int |
| 184 |
QuadRow::nnz_grad(){ |
| 185 |
return static_cast<int>(g_.size());} |
| 186 |
/** Get structure of gradiant */ |
| 187 |
void |
| 188 |
QuadRow::gradiant_struct(const int nnz, int * indices, bool offset){ |
| 189 |
int n = 0; |
| 190 |
for(gStore::iterator i = g_.begin() ; i != g_.end() ; i++){ |
| 191 |
indices[n++] = i->first + offset; |
| 192 |
} |
| 193 |
assert(n == nnz); |
| 194 |
assert(nnz == (int) g_.size()); |
| 195 |
} |
| 196 |
|
| 197 |
/** Evaluate gradiant of quadratic form.*/ |
| 198 |
void |
| 199 |
QuadRow::eval_grad(const int nnz, const double * x, bool new_x, double * values){ |
| 200 |
|
| 201 |
#ifdef DEBUG |
| 202 |
// Output relevant components of x |
| 203 |
for(gStore::iterator i = g_.begin() ; i != g_.end() ; i++){ |
| 204 |
printf("x[%i] = %g, ",i->first, x[i->first]); |
| 205 |
} |
| 206 |
#endif |
| 207 |
//if(new_x){ |
| 208 |
internal_eval_grad(x);//} |
| 209 |
int n = 0; |
| 210 |
#ifdef DEBUG |
| 211 |
std::cout<<"Computing gradient"<<std::endl; |
| 212 |
#endif |
| 213 |
for(gStore::iterator i = g_.begin() ; i != g_.end() ; i++){ |
| 214 |
#ifdef DEBUG |
| 215 |
printf("%i: %g, %g\n", i->first, i->second.second, i->second.first); |
| 216 |
#endif |
| 217 |
values[n++] = 2*i->second.second + i->second.first; |
| 218 |
} |
| 219 |
assert (nnz == (int) g_.size()); |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
QuadRow::internal_eval_grad(const double *x){ |
| 224 |
// Zero out gradiant storage |
| 225 |
for(gStore::iterator i = g_.begin() ; i != g_.end() ; i++){ |
| 226 |
i->second.second = 0; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
const TMat::RowS & nonEmptyRows = Q_.nonEmptyRows(); |
| 231 |
int k = 0;//Iterates on Q_grad_idx_; |
| 232 |
for(TMat::RowS::const_iterator ii = nonEmptyRows.begin() ; ii != nonEmptyRows.end(); |
| 233 |
ii++, k++){ |
| 234 |
double value = 0; |
| 235 |
assert(ii->first == Q_.iRow_[Q_.rowOrdering_[ii->second]]); |
| 236 |
for(int i = ii->second ; i < Q_.nnz_ && ii->first == Q_.iRow_[Q_.rowOrdering_[i]] ; i++) |
| 237 |
{ |
| 238 |
value += x[Q_.jCol_[Q_.rowOrdering_[i]]] * Q_.value_[Q_.rowOrdering_[i]]; |
| 239 |
} |
| 240 |
Q_row_grad_idx_[k]->second.second += value; |
| 241 |
assert(Q_row_grad_idx_[k]->first == ii->first); |
| 242 |
} |
| 243 |
const TMat::RowS & nonEmptyCols = Q_.nonEmptyCols(); |
| 244 |
k = 0;//Iterates on Q_grad_idx_; |
| 245 |
for(TMat::RowS::const_iterator ii = nonEmptyCols.begin() ; ii != nonEmptyCols.end(); |
| 246 |
ii++, k++){ |
| 247 |
double value = 0; |
| 248 |
assert(ii->first == Q_.jCol_[Q_.columnOrdering_[ii->second]]); |
| 249 |
for(int i = ii->second ; i < Q_.nnz_ && ii->first == Q_.jCol_[Q_.columnOrdering_[i]] ; i++) |
| 250 |
{ |
| 251 |
if(Q_.iRow_[Q_.columnOrdering_[i]] != Q_.jCol_[Q_.columnOrdering_[i]]) |
| 252 |
value += x[Q_.iRow_[Q_.columnOrdering_[i]]] * Q_.value_[Q_.columnOrdering_[i]]; |
| 253 |
} |
| 254 |
Q_col_grad_idx_[k]->second.second += value; |
| 255 |
assert(Q_col_grad_idx_[k]->first == ii->first); |
| 256 |
} |
| 257 |
|
| 258 |
grad_evaled_ = true; |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
QuadRow::add_to_hessian(AdjustableMat &H, bool offset){ |
| 263 |
assert(Q_hessian_idx_.empty()); |
| 264 |
for(int i = 0 ; i < Q_.nnz_ ; i++){ |
| 265 |
std::pair<int, int> e; |
| 266 |
e = std::make_pair(Q_.jCol_[i] + (offset ? 1 : 0), Q_.iRow_[i] + (offset ? 1 : 0)); |
| 267 |
AdjustableMat::iterator pos = H.find(e); |
| 268 |
if(pos != H.end()){//Already exists |
| 269 |
if(pos->second.second != -1) |
| 270 |
pos->second.second++; |
| 271 |
Q_hessian_idx_.push_back(pos); |
| 272 |
} |
| 273 |
else { |
| 274 |
std::pair<AdjustableMat::iterator, bool> res = |
| 275 |
H.insert(std::make_pair(e, std::make_pair((int)H.size(), 1))); |
| 276 |
assert(res.second == true); |
| 277 |
Q_hessian_idx_.push_back(res.first); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
void |
| 283 |
QuadRow::remove_from_hessian(AdjustableMat &H){ |
| 284 |
for(int i = 0 ; i < Q_.nnz_ ; i++){ |
| 285 |
if(Q_hessian_idx_[i]->second.second != -1) |
| 286 |
Q_hessian_idx_[i]->second.second--; |
| 287 |
if(Q_hessian_idx_[i]->second.second == 0){ |
| 288 |
H.erase(Q_hessian_idx_[i]); |
| 289 |
} |
| 290 |
} |
| 291 |
Q_hessian_idx_.clear(); |
| 292 |
} |
| 293 |
|
| 294 |
/** Return hessian values (i.e. Q_) in values.*/ |
| 295 |
void |
| 296 |
QuadRow::eval_hessian(double lambda, double * values){ |
| 297 |
for(int i = 0 ; i < Q_.nnz_ ; i++){ |
| 298 |
#ifdef DEBUG |
| 299 |
printf("iRow %i, jCol %i, value %g , nnz %i\n", |
| 300 |
Q_hessian_idx_[i]->first.second, |
| 301 |
Q_hessian_idx_[i]->first.first, |
| 302 |
Q_.value_[i], |
| 303 |
Q_hessian_idx_[i]->second.first); |
| 304 |
#endif |
| 305 |
values[Q_hessian_idx_[i]->second.first] += (lambda * 2 * Q_.value_[i]); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
}// Ends Bonmin namespace |
| 310 |
|