//@{
template <class BinaryFunction> void
binaryOp(CoinPackedVector& retVal,
	 const CoinPackedVectorBase& op1, double value,
	 BinaryFunction bf)
{
   retVal.clear();
   const int s = op1.getNumElements();
   if (s > 0) {
      retVal.reserve(s);
      const int * inds = op1.getIndices();
      const double * elems = op1.getElements();
      for (int i=0; i<s; ++i ) {
	 retVal.insert(inds[i], bf(value, elems[i]));
      }
   }
}
template <class BinaryFunction> void
binaryOp(CoinPackedVector& retVal,
	 const CoinPackedVectorBase& op1, const CoinPackedVectorBase& op2,
	 BinaryFunction bf)
{
   retVal.clear();
   const int s1 = op1.getNumElements();
   const int s2 = op2.getNumElements();
/*
  Replaced || with &&, in response to complaint from Sven deVries, who
  rightly points out || is not appropriate for additive operations. &&
  should be ok as long as binaryOp is understood not to create something
  from nothing.		-- lh, 04.06.11
*/
   if (s1 == 0 && s2 == 0)
      return;

   retVal.reserve(s1+s2);

   const int * inds1 = op1.getIndices();
   const double * elems1 = op1.getElements();
   const int * inds2 = op2.getIndices();
   const double * elems2 = op2.getElements();

   int i;
   // loop once for each element in op1
   for ( i=0; i<s1; ++i ) {
      const int index = inds1[i];
      const int pos2 = op2.findIndex(index);
      const double val = bf(elems1[i], pos2 == -1 ? 0.0 : elems2[pos2]);
      // if (val != 0.0) // *THINK* : should we put in only nonzeros?
      retVal.insert(index, val);
   }
   // loop once for each element in operand2  
   for ( i=0; i<s2; ++i ) {
      const int index = inds2[i];
      // if index exists in op1, then element was processed in prior loop
      if ( op1.isExistingIndex(index) )
	 continue;
      // Index does not exist in op1, so the element value must be zero
      const double val = bf(0.0, elems2[i]);
      // if (val != 0.0) // *THINK* : should we put in only nonzeros?
      retVal.insert(index, val);
   }
}
Exemple #3
0
void OsiIF::_addRows(ArrayBuffer<Row*> &rows)
{
	CoinPackedVector *coinrow = new CoinPackedVector();

	for (int i = 0; i < rows.size(); i++) {
		coinrow->clear();
		for (int j = 0; j < rows[i]->nnz(); j++){
			coinrow->insert(rows[i]->support(j), rows[i]->coeff(j));
		}
		lpSolverTime_.start();
		osiLP_->addRow(*coinrow, csense2osi(rows[i]->sense()), rows[i]->rhs(), 0.0);
		lpSolverTime_.stop();
	}

	delete coinrow;
	lpSolverTime_.start();
	numRows_ = osiLP_->getNumRows();
	rhs_ = osiLP_->getRightHandSide();
	numCols_ = osiLP_->getNumCols();
	collower_ = osiLP_->getColLower();
	colupper_ = osiLP_->getColUpper();
	lpSolverTime_.stop();

}
Exemple #4
0
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);
}