| 1 |
// Copyright (C) 2006, International Business Machines |
| 2 |
// Corporation and others. All Rights Reserved. |
| 3 |
|
| 4 |
#include "OsiConfig.h" |
| 5 |
|
| 6 |
#include <cstdlib> |
| 7 |
#include <cstdio> |
| 8 |
#include <cassert> |
| 9 |
#include <cmath> |
| 10 |
#include <cfloat> |
| 11 |
#include <string> |
| 12 |
#include <iostream> |
| 13 |
|
| 14 |
#include "CoinPragma.hpp" |
| 15 |
#include "CoinHelperFunctions.hpp" |
| 16 |
#include "CoinPackedVector.hpp" |
| 17 |
#include "CoinPackedMatrix.hpp" |
| 18 |
#include "CoinTime.hpp" |
| 19 |
|
| 20 |
#include "opbdp_solve.hpp" |
| 21 |
#include "PBCS.h" |
| 22 |
// Not threadsafe |
| 23 |
static unsigned int ** opbdp_solution=NULL; |
| 24 |
static int opbdp_number_solutions=0; |
| 25 |
static int opbdp_maximum_solutions=0; |
| 26 |
static int size_solution=0; |
| 27 |
static PBCS *save_pbcs=NULL; |
| 28 |
void opbdp_save_solution(OrdInt & sol) |
| 29 |
{ |
| 30 |
if (opbdp_number_solutions==opbdp_maximum_solutions) { |
| 31 |
opbdp_maximum_solutions = opbdp_maximum_solutions*2+100; |
| 32 |
unsigned int ** temp = new unsigned int * [opbdp_maximum_solutions]; |
| 33 |
CoinMemcpyN(opbdp_solution,opbdp_number_solutions,temp); |
| 34 |
CoinZeroN(temp+opbdp_number_solutions,opbdp_maximum_solutions-opbdp_number_solutions); |
| 35 |
delete [] opbdp_solution; |
| 36 |
opbdp_solution=temp; |
| 37 |
} |
| 38 |
unsigned int * array = new unsigned int [size_solution]; |
| 39 |
CoinZeroN(array,size_solution); |
| 40 |
opbdp_solution[opbdp_number_solutions++]=array; |
| 41 |
sol.first(); |
| 42 |
while(!sol.last()) { |
| 43 |
int var = sol.next(); |
| 44 |
if (var > 0) |
| 45 |
setAtOne(var-1,array,true); |
| 46 |
} |
| 47 |
OrdInt Fixed = save_pbcs->get_fixed(); |
| 48 |
Fixed.first(); |
| 49 |
while(!Fixed.last()) { |
| 50 |
int flit = Fixed.next(); |
| 51 |
if (flit > 0) |
| 52 |
setAtOne(flit-1,array,true); |
| 53 |
} |
| 54 |
// add hidden ones due to equations |
| 55 |
save_pbcs->Eq.first(); |
| 56 |
while(!save_pbcs->Eq.last()) { |
| 57 |
int l = save_pbcs->Eq.next(); |
| 58 |
int r = save_pbcs->Eq(l); |
| 59 |
if (sol.member(r) || Fixed.member(r)) |
| 60 |
setAtOne(l-1,array,true); |
| 61 |
else if (sol.member(-r) || Fixed.member(-r)) |
| 62 |
setAtOne(l-1,array,true); |
| 63 |
// just positives are added |
| 64 |
} |
| 65 |
} |
| 66 |
//------------------------------------------------------------------- |
| 67 |
// Returns the greatest common denominator of two |
| 68 |
// positive integers, a and b, found using Euclid's algorithm |
| 69 |
//------------------------------------------------------------------- |
| 70 |
static int gcd(int a, int b) |
| 71 |
{ |
| 72 |
int remainder = -1; |
| 73 |
// make sure a<=b (will always remain so) |
| 74 |
if(a > b) { |
| 75 |
// Swap a and b |
| 76 |
int temp = a; |
| 77 |
a = b; |
| 78 |
b = temp; |
| 79 |
} |
| 80 |
// if zero then gcd is nonzero (zero may occur in rhs of packed) |
| 81 |
if (!a) { |
| 82 |
if (b) { |
| 83 |
return b; |
| 84 |
} else { |
| 85 |
printf("**** gcd given two zeros!!\n"); |
| 86 |
abort(); |
| 87 |
} |
| 88 |
} |
| 89 |
while (remainder) { |
| 90 |
remainder = b % a; |
| 91 |
b = a; |
| 92 |
a = remainder; |
| 93 |
} |
| 94 |
return b; |
| 95 |
} |
| 96 |
static int solve(const OsiSolverInterface * model,PBCS & pbcs, OrdInt & sol) |
| 97 |
{ |
| 98 |
int numberColumns = model->getNumCols(); |
| 99 |
size_solution = (numberColumns+31)/32; |
| 100 |
int numberRows = model->getNumRows(); |
| 101 |
bool all01=true; |
| 102 |
int i; |
| 103 |
const double * objective = model->getObjCoefficients(); |
| 104 |
double infinity = model->getInfinity(); |
| 105 |
const CoinPackedMatrix * matrix = model->getMatrixByRow(); |
| 106 |
const double * columnLower = model->getColLower(); |
| 107 |
const double * columnUpper = model->getColUpper(); |
| 108 |
const double * rowLower = model->getRowLower(); |
| 109 |
const double * rowUpper = model->getRowUpper(); |
| 110 |
for (i = 0; i < numberColumns; i++) { |
| 111 |
if (!model->isInteger(i)||columnLower[i]<0.0||columnUpper[i]>1.0) { |
| 112 |
all01=false; |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
if (!all01) |
| 117 |
return -1; |
| 118 |
// Get scale factors to make integral |
| 119 |
const int * column = matrix->getIndices(); |
| 120 |
const int * rowLength = matrix->getVectorLengths(); |
| 121 |
const CoinBigIndex * rowStart = matrix->getVectorStarts(); |
| 122 |
const double * elementByRow = matrix->getElements(); |
| 123 |
// objective not too important |
| 124 |
double maximumObjElement = 0.0 ; |
| 125 |
for (i = 0 ; i < numberColumns ; i++) |
| 126 |
maximumObjElement = CoinMax(maximumObjElement,fabs(objective[i])) ; |
| 127 |
int objGood = 0 ; |
| 128 |
double objMultiplier = 2520.0 ; |
| 129 |
bool good=true; |
| 130 |
if (maximumObjElement) { |
| 131 |
while (10.0*objMultiplier*maximumObjElement < 1.0e9) |
| 132 |
objMultiplier *= 10.0 ; |
| 133 |
if (maximumObjElement*2520>=1.0e9) |
| 134 |
objMultiplier=1.0; |
| 135 |
double tolerance = 1.0e-9*objMultiplier; |
| 136 |
for (i = 0 ; i < numberColumns ; i++) { |
| 137 |
double value=fabs(objective[i])*objMultiplier ; |
| 138 |
if (!value) |
| 139 |
continue; |
| 140 |
int nearest = (int) floor(value+0.5) ; |
| 141 |
if (fabs(value-floor(value+0.5)) > tolerance) { |
| 142 |
// just take for now |
| 143 |
objGood = 1 ; |
| 144 |
good=false; |
| 145 |
break ; |
| 146 |
} else if (!objGood) { |
| 147 |
objGood = nearest ; |
| 148 |
} else { |
| 149 |
objGood = gcd(objGood,nearest) ; |
| 150 |
} |
| 151 |
} |
| 152 |
objMultiplier /= objGood; |
| 153 |
if (!good) { |
| 154 |
printf("Unable to scale objective correctly - maximum %g\n", |
| 155 |
maximumObjElement); |
| 156 |
objMultiplier = 1.0e7/maximumObjElement; |
| 157 |
} |
| 158 |
} else { |
| 159 |
objMultiplier=0.0; // no objective |
| 160 |
} |
| 161 |
double maximumElement=0.0; |
| 162 |
// now real stuff |
| 163 |
for (i=0;i<numberRows;i++) { |
| 164 |
if (rowLower[i]!=-infinity) |
| 165 |
maximumElement = CoinMax(maximumElement,fabs(rowLower[i])) ; |
| 166 |
if (rowUpper[i]!=infinity) |
| 167 |
maximumElement = CoinMax(maximumElement,fabs(rowUpper[i])) ; |
| 168 |
for (CoinBigIndex j=rowStart[i];j<rowStart[i]+rowLength[i];j++) |
| 169 |
maximumElement = CoinMax(maximumElement,fabs(elementByRow[j])) ; |
| 170 |
} |
| 171 |
assert (maximumElement); |
| 172 |
int elGood = 0 ; |
| 173 |
double elMultiplier = 2520.0 ; |
| 174 |
while (10.0*elMultiplier*maximumElement < 1.0e8) |
| 175 |
elMultiplier *= 10.0 ; |
| 176 |
if (maximumElement*2520>=1.0e8) |
| 177 |
elMultiplier=1.0; |
| 178 |
double tolerance = 1.0e-8*elMultiplier; |
| 179 |
good=true; |
| 180 |
for (i=0;i<numberRows;i++) { |
| 181 |
if (rowLower[i]!=-infinity) { |
| 182 |
double value=fabs(rowLower[i])*elMultiplier ; |
| 183 |
if (value) { |
| 184 |
int nearest = (int) floor(value+0.5) ; |
| 185 |
if (fabs(value-floor(value+0.5)) > tolerance) { |
| 186 |
elGood = 0 ; |
| 187 |
good=false; |
| 188 |
break ; |
| 189 |
} else if (!elGood) { |
| 190 |
elGood = nearest ; |
| 191 |
} else { |
| 192 |
elGood = gcd(elGood,nearest) ; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
if (rowUpper[i]!=infinity) { |
| 197 |
double value=fabs(rowUpper[i])*elMultiplier ; |
| 198 |
if (value) { |
| 199 |
int nearest = (int) floor(value+0.5) ; |
| 200 |
if (fabs(value-floor(value+0.5)) > tolerance) { |
| 201 |
elGood = 0 ; |
| 202 |
good=false; |
| 203 |
break ; |
| 204 |
} else if (!elGood) { |
| 205 |
elGood = nearest ; |
| 206 |
} else { |
| 207 |
elGood = gcd(elGood,nearest) ; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
for (CoinBigIndex j=rowStart[i];j<rowStart[i]+rowLength[i];j++) { |
| 212 |
double value=fabs(elementByRow[j])*elMultiplier ; |
| 213 |
if (!value) |
| 214 |
continue; |
| 215 |
int nearest = (int) floor(value+0.5) ; |
| 216 |
if (fabs(value-floor(value+0.5)) > tolerance) { |
| 217 |
elGood = 0 ; |
| 218 |
good=false; |
| 219 |
break ; |
| 220 |
} else if (!elGood) { |
| 221 |
elGood = nearest ; |
| 222 |
} else { |
| 223 |
elGood = gcd(elGood,nearest) ; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
if (!good) |
| 228 |
return -1; // no good |
| 229 |
// multiplier |
| 230 |
elMultiplier /= elGood; |
| 231 |
double objOffset=0.0; |
| 232 |
model->getDblParam(OsiObjOffset,objOffset); |
| 233 |
int doMax= (model->getObjSense()<0.0) ? 1 : 0; |
| 234 |
printf("Objective multiplier is %g, element mutiplier %g, offset %g\n", |
| 235 |
objMultiplier,elMultiplier,objOffset); |
| 236 |
|
| 237 |
Products objf; |
| 238 |
Atoms atoms; |
| 239 |
save_pbcs = & pbcs; |
| 240 |
pbcs.set_atoms(&atoms); |
| 241 |
pbcs.set_enum_heuristic(0); |
| 242 |
for (i=0;i<numberColumns;i++) { |
| 243 |
char name[9]; |
| 244 |
sprintf(name,"C%7.7d",i); |
| 245 |
SimpleString thisName(name); |
| 246 |
int literal = atoms(thisName); |
| 247 |
assert (literal==i+1); |
| 248 |
int value = (int) floor(objective[i]*objMultiplier+0.5); |
| 249 |
objf.add(value,literal); |
| 250 |
} |
| 251 |
LPBIneqSet equations; |
| 252 |
for (i=0;i<numberRows;i++) { |
| 253 |
Products row; |
| 254 |
if (rowLower[i]==-infinity||rowUpper[i]==infinity) { |
| 255 |
for (CoinBigIndex j=rowStart[i];j<rowStart[i]+rowLength[i];j++) { |
| 256 |
int value = (int) floor(elementByRow[j]*elMultiplier+0.5); |
| 257 |
row.add(value,column[j]+1); |
| 258 |
} |
| 259 |
Coefficient rhs; |
| 260 |
if (rowLower[i]==-infinity) { |
| 261 |
int value = (int) floor(rowUpper[i]*elMultiplier+0.5); |
| 262 |
rhs=value; |
| 263 |
row.set_constant(row.get_constant() - rhs); |
| 264 |
Coefficient rhs2 = -row.get_constant(); |
| 265 |
row.set_constant(0); |
| 266 |
row.mult(-1); |
| 267 |
row.set_constant(row.get_constant() + rhs2); |
| 268 |
} else { |
| 269 |
int value = (int) floor(rowLower[i]*elMultiplier+0.5); |
| 270 |
rhs=value; |
| 271 |
row.set_constant(row.get_constant() - rhs); |
| 272 |
} |
| 273 |
rhs = -row.get_constant(); |
| 274 |
row.set_constant(0); |
| 275 |
LPBIneq lpbi(row,rhs); |
| 276 |
pbcs.add(lpbi); |
| 277 |
} else { |
| 278 |
// put in both |
| 279 |
{ |
| 280 |
Products row; |
| 281 |
for (CoinBigIndex j=rowStart[i];j<rowStart[i]+rowLength[i];j++) { |
| 282 |
int value = (int) floor(elementByRow[j]*elMultiplier+0.5); |
| 283 |
row.add(value,column[j]+1); |
| 284 |
} |
| 285 |
Coefficient rhs; |
| 286 |
int value = (int) floor(rowUpper[i]*elMultiplier+0.5); |
| 287 |
rhs=value; |
| 288 |
row.set_constant(row.get_constant() - rhs); |
| 289 |
Coefficient rhs2 = -row.get_constant(); |
| 290 |
row.set_constant(0); |
| 291 |
row.mult(-1); |
| 292 |
row.set_constant(row.get_constant() + rhs2); |
| 293 |
rhs = -row.get_constant(); |
| 294 |
row.set_constant(0); |
| 295 |
LPBIneq lpbi(row,rhs); |
| 296 |
pbcs.add(lpbi); |
| 297 |
} |
| 298 |
{ |
| 299 |
Products row; |
| 300 |
for (CoinBigIndex j=rowStart[i];j<rowStart[i]+rowLength[i];j++) { |
| 301 |
int value = (int) floor(elementByRow[j]*elMultiplier+0.5); |
| 302 |
row.add(value,column[j]+1); |
| 303 |
} |
| 304 |
Coefficient rhs; |
| 305 |
int value = (int) floor(rowLower[i]*elMultiplier+0.5); |
| 306 |
rhs=value; |
| 307 |
row.set_constant(row.get_constant() - rhs); |
| 308 |
rhs = -row.get_constant(); |
| 309 |
row.set_constant(0); |
| 310 |
LPBIneq lpbi(row,rhs); |
| 311 |
pbcs.add(lpbi); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
// Add extra rows to fix |
| 316 |
int numberFixed=0; |
| 317 |
for (i=0;i<numberColumns;i++) { |
| 318 |
if (columnLower[i]) { |
| 319 |
numberFixed++; |
| 320 |
Products row; |
| 321 |
row.add(1,i+1); |
| 322 |
Coefficient rhs; |
| 323 |
row.set_constant(row.get_constant() - 1); |
| 324 |
rhs = -row.get_constant(); |
| 325 |
row.set_constant(0); |
| 326 |
LPBIneq lpbi(row,rhs); |
| 327 |
pbcs.add(lpbi); |
| 328 |
} else if (!columnUpper[i]) { |
| 329 |
numberFixed++; |
| 330 |
Products row; |
| 331 |
row.add(1,i+1); |
| 332 |
Coefficient rhs; |
| 333 |
rhs=0; |
| 334 |
row.set_constant(row.get_constant() - rhs); |
| 335 |
Coefficient rhs2 = -row.get_constant(); |
| 336 |
row.set_constant(0); |
| 337 |
row.mult(-1); |
| 338 |
row.set_constant(row.get_constant() + rhs2); |
| 339 |
rhs = -row.get_constant(); |
| 340 |
row.set_constant(0); |
| 341 |
LPBIneq lpbi(row,rhs); |
| 342 |
pbcs.add(lpbi); |
| 343 |
} |
| 344 |
} |
| 345 |
Coefficient value; |
| 346 |
int consistent=0; |
| 347 |
pbcs.Enum.stat_reset(); |
| 348 |
// FM preprocessing |
| 349 |
if (numberFixed<numberColumns) { |
| 350 |
OrdInt fixed_lits = pbcs.fixing(); |
| 351 |
if (!fixed_lits.empty()) |
| 352 |
{ |
| 353 |
int lit; |
| 354 |
std::cout << "Fixing : "; |
| 355 |
fixed_lits.first(); |
| 356 |
while(!fixed_lits.last()) |
| 357 |
{ |
| 358 |
objf.fix(lit = fixed_lits.next()); |
| 359 |
} |
| 360 |
if (pbcs.is_unsatisfiable()) |
| 361 |
std::cout << "\n'fixing' detected inconsistency"<<std::endl; |
| 362 |
else if (fixed_lits.length() == 1) |
| 363 |
std::cout << "\n1 Literal has been fixed by 'fixing' "<<std::endl; |
| 364 |
else |
| 365 |
std::cout << "\n" << fixed_lits.length() << " Literals have been fixed by 'fixing' "<<std::endl; |
| 366 |
} |
| 367 |
} |
| 368 |
if (numberFixed<numberColumns) { |
| 369 |
int changed = pbcs.coefficient_reduction_bigM(); |
| 370 |
if (changed > 1) |
| 371 |
std::cout << "Big M coefficient reduction changed " << changed << " coefficients"<<std::endl; |
| 372 |
else if (changed == 1) |
| 373 |
std::cout << "Big M coefficient reduction changed " << changed << " coefficient"<<std::endl; |
| 374 |
std::cout.flush(); |
| 375 |
} |
| 376 |
pbcs.Enum.stat_reset(); |
| 377 |
if (pbcs.is_unsatisfiable()) |
| 378 |
{ |
| 379 |
value = 0; |
| 380 |
consistent = 0; |
| 381 |
} |
| 382 |
else if (pbcs.is_empty()) |
| 383 |
{ |
| 384 |
value = doMax ? objf.get_upperbound() : objf.get_lowerbound(); |
| 385 |
consistent = 1; |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
//////////////////////////////// |
| 390 |
value = pbcs.Enum.optimize(objf,consistent,sol,doMax,0); |
| 391 |
//////////////////////////////// |
| 392 |
} |
| 393 |
if (consistent == 0) |
| 394 |
return 0; |
| 395 |
else |
| 396 |
return 1; |
| 397 |
} |
| 398 |
/* Solve pure 0-1 with integral coefficients etc (or can be made by scaling) using opdbp |
| 399 |
The solution will be set in model |
| 400 |
If no solution then returns 0, if not suitable returns -1 |
| 401 |
*/ |
| 402 |
int |
| 403 |
solveOpbdp(OsiSolverInterface * model) |
| 404 |
{ |
| 405 |
PBCS pbcs; |
| 406 |
OrdInt sol; |
| 407 |
pbcs.set_enum_verbosity(3); |
| 408 |
pbcs.set_verbosity(3); |
| 409 |
double time1 = CoinCpuTime(); |
| 410 |
int numberFound=solve(model,pbcs,sol); |
| 411 |
if (numberFound>0) { |
| 412 |
const double * objective = model->getObjCoefficients(); |
| 413 |
double objOffset=0.0; |
| 414 |
model->getDblParam(OsiObjOffset,objOffset); |
| 415 |
int numberColumns = model->getNumCols(); |
| 416 |
double * solution = new double [numberColumns]; |
| 417 |
CoinZeroN(solution,numberColumns); |
| 418 |
sol.first(); |
| 419 |
while(!sol.last()) { |
| 420 |
int var = sol.next(); |
| 421 |
if (var > 0) |
| 422 |
solution[var-1]=1.0; |
| 423 |
} |
| 424 |
OrdInt Fixed = pbcs.get_fixed(); |
| 425 |
Fixed.first(); |
| 426 |
while(!Fixed.last()) { |
| 427 |
int flit = Fixed.next(); |
| 428 |
if (flit > 0) |
| 429 |
solution[flit-1]=1.0; |
| 430 |
} |
| 431 |
// add hidden ones due to equations |
| 432 |
pbcs.Eq.first(); |
| 433 |
while(!pbcs.Eq.last()) { |
| 434 |
int l = pbcs.Eq.next(); |
| 435 |
int r = pbcs.Eq(l); |
| 436 |
if (sol.member(r) || Fixed.member(r)) |
| 437 |
solution[l-1]=1.0; |
| 438 |
else if (sol.member(-r) || Fixed.member(-r)) |
| 439 |
solution[l-1]=1.0; |
| 440 |
// just positives are added |
| 441 |
} |
| 442 |
numberFound=1; |
| 443 |
double objValue = - objOffset; |
| 444 |
for (int i=0;i<numberColumns;i++) |
| 445 |
objValue += solution[i]*objective[i]; |
| 446 |
std::cout<<"Global "<<(model->getObjSense()>0 ? "minimum" : "maximum")<<" of "<<objValue |
| 447 |
<<" took "<< CoinCpuTime()-time1<<" seconds"<<std::endl; |
| 448 |
std::cout << "Explored Nodes: " << pbcs.Enum.stat_nodes(); |
| 449 |
std::cout << " Max. Depth: " << pbcs.Enum.stat_maxdepth(); |
| 450 |
std::cout << " Fixed Literals: " << pbcs.Enum.stat_fixedlits() << ""<<std::endl; |
| 451 |
//model->setObjValue(objValue); |
| 452 |
model->setColSolution(solution); |
| 453 |
delete [] solution; |
| 454 |
} else if (!numberFound) { |
| 455 |
std::cout << "Constraint Set is unsatisfiable"<<std::endl; |
| 456 |
} else { |
| 457 |
std::cout<<"Problem in incorrect format"<<std::endl; |
| 458 |
} |
| 459 |
return numberFound; |
| 460 |
} |
| 461 |
/* Find all solutions of a pure 0-1 with integral coefficients etc (or can be made by scaling) using opdbp |
| 462 |
Returns an array of bit solution vectors. |
| 463 |
i is 1 if bit set (see below) |
| 464 |
If no solution then numberFound will be 0, if not suitable -1 |
| 465 |
*/ |
| 466 |
unsigned int ** |
| 467 |
solveOpbdp(const OsiSolverInterface * model,int & numberFound) |
| 468 |
{ |
| 469 |
PBCS pbcs; |
| 470 |
OrdInt sol; |
| 471 |
pbcs.set_enum_verbosity(-1); |
| 472 |
pbcs.set_verbosity(-1); |
| 473 |
numberFound=solve(model,pbcs,sol); |
| 474 |
if (numberFound>0||opbdp_number_solutions) { |
| 475 |
// all solutions |
| 476 |
numberFound = opbdp_number_solutions; |
| 477 |
return opbdp_solution; |
| 478 |
} else { |
| 479 |
// no solution |
| 480 |
return NULL; |
| 481 |
} |
| 482 |
} |