本文整理汇总了C++中OsiSolverInterface::getMatrixByCol方法的典型用法代码示例。如果您正苦于以下问题:C++ OsiSolverInterface::getMatrixByCol方法的具体用法?C++ OsiSolverInterface::getMatrixByCol怎么用?C++ OsiSolverInterface::getMatrixByCol使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OsiSolverInterface
的用法示例。
在下文中一共展示了OsiSolverInterface::getMatrixByCol方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: printLPMatrix
void printLPMatrix (const OsiSolverInterface &si) {
// the coefficient matrix
const CoinPackedMatrix *A = si.getMatrixByCol ();
printMatrix (A);
}
示例2: clique
void
CglClique::selectRowCliques(const OsiSolverInterface& si,int numOriginalRows)
{
const int numrows = si.getNumRows();
std::vector<int> clique(numrows, 1);
int i, j, k;
// First scan through the binary fractional variables and see where do they
// have a 1 coefficient
const CoinPackedMatrix& mcol = *si.getMatrixByCol();
for (j = 0; j < sp_numcols; ++j) {
const CoinShallowPackedVector& vec = mcol.getVector(sp_orig_col_ind[j]);
const int* ind = vec.getIndices();
const double* elem = vec.getElements();
for (i = vec.getNumElements() - 1; i >= 0; --i) {
if (elem[i] != 1.0) {
clique[ind[i]] = 0;
}
}
}
// Now check the sense and rhs (by checking rowupper) and the rest of the
// coefficients
const CoinPackedMatrix& mrow = *si.getMatrixByRow();
const double* rub = si.getRowUpper();
for (i = 0; i < numrows; ++i) {
if (rub[i] != 1.0||i>=numOriginalRows) {
clique[i] = 0;
continue;
}
if (clique[i] == 1) {
const CoinShallowPackedVector& vec = mrow.getVector(i);
const double* elem = vec.getElements();
for (j = vec.getNumElements() - 1; j >= 0; --j) {
if (elem[j] < 0) {
clique[i] = 0;
break;
}
}
}
}
// Finally collect the still standing rows into sp_orig_row_ind
sp_numrows = std::accumulate(clique.begin(), clique.end(), 0);
sp_orig_row_ind = new int[sp_numrows];
for (i = 0, k = 0; i < numrows; ++i) {
if (clique[i] == 1) {
sp_orig_row_ind[k++] = i;
}
}
}
示例3: CbcObject
// Useful constructor
CbcFollowOn::CbcFollowOn (CbcModel * model)
: CbcObject(model)
{
assert (model);
OsiSolverInterface * solver = model_->solver();
matrix_ = *solver->getMatrixByCol();
matrix_.removeGaps();
matrix_.setExtraGap(0.0);
matrixByRow_ = *solver->getMatrixByRow();
int numberRows = matrix_.getNumRows();
rhs_ = new int[numberRows];
int i;
const double * rowLower = solver->getRowLower();
const double * rowUpper = solver->getRowUpper();
// Row copy
const double * elementByRow = matrixByRow_.getElements();
const int * column = matrixByRow_.getIndices();
const CoinBigIndex * rowStart = matrixByRow_.getVectorStarts();
const int * rowLength = matrixByRow_.getVectorLengths();
for (i = 0; i < numberRows; i++) {
rhs_[i] = 0;
double value = rowLower[i];
if (value == rowUpper[i]) {
if (floor(value) == value && value >= 1.0 && value < 10.0) {
// check elements
bool good = true;
for (int j = rowStart[i]; j < rowStart[i] + rowLength[i]; j++) {
int iColumn = column[j];
if (!solver->isBinary(iColumn))
good = false;
double elValue = elementByRow[j];
if (floor(elValue) != elValue || value < 1.0)
good = false;
}
if (good)
rhs_[i] = static_cast<int> (value);
}
}
}
}
示例4: objSense
//#############################################################################
void
MibSHeuristic::lowerObjHeuristic()
{
/*
optimize wrt to lower-level objective
over current feasible lp feasible region
*/
MibSModel * model = MibSModel_;
OsiSolverInterface * oSolver = model->getSolver();
//OsiSolverInterface * hSolver = new OsiCbcSolverInterface();
OsiSolverInterface* hSolver = new OsiSymSolverInterface();
double objSense(model->getLowerObjSense());
int lCols(model->getLowerDim());
int uCols(model->getUpperDim());
int * lColIndices = model->getLowerColInd();
int * uColIndices = model->getUpperColInd();
double * lObjCoeffs = model->getLowerObjCoeffs();
//int tCols(lCols + uCols);
int tCols(oSolver->getNumCols());
//assert(tCols == oSolver->getNumCols());
hSolver->loadProblem(*oSolver->getMatrixByCol(),
oSolver->getColLower(), oSolver->getColUpper(),
oSolver->getObjCoefficients(),
oSolver->getRowLower(), oSolver->getRowUpper());
int j(0);
for(j = 0; j < tCols; j++){
if(oSolver->isInteger(j))
hSolver->setInteger(j);
}
double * nObjCoeffs = new double[tCols];
int i(0), index(0);
CoinZeroN(nObjCoeffs, tCols);
for(i = 0; i < lCols; i++){
index = lColIndices[i];
nObjCoeffs[index] = lObjCoeffs[i];
}
//MibS objective sense is the opposite of OSI's!
hSolver->setObjSense(objSense);
hSolver->setObjective(nObjCoeffs);
//double cutoff(model->getCutoff());
double cutoff(model->getKnowledgeBroker()->getIncumbentValue());
if(model->getNumSolutions()){
CoinPackedVector objCon;
//double rhs(cutoff * objSense);
//double smlTol(1.0);
double rhs(cutoff);
for(i = 0; i < tCols; i++){
objCon.insert(i, oSolver->getObjCoefficients()[i]
* oSolver->getObjSense());
}
hSolver->addRow(objCon, - hSolver->getInfinity(), rhs);
}
if(0)
hSolver->writeLp("lobjheurstic");
if(0){
dynamic_cast<OsiCbcSolverInterface *>
(hSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
}
else{
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("prep_level", -1);
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("verbosity", -2);
dynamic_cast<OsiSymSolverInterface *>
(hSolver)->setSymParam("max_active_nodes", 1);
}
hSolver->branchAndBound();
if(hSolver->isProvenOptimal()){
double upperObjVal(0.0);
/*****************NEW ******************/
MibSSolution *mibSol = NULL;
OsiSolverInterface * lSolver = model->bS_->setUpModel(hSolver, true);
//.........这里部分代码省略.........
示例5: uObjSense
//#############################################################################
mcSol
MibSHeuristic::solveSubproblem(double beta)
{
/*
optimize wrt to weighted upper-level objective
over current feasible lp feasible region
*/
MibSModel * model = MibSModel_;
OsiSolverInterface * oSolver = model->getSolver();
//OsiSolverInterface * sSolver = new OsiCbcSolverInterface();
OsiSolverInterface* sSolver = new OsiSymSolverInterface();
//sSolver = oSolver->clone();
//OsiSolverInterface * sSolver = tmpSolver;
//OsiSolverInterface * tmpSolver = new OsiSolverInterface(oSolver);
double uObjSense(oSolver->getObjSense());
double lObjSense(model->getLowerObjSense());
int lCols(model->getLowerDim());
int uCols(model->getUpperDim());
int * lColIndices = model->getLowerColInd();
int * uColIndices = model->getUpperColInd();
double * lObjCoeffs = model->getLowerObjCoeffs();
const double * uObjCoeffs = oSolver->getObjCoefficients();
double etol(etol_);
int tCols(uCols + lCols);
assert(tCols == oSolver->getNumCols());
sSolver->loadProblem(*oSolver->getMatrixByCol(),
oSolver->getColLower(), oSolver->getColUpper(),
oSolver->getObjCoefficients(),
oSolver->getRowLower(), oSolver->getRowUpper());
int j(0);
for(j = 0; j < tCols; j++){
if(oSolver->isInteger(j))
sSolver->setInteger(j);
}
double * nObjCoeffs = new double[tCols];
int i(0), index(0);
CoinZeroN(nObjCoeffs, tCols);
/* Multiply the UL columns of the UL objective by beta */
for(i = 0; i < uCols; i++){
index = uColIndices[i];
if(fabs(uObjCoeffs[index]) > etol)
nObjCoeffs[index] = beta * uObjCoeffs[index] * uObjSense;
else
nObjCoeffs[index] = 0.0;
}
/* Multiply the LL columns of the UL objective by beta */
for(i = 0; i < lCols; i++){
index = lColIndices[i];
if(fabs(uObjCoeffs[index]) > etol)
nObjCoeffs[index] = beta* uObjCoeffs[index] * uObjSense;
else
nObjCoeffs[index] = 0.0;
}
/* Add the LL columns of the LL objective multiplied by (1 - beta) */
for(i = 0; i < lCols; i++){
index = lColIndices[i];
if(fabs(lObjCoeffs[i]) > etol)
nObjCoeffs[index] += (1 - beta) * lObjCoeffs[i] * lObjSense;
}
sSolver->setObjective(nObjCoeffs);
//int i(0);
if(0){
for(i = 0; i < sSolver->getNumCols(); i++){
std::cout << "betaobj " << sSolver->getObjCoefficients()[i] << std::endl;
}
}
if(0){
sSolver->writeLp("afterbeta");
//sSolver->writeMps("afterbeta");
}
if(0){
for(i = 0; i < sSolver->getNumCols(); i++){
std::cout << "obj " << sSolver->getObjCoefficients()[i] << std::endl;
std::cout << "upper " << sSolver->getColUpper()[i] << std::endl;
std::cout << "lower " << sSolver->getColLower()[i] << std::endl;
}
}
if(0){
dynamic_cast<OsiCbcSolverInterface *>
(sSolver)->getModelPtr()->messageHandler()->setLogLevel(0);
//.........这里部分代码省略.........
示例6: model
int * analyze(OsiClpSolverInterface * solverMod, int & numberChanged,
double & increment, bool changeInt,
CoinMessageHandler * generalMessageHandler, bool noPrinting)
{
bool noPrinting_ = noPrinting;
OsiSolverInterface * solver = solverMod->clone();
char generalPrint[200];
if (0) {
// just get increment
CbcModel model(*solver);
model.analyzeObjective();
double increment2 = model.getCutoffIncrement();
printf("initial cutoff increment %g\n", increment2);
}
const double *objective = solver->getObjCoefficients() ;
const double *lower = solver->getColLower() ;
const double *upper = solver->getColUpper() ;
int numberColumns = solver->getNumCols() ;
int numberRows = solver->getNumRows();
double direction = solver->getObjSense();
int iRow, iColumn;
// Row copy
CoinPackedMatrix matrixByRow(*solver->getMatrixByRow());
const double * elementByRow = matrixByRow.getElements();
const int * column = matrixByRow.getIndices();
const CoinBigIndex * rowStart = matrixByRow.getVectorStarts();
const int * rowLength = matrixByRow.getVectorLengths();
// Column copy
CoinPackedMatrix matrixByCol(*solver->getMatrixByCol());
const double * element = matrixByCol.getElements();
const int * row = matrixByCol.getIndices();
const CoinBigIndex * columnStart = matrixByCol.getVectorStarts();
const int * columnLength = matrixByCol.getVectorLengths();
const double * rowLower = solver->getRowLower();
const double * rowUpper = solver->getRowUpper();
char * ignore = new char [numberRows];
int * changed = new int[numberColumns];
int * which = new int[numberRows];
double * changeRhs = new double[numberRows];
memset(changeRhs, 0, numberRows*sizeof(double));
memset(ignore, 0, numberRows);
numberChanged = 0;
int numberInteger = 0;
for (iColumn = 0; iColumn < numberColumns; iColumn++) {
if (upper[iColumn] > lower[iColumn] + 1.0e-8 && solver->isInteger(iColumn))
numberInteger++;
}
bool finished = false;
while (!finished) {
int saveNumberChanged = numberChanged;
for (iRow = 0; iRow < numberRows; iRow++) {
int numberContinuous = 0;
double value1 = 0.0, value2 = 0.0;
bool allIntegerCoeff = true;
double sumFixed = 0.0;
int jColumn1 = -1, jColumn2 = -1;
for (CoinBigIndex j = rowStart[iRow]; j < rowStart[iRow] + rowLength[iRow]; j++) {
int jColumn = column[j];
double value = elementByRow[j];
if (upper[jColumn] > lower[jColumn] + 1.0e-8) {
if (!solver->isInteger(jColumn)) {
if (numberContinuous == 0) {
jColumn1 = jColumn;
value1 = value;
} else {
jColumn2 = jColumn;
value2 = value;
}
numberContinuous++;
} else {
if (fabs(value - floor(value + 0.5)) > 1.0e-12)
allIntegerCoeff = false;
}
} else {
sumFixed += lower[jColumn] * value;
}
}
double low = rowLower[iRow];
if (low > -1.0e20) {
low -= sumFixed;
if (fabs(low - floor(low + 0.5)) > 1.0e-12)
allIntegerCoeff = false;
}
double up = rowUpper[iRow];
if (up < 1.0e20) {
up -= sumFixed;
if (fabs(up - floor(up + 0.5)) > 1.0e-12)
allIntegerCoeff = false;
}
if (!allIntegerCoeff)
continue; // can't do
if (numberContinuous == 1) {
// see if really integer
// This does not allow for complicated cases
if (low == up) {
if (fabs(value1) > 1.0e-3) {
//.........这里部分代码省略.........
示例7: NoBasisError
void
CglLandP::CachedData::getData(const OsiSolverInterface &si)
{
int nBasics = si.getNumRows();
int nNonBasics = si.getNumCols();
if (basis_ != NULL)
delete basis_;
basis_ = dynamic_cast<CoinWarmStartBasis *> (si.getWarmStart());
if (!basis_)
throw NoBasisError();
if (nBasics_ > 0 || nBasics != nBasics_)
{
delete [] basics_;
basics_ = NULL;
}
if (basics_ == NULL)
{
basics_ = new int[nBasics];
nBasics_ = nBasics;
}
if (nNonBasics_ > 0 || nNonBasics != nNonBasics_)
{
delete [] nonBasics_;
nonBasics_ = NULL;
}
if (nonBasics_ == NULL)
{
nonBasics_ = new int[nNonBasics];
nNonBasics_ = nNonBasics;
}
int n = nBasics + nNonBasics;
if ( nBasics_ + nNonBasics_ > 0 || nBasics_ + nNonBasics_ != n)
{
delete [] colsol_;
delete [] integers_;
integers_ = NULL;
colsol_ = NULL;
slacks_ = NULL;
}
if (colsol_ == NULL)
{
colsol_ = new double[n];
slacks_ = &colsol_[nNonBasics];
}
if (integers_ == NULL)
{
integers_ = new bool[n];
}
const double * rowLower = si.getRowLower();
const double * rowUpper = si.getRowUpper();
//determine which slacks are integer
const CoinPackedMatrix * m = si.getMatrixByCol();
const double * elems = m->getElements();
const int * inds = m->getIndices();
const CoinBigIndex * starts = m->getVectorStarts();
const int * lengths = m->getVectorLengths();
// int numElems = m->getNumElements();
int numCols = m->getNumCols();
assert(numCols == nNonBasics_);
// int numRows = m->getNumRows();
CoinFillN(integers_ ,n, true);
for (int i = 0 ; i < numCols ; i++)
{
if (si.isContinuous(i))
integers_[i] = false;
}
bool * integerSlacks = integers_ + numCols;
for (int i = 0 ; i < nBasics ; i++)
{
if (rowLower[i] > -1e50 && INT_INFEAS(rowLower[i]) > 1e-15)
integerSlacks[i] = false;
if (rowUpper[i] < 1e50 && INT_INFEAS(rowUpper[i]) > 1e-15)
integerSlacks[i] = false;
}
for (int i = 0 ; i < numCols ; i++)
{
CoinBigIndex end = starts[i] + lengths[i];
if (integers_[i])
{
for (CoinBigIndex k=starts[i] ; k < end; k++)
{
if (integerSlacks[inds[k]] && INT_INFEAS(elems[k])>1e-15 )
integerSlacks[inds[k]] = false;
}
}
else
{
for (CoinBigIndex k=starts[i] ; k < end; k++)
{
if (integerSlacks[inds[k]])
integerSlacks[inds[k]] = false;
}
}
}
CoinCopyN(si.getColSolution(), si.getNumCols(), colsol_);
//.........这里部分代码省略.........
示例8: assert
/*===========================================================================*
Create the set packing submatrix
*===========================================================================*/
void
CglClique::createSetPackingSubMatrix(const OsiSolverInterface& si)
{
sp_col_start = new int[sp_numcols+1];
sp_row_start = new int[sp_numrows+1];
std::fill(sp_col_start, sp_col_start + (sp_numcols+1), 0);
std::fill(sp_row_start, sp_row_start + (sp_numrows+1), 0);
int i, j;
const CoinPackedMatrix& mcol = *si.getMatrixByCol();
const int numrows = si.getNumRows();
int* clique = new int[numrows];
std::fill(clique, clique+numrows, -1);
for (i = 0; i < sp_numrows; ++i)
clique[sp_orig_row_ind[i]] = i;
for (j = 0; j < sp_numcols; ++j) {
const CoinShallowPackedVector& vec = mcol.getVector(sp_orig_col_ind[j]);
const int* ind = vec.getIndices();
for (i = vec.getNumElements() - 1; i >= 0; --i) {
if (clique[ind[i]] >= 0) {
++sp_col_start[j];
++sp_row_start[clique[ind[i]]];
}
}
}
std::partial_sum(sp_col_start, sp_col_start+sp_numcols, sp_col_start);
std::rotate(sp_col_start, sp_col_start+sp_numcols,
sp_col_start + (sp_numcols+1));
std::partial_sum(sp_row_start, sp_row_start+sp_numrows, sp_row_start);
std::rotate(sp_row_start, sp_row_start+sp_numrows,
sp_row_start + (sp_numrows+1));
const int nzcnt = sp_col_start[sp_numcols];
assert(nzcnt == sp_row_start[sp_numrows]);
/*
Now create the vectors with row indices for each column (sp_col_ind) and
column indices for each row (sp_row_ind). It turns out that
CoinIsOrthogonal assumes that the row indices for a given column are listed
in ascending order. This is *not* a solver-independent assumption! At best,
one can hope that the underlying solver will produce an index vector that's
either ascending or descending. Under that assumption, compare the first
and last entries and proceed accordingly. Eventually some solver will come
along that hands back an index vector in random order, and CoinIsOrthogonal
will break. Until then, try and avoid the cost of a sort.
*/
sp_col_ind = new int[nzcnt];
sp_row_ind = new int[nzcnt];
int last=0;
for (j = 0; j < sp_numcols; ++j) {
const CoinShallowPackedVector& vec = mcol.getVector(sp_orig_col_ind[j]);
const int len = vec.getNumElements();
const int* ind = vec.getIndices();
if (ind[0] < ind[len-1]) {
for (i = 0; i < len; ++i) {
const int sp_row = clique[ind[i]];
if (sp_row >= 0) {
sp_col_ind[sp_col_start[j]++] = sp_row;
sp_row_ind[sp_row_start[sp_row]++] = j;
}
}
}
else {
for (i = len-1; i >= 0; --i) {
const int sp_row = clique[ind[i]];
if (sp_row >= 0) {
sp_col_ind[sp_col_start[j]++] = sp_row;
sp_row_ind[sp_row_start[sp_row]++] = j;
}
}
}
// sort
std::sort(sp_col_ind+last,sp_col_ind+sp_col_start[j]);
last=sp_col_start[j];
}
std::rotate(sp_col_start, sp_col_start+sp_numcols,
sp_col_start + (sp_numcols+1));
sp_col_start[0] = 0;
std::rotate(sp_row_start, sp_row_start+sp_numrows,
sp_row_start + (sp_numrows+1));
sp_row_start[0] = 0;
delete[] clique;
}
示例9: CoinMax
// This looks at solution and sets bounds to contain solution
void
CbcLink::feasibleRegion()
{
int j;
int firstNonZero=-1;
int lastNonZero = -1;
OsiSolverInterface * solver = model_->solver();
const double * solution = model_->testSolution();
const double * upper = solver->getColUpper();
double integerTolerance =
model_->getDblParam(CbcModel::CbcIntegerTolerance);
double weight = 0.0;
double sum =0.0;
int base=0;
for (j=0;j<numberMembers_;j++) {
for (int k=0;k<numberLinks_;k++) {
int iColumn = which_[base+k];
double value = CoinMax(0.0,solution[iColumn]);
sum += value;
if (value>integerTolerance&&upper[iColumn]) {
weight += weights_[j]*value;
if (firstNonZero<0)
firstNonZero=j;
lastNonZero=j;
}
}
base += numberLinks_;
}
#ifdef DISTANCE
if (lastNonZero-firstNonZero>sosType_-1) {
/* may still be satisfied.
For LOS type 2 we might wish to move coding around
and keep initial info in model_ for speed
*/
int iWhere;
bool possible=false;
for (iWhere=firstNonZero;iWhere<=lastNonZero;iWhere++) {
if (fabs(weight-weights_[iWhere])<1.0e-8) {
possible=true;
break;
}
}
if (possible) {
// One could move some of this (+ arrays) into model_
const CoinPackedMatrix * matrix = solver->getMatrixByCol();
const double * element = matrix->getMutableElements();
const int * row = matrix->getIndices();
const CoinBigIndex * columnStart = matrix->getVectorStarts();
const int * columnLength = matrix->getVectorLengths();
const double * rowSolution = solver->getRowActivity();
const double * rowLower = solver->getRowLower();
const double * rowUpper = solver->getRowUpper();
int numberRows = matrix->getNumRows();
double * array = new double [numberRows];
CoinZeroN(array,numberRows);
int * which = new int [numberRows];
int n=0;
int base=numberLinks_*firstNonZero;
for (j=firstNonZero;j<=lastNonZero;j++) {
for (int k=0;k<numberLinks_;k++) {
int iColumn = which_[base+k];
double value = CoinMax(0.0,solution[iColumn]);
if (value>integerTolerance&&upper[iColumn]) {
value = CoinMin(value,upper[iColumn]);
for (int j=columnStart[iColumn];j<columnStart[iColumn]+columnLength[iColumn];j++) {
int iRow = row[j];
double a = array[iRow];
if (a) {
a += value*element[j];
if (!a)
a = 1.0e-100;
} else {
which[n++]=iRow;
a=value*element[j];
assert (a);
}
array[iRow]=a;
}
}
}
base += numberLinks_;
}
base=numberLinks_*iWhere;
for (int k=0;k<numberLinks_;k++) {
int iColumn = which_[base+k];
const double value = 1.0;
for (int j=columnStart[iColumn];j<columnStart[iColumn]+columnLength[iColumn];j++) {
int iRow = row[j];
double a = array[iRow];
if (a) {
a -= value*element[j];
if (!a)
a = 1.0e-100;
} else {
which[n++]=iRow;
a=-value*element[j];
assert (a);
}
//.........这里部分代码省略.........
示例10: CoinError
// Infeasibility - large is 0.5
double
CbcLink::infeasibility(int & preferredWay) const
{
int j;
int firstNonZero=-1;
int lastNonZero = -1;
OsiSolverInterface * solver = model_->solver();
const double * solution = model_->testSolution();
//const double * lower = solver->getColLower();
const double * upper = solver->getColUpper();
double integerTolerance =
model_->getDblParam(CbcModel::CbcIntegerTolerance);
double weight = 0.0;
double sum =0.0;
// check bounds etc
double lastWeight=-1.0e100;
int base=0;
for (j=0;j<numberMembers_;j++) {
for (int k=0;k<numberLinks_;k++) {
int iColumn = which_[base+k];
//if (lower[iColumn])
//throw CoinError("Non zero lower bound in CBCLink","infeasibility","CbcLink");
if (lastWeight>=weights_[j]-1.0e-7)
throw CoinError("Weights too close together in CBCLink","infeasibility","CbcLink");
double value = CoinMax(0.0,solution[iColumn]);
sum += value;
if (value>integerTolerance&&upper[iColumn]) {
// Possibly due to scaling a fixed variable might slip through
if (value>upper[iColumn]+1.0e-8) {
// Could change to #ifdef CBC_DEBUG
#ifndef NDEBUG
if (model_->messageHandler()->logLevel()>1)
printf("** Variable %d (%d) has value %g and upper bound of %g\n",
iColumn,j,value,upper[iColumn]);
#endif
}
value = CoinMin(value,upper[iColumn]);
weight += weights_[j]*value;
if (firstNonZero<0)
firstNonZero=j;
lastNonZero=j;
}
}
base += numberLinks_;
}
double valueInfeasibility;
preferredWay=1;
if (lastNonZero-firstNonZero>=sosType_) {
// find where to branch
assert (sum>0.0);
weight /= sum;
valueInfeasibility = lastNonZero-firstNonZero+1;
valueInfeasibility *= 0.5/((double) numberMembers_);
//#define DISTANCE
#ifdef DISTANCE
assert (sosType_==1); // code up
/* may still be satisfied.
For LOS type 2 we might wish to move coding around
and keep initial info in model_ for speed
*/
int iWhere;
bool possible=false;
for (iWhere=firstNonZero;iWhere<=lastNonZero;iWhere++) {
if (fabs(weight-weights_[iWhere])<1.0e-8) {
possible=true;
break;
}
}
if (possible) {
// One could move some of this (+ arrays) into model_
const CoinPackedMatrix * matrix = solver->getMatrixByCol();
const double * element = matrix->getMutableElements();
const int * row = matrix->getIndices();
const CoinBigIndex * columnStart = matrix->getVectorStarts();
const int * columnLength = matrix->getVectorLengths();
const double * rowSolution = solver->getRowActivity();
const double * rowLower = solver->getRowLower();
const double * rowUpper = solver->getRowUpper();
int numberRows = matrix->getNumRows();
double * array = new double [numberRows];
CoinZeroN(array,numberRows);
int * which = new int [numberRows];
int n=0;
int base=numberLinks_*firstNonZero;
for (j=firstNonZero;j<=lastNonZero;j++) {
for (int k=0;k<numberLinks_;k++) {
int iColumn = which_[base+k];
double value = CoinMax(0.0,solution[iColumn]);
if (value>integerTolerance&&upper[iColumn]) {
value = CoinMin(value,upper[iColumn]);
for (int j=columnStart[iColumn];j<columnStart[iColumn]+columnLength[iColumn];j++) {
int iRow = row[j];
double a = array[iRow];
if (a) {
a += value*element[j];
if (!a)
a = 1.0e-100;
} else {
//.........这里部分代码省略.........
示例11: if
// Returns type
int
CbcFathomDynamicProgramming::checkPossible(int allowableSize)
{
algorithm_ = -1;
assert(model_->solver());
OsiSolverInterface * solver = model_->solver();
const CoinPackedMatrix * matrix = solver->getMatrixByCol();
int numberIntegers = model_->numberIntegers();
int numberColumns = solver->getNumCols();
size_ = 0;
if (numberIntegers != numberColumns)
return -1; // can't do dynamic programming
const double * lower = solver->getColLower();
const double * upper = solver->getColUpper();
const double * rowUpper = solver->getRowUpper();
int numberRows = model_->getNumRows();
int i;
// First check columns to see if possible
double * rhs = new double [numberRows];
CoinCopyN(rowUpper, numberRows, rhs);
// Column copy
const double * element = matrix->getElements();
const int * row = matrix->getIndices();
const CoinBigIndex * columnStart = matrix->getVectorStarts();
const int * columnLength = matrix->getVectorLengths();
bool bad = false;
/* It is just possible that we could say okay as
variables may get fixed but seems unlikely */
for (i = 0; i < numberColumns; i++) {
int j;
double lowerValue = lower[i];
assert (lowerValue == floor(lowerValue));
for (j = columnStart[i];
j < columnStart[i] + columnLength[i]; j++) {
int iRow = row[j];
double value = element[j];
if (upper[i] > lowerValue && (value <= 0.0 || value != floor(value)))
bad = true;
if (lowerValue)
rhs[iRow] -= lowerValue * value;
}
}
// check possible (at present do not allow covering)
int numberActive = 0;
bool infeasible = false;
bool saveBad = bad;
for (i = 0; i < numberRows; i++) {
if (rhs[i] < 0)
infeasible = true;
else if (rhs[i] > 1.0e5 || fabs(rhs[i] - floor(rhs[i] + 0.5)) > 1.0e-7)
bad = true;
else if (rhs[i] > 0.0)
numberActive++;
}
if (bad || infeasible) {
delete [] rhs;
if (!saveBad && infeasible)
return -2;
else
return -1;
}
// check size of array needed
double size = 1.0;
double check = COIN_INT_MAX;
for (i = 0; i < numberRows; i++) {
int n = static_cast<int> (floor(rhs[i] + 0.5));
if (n) {
n++; // allow for 0,1... n
if (numberActive != 1) {
// power of 2
int iBit = 0;
int k = n;
k &= ~1;
while (k) {
iBit++;
k &= ~(1 << iBit);
}
// See if exact power
if (n != (1 << iBit)) {
// round up to next power of 2
n = 1 << (iBit + 1);
}
size *= n;
if (size >= check)
break;
} else {
size = n; // just one constraint
}
}
}
// set size needed
if (size >= check)
size_ = COIN_INT_MAX;
else
//.........这里部分代码省略.........