-
Notifications
You must be signed in to change notification settings - Fork 4
/
optimal_greedy.py
46 lines (33 loc) · 1 KB
/
optimal_greedy.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
import random
import matplotlib.pyplot as plt
from core import greedy,optimal
def optimal_greedy():
size = [10.0,10.0]
start = [e/2 for e in size]
n_diffs = []
repeats = 1000
ns = range(4,12)
for n in ns:
diffs = []
for k in range(repeats):
print n,k
coords = [[random.random()*e for e in size] for i in range(n)]
greedy_dist,greedy_order = greedy(start,coords)
optimal_dist,optimal_order = optimal(start,coords,greedy_dist)
diff = (greedy_dist-optimal_dist)/optimal_dist*100.0
diffs.append(diff)
n_diffs.append(diffs)
plt.figure(figsize=(5,4))
plt.boxplot(n_diffs)
plt.xlabel('#Collectibles')
labels = [str(e) for e in ns]
labels.insert(0,'')
plt.xticks(range(len(ns)+1), labels)
plt.ylabel('Difference - %')
plt.grid()
plt.xlim([.5,len(ns)+.5])
plt.ylim([-0.2,30.2])
plt.yticks([e*2 for e in range(0,16)],[''if e%3>0 else str(e*2) for e in range(0,16) ])
plt.tight_layout()
plt.savefig('A1_difference.png')
optimal_greedy()