Beispiel #1
0
    def __init__(
        self,
        generator_fn=None,
        fitness_fn=None,
        pop_size=1,
        maximize=True,
        errors="raise",
        early_stop=0.5,
        evaluation_timeout: int = 5 * Min,
        memory_limit: int = 4 * Gb,
        search_timeout: int = 60 * 60,
        target_fn=None,
        allow_duplicates=True,
    ):
        if generator_fn is None and fitness_fn is None:
            raise ValueError(
                "You must provide either `generator_fn` or `fitness_fn`")

        self._generator_fn = generator_fn
        self._fitness_fn = fitness_fn or (lambda x: x)
        self._pop_size = pop_size
        self._maximize = maximize
        self._errors = errors
        self._evaluation_timeout = evaluation_timeout
        self._memory_limit = memory_limit
        self._early_stop = early_stop
        self._search_timeout = search_timeout
        self._target_fn = target_fn
        self._allow_duplicates = allow_duplicates

        if self._evaluation_timeout > 0 or self._memory_limit > 0:
            self._fitness_fn = RestrictedWorkerByJoin(self._fitness_fn,
                                                      self._evaluation_timeout,
                                                      self._memory_limit)
Beispiel #2
0
def test_handles_exc():
    fn = RestrictedWorkerByJoin(func, timeout=None, memory=None)

    with pytest.raises(Exception) as e:
        fn(0, 0, True)

    assert str(e.value).startswith("You asked for it.")
Beispiel #3
0
def test_restrict_memory_fails():
    fn = RestrictedWorkerByJoin(func, timeout=None, memory=1 * Mb)

    with pytest.raises(ValueError) as e:
        fn(0, 10**8)

    assert isinstance(e.value, ValueError)
def test_restrict_memory():
    fn = RestrictedWorkerByJoin(func, timeout=None, memory=1 * Gb)

    if platform.system() == "Linux":
        with pytest.raises(MemoryError):
            fn(0, 10**8)

    if platform.system() == "Windows":
        fn(0, 10**8)
def test_restrict_memory_fails():
    fn = RestrictedWorkerByJoin(func, timeout=None, memory=1 * Mb)

    if platform.system() == "Linux":
        with pytest.raises(ValueError) as e:
            fn(0, 10**8)

    if platform.system() == "Windows":
        # TODO: implement memory restriction for Windows systems
        pass

    assert isinstance(e.value, ValueError)
Beispiel #6
0
def test_restrict_memory():
    fn = RestrictedWorkerByJoin(func, timeout=None, memory=1 * Gb)

    with pytest.raises(MemoryError):
        fn(0, 10**8)
Beispiel #7
0
def test_restrict_time():
    fn = RestrictedWorkerByJoin(func, timeout=1, memory=None)

    with pytest.raises(TimeoutError):
        fn(2, 1024)
Beispiel #8
0
def test_no_restriction():
    fn = RestrictedWorkerByJoin(func, timeout=None, memory=None)
    fn(0, 1024)