-
Notifications
You must be signed in to change notification settings - Fork 0
/
hillcar_refine_iterations.py
124 lines (107 loc) · 3.7 KB
/
hillcar_refine_iterations.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
import subprocess
import sys
import numpy as np
import matplotlib.pyplot as plt
import tri_mesh_viewer as tmv
from utils.archiver import Unarchiver,read_shewchuk
from kojima_solver import solve_lcp_file
GEN_COMMAND = './cdiscrete/hillcar_gen'
REFINE_COMMAND = './cdiscrete/hillcar_refine'
BASE_FILENAME = 'hillcar'
def run_gen_initial(base_file):
filename = base_file + '.0'
cmd = GEN_COMMAND + ' --outfile_base ' + filename
print '#'*(len(cmd)+8)
print 'RUNNING:', cmd
print '-'*(len(cmd)+8)
return subprocess.check_call([cmd],
shell=True,
stdout=sys.stdout,
stderr=sys.stderr)
def run_refine(base_file,i):
in_name = base_file + '.' + str(i)
out_name = base_file + '.' + str(i+1)
cmd = REFINE_COMMAND + ' --infile_base ' + in_name \
+ ' --outfile_base ' + out_name
print '#'*(len(cmd)+8)
print 'RUNNING:', cmd
print '-'*(len(cmd)+8)
return subprocess.check_call([cmd],
shell=True,
stdout=sys.stdout,
stderr=sys.stderr)
def run_regen(base_file,i):
filename = base_file + '.' + str(i)
cmd = GEN_COMMAND + ' --outfile_base ' + filename\
+ ' --mesh_file ' + filename + '.tri'
print '#'*(len(cmd)+8)
print 'RUNNING:', cmd
print '-'*(len(cmd)+8)
return subprocess.check_call([cmd],
shell=True,
stdout=sys.stdout,
stderr=sys.stderr)
def plot_solution(base_file, I):
Modes = [('value',False),('policy',False),('agg',True)]
plt.figure(2)
plt.clf()
for (i,(mode,log)) in enumerate(Modes):
plt.subplot(2,2,i+1)
tmv.plot_solution_mesh(base_file + '.' + str(I),
base_file + '.' + str(I) + '.sol',
mode,log)
plt.title(mode)
plt.suptitle(base_file + str(I))
def plot_refine_stats(base_file, I):
plt.figure(3)
plt.clf()
(nodes,faces) = read_shewchuk(base_file + '.' + str(I))
F = faces.shape[0]
unarch = Unarchiver(base_file + '.' + str(I+1) + '.stats')
number_of_vects = len(unarch.data)
R = int(np.ceil(np.sqrt(number_of_vects)))
C = int(np.ceil(float(number_of_vects) / float(R)))
assert R*(C-1) < number_of_vects <= R*C
for (i,(name,f)) in enumerate(unarch.data.items()):
plt.subplot(R,C,i+1)
assert((F,) == f.shape)
tmv.plot_faces(nodes,faces,f)
plt.title(name)
plt.suptitle(base_file + '.' + str(I))
if __name__ == "__main__":
(_,dir) = sys.argv
base_file = dir + '/' + BASE_FILENAME
# INITIAL MESH AND LCP GENERATION
rc = run_gen_initial(base_file)
print rc
assert(0==rc)
plt.figure(1)
plt.subplot(1,2,1)
tmv.plot_bare_mesh(base_file + '.0')
plt.title('Initial mesh')
plt.draw()
show_plots = True
iterations = 12
for I in xrange(iterations):
print 'Iteration',I
# SOLVE THE LCP
solve_lcp_file(base_file + '.' + str(I) + '.lcp') # Generates .sol
# PLOT THE SOLUTION
if show_plots:
plot_solution(base_file,I)
plt.draw()
# REFINE
rc = run_refine(base_file,I)
assert(0==rc)
# PLOT THE HEURISTICS
if show_plots:
plot_refine_stats(base_file,I)
plt.draw()
# Generate the LCP based on the refined mesh
rc = run_regen(base_file,I+1)
assert(0==rc)
plt.figure(1)
plt.subplot(1,2,2)
tmv.plot_bare_mesh(base_file + '.' + str(iterations))
plt.title('Final mesh')
plt.show()