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