Skip to content

Commit

Permalink
Merge pull request #84 from gaorkl/feat/cleanup
Browse files Browse the repository at this point in the history
All tests passing
  • Loading branch information
gaorkl authored Aug 22, 2024
2 parents c0395df + 938247b commit e2fbeb3
Show file tree
Hide file tree
Showing 365 changed files with 564 additions and 1,592 deletions.
1 change: 0 additions & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
sudo apt-get install xvfb
sudo apt-get install freeglut3-dev
pip install flake8 pytest pytest-cov
pip install -r requirements.txt
pip install -e .
- name: Lint with flake8
run: |
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 mgarciaortiz
Copyright (c) 2021 Michael Garcia Ortiz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 4 additions & 5 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Include requirements:
include requirements.txt
# Include Assets:
recursive-include src/spg *.txt *.md *.url *.png *.jpg *.jpeg

# Include configs:
recursive-include src/spg/resources *.txt *.md *.url *.png *.jpg *.jpeg
recursive-include src/spg/agent/sensor/shaders *.glsl
# Include Shaders:
recursive-include src/spg *.glsl

12 changes: 0 additions & 12 deletions mypy.ini

This file was deleted.

5 changes: 0 additions & 5 deletions pytest.ini

This file was deleted.

12 changes: 0 additions & 12 deletions requirements.txt

This file was deleted.

4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ disable=missing-docstring,
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=numpy.*,torch.*
env =
TESTING=1
log_cli = 1
log_cli_level = INFO
36 changes: 33 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,34 @@

from setuptools import find_packages, setup

with open("requirements.txt", "r") as f:
requirements = f.read().splitlines()
install_requires = [
"numpy",
"pyyaml",
"pymunk>=6.0.0",
"scikit-image",
"pillow",
"pytest",
"matplotlib",
"arcade",
"tqdm",
"gymnasium",
]

test_requires = [
"flake8",
"pytest",
"pytest-cov",
"pytest-env",
"pytest-sugar",
"pylint",
]


dev_requires = [
"pre-commit",
"ipdb",
]


# read the contents of the README file

Expand All @@ -21,7 +47,11 @@
packages=find_packages(where="."),
package_dir={"": "."},
include_package_data=True,
install_requires=requirements,
install_requires=install_requires,
extras_require={
"test": test_requires,
"dev": dev_requires,
},
long_description=long_description,
long_description_content_type="text/markdown",
)
97 changes: 97 additions & 0 deletions spg/components/agents/attached.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import math

import pymunk
from gymnasium import spaces

from spg.components.agents.constants import ANGULAR_VELOCITY, ARM_MASS, HEAD_MASS
from spg.core.entity import Entity
from spg.core.entity.body import AttachedDynamicMixin
from spg.core.entity.interaction.action import ActionMixin


class AttachedPart(Entity, AttachedDynamicMixin, ActionMixin):
def __init__(self, rotation_range=math.pi / 3, **kwargs):
self.rotation_range = rotation_range
super().__init__(**kwargs)

def _get_joint(self):
joint = pymunk.PivotJoint(
self.anchor.pm_body,
self.pm_body,
self.anchor.attachment_points[self][0],
self.attachment_point,
)
joint.collide_bodies = False
return joint

def _get_limit(self):

relative_angle = self.pm_body.angle - self.anchor.pm_body.angle

limit = pymunk.RotaryLimitJoint(
self.anchor.pm_body,
self.pm_body,
relative_angle - self.rotation_range / 2,
relative_angle + self.rotation_range / 2,
)
limit.collide_bodies = False
return limit

def _get_motor(self):
motor = pymunk.SimpleMotor(self.anchor.pm_body, self.pm_body, 0)
motor.max_force = 5
motor.collide_bodies = False
return motor

@property
def _action_space(self):
return spaces.Box(-1, 1, shape=(1,))

def apply_action(self, action):
self.motor.rate = action * ANGULAR_VELOCITY


class Arm(AttachedPart):
def __init__(
self,
mass=ARM_MASS,
rotation_range=math.pi / 4,
filename=":spg:agent/arm.png",
sprite_front_is_up=True,
**kwargs
):

super().__init__(
mass=mass,
rotation_range=rotation_range,
sprite_front_is_up=sprite_front_is_up,
filename=filename,
**kwargs,
)

@property
def attachment_point(self):
return -self.radius, 0.0


class Head(AttachedPart):
def __init__(
self,
mass=HEAD_MASS,
filename=":spg:agent/head.png",
sprite_front_is_up=True,
rotation_range=math.pi / 2,
**kwargs
):

super().__init__(
mass=mass,
rotation_range=rotation_range,
sprite_front_is_up=sprite_front_is_up,
filename=filename,
**kwargs,
)

@property
def attachment_point(self):
return 0, 0
85 changes: 85 additions & 0 deletions spg/components/agents/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from abc import ABC

import pymunk
from gymnasium import spaces

from spg.core.entity import Agent
from spg.core.entity.body import BaseDynamicMixin, BaseStaticMixin

from .constants import ANGULAR_VELOCITY, BASE_MASS, LINEAR_FORCE


class DynamicAgent(Agent, BaseDynamicMixin, ABC):
def __init__(self, **kwargs):
super().__init__(
sprite_front_is_up=True,
filename=":spg:agent/base.png",
shape_approximation="decomposition",
mass=BASE_MASS,
**kwargs,
)


class HolonomicAgent(DynamicAgent, ABC):
def _apply_action(self, action):

forward_force, lateral_force, angular_velocity = action

self.pm_body.apply_force_at_local_point(
pymunk.Vec2d(forward_force, lateral_force) * LINEAR_FORCE, (0, 0)
)

self.pm_body.angular_velocity = angular_velocity * ANGULAR_VELOCITY


class HolonomicContinuousAgent(HolonomicAgent, ABC):
@property
def _action_space(self):
return spaces.Box(low=-1, high=1, shape=(3,))


class HolonomicDiscreteAgent(HolonomicAgent, ABC):
@property
def _action_space(self):
return spaces.MultiDiscrete([3, 3, 3], start=[-1, -1, -1])


class ForwardAgent(DynamicAgent, ABC):
def _apply_action(self, action):

forward_force, angular_velocity = action

self.pm_body.apply_force_at_local_point(
pymunk.Vec2d(forward_force, 0) * LINEAR_FORCE, (0, 0)
)

self.pm_body.angular_velocity = angular_velocity * ANGULAR_VELOCITY


class ForwardContinuousAgent(ForwardAgent, ABC):
@property
def _action_space(self):
return spaces.Box(low=-1, high=1, shape=(2,))


class ForwardDiscreteAgent(ForwardAgent, ABC):
@property
def _action_space(self):
return spaces.MultiDiscrete([3, 3], start=[-1, -1])


class StaticAgent(Agent, BaseStaticMixin, ABC):
def __init__(self, **kwargs):
super().__init__(
sprite_front_is_up=True,
filename=":spg:agent/base.png",
shape_approximation="decomposition",
**kwargs,
)

@property
def _action_space(self):
return None

def _apply_action(self, action):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@


DEFAULT_INTERACTION_RANGE = 5

BASE_MASS = 10
ARM_MASS = 5
HEAD_MASS = 5
57 changes: 57 additions & 0 deletions spg/components/agents/interaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from gymnasium import spaces

from spg.core.entity import Entity
from spg.core.entity.body import AttachedStaticMixin
from spg.core.entity.interaction.activable import ActivableMixin
from spg.core.entity.interaction.grasper import GrasperHold
from spg.core.entity.sprite import get_texture_from_geometry


class Trigger(Entity, ActivableMixin, AttachedStaticMixin):
def __init__(self, **kwargs):

texture, _ = get_texture_from_geometry(
geometry="circle", radius=20, color=(255, 0, 0)
)

super().__init__(ghost=True, texture=texture, **kwargs)

self.triggered = False

def activate(self, entity, **kwargs):
self.activated = True

def pre_step(self):
self.triggered = False
super().pre_step()

@property
def attachment_point(self):
return 0, 0

@property
def action_space(self):
return spaces.Discrete(2)

def apply_action(self, action):
self.triggered = bool(action)


class GrasperHand(Entity, AttachedStaticMixin, GrasperHold):
def __init__(self, grasper_radius, **kwargs):

texture, _ = get_texture_from_geometry(
geometry="circle", radius=grasper_radius, color=(255, 0, 0)
)

super().__init__(
texture=texture,
ghost=True,
**kwargs,
)

GrasperHold.__init__(self)

@property
def attachment_point(self):
return 0, 0
4 changes: 0 additions & 4 deletions spg/components/agents/part/__init__.py

This file was deleted.

Loading

0 comments on commit e2fbeb3

Please sign in to comment.