-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct.py
496 lines (440 loc) · 16.6 KB
/
direct.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# Copyright 2023 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Python interface for direct trajectory optimization."""
import atexit
import os
import pathlib
import socket
import subprocess
import sys
import tempfile
from typing import Literal, Optional
import grpc
import mujoco
import numpy as np
from numpy import typing as npt
# INTERNAL IMPORT
from mujoco_mpc.proto import direct_pb2
from mujoco_mpc.proto import direct_pb2_grpc
def find_free_port() -> int:
"""Find an available TCP port on the system.
This function creates a temporary socket, binds it to an available port
chosen by the operating system, and returns the chosen port number.
Returns:
int: An available TCP port number.
"""
with socket.socket(family=socket.AF_INET6) as s:
s.bind(("", 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
class Direct:
"""`Direct` class to interface with MuJoCo MPC direct estimator.
Attributes:
port:
channel:
stub:
server_process:
"""
def __init__(
self,
model: mujoco.MjModel,
configuration_length: int,
server_binary_path: Optional[str] = None,
send_as: Literal["mjb", "xml"] = "xml",
colab_logging: bool = True,
):
# server
if server_binary_path is None:
binary_name = "direct_server"
server_binary_path = pathlib.Path(__file__).parent / "mjpc" / binary_name
self._colab_logging = colab_logging
self.port = find_free_port()
self.server_process = subprocess.Popen(
[str(server_binary_path), f"--mjpc_port={self.port}"],
stdout=subprocess.PIPE if colab_logging else None,
)
os.set_blocking(self.server_process.stdout.fileno(), False)
atexit.register(self.server_process.kill)
credentials = grpc.local_channel_credentials(grpc.LocalConnectionType.LOCAL_TCP)
self.channel = grpc.secure_channel(f"localhost:{self.port}", credentials)
grpc.channel_ready_future(self.channel).result(timeout=10)
self.stub = direct_pb2_grpc.DirectStub(self.channel)
# initialize
self.init(
model,
configuration_length,
send_as=send_as,
)
def close(self):
self.channel.close()
self.server_process.kill()
self.server_process.wait()
def init(
self,
model: mujoco.MjModel,
configuration_length: int,
send_as: Literal["mjb", "xml"] = "xml",
):
"""Initialize the direct estimator estimation horizon with `configuration_length`.
Args:
model: optional `MjModel` instance, which, if provided, will be used as
the underlying model for planning. If not provided, the default MJPC
task xml will be used.
configuration_length: estimation horizon.
send_as: The serialization format for sending the model over gRPC; "xml".
"""
# setup model
def model_to_mjb(model: mujoco.MjModel) -> bytes:
buffer_size = mujoco.mj_sizeModel(model)
buffer = np.empty(shape=buffer_size, dtype=np.uint8)
mujoco.mj_saveModel(model, None, buffer)
return buffer.tobytes()
def model_to_xml(model: mujoco.MjModel) -> str:
tmp = tempfile.NamedTemporaryFile()
mujoco.mj_saveLastXML(tmp.name, model)
with pathlib.Path(tmp.name).open("rt") as f:
xml_string = f.read()
return xml_string
if model is not None:
if send_as == "mjb":
model_message = direct_pb2.MjModel(mjb=model_to_mjb(model))
else:
model_message = direct_pb2.MjModel(xml=model_to_xml(model))
else:
model_message = None
# initialize request
init_request = direct_pb2.InitRequest(
model=model_message,
configuration_length=configuration_length,
)
# initialize response
self._wait(self.stub.Init.future(init_request))
def data(
self,
index: int,
configuration: Optional[npt.ArrayLike] = [],
velocity: Optional[npt.ArrayLike] = [],
acceleration: Optional[npt.ArrayLike] = [],
time: Optional[npt.ArrayLike] = [],
ctrl: Optional[npt.ArrayLike] = [],
configuration_previous: Optional[npt.ArrayLike] = [],
sensor_measurement: Optional[npt.ArrayLike] = [],
sensor_prediction: Optional[npt.ArrayLike] = [],
sensor_mask: Optional[npt.ArrayLike] = [],
force_measurement: Optional[npt.ArrayLike] = [],
force_prediction: Optional[npt.ArrayLike] = [],
parameters: Optional[npt.ArrayLike] = [],
parameters_previous: Optional[npt.ArrayLike] = [],
) -> dict[str, np.ndarray]:
# assemble inputs
inputs = direct_pb2.Data(
configuration=configuration,
velocity=velocity,
acceleration=acceleration,
time=time,
ctrl=ctrl,
configuration_previous=configuration_previous,
sensor_measurement=sensor_measurement,
sensor_prediction=sensor_prediction,
sensor_mask=sensor_mask,
force_measurement=force_measurement,
force_prediction=force_prediction,
parameters=parameters,
parameters_previous=parameters_previous,
)
# data request
request = direct_pb2.DataRequest(data=inputs, index=index)
# data response
data = self._wait(self.stub.Data.future(request)).data
# return all data
return {
"configuration": np.array(data.configuration),
"velocity": np.array(data.velocity),
"acceleration": np.array(data.acceleration),
"time": np.array(data.time),
"ctrl": np.array(data.ctrl),
"configuration_previous": np.array(data.configuration_previous),
"sensor_measurement": np.array(data.sensor_measurement),
"sensor_prediction": np.array(data.sensor_prediction),
"sensor_mask": np.array(data.sensor_mask),
"force_measurement": np.array(data.force_measurement),
"force_prediction": np.array(data.force_prediction),
"parameters": np.array(data.parameters),
"parameters_previous": np.array(data.parameters_previous),
}
def settings(
self,
configuration_length: Optional[int] = None,
sensor_flag: Optional[bool] = None,
force_flag: Optional[bool] = None,
max_search_iterations: Optional[int] = None,
max_smoother_iterations: Optional[int] = None,
gradient_tolerance: Optional[float] = None,
verbose_iteration: Optional[bool] = None,
verbose_optimize: Optional[bool] = None,
verbose_cost: Optional[bool] = None,
search_type: Optional[int] = None,
step_scaling: Optional[float] = None,
regularization_initial: Optional[float] = None,
regularization_scaling: Optional[float] = None,
time_scaling_force: Optional[bool] = None,
time_scaling_sensor: Optional[bool] = None,
search_direction_tolerance: Optional[float] = None,
cost_tolerance: Optional[float] = None,
assemble_sensor_jacobian: Optional[bool] = None,
assemble_force_jacobian: Optional[bool] = None,
assemble_sensor_norm_hessian: Optional[bool] = None,
assemble_force_norm_hessian: Optional[bool] = None,
first_step_position_sensors: Optional[bool] = None,
last_step_position_sensors: Optional[bool] = None,
last_step_velocity_sensors: Optional[bool] = None,
) -> dict[str, int | bool]:
# assemble settings
inputs = direct_pb2.Settings(
configuration_length=configuration_length,
sensor_flag=sensor_flag,
force_flag=force_flag,
max_search_iterations=max_search_iterations,
max_smoother_iterations=max_smoother_iterations,
gradient_tolerance=gradient_tolerance,
verbose_iteration=verbose_iteration,
verbose_optimize=verbose_optimize,
verbose_cost=verbose_cost,
search_type=search_type,
step_scaling=step_scaling,
regularization_initial=regularization_initial,
regularization_scaling=regularization_scaling,
time_scaling_force=time_scaling_force,
time_scaling_sensor=time_scaling_sensor,
search_direction_tolerance=search_direction_tolerance,
cost_tolerance=cost_tolerance,
assemble_sensor_jacobian=assemble_sensor_jacobian,
assemble_force_jacobian=assemble_force_jacobian,
assemble_sensor_norm_hessian=assemble_sensor_norm_hessian,
assemble_force_norm_hessian=assemble_force_norm_hessian,
first_step_position_sensors=first_step_position_sensors,
last_step_position_sensors=last_step_position_sensors,
last_step_velocity_sensors=last_step_velocity_sensors,
)
# settings request
request = direct_pb2.SettingsRequest(
settings=inputs,
)
# settings response
settings = self._wait(self.stub.Settings.future(request)).settings
# return all settings
return {
"configuration_length": settings.configuration_length,
"sensor_flag": settings.sensor_flag,
"force_flag": settings.force_flag,
"max_search_iterations": settings.max_search_iterations,
"max_smoother_iterations": settings.max_smoother_iterations,
"gradient_tolerance": settings.gradient_tolerance,
"verbose_iteration": settings.verbose_iteration,
"verbose_optimize": settings.verbose_optimize,
"verbose_cost": settings.verbose_cost,
"search_type": settings.search_type,
"step_scaling": settings.step_scaling,
"regularization_initial": settings.regularization_initial,
"regularization_scaling": settings.regularization_scaling,
"time_scaling_force": settings.time_scaling_force,
"time_scaling_sensor": settings.time_scaling_sensor,
"search_direction_tolerance": settings.search_direction_tolerance,
"cost_tolerance": settings.cost_tolerance,
"assemble_sensor_jacobian": settings.assemble_sensor_jacobian,
"assemble_force_jacobian": settings.assemble_force_jacobian,
"assemble_sensor_norm_hessian": settings.assemble_sensor_norm_hessian,
"assemble_force_norm_hessian": settings.assemble_force_norm_hessian,
"first_step_position_sensors": settings.first_step_position_sensors,
"last_step_position_sensors": settings.last_step_position_sensors,
"last_step_velocity_sensors": settings.last_step_velocity_sensors,
}
def noise(
self,
process: Optional[npt.ArrayLike] = [],
sensor: Optional[npt.ArrayLike] = [],
parameter: Optional[npt.ArrayLike] = [],
) -> dict[str, np.ndarray]:
# assemble input noise
inputs = direct_pb2.Noise(
process=process,
sensor=sensor,
parameter=parameter,
)
# noise request
request = direct_pb2.NoiseRequest(noise=inputs)
# noise response
noise = self._wait(self.stub.Noise.future(request)).noise
# return noise
return {
"process": np.array(noise.process),
"sensor": np.array(noise.sensor),
"parameter": np.array(noise.parameter),
}
def cost(
self,
derivatives: Optional[bool] = False,
internals: Optional[bool] = False,
) -> dict[str, float | np.ndarray | int | list]:
# cost request
request = direct_pb2.CostRequest(
derivatives=derivatives, internals=internals
)
# cost response
cost = self._wait(self.stub.Cost.future(request))
# return all costs
return {
"total": cost.total,
"sensor": cost.sensor,
"force": cost.force,
"parameters": cost.parameter,
"initial": cost.initial,
"gradient": np.array(cost.gradient) if derivatives else [],
"hessian": (
np.array(cost.hessian).reshape(cost.nvar, cost.nvar)
if derivatives
else []
),
"residual_sensor": np.array(cost.residual_sensor) if internals else [],
"residual_force": np.array(cost.residual_force) if internals else [],
"jacobian_sensor": (
np.array(cost.jacobian_sensor).reshape(cost.nsensor, cost.nvar)
if internals
else []
),
"jacobian_force": (
np.array(cost.jacobian_force).reshape(cost.nforce, cost.nvar)
if internals
else []
),
"norm_gradient_sensor": (
np.array(cost.norm_gradient_sensor) if internals else []
),
"norm_gradient_force": (
np.array(cost.norm_gradient_force) if internals else []
),
"norm_hessian_sensor": (
np.array(cost.norm_hessian_sensor).reshape(
cost.nsensor, cost.nsensor
)
if internals
else []
),
"norm_hessian_force": (
np.array(cost.norm_hessian_force).reshape(cost.nforce, cost.nforce)
if internals
else []
),
"nvar": cost.nvar,
"nsensor": cost.nsensor,
"nforce": cost.nforce,
}
def status(self) -> dict[str, int]:
# status request
request = direct_pb2.StatusRequest()
# status response
status = self._wait(self.stub.Status.future(request)).status
# return all status
return {
"search_iterations": status.search_iterations,
"smoother_iterations": status.smoother_iterations,
"step_size": status.step_size,
"regularization": status.regularization,
"gradient_norm": status.gradient_norm,
"search_direction_norm": status.search_direction_norm,
"solve_status": status.solve_status,
"cost_difference": status.cost_difference,
"improvement": status.improvement,
"expected": status.expected,
"reduction_ratio": status.reduction_ratio,
}
def reset(self):
# reset request
request = direct_pb2.ResetRequest()
# reset response
self._wait(self.stub.Reset.future(request))
def optimize(self):
# optimize request
request = direct_pb2.OptimizeRequest()
# optimize response
self._wait(self.stub.Optimize.future(request))
def sensor_info(self) -> dict[str, int]:
# info request
request = direct_pb2.SensorInfoRequest()
# info response
response = self._wait(self.stub.SensorInfo.future(request))
# return info
return {
"start_index": response.start_index,
"num_measurements": response.num_measurements,
"dim_measurements": response.dim_measurements,
}
def measurements_from_sensordata(self, data: npt.ArrayLike) -> np.ndarray:
# get sensor info
info = self.sensor_info()
# return measurements from sensor data
index = info["start_index"]
dim = info["dim_measurements"]
return data[index : (index + dim)]
def print_cost(self):
# get costs
cost = self.cost()
# print
print("cost:")
print(" [total] = ", cost["total"])
print(" sensor = ", cost["sensor"])
print(" force = ", cost["force"])
print(" parameter = ", cost["parameter"])
print(" (initial = ", cost["initial"], ")")
def print_status(self):
# get status
status = self.status()
# print
print("status:")
print(" search iterations = ", status["search_iterations"])
print(" smoother iterations = ", status["smoother_iterations"])
print(" step size = ", status["step_size"])
print(" regularization = ", status["regularization"])
print(" gradient norm = ", status["gradient_norm"])
def status_code(code):
if code == 0:
return "UNSOLVED"
elif code == 1:
return "SEARCH_FAILURE"
elif code == 2:
return "MAX_ITERATIONS_FAILURE"
elif code == 3:
return "SMALL_DIRECTION_FAILURE"
elif code == 4:
return "MAX_REGULARIZATION_FAILURE"
elif code == 5:
return "COST_DIFFERENCE_FAILURE"
elif code == 6:
return "EXPECTED_DECREASE_FAILURE"
elif code == 7:
return "SOLVED"
else:
return "CODE_ERROR"
print("- solve status = ", status_code(status["solve_status"]))
def _wait(self, future):
"""Waits for the future to complete, while printing out subprocess stdout."""
if self._colab_logging:
while True:
line = self.server_process.stdout.readline()
if line:
sys.stdout.write(line.decode("utf-8"))
if future.done():
break
return future.result()