001    package net.sf.cpsolver.coursett;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileReader;
006    import java.io.FileWriter;
007    import java.io.IOException;
008    import java.io.PrintWriter;
009    import java.util.Enumeration;
010    import java.util.Hashtable;
011    import java.util.Iterator;
012    import java.util.Map;
013    import java.util.TreeSet;
014    
015    import net.sf.cpsolver.ifs.util.ToolBox;
016    
017    /**
018     * Process all solutions (files output.csv) in all subfolders of the given folder 
019     * and create a CSV (comma separated values text file) combining all minimal perturbation 
020     * information of the found solutions.
021     * 
022     * @version
023     * CourseTT 1.1 (University Course Timetabling)<br>
024     * Copyright (C) 2007 Tomáš Müller<br>
025     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
026     * Lazenska 391, 76314 Zlin, Czech Republic<br>
027     * <br>
028     * This library is free software; you can redistribute it and/or
029     * modify it under the terms of the GNU Lesser General Public
030     * License as published by the Free Software Foundation; either
031     * version 2.1 of the License, or (at your option) any later version.
032     * <br><br>
033     * This library is distributed in the hope that it will be useful,
034     * but WITHOUT ANY WARRANTY; without even the implied warranty of
035     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
036     * Lesser General Public License for more details.
037     * <br><br>
038     * You should have received a copy of the GNU Lesser General Public
039     * License along with this library; if not, write to the Free Software
040     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
041     */
042    public class GetMppInfo {
043        public static Hashtable getInfo(File outputFile) {
044            try {
045                BufferedReader reader = new BufferedReader(new FileReader(outputFile));
046                String line = null;
047                Hashtable info = new Hashtable();
048                while ((line=reader.readLine())!=null) {
049                    int idx = line.indexOf(','); 
050                    if (idx>=0) {
051                        String key = line.substring(0, idx).trim();
052                        String value = line.substring(idx+1).trim();
053                        if (value.indexOf('(')>=0 && value.indexOf(')')>=0) {
054                            value = value.substring(value.indexOf('(')+1, value.indexOf(')'));
055                            if (value.indexOf('/')>=0) {
056                                String bound = value.substring(value.indexOf('/')+1);
057                                if (bound.indexOf("..")>=0) {
058                                    String min = bound.substring(0, bound.indexOf(".."));
059                                    String max = bound.substring(bound.indexOf("..")+2);
060                                    info.put(key+" Min",min);
061                                    info.put(key+" Max",max);
062                                } else {
063                                    info.put(key+" Bound",bound);
064                                }
065                                value = value.substring(0, value.indexOf('/'));
066                            }
067                        }
068                        if (value.length()>0) info.put(key, value);
069                    }
070                }
071                reader.close();
072                return info;
073            } catch (Exception e) {
074                System.err.println("Error reading info, message: "+e.getMessage());
075                e.printStackTrace();
076                return null;
077            }
078        }
079    
080        public static void getInfos(File file, Hashtable infos, String instance) {
081            Hashtable info = getInfo(file);
082            if (info==null || info.isEmpty() || !info.containsKey("000.053 Given perturbations")) return;
083            Integer pert = Integer.valueOf((String)info.get("000.053 Given perturbations"));
084            for (Iterator i=info.entrySet().iterator();i.hasNext();) {
085                Map.Entry entry = (Map.Entry)i.next();
086                String key = (String)entry.getKey();
087                String value = (String)entry.getValue();
088                if (!key.startsWith("000.") || key.equals("000.053 Given perturbations")) continue;
089                Hashtable keyTable = (Hashtable)infos.get(key);
090                if (keyTable==null) {
091                    keyTable = new Hashtable();
092                    infos.put(key, keyTable);
093                }
094                Hashtable instanceTable = (Hashtable)keyTable.get(instance);
095                if (instanceTable==null) {
096                    instanceTable = new Hashtable();
097                    keyTable.put(instance, instanceTable);
098                }
099                double[] pertTable = (double[])instanceTable.get(pert);
100                if (pertTable==null) {
101                    pertTable = new double[]{0,0};
102                    instanceTable.put(pert, pertTable);
103                }
104                pertTable[0] += Double.parseDouble(value);
105                pertTable[1] += 1;
106            }
107        }
108        
109        public static void writeInfos(Hashtable infos,File file) throws IOException {
110            PrintWriter out = new PrintWriter(new FileWriter(file));
111            for (Enumeration e=ToolBox.sortEnumeration(infos.keys());e.hasMoreElements();) {
112                String key = (String)e.nextElement();
113                out.println(key);
114                Hashtable keyTable = (Hashtable)infos.get(key);
115                TreeSet perts = new TreeSet();
116                for (Enumeration f=ToolBox.sortEnumeration(keyTable.keys());f.hasMoreElements();) {
117                    String instance = (String)f.nextElement();
118                    Hashtable instanceTable = (Hashtable)keyTable.get(instance);
119                    perts.addAll(instanceTable.keySet());
120                }
121                out.print(",,");
122                for (Iterator i=perts.iterator();i.hasNext();) {
123                    Integer pert = (Integer)i.next();
124                    out.print(pert);
125                    if (i.hasNext()) out.print(",");
126                }
127                out.println();
128                for (Enumeration f=ToolBox.sortEnumeration(keyTable.keys());f.hasMoreElements();) {
129                    String instance = (String)f.nextElement();
130                    Hashtable instanceTable = (Hashtable)keyTable.get(instance);
131                    perts.addAll(instanceTable.keySet());
132                    out.print(","+instance+",");
133                    for (Iterator i=perts.iterator();i.hasNext();) {
134                        Integer pert = (Integer)i.next();
135                        double[] pertTable = (double[])instanceTable.get(pert);
136                        if (pertTable!=null)
137                            out.print(pertTable[0]/pertTable[1]);
138                        if (i.hasNext()) out.print(",");
139                    }
140                    out.println();
141                }
142            }
143            out.flush(); out.close();
144        }
145        
146        public static void main(String args[]) {
147            try {
148                File folder = new File(".");
149                if (args.length>=1) folder = new File(args[0]);
150                String config = "mpp";
151                if (args.length>=2) config = args[1];
152                File[] instanceFolders = folder.listFiles();
153                Hashtable infos = new Hashtable();
154                for (int i=0;i<instanceFolders.length;i++) {
155                    File instanceFolder = instanceFolders[i];
156                    if (!instanceFolder.exists() || !instanceFolder.isDirectory() || !instanceFolder.getName().startsWith(config+"-")) continue;
157                    System.out.println("Checking "+instanceFolder.getName()+" ...");
158                    File[] files = instanceFolder.listFiles();
159                    for (int j=0;j<files.length;j++)
160                        if (files[j].isDirectory()) {
161                            File outputFile = new File(files[j], "output.csv");
162                            if (outputFile.exists()) {
163                                System.out.println("  Checking "+files[j].getName()+" ...");
164                                getInfos(outputFile, infos, instanceFolder.getName().substring(config.length()+1));
165                            }
166                        }
167                }
168                if (!infos.isEmpty())
169                    writeInfos(infos,new File(folder,"info.csv"));
170            } catch (Exception e) {
171                e.printStackTrace();
172            }
173        }
174    }