-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_opt.py
executable file
·201 lines (175 loc) · 7.76 KB
/
genetic_opt.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
#!/usr/bin/env python
import os, sys, re
import numpy as np
import argparse
from itertools import izip
parser = argparse.ArgumentParser(description="genetic algorithm optimization of trapeze swing")
parser.add_argument("--flyer_file",type=str,help="file describing flyer")
parser.add_argument("--poses_file",type=str,help="file of initial poses")
parser.add_argument("--pose_seq_file",type=str,help="file of initial pose timings")
parser.add_argument("--initial_pert",type=float,help="initial perturbation",default=0.01)
parser.add_argument("--pert",type=float,help="perturbation for each generation",default=0.006)
parser.add_argument("--n_pop",type=int,help="number of members in population",default=20)
parser.add_argument("--n_gen",type=int,help="number of generations",default=200)
parser.add_argument("prefix",type=str,help="prefix for output files")
args = parser.parse_args()
print "# args", args
output_prefix = args.prefix
n_pop = args.n_pop
n_gen = args.n_gen
# read list of joints and max torques
joint_limits = {}
with open(args.flyer_file) as fin:
for l in fin.readlines():
m = re.search("^\s*([^_]*)_(max_torque|lower_limit|upper_limit)\s+(.+)\s*$", l.strip())
if m:
name = m.group(1)
prop = m.group(2)
val = float(m.group(3))
if name not in joint_limits:
joint_limits[name] = {}
joint_limits[name][prop] = val
joints = sorted(joint_limits.keys())
print "# joints", joints
# read initial poses into dict
pose_params = {}
cur_pose = None
with open(args.poses_file) as fin:
for l in fin.readlines():
m = re.search("^\s*POSE\s+([^ ]+)\s+.\s+[0-9]+\s+([^ ]+)\s*$",l.strip())
if m:
cur_pose = m.group(1)
base_pose = m.group(2)
if base_pose != "-":
pose_params[cur_pose] = [x for x in pose_params[base_pose]]
else:
pose_params[cur_pose] = [None] * (2*len(joints))
else:
m = re.search("^\s*([^ ]+)\s+([^ ]+)\s+([^ ]+)", l.strip())
joint_name = m.group(1)
angle = float(m.group(2))
max_torque = m.group(3)
if max_torque == "-":
max_torque = joint_limits[joint_name]["max_torque"]
else:
max_torque = float(max_torque)
pose_params[cur_pose][2*joints.index(joint_name)] = angle
pose_params[cur_pose][2*joints.index(joint_name)+1] = max_torque
print "got pose_params", pose_params.keys()
pose_timings = []
pose_seq = []
with open(args.pose_seq_file) as fin:
l = fin.readline()
for (pose_time, pose_name) in izip(l.split()[0::2], l.split()[1::2]):
pose_timings.append(float(pose_time))
pose_seq.append(pose_name)
print "got pose_timings", pose_timings
print "got pose_names", pose_seq
n_pose_seq = len(pose_seq)
# initialize params
initial_params_vec = []
initial_params_vec.extend(pose_timings)
for pose in sorted(pose_seq):
initial_params_vec.extend(pose_params[pose])
n_params = len(initial_params_vec)
params = np.zeros((n_pop,n_params))
params[:,:] = initial_params_vec
# initialize param limits
params_min = np.zeros((n_pop,n_params))
params_max = np.zeros((n_pop,n_params))
# timings params
params_min[:,0:n_pose_seq] = 0.001
params_max[:,0:n_pose_seq] = 1.999
# pose params
for (pose_i, pose) in enumerate(sorted(pose_seq)):
for (joint_i, joint) in enumerate(joints):
params_min[:,n_pose_seq+pose_i*len(joints)*2+joint_i*2] = joint_limits[joint]["lower_limit"]
params_max[:,n_pose_seq+pose_i*len(joints)*2+joint_i*2] = joint_limits[joint]["upper_limit"]
params_min[:,n_pose_seq+pose_i*len(joints)*2+joint_i*2+1] = 0.0
params_max[:,n_pose_seq+pose_i*len(joints)*2+joint_i*2+1] = joint_limits[joint]["max_torque"]
def write_poses(fout, pose_params, pose_seq):
kf = ord('a')
for (ki, pose) in enumerate(sorted(pose_seq)):
fout.write("POSE {} {} {} -\n".format(pose, chr(kf+ki), len(joints)))
for (ji, joint) in enumerate(joints):
fout.write(" {} {} {}\n".format(joint, pose_params[ki*len(joints)*2+ji*2], pose_params[ki*len(joints)*2+ji*2+1]))
def pose_seq_str(params, pose_seq):
return " ".join([str(params[i])+" "+pose_seq[i] for i in range(len(pose_seq))])
def pert_params(params, mag):
params_range = params_max - params_min
params += params_range*np.random.normal(scale=mag, size=params.shape)
timing_params = params[:,:n_pose_seq]
timing_params.sort(axis=1)
params[:,:n_pose_seq] = timing_params
too_low_ind = np.where(params < params_min)
params[too_low_ind] = params_min[too_low_ind]
too_high_ind = np.where(params > params_max)
params[too_high_ind] = params_max[too_high_ind]
def eval_params(params, pose_seq):
scores = []
for i_p in range(params.shape[0]):
sys.stderr.write("eval {}\n".format(i_p))
with open("t_poses.data","w") as fout:
write_poses(fout, params[i_p][n_pose_seq:], pose_seq)
result = os.popen("env DYLD_LIBRARY_PATH=':/Users/noamb/simbody/lib' ./trapeze --headless --len 25 " +
"--flyer {} --poses t_poses.data --pose_seq {} " .format(args.flyer_file, pose_seq_str(params[i_p][0:n_pose_seq], pose_seq)) +
"| egrep 'bar_pos -' | nl | head -4 | tail -1" ).readline()
scores.append(float(result.split()[4]))
return np.array(scores)
def process_best(max_score, prefix, scores, params, iter_label):
max_score_ind = np.argmax(scores)
if scores[max_score_ind] > max_score:
max_score = scores[max_score_ind]
print "# BEST ", iter_label, max_score
with open(prefix+".genetic_opt.{}.pose_seq.data".format(iter_label),"w") as fout:
fout.write(pose_seq_str(params[max_score_ind], pose_seq)+"\n")
with open(prefix+".genetic_opt.{}.poses.data".format(iter_label),"w") as fout:
write_poses(fout, params[max_score_ind][n_pose_seq:], pose_seq)
with open(prefix+".genetic_opt.{}.pose_seq.data".format("BEST"),"w") as fout:
fout.write(pose_seq_str(params[max_score_ind], pose_seq)+"\n")
with open(prefix+".genetic_opt.{}.poses.data".format("BEST"),"w") as fout:
write_poses(fout, params[max_score_ind][n_pose_seq:], pose_seq)
return max_score
# write initial set
scores = eval_params(np.array([params[0]]), pose_seq)
max_score = process_best(-1.0e38, output_prefix, scores, params, -1)
sys.stdout.flush()
pert_params(params, args.initial_pert)
# write initial perturbation set and the best (if better than initial)
scores = eval_params(params, pose_seq)
for i in range(len(scores)):
print 0, scores[i], " ".join([str(x) for x in params[i]])
print ""
print ""
max_score = process_best(max_score, output_prefix, scores, params, 0)
sys.stdout.flush()
for i_gen in range(n_gen):
# find best half
best_scores_ind = np.argsort(scores)[n_pop/2:]
# duplicate best half twice
params[:n_pop/2] = params[best_scores_ind]
params[n_pop/2:] = params[0:n_pop/2]
# permute
np.random.shuffle(params)
# cross half
for i in range(0,n_pop/2,2):
(c1, c2) = np.random.choice(n_params+1, 2, replace=False)
if c1 > c2:
(c1,c2) = (c2, c1)
mask = [ ii >= c1 and ii < c2 for ii in range(n_params)]
p1 = np.select([mask, np.logical_not(mask)], [params[i], params[i+1]])
p2 = np.select([np.logical_not(mask), mask], [params[i], params[i+1]])
params[i][:] = p1
params[i+1][:] = p2
# perturb
pert_params(params, args.pert)
# new scores
scores = eval_params(params, pose_seq)
# print set
for i in range(len(scores)):
print i_gen+1, scores[i], " ".join([str(x) for x in params[i]])
print ""
print ""
# check for new best
max_score = process_best(max_score, output_prefix, scores, params, i_gen+1)
sys.stdout.flush()