source: trunk/Bonmin/src/IpoptInterface/IpCbcBoundsReader.cpp @ 1

Last change on this file since 1 was 1, checked in by andreasw, 13 years ago

imported initial code

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