Skip to content

Commit

Permalink
Merge pull request #123 from microcosmAI/refactor/adapt-size-setter-m…
Browse files Browse the repository at this point in the history
…ujoco-object

Refactor/adapt size setter mujoco object
  • Loading branch information
PeterKeffer authored May 30, 2023
2 parents 3bb096c + 4477273 commit 4f99a31
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
5 changes: 1 addition & 4 deletions peters_algorithm/base/asset_parsing/mujoco_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ def size(self) -> float:
@size.setter
def size(self, size: float):
"""Set size of object"""
mjcf_pos = self.position
mjcf_pos[2] = size
self.position = mjcf_pos
self._mjcf_obj.find("body", self._name.lower()).geom[0].size = [size]
self._mjcf_obj.find("body", self._name.lower()).geom[0].size = size

@property
def tags(self) -> list[str]:
Expand Down
22 changes: 11 additions & 11 deletions peters_algorithm/base/asset_placement/random_placer.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ def add(
else:
mujoco_objects_rule_blueprint.size = sizes[i]

# Old position is used to keep the z position
old_position = mujoco_object_blueprint.position

# We only want to sample x and y, so we keep the old z position
z_position = old_position[2]
# Save size of object for setting the z coordinate
new_z_position = mujoco_objects_rule_blueprint.size[0]

# Sample a new position
mujoco_objects_rule_blueprint.position = [*self.distribution(), z_position]
mujoco_objects_rule_blueprint.position = [
*self.distribution(),
new_z_position,
]

count = 0
# Ask every validator for approval until all approve or MAX_TRIES is reached, then throw error
Expand All @@ -199,7 +199,7 @@ def add(
# If placement is not possible, sample a new position
mujoco_objects_rule_blueprint.position = [
*self.distribution(),
old_position[2],
new_z_position,
]

# Copy the blueprint to avoid changing the original
Expand All @@ -214,8 +214,8 @@ def add(
if sizes is not None:
# Exchange parameters i.e. Reset rule blueprint and modify the mujoco_object copy
old_size = mujoco_object.size
mujoco_object.size = mujoco_objects_rule_blueprint.size[0]
mujoco_objects_rule_blueprint.size = old_size[0]
mujoco_object.size = mujoco_objects_rule_blueprint.size
mujoco_objects_rule_blueprint.size = old_size

mujoco_object.position = mujoco_objects_rule_blueprint.position

Expand Down Expand Up @@ -300,7 +300,7 @@ def _get_random_sizes(sizes_range: tuple[float, float]):
random_size = round(
np.random.random() * 2, 2
) # sets a size between 0 and 2
sizes.append(random_size)
sizes.append(random_size)
sizes.append([random_size])
sizes.append([random_size])

return sizes

0 comments on commit 4f99a31

Please sign in to comment.