예제 #1
0
def test_bad_interact():

    cats = [1, 2]
    interacter = OneHotCategoricalFeatureInteracter(cats)
    with pytest.raises(ValueError, match='Expected '):
        interacter.interact(np.array([
            [1, 2, 3],
            [4, 5, 6]
        ]), [1])
예제 #2
0
class SignedCodingFeatureExtractor(ContinuousFeatureExtractor):
    """
    Signed-coding feature extractor. Forms a category from the conjunction of all state-feature signs and then places
    the continuous feature vector into its associated category.
    """

    def extract(
            self,
            state: GymState,
            refit_scaler: bool
    ) -> np.ndarray:
        """
        Extract state features.

        :param state: State.
        :param refit_scaler: Whether to refit the feature scaler before scaling the extracted features.
        :return: State-feature vector.
        """

        if self.state_category_interacter is None:
            self.state_category_interacter = OneHotCategoricalFeatureInteracter([
                OneHotCategory(*category_args)
                for category_args in product(*([[True, False]] * state.observation.shape[0]))
            ])

        # form the one-hot state category
        state_category = OneHotCategory(*[
            value < 0.0
            for value in state.observation
        ])

        # extract and encode feature values
        raw_feature_values = super().extract(state, refit_scaler)
        encoded_feature_values = self.state_category_interacter.interact(
            np.array([raw_feature_values]),
            [state_category]
        )[0]

        return encoded_feature_values

    def __init__(
            self
    ):
        """
        Initialize the feature extractor.
        """

        super().__init__()

        self.state_category_interacter = None