/************************************************************************\ |* gtf is a framework for analyzing two-player zero-sum games *| |* Copyright (C) 2005 Troels Bjerre Sorensen *| |* *| |* This program is free software; you can redistribute it and/or modify *| |* it under the terms of the GNU General Public License as published by *| |* the Free Software Foundation; either version 2 of the License, or *| |* (at your option) any later version. *| |* *| |* This program is distributed in the hope that it will be useful, but *| |* WITHOUT ANY WARRANTY; without even the implied warranty of *| |* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *| |* General Public License for more details. *| |* *| |* You should have received a copy of the GNU General Public License *| |* along with this program; if not, write to the *| |* Free Software Foundation, Inc., 59 Temple Place - Suite 330, *| |* Boston, MA 02111-1307, USA. *| \************************************************************************/ package gtf.util; import java.io.*; import java.util.*; class MPSPrettyPrinter { public static void main(String[] args) { try { Map equations = new HashMap(); Map rhss = new HashMap(); if (args.length != 1) { System.err.println("usage: java MPSPrettyPrinter "); System.exit(1); } BufferedReader bf = new BufferedReader(new FileReader(args[0])); System.out.println(bf.readLine()); if ( ! (new StringTokenizer(bf.readLine(), " ", false).nextToken().equals("ROWS"))) { System.err.println("Malformed mps..."); System.exit(1); } StringTokenizer st = new StringTokenizer(bf.readLine()); String type = st.nextToken(); while ( ! type.equals("COLUMNS")) { if (type.equals("N")) { equations.put(st.nextToken(), ""); } else if (type.equals("E")) { equations.put(st.nextToken(), " = "); } else if (type.equals("L")) { equations.put(st.nextToken(), " < "); } else if (type.equals("G")) { equations.put(st.nextToken(), " > "); } else { System.err.println("Unknown type: "+type); System.exit(1); } st = new StringTokenizer(bf.readLine()); type = st.nextToken(); } st = new StringTokenizer(bf.readLine()); String var = st.nextToken(); while ( ! var.equals("RHS")) { String equ = st.nextToken(); String val = st.nextToken(); if (val.equals("1.0")) val = " + "; else if (val.equals("-1.0")) val = " - "; else { double fval = Double.parseDouble(val); if (fval < 0) { val = " - " + (-fval) + " * "; } else { val = " + " + fval + " * "; } } equations.put(equ, val + var + equations.get(equ)); st = new StringTokenizer(bf.readLine()); var = st.nextToken(); } st = new StringTokenizer(bf.readLine()); String temp = st.nextToken(); while ( ! temp.equals("BOUNDS")) { String eq = st.nextToken(); String val = st.nextToken(); rhss.put(eq, Double.parseDouble(val)); st = new StringTokenizer(bf.readLine()); temp = st.nextToken(); } String[] array = equations.keySet().toArray(new String[0]); Arrays.sort(array , new StringLengthComparator()); for (int i = 0 ; i < array.length ; i++) { if (rhss.containsKey(array[i])) { System.out.println(array[i] + ":\t" + equations.get(array[i]) + rhss.get(array[i])); } else if (array[i].equals("COST")) { System.out.println(array[i] + ":\t" + equations.get(array[i])); } else { System.out.println(array[i] + ":\t" + equations.get(array[i]) + "0.0"); } } } catch (Exception e) { System.err.println("Caught exception: " + e); e.printStackTrace(System.err); } } }