Coverage for moptipy / operators / vectors / op0_uniform.py: 95%
21 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-24 08:49 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-11-24 08:49 +0000
1"""A nullary operator filling a vector with uniformly distribute values."""
3from typing import Final
5import numpy as np
6from numpy.random import Generator
7from pycommons.types import type_error
9from moptipy.api.operators import Op0
10from moptipy.spaces.vectorspace import VectorSpace
11from moptipy.utils.logger import KeyValueLogSection
14class Op0Uniform(Op0):
15 """Fill a vector with uniformly distributed random values."""
17 def __init__(self, space: VectorSpace) -> None:
18 """
19 Initialize the operator.
21 :param space: the vector space
22 """
23 super().__init__()
24 if not isinstance(space, VectorSpace):
25 raise type_error(space, "space", VectorSpace)
26 #: store the space
27 self.space: Final[VectorSpace] = space
29 def __str__(self) -> str:
30 """
31 Get the name of this operator.
33 :returns: the name of this space
34 """
35 return "uniform"
37 def op0(self, random: Generator, dest: np.ndarray) -> None:
38 """
39 Fill the string `dest` with random values.
41 :param random: the random number generator
42 :param dest: the vector to be filled. Afterwards it contains uniformly
43 distributed random values
44 """
45 sp: Final[VectorSpace] = self.space
46 np.copyto(dest, random.uniform(
47 sp.lower_bound, sp.upper_bound, sp.dimension))
49 def log_parameters_to(self, logger: KeyValueLogSection) -> None:
50 """
51 Log the parameters of this operator to the given logger.
53 :param logger: the logger for the parameters
54 """
55 super().log_parameters_to(logger)
56 self.space.log_bounds(logger)