1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* $Id: addColumns.cpp 1662 2011-01-04 17:52:40Z lou $ */
// Copyright (C) 2005, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
// This is a simple example to create a model by column
#include "ClpSimplex.hpp"
#include "CoinHelperFunctions.hpp"
#include "CoinTime.hpp"
#include "CoinBuild.hpp"
#include "CoinModel.hpp"
#include <iomanip>
#include <cassert>
int main(int argc, const char *argv[])
{
{
// Empty model
ClpSimplex model;
// Bounds on rows - as dense vector
double lower[] = {2.0, 1.0};
double upper[] = {COIN_DBL_MAX, 1.0};
// Create space for 2 rows
model.resize(2, 0);
// Fill in
int i;
// Now row bounds
for (i = 0; i < 2; i++) {
model.setRowLower(i, lower[i]);
model.setRowUpper(i, upper[i]);
}
// Now add column 1
int column1Index[] = {0, 1};
double column1Value[] = {1.0, 1.0};
model.addColumn(2, column1Index, column1Value,
0.0, 2, 1.0);
// Now add column 2
int column2Index[] = {1};
double column2Value[] = { -5.0};
model.addColumn(1, column2Index, column2Value,
0.0, COIN_DBL_MAX, 0.0);
// Now add column 3
int column3Index[] = {0, 1};
double column3Value[] = {1.0, 1.0};
model.addColumn(2, column3Index, column3Value,
0.0, 4.0, 4.0);
// solve
model.dual();
/*
Adding one column at a time has a significant overhead so let's
try a more complicated but faster way
First time adding in 10000 columns one by one
*/
model.allSlackBasis();
ClpSimplex modelSave = model;
double time1 = CoinCpuTime();
int k;
for (k = 0; k < 10000; k++) {
int column2Index[] = {0, 1};
double column2Value[] = {1.0, -5.0};
model.addColumn(2, column2Index, column2Value,
0.0, 1.0, 10000.0);
}
printf("Time for 10000 addColumn is %g\n", CoinCpuTime() - time1);
model.dual();
model = modelSave;
// Now use build
CoinBuild buildObject;
time1 = CoinCpuTime();
for (k = 0; k < 100000; k++) {
int column2Index[] = {0, 1};
double column2Value[] = {1.0, -5.0};
buildObject.addColumn(2, column2Index, column2Value,
0.0, 1.0, 10000.0);
}
model.addColumns(buildObject);
printf("Time for 100000 addColumn using CoinBuild is %g\n", CoinCpuTime() - time1);
model.dual();
model = modelSave;
// Now use build +-1
int del[] = {0, 1, 2};
model.deleteColumns(3, del);
CoinBuild buildObject2;
time1 = CoinCpuTime();
for (k = 0; k < 10000; k++) {
int column2Index[] = {0, 1};
double column2Value[] = {1.0, 1.0, -1.0};
int bias = k & 1;
buildObject2.addColumn(2, column2Index, column2Value + bias,
0.0, 1.0, 10000.0);
}
model.addColumns(buildObject2, true);
printf("Time for 10000 addColumn using CoinBuild+-1 is %g\n", CoinCpuTime() - time1);
model.dual();
model = modelSave;
// Now use build +-1
model.deleteColumns(3, del);
CoinModel modelObject2;
time1 = CoinCpuTime();
for (k = 0; k < 10000; k++) {
int column2Index[] = {0, 1};
double column2Value[] = {1.0, 1.0, -1.0};
int bias = k & 1;
modelObject2.addColumn(2, column2Index, column2Value + bias,
0.0, 1.0, 10000.0);
}
model.addColumns(modelObject2, true);
printf("Time for 10000 addColumn using CoinModel+-1 is %g\n", CoinCpuTime() - time1);
//model.writeMps("xx.mps");
model.dual();
model = modelSave;
// Now use model
CoinModel modelObject;
time1 = CoinCpuTime();
for (k = 0; k < 100000; k++) {
int column2Index[] = {0, 1};
double column2Value[] = {1.0, -5.0};
modelObject.addColumn(2, column2Index, column2Value,
0.0, 1.0, 10000.0);
}
model.addColumns(modelObject);
printf("Time for 100000 addColumn using CoinModel is %g\n", CoinCpuTime() - time1);
model.dual();
// Print column solution Just first 3 columns
int numberColumns = model.numberColumns();
numberColumns = CoinMin(3, 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];
if (fabs(value) < 1.0e5)
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14) << value;
else
std::cout << std::setiosflags(std::ios::scientific) << std::setw(14) << value;
value = columnDual[iColumn];
if (fabs(value) < 1.0e5)
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14) << value;
else
std::cout << std::setiosflags(std::ios::scientific) << std::setw(14) << value;
value = columnLower[iColumn];
if (fabs(value) < 1.0e5)
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14) << value;
else
std::cout << std::setiosflags(std::ios::scientific) << std::setw(14) << value;
value = columnUpper[iColumn];
if (fabs(value) < 1.0e5)
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14) << value;
else
std::cout << std::setiosflags(std::ios::scientific) << std::setw(14) << value;
value = columnObjective[iColumn];
if (fabs(value) < 1.0e5)
std::cout << std::setiosflags(std::ios::fixed | std::ios::showpoint) << std::setw(14) << value;
else
std::cout << std::setiosflags(std::ios::scientific) << std::setw(14) << value;
std::cout << std::endl;
}
std::cout << "--------------------------------------" << std::endl;
}
{
// Now copy a model
ClpSimplex model;
int status;
if (argc < 2) {
#if defined(SAMPLEDIR)
status = model.readMps(SAMPLEDIR "/p0033.mps", true);
#else
fprintf(stderr, "Do not know where to find sample MPS files.\n");
exit(1);
#endif
} else
status = model.readMps(argv[1]);
if (status) {
printf("errors on input\n");
exit(77);
}
model.initialSolve();
int numberRows = model.numberRows();
int numberColumns = model.numberColumns();
const double * rowLower = model.rowLower();
const double * rowUpper = model.rowUpper();
// Start off model2
ClpSimplex model2;
model2.addRows(numberRows, rowLower, rowUpper, NULL);
// Build object
CoinBuild buildObject;
// Add columns
const double * columnLower = model.columnLower();
const double * columnUpper = model.columnUpper();
const double * objective = model.objective();
CoinPackedMatrix * matrix = model.matrix();
const int * row = matrix->getIndices();
const int * columnLength = matrix->getVectorLengths();
const CoinBigIndex * columnStart = matrix->getVectorStarts();
const double * elementByColumn = matrix->getElements();
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
CoinBigIndex start = columnStart[iColumn];
buildObject.addColumn(columnLength[iColumn], row + start, elementByColumn + start,
columnLower[iColumn], columnUpper[iColumn],
objective[iColumn]);
}
// add in
model2.addColumns(buildObject);
model2.initialSolve();
}
{
// and again
ClpSimplex model;
int status;
if (argc < 2) {
#if defined(SAMPLEDIR)
status = model.readMps(SAMPLEDIR "/p0033.mps", true);
#else
fprintf(stderr, "Do not know where to find sample MPS files.\n");
exit(1);
#endif
} else
status = model.readMps(argv[1]);
if (status) {
printf("errors on input\n");
exit(77);
}
model.initialSolve();
int numberRows = model.numberRows();
int numberColumns = model.numberColumns();
const double * rowLower = model.rowLower();
const double * rowUpper = model.rowUpper();
// Build object
CoinModel buildObject;
for (int iRow = 0; iRow < numberRows; iRow++)
buildObject.setRowBounds(iRow, rowLower[iRow], rowUpper[iRow]);
// Add columns
const double * columnLower = model.columnLower();
const double * columnUpper = model.columnUpper();
const double * objective = model.objective();
CoinPackedMatrix * matrix = model.matrix();
const int * row = matrix->getIndices();
const int * columnLength = matrix->getVectorLengths();
const CoinBigIndex * columnStart = matrix->getVectorStarts();
const double * elementByColumn = matrix->getElements();
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
CoinBigIndex start = columnStart[iColumn];
buildObject.addColumn(columnLength[iColumn], row + start, elementByColumn + start,
columnLower[iColumn], columnUpper[iColumn],
objective[iColumn]);
}
// add in
ClpSimplex model2;
model2.loadProblem(buildObject);
model2.initialSolve();
}
return 0;
}
|