culebra.solution.abc.Individual class

class Individual(species: Species, fitness_cls: type[Fitness])

Bases: Solution

Construct a default solution.

Parameters:
  • species (Species) – The species the solution will belong to

  • fitness_cls (type[Fitness]) – The solutions’s fitness class

Raises:
  • TypeError – If species is not a valid species

  • TypeError – If fitness_cls is not a valid fitness class

Class attributes

Individual.species_cls = <class 'culebra.abc.Species'>

Class for the species used by the Solution class to constrain all its instances.

Class methods

classmethod Individual.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property Individual.fitness: Fitness

Solution’s fitness.

Return type:

Fitness

Setter:

Set a new Fitness

Parameters:

value (Fitness) – The new fitness

property Individual.species: Species

Solution’s species.

Return type:

Species

Methods

abstract Individual.crossover(other: Individual) tuple[Individual, Individual]

Cross this individual with another one.

This method must be overridden by subclasses to return a correct value.

Parameters:

other (Individual) – The other individual

Returns:

The two offspring

Return type:

tuple[Individual]

Raises:

NotImplementedError – If has not been overridden

Individual.delete_fitness() None

Delete the solution’s fitness.

Individual.dominates(other: Solution) bool

Dominate operator.

Parameters:

other (Solution) – Other solution

Returns:

True if each objective of the solution is not strictly worse than the corresponding objective of other and at least one objective is strictly better.

Return type:

bool

Individual.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
abstract Individual.mutate(indpb: float) tuple[Individual]

Mutate the individual.

This method must be overridden by subclasses to return a correct value.

Parameters:

indpb (float) – Independent probability for each gene to be mutated.

Returns:

The mutant

Return type:

tuple[Individual]

Raises:

NotImplementedError – If has not been overridden

Dunder methods

Intended to compare (lexicographically) two individuals according to their fitness.

Individual.__eq__(other: Solution) bool

Equality test.

Parameters:

other (Solution) – Other solution

Returns:

True if other codes the same solution, or False otherwise

Return type:

bool

Individual.__ge__(other: Solution) bool

Greater than or equal to operator.

Parameters:

other (Solution) – Other solution

Returns:

True if the solution’s fitness is greater than or equal to the other’s fitness

Return type:

bool

Individual.__gt__(other: Solution) bool

Greater than operator.

Parameters:

other (Solution) – Other solution

Returns:

True if the solution’s fitness is greater than the other’s fitness

Return type:

bool

Individual.__hash__() int

Return the hash number for this solution.

The hash number is used for equality comparisons. Currently is implemented as the hash of the solution’s string representation.

Return type:

int

Individual.__le__(other: Solution) bool

Less than or equal to operator.

Parameters:

other (Solution) – Other solution

Returns:

True if the solution’s fitness is less than or equal to the other’s fitness

Return type:

bool

Individual.__lt__(other: Solution) bool

Less than operator.

Parameters:

other (Solution) – Other solution

Returns:

True if the solution’s fitness is less than the other’s fitness

Return type:

bool

Individual.__ne__(other: Solution) bool

Not equality test.

Parameters:

other (Solution) – Other solution

Returns:

False if other codes the same solutions, or True otherwise

Return type:

bool