gaorkl / simple-playgrounds

Simulator for Reinforcement Learning and AI. 2D environments with physics and interactive entities. Agents with rich sensors and actuators.
MIT License
27 stars 10 forks source link

Pygame video system error when running SPG while it is imported as a package #74

Closed DamienLopez1 closed 2 years ago

DamienLopez1 commented 2 years ago

I have imported simple playgrounds into my environment and I am trying to run a simple scenario using a keyboard controlled agent.

The code for the scenario is shown below:

from simple_playgrounds.playground.layouts import SingleRoom

from simple_playgrounds.engine import Engine
from simple_playgrounds.element.elements.basic import Physical
from simple_playgrounds.agent.controllers import Keyboard
from simple_playgrounds.agent.agents import BaseAgent
from simple_playgrounds.common.position_utils import CoordinateSampler
from simple_playgrounds.playground import LineRooms, GridRooms
from simple_playgrounds.element.elements.activable import OpenCloseSwitch
from simple_playgrounds.element.elements.contact import VisibleEndGoal, Candy
from simple_playgrounds.element.elements.zone import RewardZone
from simple_playgrounds.common.definitions import CollisionTypes as SPGC
from simple_playgrounds.common.definitions import add_custom_collision
import matplotlib.pyplot as plt
CollisionTypes = add_custom_collision(SPGC, "WEAPON_RANGE")

def plt_image(img):
    plt.axis('off')
    plt.imshow(img)
    plt.show()

my_playground = GridRooms(size=(400, 400), room_layout=(3,3), random_doorstep_position=True, doorstep_size = 60,wall_type = "light")

# we use the option screen=True to use a keyboard controlled agent later on.
engine = Engine(time_limit=10000, playground=my_playground, debug=False)

#topdown_img = engine.generate_playground_image(plt_mode=True)

room_left = my_playground.grid_rooms[0][1]

doorstep = room_left.doorstep_right

door = doorstep.generate_door()

my_playground.add_element(door)

switch = OpenCloseSwitch(door=door)
position_switch = room_left.get_random_position_on_wall(wall_location='right', element=switch)
my_playground.add_element(switch, position_switch)

enemy_reward_zone = RewardZone(reward=1,limit =10,config_key = None,physical_shape = "square", texture = [123, 234, 0],size = [100,100],radius = 100)
my_playground.add_element(enemy_reward_zone, ((200, 200), 0))

candy = Candy()
my_playground.add_element(candy, ((70,70),0))

pentagon_object = Physical(config_key='circle', graspable=True, mass=1)
my_playground.add_element(pentagon_object, ((170, 30), 0.5))

my_agent = BaseAgent(controller=Keyboard(), radius=10, interactive = True)
my_playground.add_agent(my_agent)

engine.terminate()
engine = Engine(time_limit=10000, playground= my_playground, debug=True)
engine.run(print_rewards = True)

engine.terminate()

When running the above from command line I get the following error:

Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/home/dlopez-dlm/Documents/Github/NDA-AUDI/scripts/testing.py", line 60, in <module>
    engine.run(print_rewards = True)
  File "/home/dlopez-dlm/.cache/pypoetry/virtualenvs/audi-faG26ffs-py3.9/lib/python3.9/site-packages/simple_playgrounds/engine.py", line 495, in run
    actions[agent] = agent.controller.generate_actions()
  File "/home/dlopez-dlm/.cache/pypoetry/virtualenvs/audi-faG26ffs-py3.9/lib/python3.9/site-packages/simple_playgrounds/agent/controllers.py", line 212, in generate_actions
    all_key_pressed = pygame.key.get_pressed()
pygame.error: video system not initialized

Using similar code while just cloning the simple playgrounds repository works fine.

vakker commented 2 years ago

You have 2 engine instances in the same script, referencing a single playground instance, which causes the issue.

Remove the 2nd engine = Engine(time_limit=10000, playground= my_playground, debug=True) and move the engine.run before the engine.terminate and it'll run.

DamienLopez1 commented 2 years ago

Thanks that worked! Any reason that the window is extremely tiny? the code does not seem to follow the size parameter even when full_surface=True

engine = Engine(time_limit=10000, playground=my_playground, debug=True,full_surface=True)

vakker commented 2 years ago

The window should be the size of the playground in pixels, i.e. size=(400, 400) -> 400x400 pixels. If I change that to size=(800, 800) then it does change the window size accordingly on my end.

DamienLopez1 commented 2 years ago
my_playground = GridRooms(size=(400, 400), room_layout=(3,3), random_doorstep_position=True, doorstep_size = 60,wall_type = "light")

# we use the option screen=True to use a keyboard controlled agent later on.
engine = Engine(time_limit=10000, playground=my_playground, debug=True,full_surface=True)

#topdown_img = engine.generate_playground_image()

room_left = my_playground.grid_rooms[0][1]

doorstep = room_left.doorstep_right

door = doorstep.generate_door()

my_playground.add_element(door)

switch = OpenCloseSwitch(door=door)
position_switch = room_left.get_random_position_on_wall(wall_location='right', element=switch)
my_playground.add_element(switch, position_switch)

enemy_reward_zone = RewardZone(reward=1,limit =10,config_key = None,physical_shape = "square", texture = [123, 234, 0],size = [100,100],radius = 100)
my_playground.add_element(enemy_reward_zone, ((200, 200), 0))

candy = Candy()
my_playground.add_element(candy, ((70,70),0))

pentagon_object = Physical(config_key='circle', graspable=True, mass=1)
my_playground.add_element(pentagon_object, ((170, 30), 0.5))

my_agent = BaseAgent(controller=Keyboard(), radius=10, interactive = True)
my_playground.add_agent(my_agent)

engine.run(print_rewards = True)
#plt_image(topdown_img)
engine.terminate()

screen

It seems to be stuck at this tiny size

vakker commented 2 years ago

Right, that's because the camera follows the agent and you don't see the whole playground in the game window. This is to avoid rendering large playgrounds and running out of memory. You can get the whole PG image with: engine.generate_playground_image()

DamienLopez1 commented 2 years ago

Thanks. Appreciate the help and feedback.