Skip to content

Commit

Permalink
fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Luunynliny committed Nov 18, 2023
1 parent bc511cc commit dbd80ac
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 232 deletions.
47 changes: 31 additions & 16 deletions genetic/crossover.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import numpy as np
from numpy import typing as npt


def one_point_crossover(
p1: npt.NDArray[np.float64], p2: npt.NDArray[np.float64]
) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]:
p1: np.ndarray[np.float64, np.dtype[np.float64]],
p2: np.ndarray[np.float64, np.dtype[np.float64]],
) -> tuple[
np.ndarray[np.float64, np.dtype[np.float64]],
np.ndarray[np.float64, np.dtype[np.float64]],
]:
"""Performs one-point crossover on two parent chromosomes and retuns two new child
chromosomes.
New chromosomes are normalized to fit requirements.
Args:
p1 (npt.NDArray[np.float64]): first parent chromosome.
p2 (npt.NDArray[np.float64]): second parent chromosome.
p1 (np.ndarray[np.float64, np.dtype[np.float64]]): first parent chromosome.
p2 (np.ndarray[np.float64, np.dtype[np.float64]]): second parent chromosome.
Returns:
tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]: child chromosomes.
tuple[np.ndarray[np.float64, np.dtype[np.float64]], np.ndarray[np.float64,
np.dtype[np.float64]]]: child chromosomes.
"""
rng = np.random.default_rng()

Expand All @@ -28,19 +32,24 @@ def one_point_crossover(


def two_point_crossover(
p1: npt.NDArray[np.float64], p2: npt.NDArray[np.float64]
) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]:
p1: np.ndarray[np.float64, np.dtype[np.float64]],
p2: np.ndarray[np.float64, np.dtype[np.float64]],
) -> tuple[
np.ndarray[np.float64, np.dtype[np.float64]],
np.ndarray[np.float64, np.dtype[np.float64]],
]:
"""Performs two-point crossover on two parent chromosomes and retuns two new child
chromosomes.
New chromosomes are normalized to fit requirements.
Args:
p1 (npt.NDArray[np.float64]): first parent chromosome.
p2 (npt.NDArray[np.float64]): second parent chromosome.
p1 (np.ndarray[np.float64, np.dtype[np.float64]]): first parent chromosome.
p2 (np.ndarray[np.float64, np.dtype[np.float64]]): second parent chromosome.
Returns:
tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]: child chromosomes.
tuple[np.ndarray[np.float64, np.dtype[np.float64]], np.ndarray[np.float64,
np.dtype[np.float64]]]: child chromosomes.
"""
rng = np.random.default_rng()

Expand All @@ -56,20 +65,26 @@ def two_point_crossover(


def uniform_crossover(
p1: npt.NDArray[np.float64], p2: npt.NDArray[np.float64], p_swap: float
) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]:
p1: np.ndarray[np.float64, np.dtype[np.float64]],
p2: np.ndarray[np.float64, np.dtype[np.float64]],
p_swap: float,
) -> tuple[
np.ndarray[np.float64, np.dtype[np.float64]],
np.ndarray[np.float64, np.dtype[np.float64]],
]:
"""Performs uniform crossover on two parent chromosomes and retuns two new child
chromosomes.
New chromosomes are normalized to fit requirements.
Args:
p1 (npt.NDArray[np.float64]): first parent chromosome.
p2 (npt.NDArray[np.float64]): second parent chromosome.
p1 (np.ndarray[np.float64, np.dtype[np.float64]]): first parent chromosome.
p2 (np.ndarray[np.float64, np.dtype[np.float64]]): second parent chromosome.
p_swap(float): probability of gene swapping.
Returns:
tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]: child chromosomes.
tuple[np.ndarray[np.float64, np.dtype[np.float64]], np.ndarray[np.float64,
np.dtype[np.float64]]]: child chromosomes.
"""
rng = np.random.default_rng()

Expand Down
Loading

0 comments on commit dbd80ac

Please sign in to comment.