Esempio n. 1
0
def test_observed_value_types():
    spaces = [
        ObservationSpace(
            name="ir",
            space=Space(string_value=StringSpace(length_range=Int64Range(
                min=0))),
        ),
        ObservationSpace(
            name="features",
            space=Space(int64_box=Int64Box(
                low=Int64Tensor(shape=[2], value=[-100, -100]),
                high=Int64Tensor(shape=[2], value=[100, 100]),
            ), ),
        ),
        ObservationSpace(
            name="dfeat",
            space=Space(double_box=DoubleBox(
                low=DoubleTensor(shape=[1], value=[0.5]),
                high=DoubleTensor(shape=[1], value=[2.5]),
            ), ),
        ),
        ObservationSpace(
            name="binary",
            space=Space(int64_value=Int64Range(min=5, max=5)),
        ),
    ]
    mock = MockRawStep(ret=[
        "Hello, IR",
        [1.0, 2.0],
        [-5, 15],
        b"Hello, bytes\0",
        "Hello, IR",
        [1.0, 2.0],
        [-5, 15],
        b"Hello, bytes\0",
    ])
    observation = ObservationView(mock, spaces)

    value = observation["ir"]
    assert isinstance(value, str)
    assert value == "Hello, IR"

    value = observation["dfeat"]
    np.testing.assert_array_almost_equal(value, [1.0, 2.0])

    value = observation["features"]
    np.testing.assert_array_equal(value, [-5, 15])

    value = observation["binary"]
    assert value == b"Hello, bytes\0"

    # Check that the correct observation_space_list indices were used.
    assert mock.called_observation_spaces == [
        "ir", "dfeat", "features", "binary"
    ]
    mock.called_observation_spaces = []

    # Repeat the above tests using the generated bound methods.
    value = observation.ir()
    assert isinstance(value, str)
    assert value == "Hello, IR"

    value = observation.dfeat()
    np.testing.assert_array_almost_equal(value, [1.0, 2.0])

    value = observation.features()
    np.testing.assert_array_equal(value, [-5, 15])

    value = observation.binary()
    assert value == b"Hello, bytes\0"

    # Check that the correct observation_space_list indices were used.
    assert mock.called_observation_spaces == [
        "ir", "dfeat", "features", "binary"
    ]
Esempio n. 2
0
def test_observed_value_types():
    spaces = [
        ObservationSpace(
            name="ir",
            string_size_range=ScalarRange(min=ScalarLimit(value=0)),
        ),
        ObservationSpace(
            name="features",
            int64_range_list=ScalarRangeList(range=[
                ScalarRange(min=ScalarLimit(value=-100),
                            max=ScalarLimit(value=100)),
                ScalarRange(min=ScalarLimit(value=-100),
                            max=ScalarLimit(value=100)),
            ]),
        ),
        ObservationSpace(
            name="dfeat",
            double_range_list=ScalarRangeList(range=[
                ScalarRange(min=ScalarLimit(value=0.5),
                            max=ScalarLimit(value=2.5))
            ]),
        ),
        ObservationSpace(
            name="binary",
            binary_size_range=ScalarRange(min=ScalarLimit(value=5),
                                          max=ScalarLimit(value=5)),
        ),
    ]
    mock = MockRawStep(ret=[
        "Hello, IR",
        [1.0, 2.0],
        [-5, 15],
        b"Hello, bytes\0",
        "Hello, IR",
        [1.0, 2.0],
        [-5, 15],
        b"Hello, bytes\0",
    ])
    observation = ObservationView(mock, spaces)

    value = observation["ir"]
    assert isinstance(value, str)
    assert value == "Hello, IR"

    value = observation["dfeat"]
    np.testing.assert_array_almost_equal(value, [1.0, 2.0])

    value = observation["features"]
    np.testing.assert_array_equal(value, [-5, 15])

    value = observation["binary"]
    assert value == b"Hello, bytes\0"

    # Check that the correct observation_space_list indices were used.
    assert mock.called_observation_spaces == [
        "ir", "dfeat", "features", "binary"
    ]
    mock.called_observation_spaces = []

    # Repeat the above tests using the generated bound methods.
    value = observation.ir()
    assert isinstance(value, str)
    assert value == "Hello, IR"

    value = observation.dfeat()
    np.testing.assert_array_almost_equal(value, [1.0, 2.0])

    value = observation.features()
    np.testing.assert_array_equal(value, [-5, 15])

    value = observation.binary()
    assert value == b"Hello, bytes\0"

    # Check that the correct observation_space_list indices were used.
    assert mock.called_observation_spaces == [
        "ir", "dfeat", "features", "binary"
    ]