-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMA.py
More file actions
115 lines (104 loc) · 3.53 KB
/
Copy pathCMA.py
File metadata and controls
115 lines (104 loc) · 3.53 KB
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
import matplotlib.pyplot as plt
import random
import sys
import numpy as np
import os
from neuralnetwork import NN
from tracker import Tracker
from numpy.random import rand
from numpy import pi, std, array, linspace, sin, cos
from car import Car
from trajectory import Trajectory
from multiprocessing import Pool
from colorsys import hsv_to_rgb
from controllers import PID_controller, Simple_controller
from cma import fmin2
N_TRAIN_DIRECTIONS = 1
N_TRAIN_TRAJ = 1
N_NEURONS = 10
VELOCITY = 1
STEP = 33
def generate_path(STEP):
path_list = []
point = [0,0]
location = 0
path_list.append(point)
for _ in range(3):
angle = random.uniform(-pi/4,pi/4)
y_val = sin(angle)*STEP + point[1]
x_val = cos(angle)*STEP + point[0]
point = [x_val,y_val]
path_list.append(point)
return path_list
def generate_paths():
paths =[]
for i in range(N_TRAIN_TRAJ):
path_list = generate_path(STEP)
paths.append(path_list)
return paths
def function_x(args):
x, v, dir, traj = args
controller = NN(x).controller
traj = Trajectory(traj)
tracker = Tracker(v, dir, traj, controller)
return tracker.run()[0]
if __name__ == '__main__':
pool = Pool()
paths = generate_paths()
if len(sys.argv) <= 1:
print "type python CMA.py name.file or python CMA.py -i name.file"
elif len(sys.argv) == 2:
def objective(x, v):
controller = NN(x).controller
trackers = []
for i in range(N_TRAIN_DIRECTIONS):
for traj in paths:
trackers.append([x,v,i*2*pi/N_TRAIN_DIRECTIONS,traj])
costs = pool.map(function_x, trackers)
return max(costs)
x = 2*rand(1,4*N_NEURONS+1)[0]-1
res = fmin2(objective,
x,
.5,
args=(VELOCITY, ),
options={'popsize': 256,
'bounds': [-1, 1],
'maxiter': 256}) # 5th is mean of final sample distribution
res=res[1].result[0]
controller = NN(res).controller
trackers = []
for i in range(N_TRAIN_DIRECTIONS):
for t in paths:
traj = Trajectory(t)
orientation = i*2*pi/N_TRAIN_DIRECTIONS
trackers.append(Tracker(VELOCITY,orientation,traj,controller))
traces = map(lambda x: x.run(), trackers)
for i, trace in enumerate(traces):
plt.plot(trace[1], trace[2], ':', color=hsv_to_rgb(linspace(0, 1, len(traces))[i],1,1))
for path in paths:
plt.plot(*zip(*path))
plt.show()
res = res.tolist()
f = open(sys.argv[1],"w+")
f.write(str(res))
f.close()
elif sys.argv[1] == '-i' and len(sys.argv) == 3:
if os.path.isfile(sys.argv[2]):
searchfile = open(sys.argv[2])
lines = searchfile.readlines()
res = np.array(eval(lines[0]))
searchfile.close()
controller = NN(res).controller
trackers = []
for i in range(N_TRAIN_DIRECTIONS):
for traj in paths:
traj = Trajectory(traj)
trackers.append(Tracker(VELOCITY,i*2*pi/N_TRAIN_DIRECTIONS,traj,controller))
traces = map(lambda x: x.run(), trackers)
for i, trace in enumerate(traces):
plt.plot(trace[1], trace[2], ':', color=hsv_to_rgb(linspace(0, 1, len(traces))[i],1,1))
for path in paths:
plt.plot(*zip(*path))
plt.show()
else:
print "file does not exist"