-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexus.py
executable file
·143 lines (128 loc) · 4.92 KB
/
nexus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import algorithms
from main import RunnerForPool
import argparse
import itertools
import json
import matplotlib.pyplot as plt
import numpy as np
import os.path
import random
import sys
import time
import matplotlib.colors as mcolors
from multiprocessing import Pool
def EvalHelmbold(datasets, num_runs, output_basename):
ALGORITHMS_ONLINE = (
algorithms.OPT_multiple,
algorithms.RandomOnline_multiple_prudent,
)
ALGORITHMS_PRED= (
algorithms.FTP_multiple,
algorithms.RobustRhoMu_multiple_prudent,
algorithms.RobustKumar_multiple_prudent,
algorithms.RobustAngelo_multiple,
algorithms.RobustFTP_multiple_prudent,
)
ALGORITHMS = ALGORITHMS_ONLINE + ALGORITHMS_PRED
json_fname = output_basename + '.json'
try:
with open(json_fname) as f:
competitive_ratios = np.array(json.load(f))
datasets = datasets + datasets
except:
datasets_requests = []
for dataset in datasets:
with open(dataset) as f:
requests = [float(x) for x in f]
datasets_requests.append(requests)
datasets_predictions = [
algorithms.PredHelmbold(requests) for requests in datasets_requests
] + [
algorithms.PredHelmbold(requests, ETA=-4.0) for requests in datasets_requests
]
datasets = datasets + datasets
datasets_requests = datasets_requests + datasets_requests
costs = np.zeros((len(datasets), len(ALGORITHMS), num_runs))
for i, requests in enumerate(datasets_requests):
for j, algorithm in enumerate(ALGORITHMS_ONLINE):
output = algorithm(requests)
cost = algorithms.Cost(output, requests, algorithm.__name__)
for run in range(num_runs): # these algorithms behave exactly the same across all runs
costs[i][j][run] = cost
with Pool() as pool:
grid = list(itertools.product(range(len(datasets)), range(len(ALGORITHMS_PRED)), range(num_runs)))
args = [(
ALGORITHMS_PRED[algorithm_idx],
datasets_requests[dataset_idx],
datasets_predictions[dataset_idx]) for dataset_idx, algorithm_idx, _ in grid]
runs = pool.starmap(RunnerForPool, args)
for (dataset_idx, algorithm_idx, run_idx), cost in zip(grid, runs):
costs[dataset_idx][len(ALGORITHMS_ONLINE) + algorithm_idx][run_idx] = cost
costs = np.mean(costs, axis=2)
competitive_ratios = costs / costs[:,0].reshape(-1, 1)
with open(json_fname, 'w') as f:
json.dump(competitive_ratios.tolist(), f)
LABELS = [algorithm.__name__ for algorithm in ALGORITHMS]
for i, dataset in enumerate(datasets):
for j, label in enumerate(LABELS):
print(dataset, label, competitive_ratios[i][j])
TRANSLATE = {
"RandomOnline_multiple_prudent" : "$(\\frac{e}{e-1})$-competitive",
"FTP_multiple": "Lemma 3 + FTP",
"RobustFTP_multiple_prudent": "Lemma 3 + Thm 28 + FTP",
"RobustKumar_multiple_prudent": "Lemma 3 + Thm 28 + PSK",
"RobustAngelo_multiple": "Lemma 3 + Thm 28 + ADJKR",
"RobustRhoMu_multiple_prudent": "Our algorithm",
}
GENERAL_FONTSIZE, LABEL_FONTSIZE, LEGEND_FONTSIZE, DATASET_FONTSIZE = 11, 16, 11, 9
plt.rcParams.update({
'pgf.texsystem': 'pdflatex',
'font.family': 'serif',
'font.size': GENERAL_FONTSIZE,
'pgf.rcfonts': False,
})
barwidth = 0.75 / (len(ALGORITHMS) - 1)
for j, algorithm in enumerate(ALGORITHMS[1:]):
plt.bar(
np.arange(len(datasets)) + j * barwidth,
competitive_ratios[:,j + 1],
width=0.8 * barwidth,
label=TRANSLATE[LABELS[j + 1]],
)
plt.xticks(
np.arange(len(datasets)) + 0.75,
[
os.path.splitext(os.path.basename(dataset))[0].replace('nexus_log', '')
for dataset in datasets
],
rotation=30, ha='right', fontsize=DATASET_FONTSIZE,
)
plt.gca().tick_params('x', length=0)
plt.legend(loc='best', fontsize=LEGEND_FONTSIZE)
plt.ylim(1, 3)
plt.ylabel('Competitive ratio', fontsize=LABEL_FONTSIZE)
n = len(datasets)
import matplotlib.ticker as ticker
ax = plt.gca().twiny()
ax.spines['bottom'].set_position(('axes', -0.50))
ax.tick_params('both', length=0, width=0, which='minor', labelsize=LABEL_FONTSIZE)
ax.tick_params('both', direction='in', which='major')
ax.xaxis.set_ticks_position('bottom')
ax.xaxis.set_label_position('bottom')
ax.set_xticks([0, 0.5 * n, n])
ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.xaxis.set_minor_locator(ticker.FixedLocator([0.25 * n, 0.75 * n]))
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(['Good predictions', 'Bad predictions']))
plt.gcf().set_size_inches(w=12, h=4.5)
plt.tight_layout()
plt.savefig(output_basename + '.pdf')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--num_runs', type=int, default=1, help='number of runs')
parser.add_argument('-o', '--output_basename', type=str)
parser.add_argument('DATASETS', type=str, nargs='+')
args = parser.parse_args()
EvalHelmbold(args.DATASETS, args.num_runs, args.output_basename)
if __name__ == '__main__':
main()