|
| 1 | +from sys import stdin |
| 2 | +import math |
| 3 | +import numpy as np |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +n_constraints = 0 |
| 7 | +fixed_charge = False |
| 8 | + |
| 9 | +def transform_scale(x): |
| 10 | + return -np.log2(1.0 - x) |
| 11 | + |
| 12 | +def mkboxes(X, Y): |
| 13 | + pos = list(set([i for i in X])) |
| 14 | + pos.sort() |
| 15 | + idx = {} |
| 16 | + for i, x in enumerate(pos): |
| 17 | + idx[x] = i |
| 18 | + boxes = [ [] for i in range(len(pos))] |
| 19 | + for x,y in zip(X,Y): |
| 20 | + boxes[idx[x]].append(y) |
| 21 | + return boxes, pos |
| 22 | + |
| 23 | +def plot_scatter(X, Y): |
| 24 | + quantiles = [0.5, 0.75, 0.95, 0.99] |
| 25 | + |
| 26 | + X_transf = transform_scale(X) |
| 27 | + boxes, positions = mkboxes(X_transf, Y) |
| 28 | + X_ticks = [i for i in range(2+int(max(X_transf)))] |
| 29 | + X_labels = ["%.2f" % (1. - 1./2**i) for i in X_ticks] |
| 30 | + |
| 31 | + fig, ax = plt.subplots() |
| 32 | + ax.set_ylim(0.999, 1.6) |
| 33 | + # ax.boxplot(boxes, positions=positions) |
| 34 | + ax.violinplot(boxes, positions=positions) |
| 35 | + ax.set_xticks(X_ticks) |
| 36 | + ax.set_xticklabels(X_labels) |
| 37 | + ax.set_xlabel("difficulty") |
| 38 | + ax.set_ylabel("approximate/best solution ratio") |
| 39 | + ax.set_title("%d-constraints solution accuracy (%s activation costs)" % (n_constraints-1, |
| 40 | + "with" if fixed_charge else "without")) |
| 41 | + |
| 42 | + qvalues = np.array([ np.quantile(b, quantiles) for b in boxes ]) |
| 43 | + qvalues = qvalues.transpose() |
| 44 | + |
| 45 | + for i in range(len(quantiles)): |
| 46 | + ax.plot(positions, qvalues[i], label = "%.2f%%" % (100*quantiles[i])) |
| 47 | + |
| 48 | + plt.legend() |
| 49 | + plt.show() |
| 50 | + |
| 51 | + |
| 52 | +def plot_successrate(X, Y): |
| 53 | + X_transf = transform_scale(X) |
| 54 | + boxes, positions = mkboxes(X_transf, Y) |
| 55 | + |
| 56 | + positions = [float(p) for p in positions ] |
| 57 | + Y_rate = [ float(sum(b)/len(b)) for b in boxes ] |
| 58 | + |
| 59 | + X_ticks = [i for i in range(2+int(max(X_transf)))] |
| 60 | + X_labels = ["%.2f" % (1. - 1./2**i) for i in X_ticks] |
| 61 | + |
| 62 | + fig, ax = plt.subplots() |
| 63 | + ax.plot(positions, Y_rate, ls='', marker='o') |
| 64 | + |
| 65 | + ax.set_xticks(X_ticks) |
| 66 | + ax.set_xticklabels(X_labels) |
| 67 | + ax.set_xlabel("difficulty") |
| 68 | + ax.set_ylabel("success rate") |
| 69 | + ax.set_title("%d-constraints success rate (%s activation costs)" % (n_constraints-1, |
| 70 | + "with" if fixed_charge else "without")) |
| 71 | + plt.show() |
| 72 | + |
| 73 | +# all results |
| 74 | +DIFF = [] |
| 75 | +SUCCESS = [] |
| 76 | + |
| 77 | +# only those results where constraints are satisfied |
| 78 | +DIFF_filtered = [] |
| 79 | +COST_relative = [] |
| 80 | + |
| 81 | +for line in stdin: |
| 82 | + elements = line.split() |
| 83 | + pid = elements[0] |
| 84 | + d = float(elements[1]) |
| 85 | + exact, nc, mcf, nc_mcf, approx, nc_approx = map(int, elements[2:]) |
| 86 | + |
| 87 | + n_constraints = nc |
| 88 | + |
| 89 | + DIFF.append(d) |
| 90 | + # constraints satisfied |
| 91 | + assert nc_approx<=nc |
| 92 | + SUCCESS.append(int(nc_approx==nc)) |
| 93 | + |
| 94 | + if nc_approx==nc: |
| 95 | + DIFF_filtered.append(d) |
| 96 | + # the approximate is never better than the exact |
| 97 | + assert exact<=approx |
| 98 | + COST_relative.append(approx/exact) |
| 99 | + |
| 100 | + if COST_relative[-1]>1.5: |
| 101 | + print(line) |
| 102 | + |
| 103 | +plot_scatter(np.array(DIFF_filtered), np.array(COST_relative)) |
| 104 | +plot_successrate(np.array(DIFF), np.array(SUCCESS)) |
0 commit comments