本文整理汇总了C++中CoinPackedMatrix::setDimensions方法的典型用法代码示例。如果您正苦于以下问题:C++ CoinPackedMatrix::setDimensions方法的具体用法?C++ CoinPackedMatrix::setDimensions怎么用?C++ CoinPackedMatrix::setDimensions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CoinPackedMatrix
的用法示例。
在下文中一共展示了CoinPackedMatrix::setDimensions方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: loadDummyRow
void OsiIF::loadDummyRow(OsiSolverInterface* s2, const double* lbounds, const double* ubounds, const double* objectives)
{
CoinPackedVector *coinrow = new CoinPackedVector();
CoinPackedMatrix *matrix = new CoinPackedMatrix(false,0,0);
matrix->setDimensions(0, numCols_);
ArrayBuffer<int> dummy(1,false);
dummy.push(0);
char *senses = new char[1];
double *rhs = new double[1];
double *ranges = new double[1];
coinrow->insert(0, 1.);
matrix->appendRow(*coinrow);
senses[0] = 'E';
rhs[0] = 1.;
ranges[0] = 0.;
lpSolverTime_.start();
s2->loadProblem(*matrix, lbounds, ubounds, objectives, senses, rhs, ranges);
lpSolverTime_.stop();
_remRows(dummy);
delete coinrow;
delete matrix;
freeChar(senses);
freeDouble(rhs);
freeDouble(ranges);
return;
}
示例2: assert
bool OSI_X_SolverWrapper<SOLVERINTERFACE>::setup(const LP_Constraints & cstraints) //cstraints <-> constraints
{
bool bOk = true;
if ( si == NULL )
{
return false;
}
assert(_nbParams == cstraints._nbParams);
int NUMVAR = cstraints._constraintMat.cols();
int NUMCON = cstraints._constraintMat.rows();
int NUMANZ = cstraints._constraintMat.cols() * cstraints._constraintMat.rows(); //DENSE MATRIX
std::vector<double> col_lb(NUMVAR);//the column lower bounds
std::vector<double> col_ub(NUMVAR);//the column upper bounds
this->_nbParams = NUMVAR;
if (cstraints._bminimize)
{
si->setObjSense( 1 );
}
else
{
si->setObjSense( -1 );
}
const Mat & A = cstraints._constraintMat;
//Equality constraint will be handked by two constraintsdue to the API limitation.
size_t nbLine = A.rows() + std::count(cstraints._vec_sign.begin(), cstraints._vec_sign.end(), EQ);
std::vector<double> row_lb(nbLine);//the row lower bounds
std::vector<double> row_ub(nbLine);//the row upper bounds
CoinPackedMatrix * matrix = new CoinPackedMatrix(false,0,0);
matrix->setDimensions(0, NUMVAR);
//-- Add row-wise constraint
size_t indexRow = 0;
for (int i=0; i < A.rows(); ++i)
{
Vec temp = A.row(i);
CoinPackedVector row;
if ( cstraints._vec_sign[i] == EQ || cstraints._vec_sign[i] == LE )
{
int coef = 1;
for ( int j = 0; j < A.cols() ; j++ )
{
row.insert(j, coef * temp.data()[j]);
}
row_lb[indexRow] = -1.0 * si->getInfinity();
row_ub[indexRow] = coef * cstraints._Cst_objective(i);
matrix->appendRow(row);
indexRow++;
}
if ( cstraints._vec_sign[i] == EQ || cstraints._vec_sign[i] == GE )
{
int coef = -1;
for ( int j = 0; j < A.cols() ; j++ )
{
row.insert(j, coef * temp.data()[j]);
}
row_lb[indexRow] = -1.0 * si->getInfinity();
row_ub[indexRow] = coef * cstraints._Cst_objective(i);
matrix->appendRow(row);
indexRow++;
}
}
//-- Setup bounds
if (cstraints._vec_bounds.size() == 1)
{
// Setup the same bound for all the parameter
for (int i=0; i < this->_nbParams; ++i)
{
col_lb[i] = cstraints._vec_bounds[0].first;
col_ub[i] = cstraints._vec_bounds[0].second;
}
}
else
{
for (int i=0; i < this->_nbParams; ++i)
{
col_lb[i] = cstraints._vec_bounds[i].first;
col_ub[i] = cstraints._vec_bounds[i].second;
}
}
si->loadProblem(*matrix, &col_lb[0], &col_ub[0], cstraints._vec_cost.empty() ? NULL : &cstraints._vec_cost[0], &row_lb[0], &row_ub[0] );
delete matrix;
return bOk;
}
示例3: CoinPackedMatrix
int
main(void)
{
// Create a problem pointer. We use the base class here.
OsiSolverInterface *si;
// When we instantiate the object, we need a specific derived class.
si = new OSIXXX;
// Build our own instance from scratch
/*
* This section adapted from Matt Galati's example
* on the COIN-OR Tutorial website.
*
* Problem from Bertsimas, Tsitsiklis page 21
*
* optimal solution: x* = (1,1)
*
* minimize -1 x0 - 1 x1
* s.t 1 x0 + 2 x1 <= 3
* 2 x0 + 1 x1 <= 3
* x0 >= 0
* x1 >= 0
*/
int n_cols = 2;
double *objective = new double[n_cols];//the objective coefficients
double *col_lb = new double[n_cols];//the column lower bounds
double *col_ub = new double[n_cols];//the column upper bounds
//Define the objective coefficients.
//minimize -1 x0 - 1 x1
objective[0] = -1.0;
objective[1] = -1.0;
//Define the variable lower/upper bounds.
// x0 >= 0 => 0 <= x0 <= infinity
// x1 >= 0 => 0 <= x1 <= infinity
col_lb[0] = 0.0;
col_lb[1] = 0.0;
col_ub[0] = si->getInfinity();
col_ub[1] = si->getInfinity();
int n_rows = 2;
double *row_lb = new double[n_rows]; //the row lower bounds
double *row_ub = new double[n_rows]; //the row upper bounds
//Define the constraint matrix.
CoinPackedMatrix *matrix = new CoinPackedMatrix(false,0,0);
matrix->setDimensions(0, n_cols);
//1 x0 + 2 x1 <= 3 => -infinity <= 1 x0 + 2 x2 <= 3
CoinPackedVector row1;
row1.insert(0, 1.0);
row1.insert(1, 2.0);
row_lb[0] = -1.0 * si->getInfinity();
row_ub[0] = 3.0;
matrix->appendRow(row1);
//2 x0 + 1 x1 <= 3 => -infinity <= 2 x0 + 1 x1 <= 3
CoinPackedVector row2;
row2.insert(0, 2.0);
row2.insert(1, 1.0);
row_lb[1] = -1.0 * si->getInfinity();
row_ub[1] = 3.0;
matrix->appendRow(row2);
//load the problem to OSI
si->loadProblem(*matrix, col_lb, col_ub, objective, row_lb, row_ub);
//write the MPS file to a file called example.mps
si->writeMps("example");
// Solve the (relaxation of the) problem
si->initialSolve();
// Check the solution
if ( si->isProvenOptimal() ) {
std::cout << "Found optimal solution!" << std::endl;
std::cout << "Objective value is " << si->getObjValue() << std::endl;
int n = si->getNumCols();
const double *solution;
solution = si->getColSolution();
// We could then print the solution or examine it.
} else {
std::cout << "Didn't find optimal solution." << std::endl;
// Could then check other status functions.
}
return 0;
}
示例4: etol
//.........这里部分代码省略.........
}else if (feasCheckSolver == "CPLEX"){
#ifdef USE_CPLEX
nSolver = new OsiCpxSolverInterface();
#else
throw CoinError("CPLEX chosen as solver, but it has not been enabled",
"setUpModel", "MibsBilevel");
#endif
}else{
throw CoinError("Unknown solver chosen",
"setUpModel", "MibsBilevel");
}
int * integerVars = new int[lCols];
double * objCoeffs = new double[lCols];
CoinFillN(integerVars, lCols, 0);
//CoinZeroN(objCoeffs, lCols);
int intCnt(0);
/** Fill in array of lower-level integer variables **/
for(i = 0; i < lCols; i++){
index1 = lColIndices[i];
if(oSolver->isInteger(index1)){
integerVars[intCnt] = i;
intCnt++;
}
}
CoinDisjointCopyN(lObjCoeffs, lCols, objCoeffs);
CoinPackedMatrix * newMat = new CoinPackedMatrix(false, 0, 0);
newMat->setDimensions(0, lCols);
double tmp(0.0);
/*
for(i = 0; i < lRows; i++){
CoinPackedVector row;
index1 = lRowIndices[i];
start = matStarts[index1];
end = start + matrix->getVectorSize(index1);
for(j = start; j < end; j++){
index2 = matIndices[j];
//tmp = findIndex(index, lCols, lColIndices);
tmp = binarySearch(0, lCols - 1, index2, lColIndices);
if(tmp > -1)
row.insert(tmp, matElements[j]);
}
newMat->appendRow(row);
}
*/
for(i = 0; i < lRows; i++){
CoinPackedVector row;
index1 = lRowIndices[i];
for(j = 0; j < lCols; j++){
index2 = lColIndices[j];
tmp = matrix->getCoefficient(index1, index2);
row.insert(j, tmp);
}
newMat->appendRow(row);
}
/*
nSolver->assignProblem(newMat, colLb, colUb,
objCoeffs, rowLb, rowUb);
示例5: phaseDone
//===========================================================================//
void DecompAlgoRC::phaseDone()
{
//take the current set of variables and solve DW master to get primal
//TODO: right now, creating from scratch each time -- really need
// to append and warm start - esp if doing alot of branching
//delete m_masterSI;
//m_masterSI = NULL;
//m_masterSI = new OsiLpSolverInterface();
//CoinAssertHint(m_masterSI, "Error: Out of Memory");
//m_masterSI->messageHandler()->setLogLevel(m_param.LogLpLevel);
DecompConstraintSet* modelCore = m_modelCore.getModel();
#if 0
//---
//--- Initialize the solver interface for the master problem.
//--- PC: min c(s lam)
//--- A''s lam >= b'',
//--- sum{s} lam_s = 1 ,
//--- lam_s >= 0 , s in F'[0]
//--- modelCore contains [A'', b''], from the original model in
//--- terms of x. In this function we create the DW-LP in terms of
//--- lambda, [[A''s, 1], [b'', 1]] and load that into the OSI
//--- interface m_masterSI.
//---
//--- NOTE: if 0 is feasible to subproblem, we can relax convexity to <= 1
//---
UtilPrintFuncBegin(m_osLog, m_classTag,
"phaseDone()", m_param.LogDebugLevel, 2);
CoinAssert(m_vars.size() > 0);
int nColsCore = modelCore->getNumCols();
int nRowsCore = modelCore->getNumRows();
modelCore->nBaseRowsOrig = modelCore->nBaseRows;
modelCore->nBaseRows = nRowsCore;
//THINK? should initVars be in pool and do addVarsFromPool here?
CoinPackedMatrix* M = new CoinPackedMatrix(true, 0, 0);
M->setDimensions(nRowsCore + 1, 0);
const int n_cols = static_cast<int>(m_vars.size());
double* colLB = new double[n_cols];
double* colUB = new double[n_cols];
double* obj = new double[n_cols];
double* denseCol = new double[nRowsCore + 1];
CoinAssertHint(colLB && colUB && obj && denseCol, "Error: Out of Memory");
int col_index = 0;
DecompVarList::iterator li;
for (li = m_vars.begin(); li != m_vars.end(); li++) {
UTIL_DEBUG(m_param.LogDebugLevel, 5,
(*li)->print(m_osLog, m_app);
);
//---
//--- get dense column = A''s, append convexity constraint on end
//---
modelCore->M->times((*li)->m_s, denseCol);
denseCol[nRowsCore] = 1.0;
//---
//--- create a sparse column from the dense column
//---
// THINK: do i need a DecompCol?
// THINK: does this allocate memory for coinpackedvec twice?
CoinPackedVector* sparseCol
= UtilPackedVectorFromDense(nRowsCore + 1,
denseCol, m_param.TolZero);
UTIL_DEBUG(m_param.LogDebugLevel, 5,
(*m_osLog) << "\nSparse Col: \n";
UtilPrintPackedVector(*sparseCol, m_osLog);
);
示例6: _initialize
void OsiIF::_initialize(
OptSense sense,
int nRow,
int maxRow,
int nCol,
int maxCol,
Array<double> &obj,
Array<double> &lBound,
Array<double> &uBound,
Array<Row*> &rows)
{
osiLP_ = getDefaultInterface();
currentSolverType_ = Exact;
// switch off output from the solver
// can be reset in setSolverParameters
osiLP_->setHintParam(OsiDoReducePrint, true, OsiHintDo);
osiLP_->messageHandler()->setLogLevel(0);
master_->setSolverParameters(osiLP_, currentSolverType() == Approx);
numRows_ = nRow;
numCols_ = nCol;
double *lbounds = new double[numCols_];
double *ubounds = new double[numCols_];
double *objectives = new double[numCols_];
CoinPackedVector *coinrow = new CoinPackedVector();
CoinPackedMatrix *matrix = new CoinPackedMatrix(false,0,0);
matrix->setDimensions(0, numCols_);
for (int i = 0; i < numCols_; i++){
lbounds[i] = lBound[i];
ubounds[i] = uBound[i];
objectives[i] = obj[i];
}
if (currentSolverType() == Exact && numRows_ == 0 && master_->defaultLpSolver() == Master::CPLEX) {
loadDummyRow(osiLP_, lbounds, ubounds, objectives);
}
else {
char *senses = new char[numRows_];
double *rhs = new double[numRows_];
double *ranges = new double[numRows_];
for (int i = 0; i < numRows_; i++){
coinrow->clear();
for (int j = 0; j < rows[i]->nnz(); j++){
coinrow->insert(rows[i]->support(j), rows[i]->coeff(j));
}
matrix->appendRow(*coinrow);
senses[i] = csense2osi(rows[i]->sense());
rhs[i] = rows[i]->rhs();
ranges[i] = 0.0;
}
lpSolverTime_.start();
osiLP_->loadProblem(*matrix, lbounds, ubounds, objectives, senses, rhs, ranges);
lpSolverTime_.stop();
freeChar(senses);
freeDouble(rhs);
freeDouble(ranges);
}
// set the sense of the optimization
_sense(sense);
// get the pointers to the solution, reduced costs etc.
lpSolverTime_.start();
numRows_ = osiLP_->getNumRows();
numCols_ = osiLP_->getNumCols();
rhs_ = osiLP_->getRightHandSide();
rowsense_ = osiLP_->getRowSense();
colupper_ = osiLP_->getColUpper();
collower_ = osiLP_->getColLower();
objcoeff_ = osiLP_->getObjCoefficients();
if( ws_ != nullptr )
delete ws_;
//ws_ = dynamic_cast<CoinWarmStartBasis *>(osiLP_->getWarmStart());
ws_=nullptr;
xValStatus_ = recoStatus_ = yValStatus_ = slackStatus_ = basisStatus_ = Missing;
lpSolverTime_.stop();
delete coinrow;
delete matrix;
freeDouble(lbounds);
freeDouble(ubounds);
freeDouble(objectives);
}
示例7: if
int
SmiSmpsIO::readStochFile(SmiScnModel *smi,SmiCoreData *core, const char *c, const char *ext)
{
CoinFileInput *input = 0;
int returnCode = dealWithFileName(c,ext,input);
if (returnCode<0) {
return -1;
} else if (returnCode>0) {
delete smpsCardReader_;
smpsCardReader_ = new SmiSmpsCardReader ( input, this);
if(combineRuleSet)
smpsCardReader_->setCoreCombineRule(combineRule_);
}
smpsCardReader_->readToNextSection();
if ( smpsCardReader_->whichSection ( ) == COIN_NAME_SECTION ) {
ifstoch = true;
// check problem name and issue warning if needed
if (strcmp(problemName_,smpsCardReader_->columnName()))
{
printf("Warning: Time file name %s does not match problem file name %s\n",
smpsCardReader_->columnName(),problemName_);
}
} else if ( smpsCardReader_->whichSection ( ) == COIN_UNKNOWN_SECTION ) {
handler_->message(COIN_MPS_BADFILE1,messages_)<<smpsCardReader_->card()
<<1
<<fileName_
<<CoinMessageEol;
#if 0 //FIXME: SmiSmpsCardReader has no member filePointer(), also none in CoinMpsCardReader
if (CoinFileInput::haveGzipSupport() && !smpsCardReader_->filePointer())
handler_->message(COIN_MPS_BADFILE2,messages_)<<CoinMessageEol;
#endif
return -2;
} else if ( smpsCardReader_->whichSection ( ) != COIN_EOF_SECTION ) {
// save name of section
free(problemName_);
problemName_=strdup(smpsCardReader_->card());
} else {
handler_->message(COIN_MPS_EOF,messages_)<<fileName_
<<CoinMessageEol;
return -3;
}
if (ifstoch)
{
switch( smpsCardReader_->nextSmpsField() )
{
case SMI_INDEPENDENT_SECTION: // INDEPENDENT card
{
printf("Processing INDEPENDENT section\n");
// Create discrete distribution
SmiDiscreteDistribution *smiDD = new SmiDiscreteDistribution(core);
SmiDiscreteRV *smiRV = NULL;
CoinPackedVector drlo,drup,dclo,dcup,dobj;
CoinPackedMatrix *matrix = new CoinPackedMatrix(false,0.25,0.25);
assert(!matrix->isColOrdered());
int nrow = core->getNumRows();
int ncol = core->getNumCols();
matrix->setDimensions(nrow,ncol);
int oldi=-1;
int oldj=-1;
while( smpsCardReader_->nextSmpsField ( ) == SMI_INDEPENDENT_SECTION )
{
switch (smpsCardReader_->whichSmpsType())
{
case SMI_BL_CARD:
{
//this is a block
// not handled yet
return -2;
}
case SMI_COLUMN_CARD:
{
int j=this->columnIndex(smpsCardReader_->columnName());
int i=this->rowIndex(smpsCardReader_->rowName());
double value = smpsCardReader_->value();
if (j<0) // check RHS
{
if (!strcmp(this->getRhsName(),"")) {
free(rhsName_);
rhsName_=strdup(smpsCardReader_->columnName());
} else
assert(!strcmp(smpsCardReader_->columnName(),this->getRhsName()));
assert(!(i<0));
char c=this->getRowSense()[i];
switch(c)
{
//.........这里部分代码省略.........