QDpy
Loading...
Searching...
No Matches
search.py
Go to the documentation of this file.
1# This file is part of qdpy.
2#
3# qdpy is free software: you can redistribute it and/or modify
4# it under the terms of the GNU Lesser General Public License as
5# published by the Free Software Foundation, either version 3 of
6# the License, or (at your option) any later version.
7#
8# qdpy is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU Lesser General Public License for more details.
12#
13# You should have received a copy of the GNU Lesser General Public
14# License along with qdpy. If not, see <http://www.gnu.org/licenses/>.
15
16
18
19"""TODO""" # TODO
20
21__all__ = ["RandomUniform", "Sobol"]
22
23from typing import Optional, Tuple, List, Iterable, Iterator, Any, TypeVar, Generic, Union, Sequence, MutableSet, MutableSequence, Type, Callable, Generator, Mapping, MutableMapping, overload
24import numpy as np
25import random
26
27
28from .base import *
29from qdpy.utils import *
30from qdpy.phenotype import *
31from qdpy.base import *
32from qdpy.containers import *
33from qdpy import tools
34
35import qdpy.hdsobol as hdsobol
36
37
38@registry.register
40 """Generate independent uniform genome coordinates.
41
42 Set ``dimension`` to the genome length and ``ind_domain`` to the
43 common interval. The generator uses Python's random state.
44 """
45 ind_domain: DomainLike
46 dimension: int
47
48 def __init__(self, container: Container, budget: int, ind_domain: DomainLike, **kwargs):
49 super().__init__(container, budget, **kwargs)
50 self.ind_domain = ind_domain
51 if self.dimension is None:
52 raise ValueError("`dimension` must be provided.")
53
54 def _internal_ask(self, base_ind: IndividualLike) -> IndividualLike:
55 #base_ind[:] = np.random.uniform(self.ind_domain[0], self.ind_domain[1], size=self.dimension)
56 base_ind[:] = [random.uniform(self.ind_domain[0], self.ind_domain[1]) for _ in range(self.dimension)]
57 return base_ind
58
59
60
61@registry.register
63 """Propose a deterministic low-discrepancy sequence within the genome bounds.
64
65 Skip the zero vector and scale the remaining Sobol points to
66 ``ind_domain``. If a run needs more points than initially buffered,
67 extend the sequence and preserve the already consumed prefix.
68 Reinitialising resets candidate accounting and starts the sequence again.
69 """
70
71 def __init__(self, container: Container, budget: int, ind_domain: DomainLike, **kwargs):
72 super().__init__(container, budget, ind_domain=ind_domain, **kwargs)
73 self._generate_vectors(budget or 0)
74
75
77 def _generate_vectors(self, count: int) -> None:
78 lower, upper = self.ind_domain
79 self.sobol_vect = hdsobol.gen_sobol_vectors(count, self.dimension) * (upper - lower) + lower
80
81 def _internal_ask(self, base_ind: IndividualLike) -> IndividualLike:
82 if self._nb_suggestions >= len(self.sobol_vect):
83 count = max(self._nb_suggestions + 1, 2 * len(self.sobol_vect), self.budget or 0)
84 self._generate_vectors(count)
85 base_ind[:] = self.sobol_vect[self._nb_suggestions]
86 return base_ind
87
88
89
90# MODELINE "{{{1
91# vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
92# vim:foldmethod=marker
IndividualLike _internal_ask(self, IndividualLike base_ind)
Definition search.py:54
IndividualLike _internal_ask(self, IndividualLike base_ind)
Definition search.py:81
None _generate_vectors(self, int count)
Build an origin-free sequence prefix for the current genome bounds.
Definition search.py:77