alpar@1: /* glplpx03.c (OPB format) */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Author: Oscar Gustafsson . 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 "glpapi.h" alpar@1: #if 0 /* 24/XII-2009; by mao */ alpar@1: #include "glpipp.h" alpar@1: #endif alpar@1: alpar@1: /*---------------------------------------------------------------------- alpar@1: -- lpx_write_pb - write problem data in (normalized) OPB format. alpar@1: -- alpar@1: -- *Synopsis* alpar@1: -- alpar@1: -- #include "glplpx.h" alpar@1: -- int lpx_write_pb(LPX *lp, const char *fname, int normalized, alpar@1: -- int binarize); alpar@1: -- alpar@1: -- *Description* alpar@1: -- alpar@1: -- The routine lpx_write_pb writes problem data in OPB format alpar@1: -- to an output text file whose name is the character string fname. alpar@1: -- If normalized is non-zero the output will be generated in a alpar@1: -- normalized form with sequentially numbered variables, x1, x2 etc. alpar@1: -- If binarize, any integer variable will be repalzec by binary ones, alpar@1: -- see ipp_binarize alpar@1: -- alpar@1: -- *Returns* alpar@1: -- alpar@1: -- If the operation was successful, the routine returns zero. Otherwise alpar@1: -- the routine prints an error message and returns non-zero. */ alpar@1: alpar@1: #if 1 /* 24/XII-2009; by mao (disabled, because IPP was removed) */ alpar@1: int lpx_write_pb(LPX *lp, const char *fname, int normalized, alpar@1: int binarize) alpar@1: { xassert(lp == lp); alpar@1: xassert(fname == fname); alpar@1: xassert(normalized == normalized); alpar@1: xassert(binarize == binarize); alpar@1: xprintf("lpx_write_pb: sorry, currently this operation is not ava" alpar@1: "ilable\n"); alpar@1: return 1; alpar@1: } alpar@1: #else alpar@1: int lpx_write_pb(LPX *lp, const char *fname, int normalized, alpar@1: int binarize) alpar@1: { alpar@1: FILE* fp; alpar@1: int m,n,i,j,k,o,nonfree=0, obj_dir, dbl, *ndx, row_type, emptylhs=0; alpar@1: double coeff, *val, bound, constant/*=0.0*/; alpar@1: char* objconstname = "dummy_one"; alpar@1: char* emptylhsname = "dummy_zero"; alpar@1: alpar@1: /* Variables needed for possible binarization */ alpar@1: /*LPX* tlp;*/ alpar@1: IPP *ipp = NULL; alpar@1: /*tlp=lp;*/ alpar@1: alpar@1: if(binarize) /* Transform integer variables to binary ones */ alpar@1: { alpar@1: ipp = ipp_create_wksp(); alpar@1: ipp_load_orig(ipp, lp); alpar@1: ipp_binarize(ipp); alpar@1: lp = ipp_build_prob(ipp); alpar@1: } alpar@1: fp = fopen(fname, "w"); alpar@1: alpar@1: if(fp!= NULL) alpar@1: { alpar@1: xprintf( alpar@1: "lpx_write_pb: writing problem in %sOPB format to `%s'...\n", alpar@1: (normalized?"normalized ":""), fname); alpar@1: alpar@1: m = glp_get_num_rows(lp); alpar@1: n = glp_get_num_cols(lp); alpar@1: for(i=1;i<=m;i++) alpar@1: { alpar@1: switch(glp_get_row_type(lp,i)) alpar@1: { alpar@1: case GLP_LO: alpar@1: case GLP_UP: alpar@1: case GLP_FX: alpar@1: { alpar@1: nonfree += 1; alpar@1: break; alpar@1: } alpar@1: case GLP_DB: alpar@1: { alpar@1: nonfree += 2; alpar@1: break; alpar@1: } alpar@1: } alpar@1: } alpar@1: constant=glp_get_obj_coef(lp,0); alpar@1: fprintf(fp,"* #variables = %d #constraints = %d\n", alpar@1: n + (constant == 0?1:0), nonfree + (constant == 0?1:0)); alpar@1: /* Objective function */ alpar@1: obj_dir = glp_get_obj_dir(lp); alpar@1: fprintf(fp,"min: "); alpar@1: for(i=1;i<=n;i++) alpar@1: { alpar@1: coeff = glp_get_obj_coef(lp,i); alpar@1: if(coeff != 0.0) alpar@1: { alpar@1: if(obj_dir == GLP_MAX) alpar@1: coeff=-coeff; alpar@1: if(normalized) alpar@1: fprintf(fp, " %d x%d", (int)coeff, i); alpar@1: else alpar@1: fprintf(fp, " %d*%s", (int)coeff, alpar@1: glp_get_col_name(lp,i)); alpar@1: alpar@1: } alpar@1: } alpar@1: if(constant) alpar@1: { alpar@1: if(normalized) alpar@1: fprintf(fp, " %d x%d", (int)constant, n+1); alpar@1: else alpar@1: fprintf(fp, " %d*%s", (int)constant, objconstname); alpar@1: } alpar@1: fprintf(fp,";\n"); alpar@1: alpar@1: if(normalized && !binarize) /* Name substitution */ alpar@1: { alpar@1: fprintf(fp,"* Variable name substitution:\n"); alpar@1: for(j=1;j<=n;j++) alpar@1: { alpar@1: fprintf(fp, "* x%d = %s\n", j, glp_get_col_name(lp,j)); alpar@1: } alpar@1: if(constant) alpar@1: fprintf(fp, "* x%d = %s\n", n+1, objconstname); alpar@1: } alpar@1: alpar@1: ndx = xcalloc(1+n, sizeof(int)); alpar@1: val = xcalloc(1+n, sizeof(double)); alpar@1: alpar@1: /* Constraints */ alpar@1: for(j=1;j<=m;j++) alpar@1: { alpar@1: row_type=glp_get_row_type(lp,j); alpar@1: if(row_type!=GLP_FR) alpar@1: { alpar@1: if(row_type == GLP_DB) alpar@1: { alpar@1: dbl=2; alpar@1: row_type = GLP_UP; alpar@1: } alpar@1: else alpar@1: { alpar@1: dbl=1; alpar@1: } alpar@1: k=glp_get_mat_row(lp, j, ndx, val); alpar@1: for(o=1;o<=dbl;o++) alpar@1: { alpar@1: if(o==2) alpar@1: { alpar@1: row_type = GLP_LO; alpar@1: } alpar@1: if(k==0) /* Empty LHS */ alpar@1: { alpar@1: emptylhs = 1; alpar@1: if(normalized) alpar@1: { alpar@1: fprintf(fp, "0 x%d ", n+2); alpar@1: } alpar@1: else alpar@1: { alpar@1: fprintf(fp, "0*%s ", emptylhsname); alpar@1: } alpar@1: } alpar@1: alpar@1: for(i=1;i<=k;i++) alpar@1: { alpar@1: if(val[i] != 0.0) alpar@1: { alpar@1: alpar@1: if(normalized) alpar@1: { alpar@1: fprintf(fp, "%d x%d ", alpar@1: (row_type==GLP_UP)?(-(int)val[i]):((int)val[i]), ndx[i]); alpar@1: } alpar@1: else alpar@1: { alpar@1: fprintf(fp, "%d*%s ", (int)val[i], alpar@1: glp_get_col_name(lp,ndx[i])); alpar@1: } alpar@1: } alpar@1: } alpar@1: switch(row_type) alpar@1: { alpar@1: case GLP_LO: alpar@1: { alpar@1: fprintf(fp, ">="); alpar@1: bound = glp_get_row_lb(lp,j); alpar@1: break; alpar@1: } alpar@1: case GLP_UP: alpar@1: { alpar@1: if(normalized) alpar@1: { alpar@1: fprintf(fp, ">="); alpar@1: bound = -glp_get_row_ub(lp,j); alpar@1: } alpar@1: else alpar@1: { alpar@1: fprintf(fp, "<="); alpar@1: bound = glp_get_row_ub(lp,j); alpar@1: } alpar@1: alpar@1: break; alpar@1: } alpar@1: case GLP_FX: alpar@1: { alpar@1: fprintf(fp, "="); alpar@1: bound = glp_get_row_lb(lp,j); alpar@1: break; alpar@1: } alpar@1: } alpar@1: fprintf(fp," %d;\n",(int)bound); alpar@1: } alpar@1: } alpar@1: } alpar@1: xfree(ndx); alpar@1: xfree(val); alpar@1: alpar@1: if(constant) alpar@1: { alpar@1: xprintf( alpar@1: "lpx_write_pb: adding constant objective function variable\n"); alpar@1: alpar@1: if(normalized) alpar@1: fprintf(fp, "1 x%d = 1;\n", n+1); alpar@1: else alpar@1: fprintf(fp, "1*%s = 1;\n", objconstname); alpar@1: } alpar@1: if(emptylhs) alpar@1: { alpar@1: xprintf( alpar@1: "lpx_write_pb: adding dummy variable for empty left-hand si" alpar@1: "de constraint\n"); alpar@1: alpar@1: if(normalized) alpar@1: fprintf(fp, "1 x%d = 0;\n", n+2); alpar@1: else alpar@1: fprintf(fp, "1*%s = 0;\n", emptylhsname); alpar@1: } alpar@1: alpar@1: } alpar@1: else alpar@1: { alpar@1: xprintf("Problems opening file for writing: %s\n", fname); alpar@1: return(1); alpar@1: } alpar@1: fflush(fp); alpar@1: if (ferror(fp)) alpar@1: { xprintf("lpx_write_pb: can't write to `%s' - %s\n", fname, alpar@1: strerror(errno)); alpar@1: goto fail; alpar@1: } alpar@1: fclose(fp); alpar@1: alpar@1: alpar@1: if(binarize) alpar@1: { alpar@1: /* delete the resultant problem object */ alpar@1: if (lp != NULL) lpx_delete_prob(lp); alpar@1: /* delete MIP presolver workspace */ alpar@1: if (ipp != NULL) ipp_delete_wksp(ipp); alpar@1: /*lp=tlp;*/ alpar@1: } alpar@1: return 0; alpar@1: fail: if (fp != NULL) fclose(fp); alpar@1: return 1; alpar@1: } alpar@1: #endif alpar@1: alpar@1: /* eof */