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
23
from
typing
import
Optional, Tuple, List, Iterable, Iterator, Any, TypeVar, Generic, Union, Sequence, MutableSet, MutableSequence, Type, Callable, Generator, Mapping, MutableMapping, overload
24
import
numpy
as
np
25
import
random
26
27
28
from
.base
import
*
29
from
qdpy.utils
import
*
30
from
qdpy.phenotype
import
*
31
from
qdpy.base
import
*
32
from
qdpy.containers
import
*
33
from
qdpy
import
tools
34
35
import
qdpy.hdsobol
as
hdsobol
36
37
38
@registry.register
39
class
RandomUniform
(
QDAlgorithm
):
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
62
class
Sobol
(
RandomUniform
):
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
qdpy.algorithms.base.QDAlgorithmLike.dimension
Optional dimension
Definition
base.py:190
qdpy.algorithms.base.QDAlgorithm
Definition
base.py:244
qdpy.algorithms.base.QDAlgorithm._nb_suggestions
int _nb_suggestions
Definition
base.py:354
qdpy.algorithms.base.QDAlgorithm.budget
budget
Definition
base.py:303
qdpy.algorithms.search.RandomUniform
Definition
search.py:39
qdpy.algorithms.search.RandomUniform.ind_domain
ind_domain
Definition
search.py:50
qdpy.algorithms.search.RandomUniform._internal_ask
IndividualLike _internal_ask(self, IndividualLike base_ind)
Definition
search.py:54
qdpy.algorithms.search.Sobol
Definition
search.py:62
qdpy.algorithms.search.Sobol.sobol_vect
sobol_vect
Definition
search.py:79
qdpy.algorithms.search.Sobol._internal_ask
IndividualLike _internal_ask(self, IndividualLike base_ind)
Definition
search.py:81
qdpy.algorithms.search.Sobol._generate_vectors
None _generate_vectors(self, int count)
Build an origin-free sequence prefix for the current genome bounds.
Definition
search.py:77
qdpy.base
Definition
base.py:1
qdpy.containers
Definition
__init__.py:1
qdpy.hdsobol
Definition
__init__.py:1
qdpy.phenotype
Definition
phenotype.py:1
qdpy.utils
Definition
utils.py:1
qdpy
algorithms
search.py
Generated by
1.15.0