static int populatebycolumn (CPXENVptr env, CPXLPptr lp) { int status = 0; double obj[NUMCOLS]; double lb[NUMCOLS]; double ub[NUMCOLS]; char *colname[NUMCOLS]; int matbeg[NUMCOLS]; int matind[NUMNZ]; double matval[NUMNZ]; double rhs[NUMROWS]; char sense[NUMROWS]; char *rowname[NUMROWS]; /* To build the problem by column, create the rows, and then add the columns. */ status = CPXchgobjsen (env, lp, CPX_MAX); /* Problem is maximization */ if ( status ) goto TERMINATE; /* Now create the new rows. First, populate the arrays. */ rowname[0] = "c1"; sense[0] = 'L'; rhs[0] = 20.0; rowname[1] = "c2"; sense[1] = 'L'; rhs[1] = 30.0; status = CPXnewrows (env, lp, NUMROWS, rhs, sense, NULL, rowname); if ( status ) goto TERMINATE; /* Now add the new columns. First, populate the arrays. */ obj[0] = 1.0; obj[1] = 2.0; obj[2] = 3.0; matbeg[0] = 0; matbeg[1] = 2; matbeg[2] = 4; matind[0] = 0; matind[2] = 0; matind[4] = 0; matval[0] = -1.0; matval[2] = 1.0; matval[4] = 1.0; matind[1] = 1; matind[3] = 1; matind[5] = 1; matval[1] = 1.0; matval[3] = -3.0; matval[5] = 1.0; lb[0] = 0.0; lb[1] = 0.0; lb[2] = 0.0; ub[0] = 40.0; ub[1] = CPX_INFBOUND; ub[2] = CPX_INFBOUND; colname[0] = "x1"; colname[1] = "x2"; colname[2] = "x3"; status = CPXaddcols (env, lp, NUMCOLS, NUMNZ, obj, matbeg, matind, matval, lb, ub, colname); if ( status ) goto TERMINATE; TERMINATE: return (status); } /* END populatebycolumn */
long GenModelCplex::AddCol(int* newi, double* newcol, int nz, double obj, double lb, double ub, const char* name, char type) { if(!bcreated) return ThrowError("AddCol() not available : Problem not created yet"); CplexData* d = (CplexData*)solverdata; int cmatbeg = 0; double clb = lb; if(clb == numeric_limits<double>::infinity()) clb = CPX_INFBOUND; else if(clb == -numeric_limits<double>::infinity()) clb = -CPX_INFBOUND; double cub = ub; if(cub == numeric_limits<double>::infinity()) cub = CPX_INFBOUND; else if(cub == -numeric_limits<double>::infinity()) cub = -CPX_INFBOUND; CPXaddcols(d->env, d->lp, 1, nz, &obj, &cmatbeg, newi, newcol, &clb, &cub, (char**)(&name)); if(type != 'C') { int cind = d->nc; CPXchgctype(d->env, d->lp, 1, &cind, &type); } d->nc++; return 0; }
void CplexSolver::add(ColumnBuffer & buffer) { buffer.add_last_begin(); if (buffer.name().empty()) { CPXaddcols(_env, _prob, buffer.size(), buffer.nz(), buffer.rhsObj(), buffer.begin(), buffer.index(), buffer.value(), buffer.lower(), buffer.upper(), NULL); } else { assert((int )buffer.name().size() == buffer.size() && "you should provide a name for each element"); std::vector<char*> cpxName(buffer.name().size()); for (int i(0); i < buffer.name().size(); ++i) { cpxName[i] = const_cast<char*>(buffer.name()[i].c_str()); } CPXaddcols(_env, _prob, buffer.size(), buffer.nz(), buffer.rhsObj(), buffer.begin(), buffer.index(), buffer.value(), buffer.lower(), buffer.upper(), cpxName.data()); } buffer.rem_last_begin(); if (!buffer.only_continous()) { _is_mip = true; std::vector<int> sequence(buffer.size()); for (int i(0); i < buffer.size(); ++i) { sequence[i] = CPXgetnumcols(_env, _prob) - buffer.size() + i; } CPXchgctype(_env, _prob, buffer.size(), sequence.data(), buffer.type()); } }
static int populatebycolumn (CPXENVptr env, CPXLPptr lp, int nfoods, double *cost, double *lb, double *ub, int nnutr, double *nutrmin, double *nutrmax, double **nutrper) { int status = 0; int i, j; int zero = 0; int *ind = NULL; double *val = NULL; char *sense = NULL; double *rngval = NULL; sense = (char*)malloc(nnutr * sizeof(char)); if ( sense == NULL ) { status = CPXERR_NO_MEMORY; goto TERMINATE; } for (i = 0; i < nnutr; i++) { sense[i] = 'R'; } val = (double*)malloc(nnutr * sizeof(double)); if ( val == NULL ) { status = CPXERR_NO_MEMORY; goto TERMINATE; } rngval = (double*)malloc(nnutr * sizeof(double)); if ( rngval == NULL ) { status = CPXERR_NO_MEMORY; goto TERMINATE; } for (i = 0; i < nnutr; i++) { rngval[i] = nutrmax[i] - nutrmin[i]; } ind = (int*) malloc(nfoods * sizeof(int)); if ( ind == NULL ) { status = CPXERR_NO_MEMORY; goto TERMINATE; } for (i = 0; i < nnutr; i++) { ind[i] = i; } status = CPXnewrows (env, lp, nnutr, nutrmin, sense, rngval, NULL); if ( status ) goto TERMINATE; for (j = 0; j < nfoods; ++j) { for (i = 0; i < nnutr; i++) { val[i] = nutrper[i][j]; } status = CPXaddcols (env, lp, 1, nnutr, cost+j, &zero, ind, val, lb+j, ub+j, NULL); if ( status ) goto TERMINATE; } TERMINATE: free_and_null ((char **)&sense); free_and_null ((char **)&rngval); free_and_null ((char **)&ind); free_and_null ((char **)&val); return (status); } /* END populatebycolumn */
static int buildnetwork (CPXENVptr env, CPXLPptr lp) { char sense[NUMNODES]; double rhs[NUMNODES]; int ind[2]; double val[2]; char *name[1]; char buffer[100]; int i, j, varindex; int zero = 0; double dblzero = 0.0; double dblone = 1.0; char binary = 'B'; int status = 0; /* Create constraint placeholders --- One constraint for each node (flow constraints) */ for (i = 0; i < NUMNODES; i++) { rhs[i] = demand[i]; sense[i] = 'G'; } status = CPXnewrows (env, lp, NUMNODES, rhs, sense, NULL, NULL); if ( status ) { fprintf (stderr, "Could not create new rows.\n"); goto TERMINATE; } /* Add flow variables */ for (j = 0; j < NUMEDGES; j++) { ind[0] = orig[j]; /* Flow leaves origin */ val[0] = -1.0; ind[1] = dest[j]; /* Flow arrives at destination */ val[1] = 1.0; name[0] = buffer; sprintf(buffer, "x%d%d", orig[j], dest[j]); status = CPXaddcols (env, lp, 1, 2, &unitcost[j], &zero, ind, val, NULL, NULL, name); if ( status ) { fprintf (stderr, "Failed to add flow edge.\n"); goto TERMINATE; } } /* Add fixed charge variables */ for (j = 0; j < NUMEDGES; j++) { name[0] = buffer; sprintf(buffer, "f%d%d", orig[j], dest[j]); status = CPXaddcols (env, lp, 1, 0, &fixedcost[j], &zero, ind, val, &dblzero, &dblone, name); if ( status ) { fprintf (stderr, "Failed to add fixed charge variable.\n"); goto TERMINATE; } varindex = NUMEDGES+j; status = CPXchgctype (env, lp, 1, &varindex, &binary); if ( status ) { fprintf (stderr, "Failed to change variable type.\n"); goto TERMINATE; } } /* Add indicator constraints -- f = 0 -> x <= 0 */ for (j = 0; j < NUMEDGES; j++) { varindex = j; sprintf(buffer, "indicator%d", j); status = CPXaddindconstr (env, lp, NUMEDGES+j, 1, 1, 0.0, 'L', &varindex, &dblone, buffer); if ( status ) { fprintf (stderr, "Failed to add indicator constraint."); goto TERMINATE; } } TERMINATE: return (status); } /* END buildnetwork */