int CClp_addcols(CClp *lp, int newcols, int newnz, double *obj, int *cmatbeg, int *cmatind, double *cmatval, double *lb, double *ub) { /* ADDS the columns to the LP. */ int j; lpx_add_cols(lp->lp, newcols); for (j = 0; j < newcols; j++) { int seqn, type, k, t; double lo, up; seqn = lpx_get_num_cols(lp->lp) - newcols + j + 1; lpx_set_col_coef(lp->lp, seqn, obj == NULL ? 0.0 : obj[j]); lo = lb[j], up = ub[j]; /* check for finite bounds */ insist(-1e10 <= lo && lo <= up && up <= +1e10); type = (lo == up ? LPX_FX : LPX_DB); lpx_set_col_bnds(lp->lp, seqn, type, lo, up); insist(cmatbeg != NULL); insist(cmatind != NULL); insist(cmatval != NULL); insist(cmatbeg[0] == 0); t = (j < newcols-1 ? cmatbeg[j+1] : newnz); for (k = cmatbeg[j]; k < t; k++) cmatind[k]++; lpx_set_mat_col(lp->lp, seqn, t - cmatbeg[j], &cmatind[cmatbeg[j]] - 1, &cmatval[cmatbeg[j]] - 1); for (k = cmatbeg[j]; k < t; k++) cmatind[k]--; } return 0; }
int CClp_loadlp(CClp *lp, const char *name, int ncols, int nrows, int objsense, double *obj, double *rhs, char *sense, int *matbeg, int *matcnt, int *matind, double *matval, double *lb, double *ub) { /* LOADS the data into the LP. - name attaches a name to the LP (it can be used by the LP solver in io routines) - ncols and nrows give the number of columns and rows in the LP - objsense should be 1 for minimize and -1 for maximize - obj and rhs are arrays giving the objective function and rhs - sense is an array specifying 'L', 'E', or 'G' for each of the rows - matbeg, matcnt, matind, and matval give the coefficients of the constraint matrix in column by column order. matbeg gives the index of the start of each column; matcnt gives the number of coefficients in each column; matind gives the indices of the rows where the coefficients are located in the constraint matrix (so for column j, the indices are given in matcnt[j] locations starting at matind[matbeg[j]]; and matval gives the actual coefficients (organized like matind). - lb and ub are arrays giving the upper and lower bounds of the variables. */ int i, j; /* create empty problem object */ insist(lp->lp == NULL); lp->lp = lpx_create_prob(); lpx_set_prob_name(lp->lp, (char *)name); /* set objective sense */ switch (objsense) { case +1: /* minimization */ lpx_set_obj_dir(lp->lp, LPX_MIN); break; case -1: /* maximization */ lpx_set_obj_dir(lp->lp, LPX_MAX); break; default: insist(objsense != objsense); } /* add rows */ lpx_add_rows(lp->lp, nrows); for (i = 0; i < nrows; i++) { int seqn, type; double lo, up; seqn = i+1; switch (sense[i]) { case 'L': type = LPX_UP, lo = 0.0, up = rhs[i]; break; case 'E': type = LPX_FX, lo = up = rhs[i]; break; case 'G': type = LPX_LO, lo = rhs[i], up = 0.0; break; default: insist(sense[i] != sense[i]); } lpx_set_row_bnds(lp->lp, seqn, type, lo, up); } /* add columns and constraint coefficients */ lpx_add_cols(lp->lp, ncols); for (j = 0; j < ncols; j++) { int seqn, type, k; double lo, up; seqn = j+1; lpx_set_col_coef(lp->lp, seqn, obj == NULL ? 0.0 : obj[j]); lo = lb[j], up = ub[j]; /* check for finite bounds */ insist(-1e12 <= lo && lo <= up && up <= +1e12); type = (lo == up ? LPX_FX : LPX_DB); lpx_set_col_bnds(lp->lp, seqn, type, lo, up); for (k = matbeg[j]; k < matbeg[j] + matcnt[j]; k++) matind[k]++; lpx_set_mat_col(lp->lp, seqn, matcnt[j], &matind[matbeg[j]] - 1, &matval[matbeg[j]] - 1); for (k = matbeg[j]; k < matbeg[j] + matcnt[j]; k++) matind[k]--; } return 0; }
LPX *ipp_build_prob(IPP *ipp) { LPX *prob; IPPROW *row; IPPCOL *col; IPPAIJ *aij; int i, j, type, len, *ind; double *val; /* create problem object */ prob = lpx_create_prob(); #if 0 lpx_set_class(prob, LPX_MIP); #endif /* the resultant problem should have the same optimization sense as the original problem */ lpx_set_obj_dir(prob, ipp->orig_dir); /* set the constant term of the objective function */ lpx_set_obj_coef(prob, 0, ipp->orig_dir == LPX_MIN ? + ipp->c0 : - ipp->c0); /* copy rows of the resultant problem */ for (row = ipp->row_ptr; row != NULL; row = row->next) { i = lpx_add_rows(prob, 1); if (row->lb == -DBL_MAX && row->ub == +DBL_MAX) type = LPX_FR; else if (row->ub == +DBL_MAX) type = LPX_LO; else if (row->lb == -DBL_MAX) type = LPX_UP; else if (row->lb != row->ub) type = LPX_DB; else type = LPX_FX; lpx_set_row_bnds(prob, i, type, row->lb, row->ub); row->temp = i; } /* copy columns of the resultant problem */ ind = xcalloc(1+lpx_get_num_rows(prob), sizeof(int)); val = xcalloc(1+lpx_get_num_rows(prob), sizeof(double)); for (col = ipp->col_ptr; col != NULL; col = col->next) { j = lpx_add_cols(prob, 1); if (col->i_flag) lpx_set_col_kind(prob, j, LPX_IV); if (col->lb == -DBL_MAX && col->ub == +DBL_MAX) type = LPX_FR; else if (col->ub == +DBL_MAX) type = LPX_LO; else if (col->lb == -DBL_MAX) type = LPX_UP; else if (col->lb != col->ub) type = LPX_DB; else type = LPX_FX; lpx_set_col_bnds(prob, j, type, col->lb, col->ub); lpx_set_obj_coef(prob, j, ipp->orig_dir == LPX_MIN ? + col->c : - col->c); /* copy constraint coefficients */ len = 0; for (aij = col->ptr; aij != NULL; aij = aij->c_next) { len++; ind[len] = aij->row->temp; val[len] = aij->val; } lpx_set_mat_col(prob, j, len, ind, val); } xfree(ind); xfree(val); return prob; }