def generate_test_set(levels: typing.List[int], samples_per_level: int,
                      logfile_tmpl: str) -> typing.List[TestSample]:
    """Generate random test set for policy evaluation.

    For each difficulty level a list of samples, consisting of randomized
    initial pose and goal pose, is generated.

    Args:
        levels:  List of difficulty levels.
        samples_per_level:  How many samples are generated per level.
        logfile_tmpl:  Format string for the log file associated with this
            sample.  Needs to contain placeholders "{level}" and "{iteration}".

    Returns:
        List of ``len(levels) * samples_per_level`` test samples.
    """
    samples = []
    for level in levels:
        for i in range(samples_per_level):
            init = move_cube.sample_goal(-1)
            goal = move_cube.sample_goal(level)
            logfile = logfile_tmpl.format(level=level, iteration=i)

            samples.append(
                TestSample(level, i, init.to_json(), goal.to_json(), logfile))

    return samples
示例#2
0
def generate_eval_episodes(num_episodes):
    '''genereate code/eval_episodes/level[n] directories that contain evaluation episodes.'''
    for level in range(1, 4 + 1):
        eval_dir = os.environ['RRC_SIM_ROOT'] + '/code/eval_episodes/level{}'.format(level)
        if not os.path.isdir(eval_dir):
            os.makedirs(eval_dir)

            for i in range(num_episodes):
                # sample init_pos, goal and convert it to json
                init = sample_goal(-1)
                goal = sample_goal(level)
                with open(os.path.join(eval_dir, '{:05d}-init.json'.format(i)), 'w') as f:
                    f.write(init.to_json())
                with open(os.path.join(eval_dir, '{:05d}-goal.json'.format(i)), 'w') as f:
                    f.write(goal.to_json())
        else:
            print('directory {} already exists. skipping...'.format(eval_dir))
示例#3
0
    def reset(self):
        # reset simulation
        del self.platform

        # initialize simulation
        if self.initializer is None:
            # if no initializer is given (which will be the case during training),
            # we can initialize in any way desired. here, we initialize the cube always
            # in the center of the arena, instead of randomly, as this appears to help
            # training
            initial_robot_position = TriFingerPlatform.spaces.robot_position.default
            default_object_position = (
                TriFingerPlatform.spaces.object_position.default)
            default_object_orientation = (
                TriFingerPlatform.spaces.object_orientation.default)
            initial_object_pose = move_cube.Pose(
                position=default_object_position,
                orientation=default_object_orientation,
            )
            goal_object_pose = move_cube.sample_goal(difficulty=1)
        else:
            # if an initializer is given, i.e. during evaluation, we need to initialize
            # according to it, to make sure we remain coherent with the standard CubeEnv.
            # otherwise the trajectories produced during evaluation will be invalid.
            initial_robot_position = TriFingerPlatform.spaces.robot_position.default
            initial_object_pose = self.initializer.get_initial_state()
            goal_object_pose = self.initializer.get_goal()

        self.platform = TriFingerPlatform(
            visualization=self.visualization,
            initial_robot_position=initial_robot_position,
            initial_object_pose=initial_object_pose,
        )

        self.goal = {
            "position": goal_object_pose.position,
            "orientation": goal_object_pose.orientation,
        }
        # visualize the goal
        if self.visualization:
            self.goal_marker = visual_objects.CubeMarker(
                width=0.065,
                position=goal_object_pose.position,
                orientation=goal_object_pose.orientation,
                physicsClientId=self.platform.simfinger._pybullet_client_id,
            )

        self.info = {"difficulty": 1}

        self.step_count = 0

        return self._create_observation(0)
    def test_sample_goal_difficulty_4_no_initial_pose(self):
        for i in range(1000):
            goal = move_cube.sample_goal(difficulty=4)
            # verify the goal is valid (i.e. within the allowed ranges)
            try:
                move_cube.validate_goal(goal)
            except move_cube.InvalidGoalError as e:
                self.fail(msg="Invalid goal: {}  pose is {}, {}".format(
                    e, e.position, e.orientation), )

            # verify the goal satisfies conditions of difficulty 2
            self.assertLessEqual(goal.position[2], move_cube._max_height)
            self.assertGreaterEqual(goal.position[2], move_cube._min_height)
    def test_sample_goal_difficulty_1_no_initial_pose(self):
        for i in range(1000):
            goal = move_cube.sample_goal(difficulty=1)
            # verify the goal is valid (i.e. within the allowed ranges)
            try:
                move_cube.validate_goal(goal)
            except move_cube.InvalidGoalError as e:
                self.fail(msg="Invalid goal: {}  pose is {}, {}".format(
                    e, e.position, e.orientation), )

            # verify the goal satisfies conditions of difficulty 1
            # always on ground
            self.assertEqual(goal.position[2], move_cube._CUBE_WIDTH / 2)

            # no orientation
            np.testing.assert_array_equal(goal.orientation, [0, 0, 0, 1])
 def get_goal(self):
     """Get a random goal depending on the difficulty."""
     self.difficulty = np.random.choice(self.difficulties)
     return move_cube.sample_goal(difficulty=self.difficulty)
 def get_initial_state(self):
     """Get a random initial object pose (always on the ground)."""
     return move_cube.sample_goal(difficulty=-1)
示例#8
0
 def get_goal(self):
     """Get a random goal depending on the difficulty."""
     self.difficulty = random.randrange(1, 5)
     return move_cube.sample_goal(difficulty=self.difficulty )
示例#9
0
    def reset(self):
        # reset simulation
        del self.platform
        if hasattr(self, 'goal_marker'):
            del self.goal_marker

        # initialize simulation
        if self.initializer is None:
            # if no initializer is given (which will be the case during training),
            # we can initialize in any way desired. here, we initialize the cube always
            # in the center of the arena, instead of randomly, as this appears to help
            # training
            initial_robot_position = TriFingerPlatform.spaces.robot_position.default
            # default_object_position = (
            #     TriFingerPlatform.spaces.object_position.default
            # )
            # default_object_orientation = (
            #     TriFingerPlatform.spaces.object_orientation.default
            # )
            # initial_object_pose = move_cube.Pose(
            #     position=default_object_position,
            #     orientation=default_object_orientation,
            # )
            goal_object_pose = move_cube.sample_goal(difficulty=1)
        else:
            # if an initializer is given, i.e. during evaluation, we need to initialize
            # according to it, to make sure we remain coherent with the standard CubeEnv.
            # otherwise the trajectories produced during evaluation will be invalid.
            initial_robot_position = TriFingerPlatform.spaces.robot_position.default
            # initial_object_pose=self.initializer.get_initial_state()
            goal_object_pose = self.initializer.get_goal()

        # self.platform = TriFingerPlatform(
        #     visualization=self.visualization,
        #     initial_robot_position=initial_robot_position,
        #     initial_object_pose=initial_object_pose,
        # )
        self._reset_platform_frontend()

        self.goal = {
            "position": goal_object_pose.position,
            "orientation": goal_object_pose.orientation,
        }
        # visualize the goal
        if self.visualization:
            if self.is_level_4:
                self.goal_marker = visual_objects.CubeMarker(
                    width=0.065,
                    position=goal_object_pose.position,
                    orientation=goal_object_pose.orientation,
                )
                self.ori_goal_marker = VisualCubeOrientation(
                    goal_object_pose.position,
                    goal_object_pose.orientation
                )

            else:
                self.goal_marker = visual_objects.SphereMaker(
                    radius=0.065 / 2,
                    position=goal_object_pose.position,
                )

        self.info = dict()

        self.step_count = 0
        observation, _, _, _ = self.step(self._initial_action)
        # init_obs = self._create_observation(0)

        if self.visualization:
            self.ori_marker = VisualCubeOrientation(
                init_obs['object_position'],
                init_obs['object_orientation']
            )

        return init_obs