@File : solution.py @Time : 2024/06/07 14:09:54 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2024, Alejandro Marrero @Desc : None

Solution

Class representing a solution in a genetic algorithm. It contains the chromosome, objectives, constraints, and fitness of the solution.

Source code in digneapy/_core/_solution.py
 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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class Solution:
    """
    Class representing a solution in a genetic algorithm.
    It contains the chromosome, objectives, constraints, and fitness of the solution.
    """

    def __init__(
        self,
        chromosome: Optional[Iterable] = None,
        objectives: Optional[Iterable] = None,
        constraints: Optional[Iterable] = None,
        fitness: float = 0.0,
    ):
        """Creates a new solution object.
        The chromosome is a numpy array of the solution's genes.
        The objectives and constraints are numpy arrays of the solution's objectives and constraints.
        The fitness is a float representing the solution's fitness value.

        Args:
            chromosome (Optional[Iterable], optional): Tuple or any other iterable with the chromosome/variables. Defaults to None.
            objectives (Optional[Iterable], optional): Tuple or any other iterable with the objectives values. Defaults to None.
            constraints (Optional[Iterable], optional): Tuple or any other iterable with the constraint values. Defaults to None.
            fitness (float, optional): Fitness of the solution. Defaults to 0.0.
        """
        if chromosome is not None:
            self.chromosome = np.asarray(chromosome)
        else:
            self.chromosome = np.empty(0)
        self.objectives = np.array(objectives) if objectives else np.empty(0)
        self.constraints = np.array(constraints) if constraints else np.empty(0)
        self.fitness = fitness

    def clone(self) -> Self:
        """Returns a deep copy of the solution. It is more efficient than using the copy module.

        Returns:
            Self: Solution object
        """
        return Solution(
            chromosome=list(self.chromosome),
            objectives=list(self.objectives),
            constraints=list(self.constraints),
            fitness=self.fitness,
        )

    def __str__(self) -> str:
        return f"Solution(dim={len(self.chromosome)},f={self.fitness},objs={self.objectives},const={self.constraints})"

    def __repr__(self) -> str:
        return f"Solution<dim={len(self.chromosome)},f={self.fitness},objs={self.objectives},const={self.constraints}>"

    def __len__(self) -> int:
        return len(self.chromosome)

    def __iter__(self):
        return iter(self.chromosome)

    def __bool__(self):
        return len(self) != 0

    def __eq__(self, other) -> bool:
        if isinstance(other, Solution):
            try:
                return all(a == b for a, b in zip(self, other, strict=True))
            except ValueError:
                return False
        else:
            return NotImplemented

    def __gt__(self, other):
        if not isinstance(other, Solution):
            msg = f"Other of type {other.__class__.__name__} can not be compared with with {self.__class__.__name__}"
            print(msg)
            return NotImplemented
        return self.fitness > other.fitness

    def __getitem__(self, key):
        if isinstance(key, slice):
            cls = type(self)  # To facilitate subclassing
            return cls(self.chromosome[key])
        index = operator.index(key)
        return self.chromosome[index]

    def __setitem__(self, key, value):
        self.chromosome[key] = value

__init__(chromosome=None, objectives=None, constraints=None, fitness=0.0)

Creates a new solution object. The chromosome is a numpy array of the solution's genes. The objectives and constraints are numpy arrays of the solution's objectives and constraints. The fitness is a float representing the solution's fitness value.

Parameters:
  • chromosome (Optional[Iterable], default: None ) –

    Tuple or any other iterable with the chromosome/variables. Defaults to None.

  • objectives (Optional[Iterable], default: None ) –

    Tuple or any other iterable with the objectives values. Defaults to None.

  • constraints (Optional[Iterable], default: None ) –

    Tuple or any other iterable with the constraint values. Defaults to None.

  • fitness (float, default: 0.0 ) –

    Fitness of the solution. Defaults to 0.0.

Source code in digneapy/_core/_solution.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
def __init__(
    self,
    chromosome: Optional[Iterable] = None,
    objectives: Optional[Iterable] = None,
    constraints: Optional[Iterable] = None,
    fitness: float = 0.0,
):
    """Creates a new solution object.
    The chromosome is a numpy array of the solution's genes.
    The objectives and constraints are numpy arrays of the solution's objectives and constraints.
    The fitness is a float representing the solution's fitness value.

    Args:
        chromosome (Optional[Iterable], optional): Tuple or any other iterable with the chromosome/variables. Defaults to None.
        objectives (Optional[Iterable], optional): Tuple or any other iterable with the objectives values. Defaults to None.
        constraints (Optional[Iterable], optional): Tuple or any other iterable with the constraint values. Defaults to None.
        fitness (float, optional): Fitness of the solution. Defaults to 0.0.
    """
    if chromosome is not None:
        self.chromosome = np.asarray(chromosome)
    else:
        self.chromosome = np.empty(0)
    self.objectives = np.array(objectives) if objectives else np.empty(0)
    self.constraints = np.array(constraints) if constraints else np.empty(0)
    self.fitness = fitness

clone()

Returns a deep copy of the solution. It is more efficient than using the copy module.

Returns:
  • Self( Self ) –

    Solution object

Source code in digneapy/_core/_solution.py
52
53
54
55
56
57
58
59
60
61
62
63
def clone(self) -> Self:
    """Returns a deep copy of the solution. It is more efficient than using the copy module.

    Returns:
        Self: Solution object
    """
    return Solution(
        chromosome=list(self.chromosome),
        objectives=list(self.objectives),
        constraints=list(self.constraints),
        fitness=self.fitness,
    )