alpar@1: /* glpmpl04.c */ 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: #define _GLPSTD_ERRNO alpar@1: #define _GLPSTD_STDIO alpar@1: #include "glpmpl.h" alpar@1: #define xfault xerror alpar@1: #define dmp_create_poolx(size) dmp_create_pool() alpar@1: alpar@1: /**********************************************************************/ alpar@1: /* * * GENERATING AND POSTSOLVING MODEL * * */ alpar@1: /**********************************************************************/ alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- alloc_content - allocate content arrays for all model objects. alpar@1: -- alpar@1: -- This routine allocates content arrays for all existing model objects alpar@1: -- and thereby finalizes creating model. alpar@1: -- alpar@1: -- This routine must be called immediately after reading model section, alpar@1: -- i.e. before reading data section or generating model. */ alpar@1: alpar@1: void alloc_content(MPL *mpl) alpar@1: { STATEMENT *stmt; alpar@1: /* walk through all model statements */ alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: { switch (stmt->type) alpar@1: { case A_SET: alpar@1: /* model set */ alpar@1: xassert(stmt->u.set->array == NULL); alpar@1: stmt->u.set->array = create_array(mpl, A_ELEMSET, alpar@1: stmt->u.set->dim); alpar@1: break; alpar@1: case A_PARAMETER: alpar@1: /* model parameter */ alpar@1: xassert(stmt->u.par->array == NULL); alpar@1: switch (stmt->u.par->type) alpar@1: { case A_NUMERIC: alpar@1: case A_INTEGER: alpar@1: case A_BINARY: alpar@1: stmt->u.par->array = create_array(mpl, A_NUMERIC, alpar@1: stmt->u.par->dim); alpar@1: break; alpar@1: case A_SYMBOLIC: alpar@1: stmt->u.par->array = create_array(mpl, A_SYMBOLIC, alpar@1: stmt->u.par->dim); alpar@1: break; alpar@1: default: alpar@1: xassert(stmt != stmt); alpar@1: } alpar@1: break; alpar@1: case A_VARIABLE: alpar@1: /* model variable */ alpar@1: xassert(stmt->u.var->array == NULL); alpar@1: stmt->u.var->array = create_array(mpl, A_ELEMVAR, alpar@1: stmt->u.var->dim); alpar@1: break; alpar@1: case A_CONSTRAINT: alpar@1: /* model constraint/objective */ alpar@1: xassert(stmt->u.con->array == NULL); alpar@1: stmt->u.con->array = create_array(mpl, A_ELEMCON, alpar@1: stmt->u.con->dim); alpar@1: break; alpar@1: #if 1 /* 11/II-2008 */ alpar@1: case A_TABLE: alpar@1: #endif alpar@1: case A_SOLVE: alpar@1: case A_CHECK: alpar@1: case A_DISPLAY: alpar@1: case A_PRINTF: alpar@1: case A_FOR: alpar@1: /* functional statements have no content array */ alpar@1: break; alpar@1: default: alpar@1: xassert(stmt != stmt); alpar@1: } alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- generate_model - generate model. alpar@1: -- alpar@1: -- This routine executes the model statements which precede the solve alpar@1: -- statement. */ alpar@1: alpar@1: void generate_model(MPL *mpl) alpar@1: { STATEMENT *stmt; alpar@1: xassert(!mpl->flag_p); alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: { execute_statement(mpl, stmt); alpar@1: if (mpl->stmt->type == A_SOLVE) break; alpar@1: } alpar@1: mpl->stmt = stmt; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- build_problem - build problem instance. alpar@1: -- alpar@1: -- This routine builds lists of rows and columns for problem instance, alpar@1: -- which corresponds to the generated model. */ alpar@1: alpar@1: void build_problem(MPL *mpl) alpar@1: { STATEMENT *stmt; alpar@1: MEMBER *memb; alpar@1: VARIABLE *v; alpar@1: CONSTRAINT *c; alpar@1: FORMULA *t; alpar@1: int i, j; alpar@1: xassert(mpl->m == 0); alpar@1: xassert(mpl->n == 0); alpar@1: xassert(mpl->row == NULL); alpar@1: xassert(mpl->col == NULL); alpar@1: /* check that all elemental variables has zero column numbers */ alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: { if (stmt->type == A_VARIABLE) alpar@1: { v = stmt->u.var; alpar@1: for (memb = v->array->head; memb != NULL; memb = memb->next) alpar@1: xassert(memb->value.var->j == 0); alpar@1: } alpar@1: } alpar@1: /* assign row numbers to elemental constraints and objectives */ alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: { if (stmt->type == A_CONSTRAINT) alpar@1: { c = stmt->u.con; alpar@1: for (memb = c->array->head; memb != NULL; memb = memb->next) alpar@1: { xassert(memb->value.con->i == 0); alpar@1: memb->value.con->i = ++mpl->m; alpar@1: /* walk through linear form and mark elemental variables, alpar@1: which are referenced at least once */ alpar@1: for (t = memb->value.con->form; t != NULL; t = t->next) alpar@1: { xassert(t->var != NULL); alpar@1: t->var->memb->value.var->j = -1; alpar@1: } alpar@1: } alpar@1: } alpar@1: } alpar@1: /* assign column numbers to marked elemental variables */ alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: { if (stmt->type == A_VARIABLE) alpar@1: { v = stmt->u.var; alpar@1: for (memb = v->array->head; memb != NULL; memb = memb->next) alpar@1: if (memb->value.var->j != 0) memb->value.var->j = alpar@1: ++mpl->n; alpar@1: } alpar@1: } alpar@1: /* build list of rows */ alpar@1: mpl->row = xcalloc(1+mpl->m, sizeof(ELEMCON *)); alpar@1: for (i = 1; i <= mpl->m; i++) mpl->row[i] = NULL; alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: { if (stmt->type == A_CONSTRAINT) alpar@1: { c = stmt->u.con; alpar@1: for (memb = c->array->head; memb != NULL; memb = memb->next) alpar@1: { i = memb->value.con->i; alpar@1: xassert(1 <= i && i <= mpl->m); alpar@1: xassert(mpl->row[i] == NULL); alpar@1: mpl->row[i] = memb->value.con; alpar@1: } alpar@1: } alpar@1: } alpar@1: for (i = 1; i <= mpl->m; i++) xassert(mpl->row[i] != NULL); alpar@1: /* build list of columns */ alpar@1: mpl->col = xcalloc(1+mpl->n, sizeof(ELEMVAR *)); alpar@1: for (j = 1; j <= mpl->n; j++) mpl->col[j] = NULL; alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: { if (stmt->type == A_VARIABLE) alpar@1: { v = stmt->u.var; alpar@1: for (memb = v->array->head; memb != NULL; memb = memb->next) alpar@1: { j = memb->value.var->j; alpar@1: if (j == 0) continue; alpar@1: xassert(1 <= j && j <= mpl->n); alpar@1: xassert(mpl->col[j] == NULL); alpar@1: mpl->col[j] = memb->value.var; alpar@1: } alpar@1: } alpar@1: } alpar@1: for (j = 1; j <= mpl->n; j++) xassert(mpl->col[j] != NULL); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- postsolve_model - postsolve model. alpar@1: -- alpar@1: -- This routine executes the model statements which follow the solve alpar@1: -- statement. */ alpar@1: alpar@1: void postsolve_model(MPL *mpl) alpar@1: { STATEMENT *stmt; alpar@1: xassert(!mpl->flag_p); alpar@1: mpl->flag_p = 1; alpar@1: for (stmt = mpl->stmt; stmt != NULL; stmt = stmt->next) alpar@1: execute_statement(mpl, stmt); alpar@1: mpl->stmt = NULL; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- clean_model - clean model content. alpar@1: -- alpar@1: -- This routine cleans the model content that assumes deleting all stuff alpar@1: -- dynamically allocated on generating/postsolving phase. alpar@1: -- alpar@1: -- Actually cleaning model content is not needed. This function is used alpar@1: -- mainly to be sure that there were no logical errors on using dynamic alpar@1: -- memory pools during the generation phase. alpar@1: -- alpar@1: -- NOTE: This routine must not be called if any errors were detected on alpar@1: -- the generation phase. */ alpar@1: alpar@1: void clean_model(MPL *mpl) alpar@1: { STATEMENT *stmt; alpar@1: for (stmt = mpl->model; stmt != NULL; stmt = stmt->next) alpar@1: clean_statement(mpl, stmt); alpar@1: /* check that all atoms have been returned to their pools */ alpar@1: if (dmp_in_use(mpl->strings).lo != 0) alpar@1: error(mpl, "internal logic error: %d string segment(s) were lo" alpar@1: "st", dmp_in_use(mpl->strings).lo); alpar@1: if (dmp_in_use(mpl->symbols).lo != 0) alpar@1: error(mpl, "internal logic error: %d symbol(s) were lost", alpar@1: dmp_in_use(mpl->symbols).lo); alpar@1: if (dmp_in_use(mpl->tuples).lo != 0) alpar@1: error(mpl, "internal logic error: %d n-tuple component(s) were" alpar@1: " lost", dmp_in_use(mpl->tuples).lo); alpar@1: if (dmp_in_use(mpl->arrays).lo != 0) alpar@1: error(mpl, "internal logic error: %d array(s) were lost", alpar@1: dmp_in_use(mpl->arrays).lo); alpar@1: if (dmp_in_use(mpl->members).lo != 0) alpar@1: error(mpl, "internal logic error: %d array member(s) were lost" alpar@1: , dmp_in_use(mpl->members).lo); alpar@1: if (dmp_in_use(mpl->elemvars).lo != 0) alpar@1: error(mpl, "internal logic error: %d elemental variable(s) wer" alpar@1: "e lost", dmp_in_use(mpl->elemvars).lo); alpar@1: if (dmp_in_use(mpl->formulae).lo != 0) alpar@1: error(mpl, "internal logic error: %d linear term(s) were lost", alpar@1: dmp_in_use(mpl->formulae).lo); alpar@1: if (dmp_in_use(mpl->elemcons).lo != 0) alpar@1: error(mpl, "internal logic error: %d elemental constraint(s) w" alpar@1: "ere lost", dmp_in_use(mpl->elemcons).lo); alpar@1: return; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: /* * * INPUT/OUTPUT * * */ alpar@1: /**********************************************************************/ alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- open_input - open input text file. alpar@1: -- alpar@1: -- This routine opens the input text file for scanning. */ alpar@1: alpar@1: void open_input(MPL *mpl, char *file) alpar@1: { mpl->line = 0; alpar@1: mpl->c = '\n'; alpar@1: mpl->token = 0; alpar@1: mpl->imlen = 0; alpar@1: mpl->image[0] = '\0'; alpar@1: mpl->value = 0.0; alpar@1: mpl->b_token = T_EOF; alpar@1: mpl->b_imlen = 0; alpar@1: mpl->b_image[0] = '\0'; alpar@1: mpl->b_value = 0.0; alpar@1: mpl->f_dots = 0; alpar@1: mpl->f_scan = 0; alpar@1: mpl->f_token = 0; alpar@1: mpl->f_imlen = 0; alpar@1: mpl->f_image[0] = '\0'; alpar@1: mpl->f_value = 0.0; alpar@1: memset(mpl->context, ' ', CONTEXT_SIZE); alpar@1: mpl->c_ptr = 0; alpar@1: xassert(mpl->in_fp == NULL); alpar@1: mpl->in_fp = xfopen(file, "r"); alpar@1: if (mpl->in_fp == NULL) alpar@1: error(mpl, "unable to open %s - %s", file, xerrmsg()); alpar@1: mpl->in_file = file; alpar@1: /* scan the very first character */ alpar@1: get_char(mpl); alpar@1: /* scan the very first token */ alpar@1: get_token(mpl); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- read_char - read next character from input text file. alpar@1: -- alpar@1: -- This routine returns a next ASCII character read from the input text alpar@1: -- file. If the end of file has been reached, EOF is returned. */ alpar@1: alpar@1: int read_char(MPL *mpl) alpar@1: { int c; alpar@1: xassert(mpl->in_fp != NULL); alpar@1: c = xfgetc(mpl->in_fp); alpar@1: if (c < 0) alpar@1: { if (xferror(mpl->in_fp)) alpar@1: error(mpl, "read error on %s - %s", mpl->in_file, alpar@1: xerrmsg()); alpar@1: c = EOF; alpar@1: } alpar@1: return c; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- close_input - close input text file. alpar@1: -- alpar@1: -- This routine closes the input text file. */ alpar@1: alpar@1: void close_input(MPL *mpl) alpar@1: { xassert(mpl->in_fp != NULL); alpar@1: xfclose(mpl->in_fp); alpar@1: mpl->in_fp = NULL; alpar@1: mpl->in_file = NULL; alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- open_output - open output text file. alpar@1: -- alpar@1: -- This routine opens the output text file for writing data produced by alpar@1: -- display and printf statements. */ alpar@1: alpar@1: void open_output(MPL *mpl, char *file) alpar@1: { xassert(mpl->out_fp == NULL); alpar@1: if (file == NULL) alpar@1: { file = ""; alpar@1: mpl->out_fp = (void *)stdout; alpar@1: } alpar@1: else alpar@1: { mpl->out_fp = xfopen(file, "w"); alpar@1: if (mpl->out_fp == NULL) alpar@1: error(mpl, "unable to create %s - %s", file, xerrmsg()); alpar@1: } alpar@1: mpl->out_file = xmalloc(strlen(file)+1); alpar@1: strcpy(mpl->out_file, file); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- write_char - write next character to output text file. alpar@1: -- alpar@1: -- This routine writes an ASCII character to the output text file. */ alpar@1: alpar@1: void write_char(MPL *mpl, int c) alpar@1: { xassert(mpl->out_fp != NULL); alpar@1: if (mpl->out_fp == (void *)stdout) alpar@1: xprintf("%c", c); alpar@1: else alpar@1: xfprintf(mpl->out_fp, "%c", c); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- write_text - format and write text to output text file. alpar@1: -- alpar@1: -- This routine formats a text using the format control string and then alpar@1: -- writes this text to the output text file. */ alpar@1: alpar@1: void write_text(MPL *mpl, char *fmt, ...) alpar@1: { va_list arg; alpar@1: char buf[OUTBUF_SIZE], *c; alpar@1: va_start(arg, fmt); alpar@1: vsprintf(buf, fmt, arg); alpar@1: xassert(strlen(buf) < sizeof(buf)); alpar@1: va_end(arg); alpar@1: for (c = buf; *c != '\0'; c++) write_char(mpl, *c); alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- flush_output - finalize writing data to output text file. alpar@1: -- alpar@1: -- This routine finalizes writing data to the output text file. */ alpar@1: alpar@1: void flush_output(MPL *mpl) alpar@1: { xassert(mpl->out_fp != NULL); alpar@1: if (mpl->out_fp != (void *)stdout) alpar@1: { xfflush(mpl->out_fp); alpar@1: if (xferror(mpl->out_fp)) alpar@1: error(mpl, "write error on %s - %s", mpl->out_file, alpar@1: xerrmsg()); alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /**********************************************************************/ alpar@1: /* * * SOLVER INTERFACE * * */ alpar@1: /**********************************************************************/ alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- error - print error message and terminate model processing. alpar@1: -- alpar@1: -- This routine formats and prints an error message and then terminates alpar@1: -- model processing. */ alpar@1: alpar@1: void error(MPL *mpl, char *fmt, ...) alpar@1: { va_list arg; alpar@1: char msg[4095+1]; alpar@1: va_start(arg, fmt); alpar@1: vsprintf(msg, fmt, arg); alpar@1: xassert(strlen(msg) < sizeof(msg)); alpar@1: va_end(arg); alpar@1: switch (mpl->phase) alpar@1: { case 1: alpar@1: case 2: alpar@1: /* translation phase */ alpar@1: xprintf("%s:%d: %s\n", alpar@1: mpl->in_file == NULL ? "(unknown)" : mpl->in_file, alpar@1: mpl->line, msg); alpar@1: print_context(mpl); alpar@1: break; alpar@1: case 3: alpar@1: /* generation/postsolve phase */ alpar@1: xprintf("%s:%d: %s\n", alpar@1: mpl->mod_file == NULL ? "(unknown)" : mpl->mod_file, alpar@1: mpl->stmt == NULL ? 0 : mpl->stmt->line, msg); alpar@1: break; alpar@1: default: alpar@1: xassert(mpl != mpl); alpar@1: } alpar@1: mpl->phase = 4; alpar@1: longjmp(mpl->jump, 1); alpar@1: /* no return */ alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- warning - print warning message and continue model processing. alpar@1: -- alpar@1: -- This routine formats and prints a warning message and returns to the alpar@1: -- calling program. */ alpar@1: alpar@1: void warning(MPL *mpl, char *fmt, ...) alpar@1: { va_list arg; alpar@1: char msg[4095+1]; alpar@1: va_start(arg, fmt); alpar@1: vsprintf(msg, fmt, arg); alpar@1: xassert(strlen(msg) < sizeof(msg)); alpar@1: va_end(arg); alpar@1: switch (mpl->phase) alpar@1: { case 1: alpar@1: case 2: alpar@1: /* translation phase */ alpar@1: xprintf("%s:%d: warning: %s\n", alpar@1: mpl->in_file == NULL ? "(unknown)" : mpl->in_file, alpar@1: mpl->line, msg); alpar@1: break; alpar@1: case 3: alpar@1: /* generation/postsolve phase */ alpar@1: xprintf("%s:%d: warning: %s\n", alpar@1: mpl->mod_file == NULL ? "(unknown)" : mpl->mod_file, alpar@1: mpl->stmt == NULL ? 0 : mpl->stmt->line, msg); alpar@1: break; alpar@1: default: alpar@1: xassert(mpl != mpl); alpar@1: } alpar@1: return; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_initialize - create and initialize translator database. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- MPL *mpl_initialize(void); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_initialize creates and initializes the database used alpar@1: -- by the GNU MathProg translator. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine returns a pointer to the database created. */ alpar@1: alpar@1: MPL *mpl_initialize(void) alpar@1: { MPL *mpl; alpar@1: mpl = xmalloc(sizeof(MPL)); alpar@1: /* scanning segment */ alpar@1: mpl->line = 0; alpar@1: mpl->c = 0; alpar@1: mpl->token = 0; alpar@1: mpl->imlen = 0; alpar@1: mpl->image = xcalloc(MAX_LENGTH+1, sizeof(char)); alpar@1: mpl->image[0] = '\0'; alpar@1: mpl->value = 0.0; alpar@1: mpl->b_token = 0; alpar@1: mpl->b_imlen = 0; alpar@1: mpl->b_image = xcalloc(MAX_LENGTH+1, sizeof(char)); alpar@1: mpl->b_image[0] = '\0'; alpar@1: mpl->b_value = 0.0; alpar@1: mpl->f_dots = 0; alpar@1: mpl->f_scan = 0; alpar@1: mpl->f_token = 0; alpar@1: mpl->f_imlen = 0; alpar@1: mpl->f_image = xcalloc(MAX_LENGTH+1, sizeof(char)); alpar@1: mpl->f_image[0] = '\0'; alpar@1: mpl->f_value = 0.0; alpar@1: mpl->context = xcalloc(CONTEXT_SIZE, sizeof(char)); alpar@1: memset(mpl->context, ' ', CONTEXT_SIZE); alpar@1: mpl->c_ptr = 0; alpar@1: mpl->flag_d = 0; alpar@1: /* translating segment */ alpar@1: mpl->pool = dmp_create_poolx(0); alpar@1: mpl->tree = avl_create_tree(avl_strcmp, NULL); alpar@1: mpl->model = NULL; alpar@1: mpl->flag_x = 0; alpar@1: mpl->as_within = 0; alpar@1: mpl->as_in = 0; alpar@1: mpl->as_binary = 0; alpar@1: mpl->flag_s = 0; alpar@1: /* common segment */ alpar@1: mpl->strings = dmp_create_poolx(sizeof(STRING)); alpar@1: mpl->symbols = dmp_create_poolx(sizeof(SYMBOL)); alpar@1: mpl->tuples = dmp_create_poolx(sizeof(TUPLE)); alpar@1: mpl->arrays = dmp_create_poolx(sizeof(ARRAY)); alpar@1: mpl->members = dmp_create_poolx(sizeof(MEMBER)); alpar@1: mpl->elemvars = dmp_create_poolx(sizeof(ELEMVAR)); alpar@1: mpl->formulae = dmp_create_poolx(sizeof(FORMULA)); alpar@1: mpl->elemcons = dmp_create_poolx(sizeof(ELEMCON)); alpar@1: mpl->a_list = NULL; alpar@1: mpl->sym_buf = xcalloc(255+1, sizeof(char)); alpar@1: mpl->sym_buf[0] = '\0'; alpar@1: mpl->tup_buf = xcalloc(255+1, sizeof(char)); alpar@1: mpl->tup_buf[0] = '\0'; alpar@1: /* generating/postsolving segment */ alpar@1: mpl->rand = rng_create_rand(); alpar@1: mpl->flag_p = 0; alpar@1: mpl->stmt = NULL; alpar@1: #if 1 /* 11/II-2008 */ alpar@1: mpl->dca = NULL; alpar@1: #endif alpar@1: mpl->m = 0; alpar@1: mpl->n = 0; alpar@1: mpl->row = NULL; alpar@1: mpl->col = NULL; alpar@1: /* input/output segment */ alpar@1: mpl->in_fp = NULL; alpar@1: mpl->in_file = NULL; alpar@1: mpl->out_fp = NULL; alpar@1: mpl->out_file = NULL; alpar@1: mpl->prt_fp = NULL; alpar@1: mpl->prt_file = NULL; alpar@1: /* solver interface segment */ alpar@1: if (setjmp(mpl->jump)) xassert(mpl != mpl); alpar@1: mpl->phase = 0; alpar@1: mpl->mod_file = NULL; alpar@1: mpl->mpl_buf = xcalloc(255+1, sizeof(char)); alpar@1: mpl->mpl_buf[0] = '\0'; alpar@1: return mpl; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_read_model - read model section and optional data section. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_read_model(MPL *mpl, char *file, int skip_data); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_read_model reads model section and optionally data alpar@1: -- section, which may follow the model section, from the text file, alpar@1: -- whose name is the character string file, performs translating model alpar@1: -- statements and data blocks, and stores all the information in the alpar@1: -- translator database. alpar@1: -- alpar@1: -- The parameter skip_data is a flag. If the input file contains the alpar@1: -- data section and this flag is set, the data section is not read as alpar@1: -- if there were no data section and a warning message is issued. This alpar@1: -- allows reading the data section from another input file. alpar@1: -- alpar@1: -- This routine should be called once after the routine mpl_initialize alpar@1: -- and before other API routines. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_read_model returns one the following codes: alpar@1: -- alpar@1: -- 1 - translation successful. The input text file contains only model alpar@1: -- section. In this case the calling program may call the routine alpar@1: -- mpl_read_data to read data section from another file. alpar@1: -- 2 - translation successful. The input text file contains both model alpar@1: -- and data section. alpar@1: -- 4 - processing failed due to some errors. In this case the calling alpar@1: -- program should call the routine mpl_terminate to terminate model alpar@1: -- processing. */ alpar@1: alpar@1: int mpl_read_model(MPL *mpl, char *file, int skip_data) alpar@1: { if (mpl->phase != 0) alpar@1: xfault("mpl_read_model: invalid call sequence\n"); alpar@1: if (file == NULL) alpar@1: xfault("mpl_read_model: no input filename specified\n"); alpar@1: /* set up error handler */ alpar@1: if (setjmp(mpl->jump)) goto done; alpar@1: /* translate model section */ alpar@1: mpl->phase = 1; alpar@1: xprintf("Reading model section from %s...\n", file); alpar@1: open_input(mpl, file); alpar@1: model_section(mpl); alpar@1: if (mpl->model == NULL) alpar@1: error(mpl, "empty model section not allowed"); alpar@1: /* save name of the input text file containing model section for alpar@1: error diagnostics during the generation phase */ alpar@1: mpl->mod_file = xcalloc(strlen(file)+1, sizeof(char)); alpar@1: strcpy(mpl->mod_file, mpl->in_file); alpar@1: /* allocate content arrays for all model objects */ alpar@1: alloc_content(mpl); alpar@1: /* optional data section may begin with the keyword 'data' */ alpar@1: if (is_keyword(mpl, "data")) alpar@1: { if (skip_data) alpar@1: { warning(mpl, "data section ignored"); alpar@1: goto skip; alpar@1: } alpar@1: mpl->flag_d = 1; alpar@1: get_token(mpl /* data */); alpar@1: if (mpl->token != T_SEMICOLON) alpar@1: error(mpl, "semicolon missing where expected"); alpar@1: get_token(mpl /* ; */); alpar@1: /* translate data section */ alpar@1: mpl->phase = 2; alpar@1: xprintf("Reading data section from %s...\n", file); alpar@1: data_section(mpl); alpar@1: } alpar@1: /* process end statement */ alpar@1: end_statement(mpl); alpar@1: skip: xprintf("%d line%s were read\n", alpar@1: mpl->line, mpl->line == 1 ? "" : "s"); alpar@1: close_input(mpl); alpar@1: done: /* return to the calling program */ alpar@1: return mpl->phase; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_read_data - read data section. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_read_data(MPL *mpl, char *file); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_read_data reads data section from the text file, alpar@1: -- whose name is the character string file, performs translating data alpar@1: -- blocks, and stores the data read in the translator database. alpar@1: -- alpar@1: -- If this routine is used, it should be called once after the routine alpar@1: -- mpl_read_model and if the latter returned the code 1. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_read_data returns one of the following codes: alpar@1: -- alpar@1: -- 2 - data section has been successfully processed. alpar@1: -- 4 - processing failed due to some errors. In this case the calling alpar@1: -- program should call the routine mpl_terminate to terminate model alpar@1: -- processing. */ alpar@1: alpar@1: int mpl_read_data(MPL *mpl, char *file) alpar@1: #if 0 /* 02/X-2008 */ alpar@1: { if (mpl->phase != 1) alpar@1: #else alpar@1: { if (!(mpl->phase == 1 || mpl->phase == 2)) alpar@1: #endif alpar@1: xfault("mpl_read_data: invalid call sequence\n"); alpar@1: if (file == NULL) alpar@1: xfault("mpl_read_data: no input filename specified\n"); alpar@1: /* set up error handler */ alpar@1: if (setjmp(mpl->jump)) goto done; alpar@1: /* process data section */ alpar@1: mpl->phase = 2; alpar@1: xprintf("Reading data section from %s...\n", file); alpar@1: mpl->flag_d = 1; alpar@1: open_input(mpl, file); alpar@1: /* in this case the keyword 'data' is optional */ alpar@1: if (is_literal(mpl, "data")) alpar@1: { get_token(mpl /* data */); alpar@1: if (mpl->token != T_SEMICOLON) alpar@1: error(mpl, "semicolon missing where expected"); alpar@1: get_token(mpl /* ; */); alpar@1: } alpar@1: data_section(mpl); alpar@1: /* process end statement */ alpar@1: end_statement(mpl); alpar@1: xprintf("%d line%s were read\n", alpar@1: mpl->line, mpl->line == 1 ? "" : "s"); alpar@1: close_input(mpl); alpar@1: done: /* return to the calling program */ alpar@1: return mpl->phase; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_generate - generate model. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_generate(MPL *mpl, char *file); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_generate generates the model using its description alpar@1: -- stored in the translator database. This phase means generating all alpar@1: -- variables, constraints, and objectives, executing check and display alpar@1: -- statements, which precede the solve statement (if it is presented), alpar@1: -- and building the problem instance. alpar@1: -- alpar@1: -- The character string file specifies the name of output text file, to alpar@1: -- which output produced by display statements should be written. It is alpar@1: -- allowed to specify NULL, in which case the output goes to stdout via alpar@1: -- the routine print. alpar@1: -- alpar@1: -- This routine should be called once after the routine mpl_read_model alpar@1: -- or mpl_read_data and if one of the latters returned the code 2. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_generate returns one of the following codes: alpar@1: -- alpar@1: -- 3 - model has been successfully generated. In this case the calling alpar@1: -- program may call other api routines to obtain components of the alpar@1: -- problem instance from the translator database. alpar@1: -- 4 - processing failed due to some errors. In this case the calling alpar@1: -- program should call the routine mpl_terminate to terminate model alpar@1: -- processing. */ alpar@1: alpar@1: int mpl_generate(MPL *mpl, char *file) alpar@1: { if (!(mpl->phase == 1 || mpl->phase == 2)) alpar@1: xfault("mpl_generate: invalid call sequence\n"); alpar@1: /* set up error handler */ alpar@1: if (setjmp(mpl->jump)) goto done; alpar@1: /* generate model */ alpar@1: mpl->phase = 3; alpar@1: open_output(mpl, file); alpar@1: generate_model(mpl); alpar@1: flush_output(mpl); alpar@1: /* build problem instance */ alpar@1: build_problem(mpl); alpar@1: /* generation phase has been finished */ alpar@1: xprintf("Model has been successfully generated\n"); alpar@1: done: /* return to the calling program */ alpar@1: return mpl->phase; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_prob_name - obtain problem (model) name. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- char *mpl_get_prob_name(MPL *mpl); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_prob_name returns a pointer to internal buffer, alpar@1: -- which contains symbolic name of the problem (model). alpar@1: -- alpar@1: -- *Note* alpar@1: -- alpar@1: -- Currently MathProg has no feature to assign a symbolic name to the alpar@1: -- model. Therefore the routine mpl_get_prob_name tries to construct alpar@1: -- such name using the name of input text file containing model section, alpar@1: -- although this is not a good idea (due to portability problems). */ alpar@1: alpar@1: char *mpl_get_prob_name(MPL *mpl) alpar@1: { char *name = mpl->mpl_buf; alpar@1: char *file = mpl->mod_file; alpar@1: int k; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_prob_name: invalid call sequence\n"); alpar@1: for (;;) alpar@1: { if (strchr(file, '/') != NULL) alpar@1: file = strchr(file, '/') + 1; alpar@1: else if (strchr(file, '\\') != NULL) alpar@1: file = strchr(file, '\\') + 1; alpar@1: else if (strchr(file, ':') != NULL) alpar@1: file = strchr(file, ':') + 1; alpar@1: else alpar@1: break; alpar@1: } alpar@1: for (k = 0; ; k++) alpar@1: { if (k == 255) break; alpar@1: if (!(isalnum((unsigned char)*file) || *file == '_')) break; alpar@1: name[k] = *file++; alpar@1: } alpar@1: if (k == 0) alpar@1: strcpy(name, "Unknown"); alpar@1: else alpar@1: name[k] = '\0'; alpar@1: xassert(strlen(name) <= 255); alpar@1: return name; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_num_rows - determine number of rows. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_get_num_rows(MPL *mpl); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_num_rows returns total number of rows in the alpar@1: -- problem, where each row is an individual constraint or objective. */ alpar@1: alpar@1: int mpl_get_num_rows(MPL *mpl) alpar@1: { if (mpl->phase != 3) alpar@1: xfault("mpl_get_num_rows: invalid call sequence\n"); alpar@1: return mpl->m; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_num_cols - determine number of columns. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_get_num_cols(MPL *mpl); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_num_cols returns total number of columns in the alpar@1: -- problem, where each column is an individual variable. */ alpar@1: alpar@1: int mpl_get_num_cols(MPL *mpl) alpar@1: { if (mpl->phase != 3) alpar@1: xfault("mpl_get_num_cols: invalid call sequence\n"); alpar@1: return mpl->n; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_row_name - obtain row name. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- char *mpl_get_row_name(MPL *mpl, int i); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_row_name returns a pointer to internal buffer, alpar@1: -- which contains symbolic name of i-th row of the problem. */ alpar@1: alpar@1: char *mpl_get_row_name(MPL *mpl, int i) alpar@1: { char *name = mpl->mpl_buf, *t; alpar@1: int len; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_row_name: invalid call sequence\n"); alpar@1: if (!(1 <= i && i <= mpl->m)) alpar@1: xfault("mpl_get_row_name: i = %d; row number out of range\n", alpar@1: i); alpar@1: strcpy(name, mpl->row[i]->con->name); alpar@1: len = strlen(name); alpar@1: xassert(len <= 255); alpar@1: t = format_tuple(mpl, '[', mpl->row[i]->memb->tuple); alpar@1: while (*t) alpar@1: { if (len == 255) break; alpar@1: name[len++] = *t++; alpar@1: } alpar@1: name[len] = '\0'; alpar@1: if (len == 255) strcpy(name+252, "..."); alpar@1: xassert(strlen(name) <= 255); alpar@1: return name; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_row_kind - determine row kind. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_get_row_kind(MPL *mpl, int i); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_row_kind returns the kind of i-th row, which can alpar@1: -- be one of the following: alpar@1: -- alpar@1: -- MPL_ST - non-free (constraint) row; alpar@1: -- MPL_MIN - free (objective) row to be minimized; alpar@1: -- MPL_MAX - free (objective) row to be maximized. */ alpar@1: alpar@1: int mpl_get_row_kind(MPL *mpl, int i) alpar@1: { int kind; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_row_kind: invalid call sequence\n"); alpar@1: if (!(1 <= i && i <= mpl->m)) alpar@1: xfault("mpl_get_row_kind: i = %d; row number out of range\n", alpar@1: i); alpar@1: switch (mpl->row[i]->con->type) alpar@1: { case A_CONSTRAINT: alpar@1: kind = MPL_ST; break; alpar@1: case A_MINIMIZE: alpar@1: kind = MPL_MIN; break; alpar@1: case A_MAXIMIZE: alpar@1: kind = MPL_MAX; break; alpar@1: default: alpar@1: xassert(mpl != mpl); alpar@1: } alpar@1: return kind; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_row_bnds - obtain row bounds. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_get_row_bnds(MPL *mpl, int i, double *lb, double *ub); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_get_row_bnds stores lower and upper bounds of i-th alpar@1: -- row of the problem to the locations, which the parameters lb and ub alpar@1: -- point to, respectively. Besides the routine returns the type of the alpar@1: -- i-th row. alpar@1: -- alpar@1: -- If some of the parameters lb and ub is NULL, the corresponding bound alpar@1: -- value is not stored. alpar@1: -- alpar@1: -- Types and bounds have the following meaning: alpar@1: -- alpar@1: -- Type Bounds Note alpar@1: -- ----------------------------------------------------------- alpar@1: -- MPL_FR -inf < f(x) < +inf Free linear form alpar@1: -- MPL_LO lb <= f(x) < +inf Inequality f(x) >= lb alpar@1: -- MPL_UP -inf < f(x) <= ub Inequality f(x) <= ub alpar@1: -- MPL_DB lb <= f(x) <= ub Inequality lb <= f(x) <= ub alpar@1: -- MPL_FX f(x) = lb Equality f(x) = lb alpar@1: -- alpar@1: -- where f(x) is the corresponding linear form of the i-th row. alpar@1: -- alpar@1: -- If the row has no lower bound, *lb is set to zero; if the row has alpar@1: -- no upper bound, *ub is set to zero; and if the row is of fixed type, alpar@1: -- both *lb and *ub are set to the same value. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine returns the type of the i-th row as it is stated in the alpar@1: -- table above. */ alpar@1: alpar@1: int mpl_get_row_bnds(MPL *mpl, int i, double *_lb, double *_ub) alpar@1: { ELEMCON *con; alpar@1: int type; alpar@1: double lb, ub; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_row_bnds: invalid call sequence\n"); alpar@1: if (!(1 <= i && i <= mpl->m)) alpar@1: xfault("mpl_get_row_bnds: i = %d; row number out of range\n", alpar@1: i); alpar@1: con = mpl->row[i]; alpar@1: #if 0 /* 21/VII-2006 */ alpar@1: if (con->con->lbnd == NULL && con->con->ubnd == NULL) alpar@1: type = MPL_FR, lb = ub = 0.0; alpar@1: else if (con->con->ubnd == NULL) alpar@1: type = MPL_LO, lb = con->lbnd, ub = 0.0; alpar@1: else if (con->con->lbnd == NULL) alpar@1: type = MPL_UP, lb = 0.0, ub = con->ubnd; alpar@1: else if (con->con->lbnd != con->con->ubnd) alpar@1: type = MPL_DB, lb = con->lbnd, ub = con->ubnd; alpar@1: else alpar@1: type = MPL_FX, lb = ub = con->lbnd; alpar@1: #else alpar@1: lb = (con->con->lbnd == NULL ? -DBL_MAX : con->lbnd); alpar@1: ub = (con->con->ubnd == NULL ? +DBL_MAX : con->ubnd); alpar@1: if (lb == -DBL_MAX && ub == +DBL_MAX) alpar@1: type = MPL_FR, lb = ub = 0.0; alpar@1: else if (ub == +DBL_MAX) alpar@1: type = MPL_LO, ub = 0.0; alpar@1: else if (lb == -DBL_MAX) alpar@1: type = MPL_UP, lb = 0.0; alpar@1: else if (con->con->lbnd != con->con->ubnd) alpar@1: type = MPL_DB; alpar@1: else alpar@1: type = MPL_FX; alpar@1: #endif alpar@1: if (_lb != NULL) *_lb = lb; alpar@1: if (_ub != NULL) *_ub = ub; alpar@1: return type; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_mat_row - obtain row of the constraint matrix. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_get_mat_row(MPL *mpl, int i, int ndx[], double val[]); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_get_mat_row stores column indices and numeric values alpar@1: -- of constraint coefficients for the i-th row to locations ndx[1], ..., alpar@1: -- ndx[len] and val[1], ..., val[len], respectively, where 0 <= len <= n alpar@1: -- is number of (structural) non-zero constraint coefficients, and n is alpar@1: -- number of columns in the problem. alpar@1: -- alpar@1: -- If the parameter ndx is NULL, column indices are not stored. If the alpar@1: -- parameter val is NULL, numeric values are not stored. alpar@1: -- alpar@1: -- Note that free rows may have constant terms, which are not part of alpar@1: -- the constraint matrix and therefore not reported by this routine. The alpar@1: -- constant term of a particular row can be obtained, if necessary, via alpar@1: -- the routine mpl_get_row_c0. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_mat_row returns len, which is length of i-th row alpar@1: -- of the constraint matrix (i.e. number of non-zero coefficients). */ alpar@1: alpar@1: int mpl_get_mat_row(MPL *mpl, int i, int ndx[], double val[]) alpar@1: { FORMULA *term; alpar@1: int len = 0; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_mat_row: invalid call sequence\n"); alpar@1: if (!(1 <= i && i <= mpl->m)) alpar@1: xfault("mpl_get_mat_row: i = %d; row number out of range\n", alpar@1: i); alpar@1: for (term = mpl->row[i]->form; term != NULL; term = term->next) alpar@1: { xassert(term->var != NULL); alpar@1: len++; alpar@1: xassert(len <= mpl->n); alpar@1: if (ndx != NULL) ndx[len] = term->var->j; alpar@1: if (val != NULL) val[len] = term->coef; alpar@1: } alpar@1: return len; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_row_c0 - obtain constant term of free row. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- double mpl_get_row_c0(MPL *mpl, int i); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_row_c0 returns numeric value of constant term of alpar@1: -- i-th row. alpar@1: -- alpar@1: -- Note that only free rows may have non-zero constant terms. Therefore alpar@1: -- if i-th row is not free, the routine returns zero. */ alpar@1: alpar@1: double mpl_get_row_c0(MPL *mpl, int i) alpar@1: { ELEMCON *con; alpar@1: double c0; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_row_c0: invalid call sequence\n"); alpar@1: if (!(1 <= i && i <= mpl->m)) alpar@1: xfault("mpl_get_row_c0: i = %d; row number out of range\n", alpar@1: i); alpar@1: con = mpl->row[i]; alpar@1: if (con->con->lbnd == NULL && con->con->ubnd == NULL) alpar@1: c0 = - con->lbnd; alpar@1: else alpar@1: c0 = 0.0; alpar@1: return c0; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_col_name - obtain column name. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- char *mpl_get_col_name(MPL *mpl, int j); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_col_name returns a pointer to internal buffer, alpar@1: -- which contains symbolic name of j-th column of the problem. */ alpar@1: alpar@1: char *mpl_get_col_name(MPL *mpl, int j) alpar@1: { char *name = mpl->mpl_buf, *t; alpar@1: int len; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_col_name: invalid call sequence\n"); alpar@1: if (!(1 <= j && j <= mpl->n)) alpar@1: xfault("mpl_get_col_name: j = %d; column number out of range\n" alpar@1: , j); alpar@1: strcpy(name, mpl->col[j]->var->name); alpar@1: len = strlen(name); alpar@1: xassert(len <= 255); alpar@1: t = format_tuple(mpl, '[', mpl->col[j]->memb->tuple); alpar@1: while (*t) alpar@1: { if (len == 255) break; alpar@1: name[len++] = *t++; alpar@1: } alpar@1: name[len] = '\0'; alpar@1: if (len == 255) strcpy(name+252, "..."); alpar@1: xassert(strlen(name) <= 255); alpar@1: return name; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_col_kind - determine column kind. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_get_col_kind(MPL *mpl, int j); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_get_col_kind returns the kind of j-th column, which alpar@1: -- can be one of the following: alpar@1: -- alpar@1: -- MPL_NUM - continuous variable; alpar@1: -- MPL_INT - integer variable; alpar@1: -- MPL_BIN - binary variable. alpar@1: -- alpar@1: -- Note that column kinds are defined independently on type and bounds alpar@1: -- (reported by the routine mpl_get_col_bnds) of corresponding columns. alpar@1: -- This means, in particular, that bounds of an integer column may be alpar@1: -- fractional, or a binary column may have lower and upper bounds that alpar@1: -- are not 0 and 1 (or it may have no lower/upper bound at all). */ alpar@1: alpar@1: int mpl_get_col_kind(MPL *mpl, int j) alpar@1: { int kind; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_col_kind: invalid call sequence\n"); alpar@1: if (!(1 <= j && j <= mpl->n)) alpar@1: xfault("mpl_get_col_kind: j = %d; column number out of range\n" alpar@1: , j); alpar@1: switch (mpl->col[j]->var->type) alpar@1: { case A_NUMERIC: alpar@1: kind = MPL_NUM; break; alpar@1: case A_INTEGER: alpar@1: kind = MPL_INT; break; alpar@1: case A_BINARY: alpar@1: kind = MPL_BIN; break; alpar@1: default: alpar@1: xassert(mpl != mpl); alpar@1: } alpar@1: return kind; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_get_col_bnds - obtain column bounds. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_get_col_bnds(MPL *mpl, int j, double *lb, double *ub); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_get_col_bnds stores lower and upper bound of j-th alpar@1: -- column of the problem to the locations, which the parameters lb and alpar@1: -- ub point to, respectively. Besides the routine returns the type of alpar@1: -- the j-th column. alpar@1: -- alpar@1: -- If some of the parameters lb and ub is NULL, the corresponding bound alpar@1: -- value is not stored. alpar@1: -- alpar@1: -- Types and bounds have the following meaning: alpar@1: -- alpar@1: -- Type Bounds Note alpar@1: -- ------------------------------------------------------ alpar@1: -- MPL_FR -inf < x < +inf Free (unbounded) variable alpar@1: -- MPL_LO lb <= x < +inf Variable with lower bound alpar@1: -- MPL_UP -inf < x <= ub Variable with upper bound alpar@1: -- MPL_DB lb <= x <= ub Double-bounded variable alpar@1: -- MPL_FX x = lb Fixed variable alpar@1: -- alpar@1: -- where x is individual variable corresponding to the j-th column. alpar@1: -- alpar@1: -- If the column has no lower bound, *lb is set to zero; if the column alpar@1: -- has no upper bound, *ub is set to zero; and if the column is of fixed alpar@1: -- type, both *lb and *ub are set to the same value. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine returns the type of the j-th column as it is stated in alpar@1: -- the table above. */ alpar@1: alpar@1: int mpl_get_col_bnds(MPL *mpl, int j, double *_lb, double *_ub) alpar@1: { ELEMVAR *var; alpar@1: int type; alpar@1: double lb, ub; alpar@1: if (mpl->phase != 3) alpar@1: xfault("mpl_get_col_bnds: invalid call sequence\n"); alpar@1: if (!(1 <= j && j <= mpl->n)) alpar@1: xfault("mpl_get_col_bnds: j = %d; column number out of range\n" alpar@1: , j); alpar@1: var = mpl->col[j]; alpar@1: #if 0 /* 21/VII-2006 */ alpar@1: if (var->var->lbnd == NULL && var->var->ubnd == NULL) alpar@1: type = MPL_FR, lb = ub = 0.0; alpar@1: else if (var->var->ubnd == NULL) alpar@1: type = MPL_LO, lb = var->lbnd, ub = 0.0; alpar@1: else if (var->var->lbnd == NULL) alpar@1: type = MPL_UP, lb = 0.0, ub = var->ubnd; alpar@1: else if (var->var->lbnd != var->var->ubnd) alpar@1: type = MPL_DB, lb = var->lbnd, ub = var->ubnd; alpar@1: else alpar@1: type = MPL_FX, lb = ub = var->lbnd; alpar@1: #else alpar@1: lb = (var->var->lbnd == NULL ? -DBL_MAX : var->lbnd); alpar@1: ub = (var->var->ubnd == NULL ? +DBL_MAX : var->ubnd); alpar@1: if (lb == -DBL_MAX && ub == +DBL_MAX) alpar@1: type = MPL_FR, lb = ub = 0.0; alpar@1: else if (ub == +DBL_MAX) alpar@1: type = MPL_LO, ub = 0.0; alpar@1: else if (lb == -DBL_MAX) alpar@1: type = MPL_UP, lb = 0.0; alpar@1: else if (var->var->lbnd != var->var->ubnd) alpar@1: type = MPL_DB; alpar@1: else alpar@1: type = MPL_FX; alpar@1: #endif alpar@1: if (_lb != NULL) *_lb = lb; alpar@1: if (_ub != NULL) *_ub = ub; alpar@1: return type; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_has_solve_stmt - check if model has solve statement. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_has_solve_stmt(MPL *mpl); alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- If the model has the solve statement, the routine returns non-zero, alpar@1: -- otherwise zero is returned. */ alpar@1: alpar@1: int mpl_has_solve_stmt(MPL *mpl) alpar@1: { if (mpl->phase != 3) alpar@1: xfault("mpl_has_solve_stmt: invalid call sequence\n"); alpar@1: return mpl->flag_s; alpar@1: } alpar@1: alpar@1: #if 1 /* 15/V-2010 */ alpar@1: void mpl_put_row_soln(MPL *mpl, int i, int stat, double prim, alpar@1: double dual) alpar@1: { /* store row (constraint/objective) solution components */ alpar@1: xassert(mpl->phase == 3); alpar@1: xassert(1 <= i && i <= mpl->m); alpar@1: mpl->row[i]->stat = stat; alpar@1: mpl->row[i]->prim = prim; alpar@1: mpl->row[i]->dual = dual; alpar@1: return; alpar@1: } alpar@1: #endif alpar@1: alpar@1: #if 1 /* 15/V-2010 */ alpar@1: void mpl_put_col_soln(MPL *mpl, int j, int stat, double prim, alpar@1: double dual) alpar@1: { /* store column (variable) solution components */ alpar@1: xassert(mpl->phase == 3); alpar@1: xassert(1 <= j && j <= mpl->n); alpar@1: mpl->col[j]->stat = stat; alpar@1: mpl->col[j]->prim = prim; alpar@1: mpl->col[j]->dual = dual; alpar@1: return; alpar@1: } alpar@1: #endif alpar@1: alpar@1: #if 0 /* 15/V-2010 */ alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_put_col_value - store column value. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- void mpl_put_col_value(MPL *mpl, int j, double val); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_put_col_value stores numeric value of j-th column alpar@1: -- into the translator database. It is assumed that the column value is alpar@1: -- provided by the solver. */ alpar@1: alpar@1: void mpl_put_col_value(MPL *mpl, int j, double val) alpar@1: { if (mpl->phase != 3) alpar@1: xfault("mpl_put_col_value: invalid call sequence\n"); alpar@1: if (!(1 <= j && j <= mpl->n)) alpar@1: xfault( alpar@1: "mpl_put_col_value: j = %d; column number out of range\n", j); alpar@1: mpl->col[j]->prim = val; alpar@1: return; alpar@1: } alpar@1: #endif alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_postsolve - postsolve model. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- int mpl_postsolve(MPL *mpl); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_postsolve performs postsolving of the model using alpar@1: -- its description stored in the translator database. This phase means alpar@1: -- executing statements, which follow the solve statement. alpar@1: -- alpar@1: -- If this routine is used, it should be called once after the routine alpar@1: -- mpl_generate and if the latter returned the code 3. alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- The routine mpl_postsolve returns one of the following codes: alpar@1: -- alpar@1: -- 3 - model has been successfully postsolved. alpar@1: -- 4 - processing failed due to some errors. In this case the calling alpar@1: -- program should call the routine mpl_terminate to terminate model alpar@1: -- processing. */ alpar@1: alpar@1: int mpl_postsolve(MPL *mpl) alpar@1: { if (!(mpl->phase == 3 && !mpl->flag_p)) alpar@1: xfault("mpl_postsolve: invalid call sequence\n"); alpar@1: /* set up error handler */ alpar@1: if (setjmp(mpl->jump)) goto done; alpar@1: /* perform postsolving */ alpar@1: postsolve_model(mpl); alpar@1: flush_output(mpl); alpar@1: /* postsolving phase has been finished */ alpar@1: xprintf("Model has been successfully processed\n"); alpar@1: done: /* return to the calling program */ alpar@1: return mpl->phase; alpar@1: } alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- mpl_terminate - free all resources used by translator. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glpmpl.h" alpar@1: -- void mpl_terminate(MPL *mpl); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine mpl_terminate frees all the resources used by the GNU alpar@1: -- MathProg translator. */ alpar@1: alpar@1: void mpl_terminate(MPL *mpl) alpar@1: { if (setjmp(mpl->jump)) xassert(mpl != mpl); alpar@1: switch (mpl->phase) alpar@1: { case 0: alpar@1: case 1: alpar@1: case 2: alpar@1: case 3: alpar@1: /* there were no errors; clean the model content */ alpar@1: clean_model(mpl); alpar@1: xassert(mpl->a_list == NULL); alpar@1: #if 1 /* 11/II-2008 */ alpar@1: xassert(mpl->dca == NULL); alpar@1: #endif alpar@1: break; alpar@1: case 4: alpar@1: /* model processing has been finished due to error; delete alpar@1: search trees, which may be created for some arrays */ alpar@1: { ARRAY *a; alpar@1: for (a = mpl->a_list; a != NULL; a = a->next) alpar@1: if (a->tree != NULL) avl_delete_tree(a->tree); alpar@1: } alpar@1: #if 1 /* 11/II-2008 */ alpar@1: free_dca(mpl); alpar@1: #endif alpar@1: break; alpar@1: default: alpar@1: xassert(mpl != mpl); alpar@1: } alpar@1: /* delete the translator database */ alpar@1: xfree(mpl->image); alpar@1: xfree(mpl->b_image); alpar@1: xfree(mpl->f_image); alpar@1: xfree(mpl->context); alpar@1: dmp_delete_pool(mpl->pool); alpar@1: avl_delete_tree(mpl->tree); alpar@1: dmp_delete_pool(mpl->strings); alpar@1: dmp_delete_pool(mpl->symbols); alpar@1: dmp_delete_pool(mpl->tuples); alpar@1: dmp_delete_pool(mpl->arrays); alpar@1: dmp_delete_pool(mpl->members); alpar@1: dmp_delete_pool(mpl->elemvars); alpar@1: dmp_delete_pool(mpl->formulae); alpar@1: dmp_delete_pool(mpl->elemcons); alpar@1: xfree(mpl->sym_buf); alpar@1: xfree(mpl->tup_buf); alpar@1: rng_delete_rand(mpl->rand); alpar@1: if (mpl->row != NULL) xfree(mpl->row); alpar@1: if (mpl->col != NULL) xfree(mpl->col); alpar@1: if (mpl->in_fp != NULL) xfclose(mpl->in_fp); alpar@1: if (mpl->out_fp != NULL && mpl->out_fp != (void *)stdout) alpar@1: xfclose(mpl->out_fp); alpar@1: if (mpl->out_file != NULL) xfree(mpl->out_file); alpar@1: if (mpl->prt_fp != NULL) xfclose(mpl->prt_fp); alpar@1: if (mpl->prt_file != NULL) xfree(mpl->prt_file); alpar@1: if (mpl->mod_file != NULL) xfree(mpl->mod_file); alpar@1: xfree(mpl->mpl_buf); alpar@1: xfree(mpl); alpar@1: return; alpar@1: } alpar@1: alpar@1: /* eof */