| 1 |
// (C) Copyright Carnegie Mellon University 2005 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, Carnegie Mellon University, |
| 7 |
// |
| 8 |
// Date : 26/05/2005 |
| 9 |
|
| 10 |
#include "BonBoundsReader.hpp" |
| 11 |
|
| 12 |
#include <fstream> |
| 13 |
|
| 14 |
namespace Bonmin { |
| 15 |
BoundsReader::~BoundsReader() |
| 16 |
{ |
| 17 |
gutsOfDestructor(); |
| 18 |
} |
| 19 |
void BoundsReader::gutsOfDestructor() |
| 20 |
{ |
| 21 |
if(nLower_ > 0) { |
| 22 |
assert(lowerBounds_!= NULL); |
| 23 |
delete [] lowerBounds_; |
| 24 |
lowerBounds_ = NULL; |
| 25 |
assert(indexLowers_ != NULL); |
| 26 |
delete [] indexLowers_; |
| 27 |
indexLowers_ = NULL; |
| 28 |
} |
| 29 |
else { |
| 30 |
assert(lowerBounds_ == NULL); |
| 31 |
assert(indexLowers_ == NULL); |
| 32 |
} |
| 33 |
if(nUpper_ > 0) { |
| 34 |
assert(upperBounds_!= NULL); |
| 35 |
delete [] upperBounds_; |
| 36 |
upperBounds_ = NULL; |
| 37 |
assert(indexUppers_ != NULL); |
| 38 |
delete [] indexUppers_; |
| 39 |
indexUppers_ = NULL; |
| 40 |
} |
| 41 |
else { |
| 42 |
assert(upperBounds_ == NULL); |
| 43 |
assert(indexUppers_ == NULL); |
| 44 |
} |
| 45 |
nLower_=0; |
| 46 |
nUpper_=0; |
| 47 |
} |
| 48 |
|
| 49 |
void BoundsReader::read(const std::string &fileName) |
| 50 |
{ |
| 51 |
setFileName(fileName); |
| 52 |
read(); |
| 53 |
} |
| 54 |
|
| 55 |
void BoundsReader::read() |
| 56 |
{ |
| 57 |
gutsOfDestructor(); |
| 58 |
//First count the number of lower and upper bounds resp |
| 59 |
std::string lo="LO"; |
| 60 |
std::string up="UP"; |
| 61 |
std::string in; |
| 62 |
{ |
| 63 |
std::ifstream fin(fileName_.c_str()); |
| 64 |
//std::streampos begin = fin.tellg(); |
| 65 |
while(!fin.eof()) { |
| 66 |
fin>>in; |
| 67 |
if(in==lo) |
| 68 |
nLower_++; |
| 69 |
else if(in==up) |
| 70 |
nUpper_++; |
| 71 |
else |
| 72 |
throw; |
| 73 |
fin.ignore(10000,'\n'); |
| 74 |
} |
| 75 |
} |
| 76 |
if(nLower_ > 0) { |
| 77 |
lowerBounds_ = new double[nLower_]; |
| 78 |
indexLowers_ = new int[nLower_]; |
| 79 |
} |
| 80 |
if(nUpper_ > 0) { |
| 81 |
upperBounds_ = new double[nUpper_]; |
| 82 |
indexUppers_ = new int[nUpper_]; |
| 83 |
} |
| 84 |
//fin.seekg(0); |
| 85 |
// fin.seekg( 0, std::ios::beg); |
| 86 |
nLower_ = 0; |
| 87 |
nUpper_ = 0; |
| 88 |
{ |
| 89 |
std::ifstream fin2(fileName_.c_str()); |
| 90 |
//fin.close(); |
| 91 |
//fin.open(fileName_.c_str()); |
| 92 |
while(!fin2.eof()) { |
| 93 |
int index; |
| 94 |
double bound; |
| 95 |
fin2>>in>>index>>bound; |
| 96 |
if(in==lo) { |
| 97 |
lowerBounds_[nLower_] = bound; |
| 98 |
indexLowers_[nLower_++] = index; |
| 99 |
} |
| 100 |
else if(in==up) { |
| 101 |
upperBounds_[nUpper_] = bound; |
| 102 |
indexUppers_[nUpper_++] = index; |
| 103 |
} |
| 104 |
else |
| 105 |
throw; |
| 106 |
fin2.ignore(10000,'\n'); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void BoundsReader::readAndApply(OsiTMINLPInterface * solver) |
| 112 |
{ |
| 113 |
read(); |
| 114 |
for(int i = 0 ; i < nLower_ ; i++) { |
| 115 |
solver->setColLower(indexLowers_[i], lowerBounds_[i]); |
| 116 |
} |
| 117 |
for(int i = 0 ; i < nUpper_ ; i++) { |
| 118 |
solver->setColUpper(indexUppers_[i], upperBounds_[i]); |
| 119 |
} |
| 120 |
} |
| 121 |
} |