Ejemplo n.º 1
0
    def __init__(self,
                 dt: float,
                 max_steps: int = pyrado.inf,
                 task_args: [dict, None] = None):
        """
        Constructor

        :param dt: simulation step size [s]
        :param max_steps: maximum number of simulation steps
        :param task_args: arguments for the task construction
        """
        Serializable._init(self, locals())
        super().__init__(dt, max_steps)

        # Initialize the domain parameters and the derived constants
        self._domain_param = self.get_nominal_domain_param()
        self._set_domain_param_attrs(self.get_nominal_domain_param())
        self._calc_constants()

        # Initialize spaces
        self._state_space = None
        self._obs_space = None
        self._act_space = None
        self._init_space = None
        self._create_spaces()

        # Create task
        if not (isinstance(task_args, dict) or task_args is None):
            raise pyrado.TypeErr(given=task_args, expected_type=dict)
        self._task = self._create_task(
            task_args=dict() if task_args is None else task_args)

        # Animation with VPython
        self._curr_act = np.zeros(self.act_space.shape)
        self._anim = dict(canvas=None)
Ejemplo n.º 2
0
    def __init__(self,
                 task_args: Optional[dict] = None,
                 relativeZdTask: bool = True,
                 **kwargs):
        """
        Constructor

        :param task_args: arguments for the task construction
        :param relativeZdTask: if `True`, the action model uses a relative velocity task for the striking motion
        :param kwargs: keyword arguments forwarded to `RcsSim`
                       fixedInitState: bool = False,
                       checkJointLimits: bool = True,
                       collisionAvoidanceIK: bool = False,
                       observeVelocities: bool = False,
                       observeForceTorque: bool = False,
        """
        Serializable._init(self, locals())

        # Forward to the MiniGolfSim's constructor, specifying the characteristic action model
        super().__init__(
            task_args=dict() if task_args is None else task_args,
            actionModelType="ik",
            positionTasks=True,
            relativeZdTask=relativeZdTask,
            **kwargs,
        )
Ejemplo n.º 3
0
    def __init__(self,
                 dt: float,
                 max_steps: int,
                 task_args: [dict, None] = None,
                 ip: str = '192.168.2.17'):
        """
        Constructor

        :param dt: time step size on the Quanser device [s]
        :param max_steps: maximum number of steps executed on the device
        :param task_args: arguments for the task construction
        :param ip: IP address of the Cart-Pole platform
        """
        Serializable._init(self, locals())

        # Initialize spaces, dt, max_step, and communication
        super().__init__(ip,
                         rcv_dim=4,
                         snd_dim=1,
                         dt=dt,
                         max_steps=max_steps,
                         task_args=task_args)
        self._curr_act = np.zeros(
            self.act_space.shape)  # just for usage in render function

        # Calibration and limits
        self._l_rail = 0.814  # [m]
        self._x_buffer = 0.05  # [m]
        self._calibrated = False
        self._c_lim = 0.075
        self._norm_x_lim = np.zeros(2)
Ejemplo n.º 4
0
    def __init__(
        self,
        num_dof: int,
        model_path: str,
        frame_skip: int = 4,
        dt: Optional[float] = None,
        max_steps: int = pyrado.inf,
        task_args: Optional[dict] = None,
    ):
        """
        Constructor

        :param num_dof: number of degrees of freedom (4 or 7), depending on which Barrett WAM setup being used
        :param model_path: path to the MuJoCo xml model config file
        :param frame_skip: number of simulation frames for which the same action is held, results in a multiplier of
                           the time step size `dt`
        :param dt: by default the time step size is the one from the mujoco config file multiplied by the number of
                   frame skips (legacy from OpenAI environments). By passing an explicit `dt` value, this can be
                   overwritten. Possible use case if if you know that you recorded a trajectory with a specific `dt`.
        :param max_steps: max number of simulation time steps
        :param task_args: arguments for the task construction
        """
        Serializable._init(self, locals())

        self._num_dof = num_dof
        self.camera_config = dict(
            trackbodyid=0,  # id of the body to track
            elevation=-30,  # camera rotation around the axis in the plane
            azimuth=-90,  # camera rotation around the camera's vertical axis
        )

        # Call MujocoSimEnv's constructor
        super().__init__(model_path, frame_skip, dt, max_steps, task_args)
Ejemplo n.º 5
0
    def __init__(self, wrapped_rand_env: [DomainRandWrapper],
                 mapping: Dict[int, Tuple[str, str]]):
        """
        Constructor

        :param wrapped_rand_env: randomized environment to wrap
        :param mapping: mapping from index of the numpy array (coming from the algorithm) to domain parameter name
                        (e.g. mass, length) and the domain distribution parameter (e.g. mean, std)

        .. note::
            For the `mapping` arg use the this dict constructor:
            ```
            m = {0: ('name1', 'parameter_type1'), 1: ('name2', 'parameter_type2')}
            ```
        """
        if not isinstance(wrapped_rand_env, DomainRandWrapper):
            raise pyrado.TypeErr(given=wrapped_rand_env,
                                 expected_type=DomainRandWrapper)

        Serializable._init(self, locals())

        # Invoke the DomainRandWrapper's constructor
        super().__init__(wrapped_rand_env, None)

        self.mapping = mapping
Ejemplo n.º 6
0
    def __init__(self, task_args: dict = None, **kwargs):
        """
        Constructor

        :param task_args: arguments for the task construction
        :param kwargs: keyword arguments forwarded to `RcsSim`
                       positionTasks: bool = True,
                       checkJointLimits: bool = True,
                       collisionAvoidanceIK: bool = True,
                       observeVelocities: bool = True,
                       observeForceTorque: bool = True,
                       observeCollisionCost: bool = False,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemGoalDistance: bool = False,
        """
        Serializable._init(self, locals())

        task_spec_ik = [
            dict(x_des=np.array([0.0, 0.0, 0.0])),
            dict(x_des=np.array([0.0, 0.0, 0.0])),
            dict(x_des=np.array([0.0, 0.0, 0.0])),
        ]

        # Forward to the Planar3LinkSim's constructor, specifying the characteristic action model
        super().__init__(
            task_args=dict() if task_args is None else task_args,
            actionModelType="ik_activation",
            taskSpecIK=task_spec_ik,
            **kwargs,
        )
Ejemplo n.º 7
0
    def __init__(self,
                 ref_frame: str,
                 continuous_rew_fcn: bool = True,
                 **kwargs):
        """
        Constructor

        :param ref_frame: reference frame for the MPs, e.g. 'world', or 'table'
        :param continuous_rew_fcn: specify if the continuous or an uninformative reward function should be used
        :param kwargs: keyword arguments which are available for all task-based `RcsSim`
                       fixedInitState: bool = False,
                       taskCombinationMethod: str = 'sum',  # or 'mean', 'softmax', 'product'
                       checkJointLimits: bool = False,
                       collisionAvoidanceIK: bool = True,
                       observeVelocities: bool = True,
                       observeCollisionCost: bool = True,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemDiscrepancy: bool = False,
                       observeTaskSpaceDiscrepancy: bool = True,
                       observeForceTorque: bool = True
        """
        Serializable._init(self, locals())

        # Forward to the BallInTubeSim's constructor
        super().__init__(task_args=dict(continuous_rew_fcn=continuous_rew_fcn),
                         ref_frame=ref_frame,
                         actionModelType='ik',
                         positionTasks=False,
                         **kwargs)
Ejemplo n.º 8
0
    def __init__(self, wrapped_env: Union[SimEnv, EnvWrapper],
                 mask: Union[List[str], Tuple[str]]):
        """
        Constructor

        :param wrapped_env: environment to wrap
        :param mask: every domain parameters which names are in this mask will be transformed. Capitalisation matters.
        """
        if not isinstance(wrapped_env, (SimEnv, EnvWrapper)):
            raise pyrado.TypeErr(given=wrapped_env,
                                 expected_type=(SimEnv, EnvWrapper))
        if not isinstance(mask, (list, tuple)):
            raise pyrado.TypeErr(given=wrapped_env,
                                 expected_type=(list, tuple))

        Serializable._init(self, locals())

        # Call EnvWrapper's constructor
        super().__init__(wrapped_env)

        if any(item not in wrapped_env.supported_domain_param
               for item in mask):
            raise pyrado.ValueErr(
                msg=
                f"The specified mask {mask} contains domain parameters that are not supported by the wrapped "
                f"environment! Here are the supported domain parameters {wrapped_env.supported_domain_param}."
            )
        self._mask = mask
    def __init__(self,
                 wrapped_env: Env,
                 explicit_lb: Mapping[str, float] = None,
                 explicit_ub: Mapping[str, float] = None):
        """
        Constructor

        :param wrapped_env: environment to wrap
        :param explicit_lb: dict to override the environment's lower bound; by default (`None`) this is ignored;
                            the keys are space labels, the values the new bound for that labeled entry
        :param explicit_ub: dict to override the environment's upper bound; by default (`None`) this is ignored;
                            the keys are space labels, the values the new bound for that labeled entry
        """
        Serializable._init(self, locals())
        super().__init__(wrapped_env)

        # Explicitly override the bounds if desired
        self.explicit_lb = explicit_lb
        self.explicit_ub = explicit_ub

        # Get the bounds of the inner observation space
        wos = self.wrapped_env.obs_space
        lb, ub = wos.bounds

        # Override the bounds if desired and store the result for usage in _process_obs
        self.ov_lb = ObsNormWrapper.override_bounds(lb, self.explicit_lb, wos.labels)
        self.ov_ub = ObsNormWrapper.override_bounds(ub, self.explicit_ub, wos.labels)

        # Check if the new bounds are valid
        if any(self.ov_lb == -pyrado.inf):
            raise pyrado.ValueErr(msg=f'At least one element of the lower bounds is (negative) infinite:\n'
                                      f'(overwritten) bound: {self.ov_lb}\nnames: {wos.labels}')
        if any(self.ov_ub == pyrado.inf):
            raise pyrado.ValueErr(msg=f'At least one element of the upper bound is (positive) infinite:\n'
                                      f'(overwritten) bound: {self.ov_ub}\nnames: {wos.labels}')
Ejemplo n.º 10
0
    def __init__(self,
                 mps_left: Sequence[dict],
                 mps_right: Sequence[dict],
                 continuous_rew_fcn: bool = True,
                 **kwargs):
        """
        Constructor

        :param mps_left: left arm's movement primitives holding the dynamical systems and the goal states
        :param mps_right: right arm's movement primitives holding the dynamical systems and the goal states
        :param continuous_rew_fcn: specify if the continuous or an uninformative reward function should be used
        :param kwargs: keyword arguments which are available for all task-based `RcsSim`
                       checkJointLimits: bool = False,
                       collisionAvoidanceIK: bool = True,
                       observeCollisionCost: bool = True,
                       observePredictedCollisionCost: bool = False,
        """
        Serializable._init(self, locals())

        # Forward to the RcsSim's constructor
        RcsSim.__init__(self,
                        envType='TargetTracking',
                        task_args=dict(continuous_rew_fcn=continuous_rew_fcn,
                                       mps_left=mps_left,
                                       mps_right=mps_right),
                        extraConfigDir=osp.join(rcsenv.RCSPYSIM_CONFIG_PATH,
                                                'TargetTracking'),
                        tasksLeft=mps_left,
                        tasksRight=mps_right,
                        **kwargs)
Ejemplo n.º 11
0
    def __init__(self, task_args: [dict, None] = None, max_dist_force: [float, None] = None, **kwargs):
        """
        Constructor

        :param task_args: arguments for the task construction, pass `None` to use default values
                          state_des: 4-dim np.ndarray
                          Q: 4x4-dim np.ndarray
                          R: 4x4-dim np.ndarray
        :param max_dist_force: maximum disturbance force, pass `None` for no disturbance
        :param kwargs: keyword arguments forwarded to the RcsSim
        """
        Serializable._init(self, locals())

        # Forward to RcsSim's constructor
        RcsSim.__init__(
            self,
            envType="QuanserQube",
            task_args=task_args if task_args is not None else dict(),
            graphFileName="gQuanserQube_trqCtrl.xml",
            physicsConfigFile="pQuanserQube.xml",
            actionModelType="joint_acc",
            **kwargs,
        )

        # Setup disturbance
        self._max_dist_force = max_dist_force
Ejemplo n.º 12
0
    def __init__(self,
                 ref_frame: str,
                 mps_left: [Sequence[dict], None],
                 mps_right: [Sequence[dict], None],
                 continuous_rew_fcn: bool = True,
                 **kwargs):
        """
        Constructor

        :param ref_frame: reference frame for the MPs, e.g. 'world', 'table', or 'box'
        :param mps_left: left arm's movement primitives holding the dynamical systems and the goal states
        :param mps_right: right arm's movement primitives holding the dynamical systems and the goal states
        :param continuous_rew_fcn: specify if the continuous or an uninformative reward function should be used
        :param kwargs: keyword arguments which are available for all task-based `RcsSim`
                       taskCombinationMethod: str = 'sum', # or 'mean', 'softmax', 'product'
                       checkJointLimits: bool = False,
                       collisionAvoidanceIK: bool = True,
                       observeVelocities: bool = False,
                       observeCollisionCost: bool = True,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemDiscrepancy: bool = False,
                       observeTaskSpaceDiscrepancy: bool = True,
                       observeForceTorque: bool = True
        """
        Serializable._init(self, locals())

        # Fall back to some defaults of no MPs are defined (e.g. for testing)
        dt = kwargs.get('dt', 0.01)  # 100 Hz is the default
        # basket_extends = self.get_body_extents('Basket', 0)
        if mps_left is None:
            mps_left = [
                # Yd
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([0.1])},  # [m/s]
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([-0.1])},  # [m/s]
                # Zd
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([0.1])},  # [m/s]
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([-0.1])},  # [m/s]
            ]
        if mps_right is None:
            mps_right = [
                # Yd
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([0.1])},  # [m/s]
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([-0.1])},  # [m/s]
                # Zd
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([0.1])},  # [m/s]
                {'function': 'lin', 'errorDynamics': 1., 'goal': dt*np.array([-0.1])},  # [m/s]
            ]

        # Forward to the BoxFlippingSim's constructor
        super().__init__(
            task_args=dict(continuous_rew_fcn=continuous_rew_fcn),
            ref_frame=ref_frame,
            mps_left=mps_left,
            mps_right=mps_right,
            actionModelType='ds_activation',
            positionTasks=False,
            **kwargs
        )
Ejemplo n.º 13
0
    def __init__(
        self,
        wrapped_env: [RealEnv, EnvWrapper],
        factor: int,
        obs_filter_fcn: callable = functools.partial(np.mean, axis=0),
        init_obs: np.ndarray = None,
    ):
        """
        Constructor

        :param wrapped_env: environment to wrap around
        :param factor: downsampling factor i.e. number of time steps for which every action should be repeated
        :param obs_filter_fcn: function for processing the observations in the buffer, operates along 0-dimension
        :param init_obs: initial observation to see the buffer, if None the buffer is initialized with zero arrays
        """
        Serializable._init(self, locals())

        # Invoke base constructor
        super().__init__(wrapped_env)

        self._factor = factor
        self._cnt = 0
        self._act_last = None
        if init_obs is None:
            self._obs_buffer = deque([], maxlen=factor)
        else:
            assert isinstance(init_obs, np.ndarray)
            self._obs_buffer = deque([init_obs.copy()], maxlen=factor)
        self._obs_filter_fcn = obs_filter_fcn
Ejemplo n.º 14
0
    def __init__(
        self,
        env: Env,
        policy: Policy,
        embedding: Embedding,
        num_segments: int = None,
        len_segments: int = None,
        stop_on_done: bool = True,
    ):
        """
        Constructor

        :param env: environment which the policy operates, in sim-to-real settings this is a real-world device, i.e.
                    `RealEnv`, but in a sim-to-sim experiment this can be a (randomized) `SimEnv`
        :param policy: policy used for sampling the rollout
        :param embedding: embedding used for pre-processing the data before (later) passing it to the posterior
        :param num_segments: number of segments in which the rollouts are split into. For every segment, the initial
                             state of the simulation is reset, and thus for every set the features of the trajectories
                             are computed separately. Either specify `num_segments` or `len_segments`.
        :param len_segments: length of the segments in which the rollouts are split into. For every segment, the initial
                            state of the simulation is reset, and thus for every set the features of the trajectories
                            are computed separately. Either specify `num_segments` or `len_segments`.
        :param stop_on_done: if `True`, the rollouts are stopped as soon as they hit the state or observation space
                             boundaries. This behavior is save, but can lead to short trajectories which are eventually
                             padded with zeroes. Chose `False` to ignore the boundaries (dangerous on the real system).
        """

        Serializable._init(self, locals())

        super().__init__(env, policy, embedding, num_segments, len_segments, stop_on_done)
Ejemplo n.º 15
0
    def __init__(
        self, wrapped_env: Env, noise_mean: Union[float, np.ndarray] = None, noise_std: Union[float, np.ndarray] = None
    ):
        """
        Constructor

        :param wrapped_env: environment to wrap around (only makes sense for simulations)
        :param noise_mean: mean of the noise distribution
        :param noise_std: standard deviation of the noise distribution
        """
        Serializable._init(self, locals())

        # Invoke base constructor
        super().__init__(wrapped_env)

        # Parse noise specification
        if noise_mean is not None:
            self._mean = np.array(noise_mean)
            if not self._mean.shape == self.act_space.shape:
                raise pyrado.ShapeErr(given=self._mean, expected_match=self.act_space)
        else:
            self._mean = np.zeros(self.act_space.shape)
        if noise_std is not None:
            self._std = np.array(noise_std)
            if not self._std.shape == self.act_space.shape:
                raise pyrado.ShapeErr(given=self._noise_std, expected_match=self.act_space)
        else:
            self._std = np.zeros(self.act_space.shape)
Ejemplo n.º 16
0
    def __init__(self, ref_frame: str, continuous_rew_fcn: bool = True, **kwargs):
        """
        Constructor

        :param ref_frame: reference frame for the Rcs tasks, e.g. 'world', 'table', or 'box'
        :param continuous_rew_fcn: specify if the continuous or an uninformative reward function should be used
        :param kwargs: keyword arguments which are available for all task-based `RcsSim`
                       checkJointLimits: bool = False,
                       collisionAvoidanceIK: bool = True,
                       observeVelocities: bool = False,
                       observeCollisionCost: bool = True,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemDiscrepancy: bool = False,
                       observeTaskSpaceDiscrepancy: bool = True,
                       observeForceTorque: bool = True
        """
        Serializable._init(self, locals())

        # Forward to the BoxFlippingSim's constructor
        super().__init__(
            task_args=dict(continuous_rew_fcn=continuous_rew_fcn),
            ref_frame=ref_frame,
            position_mps=True,
            actionModelType='ik',
            **kwargs
        )
Ejemplo n.º 17
0
    def __init__(self, ref_frame: str, **kwargs):
        """
        Constructor

        :param ref_frame: reference frame for the Rcs tasks, e.g. 'world', 'table', or 'box'
        :param kwargs: keyword arguments which are available for all task-based `RcsSim`
                       checkJointLimits:m bool = False,
                       collisionAvoidanceIK: bool = True,
                       positionTasks: bool = True,
                       observeVelocities: bool = False,
                       observeCollisionCost: bool = True,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemDiscrepancy: bool = False,
                       observeTaskSpaceDiscrepancy: bool = True,
                       observeForceTorque: bool = True
        """
        Serializable._init(self, locals())

        task_spec_ik = [
            dict(x_des=np.array([-0.4])),  # Y right
            dict(x_des=np.array([0.3])),  # Z right
            dict(x_des=np.array([0.0])),  # distance Box
        ]

        # Forward to the BoxLiftingSim's constructor
        super().__init__(
            task_args=dict(),
            ref_frame=ref_frame,
            actionModelType="ik_activation",
            taskSpecIK=task_spec_ik,
            positionTasks=True,
            **kwargs,
        )
Ejemplo n.º 18
0
    def __init__(self, state_des: np.ndarray = None, **kwargs):
        """
        Constructor

        :param state_des: desired state for the task, pass `None` to use the default goal
        :param kwargs: keyword arguments forwarded to `RcsSim`
                       graphFileName: str = 'gPlanarInsert5Link.xml' or 'gPlanarInsert6Link.xml'
                       checkJointLimits: bool = True,
                       collisionAvoidanceIK: bool = True,
                       observeForceTorque: bool = True,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemGoalDistance: bool = False,
                       observeDynamicalSystemDiscrepancy: bool = False,
        """
        Serializable._init(self, locals())

        dt = kwargs.get("dt", 0.01)  # 100 Hz is the default
        task_spec_ik = [
            dict(x_des=np.array([dt * 0.1])),
            dict(x_des=np.array([dt * 0.1])),
            dict(x_des=np.array([dt * 5 / 180 * np.pi])),
        ]

        # Forward to the PlanarInsertSim's constructor, nothing more needs to be done here
        PlanarInsertSim.__init__(
            self,
            task_args=dict(state_des=state_des),
            actionModelType="ik_activation",
            taskSpecIK=task_spec_ik,
            **kwargs,
        )
Ejemplo n.º 19
0
    def __init__(self,
                 dt: float = 1 / 500.,
                 max_steps: int = pyrado.inf,
                 task_args: [dict, None] = None,
                 ip: str = '192.168.2.40'):
        """
        Constructor

        :param dt: sampling frequency on the Quanser device [Hz]
        :param max_steps: maximum number of steps executed on the device [-]
        :param task_args: arguments for the task construction
        :param ip: IP address of the Qube platform
        """
        Serializable._init(self, locals())

        # Initialize spaces, dt, max_step, and communication
        super().__init__(ip,
                         rcv_dim=4,
                         snd_dim=1,
                         dt=dt,
                         max_steps=max_steps,
                         task_args=task_args)
        self._curr_act = np.zeros(
            self.act_space.shape)  # just for usage in render function
        self._sens_offset = np.zeros(
            4
        )  # last two entries are never calibrated but useful for broadcasting
Ejemplo n.º 20
0
    def __init__(self,
                 envType: str,
                 task_args: dict,
                 dt: float = 0.01,
                 max_steps: int = pyrado.inf,
                 init_state: np.ndarray = None,
                 checkJointLimits: bool = False,
                 joint_limit_penalty: float = -1e3,
                 **kwargs):
        """
        Constructor

        .. note::
            The joint type (i.e. position or torque control) is set in the config-xml file in Rcs.

        :param envType: environment type name as defined on the C++ side
        :param task_args: arguments for the task construction
        :param dt: integration step size in seconds
        :param max_steps: max number of simulation time steps
        :param domain_param: initial domain param values
        :param init_state: initial state sampler can be a callable or one fixed state
        :param checkJointLimits: flags if the joint limits should be ignored or not passed to the C++ constructor
        :param joint_limit_penalty: cost returned on termination due to joint limits. This is a different from the
                                    state bounds since `RcsPySim` return an error when the joint limits are violated.
        :param kwargs: keyword arguments which are available for `RcsSim` on the C++ side. These arguments will not
                       be stored in the environment object, thus are saved e.g. when pickled.
        """
        Serializable._init(self, locals())

        # Initialize basic variables
        super().__init__(dt, max_steps)
        self._check_joint_limits = checkJointLimits

        # Create Rcs-based implementation (RcsSimEnv comes from the pybind11 module)
        self._impl = RcsSimEnv(dt=dt,
                               envType=envType,
                               checkJointLimits=self._check_joint_limits,
                               **kwargs)

        # Setup the initial domain parameters
        self._domain_param = self._unadapt_domain_param(self._impl.domainParam)

        if joint_limit_penalty > 0:
            raise pyrado.ValueErr(given=joint_limit_penalty, le_constraint='0')
        self._joint_limit_penalty = joint_limit_penalty

        # Initial init state space is taken from C++
        self._init_space = to_pyrado_space(self._impl.initStateSpace)

        # By default, the state space is a subset of the observation space. Set this to customize in subclass.
        self.state_mask = None

        # Dummy initialization, must be set by the derived classes
        self.init_state = None
        self.task_args = task_args
        self._task = self._create_task(self.task_args)
Ejemplo n.º 21
0
    def __init__(self,
                 task_args: dict,
                 ref_frame: str,
                 position_mps: bool,
                 mps_left: [Sequence[dict], None],
                 fixed_init_state: bool = False,
                 **kwargs):
        """
        Constructor

        .. note::
            This constructor should only be called via the subclasses.

        :param task_args: arguments for the task construction
        :param ref_frame: reference frame for the MPs, e.g. 'world', 'box', or 'upperGoal'
        :param mps_left: left arm's movement primitives holding the dynamical systems and the goal states
        :param position_mps: `True` if the MPs are defined on position level, `False` if defined on velocity level
        :param fixed_init_state: use an init state space with only one state (e.g. for debugging)
        :param kwargs: keyword arguments which are available for all task-based `RcsSim`
                       taskCombinationMethod: str = 'mean',  # 'sum', 'mean',  'product', or 'softmax'
                       checkJointLimits: bool = False,
                       collisionAvoidanceIK: bool = True,
                       observeVelocities: bool = True,
                       observeCollisionCost: bool = True,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemDiscrepancy: bool = False,
                       observeTaskSpaceDiscrepancy: bool = True,
                       observeForceTorque: bool = True
        """
        Serializable._init(self, locals())

        # Forward to the RcsSim's constructor
        RcsSim.__init__(
            self,
            envType='BoxShelving',
            extraConfigDir=osp.join(rcsenv.RCSPYSIM_CONFIG_PATH, 'BoxShelving'),
            hudColor='BLACK_RUBBER',
            task_args=task_args,
            refFrame=ref_frame,
            positionTasks=position_mps,
            tasksLeft=mps_left,
            **kwargs
        )

        # Initial state space definition
        if fixed_init_state:
            dafault_init_state = np.array([0., 0., 0., 0.8, 30.*np.pi/180, 90.*np.pi/180])  # [m, m, rad, m, rad, rad]
            self._init_space = SingularStateSpace(dafault_init_state,
                                                  labels=['$x$', '$y$', '$th$', '$z$', '$q_2$', '$q_4$'])
        else:
            min_init_state = np.array([-0.02, -0.02, -3.*np.pi/180., 0.78, 27.*np.pi/180, 77.*np.pi/180])
            max_init_state = np.array([0.02, 0.02, 3.*np.pi/180., 0.82, 33.*np.pi/180, 83.*np.pi/180])
            self._init_space = BoxSpace(min_init_state, max_init_state,  # [m, m, rad, m, rad, rad]
                                        labels=['$x$', '$y$', '$th$', '$z$', '$q_2$', '$q_4$'])
Ejemplo n.º 22
0
    def __init__(self,
                 task_args: dict,
                 max_dist_force: float = None,
                 **kwargs):
        """
        Constructor

        .. note::
            This constructor should only be called via the subclasses.

        :param task_args: arguments for the task construction
        :param max_dist_force: maximum disturbance force, pass `None` for no disturbance
        :param kwargs: keyword arguments forwarded to `RcsSim`
                       fixedInitState: bool = True,
                       collisionConfig: specification of the Rcs CollisionModel
        """
        Serializable._init(self, locals())

        if kwargs.get("collisionConfig", None) is None:
            collision_config = {
                "pairs": [
                    {
                        "body1": "Effector",
                        "body2": "Link2"
                    },
                    {
                        "body1": "Effector",
                        "body2": "Link1"
                    },
                ],
                "threshold":
                0.15,
                "predCollHorizon":
                20,
            }
        else:
            collision_config = kwargs.get("collisionConfig")

        # Forward to RcsSim's constructor
        RcsSim.__init__(
            self,
            envType="Planar3Link",
            task_args=task_args,
            graphFileName=kwargs.pop(
                "graphFileName",
                "gPlanar3Link_trqCtrl.xml"),  # by default torque control
            positionTasks=kwargs.pop(
                "positionTasks", None
            ),  # invalid default value, positionTasks can be unnecessary
            collisionConfig=collision_config,
            **kwargs,
        )

        # Setup disturbance
        self._max_dist_force = max_dist_force
Ejemplo n.º 23
0
    def __init__(
        self,
        env: Union[SimEnv, EnvWrapper],
        policy: Policy,
        dp_mapping: Mapping[int, str],
        embedding: Embedding,
        num_segments: int = None,
        len_segments: int = None,
        stop_on_done: bool = True,
        rollouts_real: Optional[List[StepSequence]] = None,
        use_rec_act: bool = True,
    ):
        """
        Constructor

        :param env: environment which the policy operates, which must not be a randomized environment since we want to
                    randomize it manually via the domain parameters coming from the sbi package
        :param policy: policy used for sampling the rollout
        :param dp_mapping: mapping from subsequent integers (starting at 0) to domain parameter names (e.g. mass)
        :param embedding: embedding used for pre-processing the data before (later) passing it to the posterior
        :param num_segments: number of segments in which the rollouts are split into. For every segment, the initial
                             state of the simulation is reset, and thus for every set the features of the trajectories
                             are computed separately. Either specify `num_segments` or `len_segments`.
        :param len_segments: length of the segments in which the rollouts are split into. For every segment, the initial
                            state of the simulation is reset, and thus for every set the features of the trajectories
                            are computed separately. Either specify `num_segments` or `len_segments`.
        :param stop_on_done: if `True`, the rollouts are stopped as soon as they hit the state or observation space
                             boundaries. This behavior is save, but can lead to short trajectories which are eventually
                             padded with zeroes. Chose `False` to ignore the boundaries (dangerous on the real system).
        :param rollouts_real: list of rollouts recorded from the target domain, which are used to sync the simulations'
                              initial states
        :param use_rec_act: if `True` the recorded actions form the target domain are used to generate the rollout
                            during simulation (feed-forward). If `False` there policy is used to generate (potentially)
                            state-dependent actions (feed-back).
        """
        if typed_env(env, DomainRandWrapper):
            raise pyrado.TypeErr(
                msg="The environment passed to sbi as simulator must not be wrapped with a subclass of"
                "DomainRandWrapper since sbi has be able to set the domain parameters explicitly!"
            )
        if rollouts_real is not None:
            if not isinstance(rollouts_real, list):
                raise pyrado.TypeErr(given=rollouts_real, expected_type=list)
            if not isinstance(rollouts_real[0], StepSequence):  # only check 1st element
                raise pyrado.TypeErr(given=rollouts_real[0], expected_type=StepSequence)

        Serializable._init(self, locals())

        super().__init__(env, policy, embedding, num_segments, len_segments, stop_on_done)

        self.dp_names = dp_mapping.values()
        self.rollouts_real = rollouts_real
        self.use_rec_act = use_rec_act
        if self.rollouts_real is not None:
            self._set_action_field(self.rollouts_real)
Ejemplo n.º 24
0
    def __init__(self,
                 task_args: dict,
                 max_dist_force: float = None,
                 **kwargs):
        """
        Constructor

        .. note::
            This constructor should only be called via the subclasses.

        :param task_args: arguments for the task construction
        :param max_dist_force: maximum disturbance force, pass `None` for no disturbance
        :param kwargs: keyword arguments forwarded to `RcsSim`
                       fixedInitState: bool = True,
                       collisionConfig: specification of the Rcs CollisionModel
        """
        Serializable._init(self, locals())

        if kwargs.get('collisionConfig', None) is None:
            collision_config = {
                'pairs': [
                    {
                        'body1': 'Effector',
                        'body2': 'Link2'
                    },
                    {
                        'body1': 'Effector',
                        'body2': 'Link1'
                    },
                ],
                'threshold':
                0.15,
                'predCollHorizon':
                20
            }
        else:
            collision_config = kwargs.get('collisionConfig')

        # Forward to RcsSim's constructor
        RcsSim.__init__(
            self,
            task_args=task_args,
            envType='Planar3Link',
            graphFileName=kwargs.pop(
                'graphFileName',
                'gPlanar3Link_trqCtrl.xml'),  # by default torque control
            positionTasks=kwargs.pop(
                'positionTasks', None
            ),  # invalid default value, positionTasks can be unnecessary
            collisionConfig=collision_config,
            **kwargs)

        # Setup disturbance
        self._max_dist_force = max_dist_force
Ejemplo n.º 25
0
    def __init__(self, wrapped_env: Env):
        """
        Constructor

        :param wrapped_env: environment to wrap
        """
        Serializable._init(self, locals())
        super().__init__(wrapped_env)

        # Explicitly override the bounds if desired
        self.normalizer = RunningNormalizer()
Ejemplo n.º 26
0
    def __init__(self, wrapped_env, policy, eps, phi):
        """
        Constructor

        :param wrapped_env: environment to be wrapped
        :param policy: policy to be updated
        :param eps: magnitude of perturbation
        :param phi: probability of perturbation
        """
        Serializable._init(self, locals())
        AdversarialWrapper.__init__(self, wrapped_env, policy, eps, phi)
Ejemplo n.º 27
0
    def __init__(self, wrapped_env: Env):
        """
        Constructor

        :param wrapped_env: environment to wrap
        """
        if not isinstance(wrapped_env, Env):
            raise pyrado.TypeErr(given=wrapped_env, expected_type=Env)

        Serializable._init(self, locals())

        self._wrapped_env = wrapped_env
Ejemplo n.º 28
0
    def __init__(self, task_args: dict = None, **kwargs):
        """
        Constructor

        :param kwargs: keyword arguments forwarded to `RcsSim`
        """
        Serializable._init(self, locals())

        # Forward to the Planar3LinkSim's constructor, specifying the characteristic action model
        super().__init__(task_args=dict() if task_args is None else task_args,
                         actionModelType='joint_pos',
                         **kwargs)
Ejemplo n.º 29
0
    def __init__(self, task_args: dict, ref_frame: str, position_mps: bool,
                 mps_left: [Sequence[dict], None], **kwargs):
        """
        Constructor

        .. note::
            This constructor should only be called via the subclasses.

        :param task_args: arguments for the task construction
        :param ref_frame: reference frame for the MPs, e.g. 'world', 'basket', or 'box'
        :param position_mps: `True` if the MPs are defined on position level, `False` if defined on velocity level
        :param mps_left: left arm's movement primitives holding the dynamical systems and the goal states
        :param kwargs: keyword arguments which are available for all task-based `RcsSim`
                       fixedInitState: bool = False,
                       taskCombinationMethod: str = 'sum',  # or 'mean', 'softmax', 'product'
                       checkJointLimits: bool = False,
                       collisionAvoidanceIK: bool = True,
                       observeVelocities: bool = False,
                       observeCollisionCost: bool = True,
                       observePredictedCollisionCost: bool = False,
                       observeManipulabilityIndex: bool = False,
                       observeCurrentManipulability: bool = True,
                       observeDynamicalSystemDiscrepancy: bool = False,
                       observeTaskSpaceDiscrepancy: bool = True,
                       observeForceTorque: bool = True
        """
        Serializable._init(self, locals())

        if kwargs.get('collisionConfig', None) is None:
            kwargs.update(
                collisionConfig={
                    'pairs': [
                        {
                            'body1': 'Hand',
                            'body2': 'Table'
                        },
                    ],
                    'threshold': 0.07
                })

        # Forward to the RcsSim's constructor
        RcsSim.__init__(self,
                        task_args=task_args,
                        envType='BoxLiftingSimple',
                        physicsConfigFile='pBoxLifting.xml',
                        extraConfigDir=osp.join(rcsenv.RCSPYSIM_CONFIG_PATH,
                                                'BoxLifting'),
                        hudColor='BLACK_RUBBER',
                        refFrame=ref_frame,
                        positionTasks=position_mps,
                        tasksLeft=mps_left,
                        **kwargs)
Ejemplo n.º 30
0
    def __init__(self,
                 task_args: dict,
                 max_dist_force: float = None,
                 **kwargs):
        """
        Constructor

        .. note::
            This constructor should only be called via the subclasses.

        :param task_args: arguments for the task construction
        :param max_dist_force: maximum disturbance force, set to None (default) for no disturbance
        :param kwargs: keyword arguments forwarded to `RcsSim`
                       collisionConfig: specification of the Rcs CollisionModel
        """
        Serializable._init(self, locals())

        if kwargs.get('collisionConfig', None) is None:
            collision_config = {
                'pairs': [
                    {
                        'body1': 'Effector',
                        'body2': 'Link2'
                    },
                    {
                        'body1': 'Effector',
                        'body2': 'Link1'
                    },
                ],
                'threshold':
                0.15,
                'predCollHorizon':
                20
            }
        else:
            collision_config = kwargs.get('collisionConfig')

        # Forward to the RcsSim's constructor, nothing more needs to be done here
        RcsSim.__init__(self,
                        envType='Planar3Link',
                        graphFileName='gPlanar3Link.xml',
                        task_args=task_args,
                        collisionConfig=collision_config,
                        **kwargs)

        # Initial state space definition
        upright_init_state = np.array([0.1, 0.1, 0.1])  # [rad, rad, rad]
        self._init_space = SingularStateSpace(
            upright_init_state, labels=['$q_1$', '$q_2$', '$q_3$'])

        # Setup disturbance
        self._max_dist_force = max_dist_force