00001
00016 #include "OSmps2osil.h"
00017 #include "OSOutput.h"
00018 #include "OSErrorClass.h"
00019 #include <iostream>
00020 #include <sstream>
00021
00022
00023 using std::cout;
00024 using std::endl;
00025
00026 OSmps2osil::OSmps2osil( std::string mpsfilename)
00027 {
00028 m_MpsData = new CoinMpsIO();
00029 int nOfSOS;
00030 CoinSet ** SOS;
00031 int status = m_MpsData->readMps( &mpsfilename[ 0], "", nOfSOS, SOS );
00032 if (status != 0)
00033 throw ErrorClass("Error trying to read MPS file");
00034
00035 m_CoinPackedMatrix = new CoinPackedMatrix( *(m_MpsData->getMatrixByCol()));
00036
00037 if (nOfSOS > 0)
00038 {
00039 #ifndef NDEBUG
00040 std::ostringstream outStr;
00041 {
00042 outStr << "Detected " << nOfSOS << " special ordered sets" << std::endl;
00043 for (int i=0; i < nOfSOS; i++)
00044 {
00045 int numberEntries = SOS[i]->numberEntries();
00046 const int * which = SOS[i]->which();
00047 const double * weights = SOS[i]->weights();
00048 outStr << "SOS " << i << " has type " << SOS[i]->setType();
00049 outStr << " and " << numberEntries << " entries:" << std::endl;
00050 for (int j=0;j<numberEntries;j++)
00051 outStr << " Idx: " << which[j] << " Weight: " << weights[j] << std::endl;
00052 }
00053 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00054 }
00055 #endif
00056
00057 }
00058
00059 #if 0 // Not yet supported
00060 int * columnStart = NULL;
00061 int * columnIdx = NULL;
00062 double * elements = NULL;
00063 status = m_MpsData->readQuadraticMps(NULL, columnStart, columnIdx, elements, 0);
00064
00065 if (status != 0)
00066 {
00067 if (status != -2 && status != -3)
00068 throw ErrorClass("Error trying to read QUADOBJ section");
00069 }
00070 else
00071 {
00072 #ifndef NDEBUG
00073 std::ostringstream outStr;
00074 int numberColumns=m_MpsData->getNumCols();
00075 outStr << "Quadratic objective has " << columnStart[numberColumns] << " entries" << std::endl;
00076 outStr << "Column starts:" << std::endl;
00077 for (int i=0; i<=numberColumns; i++)
00078 outStr << " " << columnStart[i] << std::endl;
00079 for (int i=0; i<numberColumns; i++)
00080 {
00081 if (columnStart[i] < columnStart[i+1])
00082 {
00083 outStr << "Column " << i << ": index value" << std::endl;
00084 for (int j=columnStart[i];j<columnStart[i+1];j++)
00085 outStr << " " << columnIdx[j] << " " << elements[j] << std::endl;
00086 }
00087 }
00088 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00089 #endif
00090
00091 }
00092
00093 int nOfCones;
00094 int * coneStart = NULL;
00095 int * coneIdx = NULL;
00096 int * coneType = NULL;
00097 status = m_MpsData->readConicMps(NULL, coneStart, coneIdx, coneType, nOfCones);
00098 if (status != 0)
00099 {
00100 if (status != -2 && status != -3)
00101 throw ErrorClass("Error trying to read cone section");
00102 }
00103 else
00104 {
00105 #ifndef NDEBUG
00106 std::ostringstream outStr;
00107 outStr << "Conic section has " << nOfCones << " cones" << std::endl;
00108 for (int i=0;i<nOfCones;i++)
00109 {
00110 outStr << "Cone " << i << " has " << coneStart[i+1]-coneStart[i] << " entries ";
00111 outStr << "(type " << coneType[i] << "):" << std::endl;
00112 for (int j=coneStart[i];j<coneStart[i+1];j++)
00113 outStr << " " << coneIdx[j] << std::endl;
00114 }
00115 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, outStr.str());
00116 #endif
00117
00118 }
00119 #endif
00120
00121 }
00122
00123 OSmps2osil::~OSmps2osil()
00124 {
00125 #ifndef NDEBUG
00126 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSModelInterfaces, ENUM_OUTPUT_LEVEL_debug, "now delete m_MpsData\n");
00127 #endif
00128 delete m_MpsData;
00129 m_MpsData = NULL;
00130 delete m_CoinPackedMatrix;
00131 m_CoinPackedMatrix = NULL;
00132 osinstance->instanceData->linearConstraintCoefficients->value->el = NULL;
00133 osinstance->instanceData->linearConstraintCoefficients->start->el = NULL;
00134 osinstance->instanceData->linearConstraintCoefficients->colIdx->el = NULL;
00135 osinstance->instanceData->linearConstraintCoefficients->rowIdx->el = NULL;
00136 delete osinstance;
00137 osinstance = NULL;
00138
00139
00140 }
00141
00142
00143 bool OSmps2osil::createOSInstance( )
00144 {
00145 osinstance = new OSInstance();
00146 int i;
00147 int numvar = m_MpsData->getNumCols();
00148 int numrows = m_MpsData->getNumRows();
00149 int numnonz = m_MpsData->getNumElements();
00150 int numberObj = 1;
00151 int objIndex = -1;
00152 double objWeight = 1.0;
00153 osinstance->setInstanceName( const_cast<char*>(m_MpsData->getProblemName()));
00154
00155
00156
00157 osinstance->setVariableNumber( numvar);
00158 for(i = 0; i < numvar; i++)
00159 {
00160 osinstance->addVariable(i, m_MpsData->columnName( i),
00161 m_MpsData->getColLower()[ i], m_MpsData->getColUpper()[ i],
00162 m_MpsData->isInteger( i)? 'I':'C');
00163 }
00164
00165
00166
00167 SparseVector* objectiveCoefficients = NULL;
00168 objectiveCoefficients = new SparseVector( numvar);
00169 double *p = const_cast<double*>(m_MpsData->getObjCoefficients());
00170 for(i = 0; i < numvar; i++)
00171 {
00172 objectiveCoefficients->indexes[i] = i;
00173 objectiveCoefficients->values[i] = *(p++);
00174 }
00175
00176
00177 osinstance->setObjectiveNumber( numberObj) ;
00178 osinstance->addObjective(objIndex, m_MpsData->getObjectiveName(),
00179 "min", m_MpsData->objectiveOffset(), objWeight, objectiveCoefficients) ;
00180 delete objectiveCoefficients;
00181 objectiveCoefficients = NULL;
00182
00183
00184
00185 osinstance->setConstraintNumber( numrows);
00186 double constant = 0.0;
00187 for(i = 0; i < numrows; i++)
00188 {
00189 osinstance->addConstraint(i, m_MpsData->rowName( i), m_MpsData->getRowLower()[i],
00190 m_MpsData->getRowUpper()[i], constant);
00191 }
00192
00193
00194
00195 int valuesBegin = 0;
00196 int valuesEnd = numnonz - 1;
00197 int startsBegin = 0;
00198 int indexesBegin = 0;
00199 int indexesEnd = numnonz - 1;
00200 int startsEnd = m_CoinPackedMatrix->isColOrdered()?numvar:numrows;
00201 osinstance->setLinearConstraintCoefficients(numnonz, m_CoinPackedMatrix->isColOrdered(),
00202 const_cast<double*>(m_CoinPackedMatrix->getElements()), valuesBegin, valuesEnd,
00203 const_cast<int*>(m_CoinPackedMatrix->getIndices()), indexesBegin, indexesEnd,
00204 const_cast<int*>(m_CoinPackedMatrix->getVectorStarts()), startsBegin, startsEnd);
00205 return true;
00206 }
00207
00208
00209
00210
00211