Source code for qdpy.algorithms.search

#    This file is part of qdpy.
#
#    qdpy is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of
#    the License, or (at your option) any later version.
#
#    qdpy is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with qdpy. If not, see <http://www.gnu.org/licenses/>.

## @file
# @brief Uniform and low-discrepancy candidate generators.

"""TODO""" # TODO

__all__ = ["RandomUniform", "Sobol"]

from typing import Optional, Tuple, List, Iterable, Iterator, Any, TypeVar, Generic, Union, Sequence, MutableSet, MutableSequence, Type, Callable, Generator, Mapping, MutableMapping, overload
import numpy as np
import random


from .base import *
from qdpy.utils import *
from qdpy.phenotype import *
from qdpy.base import *
from qdpy.containers import *
from qdpy import tools

import qdpy.hdsobol as hdsobol


[docs] @registry.register class RandomUniform(QDAlgorithm): """Generate independent uniform genome coordinates. Set ``dimension`` to the genome length and ``ind_domain`` to the common interval. The generator uses Python's random state. """ ind_domain: DomainLike dimension: int def __init__(self, container: Container, budget: int, ind_domain: DomainLike, **kwargs): super().__init__(container, budget, **kwargs) self.ind_domain = ind_domain if self.dimension is None: raise ValueError("`dimension` must be provided.") def _internal_ask(self, base_ind: IndividualLike) -> IndividualLike: #base_ind[:] = np.random.uniform(self.ind_domain[0], self.ind_domain[1], size=self.dimension) base_ind[:] = [random.uniform(self.ind_domain[0], self.ind_domain[1]) for _ in range(self.dimension)] return base_ind
[docs] @registry.register class Sobol(RandomUniform): """Propose a deterministic low-discrepancy sequence within the genome bounds. Skip the zero vector and scale the remaining Sobol points to ``ind_domain``. If a run needs more points than initially buffered, extend the sequence and preserve the already consumed prefix. Reinitialising resets candidate accounting and starts the sequence again. """ def __init__(self, container: Container, budget: int, ind_domain: DomainLike, **kwargs): super().__init__(container, budget, ind_domain=ind_domain, **kwargs) self._generate_vectors(budget or 0) ## @brief Build an origin-free sequence prefix for the current genome bounds. # @param count Number of usable candidate vectors to buffer. def _generate_vectors(self, count: int) -> None: lower, upper = self.ind_domain self.sobol_vect = hdsobol.gen_sobol_vectors(count, self.dimension) * (upper - lower) + lower def _internal_ask(self, base_ind: IndividualLike) -> IndividualLike: if self._nb_suggestions >= len(self.sobol_vect): count = max(self._nb_suggestions + 1, 2 * len(self.sobol_vect), self.budget or 0) self._generate_vectors(count) base_ind[:] = self.sobol_vect[self._nb_suggestions] return base_ind
# MODELINE "{{{1 # vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8 # vim:foldmethod=marker