OSmps2osil.cpp
Go to the documentation of this file.
1 /* $Id: OSmps2osil.cpp 5284 2017-12-08 13:52:50Z stefan $ */
16 #include "OSmps2osil.h"
17 #include "OSOutput.h"
18 #include "OSErrorClass.h"
19 #include <iostream>
20 #include <sstream>
21 
22 
23 using std::cout;
24 using std::endl;
25 
26 OSmps2osil::OSmps2osil( std::string mpsfilename)
27 {
28  m_MpsData = new CoinMpsIO();
29  int nOfSOS;
30  CoinSet ** SOS;
31  int status = m_MpsData->readMps( &mpsfilename[ 0], "", nOfSOS, SOS );
32  if (status != 0)
33  throw ErrorClass("Error trying to read MPS file");
34 
35  m_CoinPackedMatrix = new CoinPackedMatrix( *(m_MpsData->getMatrixByCol()));
36 
37  if (nOfSOS > 0)
38  {
39 #ifndef NDEBUG
40  std::ostringstream outStr;
41  {
42  outStr << "Detected " << nOfSOS << " special ordered sets" << std::endl;
43  for (int i=0; i < nOfSOS; i++)
44  {
45  int numberEntries = SOS[i]->numberEntries();
46  const int * which = SOS[i]->which();
47  const double * weights = SOS[i]->weights();
48  outStr << "SOS " << i << " has type " << SOS[i]->setType();
49  outStr << " and " << numberEntries << " entries:" << std::endl;
50  for (int j=0;j<numberEntries;j++)
51  outStr << " Idx: " << which[j] << " Weight: " << weights[j] << std::endl;
52  }
54  }
55 #endif
56 // throw ErrorClass("SOS has not been implemented yet");
57  }
58 
59 #if 0 // Not yet supported
60  int * columnStart = NULL;
61  int * columnIdx = NULL;
62  double * elements = NULL;
63  status = m_MpsData->readQuadraticMps(NULL, columnStart, columnIdx, elements, 0);
64 
65  if (status != 0)
66  {
67  if (status != -2 && status != -3)
68  throw ErrorClass("Error trying to read QUADOBJ section");
69  }
70  else
71  {
72 #ifndef NDEBUG
73  std::ostringstream outStr;
74  int numberColumns=m_MpsData->getNumCols();
75  outStr << "Quadratic objective has " << columnStart[numberColumns] << " entries" << std::endl;
76  outStr << "Column starts:" << std::endl;
77  for (int i=0; i<=numberColumns; i++)
78  outStr << " " << columnStart[i] << std::endl;
79  for (int i=0; i<numberColumns; i++)
80  {
81  if (columnStart[i] < columnStart[i+1])
82  {
83  outStr << "Column " << i << ": index value" << std::endl;
84  for (int j=columnStart[i];j<columnStart[i+1];j++)
85  outStr << " " << columnIdx[j] << " " << elements[j] << std::endl;
86  }
87  }
89 #endif
90 // throw ErrorClass("QUADOBJ has not been implemented yet");
91  }
92 
93  int nOfCones;
94  int * coneStart = NULL;
95  int * coneIdx = NULL;
96  int * coneType = NULL;
97  status = m_MpsData->readConicMps(NULL, coneStart, coneIdx, coneType, nOfCones);
98  if (status != 0)
99  {
100  if (status != -2 && status != -3)
101  throw ErrorClass("Error trying to read cone section");
102  }
103  else
104  {
105 #ifndef NDEBUG
106  std::ostringstream outStr;
107  outStr << "Conic section has " << nOfCones << " cones" << std::endl;
108  for (int i=0;i<nOfCones;i++)
109  {
110  outStr << "Cone " << i << " has " << coneStart[i+1]-coneStart[i] << " entries ";
111  outStr << "(type " << coneType[i] << "):" << std::endl;
112  for (int j=coneStart[i];j<coneStart[i+1];j++)
113  outStr << " " << coneIdx[j] << std::endl;
114  }
116 #endif
117 // throw ErrorClass("CSECTION has not been implemented yet");
118  }
119 #endif
120 
121 }// end constructor
122 
124 {
125 #ifndef NDEBUG
126  osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, "now delete m_MpsData\n");
127 #endif
128  delete m_MpsData;
129  m_MpsData = NULL;
130  delete m_CoinPackedMatrix;
131  m_CoinPackedMatrix = NULL;
136  delete osinstance;
137  osinstance = NULL;
138  //delete m_MpsData;
139 
140 } // end destructor
141 
142 // Kipp-- put in integer programming
144 {
145  osinstance = new OSInstance();
146  int i;
147  int numvar = m_MpsData->getNumCols();
148  int numrows = m_MpsData->getNumRows();
149  int numnonz = m_MpsData->getNumElements();
150  int numberObj = 1;
151  int objIndex = -1;
152  double objWeight = 1.0;
153  osinstance->setInstanceName( const_cast<char*>(m_MpsData->getProblemName()));
154  //
155  // get the variable information
156  //
157  osinstance->setVariableNumber( numvar);
158  for(i = 0; i < numvar; i++)
159  {
160  osinstance->addVariable(i, m_MpsData->columnName( i),
161  m_MpsData->getColLower()[ i], m_MpsData->getColUpper()[ i],
162  m_MpsData->isInteger( i)? 'I':'C');
163  }
164  //
165  // now create the objective function
166  //
167  SparseVector* objectiveCoefficients = NULL;
168  objectiveCoefficients = new SparseVector( numvar);
169  double *p = const_cast<double*>(m_MpsData->getObjCoefficients());
170  for(i = 0; i < numvar; i++)
171  {
172  objectiveCoefficients->indexes[i] = i;
173  objectiveCoefficients->values[i] = *(p++);
174  }
175  // We will go with the
176  // majority on this one and assume minimization by default
177  osinstance->setObjectiveNumber( numberObj) ;
178  osinstance->addObjective(objIndex, m_MpsData->getObjectiveName(),
179  "min", m_MpsData->objectiveOffset(), objWeight, objectiveCoefficients) ;
180  delete objectiveCoefficients;
181  objectiveCoefficients = NULL;
182  //
183  // now fill in row information
184  //
185  osinstance->setConstraintNumber( numrows);
186  double constant = 0.0;
187  for(i = 0; i < numrows; i++)
188  {
189  osinstance->addConstraint(i, m_MpsData->rowName( i), m_MpsData->getRowLower()[i],
190  m_MpsData->getRowUpper()[i], constant);
191  }
192  //
193  // now fill in the lp arrays
194  //
195  int valuesBegin = 0;
196  int valuesEnd = numnonz - 1;
197  int startsBegin = 0;
198  int indexesBegin = 0;
199  int indexesEnd = numnonz - 1;
200  int startsEnd = m_CoinPackedMatrix->isColOrdered()?numvar:numrows;
202  const_cast<double*>(m_CoinPackedMatrix->getElements()), valuesBegin, valuesEnd,
203  const_cast<int*>(m_CoinPackedMatrix->getIndices()), indexesBegin, indexesEnd,
204  const_cast<int*>(m_CoinPackedMatrix->getVectorStarts()), startsBegin, startsEnd);
205  return true;
206 }
207 
208 
209 
210 
211 
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
~OSmps2osil()
the OSmps2osil class destructor
Definition: OSmps2osil.cpp:123
IntVector * colIdx
a pointer of column indices if the problem is stored by row
Definition: OSInstance.h:312
static char * j
Definition: OSdtoa.cpp:3622
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.
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
InstanceData * instanceData
A pointer to an InstanceData object.
Definition: OSInstance.h:2278
bool createOSInstance()
create an OSInstance from the MPS instance representation
Definition: OSmps2osil.cpp:143
double * values
values holds a double array of nonzero values.
Definition: OSGeneral.h:164
double * el
Definition: OSGeneral.h:621
CoinPackedMatrix * m_CoinPackedMatrix
m_CoinPackedMatrix is a pointer to a CoinPackedMatrix object
Definition: OSmps2osil.h:63
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
OSInstance * osinstance
osinstance is a pointer to the OSInstance object that gets created from the instance represented in N...
Definition: OSmps2osil.h:58
OSmps2osil(std::string mpsfilename)
the OSmps2osil class constructor
Definition: OSmps2osil.cpp:26
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
CoinMpsIO * m_MpsData
m_MpsData is a pointer to a CoinMpsIO object
Definition: OSmps2osil.h:61