17 #include "DecompVar.h"
21 UtilPrintFuncBegin(m_osLog,
m_classTag,
"initializeApp()",
37 throw ErrorClass(
"An OSiL file not specified in the parameter file");
120 std::vector<OSInstance* >::iterator vit1;
127 m_osDipBlockCoinSolver.push_back( coinSolver ) ;
131 std::vector<std::set<int> >::iterator vit2;
132 std::set<int>::iterator sit;
133 std::set<int> blockVar;
134 std::vector<IndexValuePair*> solIndexValPair;
135 std::vector<IndexValuePair*>::iterator vit3;
149 cost =
new double[ blockVar.size() ];
153 for (sit = blockVar.begin(); sit != blockVar.end(); sit++) {
160 m_osDipBlockCoinSolver[whichBlock++]->solve( cost, &solIndexValPair, &optVal);
162 std::cout <<
"OPTIMAL VALUE = " << optVal << std::endl;
164 std::cout <<
"solIndexValPair SIZE 2 = " << solIndexValPair.size() << std::endl;
166 for (vit3 = solIndexValPair.begin(); vit3 != solIndexValPair.end(); vit3++) {
168 std::cout <<
"IDEX = " << (*vit3)->idx << std::endl;
169 std::cout <<
"VALUE = " << (*vit3)->value << std::endl;
184 UtilPrintFuncEnd(m_osLog,
m_classTag,
"initializeApp()",
191 const int nRowsPart,
const int * rowsPart) {
200 std::cout <<
"STARTING createModelPart" << std::endl;
202 model->M =
new CoinPackedMatrix(
false, 0.0, 0.0);
205 throw UtilExceptionMemory(
"createModels",
"OSDipApp");
206 model->reserve(nRowsPart, nCols);
216 for (i = 0; i < nRowsPart; i++) {
219 const char * rowName =
223 model->rowNames.push_back(rowName);
227 model->rowLB.push_back(rowLB[r]);
228 model->rowUB.push_back(rowUB[r]);
230 copy(colLB, colLB + nCols, back_inserter(model->colLB));
231 copy(colUB, colUB + nCols, back_inserter(model->colUB));
248 for (i = 0; i < nCols; i++) {
249 if (colUB[i] > 1.0e15) {
255 for (i = 0; i < nCols; i++) {
256 if (colLB[i] < -1.0e15) {
266 for (i = 0; i < nCols; i++) {
269 const char * colName =
273 model->colNames.push_back(colName);
276 if ( (integerVars != NULL) && integerVars[i] ==
'1' ) {
278 model->integerVars.push_back(i);
284 UTIL_DELARR( integerVars);
289 void OSDipApp::createModelPartSparse(DecompConstraintSet * model,
290 const int nRowsPart,
const int * rowsPart) {
302 model->setSparse(nColsOrig);
303 int nCols, origIndex, newIndex;
304 vector<int>::iterator vit;
306 for (vit = model->activeColumns.begin(); vit != model->activeColumns.end(); vit++) {
312 model->pushCol(colLB[origIndex], colUB[origIndex],
313 integerVars[origIndex] ==
'0' ?
false :
true, origIndex);
316 if(integerVars[origIndex] == 0) std::cout <<
"HERE I AM" << std::endl;
325 if (colUB[origIndex] > 1.0e15) {
330 if (colLB[origIndex] < -1.0e15) {
337 const char * colName =
341 model->colNames.push_back(colName);
346 nCols =
static_cast<int> (model->activeColumns.size());
347 assert(static_cast<int> (model->colLB.size()) == nCols);
348 assert(static_cast<int> (model->colUB.size()) == nCols);
350 model->M =
new CoinPackedMatrix(
false, 0.0, 0.0);
352 throw UtilExceptionMemory(
"createModels",
"OSDipApp");
353 model->M->setDimensions(0, nCols);
354 model->reserve(nRowsPart, nCols);
360 const map<int, int> & origToSparse = model->getMapOrigToSparse();
362 const int * matInd = M->getIndices();
363 const CoinBigIndex * matBeg = M->getVectorStarts();
364 const int * matLen = M->getVectorLengths();
365 const double * matVal = M->getElements();
366 const int * matIndI = NULL;
367 const double * matValI = NULL;
369 vector<CoinBigIndex> & rowBeg = model->m_rowBeg;
370 vector<int> & rowInd = model->m_rowInd;
371 vector<double> & rowVal = model->m_rowVal;
372 map<int, int>::const_iterator mit;
376 for (i = 0; i < nRowsPart; i++) {
379 const char * rowName =
382 model->rowNames.push_back(rowName);
384 model->rowLB.push_back(rowLB[r]);
385 model->rowUB.push_back(rowUB[r]);
387 matIndI = matInd + matBeg[
r];
388 matValI = matVal + matBeg[
r];
389 for (k = 0; k < matLen[
r]; k++) {
390 origIndex = matIndI[
k];
391 mit = origToSparse.find(origIndex);
392 assert(mit != origToSparse.end());
393 rowInd.push_back(mit->second);
394 rowVal.push_back(matValI[k]);
397 rowBeg.push_back(begInd);
399 model->M->appendRows(nRowsPart, &rowBeg[0], &rowInd[0], &rowVal[0]);
405 UTIL_DELARR( integerVars);
411 UtilPrintFuncBegin(m_osLog,
m_classTag,
"createModels()",
423 for (i = 0; i < nCols; i++) {
432 int nRowsRelax, nRowsCore;
438 std::vector<OtherConstraintOption*> otherConstraintOptions;
439 std::vector<OtherConstraintOption*>::iterator vit;
448 DecompConstraintSet *modelRelax = NULL;
450 std::set<int> blockVars;
451 std::set<int> blockVarsAll;
452 std::set<int> blockConAll;
453 std::set<int>::iterator sit;
454 CoinPackedVector *row;
461 otherConstraintOptions
465 for (vit = otherConstraintOptions.begin(); vit
466 != otherConstraintOptions.end(); vit++) {
470 if( ( (*vit)->name.compare(
"constraintSet") == 0)
471 && ( (*vit)->type.compare(
"Block") == 0)) {
481 whichBlock = atoi( (*vit)->value.c_str() );
486 nRowsRelax = (*vit)->numberOfCon;
487 rowsRelax =
new int[nRowsRelax];
492 for (i = 0; i < nRowsRelax; i++) {
493 rowsRelax[i] = (*vit)->con[i]->idx;
495 if( (*vit)->con[i]->idx >= nRows)
throw ErrorClass(
"found an invalid row index in OSoL file");
497 m_blocks[ whichBlock].push_back( rowsRelax[i] );
501 if (blockConAll.find( (*vit)->con[i]->idx ) == blockConAll.end()) {
502 blockConAll.insert( (*vit)->con[i]->idx );
507 rowSize = row->getNumElements();
508 rowVars = row->getIndices();
510 for (j = 0; j < rowSize; j++) {
512 if (blockVars.find(rowVars[j]) == blockVars.end()) {
513 blockVars.insert(rowVars[j]);
522 modelRelax =
new DecompConstraintSet();
523 CoinAssertHint(modelRelax,
"Error: Out of Memory");
528 for (sit = blockVars.begin(); sit != blockVars.end(); sit++) {
530 modelRelax->activeColumns.push_back( *sit);
536 if (blockVarsAll.find( *sit) == blockVarsAll.end()) {
537 blockVarsAll.insert (*sit);
540 throw ErrorClass(
"Variable " + UtilIntToStr(*sit) +
" appears in more than one block");
550 (*m_osLog) <<
"Active Columns : " << whichBlock << endl;
551 UtilPrintVector(modelRelax->activeColumns, m_osLog);
554 createModelPartSparse(modelRelax, nRowsRelax, rowsRelax);
557 m_modelR.insert(make_pair(whichBlock + 1, modelRelax));
558 setModelRelax(modelRelax,
"relax" + UtilIntToStr(whichBlock),
562 (*m_osLog) << std::endl << std::endl;
563 (*m_osLog) <<
"HERE COMES THE DUPLICATION (WHEN createModelPartSparse USED)" << std::endl;
566 UtilPrintVector( modelRelax->activeColumns, m_osLog);
570 UTIL_DELARR( rowsRelax);
579 int *rowsCore = NULL;
582 nRowsCore = nRows - blockConAll.size();
584 if(nRowsCore <= 0)
throw ErrorClass(
"We need at least one coupling constraint");
586 rowsCore =
new int[nRowsCore];
588 for(i = 0; i < nRows; i++){
590 if (blockConAll.find( i ) == blockConAll.end() ){
593 rowsCore[ kount++] = i;
600 if( kount != nRowsCore)
throw ErrorClass(
"There was an error counting coupling constraints");
603 DecompConstraintSet * modelCore =
new DecompConstraintSet();
606 setModelCore(modelCore,
"core");
615 for (i = 0; i < nCols; i++) {
616 if (blockVarsAll.find(i) == blockVarsAll.end()) {
617 modelCore->masterOnlyCols.push_back(i);
618 std::cout <<
"MASTER ONLY VARIABLE " << i << std::endl;
627 int nMasterOnlyCols =
static_cast<int> (modelCore->masterOnlyCols.size());
628 if (nMasterOnlyCols) {
630 (*m_osLog) <<
"Create model part Master-Only." << endl;
637 UtilPrintFuncBegin(m_osLog,
m_classTag,
"printCurrentProblem()",
642 UTIL_DELARR( rowsCore);
660 int nBlocks =
static_cast<int>(m_blocks.size());
665 int nMasterOnlyCols =
static_cast<int> (masterOnlyCols.size());
668 (*m_osLog) <<
"nCols = " << nCols << endl;
669 (*m_osLog) <<
"nMasterOnlyCols = " << nMasterOnlyCols << endl;
672 if (nMasterOnlyCols == 0)
676 vector<int>::iterator vit;
677 for (vit = masterOnlyCols.begin(); vit != masterOnlyCols.end(); vit++) {
683 DecompConstraintSet * model =
new DecompConstraintSet();
684 model->m_masterOnly =
true;
685 model->m_masterOnlyIndex = i;
686 model->m_masterOnlyLB = colLB[i];
688 model->m_masterOnlyUB = colUB[i];
691 if(integerVars[i] ==
'1') model->m_masterOnlyIsInt =
true;
694 if (colUB[i] > 1.0e15)
697 if (colLB[i] < -1.0e15)
701 setModelRelax(model,
"master_only" + UtilIntToStr(i), nBlocks);
705 UTIL_DELARR( integerVars);
717 std::cout <<
"GENERATE INIT VARS" << std::endl;
718 UtilPrintFuncBegin(m_osLog,
m_classTag,
"generateInitVars()",
727 std::vector<OtherVariableOption*> otherVarOptions;
728 std::vector<OtherVariableOption*>::iterator vit;
730 double *value = NULL;
739 std::cout <<
"Number of other variable options = "
746 for (vit = otherVarOptions.begin(); vit != otherVarOptions.end(); vit++) {
750 if ((*vit)->name.compare(
"initialCol") == 0) {
752 index =
new int[(*vit)->numberOfVar];
753 value =
new double[(*vit)->numberOfVar];
757 for (i = 0; i < (*vit)->numberOfVar; i++) {
759 index[i] = (*vit)->var[i]->idx;
762 value[ i] = atoi((*vit)->var[i]->value.c_str());
767 whichBlock = atoi( (*vit)->value.c_str() );
768 var =
new DecompVar((*vit)->numberOfVar, index, value, objValue);
769 var->setBlockId(whichBlock);
770 initVars.push_back(var);
790 UtilPrintFuncEnd(m_osLog,
m_classTag,
"generateInitVars()",
792 return static_cast<int> (initVars.size());
void getSettings(UtilParameters &utilParam)
CoinPackedMatrix * getCoinPackedMatrix()
Returns array[getNumCols()] specifying if a variable is integer.
std::vector< OtherConstraintOption * > getOtherConstraintOptions(std::string solver_name)
Get the array of other constraint options.
DecompConstraintSet * m_modelC
The model constraint systems used for different algos.
const int getVariableNumber() const
Get variable number.
int getNumberOfOtherConstraintOptions()
Get the number of other constraint options.
void dumpSettings(std::ostream *os=&std::cout)
std::map< int, DecompConstraintSet * > m_modelR
std::string errormsg
errormsg is the error that is causing the exception to be thrown
const std::string m_classTag
Class id tag (for log / debugging).
const double * getColLower() const
Get variable lower bounds.
CoinPackedMatrix * m_coinpm
double * getObjectiveFunctionCoeff()
Returns array[getNumCols()] specifying if a variable is integer.
void createModelMasterOnlys2(std::vector< int > &masterOnlyCols)
OSDipParam m_appParam
Application specific parameters.
CoinPackedVector * getRow(int i)
Returns array[getNumCols()] specifying if a variable is integer.
void initializeApp(UtilParameters &utilParam)
Initialize application.
const double * getColUpper() const
Get variable upper bounds.
void createModels()
Create model parts.
const std::string * getConstraintNames() const
Get constraint names.
std::vector< std::set< int > > getBlockVarIndexes()
Returns array[getNumCols()] specifying if a variable is integer.
void readOSiL(std::string &filename)
Returns array[getNumCols()] specifying if a variable is integer.
std::map< int, DecompConstraintSet * > m_modelMasterOnly
The model constraint system used master only vars.
const double * getRowUpper() const
Get constraint upper bounds.
void createModelPart(DecompConstraintSet *model, const int nRowsPart, const int *rowsPart)
std::vector< OSInstance * > m_blockOSInstances
m_blockOSInstances is a vector with an osinstance for each block
double * m_objective
The model objective coefficients (original space).
void fint fint fint real fint real real real real real real real * r
const int getConstraintNumber() const
Get constraint number.
const double * getRowLower() const
Get constraint lower bounds.
OS_DipInterface m_osInterface
the OS interface
std::vector< std::set< int > > m_blockVars
m_blockVars is a vector with the set of variables for each block
void readOSoL(std::string &filename)
Returns array[getNumCols()] specifying if a variable is integer.
std::vector< OSInstance * > getBlockOSInstances()
Returns array[getNumCols()] specifying if a variable is integer.
const std::string * getVariableNames() const
Get variable names.
const char * getIntegerColumns()
Returns array[getNumCols()] specifying if a variable is integer.
int generateInitVars(DecompVarList &initVars)
int getNumberOfOtherVariableOptions()
Get the number of other variable options.
used for throwing exceptions.
std::vector< OtherVariableOption * > getOtherVariableOptions(std::string solver_name)
Get the <other> variable options associated with a particular solver.