| 1 | package tuffy.util; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | |
| 5 | public class PlotMan { |
| 6 | |
| 7 | public static class TCPair{ |
| 8 | public double time, cost; |
| 9 | public TCPair(double t, double c){ |
| 10 | time = t; |
| 11 | cost = c; |
| 12 | } |
| 13 | |
| 14 | public String toString(){ |
| 15 | return time + "\t" + cost; |
| 16 | } |
| 17 | |
| 18 | public static TCPair parse(String line){ |
| 19 | String[] parts = line.trim().split("\t"); |
| 20 | if(parts.length != 2) return null; |
| 21 | double t = Double.parseDouble(parts[0]); |
| 22 | double c = Double.parseDouble(parts[1]); |
| 23 | return new TCPair(t,c); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public ArrayList<TCPair> input(String fin){ |
| 28 | System.out.println("reading..."); |
| 29 | ArrayList<String> lines = FileMan.getLines(fin); |
| 30 | ArrayList<TCPair> out = new ArrayList<TCPair>(); |
| 31 | for(String line : lines){ |
| 32 | TCPair p = TCPair.parse(line); |
| 33 | if(p != null) out.add(p); |
| 34 | } |
| 35 | System.out.println("#points = " + out.size()); |
| 36 | return out; |
| 37 | } |
| 38 | |
| 39 | public void output(ArrayList<TCPair> list, String fout){ |
| 40 | System.out.println("writing..."); |
| 41 | StringBuilder sb = new StringBuilder(); |
| 42 | for(TCPair p : list){ |
| 43 | sb.append(p + "\n"); |
| 44 | } |
| 45 | FileMan.writeToFile(fout, sb.toString()); |
| 46 | } |
| 47 | |
| 48 | public ArrayList<TCPair> filter(ArrayList<TCPair> list){ |
| 49 | System.out.println("filtering..."); |
| 50 | double granu = 100; |
| 51 | if(list.size() < 20) return list; |
| 52 | ArrayList<TCPair> out = new ArrayList<TCPair>(); |
| 53 | TCPair first = list.get(0); |
| 54 | TCPair last = list.get(list.size()-1); |
| 55 | double mint = (last.time - first.time)/granu; |
| 56 | double minc = (first.cost - last.cost)/granu; |
| 57 | |
| 58 | out.add(first); |
| 59 | TCPair prev = first; |
| 60 | for(int i=1; i<list.size()-1; i++){ |
| 61 | TCPair cur = list.get(i); |
| 62 | if(cur.time - prev.time >= mint || |
| 63 | prev.cost - cur.cost >= minc){ |
| 64 | out.add(cur); |
| 65 | prev = cur; |
| 66 | } |
| 67 | } |
| 68 | out.add(last); |
| 69 | System.out.println("#points = " + out.size()); |
| 70 | |
| 71 | return out; |
| 72 | } |
| 73 | |
| 74 | public void spit(int n, String fout){ |
| 75 | StringBuilder sb = new StringBuilder(); |
| 76 | for(int i=1; i<=n; i++){ |
| 77 | sb.append("pub(P" + i + ")\n"); |
| 78 | } |
| 79 | FileMan.writeToFile(fout, sb.toString()); |
| 80 | } |
| 81 | |
| 82 | public static void main(String[] args) { |
| 83 | String loc = "/sandbox/exp/"; |
| 84 | String fin = loc + "trace.txt"; |
| 85 | String fout = loc + "points.txt"; |
| 86 | PlotMan man = new PlotMan(); |
| 87 | man.spit(1000, "/sandbox/bench/wam/evidence.db"); |
| 88 | man.output(man.filter(man.input(fin)), fout); |
| 89 | } |
| 90 | |
| 91 | } |