Skip to content

Commit 7c5b964

Browse files
committed
Merge branch 'main' of https://github.com/kyh2010/robot_home_service into main
2 parents 003d066 + 66a74cf commit 7c5b964

18 files changed

+3158
-1
lines changed

env/tasks.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
include_object_data,
3333
)
3434

35+
from ..sEDM.test_edm import sEDM_model
3536

3637
class HomeServiceTaskType(enum.Enum):
3738

@@ -534,6 +535,11 @@ def query_planner(self, **kwargs) -> Sequence[Tuple[str, Dict[str, Any], Optiona
534535
task=self,
535536
)
536537
"""
538+
target_object = self.env.current_task_spec.pickup_target['name'].split("_")[0]
539+
target_place = self.env.current_task_spec.place_target['name'].split("_")[0]
540+
self.task_planner = sEDM_model()
541+
task_plan = self.task_planner.inference(target_object=target_object, target_place=target_place)
542+
537543
if self.task_planner is None:
538544
task_plan = []
539545
pickup_object = self.env.current_task_spec.pickup_object
@@ -1169,4 +1175,4 @@ def next_task(
11691175
else:
11701176
raise e
11711177

1172-
return self._last_sampled_task
1178+
return self._last_sampled_task

sEDM/DRN.py

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
import math
2+
import numpy as np
3+
import itertools
4+
from utils import err, fuzzy_and, fuzzy_or
5+
6+
class DRN(object):
7+
def __init__(self, channel=1, lr=1.0, glr=1.0, alpha=1.0, rho=1, d=2, gamma=1):
8+
self.lr = lr # learning rate
9+
self.glr = glr # global learning rate
10+
self.alpha = alpha # parameter for node activation
11+
self.rho = rho # rho parameter
12+
self.d = d
13+
self.gamma = gamma
14+
self.X = []
15+
self.Y = []
16+
self.w = None # weights for clusters (samples, weights)
17+
self.wg = None # global weight vector
18+
self.n_category = 0 # number of categories
19+
self.group = np.array([]) # group container
20+
self.channel = channel # number of channels in input
21+
22+
def learningNN(self, point): # sample = [-0.0213,-0.0578]
23+
self.X = point
24+
self.updateWg(self.X)
25+
self.activateNN()
26+
nu_index, all_index = self.code_comp()
27+
print(all_index)
28+
# resonance check
29+
if len(all_index) == 0:
30+
condition = False
31+
else:
32+
for i in range(self.n_category):
33+
resonanceIdx = all_index[i]
34+
condition = self.resonance(self.w[resonanceIdx])
35+
if condition:
36+
break
37+
if condition: # condition 추가
38+
self.w[resonanceIdx] = self.updateNN(point, self.w[resonanceIdx], self.lr)
39+
else:
40+
self.n_category += 1
41+
self.Y.append(1)
42+
if self.w is None:
43+
self.w = np.atleast_2d([np.hstack((self.X, self.X))])
44+
else:
45+
self.w = np.vstack((self.w, np.hstack((self.X, self.X))))
46+
nu_index = np.append(nu_index, self.n_category-1) # np.append(nu_index, self.n_category)
47+
if self.n_category > 1:
48+
self.add_group(nu_index, condition, resonanceIdx)
49+
50+
def add_group(self, index, condition, resonanceIdx):
51+
index = np.array(list(itertools.combinations(index, self.d)))
52+
for i in range(index.shape[0]):
53+
is_present = False
54+
for j in range(self.group.shape[0]): # group = np.array([[1, 2,4, 3], [2, 3, 4,4],[4,5,6,4]]) shape:(3, 4)
55+
if all(self.group[j,1:3] == index[i]):
56+
is_present = True
57+
break
58+
if is_present == False:
59+
T = self.synapticStrength(index[i])
60+
if self.group.size == 0:
61+
self.group = np.append(self.group, np.append(T, index[i]))
62+
self.group = np.array([self.group])
63+
else:
64+
self.group = np.vstack((self.group, np.append(T, index[i])))
65+
66+
if condition == True:
67+
for j in range(self.group.shape[0]):
68+
if any(self.group[j,1:3] == resonanceIdx):
69+
T = self.synapticStrength(self.group[j,1:3])
70+
self.group[i, 0] = T
71+
72+
def synapticStrength(self, index):
73+
p = []
74+
for i in range(index.size):
75+
for j in range(self.channel):
76+
points = np.vstack((self.w[int(index[i]), :self.w.shape[1]//2], self.w[int(index[i]), self.w.shape[1]//2:]))
77+
p.append(self.coM_calc(points))
78+
p = np.array(p)
79+
CoM_length = p[0] - p[1]
80+
T = math.exp(-self.alpha * np.linalg.norm(CoM_length, 2))
81+
return T
82+
83+
def coM_calc(self, points):
84+
coM_point = np.sum(points, axis=0) / points.shape[0]
85+
return coM_point
86+
87+
def updateWg(self, point):
88+
if self.n_category == 0:
89+
self.wg = np.append(self.X, self.X)
90+
else:
91+
e = err(self.X, self.wg)
92+
if e != 0:
93+
self.wg = self.updateNN(point, self.wg, self.glr)
94+
self.grouping()
95+
96+
def updateNN(self, input, weight, lr):
97+
w1 = weight[:len(weight)//2]
98+
w2 = weight[len(weight)//2:]
99+
weight = (1 - lr) * weight + lr * (np.append(fuzzy_and(input, w1), fuzzy_or(input, w2)))
100+
return weight
101+
102+
def activateNN(self):
103+
for i in range(self.n_category):
104+
e = err(self.X, self.w[i])
105+
self.Y[i] = math.exp(-self.alpha*e)
106+
107+
def code_comp(self):
108+
all_index = np.array(self.Y).argsort()[::-1]
109+
nu_index = all_index
110+
if all_index.size == 0: # if there is no node
111+
all_index = []
112+
nu_index = []
113+
elif all_index.size != 1: # if there are several nodes
114+
if self.d > all_index.size:
115+
nu_index = all_index
116+
else:
117+
nu_index = all_index[:self.d]
118+
nu_index = np.sort(nu_index)
119+
return nu_index, all_index
120+
121+
def resonance(self, weight):
122+
w1 = weight[:len(weight) // 2]
123+
w2 = weight[len(weight) // 2:]
124+
M = self.wg[len(self.wg) // 2:] - self.wg[:len(self.wg) // 2]
125+
S = fuzzy_or(self.X, w2) - fuzzy_and(self.X, w1)
126+
epsilon = 1e-6
127+
n = w1.size
128+
M[M == 0] = epsilon
129+
L = S / M
130+
if sum(L) / n <= 1 - self.rho:
131+
condition = True
132+
else:
133+
condition = False
134+
return condition
135+
136+
def resonance_grouping(self, weight):
137+
w1 = weight[:len(weight) // 2]
138+
w2 = weight[len(weight) // 2:]
139+
M = self.wg[len(self.wg) // 2:] - self.wg[:len(self.wg) // 2]
140+
S = w2 - w1
141+
epsilon = 1e-6
142+
n = w1.size
143+
M[M == 0] = epsilon
144+
L = S / M
145+
if sum(L) / n <= 1 - self.rho:
146+
condition = True
147+
else:
148+
condition = False
149+
return condition
150+
151+
def grouping(self):
152+
# Sort all activations in (nn.group)
153+
if self.group.size == 0:
154+
iter = self.group.size
155+
else:
156+
iter = np.array(self.group).shape[0]
157+
activation = np.zeros((iter))
158+
for i in range(iter):
159+
activation[i] = self.group[i, 0]
160+
sort_index = np.array(activation).argsort()[::-1]
161+
162+
# Check resonance of multi_w
163+
delete_index = []
164+
for i in range(iter):
165+
# Group two target weights
166+
target_weight = self.group[sort_index[i], 1:3]
167+
if target_weight[0] == target_weight[1]:
168+
continue
169+
170+
multi_w1 = fuzzy_and(self.w[int(target_weight[0]),:self.w.shape[1]//2], self.w[int(target_weight[1]),:self.w.shape[1]//2])
171+
multi_w2 = fuzzy_or(self.w[int(target_weight[0]),self.w.shape[1]//2:], self.w[int(target_weight[1]),self.w.shape[1]//2:])
172+
multi_w = np.append(multi_w1, multi_w2)
173+
174+
condition = self.resonance_grouping(multi_w)
175+
176+
if condition == True: # grouping
177+
self.w[min(target_weight)] = multi_w
178+
# Substitute same indices
179+
for j in range(iter):
180+
if any(self.group[j,1:3] == max(target_weight)):
181+
ind = int(np.where(self.group[j,1:3] == max(target_weight))[0])
182+
self.group[j,ind+1] = min(target_weight)
183+
delete_index.append(max(target_weight)) # Save the substituted node number
184+
# Remove all redundant nodes
185+
self.n_category = self.n_category - len(delete_index)
186+
delete_index = np.array(delete_index).argsort()[::-1]
187+
for i in range(delete_index.size):
188+
self.Y.pop(delete_index[i])
189+
self.w = np.delete(self.w, delete_index[i], axis=0)
190+
# Clear all groups
191+
for i in range(iter):
192+
if self.group[iter-i-1,1] == self.group[iter-i-1,2]:
193+
self.group = np.delete(self.group, iter-i+1, axis=0)
194+
# Clear same groups
195+
iter = self.group.shape[0]
196+
delete_group = []
197+
for i in range(self.group.shape[0]-1):
198+
for j in range(i+1, self.group.shape[0]):
199+
if all(self.group[i,1:3] == self.group[j,1:3]) or all(self.group[i,1:3] == np.append(self.group[j,2], self.group[j,1])):
200+
if len(delete_group) == 0:
201+
delete_group.append(j)
202+
else:
203+
if not any(np.array(delete_group) == j):
204+
delete_group.append(j)
205+
delete_group = np.array(delete_group).argsort()[::-1]
206+
for i in range(delete_group.size):
207+
self.group = np.delete(self.group, delete_group[i], axis=0)
208+
# Match numbers in a group
209+
for j in range(delete_index.size):
210+
for i in range(self.group.shape[0]):
211+
if any(self.group[i, 1:3] > delete_index[j]):
212+
ind = np.where(self.group[i, 1:3] > delete_index[j])[0]
213+
for k in range(ind.size):
214+
self.group[i, ind[k]+1] = self.group[i, ind[k]+1] - 1
215+
216+
# Update all related lengths with updated weight
217+
for j in range(self.group.shape[0]):
218+
T = self.synapticStrength(self.group[j, 1:3])
219+
self.group[j][0] = T
220+
221+
def train(self, X):
222+
X = np.array(X)
223+
for i in range(X.shape[0]):
224+
for j in range(np.array(X[i]).shape[0]):
225+
sample = np.array(X[i][j])
226+
self.learningNN(sample)
227+
Y = self.testDRN(X)
228+
return Y
229+
230+
def testDRN(self, X):
231+
X = np.array(X)
232+
dataNum = X.shape[0]
233+
Y = [[] for _ in range(dataNum)]
234+
for i in range(dataNum):
235+
for j in range(np.array(X[i]).shape[0]):
236+
self.X = np.array(X[i][j])
237+
self.activateNN()
238+
ind = np.argmax(self.Y)
239+
YY = [0 if i != ind else x for i, x in enumerate(self.Y)]
240+
Y[i].append(YY)
241+
return Y
242+
243+
def readout(self, Y):
244+
X = []
245+
XX = []
246+
for i in range(Y.shape[0]):
247+
for j in range(Y[i].shape[0]):
248+
ind = np.argmax(Y[i][j])
249+
if i == 0 and j == 0:
250+
X.append([self.w[ind][:self.w.shape[1] // 2]])
251+
else:
252+
X.append([self.w[ind][:self.w.shape[1]//2]])
253+
if i + 1 < Y.shape[0]:
254+
XX.append(np.array(X).squeeze(1))
255+
X = []
256+
XX.append(np.array(X).squeeze(1))
257+
XXX = np.array(XX)
258+
return XXX
259+
260+
261+
262+
263+
if __name__ == '__main__':
264+
# x = np.array([np.array([[-0.0213,-0.0578],[-0.1272,0.0689],[-0.0897,-0.1516]]),
265+
# np.array([[-0.0973,-0.0610],[ -0.0161,-0.0583]]),
266+
# np.array([[0.0138,0.0760]]),
267+
# np.array([[-0.1041,-0.1227],[0.0237,0.0132]]),
268+
# np.array([[-0.0152,-0.0120],[0.0062,0.0617]])])
269+
x = np.array([np.array([[-0.0213, -0.0578], [-0.0213, -0.0578], [-0.0897, -0.1516]]),
270+
np.array([[-0.0973,-0.0610],[ -0.0161,-0.0583]])])
271+
model = DRN()
272+
model.train(x)
273+
Y = model.testDRN(x)
274+
print(Y)

0 commit comments

Comments
 (0)