-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblocksworld2-performance.py
executable file
·208 lines (173 loc) · 5.69 KB
/
blocksworld2-performance.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import hashlib, os, re
import pylab
from matplotlib import rc
from pylab import arange,pi,sin,cos,sqrt
if os.name is 'posix':
golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_width = 3 # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
params = {'backend': 'ps',
'axes.labelsize': 8,
'font.size': 8,
'legend.fontsize': 6,
'xtick.labelsize': 6,
'ytick.labelsize': 6,
'text.usetex': True,
'ps.usedistiller': 'xpdf',
'figure.figsize': fig_size}
pylab.rcParams.update(params)
rc('font',**{'family':'serif','serif':['Times']})
pylab.rcParams['path.simplify'] = True
import sys, getopt, random, time, datetime
import numpy as np
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from optparse import OptionParser
from matplotlib.ticker import ScalarFormatter
#from matplotlib2tikz import save
print 'matplotlib.__version__ is ' + matplotlib.__version__
#global_fix = 0;
class CommaFormatter(ScalarFormatter):
def pprint_val(self, x):
px = ScalarFormatter.pprint_val(self, x)
if os.name is 'posix':
px = px[1:len(px)-1]
px = self.add_commas(px)
if os.name is 'posix' and len(px) is not 0:
px = "$" + px + "$"
return px
def add_commas(self, arg):
#global global_fix;
#if arg == "0":
#global_fix = (global_fix + 1) % 4;
#if global_fix == 3:
#return "0.01"
#elif global_fix == 0:
#return "0.1"
s = arg.split('.')
#if len(s) is 2 and s[1][0] is not '0':
#return ""
if s[0][0] not in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}:
c = '-' + self.recurse(s[0][1:])
else:
c = self.recurse(s[0])
if len(s) > 1 and s[1] is not '0':
return c + '.' + s[1]
else:
return c
def recurse(self, arg):
if len(arg) < 4:
return arg
s = len(arg) - 3
return self.recurse(arg[:s]) + ',' + arg[s:]
def write_to_csv(filename, x_label, xs, y_labels, yss):
f = open(filename, 'w')
f.write(x_label.translate(None, ',\\'))
for y_label in y_labels:
f.write(',' + y_label.translate(None, ',\\'))
f.write('\n')
for i in range(len(xs)):
f.write(str(xs[i]))
for ys in yss:
f.write(',' + str(ys[i]))
f.write('\n')
def main():
parser = argparse.ArgumentParser("./blocksworld2.py")
parser.add_argument("--scenario", help="Which graph should be generated?")
args, filenames = parser.parse_known_args()
scenario = 0
if args.scenario:
scenario = int(args.scenario)
# 1: ./blocksworld2-performance.py --scenario 1 blocksworld2-performance-episode.txt && for type in csv eps png svg; do mv blocksworld2-performance.$type blocksworld2-performance-episode.$type; done
# 2: ./blocksworld2-performance.py --scenario 2 blocksworld2-performance-step.txt && for type in csv eps png svg; do mv blocksworld2-performance.$type blocksworld2-performance-step.$type; done
reward_label = 'Time (Seconds)'
file_handles = []
smith = {}
remap_names = {}
x = []
x_max = -1
for filename in filenames:
file_handles.append(open(filename, 'r'))
for f in file_handles:
done = False
while not done:
line = f.readline()
if not line or line == '':
done = True
break
split = line.split(' ')
group = split[0]
i = 1
while True:
success = False
try:
x_val = int(split[i])
y_val = float(split[i + 3])
success = True
except ValueError:
group += ' ' + split[i]
i = i + 1
if success:
remap_names[group] = group
break
if x_val > x_max:
x.append(x_val)
x_max = x_val
try:
smith[group].append(y_val)
except KeyError:
smith[group] = []
smith[group].append(y_val)
for handle in file_handles:
handle.close()
agent_list = []
title='Blocks World Performance'
fig = plt.figure()
fig.canvas.set_window_title('Blocks World')
rect = [0.17,0.17,0.80,0.80]
pylab.axes(rect)
labels = []
#if val0 == 4:
#for a in smith:
#for i in range(1, len(smith[a])):
#smith[a][i] = 0.95 * smith[a][i - 1] + 0.05 * smith[a][i];
y_labels = []
yss = []
for agent in smith:
y_labels.append(agent)
yss.append(smith[agent])
labels += pylab.semilogy(x, smith[agent], label=agent, linestyle='solid')
remap_names = {}
#remap_names['Exact Disabled Node Sharing & Flushing WMEs'] = 'Exact Disabled Node Sharing \\& Flushing WMEs'
pylab.grid(False)
pylab.xlabel('Number of Blocks', fontsize=8)
pylab.ylabel(reward_label, fontsize=8)
if scenario == 1:
pylab.ylim(ymax=1000)
elif scenario == 2:
pylab.ylim(ymax=10)
fig.axes[0].xaxis.set_major_formatter(CommaFormatter())
fig.axes[0].yaxis.set_major_formatter(CommaFormatter())
xlabels = fig.axes[0].xaxis.get_ticklabels()
last_xlabel = xlabels[len(xlabels) - 1]
last_xlabel.set_horizontalalignment('right')
last_xlabel.set_x(0)
#fig.axes[0].yaxis.set_scale('log')
#print last_xlabel.get_size()
#print last_xlabel.get_position()
#print last_xlabel.get_text()
#print last_xlabel
# lower right
pylab.legend(labels, [l.get_label() for l in labels], loc=2, handlelength=4.2, numpoints=2)
write_to_csv('blocksworld2-performance.csv', 'Number of Blocks', x, y_labels, yss)
pylab.savefig('blocksworld2-performance.eps')
pylab.savefig('blocksworld2-performance.png', dpi=1200)
pylab.savefig('blocksworld2-performance.svg')
#plt.show()
if __name__ == "__main__":
main()