-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrmp.py
197 lines (136 loc) · 3.6 KB
/
rmp.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
# RMPflow basic classes
# @author Anqi Li
# @date April 8, 2019
import numpy as np
class RMPNode:
"""
A Generic RMP node
"""
def __init__(self, name, parent, psi, J, J_dot, verbose=False):
self.name = name
self.parent = parent
self.children = []
# connect the node to its parent
if self.parent:
self.parent.add_child(self)
# mapping/J/J_dot for the edge from the parent to the node
self.psi = psi
self.J = J
self.J_dot = J_dot
# state
self.x = None
self.x_dot = None
# RMP
self.f = None
self.a = None
self.M = None
# print the name of the node when applying operations if true
self.verbose = verbose
def add_child(self, child):
"""
Add a child to the current node
"""
self.children.append(child)
def pushforward(self):
"""
apply pushforward operation recursively
"""
if self.verbose:
print('%s: pushforward' % self.name)
if self.psi is not None and self.J is not None:
self.x = self.psi(self.parent.x)
self.x_dot = np.dot(self.J(self.parent.x), self.parent.x_dot)
assert self.x.ndim == 2 and self.x_dot.ndim == 2
[child.pushforward() for child in self.children]
def pullback(self):
"""
apply pullback operation recursively
"""
[child.pullback() for child in self.children]
if self.verbose:
print('%s: pullback' % self.name)
f = np.zeros_like(self.x, dtype='float64')
M = np.zeros((max(self.x.shape), max(self.x.shape)),
dtype='float64')
for child in self.children:
J_child = child.J(self.x)
J_dot_child = child.J_dot(self.x, self.x_dot)
assert J_child.ndim == 2 and J_dot_child.ndim == 2
if child.f is not None and child.M is not None:
f += np.dot(J_child.T , (child.f - np.dot(
np.dot(child.M, J_dot_child), self.x_dot)))
M += np.dot(np.dot(J_child.T, child.M), J_child)
self.f = f
self.M = M
class RMPRoot(RMPNode):
"""
A root node
"""
def __init__(self, name):
RMPNode.__init__(self, name, None, None, None, None)
def set_root_state(self, x, x_dot):
"""
set the state of the root node for pushforward
"""
assert x.ndim == 1 or x.ndim == 2
assert x_dot.ndim == 1 or x_dot.ndim == 2
if x.ndim == 1:
x = x.reshape(-1, 1)
if x_dot.ndim == 1:
x_dot = x_dot.reshape(-1, 1)
self.x = x
self.x_dot = x_dot
def pushforward(self):
"""
apply pushforward operation recursively
"""
if self.verbose:
print('%s: pushforward' % self.name)
[child.pushforward() for child in self.children]
def resolve(self):
"""
compute the canonical-formed RMP
"""
if self.verbose:
print('%s: resolve' % self.name)
self.a = np.dot(np.linalg.pinv(self.M), self.f)
return self.a
def solve(self, x, x_dot):
"""
given the state of the root, solve for the controls
"""
self.set_root_state(x, x_dot)
self.pushforward()
self.pullback()
return self.resolve()
class RMPLeaf(RMPNode):
"""
A leaf node
"""
def __init__(self, name, parent, parent_param, psi, J, J_dot, RMP_func):
RMPNode.__init__(self, name, parent, psi, J, J_dot)
self.RMP_func = RMP_func
self.parent_param = parent_param
def eval_leaf(self):
"""
compute the natural-formed RMP given the state
"""
self.f, self.M = self.RMP_func(self.x, self.x_dot)
def pullback(self):
"""
pullback at leaf node is just evaluating the RMP
"""
if self.verbose:
print('%s: pullback' % self.name)
self.eval_leaf()
def add_child(self, child):
print("CANNOT add a child to a leaf node")
pass
def update_params(self):
"""
to be implemented for updating the parameters
"""
pass
def update(self):
self.update_params()
self.pushforward()