Пример #1
0
    def test_register(self, space: Space, transformed_space: TransformedSpace):
        """Tests for the `register` method of the `RegistryMapping` class."""
        original_reg = Registry()
        transformed_reg = Registry()
        mapping = RegistryMapping(original_registry=original_reg,
                                  transformed_registry=transformed_reg)

        original_trial = space.sample(1)[0]
        transformed_trial = transformed_space.transform(original_trial)

        mapping.register(original_trial, transformed_trial)
        # NOTE: register doesn't actually register the trial, it just adds it to the mapping.
        assert len(mapping) == 1
        assert original_trial in mapping

        # NOTE: Here since we assume that the trials are supposed to be registered in the registries
        # externally, we can't yet iterate over the mapping (e.g. with keys(), values() or items()).

        # Now we actually register the trials in the individual registries.
        assert original_trial not in original_reg
        original_stored_id = original_reg.register(original_trial)
        assert transformed_trial not in transformed_reg
        transformed_stored_id = transformed_reg.register(transformed_trial)

        assert mapping._mapping == {
            original_stored_id: {transformed_stored_id}
        }
        assert list(mapping.keys()) == [original_trial]
        assert list(mapping.values()) == [[transformed_trial]]
        assert mapping[original_trial] == [transformed_trial]
Пример #2
0
def tspace(space, tdim, tdim2, tdim3):
    """Create an example `TransformedSpace`."""
    tspace = TransformedSpace(space)
    tspace.register(tdim)
    tspace.register(tdim2)
    tspace.register(tdim3)
    return tspace
Пример #3
0
    def test_extenal_register_doesnt_increase_len(
            self, space: Space, transformed_space: TransformedSpace):
        """Test that externally registering trials in the original or transformed registries does
        not affect the length of the mapping.
        """
        original = Registry()
        transformed = Registry()
        mapping = RegistryMapping(original_registry=original,
                                  transformed_registry=transformed)
        assert not mapping
        assert len(mapping) == 0

        original_trial = space.sample(1)[0]
        original.register(original_trial)
        assert not mapping

        transformed_trial = transformed_space.sample(1)[0]
        transformed.register(transformed_trial)
        assert not mapping