Skip to content

Commit bbad593

Browse files
committed
Implem
1 parent e44dad7 commit bbad593

16 files changed

+519
-45
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='simple_playgrounds',
5-
version='0.9.8',
5+
version='0.9.9',
66
description='Simulator for AGI and RL',
77
author='Michael Garcia Ortiz',
88
author_email='[email protected]',

simple_playgrounds/entities/agents/agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def velocity(self):
122122

123123
@velocity.setter
124124
def velocity(self, velocity):
125-
for part in self.base_platform:
125+
for part in self.parts:
126126
part.velocity = velocity
127127

128128
@property

simple_playgrounds/entities/agents/basic_agents.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,31 @@
1818

1919
class BaseAgent(Agent):
2020
"""
21-
Base Agent with a single HolonomicPlatform as a Base
21+
Base Agent with a single HolonomicPlatform as a Base.
22+
No interactive actions.
2223
"""
2324
def __init__(self, initial_position=None, **kwargs):
2425

2526
base_agent = HolonomicPlatform(name='base', radius=10,
26-
can_eat=True, can_grasp=True, can_activate=True, can_absorb=True)
27+
can_eat=False, can_grasp=False, can_activate=False, can_absorb=False)
2728

2829
super().__init__(initial_position=initial_position, base_platform=base_agent, **kwargs)
2930

3031

32+
class BaseInteractiveAgent(Agent):
33+
"""
34+
Base Agent with a single HolonomicPlatform as a Base.
35+
With Interactive actions.
36+
"""
37+
38+
def __init__(self, initial_position=None, **kwargs):
39+
base_agent = HolonomicPlatform(name='base', radius=10,
40+
can_eat=True, can_grasp=True, can_activate=True, can_absorb=True)
41+
42+
super().__init__(initial_position=initial_position, base_platform=base_agent, **kwargs)
43+
3144
@property
3245
def key_mapping(self):
33-
3446
keys = []
3547

3648
keys.append(Keymap(self.base_platform.name, ActionTypes.GRASP, K_g, KeyTypes.PRESS_HOLD, 1))

simple_playgrounds/entities/field.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Module for Field
33
"""
44
import random
5-
from simple_playgrounds.utils.definitions import SceneElementTypes
5+
from simple_playgrounds.utils import SceneElementTypes
66

77
#pylint: disable=too-many-instance-attributes
88
#pylint: disable=too-many-arguments
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from simple_playgrounds.entities.scene_elements.collection.basic import *
2-
from simple_playgrounds.entities.scene_elements.collection.contact import *
3-
from simple_playgrounds.entities.scene_elements.collection.passive import *
4-
from simple_playgrounds.entities.scene_elements.collection.interactive import *
5-
from simple_playgrounds.entities.scene_elements.collection.proximity import *
6-
from simple_playgrounds.entities.scene_elements.collection.edible import *
7-
from simple_playgrounds.entities.scene_elements.collection.gem import *
8-
from simple_playgrounds.entities.scene_elements.collection import *
1+
from simple_playgrounds.entities.scene_elements.collection.basic import Basic, Door
2+
from simple_playgrounds.entities.scene_elements.collection.conditioning import ConditionedColorChanging, ColorChanging
3+
from simple_playgrounds.entities.scene_elements.collection.contact \
4+
import Candy, Poison, VisibleDeathTrap, VisibleEndGoal, PushButton
5+
from simple_playgrounds.entities.scene_elements.collection.passive import ToxicZone, HealingZone, DeathZone, GoalZone
6+
from simple_playgrounds.entities.scene_elements.collection.interactive \
7+
import Lever, Lock, OpenCloseSwitch, VendingMachine,TimerSwitch, Dispenser, Chest
8+
from simple_playgrounds.entities.scene_elements.collection.proximity import Fireball, Fairy
9+
from simple_playgrounds.entities.scene_elements.collection.edible import Apple, RottenApple
10+
from simple_playgrounds.entities.scene_elements.collection.gem import Key, Coin

simple_playgrounds/entities/scene_elements/collection/basic.py

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self, initial_position, **kwargs):
4848

4949
self.opened = False
5050

51+
5152
@property
5253
def opened(self):
5354
""" State of the door. True if door is opened."""

simple_playgrounds/entities/scene_elements/collection/conditioning.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Scene Elements used for conditioning experiments
33
"""
4-
from simple_playgrounds.entities.scene_elements import Lever
4+
from simple_playgrounds.entities.scene_elements.collection.interactive import Lever
55
from simple_playgrounds.entities.scene_elements.element import SceneElement
66
from simple_playgrounds.playgrounds.playground import Playground
77

simple_playgrounds/entities/scene_elements/collection/contact.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,6 @@ def activate(self):
192192
list_remove = [self, self.door]
193193
list_add = []
194194

195-
self.door.open_door()
195+
self.door.opened = True
196196

197197
return list_remove, list_add

simple_playgrounds/entities/scene_elements/collection/interactive.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
from simple_playgrounds.entities.scene_elements.element import SceneElement
77
from simple_playgrounds.utils.definitions import CollisionTypes
8-
from simple_playgrounds.utils.position_utils import PositionAreaSampler
8+
from simple_playgrounds.utils.position_utils import PositionAreaSampler
99
from simple_playgrounds.entities.agents.parts import Part
1010
from simple_playgrounds.playgrounds.playground import Playground
1111

1212
#pylint: disable=line-too-long
1313

14+
1415
class InteractiveSceneElement(SceneElement, ABC):
1516
"""Base Class dor InteractiveSceneElements"""
1617
interactive = True
@@ -19,6 +20,8 @@ def __init__(self, **kwargs):
1920
SceneElement.__init__(self, **kwargs)
2021
self.pm_interaction_shape.collision_type = CollisionTypes.INTERACTIVE
2122

23+
self.activated = False
24+
2225
@abstractmethod
2326
def activate(self, activating_entity):
2427
"""
@@ -47,6 +50,10 @@ def reward(self):
4750
def reward(self, rew):
4851
...
4952

53+
def pre_step(self):
54+
self.activated = False
55+
56+
5057

5158
class Lever(InteractiveSceneElement):
5259
"""Lever Entities provide a reward when activated."""
@@ -67,7 +74,7 @@ def __init__(self, initial_position, **kwargs):
6774
self.reward_provided = False
6875

6976
def pre_step(self):
70-
77+
super().pre_step()
7178
self.reward_provided = False
7279

7380
@property
@@ -148,7 +155,7 @@ def activate(self, _):
148155
list_remove = []
149156
list_add = []
150157

151-
if len(self.produced_entities) < self.production_limit:
158+
if len(self.produced_entities) < self.production_limit and self.activated is False:
152159

153160
if self.local_dispenser:
154161
initial_position = self.location_sampler.sample([self.position[0], self.position[1]])
@@ -161,6 +168,8 @@ def activate(self, _):
161168
self.produced_entities.append(obj)
162169
list_add = [obj]
163170

171+
self.activated = True
172+
164173
return list_remove, list_add
165174

166175
def reset(self):
@@ -256,9 +265,10 @@ def activate(self, activating_entity):
256265
list_add = []
257266
list_remove = []
258267

259-
if activating_entity in self.accepted_coins.copy():
268+
if activating_entity in self.accepted_coins.copy() and self.activated is False:
260269
list_remove = [activating_entity]
261270
self.accepted_coins.remove(activating_entity)
271+
self.activated = True
262272

263273
return list_remove, list_add
264274

@@ -282,6 +292,10 @@ def __init__(self, initial_position, door, **kwargs):
282292
Can be list [x,y,theta], AreaPositionSampler or Trajectory.
283293
door: Door opened by the switch.
284294
**kwargs: other params to configure entity. Refer to Entity class.
295+
296+
Notes:
297+
It is possible to have multiple switches for a single door.
298+
However the behavior is unstable in multiagent setting.
285299
"""
286300

287301
default_config = self._parse_configuration('interactive', 'switch')
@@ -300,13 +314,16 @@ def activate(self, _):
300314
list_add = []
301315
list_remove = []
302316

303-
if self.door.opened:
304-
self.door.opened = False
305-
list_add = [self.door]
317+
if self.activated is False:
318+
if self.door.opened:
319+
self.door.opened = False
320+
list_add = [self.door]
306321

307-
else:
308-
self.door.opened = True
309-
list_remove = [self.door]
322+
else:
323+
self.door.opened = True
324+
list_remove = [self.door]
325+
326+
self.activated = True
310327

311328
return list_remove, list_add
312329

@@ -353,12 +370,13 @@ def activate(self, activating_entity):
353370
list_remove = []
354371
list_add = []
355372

356-
if isinstance(activating_entity, Part):
373+
if isinstance(activating_entity, Part) and self.activated is False:
357374
if not self.door.opened:
358375
self.door.opened = True
359376
list_remove = [self.door]
360377

361378
self._reset_timer()
379+
self.activated = True
362380

363381
if isinstance(activating_entity, Playground):
364382
self.door.opened = False
@@ -373,13 +391,14 @@ def _reset_timer(self):
373391

374392
def pre_step(self):
375393

394+
self.activated = False
376395
if self.door.opened:
377396
self.timer -= 1
378397

379398
def reset(self):
380399

381400
self.timer = self.time_open
382-
self.door.close_door()
401+
self.door.opened = False
383402

384403

385404
class Lock(InteractiveSceneElement):

simple_playgrounds/game_engine.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def generate_sensor_image(self, agent, width_sensor=200, height_sensor=30):
318318

319319
return full_img
320320

321-
def run(self):
321+
def run(self, with_screen = False):
322322
""" Run the engine for the full duration of the game"""
323323

324324
while self.game_on:
@@ -329,3 +329,11 @@ def run(self):
329329

330330
self.step(actions)
331331
self.update_observations()
332+
333+
if with_screen:
334+
self.display_full_scene()
335+
336+
# for agent in self.agents:
337+
# print(agent.position, agent.base_platform.pm_body.velocity, agent.base_platform.pm_body.kinetic_energy)
338+
# assert 0 < agent.position[0] < self.playground.size[0]
339+
# assert 0 < agent.position[1] < self.playground.size[1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .test_scene_elements import *

0 commit comments

Comments
 (0)