/home/coin/SVN-release/OS-2.4.2/examples/osDip/OSDipInterface.cpp

Go to the documentation of this file.
00001 /* $Id: OSDipInterface.cpp 3038 2009-11-07 11:43:44Z Gassmann $ */
00016 //===========================================================================//
00017 
00018 
00019 #include "OSDipInterface.h"
00020 #include "UtilMacrosDecomp.h"
00021 
00022 //===========================================================================//
00023 void OS_DipInterface::readOSiL(std::string & fileName) {
00024 
00025         FileUtil *fileUtil = NULL;
00026         fileUtil = new FileUtil();
00027 
00028         try {
00029 
00030                 std::string osil;
00031                 osil = fileUtil->getFileAsString(fileName.c_str());
00032                 m_osilreader = new OSiLReader();
00033                 m_osinstance = m_osilreader->readOSiL(osil);
00034 
00035                 bool columnMajor = false;
00036                 double maxGap = 0;
00037 
00038                 m_coinpm
00039                                 = new CoinPackedMatrix(
00040                                                 columnMajor, //Column or Row Major
00041                                                 columnMajor ? m_osinstance->getConstraintNumber()
00042                                                                 : m_osinstance->getVariableNumber(), //Minor Dimension
00043                                                 columnMajor ? m_osinstance->getVariableNumber()
00044                                                                 : m_osinstance->getConstraintNumber(), //Major Dimension
00045                                                 m_osinstance->getLinearConstraintCoefficientNumber(), //Number of nonzeroes
00046                                                 columnMajor ? m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->values
00047                                                                 : m_osinstance->getLinearConstraintCoefficientsInRowMajor()->values, //Pointer to matrix nonzeroes
00048                                                 columnMajor ? m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes
00049                                                                 : m_osinstance->getLinearConstraintCoefficientsInRowMajor()->indexes, //Pointer to start of minor dimension indexes -- change to allow for row storage
00050                                                 columnMajor ? m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts
00051                                                                 : m_osinstance->getLinearConstraintCoefficientsInRowMajor()->starts, //Pointers to start of columns.
00052                                                 0, 0, maxGap);
00053 
00054                 delete fileUtil;
00055                 fileUtil = NULL;
00056                 //kipp  -- worry about garbage collection here -- do I delete m_osinstance
00057                 //delete  osilreader;
00058                 //osilreader = NULL;
00059 
00060 
00061         } catch (const ErrorClass& eclass) {
00062                 std::cout << "PROBLEM READING OSiL File" << std::endl;
00063                 delete fileUtil;
00064                 std::cout << eclass.errormsg << std::endl;
00065                 fileUtil = NULL;
00066                 throw ErrorClass(eclass.errormsg);
00067 
00068         }
00069 
00070 }//end readOSiL
00071 
00072 
00073 //===========================================================================//
00074 void OS_DipInterface::readOSoL(std::string & fileName) {
00075 
00076         FileUtil *fileUtil = NULL;
00077         fileUtil = new FileUtil();
00078         try {
00079 
00080                 std::string osol;
00081                 osol = fileUtil->getFileAsString(fileName.c_str());
00082                 m_osolreader = new OSoLReader();
00083                 m_osoption = m_osolreader->readOSoL(osol);
00084 
00085                 delete fileUtil;
00086                 fileUtil = NULL;
00087                 //kipp  -- worry about garbage collection here -- do I delete m_osinstance
00088                 //delete  osilreader;
00089                 //osilreader = NULL;
00090 
00091 
00092         } catch (const ErrorClass& eclass) {
00093                 std::cout << "Problem reading OSoL File" << std::endl;
00094                 delete fileUtil;
00095                 std::cout << eclass.errormsg << std::endl;
00096                 fileUtil = NULL;
00097                 throw ErrorClass(eclass.errormsg);
00098 
00099         }
00100 
00101 }// end readOSoL
00102 
00103 CoinPackedMatrix * OS_DipInterface::getCoinPackedMatrix() {
00104 
00105         return m_coinpm;
00106 }//end getCoinPackedMatrix
00107 
00108 CoinPackedVector * OS_DipInterface::getRow(int i) {
00109 
00110         m_row = new CoinPackedVector();
00111 
00112         int k;
00113 
00114         SparseMatrix* sm;
00115 
00116         sm = m_osinstance->getLinearConstraintCoefficientsInRowMajor();
00117 
00118         for (k = sm->starts[i]; k < sm->starts[i + 1]; k++) {
00119 
00120                 m_row->insert(sm->indexes[k], sm->values[k]);
00121 
00122         }
00123 
00124         return m_row;
00125 }//end getRow
00126 
00127 const char* OS_DipInterface::getIntegerColumns() {
00128 
00129         int numVars = getVariableNumber();
00130         m_integerVars = new char[numVars];
00131         int i;
00132 
00133         for (i = 0; i < numVars; i++) {
00134 
00135                 if (m_osinstance->getVariableTypes()[i] == 'B'
00136                                 || m_osinstance->getVariableTypes()[i] == 'I')
00137                         m_integerVars[i] = '1';
00138                 else
00139                         m_integerVars[i] = '0';
00140 
00141         }
00142         return m_integerVars;
00143 
00144 }// end getIntegerColumns
00145 
00146 
00147 double OS_DipInterface::getObjectiveOffset() {
00148 
00149         double offset = 0;
00150         //assume only one objective function for now
00151 
00152         if (getObjectiveConstants() != NULL) {
00153                 offset = getObjectiveConstants()[0];
00154         }
00155 
00156         return offset;
00157 }// end getObjectiveOffset
00158 
00159 
00160 std::vector<std::string> OS_DipInterface::getBlockFactories() {
00161 
00162         //get the factory solver for each block in the model
00163 
00164         if (m_blockFactoriesProcessed == true)
00165                 return m_blockFactories;
00166 
00167         try {
00168                 if (m_osoption == NULL)
00169                         throw ErrorClass("we have a null osoption");
00170 
00171                 std::vector<OtherVariableOption*> otherVariableOptions;
00172                 std::vector<OtherVariableOption*>::iterator vit;
00173 
00174                 if (m_osoption != NULL && m_osoption->getNumberOfOtherVariableOptions()
00175                                 > 0) {
00176 
00177                         otherVariableOptions = m_osoption->getOtherVariableOptions("Dip");
00178                         //iterate over the vector of variable options
00179                         for (vit = otherVariableOptions.begin(); vit
00180                                         != otherVariableOptions.end(); vit++) {
00181 
00182                                 // right now we assume blocks are ordered  -- we ignor value
00183                                 if ((*vit)->name.compare("variableBlockSet") == 0) {
00184 
00185                                         if ((*vit)->value.size() > 0) {
00186 
00187                                                 m_blockFactories.push_back((*vit)->value);
00188 
00189                                         } else {
00190 
00191                                                 m_blockFactories.push_back("");
00192                                         }
00193 
00194                                 }
00195 
00196                         }
00197                 }
00198 
00199         }//end try
00200 
00201         catch (const ErrorClass& eclass) {
00202 
00203                 std::cout << eclass.errormsg << std::endl;
00204                 throw ErrorClass(eclass.errormsg);
00205 
00206         }
00207         m_blockFactoriesProcessed = true;
00208         return m_blockFactories;
00209 }//end getBlockVarIndexes
00210 
00211 std::vector<std::set<int> > OS_DipInterface::getBlockVarIndexes() {
00212 
00213         //get the variable indexes for each block in the model
00214         std::set<int> varSet; //variables indexes in a specific block
00215         if (m_blockVariableIndexesProcessed == true)
00216                 return m_blockVariableIndexes;
00217 
00218         try {
00219                 if (m_osoption == NULL)
00220                         throw ErrorClass("we have a null osoption");
00221 
00222                 int numVar;
00223                 int i;
00224                 std::vector<OtherVariableOption*> otherVariableOptions;
00225                 std::vector<OtherVariableOption*>::iterator vit;
00226 
00227                 if (m_osoption != NULL && m_osoption->getNumberOfOtherVariableOptions()
00228                                 > 0) {
00229 
00230                         otherVariableOptions = m_osoption->getOtherVariableOptions("Dip");
00231                         //iterate over the vector of variable options
00232                         for (vit = otherVariableOptions.begin(); vit
00233                                         != otherVariableOptions.end(); vit++) {
00234 
00235                                 // right now we assume blocks are ordered  -- we ignor value
00236                                 if ((*vit)->name.compare("variableBlockSet") == 0) {
00237 
00238                                         // see if we have a set of block variables
00239                                         // if so we insert into our vector of sets
00240                                         varSet.clear();
00241 
00242                                         numVar = (*vit)->numberOfVar;
00243 
00244                                         for (i = 0; i < numVar; i++) {
00245 
00246                                                 if ((*vit)->var[i]->idx
00247                                                                 >= m_osinstance->getVariableNumber())
00248                                                         throw ErrorClass(
00249                                                                         "found an invalid varaible index in OSoL file");
00250 
00251                                                 varSet.insert((*vit)->var[i]->idx);
00252 
00253                                         }//end for on variables in this block
00254 
00255                                         m_blockVariableIndexes.push_back(varSet);
00256                                 }// end of if 
00257 
00258 
00259                         }//end for over constraint options
00260                 }// if on ospton null
00261 
00262 
00263                 if (m_blockVariableIndexes.size() <= 0)
00264                         throw ErrorClass("someting wrong -- no variables in the blocks");
00265 
00266         } //end try
00267 
00268         catch (const ErrorClass& eclass) {
00269 
00270                 std::cout << eclass.errormsg << std::endl;
00271                 throw ErrorClass(eclass.errormsg);
00272 
00273         }
00274         m_blockVariableIndexesProcessed = true;
00275         return m_blockVariableIndexes;
00276 }//end getBlockVarIndexes
00277 
00278 
00279 std::vector<std::map<int, int> > OS_DipInterface::getBlockConstraintIndexes() {
00280 
00281         if (m_blockConstraintIndexesProcessed == true)
00282                 return m_blockConstraintIndexes;
00283         //get the variable indexes for each block in the model
00284         std::map<int, int> conMap; //constraint indexes in a specific block
00285         std::set<int> varSet; //constraint indexes in a specific block
00286         std::set<int> coreConstraintIndexes;
00287         std::vector<std::set<int> > blockVariableIndexes;
00288 
00289         int *starts = NULL;
00290         int *indexes = NULL;
00291         int kount;
00292 
00293         try {
00294 
00295                 // first get the block variable indexes, these are necessary
00296                 blockVariableIndexes = getBlockVarIndexes();
00297                 // get the core constraint indexes
00298                 coreConstraintIndexes = getCoreConstraintIndexes();
00299 
00300                 if (blockVariableIndexes.size() <= 0)
00301                         throw ErrorClass("someting wrong -- no variables in the blocks");
00302 
00303                 std::vector<std::set<int> >::iterator vit;
00304                 std::set<int>::iterator sit;
00305                 int i;
00306 
00307                 for (vit = blockVariableIndexes.begin(); vit
00308                                 != blockVariableIndexes.end(); vit++) {
00309 
00310                         varSet.clear();
00311                         conMap.clear();
00312 
00313                         varSet = *vit;
00314                         //now get the nonzeros for the variables in
00315                         //varSet and see which nonzeros are in non-core
00316                         //constraints
00317                         kount = 0;
00318                         for (sit = varSet.begin(); sit != varSet.end(); sit++) {
00319 
00320                                 starts
00321                                                 = m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts;
00322                                 indexes
00323                                                 = m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes;
00324 
00325                                 for (i = starts[*sit]; i < starts[*sit + 1]; i++) {
00326 
00327                                         //add the row index if not in a core constraint
00328                                         //don't count same index twice
00329                                         if (coreConstraintIndexes.find(indexes[i])
00330                                                         == coreConstraintIndexes.end() && 
00331                                                         (conMap.find(indexes[i] ) == conMap.end()  )
00332                                         ) {
00333                                                 //we have a new constraint index
00334 
00335                                                 conMap.insert(std::make_pair(indexes[i], kount));
00336                                                 kount++;
00337                                         }
00338                                 }
00339                         }
00340 
00341                         m_blockConstraintIndexes.push_back(conMap);
00342 
00343                 }//end for iterator
00344 
00345 
00346         } //end try
00347 
00348         catch (const ErrorClass& eclass) {
00349 
00350                 std::cout << eclass.errormsg << std::endl;
00351                 throw ErrorClass(eclass.errormsg);
00352 
00353         }
00354         m_blockConstraintIndexesProcessed = true;
00355         return m_blockConstraintIndexes;
00356 }//end getBlockConstraintIndexes
00357 
00358 
00359 std::vector<OSInstance*> OS_DipInterface::getBlockOSInstances() {
00360         //get the OSInstance for each block
00361         if (m_blockOSInstancesProcessed == true)
00362                 return m_blockOSInstances;
00363 
00364         std::map<int, int> conMap; //constraint indexes in a specific block
00365         std::set<int> varSet; //constraint indexes in a specific block
00366         std::vector<std::map<int, int> > blockConstraintIndexes;
00367         std::set<int> coreConstraintIndexes;
00368         std::vector<std::set<int> > blockVariableIndexes;
00369 
00370         int *starts = NULL;
00371         int *indexes = NULL;
00372         double *values = NULL;
00373 
00374         OSInstance *osinstance;
00375         std::vector<std::set<int> >::iterator vit;
00376         std::set<int>::iterator sit;
00377         std::map<int, int>::iterator mit;
00378         int i;
00379         int kount;
00380 
00381         int whichBlock;
00382         int numNonz;
00383         int k1, k2;
00384 
00385         //variable stuff
00386         int numberVar;
00387         std::string* varNames = NULL;
00388         char* varTypes = NULL;
00389         double* varLowerBounds = NULL;
00390         double* varUpperBounds = NULL;
00391 
00392         //constraint stuff
00393         int numberCon;
00394         std::string* conNames = NULL;
00395         double* conLowerBounds = NULL;
00396         double* conUpperBounds = NULL;
00397         double* conConstants = NULL;
00398 
00399         int idx;
00400 
00401         try {
00402 
00403                 // first get the block variable indexes, these are necessary
00404                 blockVariableIndexes = getBlockVarIndexes();
00405                 // get the core constraint indexes
00406 
00407                 coreConstraintIndexes = getCoreConstraintIndexes();
00408                 // get the block constraint indexes
00409 
00410                 blockConstraintIndexes = getBlockConstraintIndexes();
00411 
00412                 if (blockVariableIndexes.size() <= 0)
00413                         throw ErrorClass(
00414                                         "someting wrong in getBlockOSInstances() -- no variables in the blocks");
00415 
00416                 //
00417                 //loop over each block
00418                 //
00419                 whichBlock = 0;
00420                 for (vit = blockVariableIndexes.begin(); vit
00421                                 != blockVariableIndexes.end(); vit++) {
00422 
00423                         varSet.clear();
00424                         conMap.clear();
00425                         varSet = *vit;
00426 
00427                         osinstance = new OSInstance();
00428                         //define variable arrays
00429                         numberVar = varSet.size();
00430                         varTypes = new char[numberVar];
00431                         varNames = new std::string[numberVar];
00432                         varLowerBounds = new double[numberVar];
00433                         varUpperBounds = new double[numberVar];
00434 
00435                         //now get the nonzeros for the variables in
00436                         //varSet and see which nonzeros are in non-core
00437                         //constraints
00438                         kount = 0;
00439 
00440                         osinstance->setVariableNumber(numberVar);
00441 
00442                         SparseVector *objcoeff;
00443                         objcoeff = new SparseVector(numberVar);
00444 
00445                         //need to count the number  of nonzero elements
00446                         //in the block constraints
00447                         numNonz = 0;
00448 
00449                         for (sit = varSet.begin(); sit != varSet.end(); sit++) {
00450 
00451                                 varTypes[kount] = m_osinstance->getVariableTypes()[*sit];
00452                                 varLowerBounds[kount]
00453                                                 = m_osinstance->getVariableLowerBounds()[*sit];
00454                                 varUpperBounds[kount]
00455                                                 = m_osinstance->getVariableUpperBounds()[*sit];
00456 
00457                                 varNames[kount] = m_osinstance->getVariableNames()[*sit];
00458 
00459                                 objcoeff->indexes[kount] = kount;
00460                                 //objcoeff->values[kount] = 0.0;
00461                                 objcoeff->values[kount] =  m_osinstance->getDenseObjectiveCoefficients()[0][ *sit];
00462                                 numNonz
00463                                                 += m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts[*sit + 1]
00464                                                                 - m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts[*sit];
00465 
00466                                 kount++;
00467 
00468                         }//end of loop over the variables in this block
00469 
00470                         osinstance->setVariables(numberVar, varNames, varLowerBounds,
00471                                         varUpperBounds, varTypes);
00472 
00473                         // now the objective function
00474                         osinstance->setObjectiveNumber(1);
00475                         // now the coefficient
00476                         osinstance->addObjective(-1, "objfunction",
00477                                         m_osinstance->getObjectiveMaxOrMins()[0], 0.0, 1.0,
00478                                         objcoeff);
00479 
00480                         conMap = blockConstraintIndexes[whichBlock];
00481                         numberCon = conMap.size();
00482 
00483                         if (numberCon > 0) {
00484 
00485                                 starts = new int[numberVar + 1];
00486                                 indexes = new int[numNonz];
00487                                 values = new double[numNonz];
00488                                 kount = 0;
00489                                 starts[kount] = 0;
00490 
00491                                 numNonz = 0;
00492                                 for (sit = varSet.begin(); sit != varSet.end(); sit++) {
00493 
00494                                         k2
00495                                                         = m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts[*sit
00496                                                                         + 1];
00497                                         k1
00498                                                         = m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts[*sit];
00499 
00500                                         for (i = k1; i < k2; i++) {
00501 
00502                                                 idx
00503                                                                 = m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes[i];
00504 
00505                                                 //check to make sure we are not in a core constraint
00506                                                 if (coreConstraintIndexes.find(idx)
00507                                                                 == coreConstraintIndexes.end()) {
00508 
00509                                                         indexes[numNonz] = conMap[idx];
00510                                                         values[numNonz]
00511                                                                         = m_osinstance->getLinearConstraintCoefficientsInColumnMajor()->values[i];
00512                                                         numNonz++;
00513                                                 }
00514 
00515                                         }
00516                                         starts[kount + 1] = numNonz;
00517 
00518                                         kount++;
00519 
00520                                 }
00521 
00522                                 //now the constraints
00523 
00524 
00525                                 osinstance->setConstraintNumber(numberCon);
00526                                 conLowerBounds = new double[numberCon];
00527                                 conUpperBounds = new double[numberCon];
00528                                 conConstants = new double[numberCon];
00529 
00530                                 for (mit = conMap.begin(); mit != conMap.end(); mit++) {
00531 
00532                                         conLowerBounds[mit->second]
00533                                                         = m_osinstance->getConstraintLowerBounds()[mit->first];
00534                                         conUpperBounds[mit->second]
00535                                                         = m_osinstance->getConstraintUpperBounds()[mit->first];
00536                                         conConstants[mit->second] = 1.0;
00537 
00538 
00539 
00540                                 }
00541 
00542                                 std::cout << "Call setConstraints " << numberCon << std::endl;
00543 
00544                                 osinstance->setConstraints(numberCon, conNames, conLowerBounds,
00545                                                 conUpperBounds, conConstants);
00546 
00547                                 osinstance->setLinearConstraintCoefficients(numNonz, true,
00548                                                 values, 0, numNonz - 1, indexes, 0, numNonz - 1,
00549                                                 starts, 0, numberVar);
00550 
00551                         }
00552                         
00553                         //numNonz--;
00554 
00555 
00556                         //add the osinstance
00557                         m_blockOSInstances.push_back(osinstance);
00558 
00559                         //see what this puppy looks like
00560 
00561                         //std::cout << osinstance->printModel( ) << std::endl;
00562 
00563                         objcoeff->bDeleteArrays = true;
00564                         delete objcoeff;
00565                         if (varLowerBounds != NULL)
00566                                 delete[] varLowerBounds;
00567                         if (varUpperBounds != NULL)
00568                                 delete[] varUpperBounds;
00569                         if (varTypes != NULL)
00570                                 delete[] varTypes;
00571                         if (varNames != NULL)
00572                                 delete[] varNames;
00573 
00574                         //delete []starts;
00575                         //delete []indexes;
00576                         //delete []values;
00577 
00578                         if (conLowerBounds != NULL)
00579                                 delete[] conLowerBounds;
00580                         if (conUpperBounds != NULL)
00581                                 delete[] conUpperBounds;
00582                         if (conConstants != NULL)
00583                                 delete[] conConstants;
00584 
00585                         whichBlock++;
00586 
00587                 }//end for iterator for the blocks
00588 
00589 
00590         } //end try
00591 
00592         catch (const ErrorClass& eclass) {
00593 
00594                 std::cout << eclass.errormsg << std::endl;
00595                 throw ErrorClass(eclass.errormsg);
00596 
00597         }
00598         m_blockOSInstancesProcessed = true;
00599         return m_blockOSInstances;
00600 
00601 }//end getBlockOSInstances()
00602 
00603 std::set<int> OS_DipInterface::getCoreConstraintIndexes() {
00604 
00605         //get the indexes of the core constraints
00606 
00607         if (m_coreConstraintIndexesProcessed == true)
00608                 return m_coreConstraintIndexes;
00609         try {
00610                 if (m_osoption == NULL)
00611                         throw ErrorClass("we have a null osoption");
00612 
00613                 int numCoreCon;
00614                 int i;
00615                 std::vector<OtherConstraintOption*> otherConstraintOptions;
00616                 std::vector<OtherConstraintOption*>::iterator vit;
00617 
00618                 if (m_osoption != NULL
00619                                 && m_osoption->getNumberOfOtherConstraintOptions() > 0) {
00620 
00621                         otherConstraintOptions = m_osoption->getOtherConstraintOptions(
00622                                         "Dip");
00623                         //iterate over the vector of contraint options
00624                         for (vit = otherConstraintOptions.begin(); vit
00625                                         != otherConstraintOptions.end(); vit++) {
00626 
00627                                 // get the core constraints
00628                                 // get the indexes of these constraints
00629                                 if (((*vit)->name.compare("constraintSet") == 0)
00630                                                 && ((*vit)->type.compare("Core") == 0)) {
00631 
00632                                         numCoreCon = (*vit)->numberOfCon;
00633 
00634                                         for (i = 0; i < numCoreCon; i++) {
00635 
00636                                                 if ((*vit)->con[i]->idx
00637                                                                 >= m_osinstance->getConstraintNumber())
00638                                                         throw ErrorClass(
00639                                                                         "found an invalid constraint index in OSoL file");
00640 
00641                                                 m_coreConstraintIndexes.insert((*vit)->con[i]->idx);
00642 
00643                                         }//end for on variables in this block
00644 
00645                                 }//end iff on core constraints
00646 
00647 
00648                         }// end of for on constraint options
00649 
00650 
00651                 }//end of if on osptio null
00652 
00653                 if (m_coreConstraintIndexes.size() <= 0)
00654                         throw ErrorClass(
00655                                         "there were no core constraints listed in the option file");
00656         }// end of try
00657 
00658 
00659         catch (const ErrorClass& eclass) {
00660 
00661                 std::cout << eclass.errormsg << std::endl;
00662                 throw ErrorClass(eclass.errormsg);
00663 
00664         }
00665         m_coreConstraintIndexesProcessed = true;
00666         return m_coreConstraintIndexes;
00667 
00668 }//end getCoreConstraintIndexes
00669 
00670 
00671 double* OS_DipInterface::getObjectiveFunctionCoeff() {
00672 
00673         //Dip has only one objective function 
00674 
00675         return m_osinstance->getDenseObjectiveCoefficients()[0];
00676 }// end getObjectiveFunctionCoeff()
00677 
00678 
00680 OS_DipInterface::OS_DipInterface() :
00681 
00682         m_isProvenOptimal(false), m_bestKnownLB(-1.e20), m_bestKnownUB(1.e20),
00683                         m_osinstance(NULL),
00684                         m_osoption(NULL),       
00685                         m_osilreader(NULL),
00686                         m_osolreader(NULL),
00687                         m_integerVars(NULL),
00688                         m_coinpm(NULL),
00689                         m_row(NULL),
00690                         m_blockVariableIndexesProcessed(false),
00691                         m_coreConstraintIndexesProcessed(false),
00692                         m_blockConstraintIndexesProcessed(false),
00693                         m_blockOSInstancesProcessed(false),
00694                         m_blockFactoriesProcessed(false) 
00695 {
00696 
00697 }
00698 
00699 OS_DipInterface::~OS_DipInterface() {
00700         std::cout << "INSIDE OS DIP INTERFACE DESTRUCTOR" << std::endl;
00701         if (m_osilreader != NULL)
00702                 delete m_osilreader;
00703         if (m_osolreader != NULL)
00704                 delete m_osolreader;
00705         delete m_coinpm;
00706 
00707         std::vector<OSInstance*>::iterator vit;
00708 
00709         if (m_blockOSInstances.size() > 0) {
00710                 for (vit = m_blockOSInstances.begin(); vit != m_blockOSInstances.end(); vit++) {
00711 
00712                         delete *vit;
00713                 }
00714         }
00715 }
00716 
00717 
00718 
00719 std::map<int, std::vector< int> > OS_DipInterface::generateInitialMaster(){
00720         
00721         
00722         // define the classes
00723         FileUtil *fileUtil = NULL;
00724         OSiLReader *osilreader = NULL;
00725         DefaultSolver *solver  = NULL;
00726         OSInstance *osinstance = NULL;
00727         OSOption *osoption = NULL;
00728 
00729         // end classes    
00730 
00731         std::string testFileName;
00732         std::string osil;
00733         
00734         
00735         std::map<int, std::vector< int> > indexMap;
00736         std::vector< int> indexes;
00737         fileUtil = new FileUtil();
00738 
00739 
00740         try {
00741                 testFileName = "../data/restrictedMaster5.osil";
00742                 
00743                 osil = fileUtil->getFileAsString(testFileName.c_str());
00744 
00745 #ifdef MY_DEBUG
00746                 std::cout << "Done reading the file" << std::endl;
00747 #endif
00748 
00749                 osilreader = new OSiLReader();
00750                 
00751                 //create the osption 
00752                 osoption = new OSOption();
00753 
00754 #ifdef MY_DEBUG
00755                 parsingTestResult << "Reading files successfully" << std::endl;
00756 #endif
00757 
00758                 osinstance = osilreader->readOSiL(osil);
00759                 
00760                 solver = new CoinSolver();
00761                 solver->sSolverName ="cbc"; 
00762                 solver->osinstance = osinstance;
00763                 solver->osoption = osoption;    
00764                 std::cout << "CALLING SOLVE FOR RESTRICTED MASTER" << std::endl;
00765                 solver->solve();
00766                 int i;
00767                 int j;
00768                 int k;
00769                 int vecSize;
00770                 // now get the primal solution
00771                 std::vector<IndexValuePair*> primalValPair;
00772                 primalValPair = solver->osresult->getOptimalPrimalVariableValues( 0);
00773                 vecSize = primalValPair.size();
00774                 
00775                 
00776                 int numNodes = 5;
00777                 int numHubs = 1;
00778                 int totalDemand = 4;
00779                 
00780                 
00781                 
00782                 int* routeDemand = NULL;
00783                 int startPnt;
00784                 
00785                 routeDemand = new int[ numHubs];
00786                 int kount;
00787                 
00788                 std::string* varNames;
00789                 varNames =  osinstance->getVariableNames();
00790                 
00791                 //get route demands
00792                 kount = 0;
00793                 for(k = 0; k < numHubs; k++){
00794                         
00795                         routeDemand[k ] = (int)primalValPair[ kount]->value;
00796                         
00797                         std::cout << "Route Demand = " << routeDemand[k] << std::endl;
00798                         kount++;
00799                 
00800                 }
00801                 
00802                 //now get x variable values
00803                 
00804                 for(k = 0; k < numHubs; k++){
00805                         
00806                         startPnt = k*totalDemand*(numNodes*numNodes - numNodes) + (routeDemand[ k] - 1)*(numNodes*numNodes - numNodes);
00807                         
00808                         for(i = 0; i < numNodes; i++){
00809                                 
00810                                 
00811                                 for(j = 0; j < i; j++){
00812                                         
00813                                         if( primalValPair[ kount]->value > .1){
00814                                                 
00815                                                 std::cout << varNames[ kount] << std::endl;
00816                                                 indexes.push_back(startPnt +  i*(numNodes-1) + j    );
00817                                                 
00818                                                 
00819                                         }
00820                                         
00821                                         kount++;
00822                                         
00823                                 }
00824                                 
00825                                 for(j = i + 1; j < numNodes; j++){
00826                                         
00827                                         
00828                                         if( primalValPair[ kount]->value > .1){
00829                                                 std::cout << varNames[ kount] << std::endl;
00830                                                 indexes.push_back(startPnt +  i*(numNodes-1) + j -1   );
00831                                                 
00832                                                 
00833                                         }
00834                                         
00835                                         
00836                                         kount++;
00837                                         
00838                                 }
00839                                 
00840                         }
00841                         
00842                         //create the map
00843                         
00844                         indexMap.insert(make_pair(k, indexes));
00845                         
00846                 }//end loop on k -- hubs
00847                 
00848                 
00849                 std::cout << "DONE CALLING SOLVE FOR RESTRICTED MASTER" << std::endl;
00850 
00851                 
00852                 
00853                 delete[] routeDemand;
00854                 
00855                 
00856                 delete osilreader;
00857                 osilreader = NULL;
00858                 delete solver;
00859                 solver = NULL;
00860                 delete osoption;
00861                 osoption = NULL;
00862                 
00863 
00864         } catch (const ErrorClass& eclass) {
00865                 std::cout << std::endl << std::endl << std::endl;
00866                 if (osilreader != NULL)
00867                         delete osilreader;
00868                 if (solver != NULL)
00869                         delete solver;
00870                 if (osoption != NULL)
00871                         delete osoption;
00872                 //  Problem with the parser
00873                 throw ErrorClass(eclass.errormsg);
00874         }
00875 
00876         delete fileUtil;
00877         fileUtil = NULL;
00878         return indexMap;
00879         
00880 }//end generateInitialMaster

Generated on Wed Nov 30 03:04:10 2011 by  doxygen 1.4.7