-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody_parts.py
356 lines (284 loc) · 13 KB
/
body_parts.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
from __future__ import annotations
import pyrosim_z as psz
from typing import NamedTuple
from typing import Union
from enum import Enum
import random
import numpy as np
VALID_CUBE_ELEMENTS = (1, 6)
class CubeElement(Enum):
CENTER = (0, 0, 0)
FRONT = (1, 0, 0)
BACK = (-1, 0, 0)
RIGHT = (0, 1, 0)
LEFT = (0, -1, 0)
TOP = (0, 0, 1)
BOTTOM = (0, 0, -1)
FRONT_RIGHT = (1, 1, 0)
FRONT_LEFT = (1, -1, 0)
FRONT_TOP = (1, 0, 1)
FRONT_BOTTOM = (1, 0, -1)
BACK_RIGHT = (-1, 1, 0)
BACK_LEFT = (-1, -1, 0)
BACK_TOP = (-1, 0, 1)
BACK_BOTTOM = (-1, 0, -1)
RIGHT_TOP = (0, 1, 1)
RIGHT_BOTTOM = (0, 1, -1)
LEFT_TOP = (0, -1, 1)
LEFT_BOTTOM = (0, -1, -1)
FRONT_RIGHT_TOP = (1, 1, 1)
FRONT_RIGHT_BOTTOM = (1, 1, -1)
FRONT_LEFT_TOP = (1, -1, 1)
FRONT_LEFT_BOTTOM = (1, -1, -1)
BACK_RIGHT_TOP = (-1, 1, 1)
BACK_RIGHT_BOTTOM = (-1, 1, -1)
BACK_LEFT_TOP = (-1, -1, 1)
BACK_LEFT_BOTTOM = (-1, -1, -1)
class Axes(Enum):
X = [1, 0, 0]
Y = [0, 1, 0]
Z = [0, 0, 1]
class Position(NamedTuple):
x: float
y: float
z: float
class Dimensions(NamedTuple):
length: float
width: float
height: float
class BodyCons(NamedTuple):
body_cons_id: int
body_part: Union[BodyPart, BodyCons]
build_specifications: list[BuildSpecifications]
next_body_plans: Union[dict[CubeElement, BodyCons], None]=None
class BuildSpecifications(NamedTuple):
direction_to_build: CubeElement=CubeElement.FRONT
repitions: int=1
axis: Axes=Axes.X
class Genome(NamedTuple):
bodycons_id: int
brain_chromosome: NeuronWeightMatrix
body_chromosome: BodyCons
def create_random_xyz(mins: Union[float, Dimensions, Position], maxes: Union[float, Dimensions, Position]) -> Union[Dimensions, Position]:
return tuple(mins[index] + (maxes[index] - mins[index]) * random.random() for index in range(3))
def add_xyz(xyz_tuple1: Union[Position, Dimensions, tuple[float, float, float]], xyz_tuple2: Union[Position, Dimensions, tuple[float, float, float]]) -> tuple[float, float, float]:
return tuple(map(lambda var1, var2: var1 + var2, xyz_tuple1, xyz_tuple2))
def scalar_multiplication_xyz(scalar: float, xyz_tuple: Union[Position, Dimensions, tuple[float, float, float]]) -> tuple[float, float, float]:
return tuple(scalar * var for var in xyz_tuple)
def element_wise_multiplication_xyz(xyz_tuple1: Union[Position, Dimensions, tuple[float, float, float]], xyz_tuple2: Union[Position, Dimensions, tuple[float, float, float]]) -> tuple[float, float, float]:
return tuple(map(lambda var1, var2: var1 * var2, xyz_tuple1, xyz_tuple2))
def create_joint(upstream_center: Position, parent_part_size: Dimensions, joint_attachment_element: CubeElement, joint_type:str, axis: list[Union[float, int]], parent_part_id: int, current_part_id: int) -> str:
joint_name = "" + str(parent_part_id) + "_" + str(current_part_id) + ""
joint_attachment_point = add_xyz(
upstream_center,
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
parent_part_size),
joint_attachment_element.value)
)
psz.send_joint(
name=joint_name,
parent=str(parent_part_id),
child=str(current_part_id),
type=joint_type,
position=list(joint_attachment_point),
axis=axis.value)
return joint_name
class BodyPart():
def __init__(self):
self.properties = {
'sensor' : True,
'joint' : 'revolute',
'unchangeable' : False,
'brain' : False
}
def get_properties(self):
return self.properties
def create_body_part(self, upstream_position: Position, attachment_point_on_child: CubeElement, piece_id: int) -> tuple(Position, Dimensions):
return self.create_body_part(upstream_position, attachment_point_on_child, piece_id)
class RandomSizeBodyPiece(BodyPart):
def __init__(self):
super().__init__()
self.properties['sensor'] = False
def create_body_part(self, attachment_point_on_child: CubeElement, piece_id: int, upstream_position: Position = Position(0, 0, 0)) -> tuple(Position, Dimensions):
size: Dimensions = Dimensions(*create_random_xyz(Dimensions(0.2, 0.2, 0.1), Dimensions(1, 1, 0.6)))
center: Position = add_xyz(
upstream_position, #ONLY USED IN INITIAL PIECE
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
size),
attachment_point_on_child.value))
psz.send_link(
name=str(piece_id),
pos_xyz=list(center),
shape="box",
size_string="{} {} {}".format(*list(size)),
color_name='Blue',
color=[0.0, 0.0, 1.0, 1.0])
return center, size
class RandomSizeSensorPiece(BodyPart):
def __init__(self):
super().__init__()
self.properties['sensor'] = True
def create_body_part(self, attachment_point_on_child: CubeElement, piece_id: int, upstream_position: Position = Position(0, 0, 0)) -> tuple(Position, Dimensions):
size: Dimensions = Dimensions(*create_random_xyz(Dimensions(0.2, 0.2, 0.1), Dimensions(1, 1, 0.6)))
center: Position = add_xyz(
upstream_position, #ONLY USED IN INITIAL PIECE
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
size),
attachment_point_on_child.value))
psz.send_link(
name=str(piece_id),
pos_xyz=list(center),
shape="box",
size_string="{} {} {}".format(*list(size)),
color_name='Green',
color=[0.0, 1.0, 0.0, 1.0])
return center, size
class FixedSizeBodyPiece(BodyPart):
def __init__(self, size: float=1):
super().__init__()
self.properties['sensor'] = False
self.size = size
def create_body_part(self, attachment_point_on_child: CubeElement, piece_id: int, upstream_position: Position = Position(0, 0, 0)) -> tuple(Position, Dimensions):
size: Dimensions = Dimensions(*create_random_xyz(Dimensions(0.2, 0.2, 0.1), Dimensions(1, 1, 0.6)))
center: Position = add_xyz(
upstream_position, #ONLY USED IN INITIAL PIECE
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
size),
attachment_point_on_child.value))
psz.send_link(
name=str(piece_id),
pos_xyz=list(center),
shape="box",
size_string="{} {} {}".format(*list(size)),
color_name='Blue',
color=[0.0, 0.0, 1.0, 1.0])
return center, size
class FixedSizeSensorPiece(BodyPart):
def __init__(self, size: float=1):
super().__init__()
self.properties['sensor'] = True
self.size = size
def create_body_part(self, attachment_point_on_child: CubeElement, piece_id: int, upstream_position: Position = Position(0, 0, 0)) -> tuple(Position, Dimensions):
size: Dimensions = Dimensions(*create_random_xyz(Dimensions(0.2, 0.2, 0.1), Dimensions(1, 1, 0.6)))
center: Position = add_xyz(
upstream_position, #ONLY USED IN INITIAL PIECE
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
size),
attachment_point_on_child.value))
psz.send_link(
name=str(piece_id),
pos_xyz=list(center),
shape="box",
size_string="{} {} {}".format(*list(size)),
color_name='Green',
color=[0.0, 1.0, 0.0, 1.0])
return center, size
class FixedSizeUnmovableBodyPiece(BodyPart):
def __init__(self, size: float=1):
super().__init__()
self.properties['sensor'] = False
self.properties['joint'] = 'fixed'
self.size = size
def create_body_part(self, attachment_point_on_child: CubeElement, piece_id: int, upstream_position: Position = Position(0, 0, 0)) -> tuple(Position, Dimensions):
size: Dimensions = Dimensions(*create_random_xyz(Dimensions(0.2, 0.2, 0.1), Dimensions(1, 1, 0.6)))
center: Position = add_xyz(
upstream_position, #ONLY USED IN INITIAL PIECE
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
size),
attachment_point_on_child.value))
psz.send_link(
name=str(piece_id),
pos_xyz=list(center),
shape="box",
size_string="{} {} {}".format(*list(size)),
color_name='Purple',
color=[0.5, 0.0, 0.5, 1.0])
return center, size
class FixedSizeUnmovableSensorPiece(BodyPart):
def __init__(self, size: float=1):
super().__init__()
self.properties['sensor'] = True
self.properties['joint'] = 'fixed'
self.size = size
def create_body_part(self, attachment_point_on_child: CubeElement, piece_id: int, upstream_position: Position = Position(0, 0, 0)) -> tuple(Position, Dimensions):
size: Dimensions = Dimensions(*create_random_xyz(Dimensions(0.2, 0.2, 0.1), Dimensions(1, 1, 0.6)))
center: Position = add_xyz(
upstream_position, #ONLY USED IN INITIAL PIECE
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
size),
attachment_point_on_child.value))
psz.send_link(
name=str(piece_id),
pos_xyz=list(center),
shape="box",
size_string="{} {} {}".format(*list(size)),
color_name='Orange',
color=[1.0, 0.35, 0.2, 1.0])
return center, size
class FixedSizedUnchangeableBrain(BodyPart):
def __init__(self, size: float=1):
super().__init__()
self.properties['sensor'] = True
self.properties['unchangeable'] = True
self.properties['brain'] = True
self.size = size
def create_body_part(self, attachment_point_on_child: CubeElement, piece_id: int, upstream_position: Position = Position(0, 0, 0)) -> tuple(Position, Dimensions):
size: Dimensions = Dimensions(*create_random_xyz(Dimensions(0.2, 0.2, 0.1), Dimensions(1, 1, 0.6)))
center: Position = add_xyz(
upstream_position, #ONLY USED IN INITIAL PIECE
element_wise_multiplication_xyz(
scalar_multiplication_xyz(
0.5,
size),
attachment_point_on_child.value))
psz.send_link(
name=str(piece_id),
pos_xyz=list(center),
shape="box",
size_string="{} {} {}".format(*list(size)),
color_name='Black',
color=[0.0, 0.0, 0.0, 1.0])
return center, size
class NeuronWeightMatrix():
def __init__(self, sensor_neurons: list[str], motor_neurons: list[str], previous_weights: Union[NeuronWeightMatrix, None] = None) -> None:
self.shape = (len(sensor_neurons), len(motor_neurons))
self.sensors = { sensor_neurons[x]: x for x in range(len(sensor_neurons)) }
self.motors = { motor_neurons[x]: x for x in range(len(motor_neurons)) }
self.matrix = np.full(self.shape, np.nan)
if previous_weights is not None:
generator_sensors = (sensor for sensor in self.sensors if sensor in previous_weights.get_sensors())
generator_motors = (motor for motor in self.motors if motor in previous_weights.get_motors())
for sensor in generator_sensors:
for motor in generator_motors:
self_sensor_index = self.sensors[sensor]
self_motor_index = self.motors[motor]
previous_sensor_index = previous_weights.get_sensors()[sensor]
previous_motor_index = previous_weights.get_motors()[motor]
self.matrix[self_sensor_index, self_motor_index] = previous_weights.get_weights[previous_sensor_index, previous_motor_index]
self.matrix[np.isnan(self.matrix)] = np.random.randn(len(self.matrix[np.isnan(self.matrix)]))
def get_sensors(self):
return self.sensors
def get_motors(self):
return self.motors
def get_weights(self):
return self.matrix
def set_weights(self, weights):
self.matrix = weights
def get(self, sensor, motor):
sensor_index = self.sensors[sensor]
motor_index = self.motors[motor]
return self.matrix[sensor_index, motor_index]