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