Skip to content

grid

Grid

Bases: Designs

Super class for grid designs.

Attributes:

Name Type Description
k int

The number of factors (dimension).

seed int

The seed for the random number generator.

Source code in spotpython/design/grid.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Grid(Designs):
    """
    Super class for grid designs.

    Attributes:
        k (int): The number of factors (dimension).
        seed (int): The seed for the random number generator.
    """

    def __init__(self, k: int = 2, seed: int = 123) -> None:
        """
        Initializes a grid design object.

        Args:
            k (int): The number of factors. Defaults to 2.
            seed (int): The seed for the random number generator. Defaults to 123.
        """
        super().__init__(k, seed)
        self.k = k
        self.seed = seed

    def generate_grid_design(self, points_per_dim: int) -> np.ndarray:
        """Generates a regular grid design.

        Args:
            points_per_dim (int): The number of points per dimension.

        Returns:
            numpy.ndarray: A 2D array of shape (points_per_dim^n_dim, n_dim) with grid points.

        Examples:
            >>> from spotpython.design.grid import Grid
            >>> grid_design = Grid(k=2)
            >>> grid_points = grid_design.generate_grid_design(points_per_dim=5)
            >>> print(grid_points)
            [[0.  0. ]
             [0.  0.25]
             [0.  0.5 ]
             ...
             [1.   1. ]]

        """
        if self.k != 2:
            raise ValueError("Grid design currently implemented for 2D only for simplicity.")
        ticks = np.linspace(0, 1, points_per_dim, endpoint=True)
        x, y = np.meshgrid(ticks, ticks)
        return np.vstack([x.ravel(), y.ravel()]).T

__init__(k=2, seed=123)

Initializes a grid design object.

Parameters:

Name Type Description Default
k int

The number of factors. Defaults to 2.

2
seed int

The seed for the random number generator. Defaults to 123.

123
Source code in spotpython/design/grid.py
14
15
16
17
18
19
20
21
22
23
24
def __init__(self, k: int = 2, seed: int = 123) -> None:
    """
    Initializes a grid design object.

    Args:
        k (int): The number of factors. Defaults to 2.
        seed (int): The seed for the random number generator. Defaults to 123.
    """
    super().__init__(k, seed)
    self.k = k
    self.seed = seed

generate_grid_design(points_per_dim)

Generates a regular grid design.

Parameters:

Name Type Description Default
points_per_dim int

The number of points per dimension.

required

Returns:

Type Description
ndarray

numpy.ndarray: A 2D array of shape (points_per_dim^n_dim, n_dim) with grid points.

Examples:

>>> from spotpython.design.grid import Grid
>>> grid_design = Grid(k=2)
>>> grid_points = grid_design.generate_grid_design(points_per_dim=5)
>>> print(grid_points)
[[0.  0. ]
 [0.  0.25]
 [0.  0.5 ]
 ...
 [1.   1. ]]
Source code in spotpython/design/grid.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def generate_grid_design(self, points_per_dim: int) -> np.ndarray:
    """Generates a regular grid design.

    Args:
        points_per_dim (int): The number of points per dimension.

    Returns:
        numpy.ndarray: A 2D array of shape (points_per_dim^n_dim, n_dim) with grid points.

    Examples:
        >>> from spotpython.design.grid import Grid
        >>> grid_design = Grid(k=2)
        >>> grid_points = grid_design.generate_grid_design(points_per_dim=5)
        >>> print(grid_points)
        [[0.  0. ]
         [0.  0.25]
         [0.  0.5 ]
         ...
         [1.   1. ]]

    """
    if self.k != 2:
        raise ValueError("Grid design currently implemented for 2D only for simplicity.")
    ticks = np.linspace(0, 1, points_per_dim, endpoint=True)
    x, y = np.meshgrid(ticks, ticks)
    return np.vstack([x.ravel(), y.ravel()]).T