static ClpSimplex * clpmodel(EKKModel * model, int startup) { ClpSimplex * clp = new ClpSimplex();; int numberRows = ekk_getInumrows(model); int numberColumns = ekk_getInumcols(model); clp->loadProblem(numberColumns, numberRows, ekk_blockColumn(model, 0), ekk_blockRow(model, 0), ekk_blockElement(model, 0), ekk_collower(model), ekk_colupper(model), ekk_objective(model), ekk_rowlower(model), ekk_rowupper(model)); clp->setOptimizationDirection((int) ekk_getRmaxmin(model)); clp->setPrimalTolerance(ekk_getRtolpinf(model)); if (ekk_getRpweight(model) != 0.1) clp->setInfeasibilityCost(1.0 / ekk_getRpweight(model)); clp->setDualTolerance(ekk_getRtoldinf(model)); if (ekk_getRdweight(model) != 0.1) clp->setDualBound(1.0 / ekk_getRdweight(model)); clp->setDblParam(ClpObjOffset, ekk_getRobjectiveOffset(model)); const int * rowStatus = ekk_rowstat(model); const double * rowSolution = ekk_rowacts(model); int i; clp->createStatus(); double * clpSolution; clpSolution = clp->primalRowSolution(); memcpy(clpSolution, rowSolution, numberRows * sizeof(double)); const double * rowLower = ekk_rowlower(model); const double * rowUpper = ekk_rowupper(model); for (i = 0; i < numberRows; i++) { ClpSimplex::Status status; if ((rowStatus[i] & 0x80000000) != 0) { status = ClpSimplex::basic; } else { if (!startup) { // believe bits int ikey = rowStatus[i] & 0x60000000; if (ikey == 0x40000000) { // at ub status = ClpSimplex::atUpperBound; clpSolution[i] = rowUpper[i]; } else if (ikey == 0x20000000) { // at lb status = ClpSimplex::atLowerBound; clpSolution[i] = rowLower[i]; } else if (ikey == 0x60000000) { // free status = ClpSimplex::isFree; clpSolution[i] = 0.0; } else { // fixed status = ClpSimplex::atLowerBound; clpSolution[i] = rowLower[i]; } } else { status = ClpSimplex::superBasic; } } clp->setRowStatus(i, status); } const int * columnStatus = ekk_colstat(model); const double * columnSolution = ekk_colsol(model); clpSolution = clp->primalColumnSolution(); memcpy(clpSolution, columnSolution, numberColumns * sizeof(double)); const double * columnLower = ekk_collower(model); const double * columnUpper = ekk_colupper(model); for (i = 0; i < numberColumns; i++) { ClpSimplex::Status status; if ((columnStatus[i] & 0x80000000) != 0) { status = ClpSimplex::basic; } else { if (!startup) { // believe bits int ikey = columnStatus[i] & 0x60000000; if (ikey == 0x40000000) { // at ub status = ClpSimplex::atUpperBound; clpSolution[i] = columnUpper[i]; } else if (ikey == 0x20000000) { // at lb status = ClpSimplex::atLowerBound; clpSolution[i] = columnLower[i]; } else if (ikey == 0x60000000) { // free status = ClpSimplex::isFree; clpSolution[i] = 0.0; } else { // fixed status = ClpSimplex::atLowerBound; clpSolution[i] = columnLower[i]; } } else { status = ClpSimplex::superBasic; } } clp->setColumnStatus(i, status); } return clp; }
int main (int argc, const char *argv[]) { const char * name; // problem is actually scaled for osl, dynamically for clp (slows clp) // default is primal, no presolve, minimise and use clp bool primal = true, presolve = false; int useosl = 0; bool freeFormat = false; bool exportIt = false; EKKModel * model; EKKContext * context; if ( argc > 1 ) { name = argv[1]; } else { name = "../../Data/Sample/p0033.mps"; } /* initialize OSL environment */ context = ekk_initializeContext(); model = ekk_newModel(context, ""); int i; printf("*** Options "); for (i = 2; i < argc; i++) { printf("%s ", argv[i]); } printf("\n"); // see if free format needed for (i = 2; i < argc; i++) { if (!strncmp(argv[i], "free", 4)) { freeFormat = true; } } // create model from MPS file if (!freeFormat) { ekk_importModel(model, name); } else { ekk_importModelFree(model, name); } // other options for (i = 2; i < argc; i++) { if (!strncmp(argv[i], "max", 3)) { if (!strncmp(argv[i], "max2", 4)) { // This is for testing - it just reverses signs and maximizes int i, n = ekk_getInumcols(model); double * objective = ekk_getObjective(model); for (i = 0; i < n; i++) { objective[i] = -objective[i]; } ekk_setObjective(model, objective); ekk_setMaximize(model); } else { // maximize ekk_setMaximize(model); } } if (!strncmp(argv[i], "dual", 4)) { primal = false; } if (!strncmp(argv[i], "presol", 6)) { presolve = true; } if (!strncmp(argv[i], "osl", 3)) { useosl = 1; } if (!strncmp(argv[i], "both", 4)) { useosl = 2; } if (!strncmp(argv[i], "export", 6)) { exportIt = true; } } if (useosl) { // OSL if (presolve) ekk_preSolve(model, 3, NULL); ekk_scale(model); if (primal) ekk_primalSimplex(model, 1); else ekk_dualSimplex(model); if (presolve) { ekk_postSolve(model, NULL); ekk_primalSimplex(model, 3); } if (useosl == 2) ekk_allSlackBasis(model); // otherwise it would be easy } if ((useosl & 2) == 0) { // CLP if (presolve) ekk_preSolveClp(model, true, 5); /* 3 is because it is ignored if no presolve, and we are forcing Clp to re-optimize */ if (primal) ekk_primalClp(model, 1, 3); else ekk_dualClp(model, 3); } if (exportIt) { ClpSimplex * clp = new ClpSimplex();; int numberRows = ekk_getInumrows(model); int numberColumns = ekk_getInumcols(model); clp->loadProblem(numberColumns, numberRows, ekk_blockColumn(model, 0), ekk_blockRow(model, 0), ekk_blockElement(model, 0), ekk_collower(model), ekk_colupper(model), ekk_objective(model), ekk_rowlower(model), ekk_rowupper(model)); // Do integer stuff int * which = ekk_listOfIntegers(model); int numberIntegers = ekk_getInumints(model); for (int i = 0; i < numberIntegers; i++) clp->setInteger(which[i]); ekk_free(which); clp->writeMps("try1.mps"); delete clp; } ekk_deleteModel(model); ekk_endContext(context); return 0; }