alpar@1: /* cplex.c (CPLEX-like interface to GLPK API) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #include alpar@1: #include alpar@1: #include alpar@1: #include alpar@1: #include alpar@1: #include alpar@1: #include "cplex.h" alpar@1: alpar@1: struct CPXENV alpar@1: { /* environment block */ alpar@1: CPXLP *list; alpar@1: /* linked list of problem objects */ alpar@1: int *intparam; /* int intparam[]; */ alpar@1: /* integer control parameters */ alpar@1: double *dblparam; /* double dblparam[]; */ alpar@1: /* floating-point control parameters */ alpar@1: }; alpar@1: alpar@1: struct CPXLP alpar@1: { /* problem object */ alpar@1: CPXENV *env; alpar@1: /* pointer to environment block */ alpar@1: glp_prob *prob; alpar@1: /* pointer to underlying GLPK problem object */ alpar@1: int rflen; alpar@1: /* length of the array rflag */ alpar@1: char *rflag; /* char rflag[rflen]; */ alpar@1: /* rflag[i], i = 0,...,nrows-1, is a flag of i-th row: */ alpar@1: #define RF_NOT_RANGED 0 /* not ranged */ alpar@1: #define RF_RANGED_POS 1 /* ranged, RHS = lower bound */ alpar@1: #define RF_RANGED_NEG 2 /* ranged, RHS = upper bound */ alpar@1: int stat; alpar@1: /* solution status reported by CPXgetstat; zero means no solution alpar@1: exists */ alpar@1: int meth; alpar@1: /* method indicator reported by CPXgetmethod */ alpar@1: int iwlen; alpar@1: /* length of the working array */ alpar@1: int *iwork; /* int iwork[iwlen] */ alpar@1: /* working array initialized by binary zeros */ alpar@1: CPXLP *link; alpar@1: /* pointer to another problem object */ alpar@1: }; alpar@1: alpar@1: struct intparam alpar@1: { int which; alpar@1: int defv; alpar@1: int minv; alpar@1: int maxv; alpar@1: }; alpar@1: alpar@1: struct dblparam alpar@1: { int which; alpar@1: double defv; alpar@1: double minv; alpar@1: double maxv; alpar@1: }; alpar@1: alpar@1: struct errstring alpar@1: { int code; alpar@1: const char *string; alpar@1: }; alpar@1: alpar@1: #define BIGINT 2100000000 alpar@1: #define BIGDBL 1e75 alpar@1: alpar@1: static const struct intparam intparam[] = alpar@1: { {CPX_PARAM_ADVIND, 0, 0, 2}, alpar@1: {CPX_PARAM_AGGIND, -1, -1, BIGINT}, alpar@1: {CPX_PARAM_DATACHECK, CPX_OFF, CPX_OFF, CPX_ON}, alpar@1: {CPX_PARAM_DPRIIND, CPX_DPRIIND_AUTO, CPX_DPRIIND_AUTO, alpar@1: CPX_DPRIIND_DEVEX}, alpar@1: {CPX_PARAM_FASTMIP, CPX_OFF, CPX_OFF, CPX_ON}, /* ??? */ alpar@1: {CPX_PARAM_ITLIM, BIGINT, 0, BIGINT}, alpar@1: {CPX_PARAM_PERIND, CPX_OFF, CPX_OFF, CPX_ON}, alpar@1: {CPX_PARAM_PPRIIND, CPX_PPRIIND_AUTO, CPX_PPRIIND_PARTIAL, alpar@1: CPX_PPRIIND_FULL}, alpar@1: {CPX_PARAM_PREIND, CPX_ON, CPX_OFF, CPX_ON}, alpar@1: {CPX_PARAM_REINV, 0, 0, 10000}, alpar@1: {CPX_PARAM_SCRIND, CPX_OFF, CPX_OFF, CPX_ON}, alpar@1: {CPX_PARAM_SIMDISPLAY, 1, 0, 2}, alpar@1: }; alpar@1: alpar@1: static const struct dblparam dblparam[] = alpar@1: { {CPX_PARAM_EPOPT, 1e-6, 1e-9, 1e-1}, alpar@1: {CPX_PARAM_EPPER, 1e-6, 1e-8, BIGDBL}, alpar@1: {CPX_PARAM_EPRHS, 1e-6, 1e-9, 1e-1}, alpar@1: {CPX_PARAM_OBJLLIM, -BIGDBL, -BIGDBL, +BIGDBL}, alpar@1: {CPX_PARAM_OBJULIM, +BIGDBL, -BIGDBL, +BIGDBL}, alpar@1: }; alpar@1: alpar@1: static const struct errstring errstring[] = alpar@1: { {CPXERR_ARRAY_NOT_ASCENDING, "Array entry %d not ascending"}, alpar@1: {CPXERR_BAD_ARGUMENT, "Invalid argument"}, alpar@1: {CPXERR_BAD_CTYPE, "Invalid ctype entry %d"}, alpar@1: {CPXERR_BAD_FILETYPE, "Invalid filetype"}, alpar@1: {CPXERR_BAD_LUB, "Invalid bound change indicator entry %d"}, alpar@1: {CPXERR_BAD_PARAM_NUM, "Invalid parameter number"}, alpar@1: {CPXERR_BAD_SENSE, "Invalid sense entry %d"}, alpar@1: {CPXERR_BAD_STATUS, "Invalid status entry %d for basis specificat" alpar@1: "ion"}, alpar@1: {CPXERR_COL_INDEX_RANGE, "Column index %d out of range"}, alpar@1: {CPXERR_COUNT_RANGE, "Count entry %d negative or larger than allo" alpar@1: "wed"}, alpar@1: {CPXERR_DUP_ENTRY, "Duplicate entry"}, alpar@1: {CPXERR_FAIL_OPEN_WRITE, "Could not open file '%s' for writing"}, alpar@1: {CPXERR_INDEX_RANGE, "Index is outside range of valid values"}, alpar@1: {CPXERR_NEGATIVE_SURPLUS, "Insufficient array length"}, alpar@1: {CPXERR_NO_BASIC_SOLN, "No basic solution exists"}, alpar@1: {CPXERR_NO_ENVIRONMENT, "No environment exists"}, alpar@1: {CPXERR_NO_FILENAME, "File name not specified"}, alpar@1: {CPXERR_NO_MEMORY, "Out of memory"}, alpar@1: {CPXERR_NO_PROBLEM, "No problem exists"}, alpar@1: {CPXERR_NO_SOLN, "No solution exists"}, alpar@1: {CPXERR_NOT_FIXED, "Only fixed variables are pivoted out"}, alpar@1: {CPXERR_NULL_NAME, "Null pointer %d in name array"}, alpar@1: {CPXERR_NULL_POINTER, "Null pointer for required data"}, alpar@1: {CPXERR_PARAM_TOO_BIG, "Parameter value too big"}, alpar@1: {CPXERR_PARAM_TOO_SMALL, "Parameter value too small"}, alpar@1: {CPXERR_ROW_INDEX_RANGE, "Row index %d out of range"}, alpar@1: }; alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: #define xassert glp_assert alpar@1: #define xprintf glp_printf alpar@1: #define xmalloc glp_malloc alpar@1: #define xcalloc glp_calloc alpar@1: #define xfree glp_free alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: static int findintparam(int whichparam) alpar@1: { int k, card; alpar@1: card = sizeof(intparam) / sizeof(struct intparam); alpar@1: for (k = 0; k < card; k++) alpar@1: if (intparam[k].which == whichparam) return k; alpar@1: return -1; alpar@1: } alpar@1: alpar@1: static int getintparam(CPXENV *env, int whichparam) alpar@1: { int k; alpar@1: xassert(env != NULL); alpar@1: k = findintparam(whichparam); alpar@1: xassert(k >= 0); alpar@1: return env->intparam[k]; alpar@1: } alpar@1: alpar@1: static int finddblparam(int whichparam) alpar@1: { int k, card; alpar@1: card = sizeof(dblparam) / sizeof(struct dblparam); alpar@1: for (k = 0; k < card; k++) alpar@1: if (dblparam[k].which == whichparam) return k; alpar@1: return -1; alpar@1: } alpar@1: alpar@1: static double getdblparam(CPXENV *env, int whichparam) alpar@1: { int k; alpar@1: xassert(env != NULL); alpar@1: k = finddblparam(whichparam); alpar@1: xassert(k >= 0); alpar@1: return env->dblparam[k]; alpar@1: } alpar@1: alpar@1: static const char *finderrstring(int errcode) alpar@1: { int k, card; alpar@1: card = sizeof(errstring) / sizeof(struct errstring); alpar@1: for (k = 0; k < card; k++) alpar@1: { if (errstring[k].code == errcode) alpar@1: return errstring[k].string; alpar@1: } alpar@1: return NULL; alpar@1: } alpar@1: alpar@1: static int error(CPXENV *env, int errcode, ...) alpar@1: { va_list arg; alpar@1: char buffer[510]; alpar@1: xassert(env != NULL); alpar@1: if (getintparam(env, CPX_PARAM_SCRIND) == CPX_ON) alpar@1: { xassert(CPXgeterrorstring(env, errcode, buffer) == buffer); alpar@1: va_start(arg, errcode); alpar@1: vprintf(buffer, arg); alpar@1: va_end(arg); alpar@1: } alpar@1: return errcode; alpar@1: } alpar@1: alpar@1: static int checkenv(CPXENV *env) alpar@1: { int errcode; alpar@1: if (env == NULL) alpar@1: errcode = CPXERR_NO_ENVIRONMENT; alpar@1: else alpar@1: errcode = 0; alpar@1: return errcode; alpar@1: } alpar@1: alpar@1: static checklp(CPXENV *env, CPXLP *lp) alpar@1: { int errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: if (lp == NULL) alpar@1: errcode = error(env, CPXERR_NO_PROBLEM); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: static void invalidate(CPXLP *lp) alpar@1: { lp->stat = 0; alpar@1: lp->meth = CPX_ALG_NONE; alpar@1: return; alpar@1: } alpar@1: alpar@1: static void enlargerflag(CPXLP *lp) alpar@1: { int m; alpar@1: xassert(lp != NULL); alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: if (lp->rflen < m) alpar@1: { int rflen = lp->rflen; alpar@1: char *rflag = lp->rflag; alpar@1: while (lp->rflen < m) alpar@1: { lp->rflen += lp->rflen; alpar@1: xassert(lp->rflen > 0); alpar@1: } alpar@1: lp->rflag = xcalloc(lp->rflen, sizeof(char)); alpar@1: memcpy(lp->rflag, rflag, rflen); alpar@1: xfree(rflag); alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: static void enlargeiwork(CPXLP *lp, int len) alpar@1: { xassert(len >= 0); alpar@1: if (lp->iwlen < len) alpar@1: { xfree(lp->iwork); alpar@1: while (lp->iwlen < len) alpar@1: { lp->iwlen += lp->iwlen; alpar@1: xassert(lp->iwlen > 0); alpar@1: } alpar@1: lp->iwork = xcalloc(lp->iwlen, sizeof(int)); alpar@1: memset(lp->iwork, 0, lp->iwlen * sizeof(int)); alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: int CPXaddcols(CPXENV *env, CPXLP *lp, int ccnt, int nzcnt, alpar@1: const double obj[], const int cmatbeg[], const int cmatind[], alpar@1: const double cmatval[], const double lb[], const double ub[], alpar@1: char *colname[]) alpar@1: { int j, k, m, n, beg, end, type, errcode; alpar@1: double lbnd, ubnd; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (ccnt < 0 || nzcnt < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (ccnt > 0) alpar@1: { if (cmatbeg == NULL || cmatind == NULL || cmatval == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: enlargeiwork(lp, m); alpar@1: for (j = 0; j < ccnt; j++) alpar@1: { beg = cmatbeg[j]; alpar@1: if (j > 0 && !(cmatbeg[j-1] <= beg)) alpar@1: { errcode = error(env, CPXERR_ARRAY_NOT_ASCENDING, j); alpar@1: goto done; alpar@1: } alpar@1: if (!(0 <= beg && beg <= nzcnt)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: end = (j < ccnt-1 ? cmatbeg[j+1] : nzcnt); alpar@1: for (k = beg; k < end; k++) alpar@1: { if (!(0 <= cmatind[k] && cmatind[k] < m)) alpar@1: { errcode = error(env, CPXERR_ROW_INDEX_RANGE, k); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: for (k = beg; k < end; k++) alpar@1: { if (lp->iwork[cmatind[k]]) alpar@1: { errcode = error(env, CPXERR_DUP_ENTRY); alpar@1: break; alpar@1: } alpar@1: lp->iwork[cmatind[k]] = 1; alpar@1: } alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[cmatind[k]] = 0; alpar@1: if (errcode) goto done; alpar@1: if (colname != NULL) alpar@1: { if (colname[j] == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_NAME, j); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: if (ccnt > 0) alpar@1: glp_add_cols(lp->prob, ccnt); alpar@1: for (j = 0; j < ccnt; j++) alpar@1: { if (colname != NULL) alpar@1: glp_set_col_name(lp->prob, n+j+1, colname[j]); alpar@1: lbnd = (lb == NULL ? 0.0 : lb[j]); alpar@1: ubnd = (ub == NULL ? +CPX_INFBOUND : ub[j]); alpar@1: if (lbnd <= -CPX_INFBOUND && ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_FR; alpar@1: else if (ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_LO; alpar@1: else if (lbnd <= -CPX_INFBOUND) alpar@1: type = GLP_UP; alpar@1: else if (lbnd != ubnd) alpar@1: type = GLP_DB; alpar@1: else alpar@1: type = GLP_FX; alpar@1: glp_set_col_bnds(lp->prob, n+j+1, type, lbnd, ubnd); alpar@1: if (obj != NULL) alpar@1: glp_set_obj_coef(lp->prob, n+j+1, obj[j]); alpar@1: beg = cmatbeg[j]; alpar@1: end = (j < ccnt-1 ? cmatbeg[j+1] : nzcnt); alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[k-beg] = cmatind[k]+1; alpar@1: glp_set_mat_col(lp->prob, n+j+1, end-beg, lp->iwork-1, alpar@1: cmatval+beg-1); alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[k-beg] = 0; alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXaddrows(CPXENV *env, CPXLP *lp, int ccnt, int rcnt, int nzcnt, alpar@1: const double rhs[], const char sense[], const int rmatbeg[], alpar@1: const int rmatind[], const double rmatval[], char *colname[], alpar@1: char *rowname[]) alpar@1: { int i, j, k, m, n, beg, end, type, errcode; alpar@1: double temp; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (ccnt < 0 || rcnt < 0 || nzcnt < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (rcnt > 0) alpar@1: { if (rmatbeg == NULL || rmatind == NULL || rmatval == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: enlargeiwork(lp, n+ccnt); alpar@1: for (i = 0; i < rcnt; i++) alpar@1: { if (sense != NULL) alpar@1: { if (!(sense[i] == 'L' || sense[i] == 'E' || alpar@1: sense[i] == 'G' || sense[i] == 'R')) alpar@1: { errcode = error(env, CPXERR_BAD_SENSE, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: beg = rmatbeg[i]; alpar@1: if (i > 0 && !(rmatbeg[i-1] <= beg)) alpar@1: { errcode = error(env, CPXERR_ARRAY_NOT_ASCENDING, i); alpar@1: goto done; alpar@1: } alpar@1: if (!(0 <= beg && beg <= nzcnt)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: end = (i < rcnt-1 ? rmatbeg[i+1] : nzcnt); alpar@1: for (k = beg; k < end; k++) alpar@1: { if (!(0 <= rmatind[k] && rmatind[k] < n+ccnt)) alpar@1: { errcode = error(env, CPXERR_COL_INDEX_RANGE, k); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: for (k = beg; k < end; k++) alpar@1: { if (lp->iwork[rmatind[k]]) alpar@1: { errcode = error(env, CPXERR_DUP_ENTRY); alpar@1: break; alpar@1: } alpar@1: lp->iwork[rmatind[k]] = 1; alpar@1: } alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[rmatind[k]] = 0; alpar@1: if (errcode) goto done; alpar@1: if (rowname != NULL) alpar@1: { if (rowname[i] == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_NAME, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: } alpar@1: for (j = 0; j < ccnt; j++) alpar@1: { if (colname != NULL) alpar@1: { if (colname[j] == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_NAME, j); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: if (rcnt > 0) alpar@1: glp_add_rows(lp->prob, rcnt); alpar@1: if (ccnt > 0) alpar@1: glp_add_cols(lp->prob, ccnt); alpar@1: enlargerflag(lp); alpar@1: for (i = 0; i < rcnt; i++) alpar@1: { if (rowname != NULL) alpar@1: glp_set_row_name(lp->prob, m+i+1, rowname[i]); alpar@1: temp = (rhs == NULL ? 0.0 : rhs[i]); alpar@1: if (sense == NULL || sense[i] == 'E') alpar@1: { lp->rflag[m+i] = RF_NOT_RANGED; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else if (sense[i] == 'L') alpar@1: { lp->rflag[m+i] = RF_NOT_RANGED; alpar@1: type = GLP_UP; alpar@1: } alpar@1: else if (sense[i] == 'G') alpar@1: { lp->rflag[m+i] = RF_NOT_RANGED; alpar@1: type = GLP_LO; alpar@1: } alpar@1: else if (sense[i] == 'R') alpar@1: { lp->rflag[m+i] = RF_RANGED_POS; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else alpar@1: xassert(sense != sense); alpar@1: glp_set_row_bnds(lp->prob, m+i+1, type, temp, temp); alpar@1: beg = rmatbeg[i]; alpar@1: end = (i < rcnt-1 ? rmatbeg[i+1] : nzcnt); alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[k-beg] = rmatind[k]+1; alpar@1: glp_set_mat_row(lp->prob, m+i+1, end-beg, lp->iwork-1, alpar@1: rmatval+beg-1); alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[k-beg] = 0; alpar@1: } alpar@1: for (j = 0; j < ccnt; j++) alpar@1: { if (colname != NULL) alpar@1: glp_set_col_name(lp->prob, n+j+1, colname[j]); alpar@1: glp_set_col_bnds(lp->prob, n+j+1, GLP_LO, 0.0, 0.0); alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXbaropt(CPXENV *env, CPXLP *lp) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xprintf("CPXbaropt: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXbinvrow(CPXENV *env, CPXLP *lp, int i, double y[]) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(i == i); alpar@1: xassert(y == y); alpar@1: xprintf("CPXbinvrow: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXchgbds(CPXENV *env, CPXLP *lp, int cnt, const int indices[], alpar@1: const char lu[], const double bd[]) alpar@1: { int j, n, type, errcode; alpar@1: double lbnd, ubnd; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (cnt < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (cnt > 0) alpar@1: { if (indices == NULL || lu == NULL || bd == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: for (j = 0; j < cnt; j++) alpar@1: { if (!(0 <= indices[j] && indices[j] < n)) alpar@1: { errcode = error(env, CPXERR_COL_INDEX_RANGE, j); alpar@1: goto done; alpar@1: } alpar@1: if (!(lu[j] == 'L' || lu[j] == 'U' || lu[j] == 'B')) alpar@1: { errcode = error(env, CPXERR_BAD_LUB, j); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: for (j = 0; j < cnt; j++) alpar@1: { type = glp_get_col_type(lp->prob, indices[j]+1); alpar@1: lbnd = glp_get_col_lb(lp->prob, indices[j]+1); alpar@1: ubnd = glp_get_col_ub(lp->prob, indices[j]+1); alpar@1: if (type == GLP_FR || type == GLP_UP) alpar@1: lbnd = -CPX_INFBOUND; alpar@1: if (type == GLP_FR || type == GLP_LO) alpar@1: ubnd = +CPX_INFBOUND; alpar@1: if (lu[j] == 'L') alpar@1: lbnd = bd[j]; alpar@1: else if (lu[j] == 'U') alpar@1: ubnd = bd[j]; alpar@1: else if (lu[j] == 'B') alpar@1: lbnd = ubnd = bd[j]; alpar@1: else alpar@1: xassert(lu != lu); alpar@1: if (lbnd <= -CPX_INFBOUND && ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_FR; alpar@1: else if (ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_LO; alpar@1: else if (lbnd <= -CPX_INFBOUND) alpar@1: type = GLP_UP; alpar@1: else if (lbnd != ubnd) alpar@1: type = GLP_DB; alpar@1: else alpar@1: type = GLP_FX; alpar@1: glp_set_col_bnds(lp->prob, indices[j]+1, type, lbnd, ubnd); alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXchgcoeflist(CPXENV *env, CPXLP *lp, int numcoefs, alpar@1: const int rowlist[], const int collist[], const double vallist[]) alpar@1: { int i, j, k, m, n, rcnt, ccnt, len, ptr, errcode; alpar@1: int *head, *next, *ind; alpar@1: double *val; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (numcoefs < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (numcoefs == 0) alpar@1: { errcode = 0; alpar@1: goto done; alpar@1: } alpar@1: if (rowlist == NULL || collist == NULL || vallist == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: /* check triplets and determine the number of rows and columns alpar@1: to be changed */ alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: enlargeiwork(lp, m); alpar@1: enlargeiwork(lp, n); alpar@1: rcnt = ccnt = 0; alpar@1: for (k = 0; k < numcoefs; k++) alpar@1: { i = rowlist[k]; alpar@1: if (!(0 <= i && i < m)) alpar@1: { errcode = error(env, CPXERR_ROW_INDEX_RANGE, i); alpar@1: goto done; alpar@1: } alpar@1: if (!(lp->iwork[i] & 0x01)) alpar@1: rcnt++, lp->iwork[i] |= 0x01; alpar@1: j = collist[k]; alpar@1: if (!(0 <= j && j < n)) alpar@1: { errcode = error(env, CPXERR_COL_INDEX_RANGE, j); alpar@1: goto done; alpar@1: } alpar@1: if (!(lp->iwork[j] & 0x02)) alpar@1: ccnt++, lp->iwork[j] |= 0x02; alpar@1: } alpar@1: memset(lp->iwork, 0, m * sizeof(int)); alpar@1: memset(lp->iwork, 0, n * sizeof(int)); alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: if (rcnt <= ccnt) alpar@1: { /* change the matrix by rows */ alpar@1: /* build the linked list of triplets: alpar@1: head[i] is a pointer to first triplet for row i alpar@1: next[k] is a pointer to next triplet for the same row */ alpar@1: head = xcalloc(m, sizeof(int)); alpar@1: for (i = 0; i < m; i++) alpar@1: head[i] = -1; alpar@1: next = xcalloc(numcoefs, sizeof(int)); alpar@1: for (k = 0; k < numcoefs; k++) alpar@1: { i = rowlist[k]; alpar@1: next[k] = head[i]; alpar@1: head[i] = k; alpar@1: } alpar@1: /* check duplicate columns */ alpar@1: for (i = 0; i < m; i++) alpar@1: { for (k = head[i]; k >= 0; k = next[k]) alpar@1: { j = collist[k]; alpar@1: if (lp->iwork[j]) alpar@1: { xfree(head); alpar@1: xfree(next); alpar@1: errcode = error(env, CPXERR_DUP_ENTRY); alpar@1: goto done; alpar@1: } alpar@1: lp->iwork[j] = 1; alpar@1: } alpar@1: for (k = head[i]; k >= 0; k = next[k]) alpar@1: lp->iwork[collist[k]] = 0; alpar@1: } alpar@1: /* perform operation */ alpar@1: ind = xcalloc(1+n, sizeof(int)); alpar@1: val = xcalloc(1+n, sizeof(double)); alpar@1: for (i = 0; i < m; i++) alpar@1: { if (head[i] < 0) continue; alpar@1: len = glp_get_mat_row(lp->prob, i+1, ind, val); alpar@1: for (ptr = 1; ptr <= len; ptr++) alpar@1: { j = ind[ptr]-1; alpar@1: xassert(lp->iwork[j] == 0); alpar@1: lp->iwork[j] = ptr; alpar@1: } alpar@1: for (k = head[i]; k >= 0; k = next[k]) alpar@1: { j = collist[k]; alpar@1: if (lp->iwork[j] == 0) alpar@1: lp->iwork[j] = ++len; alpar@1: ptr = lp->iwork[j]; alpar@1: ind[ptr] = j+1, val[ptr] = vallist[k]; alpar@1: } alpar@1: glp_set_mat_row(lp->prob, i+1, len, ind, val); alpar@1: for (ptr = 1; ptr <= len; ptr++) alpar@1: lp->iwork[ind[ptr]-1] = 0; alpar@1: } alpar@1: } alpar@1: else alpar@1: { /* change the matrix by columns */ alpar@1: /* build the linked lists of triplets: alpar@1: head[j] is a pointer to first triplet for column j alpar@1: next[k] is a pointer to next triplet for the same column */ alpar@1: head = xcalloc(n, sizeof(int)); alpar@1: for (j = 0; j < n; j++) alpar@1: head[j] = -1; alpar@1: next = xcalloc(numcoefs, sizeof(int)); alpar@1: for (k = 0; k < numcoefs; k++) alpar@1: { j = collist[k]; alpar@1: next[k] = head[j]; alpar@1: head[j] = k; alpar@1: } alpar@1: /* check duplicate rows */ alpar@1: for (j = 0; j < n; j++) alpar@1: { for (k = head[j]; k >= 0; k = next[k]) alpar@1: { i = rowlist[k]; alpar@1: if (lp->iwork[i]) alpar@1: { xfree(head); alpar@1: xfree(next); alpar@1: errcode = error(env, CPXERR_DUP_ENTRY); alpar@1: goto done; alpar@1: } alpar@1: lp->iwork[i] = 1; alpar@1: } alpar@1: for (k = head[j]; k >= 0; k = next[k]) alpar@1: lp->iwork[rowlist[k]] = 0; alpar@1: } alpar@1: /* perform operation */ alpar@1: ind = xcalloc(1+m, sizeof(int)); alpar@1: val = xcalloc(1+m, sizeof(double)); alpar@1: for (j = 0; j < n; j++) alpar@1: { if (head[j] < 0) continue; alpar@1: len = glp_get_mat_col(lp->prob, j+1, ind, val); alpar@1: for (ptr = 1; ptr <= len; ptr++) alpar@1: { i = ind[ptr]-1; alpar@1: xassert(lp->iwork[i] == 0); alpar@1: lp->iwork[i] = ptr; alpar@1: } alpar@1: for (k = head[j]; k >= 0; k = next[k]) alpar@1: { i = rowlist[k]; alpar@1: if (lp->iwork[i] == 0) alpar@1: lp->iwork[i] = ++len; alpar@1: ptr = lp->iwork[i]; alpar@1: ind[ptr] = i+1, val[ptr] = vallist[k]; alpar@1: } alpar@1: glp_set_mat_col(lp->prob, j+1, len, ind, val); alpar@1: for (ptr = 1; ptr <= len; ptr++) alpar@1: lp->iwork[ind[ptr]-1] = 0; alpar@1: } alpar@1: } alpar@1: xfree(head); alpar@1: xfree(next); alpar@1: xfree(ind); alpar@1: xfree(val); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: void CPXchgobjsen(CPXENV *env, CPXLP *lp, int maxormin) alpar@1: { int errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (!(maxormin == CPX_MIN || maxormin == CPX_MAX)) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: if (maxormin == CPX_MIN) alpar@1: glp_set_obj_dir(lp->prob, GLP_MIN); alpar@1: else alpar@1: glp_set_obj_dir(lp->prob, GLP_MAX); alpar@1: done: xassert(errcode == errcode); alpar@1: return; alpar@1: } alpar@1: alpar@1: int CPXchgsense(CPXENV *env, CPXLP *lp, int cnt, const int indices[], alpar@1: const char sense[]) alpar@1: { int i, m, type, errcode; alpar@1: double rhs; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (cnt < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (cnt > 0 && (indices == NULL || sense == NULL)) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: for (i = 0; i < cnt; i++) alpar@1: { if (!(0 <= indices[i] && indices[i] < m)) alpar@1: { errcode = error(env, CPXERR_ROW_INDEX_RANGE, i); alpar@1: goto done; alpar@1: } alpar@1: if (!(sense[i] == 'L' || sense[i] == 'E' || sense[i] == 'G' || alpar@1: sense[i] == 'R')) alpar@1: { errcode = error(env, CPXERR_BAD_SENSE, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: for (i = 0; i < cnt; i++) alpar@1: { type = glp_get_row_type(lp->prob, indices[i]+1); alpar@1: if (lp->rflag[indices[i]] == RF_NOT_RANGED) alpar@1: { if (type == GLP_LO || type == GLP_FX) alpar@1: rhs = glp_get_row_lb(lp->prob, indices[i]+1); alpar@1: else if (type == GLP_UP) alpar@1: rhs = glp_get_row_ub(lp->prob, indices[i]+1); alpar@1: else alpar@1: xassert(type != type); alpar@1: } alpar@1: else if (lp->rflag[indices[i]] == RF_RANGED_POS) alpar@1: { xassert(type == GLP_DB || type == GLP_FX); alpar@1: rhs = glp_get_row_lb(lp->prob, indices[i]+1); alpar@1: } alpar@1: else if (lp->rflag[indices[i]] == RF_RANGED_NEG) alpar@1: { xassert(type == GLP_DB); alpar@1: rhs = glp_get_row_ub(lp->prob, indices[i]+1); alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: if (sense[i] == 'L') alpar@1: { lp->rflag[indices[i]] = RF_NOT_RANGED; alpar@1: type = GLP_UP; alpar@1: } alpar@1: else if (sense[i] == 'E') alpar@1: { lp->rflag[indices[i]] = RF_NOT_RANGED; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else if (sense[i] == 'G') alpar@1: { lp->rflag[indices[i]] = RF_NOT_RANGED; alpar@1: type = GLP_LO; alpar@1: } alpar@1: else if (sense[i] == 'R') alpar@1: { lp->rflag[indices[i]] = RF_RANGED_POS; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else alpar@1: xassert(sense != sense); alpar@1: glp_set_row_bnds(lp->prob, indices[i]+1, type, rhs, rhs); alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXcloseCPLEX(CPXENV **_env) alpar@1: { CPXENV *env; alpar@1: CPXLP *lp; alpar@1: int errcode; alpar@1: if (_env == NULL) alpar@1: { errcode = CPXERR_NULL_POINTER; alpar@1: goto done; alpar@1: } alpar@1: env = *_env; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: while (env->list != NULL) alpar@1: { lp = env->list; alpar@1: errcode = CPXfreeprob(env, &lp); alpar@1: xassert(!errcode); alpar@1: } alpar@1: xfree(env->intparam); alpar@1: xfree(env->dblparam); alpar@1: xfree(env); alpar@1: *_env = NULL; alpar@1: errcode = 0; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXcopybase(CPXENV *env, CPXLP *lp, const int cstat[], alpar@1: const int rstat[]) alpar@1: { int i, j, m, n, stat, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: if (m > 0 && rstat == NULL || n > 0 && cstat == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: for (i = 0; i < m; i++) alpar@1: { if (!(rstat[i] == CPX_AT_LOWER || rstat[i] == CPX_BASIC || alpar@1: rstat[i] == CPX_AT_UPPER)) alpar@1: { errcode = error(env, CPXERR_BAD_STATUS, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: for (j = 0; j < n; j++) alpar@1: { if (!(cstat[j] == CPX_AT_LOWER || cstat[j] == CPX_BASIC || alpar@1: cstat[j] == CPX_AT_UPPER || cstat[j] == CPX_FREE_SUPER)) alpar@1: { errcode = error(env, CPXERR_BAD_STATUS, j); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: for (i = 0; i < m; i++) alpar@1: { if (rstat[i] == CPX_AT_LOWER) alpar@1: stat = GLP_NL; alpar@1: else if (rstat[i] == CPX_BASIC) alpar@1: stat = GLP_BS; alpar@1: else if (rstat[i] == CPX_AT_UPPER) alpar@1: stat = GLP_NU; alpar@1: else alpar@1: xassert(rstat != rstat); alpar@1: glp_set_row_stat(lp->prob, i+1, stat); alpar@1: } alpar@1: for (j = 0; j < n; j++) alpar@1: { if (cstat[j] == CPX_AT_LOWER) alpar@1: stat = GLP_NL; alpar@1: else if (cstat[j] == CPX_BASIC) alpar@1: stat = GLP_BS; alpar@1: else if (cstat[j] == CPX_AT_UPPER) alpar@1: stat = GLP_NU; alpar@1: else if (cstat[j] == CPX_FREE_SUPER) alpar@1: stat = GLP_NF; alpar@1: else alpar@1: xassert(cstat != cstat); alpar@1: glp_set_col_stat(lp->prob, j+1, stat); alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXcopybasednorms(CPXENV *env, CPXLP *lp, const int cstat[], alpar@1: const int rstat[], const double dnorm[]) alpar@1: { int errcode; alpar@1: errcode = CPXcopybase(env, lp, cstat, rstat); alpar@1: xassert(dnorm == dnorm); alpar@1: return errcode; alpar@1: } alpar@1: alpar@1: int CPXcopylp(CPXENV *env, CPXLP *lp, int numcols, int numrows, alpar@1: int objsen, const double obj[], const double rhs[], alpar@1: const char sense[], const int matbeg[], const int matcnt[], alpar@1: const int matind[], const double matval[], const double lb[], alpar@1: const double ub[], const double rngval[]) alpar@1: { int errcode; alpar@1: errcode = CPXcopylpwnames(env, lp, numcols, numrows, objsen, obj, alpar@1: rhs, sense, matbeg, matcnt, matind, matval, lb, ub, rngval, alpar@1: NULL, NULL); alpar@1: return errcode; alpar@1: } alpar@1: alpar@1: int CPXcopylpwnames(CPXENV *env, CPXLP *lp, int numcols, int numrows, alpar@1: int objsen, const double obj[], const double rhs[], alpar@1: const char sense[], const int matbeg[], const int matcnt[], alpar@1: const int matind[], const double matval[], const double lb[], alpar@1: const double ub[], const double rngval[], char *colname[], alpar@1: char *rowname[]) alpar@1: { int i, j, k, beg, end, type, errcode; alpar@1: double lbnd, ubnd; alpar@1: char name[255+1]; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (numcols < 0 || numrows < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (!(objsen == CPX_MIN || objsen == CPX_MAX)) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (numcols > 0) alpar@1: { if (matbeg == NULL || matcnt == NULL || matind == NULL || alpar@1: matval == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: for (i = 0; i < numrows; i++) alpar@1: { if (sense != NULL) alpar@1: { if (!(sense[i] == 'L' || sense[i] == 'E' || alpar@1: sense[i] == 'G' || sense[i] == 'R')) alpar@1: { errcode = error(env, CPXERR_BAD_SENSE, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: if (rowname != NULL) alpar@1: { if (rowname[i] == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_NAME, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: } alpar@1: enlargeiwork(lp, numrows); alpar@1: for (j = 0; j < numcols; j++) alpar@1: { beg = matbeg[j]; alpar@1: if (j > 0 && !(matbeg[j-1] <= beg)) alpar@1: { errcode = error(env, CPXERR_ARRAY_NOT_ASCENDING, j); alpar@1: goto done; alpar@1: } alpar@1: if (beg < 0) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: end = beg + matcnt[j]; alpar@1: if (!(beg <= end) || j < numcols-1 && !(end <= matbeg[j+1])) alpar@1: { errcode = error(env, CPXERR_COUNT_RANGE, j); alpar@1: goto done; alpar@1: } alpar@1: for (k = beg; k < end; k++) alpar@1: { if (!(0 <= matind[k] && matind[k] < numrows)) alpar@1: { errcode = error(env, CPXERR_ROW_INDEX_RANGE, k); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: for (k = beg; k < end; k++) alpar@1: { if (lp->iwork[matind[k]]) alpar@1: { errcode = error(env, CPXERR_DUP_ENTRY); alpar@1: break; alpar@1: } alpar@1: lp->iwork[matind[k]] = 1; alpar@1: } alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[matind[k]] = 0; alpar@1: if (errcode) goto done; alpar@1: if (colname != NULL) alpar@1: { if (colname[j] != NULL) alpar@1: { errcode = error(env, CPXERR_NULL_NAME, j); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: if (glp_get_prob_name(lp->prob) == NULL) alpar@1: name[0] = '\0'; alpar@1: else alpar@1: strcpy(name, glp_get_prob_name(lp->prob)); alpar@1: glp_erase_prob(lp->prob); alpar@1: glp_set_prob_name(lp->prob, name); alpar@1: if (objsen == CPX_MIN) alpar@1: glp_set_obj_dir(lp->prob, GLP_MIN); alpar@1: else if (objsen == CPX_MAX) alpar@1: glp_set_obj_dir(lp->prob, GLP_MAX); alpar@1: else alpar@1: xassert(objsen != objsen); alpar@1: if (numrows > 0) alpar@1: glp_add_rows(lp->prob, numrows); alpar@1: enlargerflag(lp); alpar@1: for (i = 0; i < numrows; i++) alpar@1: { if (rowname != NULL) alpar@1: glp_set_row_name(lp->prob, i+1, rowname[i]); alpar@1: lbnd = ubnd = (rhs == NULL ? 0.0 : rhs[i]); alpar@1: if (sense == NULL || sense[i] == 'E') alpar@1: { lp->rflag[i] = RF_NOT_RANGED; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else if (sense[i] == 'L') alpar@1: { lp->rflag[i] = RF_NOT_RANGED; alpar@1: type = GLP_UP; alpar@1: } alpar@1: else if (sense[i] == 'G') alpar@1: { lp->rflag[i] = RF_NOT_RANGED; alpar@1: type = GLP_LO; alpar@1: } alpar@1: else if (sense[i] == 'R') alpar@1: { if (rngval == NULL || rngval[i] == 0.0) alpar@1: { lp->rflag[i] = RF_RANGED_POS; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else if (rngval[i] > 0.0) alpar@1: { lp->rflag[i] = RF_RANGED_POS; alpar@1: type = GLP_DB; alpar@1: ubnd += rngval[i]; alpar@1: } alpar@1: else /* rngval[i] < 0.0 */ alpar@1: { lp->rflag[i] = RF_RANGED_NEG; alpar@1: type = GLP_DB; alpar@1: lbnd += rngval[i]; alpar@1: } alpar@1: } alpar@1: else alpar@1: xassert(sense != sense); alpar@1: glp_set_row_bnds(lp->prob, i+1, type, lbnd, ubnd); alpar@1: } alpar@1: if (numcols > 0) alpar@1: glp_add_cols(lp->prob, numcols); alpar@1: for (j = 0; j < numcols; j++) alpar@1: { if (colname != NULL) alpar@1: glp_set_col_name(lp->prob, j+1, colname[j]); alpar@1: lbnd = (lb == NULL ? 0.0 : lb[j]); alpar@1: ubnd = (ub == NULL ? +CPX_INFBOUND : ub[j]); alpar@1: if (lbnd <= -CPX_INFBOUND && ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_FR; alpar@1: else if (ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_LO; alpar@1: else if (lbnd <= -CPX_INFBOUND) alpar@1: type = GLP_UP; alpar@1: else if (lbnd != ubnd) alpar@1: type = GLP_DB; alpar@1: else alpar@1: type = GLP_FX; alpar@1: glp_set_col_bnds(lp->prob, j+1, type, lbnd, ubnd); alpar@1: if (obj != NULL) alpar@1: glp_set_obj_coef(lp->prob, j+1, obj[j]); alpar@1: beg = matbeg[j]; alpar@1: end = beg + matcnt[j]; alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[k-beg] = matind[k]+1; alpar@1: glp_set_mat_col(lp->prob, j+1, end-beg, lp->iwork-1, alpar@1: matval+beg-1); alpar@1: for (k = beg; k < end; k++) alpar@1: lp->iwork[k-beg] = 0; alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: CPXLP *CPXcreateprob(CPXENV *env, int *status, const char *probname) alpar@1: { CPXLP *lp = NULL; alpar@1: int errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: lp = xmalloc(sizeof(struct CPXLP)); alpar@1: lp->env = env; alpar@1: lp->prob = glp_create_prob(); alpar@1: glp_set_prob_name(lp->prob, probname); alpar@1: lp->rflen = 100; alpar@1: lp->rflag = xcalloc(lp->rflen, sizeof(char)); alpar@1: lp->iwlen = 100; alpar@1: lp->iwork = xcalloc(lp->iwlen, sizeof(int)); alpar@1: memset(lp->iwork, 0, lp->iwlen * sizeof(int)); alpar@1: lp->link = env->list; alpar@1: env->list = lp; alpar@1: invalidate(lp); alpar@1: done: if (status != NULL) *status = errcode; alpar@1: return lp; alpar@1: } alpar@1: alpar@1: int CPXdelcols(CPXENV *env, CPXLP *lp, int begin, int end) alpar@1: { int j, n, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: if (!(0 <= begin && begin <= end && end < n)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: enlargeiwork(lp, end-begin+1); alpar@1: for (j = begin; j <= end; j++) alpar@1: lp->iwork[j-begin] = j+1; alpar@1: glp_del_cols(lp->prob, end-begin+1, lp->iwork-1); alpar@1: for (j = begin; j <= end; j++) alpar@1: lp->iwork[j-begin] = 0; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXdelrows(CPXENV *env, CPXLP *lp, int begin, int end) alpar@1: { int i, m, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: if (!(0 <= begin && begin <= end && end < m)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: enlargeiwork(lp, end-begin+1); alpar@1: for (i = begin; i <= end; i++) alpar@1: lp->iwork[i-begin] = i+1; alpar@1: glp_del_rows(lp->prob, end-begin+1, lp->iwork-1); alpar@1: for (i = begin; i <= end; i++) alpar@1: lp->iwork[i-begin] = 0; alpar@1: for (i = end+1; i < m; i++) alpar@1: lp->rflag[i-(end-begin+1)] = lp->rflag[i]; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXdelsetcols(CPXENV *env, CPXLP *lp, int delstat[]) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(delstat == delstat); alpar@1: xprintf("CPXdelsetcols: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXdelsetrows(CPXENV *env, CPXLP *lp, int delstat[]) alpar@1: { int i, m, cnt, ind, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: if (m > 0 && delstat == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: enlargeiwork(lp, m); alpar@1: cnt = ind = 0; alpar@1: for (i = 0; i < m; i++) alpar@1: { if (delstat[i] == 1) alpar@1: { delstat[i] = -1; alpar@1: lp->iwork[cnt++] = i+1; alpar@1: } alpar@1: else alpar@1: { delstat[i] = ind; alpar@1: lp->rflag[ind++] = lp->rflag[i]; alpar@1: } alpar@1: } alpar@1: if (cnt > 0) alpar@1: glp_del_rows(lp->prob, cnt, lp->iwork-1); alpar@1: for (i = 0; i < cnt; i++) alpar@1: lp->iwork[i] = 0; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXdualopt(CPXENV *env, CPXLP *lp); alpar@1: alpar@1: int CPXfreeprob(CPXENV *env, CPXLP **_lp) alpar@1: { CPXLP *lp; alpar@1: int errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: if (_lp == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: lp = *_lp; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: errcode = 0; alpar@1: env = lp->env; alpar@1: if (env->list == lp) alpar@1: env->list = lp->link; alpar@1: else alpar@1: { CPXLP *pp; alpar@1: for (pp = env->list; pp != NULL; pp = pp->link) alpar@1: if (pp->link == lp) break; alpar@1: xassert(pp != NULL); alpar@1: pp->link = lp->link; alpar@1: } alpar@1: glp_delete_prob(lp->prob); alpar@1: xfree(lp->rflag); alpar@1: xfree(lp->iwork); alpar@1: xfree(lp); alpar@1: *_lp = NULL; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetbase(CPXENV *env, CPXLP *lp, int cstat[], int rstat[]) alpar@1: { int i, j, m, n, stat, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (!lp->stat) alpar@1: { errcode = error(env, CPXERR_NO_SOLN); alpar@1: goto done; alpar@1: } alpar@1: if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: ; alpar@1: else alpar@1: { errcode = error(env, CPXERR_NO_BASIC_SOLN); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (rstat != NULL) alpar@1: { m = glp_get_num_rows(lp->prob); alpar@1: for (i = 0; i < m; i++) alpar@1: { stat = glp_get_row_stat(lp->prob, i+1); alpar@1: if (stat == GLP_BS) alpar@1: rstat[i] = CPX_BASIC; alpar@1: else if (lp->rflag[i] == RF_NOT_RANGED || stat != GLP_NU) alpar@1: rstat[i] = CPX_AT_LOWER; alpar@1: else alpar@1: rstat[i] = CPX_AT_UPPER; alpar@1: } alpar@1: } alpar@1: if (cstat != NULL) alpar@1: { n = glp_get_num_cols(lp->prob); alpar@1: for (j = 0; j < n; j++) alpar@1: { stat = glp_get_col_stat(lp->prob, j+1); alpar@1: if (stat == GLP_BS) alpar@1: cstat[j] = CPX_BASIC; alpar@1: else if (stat == GLP_NU) alpar@1: cstat[j] = CPX_AT_UPPER; alpar@1: else if (stat == GLP_NF) alpar@1: cstat[j] = CPX_FREE_SUPER; alpar@1: else alpar@1: cstat[j] = CPX_AT_LOWER; alpar@1: } alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetbasednorms(CPXENV *env, CPXLP *lp, int cstat[], int rstat[], alpar@1: double dnorm[]) alpar@1: { int i, m, errcode; alpar@1: errcode = CPXgetbase(env, lp, cstat, rstat); alpar@1: if (errcode) goto done; alpar@1: if (dnorm != NULL) alpar@1: { m = glp_get_num_rows(lp->prob); alpar@1: for (i = 0; i < m; i++) dnorm[i] = 1.0; alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetbhead(CPXENV *env, CPXLP *lp, int head[], double x[]) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(head == head); alpar@1: xassert(x == x); alpar@1: xprintf("CPXgetbhead: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXgetdblparam(CPXENV *env, int whichparam, double *value) alpar@1: { int k, errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: k = finddblparam(whichparam); alpar@1: if (k < 0) alpar@1: { errcode = error(env, CPXERR_BAD_PARAM_NUM); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (value != NULL) alpar@1: *value = env->dblparam[k]; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetdj(CPXENV *env, CPXLP *lp, double dj[], int begin, int end) alpar@1: { int j, n, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: if (!(0 <= begin && begin <= end && end < n)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: if (!lp->stat) alpar@1: { errcode = error(env, CPXERR_NO_SOLN); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: { if (dj != NULL) alpar@1: { for (j = begin; j <= end; j++) alpar@1: dj[j-begin] = glp_get_col_dual(lp->prob, j+1); alpar@1: } alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: char *CPXgeterrorstring(CPXENV *env, int errcode, char *buffer) alpar@1: { const char *string; alpar@1: xassert(env == env); alpar@1: string = finderrstring(errcode); alpar@1: if (string == NULL) alpar@1: buffer = NULL; alpar@1: else alpar@1: sprintf(buffer, "CPLEX Error %5d: %s.\n", errcode, string); alpar@1: return buffer; alpar@1: } alpar@1: alpar@1: int CPXgetijdiv(CPXENV *env, CPXLP *lp, int *idiv, int *jdiv) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(idiv == idiv); alpar@1: xassert(jdiv == jdiv); alpar@1: xprintf("CPXgetijdiv: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXgetintparam(CPXENV *env, int whichparam, int *value) alpar@1: { int k, errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: k = findintparam(whichparam); alpar@1: if (k < 0) alpar@1: { errcode = error(env, CPXERR_BAD_PARAM_NUM); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (value != NULL) alpar@1: *value = env->intparam[k]; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetlb(CPXENV *env, CPXLP *lp, double lb[], int begin, int end) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(lb == lb); alpar@1: xassert(begin == begin); alpar@1: xassert(end == end); alpar@1: xprintf("CPXgetlb: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXgetmethod(CPXENV *env, CPXLP *lp) alpar@1: { int method; alpar@1: if (checklp(env, lp)) alpar@1: method = CPX_ALG_NONE; alpar@1: else alpar@1: method = lp->meth; alpar@1: return method; alpar@1: } alpar@1: alpar@1: int CPXgetnumcols(CPXENV *env, CPXLP *lp) alpar@1: { int numcols; alpar@1: if (checklp(env, lp)) alpar@1: numcols = 0; alpar@1: else alpar@1: numcols = glp_get_num_cols(lp->prob); alpar@1: return numcols; alpar@1: } alpar@1: alpar@1: int CPXgetnumnz(CPXENV *env, CPXLP *lp) alpar@1: { int numnz; alpar@1: if (checklp(env, lp)) alpar@1: numnz = 0; alpar@1: else alpar@1: numnz = glp_get_num_nz(lp->prob); alpar@1: return numnz; alpar@1: } alpar@1: alpar@1: int CPXgetnumrows(CPXENV *env, CPXLP *lp) alpar@1: { int numrows; alpar@1: if (checklp(env, lp)) alpar@1: numrows = 0; alpar@1: else alpar@1: numrows = glp_get_num_rows(lp->prob); alpar@1: return numrows; alpar@1: } alpar@1: alpar@1: int CPXgetobjval(CPXENV *env, CPXLP *lp, double *objval) alpar@1: { int errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (!lp->stat) alpar@1: { errcode = error(env, CPXERR_NO_SOLN); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: { if (objval != NULL) alpar@1: *objval = glp_get_obj_val(lp->prob); alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetpi(CPXENV *env, CPXLP *lp, double pi[], int begin, int end) alpar@1: { int i, m, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: if (!(0 <= begin && begin <= end && end < m)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: if (!lp->stat) alpar@1: { errcode = error(env, CPXERR_NO_SOLN); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: { if (pi != NULL) alpar@1: { for (i = begin; i <= end; i++) alpar@1: pi[i-begin] = glp_get_row_dual(lp->prob, i+1); alpar@1: } alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetsense(CPXENV *env, CPXLP *lp, char sense[], int begin, alpar@1: int end) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(sense == sense); alpar@1: xassert(begin == begin); alpar@1: xassert(end == end); alpar@1: xprintf("CPXgetsense: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXgetslack(CPXENV *env, CPXLP *lp, double slack[], int begin, alpar@1: int end) alpar@1: { int i, m, type, errcode; alpar@1: double temp; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: if (!(0 <= begin && begin <= end && end < m)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: if (!lp->stat) alpar@1: { errcode = error(env, CPXERR_NO_SOLN); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: { if (slack != NULL) alpar@1: { for (i = begin; i <= end; i++) alpar@1: { type = glp_get_row_type(lp->prob, i+1); alpar@1: temp = glp_get_row_prim(lp->prob, i+1); alpar@1: if (lp->rflag[i] == RF_NOT_RANGED) alpar@1: { if (type == GLP_LO || type == GLP_FX) alpar@1: slack[i-begin] = alpar@1: glp_get_row_lb(lp->prob, i+1) - temp; alpar@1: else if (type == GLP_UP) alpar@1: slack[i-begin] = alpar@1: glp_get_row_ub(lp->prob, i+1) - temp; alpar@1: else alpar@1: xassert(type != type); alpar@1: } alpar@1: else if (lp->rflag[i] == RF_RANGED_POS) alpar@1: { xassert(type == GLP_DB || type == GLP_FX); alpar@1: slack[i-begin] = alpar@1: temp - glp_get_row_lb(lp->prob, i+1); alpar@1: } alpar@1: else if (lp->rflag[i] == RF_RANGED_NEG) alpar@1: { xassert(type == GLP_DB); alpar@1: slack[i-begin] = alpar@1: temp - glp_get_row_ub(lp->prob, i+1); alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: } alpar@1: } alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXgetstat(CPXENV *env, CPXLP *lp) alpar@1: { int stat; alpar@1: if (checklp(env, lp)) alpar@1: stat = 0; alpar@1: else alpar@1: stat = lp->stat; alpar@1: return stat; alpar@1: } alpar@1: alpar@1: int CPXgetub(CPXENV *env, CPXLP *lp, double ub[], int begin, int end) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(ub == ub); alpar@1: xassert(begin == begin); alpar@1: xassert(end == end); alpar@1: xprintf("CPXgetub: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXgetweight(CPXENV *env, CPXLP *lp, int rcnt, const int rmatbeg[], alpar@1: const int rmatind[], const double rmatval[], double weight[], alpar@1: int dpriind) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(rcnt == rcnt); alpar@1: xassert(rmatbeg == rmatbeg); alpar@1: xassert(rmatind == rmatind); alpar@1: xassert(rmatval == rmatval); alpar@1: xassert(weight == weight); alpar@1: xassert(dpriind == dpriind); alpar@1: xprintf("CPXgetweight: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXgetx(CPXENV *env, CPXLP *lp, double x[], int begin, int end) alpar@1: { int j, n, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: if (!(0 <= begin && begin <= end && end < n)) alpar@1: { errcode = error(env, CPXERR_INDEX_RANGE); alpar@1: goto done; alpar@1: } alpar@1: if (!lp->stat) alpar@1: { errcode = error(env, CPXERR_NO_SOLN); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: { if (x != NULL) alpar@1: { for (j = begin; j <= end; j++) alpar@1: x[j-begin] = glp_get_col_prim(lp->prob, j+1); alpar@1: } alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXinfodblparam(CPXENV *env, int whichparam, double *defvalue, alpar@1: double *minvalue, double *maxvalue) alpar@1: { int k, errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: k = finddblparam(whichparam); alpar@1: if (k < 0) alpar@1: { errcode = error(env, CPXERR_BAD_PARAM_NUM); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (defvalue != NULL) alpar@1: *defvalue = dblparam[k].defv; alpar@1: if (minvalue != NULL) alpar@1: *minvalue = dblparam[k].minv; alpar@1: if (maxvalue != NULL) alpar@1: *maxvalue = dblparam[k].maxv; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXinfointparam(CPXENV *env, int whichparam, int *defvalue, alpar@1: int *minvalue, int *maxvalue) alpar@1: { int k, errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: k = findintparam(whichparam); alpar@1: if (k < 0) alpar@1: { errcode = error(env, CPXERR_BAD_PARAM_NUM); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: if (defvalue != NULL) alpar@1: *defvalue = intparam[k].defv; alpar@1: if (minvalue != NULL) alpar@1: *minvalue = intparam[k].minv; alpar@1: if (maxvalue != NULL) alpar@1: *maxvalue = intparam[k].maxv; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXmdleave(const CPXENV *env, CPXLP *lp, const int goodlist[], alpar@1: int goodlen, double downratio[], double upratio[]) alpar@1: { int k; alpar@1: xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(goodlist == goodlist); alpar@1: xassert(goodlen >= 0); alpar@1: xassert(downratio != NULL); alpar@1: xassert(upratio != NULL); alpar@1: /* not implemented yet */ alpar@1: for (k = 0; k < goodlen; k++) alpar@1: downratio[k] = upratio[k] = 0.0; alpar@1: return 0; alpar@1: } alpar@1: alpar@1: int CPXnewcols(CPXENV *env, CPXLP *lp, int ccnt, const double obj[], alpar@1: const double lb[], const double ub[], const char ctype[], alpar@1: char *colname[]) alpar@1: { int j, n, kind, type, errcode; alpar@1: double lbnd, ubnd; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (ccnt < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: for (j = 0; j < ccnt; j++) alpar@1: { if (ctype != NULL) alpar@1: { if (!(ctype[j] == 'C' || ctype[j] == 'B' || alpar@1: ctype[j] == 'I')) alpar@1: { errcode = error(env, CPXERR_BAD_CTYPE, j); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: if (colname != NULL) alpar@1: { if (colname[j] == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_NAME, j); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: if (ccnt > 0) alpar@1: glp_add_cols(lp->prob, ccnt); alpar@1: for (j = 0; j < ccnt; j++) alpar@1: { if (colname != NULL) alpar@1: glp_set_col_name(lp->prob, n+j+1, colname[j]); alpar@1: if (obj != NULL) alpar@1: glp_set_obj_coef(lp->prob, n+j+1, obj[j]); alpar@1: lbnd = (lb == NULL ? 0.0 : lb[j]); alpar@1: ubnd = (ub == NULL ? 0.0 : ub[j]); alpar@1: if (lbnd <= -CPX_INFBOUND && ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_FR; alpar@1: else if (ubnd >= +CPX_INFBOUND) alpar@1: type = GLP_LO; alpar@1: else if (lbnd <= -CPX_INFBOUND) alpar@1: type = GLP_UP; alpar@1: else if (lbnd != ubnd) alpar@1: type = GLP_DB; alpar@1: else alpar@1: type = GLP_FX; alpar@1: glp_set_col_bnds(lp->prob, n+j+1, type, lbnd, ubnd); alpar@1: if (ctype != NULL) alpar@1: { if (ctype[j] == 'C') alpar@1: kind = GLP_CV; alpar@1: else if (ctype[j] == 'B') alpar@1: kind = GLP_BV; alpar@1: else if (ctype[j] == 'I') alpar@1: kind = GLP_IV; alpar@1: else alpar@1: xassert(ctype != ctype); alpar@1: glp_set_col_kind(lp->prob, n+j+1, kind); alpar@1: } alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXnewrows(CPXENV *env, CPXLP *lp, int rcnt, const double rhs[], alpar@1: const char sense[], const double rngval[], char *rowname[]) alpar@1: { int i, m, type, errcode; alpar@1: double lbnd, ubnd; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (rcnt < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: for (i = 0; i < rcnt; i++) alpar@1: { if (sense != NULL) alpar@1: { if (!(sense[i] == 'L' || sense[i] == 'E' || alpar@1: sense[i] == 'G' || sense[i] == 'R')) alpar@1: { errcode = error(env, CPXERR_BAD_SENSE, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: if (rowname != NULL) alpar@1: { if (rowname[i] == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_NAME, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: if (rcnt > 0) alpar@1: glp_add_rows(lp->prob, rcnt); alpar@1: enlargerflag(lp); alpar@1: for (i = 0; i < rcnt; i++) alpar@1: { if (rowname != NULL) alpar@1: glp_set_row_name(lp->prob, m+i+1, rowname[i]); alpar@1: lbnd = ubnd = (rhs == NULL ? 0.0 : rhs[i]); alpar@1: if (sense == NULL || sense[i] == 'E') alpar@1: { lp->rflag[m+i] = RF_NOT_RANGED; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else if (sense[i] == 'L') alpar@1: { lp->rflag[m+i] = RF_NOT_RANGED; alpar@1: type = GLP_UP; alpar@1: } alpar@1: else if (sense[i] == 'G') alpar@1: { lp->rflag[m+i] = RF_NOT_RANGED; alpar@1: type = GLP_LO; alpar@1: } alpar@1: else if (sense[i] == 'R') alpar@1: { if (rngval == NULL || rngval[i] == 0.0) alpar@1: { lp->rflag[m+i] = RF_RANGED_POS; alpar@1: type = GLP_FX; alpar@1: } alpar@1: else if (rngval[i] > 0.0) alpar@1: { lp->rflag[m+i] = RF_RANGED_POS; alpar@1: type = GLP_DB; alpar@1: ubnd += rngval[i]; alpar@1: } alpar@1: else /* rngval[i] < 0.0 */ alpar@1: { lp->rflag[m+i] = RF_RANGED_NEG; alpar@1: type = GLP_DB; alpar@1: lbnd += rngval[i]; alpar@1: } alpar@1: } alpar@1: else alpar@1: xassert(sense != sense); alpar@1: glp_set_row_bnds(lp->prob, m+i+1, type, lbnd, ubnd); alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: CPXENV *CPXopenCPLEX(int *status) alpar@1: { CPXENV *env; alpar@1: int k, card; alpar@1: env = xmalloc(sizeof(CPXENV)); alpar@1: env->list = NULL; alpar@1: card = sizeof(intparam) / sizeof(struct intparam); alpar@1: env->intparam = xcalloc(card, sizeof(int)); alpar@1: for (k = 0; k < card; k++) alpar@1: env->intparam[k] = intparam[k].defv; alpar@1: card = sizeof(dblparam) / sizeof(struct dblparam); alpar@1: env->dblparam = xcalloc(card, sizeof(double)); alpar@1: for (k = 0; k < card; k++) alpar@1: env->dblparam[k] = dblparam[k].defv; alpar@1: if (status != NULL) *status = 0; alpar@1: return env; alpar@1: } alpar@1: alpar@1: int CPXpivotin(CPXENV *env, CPXLP *lp, const int rlist[], int rlen) alpar@1: { int i, m, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (rlen < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (rlen > 0 && rlist == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: for (i = 0; i < rlen; i++) alpar@1: { if (!(0 <= rlist[i] && rlist[i] < m)) alpar@1: { errcode = error(env, CPXERR_ROW_INDEX_RANGE, i); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: for (i = 0; i < rlen; i++) alpar@1: { if (glp_get_row_type(lp->prob, rlist[i]+1) != GLP_FX) alpar@1: { if (glp_get_row_stat(lp->prob, rlist[i]+1) != GLP_BS) alpar@1: { /* not implemented yet */ alpar@1: break; alpar@1: } alpar@1: } alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXpivotout(CPXENV *env, CPXLP *lp, const int clist[], int clen) alpar@1: { int j, n, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (clen < 0) alpar@1: { errcode = error(env, CPXERR_BAD_ARGUMENT); alpar@1: goto done; alpar@1: } alpar@1: if (clen > 0 && clist == NULL) alpar@1: { errcode = error(env, CPXERR_NULL_POINTER); alpar@1: goto done; alpar@1: } alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: for (j = 0; j < clen; j++) alpar@1: { if (!(0 <= clist[j] && clist[j] < n)) alpar@1: { errcode = error(env, CPXERR_COL_INDEX_RANGE, j); alpar@1: goto done; alpar@1: } alpar@1: if (glp_get_col_type(lp->prob, clist[j]+1) != GLP_FX) alpar@1: { errcode = error(env, CPXERR_NOT_FIXED); alpar@1: goto done; alpar@1: } alpar@1: } alpar@1: errcode = 0; alpar@1: for (j = 0; j < clen; j++) alpar@1: { if (glp_get_col_stat(lp->prob, clist[j]+1) == GLP_BS) alpar@1: { /* not implemented yet */ alpar@1: break; alpar@1: } alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXprimopt(CPXENV *env, CPXLP *lp); alpar@1: alpar@1: int CPXsavwrite(CPXENV *env, CPXLP *lp, const char *filename) alpar@1: { xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(filename == filename); alpar@1: xprintf("CPXsavwrite: not implemented yet\n"); alpar@1: exit(EXIT_FAILURE); alpar@1: return -1; alpar@1: } alpar@1: alpar@1: int CPXsetdblparam(CPXENV *env, int whichparam, double newvalue) alpar@1: { int k, errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: k = finddblparam(whichparam); alpar@1: if (k < 0) alpar@1: { errcode = error(env, CPXERR_BAD_PARAM_NUM); alpar@1: goto done; alpar@1: } alpar@1: if (newvalue < dblparam[k].minv) alpar@1: { errcode = error(env, CPXERR_PARAM_TOO_SMALL); alpar@1: goto done; alpar@1: } alpar@1: if (newvalue > dblparam[k].maxv) alpar@1: { errcode = error(env, CPXERR_PARAM_TOO_BIG); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: env->dblparam[k] = newvalue; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXsetintparam(CPXENV *env, int whichparam, int newvalue) alpar@1: { int k, errcode; alpar@1: errcode = checkenv(env); alpar@1: if (errcode) goto done; alpar@1: k = findintparam(whichparam); alpar@1: if (k < 0) alpar@1: { errcode = error(env, CPXERR_BAD_PARAM_NUM); alpar@1: goto done; alpar@1: } alpar@1: if (newvalue < intparam[k].minv) alpar@1: { errcode = error(env, CPXERR_PARAM_TOO_SMALL); alpar@1: goto done; alpar@1: } alpar@1: if (newvalue > intparam[k].maxv) alpar@1: { errcode = error(env, CPXERR_PARAM_TOO_BIG); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: env->intparam[k] = newvalue; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXsolninfo(CPXENV *env, CPXLP *lp, int *solnmethod, int *solntype, alpar@1: int *pfeasind, int *dfeasind) alpar@1: { int type, pfeas, dfeas, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: errcode = 0; alpar@1: if (!lp->stat) alpar@1: type = CPX_NO_SOLN, pfeas = dfeas = 0; alpar@1: else if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: { type = CPX_BASIC_SOLN; alpar@1: pfeas = (glp_get_prim_stat(lp->prob) == GLP_FEAS); alpar@1: dfeas = (glp_get_dual_stat(lp->prob) == GLP_FEAS); alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: if (solnmethod != NULL) alpar@1: *solnmethod = lp->meth; alpar@1: if (solntype != NULL) alpar@1: *solntype = type; alpar@1: if (pfeasind != NULL) alpar@1: *pfeasind = pfeas; alpar@1: if (dfeasind != NULL) alpar@1: *dfeasind = dfeas; alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXsolution(CPXENV *env, CPXLP *lp, int *lpstat, double *objval, alpar@1: double x[], double pi[], double slack[], double dj[]) alpar@1: { int m, n, errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (!lp->stat) alpar@1: { errcode = error(env, CPXERR_NO_SOLN); alpar@1: goto done; alpar@1: } alpar@1: errcode = 0; alpar@1: m = glp_get_num_rows(lp->prob); alpar@1: n = glp_get_num_cols(lp->prob); alpar@1: if (lp->meth == CPX_ALG_PRIMAL || lp->meth == CPX_ALG_DUAL) alpar@1: { if (lpstat != NULL) alpar@1: *lpstat = CPXgetstat(env, lp); alpar@1: if (objval != NULL) alpar@1: xassert(CPXgetobjval(env, lp, objval) == 0); alpar@1: if (x != NULL) alpar@1: xassert(CPXgetx(env, lp, x, 0, n-1) == 0); alpar@1: if (pi != NULL) alpar@1: xassert(CPXgetpi(env, lp, pi, 0, m-1) == 0); alpar@1: if (slack != NULL) alpar@1: xassert(CPXgetslack(env, lp, slack, 0, m-1) == 0); alpar@1: if (dj != NULL) alpar@1: xassert(CPXgetdj(env, lp, dj, 0, n-1) == 0); alpar@1: } alpar@1: else alpar@1: xassert(lp != lp); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXstrongbranch(CPXENV *env, CPXLP *lp, const int goodlist[], alpar@1: int goodlen, double downpen[], double uppen[], int itlim) alpar@1: { int k; alpar@1: xassert(env == env); alpar@1: xassert(lp == lp); alpar@1: xassert(goodlist == goodlist); alpar@1: xassert(goodlen >= 0); alpar@1: xassert(downpen != NULL); alpar@1: xassert(uppen != NULL); alpar@1: xassert(itlim == itlim); alpar@1: /* not implemented yet */ alpar@1: for (k = 0; k < goodlen; k++) alpar@1: downpen[k] = uppen[k] = 0.0; alpar@1: return 0; alpar@1: } alpar@1: alpar@1: static int xstrcasecmp(const char *s1, const char *s2) alpar@1: { int c1, c2; alpar@1: for (;;) alpar@1: { c1 = toupper((unsigned char)*s1++); alpar@1: c2 = toupper((unsigned char)*s2++); alpar@1: if (c1 == '\0' || c1 != c2) break; alpar@1: } alpar@1: return c1 - c2; alpar@1: } alpar@1: alpar@1: static void getfiletype(const char *filename, char type[3+1]) alpar@1: { /* determine filetype from filename */ alpar@1: int beg, end; alpar@1: beg = end = strlen(filename); alpar@1: while (beg > 0 && filename[beg-1] != '.' && end - beg < 3) alpar@1: beg--; alpar@1: if (beg > 0 && filename[beg-1] == '.' && alpar@1: xstrcasecmp(&filename[beg], "gz") == 0) alpar@1: { end = --beg; alpar@1: while (beg > 0 && filename[beg-1] != '.' && end - beg < 3) alpar@1: beg--; alpar@1: } alpar@1: if (beg > 0 && filename[beg-1] == '.') alpar@1: { memcpy(type, &filename[beg], end - beg); alpar@1: type[end - beg] = '\0'; alpar@1: } alpar@1: else alpar@1: type[0] = '\0'; alpar@1: return; alpar@1: } alpar@1: alpar@1: int CPXwriteprob(CPXENV *env, CPXLP *lp, const char *filename, alpar@1: const char *filetype) alpar@1: { glp_prob *copy; alpar@1: int errcode; alpar@1: char type[3+1]; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: if (filename == NULL) alpar@1: { errcode = error(env, CPXERR_NO_FILENAME); alpar@1: goto done; alpar@1: } alpar@1: if (filetype == NULL) alpar@1: getfiletype(filename, type), filetype = type; alpar@1: if (xstrcasecmp(filetype, "MPS") == 0) alpar@1: { glp_term_out(GLP_OFF); alpar@1: errcode = glp_write_mps(lp->prob, GLP_MPS_FILE, NULL, filename) alpar@1: ; alpar@1: glp_term_out(GLP_ON); alpar@1: } alpar@1: else if (xstrcasecmp(filetype, "LP") == 0) alpar@1: { glp_term_out(GLP_OFF); alpar@1: errcode = glp_write_lp(lp->prob, NULL, filename); alpar@1: glp_term_out(GLP_ON); alpar@1: } alpar@1: else if (xstrcasecmp(filetype, "RMP") == 0 || alpar@1: xstrcasecmp(filetype, "REW") == 0) alpar@1: { copy = glp_create_prob(); alpar@1: glp_copy_prob(copy, lp->prob, GLP_OFF); alpar@1: glp_term_out(GLP_OFF); alpar@1: errcode = glp_write_mps(copy, GLP_MPS_DECK, NULL, filename); alpar@1: glp_term_out(GLP_ON); alpar@1: glp_delete_prob(copy); alpar@1: } alpar@1: else if (xstrcasecmp(filetype, "RLP") == 0) alpar@1: { copy = glp_create_prob(); alpar@1: glp_copy_prob(copy, lp->prob, GLP_OFF); alpar@1: glp_term_out(GLP_OFF); alpar@1: errcode = glp_write_lp(copy, NULL, filename); alpar@1: glp_term_out(GLP_ON); alpar@1: glp_delete_prob(copy); alpar@1: } alpar@1: else alpar@1: { errcode = error(env, CPXERR_BAD_FILETYPE); alpar@1: goto done; alpar@1: } alpar@1: if (errcode) alpar@1: errcode = error(env, CPXERR_FAIL_OPEN_WRITE, filename); alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: alpar@1: static int solvelp(CPXENV *env, CPXLP *lp, int meth) alpar@1: { glp_smcp parm; alpar@1: int errcode; alpar@1: errcode = checklp(env, lp); alpar@1: if (errcode) goto done; alpar@1: errcode = 0; alpar@1: invalidate(lp); alpar@1: glp_init_smcp(&parm); alpar@1: switch (meth) alpar@1: { case CPX_ALG_PRIMAL: alpar@1: parm.meth = GLP_PRIMAL; alpar@1: break; alpar@1: case CPX_ALG_DUAL: alpar@1: parm.meth = GLP_DUAL; alpar@1: break; alpar@1: default: alpar@1: xassert(meth != meth); alpar@1: } alpar@1: switch (getintparam(env, CPX_PARAM_SIMDISPLAY)) alpar@1: { case 0: alpar@1: parm.msg_lev = GLP_MSG_OFF; alpar@1: break; alpar@1: case 1: alpar@1: parm.msg_lev = GLP_MSG_ALL; alpar@1: break; alpar@1: case 2: alpar@1: parm.msg_lev = GLP_MSG_ALL; alpar@1: parm.out_frq = 1; alpar@1: break; alpar@1: default: alpar@1: xassert(env != env); alpar@1: } alpar@1: xassert(getdblparam == getdblparam); alpar@1: switch (getintparam(env, CPX_PARAM_ADVIND)) alpar@1: { case 0: alpar@1: glp_term_out(GLP_OFF); alpar@1: glp_adv_basis(lp->prob, 0); alpar@1: glp_term_out(GLP_ON); alpar@1: break; alpar@1: case 1: alpar@1: case 2: alpar@1: break; alpar@1: default: alpar@1: xassert(env != env); alpar@1: } alpar@1: if (!glp_bf_exists(lp->prob)) alpar@1: { if (glp_factorize(lp->prob) != 0) alpar@1: { glp_term_out(GLP_OFF); alpar@1: glp_adv_basis(lp->prob, 0); alpar@1: glp_term_out(GLP_ON); alpar@1: if (glp_factorize(lp->prob) != 0) alpar@1: glp_std_basis(lp->prob); alpar@1: } alpar@1: } alpar@1: xassert(glp_simplex(lp->prob, &parm) == 0); alpar@1: switch (glp_get_status(lp->prob)) alpar@1: { case GLP_OPT: alpar@1: lp->stat = CPX_STAT_OPTIMAL; alpar@1: lp->meth = meth; alpar@1: break; alpar@1: case GLP_NOFEAS: alpar@1: lp->stat = CPX_STAT_INFEASIBLE; alpar@1: lp->meth = meth; alpar@1: break; alpar@1: case GLP_UNBND: alpar@1: lp->stat = CPX_STAT_UNBOUNDED; alpar@1: lp->meth = meth; alpar@1: break; alpar@1: default: alpar@1: xassert(lp != lp); alpar@1: } alpar@1: done: return errcode; alpar@1: } alpar@1: alpar@1: int CPXprimopt(CPXENV *env, CPXLP *lp) alpar@1: { int errcode; alpar@1: errcode = solvelp(env, lp, CPX_ALG_PRIMAL); alpar@1: return errcode; alpar@1: } alpar@1: alpar@1: int CPXdualopt(CPXENV *env, CPXLP *lp) alpar@1: { int errcode; alpar@1: errcode = solvelp(env, lp, CPX_ALG_DUAL); alpar@1: return errcode; alpar@1: } alpar@1: alpar@1: int CPXlpopt(CPXENV *env, CPXLP *lp) alpar@1: { int errcode; alpar@1: errcode = solvelp(env, lp, CPX_ALG_PRIMAL); alpar@1: return errcode; alpar@1: } alpar@1: alpar@1: /* eof */