def testMoveSprites(self):
    """Take a series of actions and repeatedly check sprite motions."""
    action_space = action_spaces.DragAndDrop(scale=0.5)
    sprites = [sprite.Sprite(x=0.55, y=0.5), sprite.Sprite(x=0.5, y=0.5)]

    # Move second (top) sprite
    action_space.step(
        np.array([0.52, 0.52, 0.52, 0.5]), sprites, keep_in_frame=False)
    self.assertTrue(np.allclose(sprites[0].position, [0.55, 0.5], atol=1e-5))
    self.assertTrue(np.allclose(sprites[1].position, [0.5, 0.49], atol=1e-5))

    # Move first (bottom) sprite
    action_space.step(
        np.array([0.58, 0.5, 0.98, 0.9]), sprites, keep_in_frame=False)
    self.assertTrue(np.allclose(sprites[0].position, [0.75, 0.7], atol=1e-5))
    self.assertTrue(np.allclose(sprites[1].position, [0.5, 0.49], atol=1e-5))

    # Move neither sprite
    action_space.step(
        np.array([0.58, 0.5, 0.9, 0.9]), sprites, keep_in_frame=False)
    self.assertTrue(np.allclose(sprites[0].position, [0.75, 0.7], atol=1e-5))
    self.assertTrue(np.allclose(sprites[1].position, [0.5, 0.49], atol=1e-5))

    # Move second (top) sprite
    action_space.step(
        np.array([0.5, 0.5, 0.2, 0.5]), sprites, keep_in_frame=False)
    self.assertTrue(np.allclose(sprites[0].position, [0.75, 0.7], atol=1e-5))
    self.assertTrue(np.allclose(sprites[1].position, [0.35, 0.49], atol=1e-5))

    # Move first (bottom) sprite
    action_space.step(
        np.array([0.78, 0.74, 0.98, 0.94]), sprites, keep_in_frame=False)
    self.assertTrue(np.allclose(sprites[0].position, [0.85, 0.8], atol=1e-5))
    self.assertTrue(np.allclose(sprites[1].position, [0.35, 0.49], atol=1e-5))

    # Move first (bottom) sprite
    action_space.step(
        np.array([0.82, 0.8, 1.3, 1.0]), sprites, keep_in_frame=True)
    self.assertTrue(np.allclose(sprites[0].position, [1., 0.9], atol=1e-5))
    self.assertTrue(np.allclose(sprites[1].position, [0.35, 0.49], atol=1e-5))

    # Move first (bottom) sprite
    action_space.step(
        np.array([0.99, 0.9, 1.19, 1.3]), sprites, keep_in_frame=False)
    self.assertTrue(np.allclose(sprites[0].position, [1.1, 1.1], atol=1e-5))
    self.assertTrue(np.allclose(sprites[1].position, [0.35, 0.49], atol=1e-5))
Esempio n. 2
0
def setup_run_ui(env_config, render_size, task_hsv_colors, anti_aliasing):
    """Start a Demo UI given an env_config."""
    if isinstance(env_config['action_space'], action_spaces.SelectMove):
        # DragAndDrop is a bit easier to demo than the SelectMove action space
        #env_config['action_space'] = action_spaces.DragAndDrop(scale=0.5, noise_scale=np.array([0,0,0.025,0.025]), proportional_motion_noise=0.35)
        env_config['action_space'] = action_spaces.DragAndDrop(
            scale=0.5,
            noise_scale=0.02,
            proportional_motion_noise=0.35,
            filter_distribs=env_config['metadata']['filter_distribs'])
        agent = HumanDragAndDropAgent(env_config['action_space'])
    elif isinstance(env_config['action_space'], action_spaces.Embodied):
        agent = HumanEmbodiedAgent(env_config['action_space'])
    else:
        raise ValueError(
            'Demo is not configured to run with action space {}.'.format(
                env_config['action_space']))
    env_config['renderers'] = {
        'image':
        renderers.PILRenderer(image_size=(render_size, render_size),
                              color_to_rgb=renderers.color_maps.hsv_to_rgb
                              if task_hsv_colors else None,
                              anti_aliasing=anti_aliasing),
        'success':
        renderers.Success()
    }
    env = environment.Environment(**env_config)
    ui = MatplotlibUI()
    agent.register_callbacks(ui)

    # Start RL loop
    timestep = env.reset()
    ui.update(timestep, action=None)

    while True:
        action = agent.step(timestep)
        timestep = env.step(action)
        if isinstance(env_config['action_space'], action_spaces.DragAndDrop):
            ui.update(timestep, action)
        else:
            ui.update(timestep, None)
Esempio n. 3
0
def main(_):
    config = importlib.import_module(FLAGS.config)
    config = config.get_config(FLAGS.mode)
    if isinstance(config['action_space'], action_spaces.SelectMove):
        # DragAndDrop is a bit easier to demo than the SelectMove action space
        config['action_space'] = action_spaces.DragAndDrop(scale=0.5)
        agent = HumanDragAndDropAgent(config['action_space'])
    elif isinstance(config['action_space'], action_spaces.Embodied):
        agent = HumanEmbodiedAgent(config['action_space'])
    else:
        raise ValueError(
            'Demo is not configured to run with action space {}.'.format(
                config['action_space']))
    config['renderers'] = {
        'image':
        renderers.PILRenderer(image_size=(FLAGS.render_size,
                                          FLAGS.render_size),
                              color_to_rgb=color_maps.hsv_to_rgb
                              if FLAGS.task_hsv_colors else None,
                              anti_aliasing=FLAGS.anti_aliasing),
        'success':
        renderers.Success()
    }
    env = environment.Environment(**config)
    demo = DemoUI()

    for event_name, callback in agent.callbacks().items():
        demo.register_callback(event_name, callback)

    timestep = env.reset()
    demo.update(timestep, action=None)

    while True:
        action = agent.step(timestep)
        timestep = env.step(action)
        if isinstance(config['action_space'], action_spaces.DragAndDrop):
            demo.update(timestep, action)
        else:
            demo.update(timestep, None)
Esempio n. 4
0
def setup_run_ui(env_config, render_size, task_hsv_colors, anti_aliasing):
  """Start a Demo UI given an env_config."""
  if isinstance(env_config['action_space'], action_spaces.SelectMove):
    # DragAndDrop is a bit easier to demo than the SelectMove action space
    env_config['action_space'] = action_spaces.DragAndDrop(scale=0.5)
    agent = HumanDragAndDropAgent(env_config['action_space'])
  elif isinstance(env_config['action_space'], action_spaces.Embodied):
    agent = HumanEmbodiedAgent(env_config['action_space'])
  else:
    raise ValueError(
        'Demo is not configured to run with action space {}.'.format(
            env_config['action_space']))
  env_config['renderers'] = {
      'image':
          renderers.PILRenderer(
              image_size=(render_size, render_size),
              color_to_rgb=renderers.color_maps.hsv_to_rgb
              if task_hsv_colors else None,
              anti_aliasing=anti_aliasing),
      'success':
          renderers.Success()
  }
  env = environment.Environment(**env_config)
  demo = DemoUI()

  for event_name, callback in agent.callbacks().items():
    demo.register_callback(event_name, callback)

  # Start RL loop
  timestep = env.reset()
  demo.update(timestep, action=None)

  while True:
    action = agent.step(timestep)
    timestep = env.step(action)
    if isinstance(env_config['action_space'], action_spaces.DragAndDrop):
      demo.update(timestep, action)
    else:
      demo.update(timestep, None)
 def testGetMotion(self, scale, action, true_motion):
   action_space = action_spaces.DragAndDrop(scale=scale)
   motion = action_space.get_motion(action)
   self.assertTrue(np.allclose(motion, true_motion, atol=1e-4))