alpar@9: /* glpapi01.c (problem creating and modifying routines) */ 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 "glpios.h" alpar@9: alpar@9: /* CAUTION: DO NOT CHANGE THE LIMITS BELOW */ alpar@9: alpar@9: #define M_MAX 100000000 /* = 100*10^6 */ alpar@9: /* maximal number of rows in the problem object */ alpar@9: alpar@9: #define N_MAX 100000000 /* = 100*10^6 */ alpar@9: /* maximal number of columns in the problem object */ alpar@9: alpar@9: #define NNZ_MAX 500000000 /* = 500*10^6 */ alpar@9: /* maximal number of constraint coefficients in the problem object */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_create_prob - create problem object alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * glp_prob *glp_create_prob(void); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_create_prob creates a new problem object, which is alpar@9: * initially "empty", i.e. has no rows and columns. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine returns a pointer to the object created, which should be alpar@9: * used in any subsequent operations on this object. */ alpar@9: alpar@9: static void create_prob(glp_prob *lp) alpar@9: { lp->magic = GLP_PROB_MAGIC; alpar@9: lp->pool = dmp_create_pool(); alpar@9: #if 0 /* 17/XI-2009 */ alpar@9: lp->cps = xmalloc(sizeof(struct LPXCPS)); alpar@9: lpx_reset_parms(lp); alpar@9: #else alpar@9: lp->parms = NULL; alpar@9: #endif alpar@9: lp->tree = NULL; alpar@9: #if 0 alpar@9: lp->lwa = 0; alpar@9: lp->cwa = NULL; alpar@9: #endif alpar@9: /* LP/MIP data */ alpar@9: lp->name = NULL; alpar@9: lp->obj = NULL; alpar@9: lp->dir = GLP_MIN; alpar@9: lp->c0 = 0.0; alpar@9: lp->m_max = 100; alpar@9: lp->n_max = 200; alpar@9: lp->m = lp->n = 0; alpar@9: lp->nnz = 0; alpar@9: lp->row = xcalloc(1+lp->m_max, sizeof(GLPROW *)); alpar@9: lp->col = xcalloc(1+lp->n_max, sizeof(GLPCOL *)); alpar@9: lp->r_tree = lp->c_tree = NULL; alpar@9: /* basis factorization */ alpar@9: lp->valid = 0; alpar@9: lp->head = xcalloc(1+lp->m_max, sizeof(int)); alpar@9: lp->bfcp = NULL; alpar@9: lp->bfd = NULL; alpar@9: /* basic solution (LP) */ alpar@9: lp->pbs_stat = lp->dbs_stat = GLP_UNDEF; alpar@9: lp->obj_val = 0.0; alpar@9: lp->it_cnt = 0; alpar@9: lp->some = 0; alpar@9: /* interior-point solution (LP) */ alpar@9: lp->ipt_stat = GLP_UNDEF; alpar@9: lp->ipt_obj = 0.0; alpar@9: /* integer solution (MIP) */ alpar@9: lp->mip_stat = GLP_UNDEF; alpar@9: lp->mip_obj = 0.0; alpar@9: return; alpar@9: } alpar@9: alpar@9: glp_prob *glp_create_prob(void) alpar@9: { glp_prob *lp; alpar@9: lp = xmalloc(sizeof(glp_prob)); alpar@9: create_prob(lp); alpar@9: return lp; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_prob_name - assign (change) problem name alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_prob_name(glp_prob *lp, const char *name); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_prob_name assigns a given symbolic name (1 up to alpar@9: * 255 characters) to the specified problem object. alpar@9: * alpar@9: * If the parameter name is NULL or empty string, the routine erases an alpar@9: * existing symbolic name of the problem object. */ alpar@9: alpar@9: void glp_set_prob_name(glp_prob *lp, const char *name) alpar@9: { glp_tree *tree = lp->tree; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_set_prob_name: operation not allowed\n"); alpar@9: if (lp->name != NULL) alpar@9: { dmp_free_atom(lp->pool, lp->name, strlen(lp->name)+1); alpar@9: lp->name = NULL; alpar@9: } alpar@9: if (!(name == NULL || name[0] == '\0')) alpar@9: { int k; alpar@9: for (k = 0; name[k] != '\0'; k++) alpar@9: { if (k == 256) alpar@9: xerror("glp_set_prob_name: problem name too long\n"); alpar@9: if (iscntrl((unsigned char)name[k])) alpar@9: xerror("glp_set_prob_name: problem name contains invalid" alpar@9: " character(s)\n"); alpar@9: } alpar@9: lp->name = dmp_get_atom(lp->pool, strlen(name)+1); alpar@9: strcpy(lp->name, name); alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_obj_name - assign (change) objective function name alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_obj_name(glp_prob *lp, const char *name); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_obj_name assigns a given symbolic name (1 up to alpar@9: * 255 characters) to the objective function of the specified problem alpar@9: * object. alpar@9: * alpar@9: * If the parameter name is NULL or empty string, the routine erases an alpar@9: * existing name of the objective function. */ alpar@9: alpar@9: void glp_set_obj_name(glp_prob *lp, const char *name) alpar@9: { glp_tree *tree = lp->tree; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_set_obj_name: operation not allowed\n"); alpar@9: if (lp->obj != NULL) alpar@9: { dmp_free_atom(lp->pool, lp->obj, strlen(lp->obj)+1); alpar@9: lp->obj = NULL; alpar@9: } alpar@9: if (!(name == NULL || name[0] == '\0')) alpar@9: { int k; alpar@9: for (k = 0; name[k] != '\0'; k++) alpar@9: { if (k == 256) alpar@9: xerror("glp_set_obj_name: objective name too long\n"); alpar@9: if (iscntrl((unsigned char)name[k])) alpar@9: xerror("glp_set_obj_name: objective name contains invali" alpar@9: "d character(s)\n"); alpar@9: } alpar@9: lp->obj = dmp_get_atom(lp->pool, strlen(name)+1); alpar@9: strcpy(lp->obj, name); alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_obj_dir - set (change) optimization direction flag alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_obj_dir(glp_prob *lp, int dir); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_obj_dir sets (changes) optimization direction alpar@9: * flag (i.e. "sense" of the objective function) as specified by the alpar@9: * parameter dir: alpar@9: * alpar@9: * GLP_MIN - minimization; alpar@9: * GLP_MAX - maximization. */ alpar@9: alpar@9: void glp_set_obj_dir(glp_prob *lp, int dir) alpar@9: { glp_tree *tree = lp->tree; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_set_obj_dir: operation not allowed\n"); alpar@9: if (!(dir == GLP_MIN || dir == GLP_MAX)) alpar@9: xerror("glp_set_obj_dir: dir = %d; invalid direction flag\n", alpar@9: dir); alpar@9: lp->dir = dir; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_add_rows - add new rows to problem object alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_add_rows(glp_prob *lp, int nrs); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_add_rows adds nrs rows (constraints) to the specified alpar@9: * problem object. New rows are always added to the end of the row list, alpar@9: * so the ordinal numbers of existing rows remain unchanged. alpar@9: * alpar@9: * Being added each new row is initially free (unbounded) and has empty alpar@9: * list of the constraint coefficients. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_add_rows returns the ordinal number of the first new alpar@9: * row added to the problem object. */ alpar@9: alpar@9: int glp_add_rows(glp_prob *lp, int nrs) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPROW *row; alpar@9: int m_new, i; alpar@9: /* determine new number of rows */ alpar@9: if (nrs < 1) alpar@9: xerror("glp_add_rows: nrs = %d; invalid number of rows\n", alpar@9: nrs); alpar@9: if (nrs > M_MAX - lp->m) alpar@9: xerror("glp_add_rows: nrs = %d; too many rows\n", nrs); alpar@9: m_new = lp->m + nrs; alpar@9: /* increase the room, if necessary */ alpar@9: if (lp->m_max < m_new) alpar@9: { GLPROW **save = lp->row; alpar@9: while (lp->m_max < m_new) alpar@9: { lp->m_max += lp->m_max; alpar@9: xassert(lp->m_max > 0); alpar@9: } alpar@9: lp->row = xcalloc(1+lp->m_max, sizeof(GLPROW *)); alpar@9: memcpy(&lp->row[1], &save[1], lp->m * sizeof(GLPROW *)); alpar@9: xfree(save); alpar@9: /* do not forget about the basis header */ alpar@9: xfree(lp->head); alpar@9: lp->head = xcalloc(1+lp->m_max, sizeof(int)); alpar@9: } alpar@9: /* add new rows to the end of the row list */ alpar@9: for (i = lp->m+1; i <= m_new; i++) alpar@9: { /* create row descriptor */ alpar@9: lp->row[i] = row = dmp_get_atom(lp->pool, sizeof(GLPROW)); alpar@9: row->i = i; alpar@9: row->name = NULL; alpar@9: row->node = NULL; alpar@9: #if 1 /* 20/IX-2008 */ alpar@9: row->level = 0; alpar@9: row->origin = 0; alpar@9: row->klass = 0; alpar@9: if (tree != NULL) alpar@9: { switch (tree->reason) alpar@9: { case 0: alpar@9: break; alpar@9: case GLP_IROWGEN: alpar@9: xassert(tree->curr != NULL); alpar@9: row->level = tree->curr->level; alpar@9: row->origin = GLP_RF_LAZY; alpar@9: break; alpar@9: case GLP_ICUTGEN: alpar@9: xassert(tree->curr != NULL); alpar@9: row->level = tree->curr->level; alpar@9: row->origin = GLP_RF_CUT; alpar@9: break; alpar@9: default: alpar@9: xassert(tree != tree); alpar@9: } alpar@9: } alpar@9: #endif alpar@9: row->type = GLP_FR; alpar@9: row->lb = row->ub = 0.0; alpar@9: row->ptr = NULL; alpar@9: row->rii = 1.0; alpar@9: row->stat = GLP_BS; alpar@9: #if 0 alpar@9: row->bind = -1; alpar@9: #else alpar@9: row->bind = 0; alpar@9: #endif alpar@9: row->prim = row->dual = 0.0; alpar@9: row->pval = row->dval = 0.0; alpar@9: row->mipx = 0.0; alpar@9: } alpar@9: /* set new number of rows */ alpar@9: lp->m = m_new; alpar@9: /* invalidate the basis factorization */ alpar@9: lp->valid = 0; alpar@9: #if 1 alpar@9: if (tree != NULL && tree->reason != 0) tree->reopt = 1; alpar@9: #endif alpar@9: /* return the ordinal number of the first row added */ alpar@9: return m_new - nrs + 1; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_add_cols - add new columns to problem object alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_add_cols(glp_prob *lp, int ncs); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_add_cols adds ncs columns (structural variables) to alpar@9: * the specified problem object. New columns are always added to the end alpar@9: * of the column list, so the ordinal numbers of existing columns remain alpar@9: * unchanged. alpar@9: * alpar@9: * Being added each new column is initially fixed at zero and has empty alpar@9: * list of the constraint coefficients. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_add_cols returns the ordinal number of the first new alpar@9: * column added to the problem object. */ alpar@9: alpar@9: int glp_add_cols(glp_prob *lp, int ncs) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPCOL *col; alpar@9: int n_new, j; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_add_cols: operation not allowed\n"); alpar@9: /* determine new number of columns */ alpar@9: if (ncs < 1) alpar@9: xerror("glp_add_cols: ncs = %d; invalid number of columns\n", alpar@9: ncs); alpar@9: if (ncs > N_MAX - lp->n) alpar@9: xerror("glp_add_cols: ncs = %d; too many columns\n", ncs); alpar@9: n_new = lp->n + ncs; alpar@9: /* increase the room, if necessary */ alpar@9: if (lp->n_max < n_new) alpar@9: { GLPCOL **save = lp->col; alpar@9: while (lp->n_max < n_new) alpar@9: { lp->n_max += lp->n_max; alpar@9: xassert(lp->n_max > 0); alpar@9: } alpar@9: lp->col = xcalloc(1+lp->n_max, sizeof(GLPCOL *)); alpar@9: memcpy(&lp->col[1], &save[1], lp->n * sizeof(GLPCOL *)); alpar@9: xfree(save); alpar@9: } alpar@9: /* add new columns to the end of the column list */ alpar@9: for (j = lp->n+1; j <= n_new; j++) alpar@9: { /* create column descriptor */ alpar@9: lp->col[j] = col = dmp_get_atom(lp->pool, sizeof(GLPCOL)); alpar@9: col->j = j; alpar@9: col->name = NULL; alpar@9: col->node = NULL; alpar@9: col->kind = GLP_CV; alpar@9: col->type = GLP_FX; alpar@9: col->lb = col->ub = 0.0; alpar@9: col->coef = 0.0; alpar@9: col->ptr = NULL; alpar@9: col->sjj = 1.0; alpar@9: col->stat = GLP_NS; alpar@9: #if 0 alpar@9: col->bind = -1; alpar@9: #else alpar@9: col->bind = 0; /* the basis may remain valid */ alpar@9: #endif alpar@9: col->prim = col->dual = 0.0; alpar@9: col->pval = col->dval = 0.0; alpar@9: col->mipx = 0.0; alpar@9: } alpar@9: /* set new number of columns */ alpar@9: lp->n = n_new; alpar@9: /* return the ordinal number of the first column added */ alpar@9: return n_new - ncs + 1; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_row_name - assign (change) row name alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_row_name(glp_prob *lp, int i, const char *name); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_row_name assigns a given symbolic name (1 up to alpar@9: * 255 characters) to i-th row (auxiliary variable) of the specified alpar@9: * problem object. alpar@9: * alpar@9: * If the parameter name is NULL or empty string, the routine erases an alpar@9: * existing name of i-th row. */ alpar@9: alpar@9: void glp_set_row_name(glp_prob *lp, int i, const char *name) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPROW *row; alpar@9: if (!(1 <= i && i <= lp->m)) alpar@9: xerror("glp_set_row_name: i = %d; row number out of range\n", alpar@9: i); alpar@9: row = lp->row[i]; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: { xassert(tree->curr != NULL); alpar@9: xassert(row->level == tree->curr->level); alpar@9: } alpar@9: if (row->name != NULL) alpar@9: { if (row->node != NULL) alpar@9: { xassert(lp->r_tree != NULL); alpar@9: avl_delete_node(lp->r_tree, row->node); alpar@9: row->node = NULL; alpar@9: } alpar@9: dmp_free_atom(lp->pool, row->name, strlen(row->name)+1); alpar@9: row->name = NULL; alpar@9: } alpar@9: if (!(name == NULL || name[0] == '\0')) alpar@9: { int k; alpar@9: for (k = 0; name[k] != '\0'; k++) alpar@9: { if (k == 256) alpar@9: xerror("glp_set_row_name: i = %d; row name too long\n", alpar@9: i); alpar@9: if (iscntrl((unsigned char)name[k])) alpar@9: xerror("glp_set_row_name: i = %d: row name contains inva" alpar@9: "lid character(s)\n", i); alpar@9: } alpar@9: row->name = dmp_get_atom(lp->pool, strlen(name)+1); alpar@9: strcpy(row->name, name); alpar@9: if (lp->r_tree != NULL) alpar@9: { xassert(row->node == NULL); alpar@9: row->node = avl_insert_node(lp->r_tree, row->name); alpar@9: avl_set_node_link(row->node, row); alpar@9: } alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_col_name - assign (change) column name alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_col_name(glp_prob *lp, int j, const char *name); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_col_name assigns a given symbolic name (1 up to alpar@9: * 255 characters) to j-th column (structural variable) of the specified alpar@9: * problem object. alpar@9: * alpar@9: * If the parameter name is NULL or empty string, the routine erases an alpar@9: * existing name of j-th column. */ alpar@9: alpar@9: void glp_set_col_name(glp_prob *lp, int j, const char *name) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPCOL *col; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_set_col_name: operation not allowed\n"); alpar@9: if (!(1 <= j && j <= lp->n)) alpar@9: xerror("glp_set_col_name: j = %d; column number out of range\n" alpar@9: , j); alpar@9: col = lp->col[j]; alpar@9: if (col->name != NULL) alpar@9: { if (col->node != NULL) alpar@9: { xassert(lp->c_tree != NULL); alpar@9: avl_delete_node(lp->c_tree, col->node); alpar@9: col->node = NULL; alpar@9: } alpar@9: dmp_free_atom(lp->pool, col->name, strlen(col->name)+1); alpar@9: col->name = NULL; alpar@9: } alpar@9: if (!(name == NULL || name[0] == '\0')) alpar@9: { int k; alpar@9: for (k = 0; name[k] != '\0'; k++) alpar@9: { if (k == 256) alpar@9: xerror("glp_set_col_name: j = %d; column name too long\n" alpar@9: , j); alpar@9: if (iscntrl((unsigned char)name[k])) alpar@9: xerror("glp_set_col_name: j = %d: column name contains i" alpar@9: "nvalid character(s)\n", j); alpar@9: } alpar@9: col->name = dmp_get_atom(lp->pool, strlen(name)+1); alpar@9: strcpy(col->name, name); alpar@9: if (lp->c_tree != NULL && col->name != NULL) alpar@9: { xassert(col->node == NULL); alpar@9: col->node = avl_insert_node(lp->c_tree, col->name); alpar@9: avl_set_node_link(col->node, col); alpar@9: } alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_row_bnds - set (change) row bounds alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_row_bnds(glp_prob *lp, int i, int type, double lb, alpar@9: * double ub); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_row_bnds sets (changes) the type and bounds of alpar@9: * i-th row (auxiliary variable) of the specified problem object. alpar@9: * alpar@9: * Parameters type, lb, and ub specify the type, lower bound, and upper alpar@9: * bound, respectively, as follows: alpar@9: * alpar@9: * Type Bounds Comments alpar@9: * ------------------------------------------------------ alpar@9: * GLP_FR -inf < x < +inf Free variable alpar@9: * GLP_LO lb <= x < +inf Variable with lower bound alpar@9: * GLP_UP -inf < x <= ub Variable with upper bound alpar@9: * GLP_DB lb <= x <= ub Double-bounded variable alpar@9: * GLP_FX x = lb Fixed variable alpar@9: * alpar@9: * where x is the auxiliary variable associated with i-th row. alpar@9: * alpar@9: * If the row has no lower bound, the parameter lb is ignored. If the alpar@9: * row has no upper bound, the parameter ub is ignored. If the row is alpar@9: * an equality constraint (i.e. the corresponding auxiliary variable is alpar@9: * of fixed type), only the parameter lb is used while the parameter ub alpar@9: * is ignored. */ alpar@9: alpar@9: void glp_set_row_bnds(glp_prob *lp, int i, int type, double lb, alpar@9: double ub) alpar@9: { GLPROW *row; alpar@9: if (!(1 <= i && i <= lp->m)) alpar@9: xerror("glp_set_row_bnds: i = %d; row number out of range\n", alpar@9: i); alpar@9: row = lp->row[i]; alpar@9: row->type = type; alpar@9: switch (type) alpar@9: { case GLP_FR: alpar@9: row->lb = row->ub = 0.0; alpar@9: if (row->stat != GLP_BS) row->stat = GLP_NF; alpar@9: break; alpar@9: case GLP_LO: alpar@9: row->lb = lb, row->ub = 0.0; alpar@9: if (row->stat != GLP_BS) row->stat = GLP_NL; alpar@9: break; alpar@9: case GLP_UP: alpar@9: row->lb = 0.0, row->ub = ub; alpar@9: if (row->stat != GLP_BS) row->stat = GLP_NU; alpar@9: break; alpar@9: case GLP_DB: alpar@9: row->lb = lb, row->ub = ub; alpar@9: if (!(row->stat == GLP_BS || alpar@9: row->stat == GLP_NL || row->stat == GLP_NU)) alpar@9: row->stat = (fabs(lb) <= fabs(ub) ? GLP_NL : GLP_NU); alpar@9: break; alpar@9: case GLP_FX: alpar@9: row->lb = row->ub = lb; alpar@9: if (row->stat != GLP_BS) row->stat = GLP_NS; alpar@9: break; alpar@9: default: alpar@9: xerror("glp_set_row_bnds: i = %d; type = %d; invalid row ty" alpar@9: "pe\n", i, type); alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_col_bnds - set (change) column bounds alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_col_bnds(glp_prob *lp, int j, int type, double lb, alpar@9: * double ub); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_col_bnds sets (changes) the type and bounds of alpar@9: * j-th column (structural variable) of the specified problem object. alpar@9: * alpar@9: * Parameters type, lb, and ub specify the type, lower bound, and upper alpar@9: * bound, respectively, as follows: alpar@9: * alpar@9: * Type Bounds Comments alpar@9: * ------------------------------------------------------ alpar@9: * GLP_FR -inf < x < +inf Free variable alpar@9: * GLP_LO lb <= x < +inf Variable with lower bound alpar@9: * GLP_UP -inf < x <= ub Variable with upper bound alpar@9: * GLP_DB lb <= x <= ub Double-bounded variable alpar@9: * GLP_FX x = lb Fixed variable alpar@9: * alpar@9: * where x is the structural variable associated with j-th column. alpar@9: * alpar@9: * If the column has no lower bound, the parameter lb is ignored. If the alpar@9: * column has no upper bound, the parameter ub is ignored. If the column alpar@9: * is of fixed type, only the parameter lb is used while the parameter alpar@9: * ub is ignored. */ alpar@9: alpar@9: void glp_set_col_bnds(glp_prob *lp, int j, int type, double lb, alpar@9: double ub) alpar@9: { GLPCOL *col; alpar@9: if (!(1 <= j && j <= lp->n)) alpar@9: xerror("glp_set_col_bnds: j = %d; column number out of range\n" alpar@9: , j); alpar@9: col = lp->col[j]; alpar@9: col->type = type; alpar@9: switch (type) alpar@9: { case GLP_FR: alpar@9: col->lb = col->ub = 0.0; alpar@9: if (col->stat != GLP_BS) col->stat = GLP_NF; alpar@9: break; alpar@9: case GLP_LO: alpar@9: col->lb = lb, col->ub = 0.0; alpar@9: if (col->stat != GLP_BS) col->stat = GLP_NL; alpar@9: break; alpar@9: case GLP_UP: alpar@9: col->lb = 0.0, col->ub = ub; alpar@9: if (col->stat != GLP_BS) col->stat = GLP_NU; alpar@9: break; alpar@9: case GLP_DB: alpar@9: col->lb = lb, col->ub = ub; alpar@9: if (!(col->stat == GLP_BS || alpar@9: col->stat == GLP_NL || col->stat == GLP_NU)) alpar@9: col->stat = (fabs(lb) <= fabs(ub) ? GLP_NL : GLP_NU); alpar@9: break; alpar@9: case GLP_FX: alpar@9: col->lb = col->ub = lb; alpar@9: if (col->stat != GLP_BS) col->stat = GLP_NS; alpar@9: break; alpar@9: default: alpar@9: xerror("glp_set_col_bnds: j = %d; type = %d; invalid column" alpar@9: " type\n", j, type); alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_obj_coef - set (change) obj. coefficient or constant term alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_obj_coef(glp_prob *lp, int j, double coef); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_obj_coef sets (changes) objective coefficient at alpar@9: * j-th column (structural variable) of the specified problem object. alpar@9: * alpar@9: * If the parameter j is 0, the routine sets (changes) the constant term alpar@9: * ("shift") of the objective function. */ alpar@9: alpar@9: void glp_set_obj_coef(glp_prob *lp, int j, double coef) alpar@9: { glp_tree *tree = lp->tree; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_set_obj_coef: operation not allowed\n"); alpar@9: if (!(0 <= j && j <= lp->n)) alpar@9: xerror("glp_set_obj_coef: j = %d; column number out of range\n" alpar@9: , j); alpar@9: if (j == 0) alpar@9: lp->c0 = coef; alpar@9: else alpar@9: lp->col[j]->coef = coef; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_mat_row - set (replace) row of the constraint matrix alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_mat_row(glp_prob *lp, int i, int len, const int ind[], alpar@9: * const double val[]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_mat_row stores (replaces) the contents of i-th alpar@9: * row of the constraint matrix of the specified problem object. alpar@9: * alpar@9: * Column indices and numeric values of new row elements must be placed alpar@9: * in locations ind[1], ..., ind[len] and val[1], ..., val[len], where alpar@9: * 0 <= len <= n is the new length of i-th row, n is the current number alpar@9: * of columns in the problem object. Elements with identical column alpar@9: * indices are not allowed. Zero elements are allowed, but they are not alpar@9: * stored in the constraint matrix. alpar@9: * alpar@9: * If the parameter len is zero, the parameters ind and/or val can be alpar@9: * specified as NULL. */ alpar@9: alpar@9: void glp_set_mat_row(glp_prob *lp, int i, int len, const int ind[], alpar@9: const double val[]) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPROW *row; alpar@9: GLPCOL *col; alpar@9: GLPAIJ *aij, *next; alpar@9: int j, k; alpar@9: /* obtain pointer to i-th row */ alpar@9: if (!(1 <= i && i <= lp->m)) alpar@9: xerror("glp_set_mat_row: i = %d; row number out of range\n", alpar@9: i); alpar@9: row = lp->row[i]; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: { xassert(tree->curr != NULL); alpar@9: xassert(row->level == tree->curr->level); alpar@9: } alpar@9: /* remove all existing elements from i-th row */ alpar@9: while (row->ptr != NULL) alpar@9: { /* take next element in the row */ alpar@9: aij = row->ptr; alpar@9: /* remove the element from the row list */ alpar@9: row->ptr = aij->r_next; alpar@9: /* obtain pointer to corresponding column */ alpar@9: col = aij->col; alpar@9: /* remove the element from the column list */ alpar@9: if (aij->c_prev == NULL) alpar@9: col->ptr = aij->c_next; alpar@9: else alpar@9: aij->c_prev->c_next = aij->c_next; alpar@9: if (aij->c_next == NULL) alpar@9: ; alpar@9: else alpar@9: aij->c_next->c_prev = aij->c_prev; alpar@9: /* return the element to the memory pool */ alpar@9: dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--; alpar@9: /* if the corresponding column is basic, invalidate the basis alpar@9: factorization */ alpar@9: if (col->stat == GLP_BS) lp->valid = 0; alpar@9: } alpar@9: /* store new contents of i-th row */ alpar@9: if (!(0 <= len && len <= lp->n)) alpar@9: xerror("glp_set_mat_row: i = %d; len = %d; invalid row length " alpar@9: "\n", i, len); alpar@9: if (len > NNZ_MAX - lp->nnz) alpar@9: xerror("glp_set_mat_row: i = %d; len = %d; too many constraint" alpar@9: " coefficients\n", i, len); alpar@9: for (k = 1; k <= len; k++) alpar@9: { /* take number j of corresponding column */ alpar@9: j = ind[k]; alpar@9: /* obtain pointer to j-th column */ alpar@9: if (!(1 <= j && j <= lp->n)) alpar@9: xerror("glp_set_mat_row: i = %d; ind[%d] = %d; column index" alpar@9: " out of range\n", i, k, j); alpar@9: col = lp->col[j]; alpar@9: /* if there is element with the same column index, it can only alpar@9: be found in the beginning of j-th column list */ alpar@9: if (col->ptr != NULL && col->ptr->row->i == i) alpar@9: xerror("glp_set_mat_row: i = %d; ind[%d] = %d; duplicate co" alpar@9: "lumn indices not allowed\n", i, k, j); alpar@9: /* create new element */ alpar@9: aij = dmp_get_atom(lp->pool, sizeof(GLPAIJ)), lp->nnz++; alpar@9: aij->row = row; alpar@9: aij->col = col; alpar@9: aij->val = val[k]; alpar@9: /* add the new element to the beginning of i-th row and j-th alpar@9: column lists */ alpar@9: aij->r_prev = NULL; alpar@9: aij->r_next = row->ptr; alpar@9: aij->c_prev = NULL; alpar@9: aij->c_next = col->ptr; alpar@9: if (aij->r_next != NULL) aij->r_next->r_prev = aij; alpar@9: if (aij->c_next != NULL) aij->c_next->c_prev = aij; alpar@9: row->ptr = col->ptr = aij; alpar@9: /* if the corresponding column is basic, invalidate the basis alpar@9: factorization */ alpar@9: if (col->stat == GLP_BS && aij->val != 0.0) lp->valid = 0; alpar@9: } alpar@9: /* remove zero elements from i-th row */ alpar@9: for (aij = row->ptr; aij != NULL; aij = next) alpar@9: { next = aij->r_next; alpar@9: if (aij->val == 0.0) alpar@9: { /* remove the element from the row list */ alpar@9: if (aij->r_prev == NULL) alpar@9: row->ptr = next; alpar@9: else alpar@9: aij->r_prev->r_next = next; alpar@9: if (next == NULL) alpar@9: ; alpar@9: else alpar@9: next->r_prev = aij->r_prev; alpar@9: /* remove the element from the column list */ alpar@9: xassert(aij->c_prev == NULL); alpar@9: aij->col->ptr = aij->c_next; alpar@9: if (aij->c_next != NULL) aij->c_next->c_prev = NULL; alpar@9: /* return the element to the memory pool */ alpar@9: dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--; alpar@9: } alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_set_mat_col - set (replace) column of the constraint matrix alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_set_mat_col(glp_prob *lp, int j, int len, const int ind[], alpar@9: * const double val[]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_set_mat_col stores (replaces) the contents of j-th alpar@9: * column of the constraint matrix of the specified problem object. alpar@9: * alpar@9: * Row indices and numeric values of new column elements must be placed alpar@9: * in locations ind[1], ..., ind[len] and val[1], ..., val[len], where alpar@9: * 0 <= len <= m is the new length of j-th column, m is the current alpar@9: * number of rows in the problem object. Elements with identical column alpar@9: * indices are not allowed. Zero elements are allowed, but they are not alpar@9: * stored in the constraint matrix. alpar@9: * alpar@9: * If the parameter len is zero, the parameters ind and/or val can be alpar@9: * specified as NULL. */ alpar@9: alpar@9: void glp_set_mat_col(glp_prob *lp, int j, int len, const int ind[], alpar@9: const double val[]) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPROW *row; alpar@9: GLPCOL *col; alpar@9: GLPAIJ *aij, *next; alpar@9: int i, k; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_set_mat_col: operation not allowed\n"); alpar@9: /* obtain pointer to j-th column */ alpar@9: if (!(1 <= j && j <= lp->n)) alpar@9: xerror("glp_set_mat_col: j = %d; column number out of range\n", alpar@9: j); alpar@9: col = lp->col[j]; alpar@9: /* remove all existing elements from j-th column */ alpar@9: while (col->ptr != NULL) alpar@9: { /* take next element in the column */ alpar@9: aij = col->ptr; alpar@9: /* remove the element from the column list */ alpar@9: col->ptr = aij->c_next; alpar@9: /* obtain pointer to corresponding row */ alpar@9: row = aij->row; alpar@9: /* remove the element from the row list */ alpar@9: if (aij->r_prev == NULL) alpar@9: row->ptr = aij->r_next; alpar@9: else alpar@9: aij->r_prev->r_next = aij->r_next; alpar@9: if (aij->r_next == NULL) alpar@9: ; alpar@9: else alpar@9: aij->r_next->r_prev = aij->r_prev; alpar@9: /* return the element to the memory pool */ alpar@9: dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--; alpar@9: } alpar@9: /* store new contents of j-th column */ alpar@9: if (!(0 <= len && len <= lp->m)) alpar@9: xerror("glp_set_mat_col: j = %d; len = %d; invalid column leng" alpar@9: "th\n", j, len); alpar@9: if (len > NNZ_MAX - lp->nnz) alpar@9: xerror("glp_set_mat_col: j = %d; len = %d; too many constraint" alpar@9: " coefficients\n", j, len); alpar@9: for (k = 1; k <= len; k++) alpar@9: { /* take number i of corresponding row */ alpar@9: i = ind[k]; alpar@9: /* obtain pointer to i-th row */ alpar@9: if (!(1 <= i && i <= lp->m)) alpar@9: xerror("glp_set_mat_col: j = %d; ind[%d] = %d; row index ou" alpar@9: "t of range\n", j, k, i); alpar@9: row = lp->row[i]; alpar@9: /* if there is element with the same row index, it can only be alpar@9: found in the beginning of i-th row list */ alpar@9: if (row->ptr != NULL && row->ptr->col->j == j) alpar@9: xerror("glp_set_mat_col: j = %d; ind[%d] = %d; duplicate ro" alpar@9: "w indices not allowed\n", j, k, i); alpar@9: /* create new element */ alpar@9: aij = dmp_get_atom(lp->pool, sizeof(GLPAIJ)), lp->nnz++; alpar@9: aij->row = row; alpar@9: aij->col = col; alpar@9: aij->val = val[k]; alpar@9: /* add the new element to the beginning of i-th row and j-th alpar@9: column lists */ alpar@9: aij->r_prev = NULL; alpar@9: aij->r_next = row->ptr; alpar@9: aij->c_prev = NULL; alpar@9: aij->c_next = col->ptr; alpar@9: if (aij->r_next != NULL) aij->r_next->r_prev = aij; alpar@9: if (aij->c_next != NULL) aij->c_next->c_prev = aij; alpar@9: row->ptr = col->ptr = aij; alpar@9: } alpar@9: /* remove zero elements from j-th column */ alpar@9: for (aij = col->ptr; aij != NULL; aij = next) alpar@9: { next = aij->c_next; alpar@9: if (aij->val == 0.0) alpar@9: { /* remove the element from the row list */ alpar@9: xassert(aij->r_prev == NULL); alpar@9: aij->row->ptr = aij->r_next; alpar@9: if (aij->r_next != NULL) aij->r_next->r_prev = NULL; alpar@9: /* remove the element from the column list */ alpar@9: if (aij->c_prev == NULL) alpar@9: col->ptr = next; alpar@9: else alpar@9: aij->c_prev->c_next = next; alpar@9: if (next == NULL) alpar@9: ; alpar@9: else alpar@9: next->c_prev = aij->c_prev; alpar@9: /* return the element to the memory pool */ alpar@9: dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--; alpar@9: } alpar@9: } alpar@9: /* if j-th column is basic, invalidate the basis factorization */ alpar@9: if (col->stat == GLP_BS) lp->valid = 0; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_load_matrix - load (replace) the whole constraint matrix alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_load_matrix(glp_prob *lp, int ne, const int ia[], alpar@9: * const int ja[], const double ar[]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_load_matrix loads the constraint matrix passed in alpar@9: * the arrays ia, ja, and ar into the specified problem object. Before alpar@9: * loading the current contents of the constraint matrix is destroyed. alpar@9: * alpar@9: * Constraint coefficients (elements of the constraint matrix) must be alpar@9: * specified as triplets (ia[k], ja[k], ar[k]) for k = 1, ..., ne, alpar@9: * where ia[k] is the row index, ja[k] is the column index, ar[k] is a alpar@9: * numeric value of corresponding constraint coefficient. The parameter alpar@9: * ne specifies the total number of (non-zero) elements in the matrix alpar@9: * to be loaded. Coefficients with identical indices are not allowed. alpar@9: * Zero coefficients are allowed, however, they are not stored in the alpar@9: * constraint matrix. alpar@9: * alpar@9: * If the parameter ne is zero, the parameters ia, ja, and ar can be alpar@9: * specified as NULL. */ alpar@9: alpar@9: void glp_load_matrix(glp_prob *lp, int ne, const int ia[], alpar@9: const int ja[], const double ar[]) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPROW *row; alpar@9: GLPCOL *col; alpar@9: GLPAIJ *aij, *next; alpar@9: int i, j, k; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_load_matrix: operation not allowed\n"); alpar@9: /* clear the constraint matrix */ alpar@9: for (i = 1; i <= lp->m; i++) alpar@9: { row = lp->row[i]; alpar@9: while (row->ptr != NULL) alpar@9: { aij = row->ptr; alpar@9: row->ptr = aij->r_next; alpar@9: dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--; alpar@9: } alpar@9: } alpar@9: xassert(lp->nnz == 0); alpar@9: for (j = 1; j <= lp->n; j++) lp->col[j]->ptr = NULL; alpar@9: /* load the new contents of the constraint matrix and build its alpar@9: row lists */ alpar@9: if (ne < 0) alpar@9: xerror("glp_load_matrix: ne = %d; invalid number of constraint" alpar@9: " coefficients\n", ne); alpar@9: if (ne > NNZ_MAX) alpar@9: xerror("glp_load_matrix: ne = %d; too many constraint coeffici" alpar@9: "ents\n", ne); alpar@9: for (k = 1; k <= ne; k++) alpar@9: { /* take indices of new element */ alpar@9: i = ia[k], j = ja[k]; alpar@9: /* obtain pointer to i-th row */ alpar@9: if (!(1 <= i && i <= lp->m)) alpar@9: xerror("glp_load_matrix: ia[%d] = %d; row index out of rang" alpar@9: "e\n", k, i); alpar@9: row = lp->row[i]; alpar@9: /* obtain pointer to j-th column */ alpar@9: if (!(1 <= j && j <= lp->n)) alpar@9: xerror("glp_load_matrix: ja[%d] = %d; column index out of r" alpar@9: "ange\n", k, j); alpar@9: col = lp->col[j]; alpar@9: /* create new element */ alpar@9: aij = dmp_get_atom(lp->pool, sizeof(GLPAIJ)), lp->nnz++; alpar@9: aij->row = row; alpar@9: aij->col = col; alpar@9: aij->val = ar[k]; alpar@9: /* add the new element to the beginning of i-th row list */ alpar@9: aij->r_prev = NULL; alpar@9: aij->r_next = row->ptr; alpar@9: if (aij->r_next != NULL) aij->r_next->r_prev = aij; alpar@9: row->ptr = aij; alpar@9: } alpar@9: xassert(lp->nnz == ne); alpar@9: /* build column lists of the constraint matrix and check elements alpar@9: with identical indices */ alpar@9: for (i = 1; i <= lp->m; i++) alpar@9: { for (aij = lp->row[i]->ptr; aij != NULL; aij = aij->r_next) alpar@9: { /* obtain pointer to corresponding column */ alpar@9: col = aij->col; alpar@9: /* if there is element with identical indices, it can only alpar@9: be found in the beginning of j-th column list */ alpar@9: if (col->ptr != NULL && col->ptr->row->i == i) alpar@9: { for (k = 1; k <= ne; k++) alpar@9: if (ia[k] == i && ja[k] == col->j) break; alpar@9: xerror("glp_load_mat: ia[%d] = %d; ja[%d] = %d; duplicat" alpar@9: "e indices not allowed\n", k, i, k, col->j); alpar@9: } alpar@9: /* add the element to the beginning of j-th column list */ alpar@9: aij->c_prev = NULL; alpar@9: aij->c_next = col->ptr; alpar@9: if (aij->c_next != NULL) aij->c_next->c_prev = aij; alpar@9: col->ptr = aij; alpar@9: } alpar@9: } alpar@9: /* remove zero elements from the constraint matrix */ alpar@9: for (i = 1; i <= lp->m; i++) alpar@9: { row = lp->row[i]; alpar@9: for (aij = row->ptr; aij != NULL; aij = next) alpar@9: { next = aij->r_next; alpar@9: if (aij->val == 0.0) alpar@9: { /* remove the element from the row list */ alpar@9: if (aij->r_prev == NULL) alpar@9: row->ptr = next; alpar@9: else alpar@9: aij->r_prev->r_next = next; alpar@9: if (next == NULL) alpar@9: ; alpar@9: else alpar@9: next->r_prev = aij->r_prev; alpar@9: /* remove the element from the column list */ alpar@9: if (aij->c_prev == NULL) alpar@9: aij->col->ptr = aij->c_next; alpar@9: else alpar@9: aij->c_prev->c_next = aij->c_next; alpar@9: if (aij->c_next == NULL) alpar@9: ; alpar@9: else alpar@9: aij->c_next->c_prev = aij->c_prev; alpar@9: /* return the element to the memory pool */ alpar@9: dmp_free_atom(lp->pool, aij, sizeof(GLPAIJ)), lp->nnz--; alpar@9: } alpar@9: } alpar@9: } alpar@9: /* invalidate the basis factorization */ alpar@9: lp->valid = 0; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_check_dup - check for duplicate elements in sparse matrix alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * int glp_check_dup(int m, int n, int ne, const int ia[], alpar@9: * const int ja[]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_check_dup checks for duplicate elements (that is, alpar@9: * elements with identical indices) in a sparse matrix specified in the alpar@9: * coordinate format. alpar@9: * alpar@9: * The parameters m and n specifies, respectively, the number of rows alpar@9: * and columns in the matrix, m >= 0, n >= 0. alpar@9: * alpar@9: * The parameter ne specifies the number of (structurally) non-zero alpar@9: * elements in the matrix, ne >= 0. alpar@9: * alpar@9: * Elements of the matrix are specified as doublets (ia[k],ja[k]) for alpar@9: * k = 1,...,ne, where ia[k] is a row index, ja[k] is a column index. alpar@9: * alpar@9: * The routine glp_check_dup can be used prior to a call to the routine alpar@9: * glp_load_matrix to check that the constraint matrix to be loaded has alpar@9: * no duplicate elements. alpar@9: * alpar@9: * RETURNS alpar@9: * alpar@9: * The routine glp_check_dup returns one of the following values: alpar@9: * alpar@9: * 0 - the matrix has no duplicate elements; alpar@9: * alpar@9: * -k - indices ia[k] or/and ja[k] are out of range; alpar@9: * alpar@9: * +k - element (ia[k],ja[k]) is duplicate. */ alpar@9: alpar@9: int glp_check_dup(int m, int n, int ne, const int ia[], const int ja[]) alpar@9: { int i, j, k, *ptr, *next, ret; alpar@9: char *flag; alpar@9: if (m < 0) alpar@9: xerror("glp_check_dup: m = %d; invalid parameter\n"); alpar@9: if (n < 0) alpar@9: xerror("glp_check_dup: n = %d; invalid parameter\n"); alpar@9: if (ne < 0) alpar@9: xerror("glp_check_dup: ne = %d; invalid parameter\n"); alpar@9: if (ne > 0 && ia == NULL) alpar@9: xerror("glp_check_dup: ia = %p; invalid parameter\n", ia); alpar@9: if (ne > 0 && ja == NULL) alpar@9: xerror("glp_check_dup: ja = %p; invalid parameter\n", ja); alpar@9: for (k = 1; k <= ne; k++) alpar@9: { i = ia[k], j = ja[k]; alpar@9: if (!(1 <= i && i <= m && 1 <= j && j <= n)) alpar@9: { ret = -k; alpar@9: goto done; alpar@9: } alpar@9: } alpar@9: if (m == 0 || n == 0) alpar@9: { ret = 0; alpar@9: goto done; alpar@9: } alpar@9: /* allocate working arrays */ alpar@9: ptr = xcalloc(1+m, sizeof(int)); alpar@9: next = xcalloc(1+ne, sizeof(int)); alpar@9: flag = xcalloc(1+n, sizeof(char)); alpar@9: /* build row lists */ alpar@9: for (i = 1; i <= m; i++) alpar@9: ptr[i] = 0; alpar@9: for (k = 1; k <= ne; k++) alpar@9: { i = ia[k]; alpar@9: next[k] = ptr[i]; alpar@9: ptr[i] = k; alpar@9: } alpar@9: /* clear column flags */ alpar@9: for (j = 1; j <= n; j++) alpar@9: flag[j] = 0; alpar@9: /* check for duplicate elements */ alpar@9: for (i = 1; i <= m; i++) alpar@9: { for (k = ptr[i]; k != 0; k = next[k]) alpar@9: { j = ja[k]; alpar@9: if (flag[j]) alpar@9: { /* find first element (i,j) */ alpar@9: for (k = 1; k <= ne; k++) alpar@9: if (ia[k] == i && ja[k] == j) break; alpar@9: xassert(k <= ne); alpar@9: /* find next (duplicate) element (i,j) */ alpar@9: for (k++; k <= ne; k++) alpar@9: if (ia[k] == i && ja[k] == j) break; alpar@9: xassert(k <= ne); alpar@9: ret = +k; alpar@9: goto skip; alpar@9: } alpar@9: flag[j] = 1; alpar@9: } alpar@9: /* clear column flags */ alpar@9: for (k = ptr[i]; k != 0; k = next[k]) alpar@9: flag[ja[k]] = 0; alpar@9: } alpar@9: /* no duplicate element found */ alpar@9: ret = 0; alpar@9: skip: /* free working arrays */ alpar@9: xfree(ptr); alpar@9: xfree(next); alpar@9: xfree(flag); alpar@9: done: return ret; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_sort_matrix - sort elements of the constraint matrix alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_sort_matrix(glp_prob *P); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_sort_matrix sorts elements of the constraint matrix alpar@9: * rebuilding its row and column linked lists. On exit from the routine alpar@9: * the constraint matrix is not changed, however, elements in the row alpar@9: * linked lists become ordered by ascending column indices, and the alpar@9: * elements in the column linked lists become ordered by ascending row alpar@9: * indices. */ alpar@9: alpar@9: void glp_sort_matrix(glp_prob *P) alpar@9: { GLPAIJ *aij; alpar@9: int i, j; alpar@9: if (P == NULL || P->magic != GLP_PROB_MAGIC) alpar@9: xerror("glp_sort_matrix: P = %p; invalid problem object\n", alpar@9: P); alpar@9: /* rebuild row linked lists */ alpar@9: for (i = P->m; i >= 1; i--) alpar@9: P->row[i]->ptr = NULL; alpar@9: for (j = P->n; j >= 1; j--) alpar@9: { for (aij = P->col[j]->ptr; aij != NULL; aij = aij->c_next) alpar@9: { i = aij->row->i; alpar@9: aij->r_prev = NULL; alpar@9: aij->r_next = P->row[i]->ptr; alpar@9: if (aij->r_next != NULL) aij->r_next->r_prev = aij; alpar@9: P->row[i]->ptr = aij; alpar@9: } alpar@9: } alpar@9: /* rebuild column linked lists */ alpar@9: for (j = P->n; j >= 1; j--) alpar@9: P->col[j]->ptr = NULL; alpar@9: for (i = P->m; i >= 1; i--) alpar@9: { for (aij = P->row[i]->ptr; aij != NULL; aij = aij->r_next) alpar@9: { j = aij->col->j; alpar@9: aij->c_prev = NULL; alpar@9: aij->c_next = P->col[j]->ptr; alpar@9: if (aij->c_next != NULL) aij->c_next->c_prev = aij; alpar@9: P->col[j]->ptr = aij; alpar@9: } alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_del_rows - delete rows from problem object alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_del_rows(glp_prob *lp, int nrs, const int num[]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_del_rows deletes rows from the specified problem alpar@9: * object. Ordinal numbers of rows to be deleted should be placed in alpar@9: * locations num[1], ..., num[nrs], where nrs > 0. alpar@9: * alpar@9: * Note that deleting rows involves changing ordinal numbers of other alpar@9: * rows remaining in the problem object. New ordinal numbers of the alpar@9: * remaining rows are assigned under the assumption that the original alpar@9: * order of rows is not changed. */ alpar@9: alpar@9: void glp_del_rows(glp_prob *lp, int nrs, const int num[]) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPROW *row; alpar@9: int i, k, m_new; alpar@9: /* mark rows to be deleted */ alpar@9: if (!(1 <= nrs && nrs <= lp->m)) alpar@9: xerror("glp_del_rows: nrs = %d; invalid number of rows\n", alpar@9: nrs); alpar@9: for (k = 1; k <= nrs; k++) alpar@9: { /* take the number of row to be deleted */ alpar@9: i = num[k]; alpar@9: /* obtain pointer to i-th row */ alpar@9: if (!(1 <= i && i <= lp->m)) alpar@9: xerror("glp_del_rows: num[%d] = %d; row number out of range" alpar@9: "\n", k, i); alpar@9: row = lp->row[i]; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: { if (!(tree->reason == GLP_IROWGEN || alpar@9: tree->reason == GLP_ICUTGEN)) alpar@9: xerror("glp_del_rows: operation not allowed\n"); alpar@9: xassert(tree->curr != NULL); alpar@9: if (row->level != tree->curr->level) alpar@9: xerror("glp_del_rows: num[%d] = %d; invalid attempt to d" alpar@9: "elete row created not in current subproblem\n", k,i); alpar@9: if (row->stat != GLP_BS) alpar@9: xerror("glp_del_rows: num[%d] = %d; invalid attempt to d" alpar@9: "elete active row (constraint)\n", k, i); alpar@9: tree->reinv = 1; alpar@9: } alpar@9: /* check that the row is not marked yet */ alpar@9: if (row->i == 0) alpar@9: xerror("glp_del_rows: num[%d] = %d; duplicate row numbers n" alpar@9: "ot allowed\n", k, i); alpar@9: /* erase symbolic name assigned to the row */ alpar@9: glp_set_row_name(lp, i, NULL); alpar@9: xassert(row->node == NULL); alpar@9: /* erase corresponding row of the constraint matrix */ alpar@9: glp_set_mat_row(lp, i, 0, NULL, NULL); alpar@9: xassert(row->ptr == NULL); alpar@9: /* mark the row to be deleted */ alpar@9: row->i = 0; alpar@9: } alpar@9: /* delete all marked rows from the row list */ alpar@9: m_new = 0; alpar@9: for (i = 1; i <= lp->m; i++) alpar@9: { /* obtain pointer to i-th row */ alpar@9: row = lp->row[i]; alpar@9: /* check if the row is marked */ alpar@9: if (row->i == 0) alpar@9: { /* it is marked, delete it */ alpar@9: dmp_free_atom(lp->pool, row, sizeof(GLPROW)); alpar@9: } alpar@9: else alpar@9: { /* it is not marked; keep it */ alpar@9: row->i = ++m_new; alpar@9: lp->row[row->i] = row; alpar@9: } alpar@9: } alpar@9: /* set new number of rows */ alpar@9: lp->m = m_new; alpar@9: /* invalidate the basis factorization */ alpar@9: lp->valid = 0; alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_del_cols - delete columns from problem object alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_del_cols(glp_prob *lp, int ncs, const int num[]); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_del_cols deletes columns from the specified problem alpar@9: * object. Ordinal numbers of columns to be deleted should be placed in alpar@9: * locations num[1], ..., num[ncs], where ncs > 0. alpar@9: * alpar@9: * Note that deleting columns involves changing ordinal numbers of alpar@9: * other columns remaining in the problem object. New ordinal numbers alpar@9: * of the remaining columns are assigned under the assumption that the alpar@9: * original order of columns is not changed. */ alpar@9: alpar@9: void glp_del_cols(glp_prob *lp, int ncs, const int num[]) alpar@9: { glp_tree *tree = lp->tree; alpar@9: GLPCOL *col; alpar@9: int j, k, n_new; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_del_cols: operation not allowed\n"); alpar@9: /* mark columns to be deleted */ alpar@9: if (!(1 <= ncs && ncs <= lp->n)) alpar@9: xerror("glp_del_cols: ncs = %d; invalid number of columns\n", alpar@9: ncs); alpar@9: for (k = 1; k <= ncs; k++) alpar@9: { /* take the number of column to be deleted */ alpar@9: j = num[k]; alpar@9: /* obtain pointer to j-th column */ alpar@9: if (!(1 <= j && j <= lp->n)) alpar@9: xerror("glp_del_cols: num[%d] = %d; column number out of ra" alpar@9: "nge", k, j); alpar@9: col = lp->col[j]; alpar@9: /* check that the column is not marked yet */ alpar@9: if (col->j == 0) alpar@9: xerror("glp_del_cols: num[%d] = %d; duplicate column number" alpar@9: "s not allowed\n", k, j); alpar@9: /* erase symbolic name assigned to the column */ alpar@9: glp_set_col_name(lp, j, NULL); alpar@9: xassert(col->node == NULL); alpar@9: /* erase corresponding column of the constraint matrix */ alpar@9: glp_set_mat_col(lp, j, 0, NULL, NULL); alpar@9: xassert(col->ptr == NULL); alpar@9: /* mark the column to be deleted */ alpar@9: col->j = 0; alpar@9: /* if it is basic, invalidate the basis factorization */ alpar@9: if (col->stat == GLP_BS) lp->valid = 0; alpar@9: } alpar@9: /* delete all marked columns from the column list */ alpar@9: n_new = 0; alpar@9: for (j = 1; j <= lp->n; j++) alpar@9: { /* obtain pointer to j-th column */ alpar@9: col = lp->col[j]; alpar@9: /* check if the column is marked */ alpar@9: if (col->j == 0) alpar@9: { /* it is marked; delete it */ alpar@9: dmp_free_atom(lp->pool, col, sizeof(GLPCOL)); alpar@9: } alpar@9: else alpar@9: { /* it is not marked; keep it */ alpar@9: col->j = ++n_new; alpar@9: lp->col[col->j] = col; alpar@9: } alpar@9: } alpar@9: /* set new number of columns */ alpar@9: lp->n = n_new; alpar@9: /* if the basis header is still valid, adjust it */ alpar@9: if (lp->valid) alpar@9: { int m = lp->m; alpar@9: int *head = lp->head; alpar@9: for (j = 1; j <= n_new; j++) alpar@9: { k = lp->col[j]->bind; alpar@9: if (k != 0) alpar@9: { xassert(1 <= k && k <= m); alpar@9: head[k] = m + j; alpar@9: } alpar@9: } alpar@9: } alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_copy_prob - copy problem object content alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_copy_prob(glp_prob *dest, glp_prob *prob, int names); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_copy_prob copies the content of the problem object alpar@9: * prob to the problem object dest. alpar@9: * alpar@9: * The parameter names is a flag. If it is non-zero, the routine also alpar@9: * copies all symbolic names; otherwise, if it is zero, symbolic names alpar@9: * are not copied. */ alpar@9: alpar@9: void glp_copy_prob(glp_prob *dest, glp_prob *prob, int names) alpar@9: { glp_tree *tree = dest->tree; alpar@9: glp_bfcp bfcp; alpar@9: int i, j, len, *ind; alpar@9: double *val; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_copy_prob: operation not allowed\n"); alpar@9: if (dest == prob) alpar@9: xerror("glp_copy_prob: copying problem object to itself not al" alpar@9: "lowed\n"); alpar@9: if (!(names == GLP_ON || names == GLP_OFF)) alpar@9: xerror("glp_copy_prob: names = %d; invalid parameter\n", alpar@9: names); alpar@9: glp_erase_prob(dest); alpar@9: if (names && prob->name != NULL) alpar@9: glp_set_prob_name(dest, prob->name); alpar@9: if (names && prob->obj != NULL) alpar@9: glp_set_obj_name(dest, prob->obj); alpar@9: dest->dir = prob->dir; alpar@9: dest->c0 = prob->c0; alpar@9: if (prob->m > 0) alpar@9: glp_add_rows(dest, prob->m); alpar@9: if (prob->n > 0) alpar@9: glp_add_cols(dest, prob->n); alpar@9: glp_get_bfcp(prob, &bfcp); alpar@9: glp_set_bfcp(dest, &bfcp); alpar@9: dest->pbs_stat = prob->pbs_stat; alpar@9: dest->dbs_stat = prob->dbs_stat; alpar@9: dest->obj_val = prob->obj_val; alpar@9: dest->some = prob->some; alpar@9: dest->ipt_stat = prob->ipt_stat; alpar@9: dest->ipt_obj = prob->ipt_obj; alpar@9: dest->mip_stat = prob->mip_stat; alpar@9: dest->mip_obj = prob->mip_obj; alpar@9: for (i = 1; i <= prob->m; i++) alpar@9: { GLPROW *to = dest->row[i]; alpar@9: GLPROW *from = prob->row[i]; alpar@9: if (names && from->name != NULL) alpar@9: glp_set_row_name(dest, i, from->name); alpar@9: to->type = from->type; alpar@9: to->lb = from->lb; alpar@9: to->ub = from->ub; alpar@9: to->rii = from->rii; alpar@9: to->stat = from->stat; alpar@9: to->prim = from->prim; alpar@9: to->dual = from->dual; alpar@9: to->pval = from->pval; alpar@9: to->dval = from->dval; alpar@9: to->mipx = from->mipx; alpar@9: } alpar@9: ind = xcalloc(1+prob->m, sizeof(int)); alpar@9: val = xcalloc(1+prob->m, sizeof(double)); alpar@9: for (j = 1; j <= prob->n; j++) alpar@9: { GLPCOL *to = dest->col[j]; alpar@9: GLPCOL *from = prob->col[j]; alpar@9: if (names && from->name != NULL) alpar@9: glp_set_col_name(dest, j, from->name); alpar@9: to->kind = from->kind; alpar@9: to->type = from->type; alpar@9: to->lb = from->lb; alpar@9: to->ub = from->ub; alpar@9: to->coef = from->coef; alpar@9: len = glp_get_mat_col(prob, j, ind, val); alpar@9: glp_set_mat_col(dest, j, len, ind, val); alpar@9: to->sjj = from->sjj; alpar@9: to->stat = from->stat; alpar@9: to->prim = from->prim; alpar@9: to->dual = from->dual; alpar@9: to->pval = from->pval; alpar@9: to->dval = from->dval; alpar@9: to->mipx = from->mipx; alpar@9: } alpar@9: xfree(ind); alpar@9: xfree(val); alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_erase_prob - erase problem object content alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_erase_prob(glp_prob *lp); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_erase_prob erases the content of the specified alpar@9: * problem object. The effect of this operation is the same as if the alpar@9: * problem object would be deleted with the routine glp_delete_prob and alpar@9: * then created anew with the routine glp_create_prob, with exception alpar@9: * that the handle (pointer) to the problem object remains valid. */ alpar@9: alpar@9: static void delete_prob(glp_prob *lp); alpar@9: alpar@9: void glp_erase_prob(glp_prob *lp) alpar@9: { glp_tree *tree = lp->tree; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_erase_prob: operation not allowed\n"); alpar@9: delete_prob(lp); alpar@9: create_prob(lp); alpar@9: return; alpar@9: } alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * glp_delete_prob - delete problem object alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * void glp_delete_prob(glp_prob *lp); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine glp_delete_prob deletes the specified problem object and alpar@9: * frees all the memory allocated to it. */ alpar@9: alpar@9: static void delete_prob(glp_prob *lp) alpar@9: { lp->magic = 0x3F3F3F3F; alpar@9: dmp_delete_pool(lp->pool); alpar@9: #if 0 /* 17/XI-2009 */ alpar@9: xfree(lp->cps); alpar@9: #else alpar@9: if (lp->parms != NULL) xfree(lp->parms); alpar@9: #endif alpar@9: xassert(lp->tree == NULL); alpar@9: #if 0 alpar@9: if (lp->cwa != NULL) xfree(lp->cwa); alpar@9: #endif alpar@9: xfree(lp->row); alpar@9: xfree(lp->col); alpar@9: if (lp->r_tree != NULL) avl_delete_tree(lp->r_tree); alpar@9: if (lp->c_tree != NULL) avl_delete_tree(lp->c_tree); alpar@9: xfree(lp->head); alpar@9: if (lp->bfcp != NULL) xfree(lp->bfcp); alpar@9: if (lp->bfd != NULL) bfd_delete_it(lp->bfd); alpar@9: return; alpar@9: } alpar@9: alpar@9: void glp_delete_prob(glp_prob *lp) alpar@9: { glp_tree *tree = lp->tree; alpar@9: if (tree != NULL && tree->reason != 0) alpar@9: xerror("glp_delete_prob: operation not allowed\n"); alpar@9: delete_prob(lp); alpar@9: xfree(lp); alpar@9: return; alpar@9: } alpar@9: alpar@9: /* eof */