-
Notifications
You must be signed in to change notification settings - Fork 4
/
optimal_greedy_error.py
66 lines (51 loc) · 1.75 KB
/
optimal_greedy_error.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
import random
import matplotlib.pyplot as plt
from core import greedy,optimal
def reproduce_greedy_with_error():
size = [1000.0,1000.0]
start = [e/2 for e in size]
n_diffs = []
repeats = 1000
n = 10
filename = "correct_res_batch_2_"+str(n)+"_"+str(repeats)+".txt"
with open(filename,"w") as f:
pass
error_variances = [e*.04 for e in range(6,11)]
for error_variance in error_variances:
diffs = []
for k in range(repeats):
print error_variance,str(k+1)+"/"+str(repeats)
coords = [[random.random()*e for e in size] for i in range(n)]
greedy_dist,greedy_order = greedy(start,coords,error_variance)
optimal_dist,optimal_order = optimal(start,coords,greedy_dist)
diff = (greedy_dist-optimal_dist)/optimal_dist*100.0
diffs.append(diff)
with open(filename,"a") as f:
f.write(str(error_variance)+":"+",".join([str(e) for e in diffs])+"\n")
n_diffs.append(diffs)
print [sum(diffs)/repeats for diffs in n_diffs]
def load_data_and_plot():
filename = "correct_res_10_1000.txt"
repeats = 1000
n_diffs = []
sigmas = [e*0.04 for e in range(11)]
with open(filename,"r") as pin:
for line in pin:
sigma,diff_s = line.split(":")
diffs = [float(e) for e in diff_s.split(",")]
diffs.sort()
n_diffs.append(diffs)
plt.figure(figsize=(5,4))
plt.boxplot(n_diffs)
plt.xlabel('$\sigma$')
labels = [str(e) if i%2==0 else '' for i,e in enumerate(sigmas)]
labels.insert(0,'')
plt.xticks(range(len(sigmas)+1), labels)
plt.ylabel('Difference - %')
plt.grid()
plt.xlim([.5,len(sigmas)+.5])
plt.ylim([-0.2,50.2])
plt.yticks([e*5 for e in range(0,11)],[''if e%2==1 else str(e*5) for e in range(0,11) ])
plt.tight_layout()
plt.savefig('sigma_diff.png')
load_data_and_plot()