00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "OSDipApp.h"
00016
00017 #include "DecompVar.h"
00018
00019 void OSDipApp::initializeApp(UtilParameters & utilParam) {
00020
00021 UtilPrintFuncBegin(m_osLog, m_classTag, "initializeApp()",
00022 m_appParam.LogLevel, 2);
00023
00024
00025
00026
00027 m_appParam.getSettings(utilParam);
00028 if (m_appParam.LogLevel >= 1)
00029 m_appParam.dumpSettings(m_osLog);
00030
00031 try {
00032
00033
00034
00035
00036 if (m_appParam.OSiLFile.size() <= 1)
00037 throw ErrorClass("An OSiL file not specified in the paramater file");
00038 std::string osilFile = m_appParam.DataDir + UtilDirSlash()
00039 + m_appParam.OSiLFile;
00040 m_osInterface.readOSiL( osilFile);
00041
00042
00043
00044
00045
00046
00047 if (m_appParam.OSoLFile.size() > 0) {
00048 std::string osolFile = m_appParam.DataDir + UtilDirSlash()
00049 + m_appParam.OSoLFile;
00050 m_osInterface.readOSoL( osolFile);
00051 }
00052
00053
00054
00055
00056 createModels();
00057
00058
00059
00118 m_blockOSInstances = m_osInterface.getBlockOSInstances();
00119
00120 std::vector<OSInstance* >::iterator vit1;
00121
00122 OSDipBlockCoinSolver *coinSolver = NULL;
00123
00124 for (vit1 = m_blockOSInstances.begin(); vit1 != m_blockOSInstances.end(); vit1++) {
00125
00126 coinSolver = new OSDipBlockCoinSolver( *vit1) ;
00127 m_osDipBlockCoinSolver.push_back( coinSolver ) ;
00128
00129 }
00130
00131 std::vector<std::set<int> >::iterator vit2;
00132 std::set<int>::iterator sit;
00133 std::set<int> blockVar;
00134 std::vector<IndexValuePair*> solIndexValPair;
00135 std::vector<IndexValuePair*>::iterator vit3;
00136 double optVal;
00137
00138 double *cost = NULL;
00139 int index;
00140 int whichBlock;
00141
00142 m_blockVars = m_osInterface.getBlockVarIndexes();
00143 whichBlock = 0;
00144
00145 for (vit2 = m_blockVars.begin(); vit2 != m_blockVars.end(); vit2++) {
00146
00147 blockVar = *vit2;
00148
00149 cost = new double[ blockVar.size() ];
00150
00151 index = 0;
00152
00153 for (sit = blockVar.begin(); sit != blockVar.end(); sit++) {
00154
00155 cost[index] = m_objective[ *sit];
00156 index++;
00157
00158 }
00159
00160 m_osDipBlockCoinSolver[whichBlock++]->solve( cost, &solIndexValPair, &optVal);
00161
00162 std::cout << "OPTIMAL VALUE = " << optVal << std::endl;
00163
00164 std::cout << "solIndexValPair SIZE 2 = " << solIndexValPair.size() << std::endl;
00165
00166 for (vit3 = solIndexValPair.begin(); vit3 != solIndexValPair.end(); vit3++) {
00167
00168 std::cout << "IDEX = " << (*vit3)->idx << std::endl;
00169 std::cout << "VALUE = " << (*vit3)->value << std::endl;
00170 }
00171
00172 delete []cost;
00173
00174
00175 }
00176
00177
00178 } catch (const ErrorClass& eclass) {
00179
00180 throw ErrorClass(eclass.errormsg);
00181
00182 }
00183
00184 UtilPrintFuncEnd(m_osLog, m_classTag, "initializeApp()",
00185 m_appParam.LogLevel, 2);
00186
00187 }
00188
00189
00190 void OSDipApp::createModelPart(DecompConstraintSet * model,
00191 const int nRowsPart, const int * rowsPart) {
00192
00193 const int nCols = m_osInterface.getVariableNumber();
00194 const double * rowLB = m_osInterface.getRowLower();
00195 const double * rowUB = m_osInterface.getRowUpper();
00196 const double * colLB = m_osInterface.getColLower();
00197 const double * colUB = m_osInterface.getColUpper();
00198 const char * integerVars = m_osInterface.getIntegerColumns();
00199
00200 std::cout << "STARTING createModelPart" << std::endl;
00201
00202 model->M = new CoinPackedMatrix(false, 0.0, 0.0);
00203
00204 if (!model->M)
00205 throw UtilExceptionMemory("createModels", "OSDipApp");
00206 model->reserve(nRowsPart, nCols);
00207 model->M->submatrixOf(*m_osInterface.m_coinpm, nRowsPart,
00208 rowsPart);
00209
00210
00211
00212
00213
00214 m_appParam.UseNames = true;
00215 int i, r;
00216 for (i = 0; i < nRowsPart; i++) {
00217 r = rowsPart[i];
00218 if (m_appParam.UseNames == true) {
00219 const char * rowName =
00220 m_osInterface.getConstraintNames()[r].c_str();
00221
00222 if (rowName)
00223 model->rowNames.push_back(rowName);
00224
00225
00226 }
00227 model->rowLB.push_back(rowLB[r]);
00228 model->rowUB.push_back(rowUB[r]);
00229 }
00230 copy(colLB, colLB + nCols, back_inserter(model->colLB));
00231 copy(colUB, colUB + nCols, back_inserter(model->colUB));
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 if (m_appParam.ColumnUB < 1.0e15) {
00248 for (i = 0; i < nCols; i++) {
00249 if (colUB[i] > 1.0e15) {
00250 model->colUB[i] = m_appParam.ColumnUB;
00251 }
00252 }
00253 }
00254 if (m_appParam.ColumnLB > -1.0e15) {
00255 for (i = 0; i < nCols; i++) {
00256 if (colLB[i] < -1.0e15) {
00257 model->colLB[i] = m_appParam.ColumnLB;
00258 }
00259 }
00260 }
00261
00262
00263
00264
00265
00266 for (i = 0; i < nCols; i++) {
00267 if (m_appParam.UseNames == true) {
00268
00269 const char * colName =
00270 m_osInterface.getVariableNames()[i].c_str();
00271
00272 if (colName)
00273 model->colNames.push_back(colName);
00274 }
00275
00276 if ( (integerVars != NULL) && integerVars[i] == '1' ) {
00277
00278 model->integerVars.push_back(i);
00279 }
00280 }
00281
00282
00283
00284 UTIL_DELARR( integerVars);
00285
00286 }
00287
00288
00289 void OSDipApp::createModelPartSparse(DecompConstraintSet * model,
00290 const int nRowsPart, const int * rowsPart) {
00291
00292 const int nColsOrig = m_osInterface.getVariableNumber();
00293 const double * rowLB = m_osInterface.getRowLower();
00294 const double * rowUB = m_osInterface.getRowUpper();
00295 const double * colLB = m_osInterface.getColLower();
00296 const double * colUB = m_osInterface.getColUpper();
00297 const char * integerVars = m_osInterface.getIntegerColumns();
00298
00299
00300
00301
00302 model->setSparse(nColsOrig);
00303 int nCols, origIndex, newIndex;
00304 vector<int>::iterator vit;
00305 newIndex = 0;
00306 for (vit = model->activeColumns.begin(); vit != model->activeColumns.end(); vit++) {
00307 origIndex = *vit;
00308
00309
00310
00311
00312 model->pushCol(colLB[origIndex], colUB[origIndex],
00313 integerVars[origIndex] == '0' ? false : true, origIndex);
00314
00315
00316 if(integerVars[origIndex] == 0) std::cout << "HERE I AM" << std::endl;
00317
00318
00319
00320
00321
00322
00323
00324 if (m_appParam.ColumnUB < 1.0e15) {
00325 if (colUB[origIndex] > 1.0e15) {
00326 model->colUB[newIndex] = m_appParam.ColumnUB;
00327 }
00328 }
00329 if (m_appParam.ColumnLB > -1.0e15) {
00330 if (colLB[origIndex] < -1.0e15) {
00331 model->colLB[newIndex] = m_appParam.ColumnLB;
00332 }
00333 }
00334
00335 if (m_appParam.UseNames) {
00336
00337 const char * colName =
00338 m_osInterface.getConstraintNames()[origIndex].c_str();
00339
00340 if (colName)
00341 model->colNames.push_back(colName);
00342 }
00343 newIndex++;
00344 }
00345
00346 nCols = static_cast<int> (model->activeColumns.size());
00347 assert(static_cast<int> (model->colLB.size()) == nCols);
00348 assert(static_cast<int> (model->colUB.size()) == nCols);
00349
00350 model->M = new CoinPackedMatrix(false, 0.0, 0.0);
00351 if (!model->M)
00352 throw UtilExceptionMemory("createModels", "OSDipApp");
00353 model->M->setDimensions(0, nCols);
00354 model->reserve(nRowsPart, nCols);
00355
00356
00357
00358
00359 int i, k, r, begInd;
00360 const map<int, int> & origToSparse = model->getMapOrigToSparse();
00361 const CoinPackedMatrix * M = m_osInterface.getCoinPackedMatrix();
00362 const int * matInd = M->getIndices();
00363 const CoinBigIndex * matBeg = M->getVectorStarts();
00364 const int * matLen = M->getVectorLengths();
00365 const double * matVal = M->getElements();
00366 const int * matIndI = NULL;
00367 const double * matValI = NULL;
00368
00369 vector<CoinBigIndex> & rowBeg = model->m_rowBeg;
00370 vector<int> & rowInd = model->m_rowInd;
00371 vector<double> & rowVal = model->m_rowVal;
00372 map<int, int>::const_iterator mit;
00373
00374 begInd = 0;
00375 rowBeg.push_back(0);
00376 for (i = 0; i < nRowsPart; i++) {
00377 r = rowsPart[i];
00378 if (m_appParam.UseNames) {
00379 const char * rowName =
00380 m_osInterface.getConstraintNames()[r].c_str();
00381 if (rowName)
00382 model->rowNames.push_back(rowName);
00383 }
00384 model->rowLB.push_back(rowLB[r]);
00385 model->rowUB.push_back(rowUB[r]);
00386
00387 matIndI = matInd + matBeg[r];
00388 matValI = matVal + matBeg[r];
00389 for (k = 0; k < matLen[r]; k++) {
00390 origIndex = matIndI[k];
00391 mit = origToSparse.find(origIndex);
00392 assert(mit != origToSparse.end());
00393 rowInd.push_back(mit->second);
00394 rowVal.push_back(matValI[k]);
00395 }
00396 begInd += matLen[r];
00397 rowBeg.push_back(begInd);
00398 }
00399 model->M->appendRows(nRowsPart, &rowBeg[0], &rowInd[0], &rowVal[0]);
00400
00401
00402 rowBeg.clear();
00403 rowInd.clear();
00404 rowVal.clear();
00405 UTIL_DELARR( integerVars);
00406 }
00407
00408
00409 void OSDipApp::createModels() {
00410
00411 UtilPrintFuncBegin(m_osLog, m_classTag, "createModels()",
00412 m_appParam.LogLevel, 2);
00413 int i;
00414 int j;
00415 const int nCols = m_osInterface.getVariableNumber();
00416 const int nRows = m_osInterface.getConstraintNumber();
00417
00418 try{
00419
00420
00421
00422 m_objective = new double[nCols];
00423 for (i = 0; i < nCols; i++) {
00424 m_objective[i] = m_osInterface.getObjectiveFunctionCoeff()[i];
00425
00426 }
00427 setModelObjective( m_objective);
00428
00429
00430
00431
00432 int nRowsRelax, nRowsCore;
00433 nRowsRelax = 0;
00434 nRowsCore = 0;
00435
00436
00437
00438 std::vector<OtherConstraintOption*> otherConstraintOptions;
00439 std::vector<OtherConstraintOption*>::iterator vit;
00440
00441
00442
00443
00444
00445
00446 int *rowsRelax;
00447 int whichBlock;
00448 DecompConstraintSet *modelRelax = NULL;
00449
00450 std::set<int> blockVars;
00451 std::set<int> blockVarsAll;
00452 std::set<int> blockConAll;
00453 std::set<int>::iterator sit;
00454 CoinPackedVector *row;
00455 int *rowVars;
00456 int rowSize;
00457 if (m_osInterface.m_osoption != NULL
00458 && m_osInterface.m_osoption->getNumberOfOtherConstraintOptions()
00459 > 0) {
00460
00461 otherConstraintOptions
00462 = m_osInterface.m_osoption->getOtherConstraintOptions("Dip");
00463
00464
00465 for (vit = otherConstraintOptions.begin(); vit
00466 != otherConstraintOptions.end(); vit++) {
00467
00468
00469
00470 if( ( (*vit)->name.compare("constraintSet") == 0)
00471 && ( (*vit)->type.compare("Block") == 0)) {
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 whichBlock = atoi( (*vit)->value.c_str() );
00482
00483
00484
00485
00486 nRowsRelax = (*vit)->numberOfCon;
00487 rowsRelax = new int[nRowsRelax];
00488
00489
00490
00491 blockVars.clear();
00492 for (i = 0; i < nRowsRelax; i++) {
00493 rowsRelax[i] = (*vit)->con[i]->idx;
00494
00495 if( (*vit)->con[i]->idx >= nRows) throw ErrorClass( "found an invalid row index in OSoL file");
00496
00497 m_blocks[ whichBlock].push_back( rowsRelax[i] );
00498
00499
00500
00501 if (blockConAll.find( (*vit)->con[i]->idx ) == blockConAll.end()) {
00502 blockConAll.insert( (*vit)->con[i]->idx );
00503 }
00504
00505
00506 row = m_osInterface.getRow(rowsRelax[i]);
00507 rowSize = row->getNumElements();
00508 rowVars = row->getIndices();
00509
00510 for (j = 0; j < rowSize; j++) {
00511
00512 if (blockVars.find(rowVars[j]) == blockVars.end()) {
00513 blockVars.insert(rowVars[j]);
00514 }
00515
00516 }
00517
00518 delete row;
00519
00520 }
00521
00522 modelRelax = new DecompConstraintSet();
00523 CoinAssertHint(modelRelax, "Error: Out of Memory");
00524
00525
00526
00527
00528 for (sit = blockVars.begin(); sit != blockVars.end(); sit++) {
00529
00530 modelRelax->activeColumns.push_back( *sit);
00531
00532
00533
00534
00535
00536 if (blockVarsAll.find( *sit) == blockVarsAll.end()) {
00537 blockVarsAll.insert (*sit);
00538 }else{
00539
00540 throw ErrorClass("Variable " + UtilIntToStr(*sit) + " appears in more than one block");
00541
00542 }
00543
00544 }
00545
00546
00547
00548
00549 if (m_appParam.LogLevel >= 3) {
00550 (*m_osLog) << "Active Columns : " << whichBlock << endl;
00551 UtilPrintVector(modelRelax->activeColumns, m_osLog);
00552
00553 }
00554 createModelPartSparse(modelRelax, nRowsRelax, rowsRelax);
00555
00556
00557 m_modelR.insert(make_pair(whichBlock + 1, modelRelax));
00558 setModelRelax(modelRelax, "relax" + UtilIntToStr(whichBlock),
00559 whichBlock);
00560
00561 if (m_appParam.LogLevel >= 3) {
00562 (*m_osLog) << std::endl << std::endl;
00563 (*m_osLog) << "HERE COMES THE DUPLICATION (WHEN createModelPartSparse USED)" << std::endl;
00564 }
00565
00566 UtilPrintVector( modelRelax->activeColumns, m_osLog);
00567
00568
00569
00570 UTIL_DELARR( rowsRelax);
00571
00572
00573
00574 }
00575 }
00576 }
00577
00578
00579 int *rowsCore = NULL;
00580
00581 int kount = 0;
00582 nRowsCore = nRows - blockConAll.size();
00583
00584 if(nRowsCore <= 0) throw ErrorClass("We need at least one coupling constraint");
00585
00586 rowsCore = new int[nRowsCore];
00587
00588 for(i = 0; i < nRows; i++){
00589
00590 if (blockConAll.find( i ) == blockConAll.end() ){
00591
00592
00593 rowsCore[ kount++] = i;
00594
00595
00596
00597 }
00598 }
00599
00600 if( kount != nRowsCore) throw ErrorClass("There was an error counting coupling constraints");
00601
00602
00603 DecompConstraintSet * modelCore = new DecompConstraintSet();
00604 createModelPart(modelCore, nRowsCore, rowsCore);
00605
00606 setModelCore(modelCore, "core");
00607
00608
00609
00610 m_modelC = modelCore;
00611
00612
00613
00614
00615 for (i = 0; i < nCols; i++) {
00616 if (blockVarsAll.find(i) == blockVarsAll.end()) {
00617 modelCore->masterOnlyCols.push_back(i);
00618 std::cout << "MASTER ONLY VARIABLE " << i << std::endl;
00619 }
00620 }
00621
00622
00623
00624
00625
00626
00627 int nMasterOnlyCols = static_cast<int> (modelCore->masterOnlyCols.size());
00628 if (nMasterOnlyCols) {
00629 if (m_appParam.LogLevel >= 1)
00630 (*m_osLog) << "Create model part Master-Only." << endl;
00631
00632 createModelMasterOnlys2(modelCore->masterOnlyCols);
00633
00634 }
00635
00636
00637 UtilPrintFuncBegin(m_osLog, m_classTag, "printCurrentProblem()",
00638 m_appParam.LogLevel, 2);
00639
00640
00641
00642 UTIL_DELARR( rowsCore);
00643
00644
00645 }
00646
00647 catch(const ErrorClass& eclass){
00648
00649 throw ErrorClass( eclass.errormsg);
00650
00651 }
00652
00653 }
00654
00655
00656
00657
00658 void OSDipApp::createModelMasterOnlys2(vector<int> & masterOnlyCols) {
00659
00660 int nBlocks = static_cast<int>(m_blocks.size());
00661 const int nCols = m_osInterface.getVariableNumber();
00662 const double * colLB = m_osInterface.getColLower();
00663 const double * colUB = m_osInterface.getColUpper();
00664 const char * integerVars = m_osInterface.getIntegerColumns();
00665 int nMasterOnlyCols = static_cast<int> (masterOnlyCols.size());
00666
00667 if (m_appParam.LogLevel >= 1) {
00668 (*m_osLog) << "nCols = " << nCols << endl;
00669 (*m_osLog) << "nMasterOnlyCols = " << nMasterOnlyCols << endl;
00670 }
00671
00672 if (nMasterOnlyCols == 0)
00673 return;
00674
00675 int i;
00676 vector<int>::iterator vit;
00677 for (vit = masterOnlyCols.begin(); vit != masterOnlyCols.end(); vit++) {
00678 i = *vit;
00679
00680
00681
00682
00683 DecompConstraintSet * model = new DecompConstraintSet();
00684 model->m_masterOnly = true;
00685 model->m_masterOnlyIndex = i;
00686 model->m_masterOnlyLB = colLB[i];
00687
00688 model->m_masterOnlyUB = colUB[i];
00689
00690
00691 if(integerVars[i] == '1') model->m_masterOnlyIsInt = true;
00692
00693 if (m_appParam.ColumnUB < 1.0e15)
00694 if (colUB[i] > 1.0e15)
00695 model->m_masterOnlyUB = m_appParam.ColumnUB;
00696 if (m_appParam.ColumnLB > -1.0e15)
00697 if (colLB[i] < -1.0e15)
00698 model->m_masterOnlyLB = m_appParam.ColumnLB;
00699
00700 m_modelMasterOnly.insert(make_pair(i, model));
00701 setModelRelax(model, "master_only" + UtilIntToStr(i), nBlocks);
00702 nBlocks++;
00703 }
00704
00705 UTIL_DELARR( integerVars);
00706 return;
00707 }
00708
00709
00710 int OSDipApp::generateInitVars(DecompVarList & initVars) {
00711
00712
00713
00714
00715
00716
00717 std::cout << "GENERATE INIT VARS" << std::endl;
00718 UtilPrintFuncBegin(m_osLog, m_classTag, "generateInitVars()",
00719 m_appParam.LogLevel, 2);
00720
00721
00722
00723
00724
00725
00726
00727 std::vector<OtherVariableOption*> otherVarOptions;
00728 std::vector<OtherVariableOption*>::iterator vit;
00729 int *index = NULL;
00730 double *value = NULL;
00731 int i;
00732 double objValue;
00733 int whichBlock;
00734 DecompVar *var;
00735
00736 try{
00737 if (m_osInterface.m_osoption != NULL
00738 && m_osInterface.m_osoption->getNumberOfOtherVariableOptions() > 0) {
00739 std::cout << "Number of other variable options = "
00740 << m_osInterface.m_osoption->getNumberOfOtherVariableOptions()
00741 << std::endl;
00742 otherVarOptions = m_osInterface.m_osoption->getOtherVariableOptions(
00743 "Dip");
00744
00745
00746 for (vit = otherVarOptions.begin(); vit != otherVarOptions.end(); vit++) {
00747
00748
00749
00750 if ((*vit)->name.compare("initialCol") == 0) {
00751
00752 index = new int[(*vit)->numberOfVar];
00753 value = new double[(*vit)->numberOfVar];
00754
00755 objValue = 0.0;
00756
00757 for (i = 0; i < (*vit)->numberOfVar; i++) {
00758
00759 index[i] = (*vit)->var[i]->idx;
00760
00761
00762 value[ i] = atoi((*vit)->var[i]->value.c_str());
00763 objValue += m_objective[index[i]];
00764
00765 }
00766
00767 whichBlock = atoi( (*vit)->value.c_str() );
00768 var = new DecompVar((*vit)->numberOfVar, index, value, objValue);
00769 var->setBlockId(whichBlock);
00770 initVars.push_back(var);
00771
00772
00773 UTIL_DELARR( index);
00774 UTIL_DELARR( value);
00775
00776
00777 }
00778
00779 }
00780
00781 }
00782
00783 }
00784 catch(const ErrorClass& eclass){
00785
00786 throw ErrorClass( eclass.errormsg);
00787
00788 }
00789
00790 UtilPrintFuncEnd(m_osLog, m_classTag, "generateInitVars()",
00791 m_appParam.LogLevel, 2);
00792 return static_cast<int> (initVars.size());
00793 }