-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwpr_example.py
161 lines (123 loc) · 4.67 KB
/
lwpr_example.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
#!/usr/bin/env python2.7
import past
from numpy import *
from matplotlib import pyplot as plt
from ctypes import *
from lwpr import LWPR
import os, ast
import numpy as np
from random import *
demonstrations = []
R = Random()
y_min = -2
y_max = 2
def load_demonstration_from_folder(path, traj_file):
with open(path+traj_file, "r") as traj:
demo = ast.literal_eval(traj.read())
demonstrations.append(demo)
def load_demonstrations_from_folder(path):
files = [name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name))]
# get numbers from these files
numbers = [int(os.path.splitext(f)[0].split('_')[-1]) for f in files]
# sort them in ascending order
numbers.sort()
# make list of these files
files = ["demonstration_" + str(number) + ".txt" for number in numbers]
for demo in files:
load_demonstration_from_folder(path, demo)
# path = '/home/fmeccanici/Documents/thesis/thesis_workspace/src/promp_demo_2d/data/good/'
path = '/home/fmeccanici/Documents/thesis/thesis_workspace/src/promp_demo_2d/data/lwpr/'
load_demonstrations_from_folder(path)
# dimension of outputs
n_out = len(demonstrations[0][0])
# dimension of inputs
n_in = len(demonstrations[0][1])
# n_out = 1
# create x vector for plotting
x = np.linspace(0, 4, n_out)
plt.figure()
plt.ylim([y_min, y_max])
plt.title("Demonstrated trajectories")
for demonstration in demonstrations:
plt.plot(x, demonstration[0], 'o-')
context = np.asarray(demonstration[1])
plt.title("Predicted trajectory")
plt.ylim([y_min, y_max])
context1 = [2.0, context[0]]
context2 = [3.6, context[1]]
circle1 = plt.Circle((context1[0], context1[1]), 0.1, color='b', fill=False)
circle2 = plt.Circle((context2[0], context2[1]), 0.1, color='b', fill=False)
ax = plt.gca()
ax.add_artist(circle1)
ax.add_artist(circle2)
plt.grid()
plt.savefig('/home/fmeccanici/Documents/thesis/thesis_workspace/src/promp_demo_2d/figures/lwpr/lwpr_demos.png')
plt.clf()
# initialize lwpr model
model = LWPR(n_in, n_out)
model.init_D = 10*eye(n_in)
model.init_alpha = 0.1* eye(n_in)
# model.kernel = 'BiSquare'
for i in range(10):
for demonstration in demonstrations:
output = np.asarray(demonstration[0])
context = np.asarray(demonstration[1])
# print("added output: " + str(output))
# print("added context: " + str(context))
model.update(context, output)
# generalize
# y = [-1.0, 0.0, 1.0]
y = [-1.0, -0.5, 0.0, 0.5, 1.0]
for y1 in y:
plt.figure()
for y2 in y:
context = np.asarray([y1, y2])
output, conf = model.predict_conf(context)
# print("predicted output: " + str(output))
plt.title("Predicted trajectory")
plt.ylim([y_min, y_max])
plt.plot(x, output, 'ro-')
context1 = [2.0, context[0]]
context2 = [3.6, context[1]]
circle1 = plt.Circle((context1[0], context1[1]), 0.1, color='b', fill=False)
circle2 = plt.Circle((context2[0], context2[1]), 0.1, color='b', fill=False)
ax = plt.gca()
ax.add_artist(circle1)
ax.add_artist(circle2)
plt.grid()
plt.savefig('/home/fmeccanici/Documents/thesis/thesis_workspace/src/promp_demo_2d/figures/lwpr/lwpr_prediction_' + str((y1)) + str((y2)) + '.png' )
circle1.remove()
circle2.remove()
plt.clf()
demonstrations = []
path = '/home/fmeccanici/Documents/thesis/thesis_workspace/src/promp_demo_2d/data/lwpr/obstacle/'
load_demonstrations_from_folder(path)
for i in range(10):
for demonstration in demonstrations:
output = np.asarray(demonstration[0])
context = np.asarray(demonstration[1])
# print("added output: " + str(output))
# print("added context: " + str(context))
model.update(context, output)
y = [-1.0, 0.0, 1.0]
for y1 in y:
plt.figure()
for y2 in y:
context = np.asarray([y1, y2])
output, conf = model.predict_conf(context)
# print("predicted output: " + str(output))
plt.title("Predicted trajectory")
plt.ylim([y_min, y_max])
plt.plot(x, output, 'ro-')
context1 = [2.0, context[0]]
context2 = [3.6, context[1]]
circle1 = plt.Circle((context1[0], context1[1]), 0.1, color='b', fill=False)
circle2 = plt.Circle((context2[0], context2[1]), 0.1, color='b', fill=False)
ax = plt.gca()
ax.add_artist(circle1)
ax.add_artist(circle2)
plt.grid()
plt.savefig('/home/fmeccanici/Documents/thesis/thesis_workspace/src/promp_demo_2d/figures/lwpr/obstacle/lwpr_prediction_' + str((y1)) + str((y2)) + '.png' )
circle1.remove()
circle2.remove()
plt.clf()