本文整理汇总了C++中CoinPackedMatrix::assignMatrix方法的典型用法代码示例。如果您正苦于以下问题:C++ CoinPackedMatrix::assignMatrix方法的具体用法?C++ CoinPackedMatrix::assignMatrix怎么用?C++ CoinPackedMatrix::assignMatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CoinPackedMatrix
的用法示例。
在下文中一共展示了CoinPackedMatrix::assignMatrix方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
// no need as defaults to 0.0 double * columnLower = model2.columnLower();
double * columnUpper = model2.columnUpper();
double * objective = model2.objective();
double * rowLower = model2.rowLower();
double * rowUpper = model2.rowUpper();
// Columns - objective was packed
for (k = 0; k < 2; k++) {
int iColumn = objIndex[k];
objective[iColumn] = objValue[k];
}
for (k = 0; k < numberColumns; k++)
columnUpper[k] = upper[k];
// Rows
for (k = 0; k < numberRows; k++) {
rowLower[k] = 1.0;
rowUpper[k] = 1.0;
}
// Now elements
double row2Value[] = {1.0, -5.0, 1.0};
CoinBigIndex put = 0;
for (k = 0; k < numberColumns; k++) {
starts[k] = put;
lengths[k] = numberRows;
double value = row2Value[k];
for (int i = 0; i < numberRows; i++) {
rows[put] = i;
elements[put] = value;
put++;
}
}
starts[numberColumns] = put;
// assign to matrix
CoinPackedMatrix * matrix = new CoinPackedMatrix(true, 0.0, 0.0);
matrix->assignMatrix(true, numberRows, numberColumns, numberElements,
elements, rows, starts, lengths);
ClpPackedMatrix * clpMatrix = new ClpPackedMatrix(matrix);
model2.replaceMatrix(clpMatrix, true);
printf("Time for 10000 addRow using hand written code is %g\n", CoinCpuTime() - time1);
// If matrix is really big could switch off creation of row copy
// model2.setSpecialOptions(256);
}
model2.dual();
model2.writeMps("a.mps");
// Print column solution
int numberColumns = model.numberColumns();
// Alternatively getColSolution()
double * columnPrimal = model.primalColumnSolution();
// Alternatively getReducedCost()
double * columnDual = model.dualColumnSolution();
// Alternatively getColLower()
double * columnLower = model.columnLower();
// Alternatively getColUpper()
double * columnUpper = model.columnUpper();
// Alternatively getObjCoefficients()
double * columnObjective = model.objective();
int iColumn;
std::cout << " Primal Dual Lower Upper Cost"
<< std::endl;
for (iColumn = 0; iColumn < numberColumns; iColumn++) {
double value;
std::cout << std::setw(6) << iColumn << " ";
value = columnPrimal[iColumn];
示例2: CoinPackedMatrix
CbcLagrangeSolver::CbcLagrangeSolver(stochasticInput &input, int scenarioNumber, const vector<double> &lagrangeDiff) {
absgap = ratio = 0.;
nvar1 = input.nFirstStageVars();
int nvar2 = input.nSecondStageVars(scenarioNumber);
int ncons1 = input.nFirstStageCons();
int ncons2 = input.nSecondStageCons(scenarioNumber);
const CoinPackedMatrix &Amat = input.getFirstStageConstraints(),
&Tmat = input.getLinkingConstraints(scenarioNumber),
&Wmat = input.getSecondStageConstraints(scenarioNumber);
int totalVar = nvar1 + nvar2;
int totalCons = ncons1 + ncons2;
CoinBigIndex totalNnz = Amat.getNumElements() + Tmat.getNumElements() + Wmat.getNumElements();
// CoinPackedMatrix takes ownership of these, so we don't free them
CoinBigIndex *starts = new CoinBigIndex[totalVar+1];
double *elts = new double[totalNnz];
int *rowIdx = new int[totalNnz];
CoinBigIndex nnz = 0, start, end;
// put first-stage variables first, as is customary
int const *Aidx = Amat.getIndices();
double const *Aelts = Amat.getElements();
int const *Tidx = Tmat.getIndices();
double const *Telts = Tmat.getElements();
for (int c = 0; c < nvar1; c++) {
starts[c] = nnz;
start = Amat.getVectorFirst(c);
end = Amat.getVectorLast(c);
for (CoinBigIndex j = start; j < end; j++) {
elts[nnz] = Aelts[j];
rowIdx[nnz++] = Aidx[j];
}
start = Tmat.getVectorFirst(c);
end = Tmat.getVectorLast(c);
for (CoinBigIndex j = start; j < end; j++) {
elts[nnz] = Telts[j];
rowIdx[nnz++] = Tidx[j]+ncons1;
}
}
// now W blocks
int rowOffset = ncons1;
int colOffset = nvar1;
int const *Widx = Wmat.getIndices();
double const *Welts = Wmat.getElements();
for (int c = 0; c < nvar2; c++) {
starts[colOffset++] = nnz;
start = Wmat.getVectorFirst(c);
end = Wmat.getVectorLast(c);
for (CoinBigIndex j = start; j < end; j++) {
elts[nnz] = Welts[j];
rowIdx[nnz++] = Widx[j]+rowOffset;
}
}
starts[totalVar] = nnz;
assert(nnz == totalNnz);
CoinPackedMatrix *constr = new CoinPackedMatrix();
int *lens = 0;
constr->assignMatrix(true,totalCons,totalVar,totalNnz,elts,rowIdx,starts,lens);
constr->verifyMtx(); // debugging
// OsiClpSolverInterface takes ownership of these
double *collb = new double[totalVar];
double *colub = new double[totalVar];
double *obj = new double[totalVar];
double *rowlb = new double[totalCons];
double *rowub = new double[totalCons];
concatenateOne(input, collb, &stochasticInput::getFirstStageColLB,
&stochasticInput::getSecondStageColLB, scenarioNumber);
concatenateOne(input, colub, &stochasticInput::getFirstStageColUB,
&stochasticInput::getSecondStageColUB, scenarioNumber);
concatenateOne(input, obj, &stochasticInput::getFirstStageObj,
&stochasticInput::getSecondStageObj, scenarioNumber);
concatenateOne(input, rowlb, &stochasticInput::getFirstStageRowLB,
&stochasticInput::getSecondStageRowLB, scenarioNumber);
concatenateOne(input, rowub, &stochasticInput::getFirstStageRowUB,
&stochasticInput::getSecondStageRowUB, scenarioNumber);
// rescale first-stage objective
double scale = input.scenarioProbability(scenarioNumber);
assert(lagrangeDiff.size() == static_cast<unsigned>(nvar1));
for (int i = 0; i < nvar1; i++) {
obj[i] = scale*obj[i] + lagrangeDiff[i];
}
m.assignProblem(constr, collb, colub, obj, rowlb, rowub);
//.........这里部分代码省略.........
示例3: main
//.........这里部分代码省略.........
sscanf(temp, "%d %d %d %d", &iHead, &iTail, &iCost, &iUb);
iHead--;
iTail--;
head[numberColumns] = iHead;
tail[numberColumns] = iTail;
ub[numberColumns] = iUb;
cost[numberColumns] = iCost;
numberColumns++;
}
if (strncmp(temp, "DEMAND", 6)) {
fprintf(stderr, "Unable to find DEMAND\n");
exit(2);
}
// ***** Remember to convert to C notation
while (fgets(temp, 100, fp)) {
int row;
int value;
if (!strncmp(temp, "END", 3))
break;
sscanf(temp, "%d %d", &row, &value);
upper[row-1] = value;
lower[row-1] = value;
}
if (strncmp(temp, "END", 3)) {
fprintf(stderr, "Unable to find END\n");
exit(2);
}
printf("Problem %d has %d rows and %d columns\n", problem,
numberRows, numberColumns);
fclose(fp);
ClpSimplex model;
// now build model - we have rhs so build columns - two elements
// per column
double * objective = new double[numberColumns];
double * lowerColumn = new double[numberColumns];
double * upperColumn = new double[numberColumns];
double * element = new double [2*numberColumns];
int * start = new int[numberColumns+1];
int * row = new int[2*numberColumns];
start[numberColumns] = 2 * numberColumns;
for (i = 0; i < numberColumns; i++) {
start[i] = 2 * i;
element[2*i] = -1.0;
element[2*i+1] = 1.0;
row[2*i] = head[i];
row[2*i+1] = tail[i];
lowerColumn[i] = 0.0;
upperColumn[i] = ub[i];
objective[i] = cost[i];
}
// Create Packed Matrix
CoinPackedMatrix matrix;
int * lengths = NULL;
matrix.assignMatrix(true, numberRows, numberColumns,
2 * numberColumns, element, row, start, lengths);
ClpNetworkMatrix network(matrix);
// load model
model.loadProblem(network,
lowerColumn, upperColumn, objective,
lower, upper);
delete [] lower;
delete [] upper;
delete [] head;
delete [] tail;
delete [] ub;
delete [] cost;
delete [] objective;
delete [] lowerColumn;
delete [] upperColumn;
delete [] element;
delete [] start;
delete [] row;
/* The confusing flow below is in to exercise both dual and primal
when ClpNetworkMatrix storage used.
For practical use just one call e.g. model.dual(); would be used.
If network then factorization scheme is changed
to be much faster.
Still not as fast as a real network code, but more flexible
*/
model.factorization()->maximumPivots(200 + model.numberRows() / 100);
model.factorization()->maximumPivots(1000);
//model.factorization()->maximumPivots(1);
if (model.numberRows() < 50)
model.messageHandler()->setLogLevel(63);
model.dual();
model.setOptimizationDirection(-1);
//model.messageHandler()->setLogLevel(63);
model.primal();
model.setOptimizationDirection(1);
model.primal();
return 0;
}
示例4: dims
ClpBALPInterface::ClpBALPInterface(stochasticInput &input, BAContext &ctx, solveType t) : dims(input,ctx), t(t) {
if (ctx.nprocs() != 1) {
if (ctx.mype() == 0) {
printf("Warning, using CLP BALP interface with multiple processes\n");
}
}
// This disables Clp's internal rescaling of the problem.
// We do this for a fair comparison with PIPS-S.
// Uncomment for improved performance.
//model.scaling(0);
int const totalVar = dims.totalVars();
int const totalCons = dims.totalCons();
int const nScenarios = dims.numScenarios();
CoinBigIndex totalNnz = input.getFirstStageConstraints().getNumElements();
for (int scen = 0; scen < nScenarios; scen++) {
totalNnz += input.getSecondStageConstraints(scen).getNumElements();
totalNnz += input.getLinkingConstraints(scen).getNumElements();
}
// CoinPackedMatrix takes ownership of these, so we don't free them
CoinBigIndex *starts = new CoinBigIndex[totalVar+1];
double *elts = new double[totalNnz];
int *rowIdx = new int[totalNnz];
CoinBigIndex nnz = 0, start, end;
// put first-stage variables first, as is customary
CoinPackedMatrix const &Amat = input.getFirstStageConstraints();
int const *Aidx = Amat.getIndices();
double const *Aelts = Amat.getElements();
int const nFirstStageVars = input.nFirstStageVars();
int const nFirstStageCons = input.nFirstStageCons();
CoinPackedMatrix const &Tmat0 = input.getLinkingConstraints(0);
vector<const CoinPackedMatrix*> Tmats(nScenarios,&Tmat0);
if (!input.onlyBoundsVary()) {
for (int scen = 0; scen < nScenarios; scen++) {
Tmats[scen] = new CoinPackedMatrix(input.getLinkingConstraints(scen));
}
}
for (int c = 0; c < nFirstStageVars; c++) {
starts[c] = nnz;
start = Amat.getVectorFirst(c);
end = Amat.getVectorLast(c);
for (CoinBigIndex j = start; j < end; j++) {
elts[nnz] = Aelts[j];
rowIdx[nnz++] = Aidx[j];
}
int rowOffset = nFirstStageCons;
for (int scen = 0; scen < nScenarios; scen++) {
CoinPackedMatrix const &Tmat = *Tmats[scen];
int const *Tidx = Tmat.getIndices();
double const *Telts = Tmat.getElements();
start = Tmat.getVectorFirst(c);
end = Tmat.getVectorLast(c);
for (CoinBigIndex j = start; j < end; j++) {
elts[nnz] = Telts[j];
rowIdx[nnz++] = Tidx[j]+rowOffset;
}
rowOffset += input.nSecondStageCons(scen);
}
}
if (!input.onlyBoundsVary()) {
for (int scen = 0; scen < nScenarios; scen++) {
delete Tmats[scen];
}
}
// now W blocks
int rowOffset = nFirstStageCons;
int colOffset = nFirstStageVars;
for (int scen = 0; scen < nScenarios; scen++) {
int nSecondStageVars = input.nSecondStageVars(scen);
CoinPackedMatrix const &Wmat = input.getSecondStageConstraints(scen);
int const *Widx = Wmat.getIndices();
double const *Welts = Wmat.getElements();
for (int c = 0; c < nSecondStageVars; c++) {
starts[colOffset++] = nnz;
start = Wmat.getVectorFirst(c);
end = Wmat.getVectorLast(c);
for (CoinBigIndex j = start; j < end; j++) {
elts[nnz] = Welts[j];
rowIdx[nnz++] = Widx[j]+rowOffset;
}
}
rowOffset += input.nSecondStageCons(scen);
}
starts[totalVar] = nnz;
assert(nnz == totalNnz);
CoinPackedMatrix constr;
int *lens = 0;
constr.assignMatrix(true,totalCons,totalVar,totalNnz,
elts, rowIdx, starts, lens);
double *collb = new double[totalVar], *colub = new double[totalVar];
//.........这里部分代码省略.........