@File : init.py @Time : 2023/11/03 10:33:37 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2023, Alejandro Marrero @Desc : None

batch_uniform_one_mutation(population, lb, ub)

Performs uniform one mutation in batches

Parameters:
  • population (ndarray) –

    Batch of individuals to mutate

  • lb (ndarray) –

    Lower bound for each dimension

  • up (ndarray) –

    Upper bound for each dimension

Raises:
  • ValueError

    If the dimension of the individuals do not match the bounds

Returns:
  • ndarray

    np.ndarray: mutated population

Source code in digneapy/operators/_mutation.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def batch_uniform_one_mutation(
    population: np.ndarray, lb: np.ndarray, ub: np.ndarray
) -> np.ndarray:
    """Performs uniform one mutation in batches

    Args:
        population (np.ndarray): Batch of individuals to mutate
        lb (np.ndarray): Lower bound for each dimension
        up (np.ndarray): Upper bound for each dimension

    Raises:
        ValueError: If the dimension of the individuals do not match the bounds

    Returns:
        np.ndarray: mutated population
    """
    dimension = len(population[0])
    n_individuals = len(population)
    if len(lb) != len(ub) or dimension != len(lb):
        msg = f"The size of individuals ({dimension}) and bounds {len(lb)} is different in uniform_one_mutation"
        raise ValueError(msg)

    rng = np.random.default_rng(84793258734753)
    mutation_points = rng.integers(low=0, high=dimension, size=n_individuals)
    new_values = rng.uniform(
        low=lb[mutation_points], high=ub[mutation_points], size=n_individuals
    )

    population[np.arange(n_individuals), mutation_points] = new_values

    return population

binary_tournament_selection(population)

Binary Tournament Selection Operator

Parameters:
  • population (Sequence) –

    Population of individuals to select a parent from

Raises:
  • RuntimeError

    If the population is empty

Returns:
  • IndType

    Instance or Solution: New parent

Source code in digneapy/operators/_selection.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def binary_tournament_selection(population: Sequence[IndType]) -> IndType:
    """Binary Tournament Selection Operator

    Args:
        population (Sequence): Population of individuals to select a parent from

    Raises:
        RuntimeError: If the population is empty

    Returns:
        Instance or Solution: New parent
    """
    if not population:
        msg = "Trying to selection individuals in an empty population."
        raise ValueError(msg)
    elif len(population) == 1:
        return population[0]
    else:
        idx1, idx2 = np.random.default_rng().integers(
            low=0, high=len(population), size=2
        )
        return max(population[idx1], population[idx2], key=attrgetter("fitness"))

elitist_replacement(current_population, offspring, hof=1)

Returns a new population constructed using the Elitist approach. HoF number of individuals from the current + offspring populations are kept in the new population. The remaining individuals are selected from the offspring population.

Parameters:
  • current_population Sequence[IndType],

    Current population in the algorithm

  • offspring Sequence[IndType],

    Offspring population

  • hof (int, default: 1 ) –

    description. Defaults to 1.

Raises:
  • ValueError

    Raises if the sizes of the population are different

Returns:
  • list[IndType]

    list[IndType]:

Source code in digneapy/operators/_replacement.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def elitist_replacement(
    current_population: Sequence[IndType],
    offspring: Sequence[IndType],
    hof: int = 1,
) -> list[IndType]:
    """Returns a new population constructed using the Elitist approach.
    HoF number of individuals from the current + offspring populations are
    kept in the new population. The remaining individuals are selected from
    the offspring population.

    Args:
        current_population  Sequence[IndType],: Current population in the algorithm
        offspring  Sequence[IndType],: Offspring population
        hof (int, optional): _description_. Defaults to 1.

    Raises:
        ValueError: Raises if the sizes of the population are different

    Returns:
          list[IndType]:
    """
    if len(current_population) != len(offspring):
        msg = f"The size of the current population ({len(current_population)}) != size of the offspring ({len(offspring)}) in elitist_replacement"
        raise ValueError(msg)

    combined_population = sorted(
        itertools.chain(current_population, offspring),
        key=attrgetter("fitness"),
        reverse=True,
    )
    top = combined_population[:hof]
    return list(top + offspring[1:])

first_improve_replacement(current_population, offspring)

Returns a new population produced by a greedy operator. Each individual in the current population is compared with its analogous in the offspring population and the best survives

Parameters:
  • current_population (Sequence[IndType]) –

    Current population in the algorithm

  • offspring (Sequence[IndType]) –

    Offspring population

Raises:
  • ValueError

    Raises if the sizes of the population are different

Returns:
  • Sequence[IndType]

    Sequence[IndType]: New population

Source code in digneapy/operators/_replacement.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def first_improve_replacement(
    current_population: Sequence[IndType],
    offspring: Sequence[IndType],
) -> Sequence[IndType]:
    """Returns a new population produced by a greedy operator.
    Each individual in the current population is compared with its analogous in the offspring population
    and the best survives

    Args:
        current_population (Sequence[IndType]): Current population in the algorithm
        offspring (Sequence[IndType]): Offspring population

    Raises:
        ValueError: Raises if the sizes of the population are different

    Returns:
         Sequence[IndType]: New population
    """
    if len(current_population) != len(offspring):
        msg = f"The size of the current population ({len(current_population)}) != size of the offspring ({len(offspring)}) in first_improve_replacement"
        raise ValueError(msg)
    return [a if a > b else b for a, b in zip(current_population, offspring)]

generational_replacement(current_population, offspring)

Returns the offspring population as the new current population

Parameters:
  • current_population (Sequence[IndType]) –

    Current population in the algorithm

  • offspring (Sequence[IndType]) –

    Offspring population

Raises:
  • ValueError

    Raises if the sizes of the population are different

Returns:
  • Sequence[IndType]

    Sequence[IndType]: New population

Source code in digneapy/operators/_replacement.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def generational_replacement(
    current_population: Sequence[IndType],
    offspring: Sequence[IndType],
) -> Sequence[IndType]:
    """Returns the offspring population as the new current population

    Args:
        current_population (Sequence[IndType]): Current population in the algorithm
        offspring (Sequence[IndType]): Offspring population

    Raises:
        ValueError: Raises if the sizes of the population are different

    Returns:
         Sequence[IndType]: New population
    """
    if len(current_population) != len(offspring):
        msg = f"The size of the current population ({len(current_population)}) != size of the offspring ({len(offspring)}) in generational replacement"
        raise ValueError(msg)

    return offspring[:]

one_point_crossover(individual, other)

One point crossover

Parameters:
  • individual Instance or Solution

    First individual to apply crossover. Returned object

  • other Instance or Solution

    Second individual to apply crossover

Raises:
  • ValueError

    When the len(ind_1) != len(ind_2)

Returns:
  • IndType

    Instance or Solution: New individual

Source code in digneapy/operators/_crossover.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def one_point_crossover(individual: IndType, other: IndType) -> IndType:
    """One point crossover

    Args:
        individual Instance or Solution: First individual to apply crossover. Returned object
        other Instance or Solution: Second individual to apply crossover

    Raises:
        ValueError: When the len(ind_1) != len(ind_2)

    Returns:
        Instance or Solution: New individual
    """
    if len(individual) != len(other):
        msg = f"Individual of different length in uniform_crossover. len(ind) = {len(individual)} != len(other) = {len(other)}"
        raise ValueError(msg)

    offspring = individual.clone()
    cross_point = np.random.default_rng().integers(low=0, high=len(individual))
    offspring[cross_point:] = other[cross_point:]
    return offspring

uniform_crossover(individual, other, cxpb=np.float64(0.5))

Uniform Crossover Operator for Instances and Solutions

Parameters:
  • individual (IndType) –

    First individual to apply crossover. Returned object.

  • other (IndType) –

    Second individual to apply crossover

  • cxpb (float64, default: float64(0.5) ) –

    Crossover probability. Defaults to 0.5.

Raises:
  • ValueError

    When the len(ind_1) != len(ind_2)

Returns:
  • ndarray( IndType ) –

    New individual

Source code in digneapy/operators/_crossover.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def uniform_crossover(
    individual: IndType, other: IndType, cxpb: np.float64 = np.float64(0.5)
) -> IndType:
    """Uniform Crossover Operator for Instances and Solutions

    Args:
        individual (IndType): First individual to apply crossover. Returned object.
        other (IndType): Second individual to apply crossover
        cxpb (float64, optional): Crossover probability. Defaults to 0.5.

    Raises:
        ValueError: When the len(ind_1) != len(ind_2)

    Returns:
        ndarray: New individual
    """
    if len(individual) != len(other):
        msg = f"Individual of different length in uniform_crossover. len(ind) = {len(individual)} != len(other) = {len(other)}"
        raise ValueError(msg)

    probs = np.random.default_rng().random(size=len(individual))
    genotype = np.empty_like(individual)
    genotype = np.where(probs <= cxpb, individual, other)

    return individual.clone_with(variables=genotype)