/************************************************************************\ |* 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.*; public class MPS> { private String name; private Map rows = new HashMap(); private Map, T> cols = new HashMap, T>(); private Map rhss = new HashMap(); private Map equs = new HashMap(), vars = new HashMap(), fres = new HashMap(); public String getName() { return name; } public MPS(String name) { this.name = name; equs.put("COST", 0); } public String[] asciifyrows() { List result = new ArrayList(rows.size()); for (Map.Entry entry : rows.entrySet()) { String key = entry.getKey(); String val = entry.getValue(); result.add(" " + val + " " + key); } String[] res = result.toArray(new String[result.size()]); Arrays.sort(res); return res; } public String[] asciifycols() { List result = new ArrayList(cols.size()); for (Map.Entry, T> entry : cols.entrySet()) { Pair key = entry.getKey(); String val = asciify(entry.getValue().doubleValue()); result.add(" " + key.first + blanks(10 - key.first.length()) + key.second + blanks(22 - key.second.length() - val.length()) + val); } String[] res = result.toArray(new String[result.size()]); Arrays.sort(res); return res; } public String[] asciifyrhss() { List result = new ArrayList(rhss.size()); for (Map.Entry entry : rhss.entrySet()) { String key = entry.getKey(); String val = asciify(entry.getValue().doubleValue()); result.add(" RHS1 " + key + blanks(22 - key.length() - val.length()) + val); } String[] res = result.toArray(new String[result.size()]); Arrays.sort(res); return res; } public String[] asciifyfres() { List result = new ArrayList(fres.size()); for (String entry : fres.keySet()) { result.add(" FR BND " + entry); } String[] res = result.toArray(new String[result.size()]); Arrays.sort(res); return res; } public String asciify(Double val) { String result = val.toString(); if (result.length() <= 12) return result; int index = result.indexOf("E"); if (index == -1) { return result.substring(0, 12); } else { String exppart = result.substring(index, result.length()); return result.substring(0, 12 - exppart.length()) + exppart; } } public void printMPS(PrintStream out) { String[] cols = asciifycols(); String[] rows = asciifyrows(); String[] rhss = asciifyrhss(); String[] fres = asciifyfres(); out.println("NAME " + name); out.println("ROWS"); for (int i = 0 ; i < rows.length ; i++) out.println(rows[i]); out.println("COLUMNS"); for (int i = 0 ; i < cols.length ; i++) out.println(cols[i]); out.println("RHS"); for (int i = 0 ; i < rhss.length ; i++) out.println(rhss[i]); out.println("BOUNDS"); for (int i = 0 ; i < fres.length ; i++) out.println(fres[i]); out.println("ENDATA"); } public int tableauHeight() { return rows.size(); } public int tableauWidth() { return vars.size() + fres.size() + 1; } public T[][] toTableau(T[][] result) { if (result.length != tableauHeight() || result[0].length != tableauWidth()) { throw new RuntimeException("MPS: toTableau: tableau doesn't fit."); } T ZERO = cols.entrySet().iterator().next().getValue().zero(); for (int i = 0 ; i < result.length ; i++) { Arrays.fill(result[i], ZERO); } for (Map.Entry entry : rhss.entrySet()) {} for (Map.Entry, T> entry : cols.entrySet()) {} return result; } public void declareEquation(String eqtype, String eqname) { if (eqtype.length() > 10) throw new RuntimeException("MPS: Too long an equation name: " + eqname); if (eqtype.length() != 1) throw new RuntimeException("MPS: Malformed equation type: " + eqtype); rows.put(eqname, eqtype); } public void declareVariable(String varname) { if (varname.length() > 10) throw new RuntimeException("MPS: Too long a variable name: " + varname); if (!vars.containsKey(varname)) vars.put(varname, vars.size() + 1); } public void declareFreeVariable(String varname) { declareVariable(varname); if (!fres.containsKey(varname)) fres.put(varname, fres.size() + 1); } public void setCoef(String varname, String eqname, T val) { declareVariable(varname); if (eqname.length() > 10) throw new RuntimeException("MPS: Too long an equation name: " + eqname); cols.put(new Pair(varname, eqname), val); } public void addCoef(String varname, String eqname, T val) { declareVariable(varname); if (eqname.length() > 10) throw new RuntimeException("MPS: Too long an equation name: " + eqname); Pair key = new Pair(varname, eqname); T value = cols.get(key); if (value != null) { T temp = val.add(value); cols.put(key, temp); } else cols.put(key, val); } public void setRHS(String eqname, T val) { rhss.put(eqname, val); } private static String blanks(int k) { char[] result = new char[k]; Arrays.fill(result, ' '); return new String(result); } }