📄 glpmpl04.c
字号:
mpl->phase = 2; xprintf("Reading data section from %s...\n", file); mpl->flag_d = 1; open_input(mpl, file); /* in this case the keyword 'data' is optional */ if (is_literal(mpl, "data")) { get_token(mpl /* data */); if (mpl->token != T_SEMICOLON) error(mpl, "semicolon missing where expected"); get_token(mpl /* ; */); } data_section(mpl); /* process end statement */ end_statement(mpl); xprintf("%d line%s were read\n", mpl->line, mpl->line == 1 ? "" : "s"); close_input(mpl);done: /* return to the calling program */ return mpl->phase;}/*------------------------------------------------------------------------ mpl_generate - generate model.---- *Synopsis*---- #include "glpmpl.h"-- int mpl_generate(MPL *mpl, char *file);---- *Description*---- The routine mpl_generate generates the model using its description-- stored in the translator database. This phase means generating all-- variables, constraints, and objectives, executing check and display-- statements, which precede the solve statement (if it is presented),-- and building the problem instance.---- The character string file specifies the name of output text file, to-- which output produced by display statements should be written. It is-- allowed to specify NULL, in which case the output goes to stdout via-- the routine print.---- This routine should be called once after the routine mpl_read_model-- or mpl_read_data and if one of the latters returned the code 2.---- *Returns*---- The routine mpl_generate returns one of the following codes:---- 3 - model has been successfully generated. In this case the calling-- program may call other api routines to obtain components of the-- problem instance from the translator database.-- 4 - processing failed due to some errors. In this case the calling-- program should call the routine mpl_terminate to terminate model-- processing. */int mpl_generate(MPL *mpl, char *file){ if (!(mpl->phase == 1 || mpl->phase == 2)) xfault("mpl_generate: invalid call sequence\n"); /* set up error handler */ if (setjmp(mpl->jump)) goto done; /* generate model */ mpl->phase = 3; open_output(mpl, file); generate_model(mpl); flush_output(mpl); /* build problem instance */ build_problem(mpl); /* generation phase has been finished */ xprintf("Model has been successfully generated\n");done: /* return to the calling program */ return mpl->phase;}/*------------------------------------------------------------------------ mpl_get_prob_name - obtain problem (model) name.---- *Synopsis*---- #include "glpmpl.h"-- char *mpl_get_prob_name(MPL *mpl);---- *Returns*---- The routine mpl_get_prob_name returns a pointer to internal buffer,-- which contains symbolic name of the problem (model).---- *Note*---- Currently MathProg has no feature to assign a symbolic name to the-- model. Therefore the routine mpl_get_prob_name tries to construct-- such name using the name of input text file containing model section,-- although this is not a good idea (due to portability problems). */char *mpl_get_prob_name(MPL *mpl){ char *name = mpl->mpl_buf; char *file = mpl->mod_file; int k; if (mpl->phase != 3) xfault("mpl_get_prob_name: invalid call sequence\n"); for (;;) { if (strchr(file, '/') != NULL) file = strchr(file, '/') + 1; else if (strchr(file, '\\') != NULL) file = strchr(file, '\\') + 1; else if (strchr(file, ':') != NULL) file = strchr(file, ':') + 1; else break; } for (k = 0; ; k++) { if (k == 255) break; if (!(isalnum((unsigned char)*file) || *file == '_')) break; name[k] = *file++; } if (k == 0) strcpy(name, "Unknown"); else name[k] = '\0'; xassert(strlen(name) <= 255); return name;}/*------------------------------------------------------------------------ mpl_get_num_rows - determine number of rows.---- *Synopsis*---- #include "glpmpl.h"-- int mpl_get_num_rows(MPL *mpl);---- *Returns*---- The routine mpl_get_num_rows returns total number of rows in the-- problem, where each row is an individual constraint or objective. */int mpl_get_num_rows(MPL *mpl){ if (mpl->phase != 3) xfault("mpl_get_num_rows: invalid call sequence\n"); return mpl->m;}/*------------------------------------------------------------------------ mpl_get_num_cols - determine number of columns.---- *Synopsis*---- #include "glpmpl.h"-- int mpl_get_num_cols(MPL *mpl);---- *Returns*---- The routine mpl_get_num_cols returns total number of columns in the-- problem, where each column is an individual variable. */int mpl_get_num_cols(MPL *mpl){ if (mpl->phase != 3) xfault("mpl_get_num_cols: invalid call sequence\n"); return mpl->n;}/*------------------------------------------------------------------------ mpl_get_row_name - obtain row name.---- *Synopsis*---- #include "glpmpl.h"-- char *mpl_get_row_name(MPL *mpl, int i);---- *Returns*---- The routine mpl_get_row_name returns a pointer to internal buffer,-- which contains symbolic name of i-th row of the problem. */char *mpl_get_row_name(MPL *mpl, int i){ char *name = mpl->mpl_buf, *t; int len; if (mpl->phase != 3) xfault("mpl_get_row_name: invalid call sequence\n"); if (!(1 <= i && i <= mpl->m)) xfault("mpl_get_row_name: i = %d; row number out of range\n", i); strcpy(name, mpl->row[i]->con->name); len = strlen(name); xassert(len <= 255); t = format_tuple(mpl, '[', mpl->row[i]->memb->tuple); while (*t) { if (len == 255) break; name[len++] = *t++; } name[len] = '\0'; if (len == 255) strcpy(name+252, "..."); xassert(strlen(name) <= 255); return name;}/*------------------------------------------------------------------------ mpl_get_row_kind - determine row kind.---- *Synopsis*---- #include "glpmpl.h"-- int mpl_get_row_kind(MPL *mpl, int i);---- *Returns*---- The routine mpl_get_row_kind returns the kind of i-th row, which can-- be one of the following:---- MPL_ST - non-free (constraint) row;-- MPL_MIN - free (objective) row to be minimized;-- MPL_MAX - free (objective) row to be maximized. */int mpl_get_row_kind(MPL *mpl, int i){ int kind; if (mpl->phase != 3) xfault("mpl_get_row_kind: invalid call sequence\n"); if (!(1 <= i && i <= mpl->m)) xfault("mpl_get_row_kind: i = %d; row number out of range\n", i); switch (mpl->row[i]->con->type) { case A_CONSTRAINT: kind = MPL_ST; break; case A_MINIMIZE: kind = MPL_MIN; break; case A_MAXIMIZE: kind = MPL_MAX; break; default: xassert(mpl != mpl); } return kind;}/*------------------------------------------------------------------------ mpl_get_row_bnds - obtain row bounds.---- *Synopsis*---- #include "glpmpl.h"-- int mpl_get_row_bnds(MPL *mpl, int i, double *lb, double *ub);---- *Description*---- The routine mpl_get_row_bnds stores lower and upper bounds of i-th-- row of the problem to the locations, which the parameters lb and ub-- point to, respectively. Besides the routine returns the type of the-- i-th row.---- If some of the parameters lb and ub is NULL, the corresponding bound-- value is not stored.---- Types and bounds have the following meaning:---- Type Bounds Note-- ------------------------------------------------------------- MPL_FR -inf < f(x) < +inf Free linear form-- MPL_LO lb <= f(x) < +inf Inequality f(x) >= lb-- MPL_UP -inf < f(x) <= ub Inequality f(x) <= ub-- MPL_DB lb <= f(x) <= ub Inequality lb <= f(x) <= ub-- MPL_FX f(x) = lb Equality f(x) = lb---- where f(x) is the corresponding linear form of the i-th row.---- If the row has no lower bound, *lb is set to zero; if the row has-- no upper bound, *ub is set to zero; and if the row is of fixed type,-- both *lb and *ub are set to the same value.---- *Returns*---- The routine returns the type of the i-th row as it is stated in the-- table above. */int mpl_get_row_bnds(MPL *mpl, int i, double *_lb, double *_ub){ ELEMCON *con; int type; double lb, ub; if (mpl->phase != 3) xfault("mpl_get_row_bnds: invalid call sequence\n"); if (!(1 <= i && i <= mpl->m)) xfault("mpl_get_row_bnds: i = %d; row number out of range\n", i); con = mpl->row[i];#if 0 /* 21/VII-2006 */ if (con->con->lbnd == NULL && con->con->ubnd == NULL) type = MPL_FR, lb = ub = 0.0; else if (con->con->ubnd == NULL) type = MPL_LO, lb = con->lbnd, ub = 0.0; else if (con->con->lbnd == NULL) type = MPL_UP, lb = 0.0, ub = con->ubnd; else if (con->con->lbnd != con->con->ubnd) type = MPL_DB, lb = con->lbnd, ub = con->ubnd; else type = MPL_FX, lb = ub = con->lbnd;#else lb = (con->con->lbnd == NULL ? -DBL_MAX : con->lbnd); ub = (con->con->ubnd == NULL ? +DBL_MAX : con->ubnd); if (lb == -DBL_MAX && ub == +DBL_MAX) type = MPL_FR, lb = ub = 0.0; else if (ub == +DBL_MAX) type = MPL_LO, ub = 0.0; else if (lb == -DBL_MAX) type = MPL_UP, lb = 0.0; else if (con->con->lbnd != con->con->ubnd) type = MPL_DB; else type = MPL_FX;#endif if (_lb != NULL) *_lb = lb; if (_ub != NULL) *_ub = ub; return type;}/*------------------------------------------------------------------------ mpl_get_mat_row - obtain row of the constraint matrix.---- *Synopsis*---- #include "glpmpl.h"-- int mpl_get_mat_row(MPL *mpl, int i, int ndx[], double val[]);---- *Description*---- The routine mpl_get_mat_row stores column indices and numeric values-- of constraint coefficients for the i-th row to locations ndx[1], ...,-- ndx[len] and val[1], ..., val[len], respectively, where 0 <= len <= n-- is number of (structural) non-zero constraint coefficients, and n is-- number of columns in the problem.---- If the parameter ndx is NULL, column indices are not stored. If the-- parameter val is NULL, numeric values are not stored.---- Note that free rows may have constant terms, which are not part of-- the constraint matrix and therefore not reported by this routine. The-- constant term of a particular row can be obtained, if necessary, via-- the routine mpl_get_row_c0.---- *Returns*---- The routine mpl_get_mat_row returns len, which is length of i-th row-- of the constraint matrix (i.e. number of non-zero coefficients). */int mpl_get_mat_row(MPL *mpl, int i, int ndx[], double val[]){ FORMULA *term; int len = 0; if (mpl->phase != 3) xfault("mpl_get_mat_row: invalid call sequence\n"); if (!(1 <= i && i <= mpl->m)) xfault("mpl_get_mat_row: i = %d; row number out of range\n", i); for (term = mpl->row[i]->form; term != NULL; term = term->next) { xassert(term->var != NULL); len++; xassert(len <= mpl->n); if (ndx != NULL) ndx[len] = term->var->j; if (val != NULL) val[len] = term->coef; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -