OSmps2OS.cpp
Go to the documentation of this file.
1 /* $Id: OSmps2OS.cpp 4692 2013-10-16 13:56:17Z Gassmann $ */
16 #include "OSmps2OS.h"
17 #include "OSOutput.h"
18 #include "OSErrorClass.h"
19 #include <iostream>
20 #include <sstream>
21 
22 using std::endl;
23 
24 OSmps2OS::OSmps2OS( std::string mpsfilename)
25 {
26  m_MpsData = new CoinMpsIO();
27  m_nOfSOS = 0;
28  m_SOS = NULL;
29  m_quadColumnStart = NULL;
30  m_quadColumnIdx = NULL;
31  m_quadElements = NULL;
32  m_coneStart = NULL;
33  m_coneIdx = NULL;
34  m_coneType = NULL;
35 
36  int status = m_MpsData->readMps( &mpsfilename[ 0], "", m_nOfSOS, m_SOS );
37  if (status != 0)
38  throw ErrorClass("Error trying to read MPS file");
39 
40  m_CoinPackedMatrix = new CoinPackedMatrix( *(m_MpsData->getMatrixByCol()));
41 
42  if (m_nOfSOS > 0)
43  {
44 #ifndef NDEBUG
45  std::ostringstream outStr;
46  {
47  outStr << "Detected " << m_nOfSOS << " special ordered sets" << std::endl;
48  for (int i=0; i < m_nOfSOS; i++)
49  {
50  int numberEntries = m_SOS[i]->numberEntries();
51  const int * which = m_SOS[i]->which();
52  const double * weights = m_SOS[i]->weights();
53  outStr << "SOS " << i << " has type " << m_SOS[i]->setType();
54  outStr << " and " << numberEntries << " entries:" << std::endl;
55  for (int j=0;j<numberEntries;j++)
56  outStr << " Idx: " << which[j] << " Weight: " << weights[j] << std::endl;
57  }
59  }
60 #endif
61 // throw ErrorClass("SOS has not been implemented yet");
62  }
63 
64  while (m_MpsData->reader()->whichSection ( ) != COIN_ENDATA_SECTION )
65  {
66  switch (m_MpsData->reader()->whichSection ( ))
67  {
68  case COIN_QUAD_SECTION:
69  status = m_MpsData->readQuadraticMps(NULL, m_quadColumnStart, m_quadColumnIdx, m_quadElements, 0);
70 
71  if (status != 0)
72  {
73  if (status != -2 && status != -3)
74  throw ErrorClass("Error trying to read QUADOBJ section");
75  }
76  else
77  {
78 #ifndef NDEBUG
79  std::ostringstream outStr;
80  int numberColumns=m_MpsData->getNumCols();
81  outStr << "Quadratic objective has " << m_quadColumnStart[numberColumns] << " entries" << std::endl;
82  outStr << "Column starts:" << std::endl;
83  for (int i=0; i<=numberColumns; i++)
84  outStr << " " << m_quadColumnStart[i] << std::endl;
85  for (int i=0; i<numberColumns; i++)
86  {
88  {
89  outStr << "Column " << i << ": index value" << std::endl;
90  for (int j=m_quadColumnStart[i];j<m_quadColumnStart[i+1];j++)
91  outStr << " " << m_quadColumnIdx[j] << " " << m_quadElements[j] << std::endl;
92  }
93  }
95 #endif
96 // throw ErrorClass("QUADOBJ has not been implemented yet");
97  }
98  break;
99 
100 
101  case COIN_CONIC_SECTION:
102  m_nOfCones;
103  status = m_MpsData->readConicMps(NULL, m_coneStart, m_coneIdx, m_coneType, m_nOfCones);
104  if (status != 0)
105  {
106  if (status != -2 && status != -3)
107  throw ErrorClass("Error trying to read cone section");
108  }
109  else
110  {
111 #ifndef NDEBUG
112  std::ostringstream outStr;
113  outStr << "Conic section has " << m_nOfCones << " cones" << std::endl;
114  for (int i=0;i<m_nOfCones;i++)
115  {
116  outStr << "Cone " << i << " has " << m_coneStart[i+1]-m_coneStart[i] << " entries ";
117  outStr << "(type " << m_coneType[i] << "):" << std::endl;
118  for (int j=m_coneStart[i];j<m_coneStart[i+1];j++)
119  outStr << " " << m_coneIdx[j] << std::endl;
120  }
122 #endif
123 // throw ErrorClass("CSECTION has not been implemented yet");
124  }
125 //#endif
126  break;
127 
128 
129  case COIN_BASIS_SECTION:
130 #ifndef NDEBUG
131  {
132  std::ostringstream outStr;
133  outStr << "Basis section has not been implemented yet" << std::endl;
135  }
136 #endif
137  break;
138 
139  default:
140  throw ErrorClass("encountered unknown section in MPS file");
141  }
142  }
143 }// end constructor
144 
146 {
147 #ifndef NDEBUG
148  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, "now delete m_MpsData\n");
149 #endif
150  delete m_MpsData;
151  m_MpsData = NULL;
152  delete m_CoinPackedMatrix;
153  m_CoinPackedMatrix = NULL;
158  delete osinstance;
159  osinstance = NULL;
160 
161  if (m_SOS != NULL)
162  delete m_SOS;
163  m_SOS = NULL;
164 
165  if (m_quadColumnStart != NULL)
166  delete m_quadColumnStart;
167  m_quadColumnStart = NULL;
168 
169  if (m_quadColumnIdx != NULL)
170  delete m_quadColumnIdx;
171  m_quadColumnIdx = NULL;
172 
173  if (m_quadElements != NULL)
174  delete m_quadElements;
175  m_quadElements = NULL;
176 
177  if (m_coneStart != NULL)
178  delete m_coneStart;
179  m_coneStart = NULL;
180 
181  if (m_coneIdx != NULL)
182  delete m_coneIdx;
183  m_coneIdx = NULL;
184 
185  if (m_coneType != NULL)
186  delete m_coneType;
187  m_coneType = NULL;
188 
189 } // end destructor
190 
191 // Kipp-- put in integer programming
193 {
194  int i;
195  int numvar = m_MpsData->getNumCols();
196  int numrows = m_MpsData->getNumRows();
197  int numnonz = m_MpsData->getNumElements();
198  int numberObj = 1;
199  int objIndex = -1;
200  std::ostringstream outStr;
201 
202  osinstance = new OSInstance();
203 
204  osinstance->setInstanceName( const_cast<char*>(m_MpsData->getProblemName()));
205  //
206  // get the variable information
207  //
208  osinstance->setVariableNumber( numvar);
209 
210  for(i = 0; i < numvar; i++)
211  {
212  char vartype = 'C';
213  if (m_MpsData->isInteger( i) )
214  {
215  if (m_MpsData->getColLower()[ i] == 0 && m_MpsData->getColUpper()[ i] == 1)
216  vartype = 'B';
217  else
218  vartype = 'I';
219  }
220  osinstance->addVariable(i, m_MpsData->columnName( i),
221  m_MpsData->getColLower()[ i], m_MpsData->getColUpper()[ i],
222  vartype);
223  }
224  //
225  // now create the objective function
226  //
227  double objWeight = 1.0;
228  SparseVector* objectiveCoefficients = NULL;
229  objectiveCoefficients = new SparseVector( numvar);
230  double *p = const_cast<double*>(m_MpsData->getObjCoefficients());
231  for(i = 0; i < numvar; i++)
232  {
233  objectiveCoefficients->indexes[i] = i;
234  objectiveCoefficients->values[i] = *(p++);
235  }
236  // We will go with the
237  // majority on this one and assume minimization by default
238  osinstance->setObjectiveNumber( numberObj) ;
239  osinstance->addObjective(objIndex, m_MpsData->getObjectiveName(),
240  "min", m_MpsData->objectiveOffset(), objWeight, objectiveCoefficients) ;
241  delete objectiveCoefficients;
242  objectiveCoefficients = NULL;
243  //
244  // now fill in row information
245  //
246  osinstance->setConstraintNumber( numrows);
247  double constant = 0.0;
248  for(i = 0; i < numrows; i++)
249  {
250  osinstance->addConstraint(i, m_MpsData->rowName( i), m_MpsData->getRowLower()[i],
251  m_MpsData->getRowUpper()[i], constant);
252  }
253  //
254  // now fill in the lp arrays
255  //
256  int valuesBegin = 0;
257  int valuesEnd = numnonz - 1;
258  int startsBegin = 0;
259  int indexesBegin = 0;
260  int indexesEnd = numnonz - 1;
261  int startsEnd = m_CoinPackedMatrix->isColOrdered()?numvar:numrows;
263  const_cast<double*>(m_CoinPackedMatrix->getElements()), valuesBegin, valuesEnd,
264  const_cast<int*>(m_CoinPackedMatrix->getIndices()), indexesBegin, indexesEnd,
265  const_cast<int*>(m_CoinPackedMatrix->getVectorStarts()), startsBegin, startsEnd);
266 
267  // if there is a quadratic section, store it
268  if (m_quadColumnStart != NULL)
269  {
270  int numberColumns=m_MpsData->getNumCols();
271  int numberElements=m_quadColumnStart[numberColumns];
272  if (numberElements > 0)
273  {
274  int *rowindexes = new int[numberElements];
275  for (int i=0; i<numberElements;i++)
276  rowindexes[i] = -1;
277  int *colindexes = new int[numberElements];
278  for (int i=0; i<numberColumns; i++)
279  for (int j=m_quadColumnStart[i];j<m_quadColumnStart[i+1];j++)
280  colindexes[j] = i;
281  osinstance->setQuadraticCoefficients(numberElements, rowindexes, colindexes,
282  m_quadColumnIdx, m_quadElements, 0, numberElements-1);
283  }
284  }
285  // store any cone information (TODO)
286  // store SOS -- if there are nonstandard weights, put into an OSOption object (TODO)
287  // if there is basis information, it needs to be stored in an OSOption object (TODO)
288  return true;
289 }
290 
int * m_quadColumnStart
Data structures to hold the quadratic objective.
Definition: OSmps2OS.h:101
double * m_quadElements
Definition: OSmps2OS.h:103
bool setQuadraticCoefficients(int number, int *rowIndexes, int *varOneIndexes, int *varTwoIndexes, double *coefficients, int begin, int end)
set quadratic coefficients into the QuadraticCoefficients-&gt;qTerm data structure
bool addVariable(int index, std::string name, double lowerBound, double upperBound, char type)
add a variable.
const OSSmartPtr< OSOutput > osoutput
Definition: OSOutput.cpp:39
IntVector * rowIdx
a pointer of row indices if the problem is stored by column
Definition: OSInstance.h:309
bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, double *values, int valuesBegin, int valuesEnd, int *indexes, int indexesBegin, int indexesEnd, int *starts, int startsBegin, int startsEnd)
set linear constraint coefficients
IntVector * colIdx
a pointer of column indices if the problem is stored by row
Definition: OSInstance.h:312
int * m_coneType
Definition: OSmps2OS.h:109
OSInstance * osinstance
osinstance is a pointer to the OSInstance object that gets created from the instance represented in M...
Definition: OSmps2OS.h:65
int m_nOfSOS
Data structures to hold special ordered sets.
Definition: OSmps2OS.h:97
int * m_coneStart
Definition: OSmps2OS.h:107
static char * j
Definition: OSdtoa.cpp:3622
int * m_quadColumnIdx
Definition: OSmps2OS.h:102
IntVector * start
a pointer to the start of each row or column stored in sparse format
Definition: OSInstance.h:306
bool setInstanceName(std::string name)
set the instance name.
int m_nOfCones
Data structures to hold the cone information.
Definition: OSmps2OS.h:106
bool setConstraintNumber(int number)
set the number of constraints.
DoubleVector * value
a pointer to the array of nonzero values being stored
Definition: OSInstance.h:315
bool setObjectiveNumber(int number)
set the number of objectives.
bool addConstraint(int index, std::string name, double lowerBound, double upperBound, double constant)
add a constraint.
bool addObjective(int index, std::string name, std::string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients)
add an objective.
a sparse vector data structure
Definition: OSGeneral.h:122
~OSmps2OS()
the OSmps2os class destructor
Definition: OSmps2OS.cpp:145
bool createOSObjects()
create an OSInstance from the MPS instance representation and an OSOption in case of nonstandard sect...
Definition: OSmps2OS.cpp:192
InstanceData * instanceData
A pointer to an InstanceData object.
Definition: OSInstance.h:2278
OSmps2OS(std::string mpsfilename)
the OSmps2OS class constructor
Definition: OSmps2OS.cpp:24
CoinPackedMatrix * m_CoinPackedMatrix
m_CoinPackedMatrix is a pointer to a CoinPackedMatrix object
Definition: OSmps2OS.h:94
int * m_coneIdx
Definition: OSmps2OS.h:108
CoinMpsIO * m_MpsData
m_MpsData is a pointer to a CoinMpsIO object
Definition: OSmps2OS.h:91
double * values
values holds a double array of nonzero values.
Definition: OSGeneral.h:164
double * el
Definition: OSGeneral.h:621
int * indexes
indexes holds an integer array of indexes whose corresponding values are nonzero. ...
Definition: OSGeneral.h:159
bool setVariableNumber(int number)
set the number of variables.
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
CoinSet ** m_SOS
Definition: OSmps2OS.h:98
used for throwing exceptions.
Definition: OSErrorClass.h:31
LinearConstraintCoefficients * linearConstraintCoefficients
linearConstraintCoefficients is a pointer to a LinearConstraintCoefficients object ...
Definition: OSInstance.h:2196
int * el
Definition: OSGeneral.h:484