class TestEnvironment(unittest.TestCase): def setUp(self): config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'basic_env.yaml') self.env = DIYGym(config_file) def test_load_environment(self): self.assertTrue('plane' in self.env.models) self.assertTrue('red_marble' in self.env.models) self.assertTrue('green_marble' in self.env.models) self.assertTrue('blue_marble' in self.env.models) def test_spaces(self): self.assertTrue('force' in self.env.action_space['blue_marble'].spaces) self.assertTrue( 'camera' in self.env.observation_space['basic_env'].spaces) self.assertTrue( 'pose' in self.env.observation_space['green_marble'].spaces) def test_episode(self): observation = self.env.reset() initial_position = observation['green_marble']['pose']['position'] # try to run the blue marble into the other two for _ in range(500): observation, _, _, _ = self.env.step( {'blue_marble': { 'force': [0, -100, 0] }}) final_position = observation['green_marble']['pose']['position'] # check that the green marble has moved self.assertNotAlmostEqual(np.linalg.norm(initial_position), np.linalg.norm(final_position), places=0) observation = self.env.reset() reset_position = observation['green_marble']['pose']['position'] # check that the green marble has been reset back to its starting position self.assertAlmostEqual(np.linalg.norm(initial_position), np.linalg.norm(reset_position), places=1)
#!/usr/bin/env python import numpy as np import os from diy_gym import DIYGym if __name__ == '__main__': config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ur_high_5.yaml') env = DIYGym(config_file) observation = env.reset() while True: action = env.action_space.sample() action['ur5_l']['controller']['linear'][:] = observation['ur_high_5']['distance_to_target']['position'] * 0. action['ur5_r']['controller']['linear'][:] = -observation['ur_high_5']['distance_to_target']['position'] * 0. action['ur5_l']['controller']['rotation'][:] = 0 action['ur5_r']['controller']['rotation'][:] = 0 observation, reward, terminal, info = env.step(action) if terminal: print('slap!') observation = env.reset()
def setUp(self): config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'basic_env.yaml') self.env = DIYGym(config_file)