culebra.solution.feature_selection.BitVector class

class BitVector(species: Species, fitness_cls: type[Fitness], features: Sequence[int] | None = None)

Bases: BinarySolution, Individual

Construct a default solution.

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

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

  • features (Sequence[int]) – Initial features

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

  • TypeError – If fitness_cls is not a valid fitness class

Class attributes

BitVector.species_cls = <class 'culebra.solution.feature_selection.Species'>

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

Class methods

classmethod BitVector.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property BitVector.features: ndarray[int]

Features selected by the solution

Returns:

Return an ordered sequence with the indices of the selected features

Return type:

ndarray[int]

Setter:

Set a new set of selected features

Parameters:

values (Sequence[int]) – The indices of the new features

Raises:

ValueError – If set to new feature indices values which do not meet the species constraints.

property BitVector.fitness: Fitness

Solution’s fitness.

Return type:

Fitness

Setter:

Set a new Fitness

Parameters:

value (Fitness) – The new fitness

property BitVector.max_feat: int | None

Maximum feature index selected by the solution.

Returns:

The maximum feature index or None if no feature has been selected

Return type:

int

property BitVector.min_feat: int | None

Minimum feature index selected by the solution.

Returns:

The minimum feature index or None if no feature has been selected

Return type:

int

property BitVector.num_feats: int

Number of features selected by the solution.

Return type:

int

property BitVector.species: Species

Solution’s species.

Return type:

Species

Methods

BitVector.crossover(other: BitVector) tuple[BitVector, BitVector]

Default crossover operator.

Implemented as the single-point crossover.

Parameters:

other (BitVector) – The other individual

Returns:

The two offspring

Return type:

tuple[BitVector]

BitVector.crossover1p(other: BitVector) tuple[BitVector, BitVector]

Cross this individual with another one.

This method implements the single-point crossover.

Parameters:

other (BitVector) – The other individual

Returns:

The two offspring

Return type:

tuple[BitVector]

BitVector.crossover2p(other: BitVector) tuple[BitVector, BitVector]

Cross this individual with another one.

This method implements the two-points crossover.

Parameters:

other (BitVector) – The other individual

Returns:

The two offspring

Return type:

tuple[BitVector]

BitVector.delete_fitness() None

Delete the solution’s fitness.

BitVector.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

BitVector.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
BitVector.mutate(indpb: float) tuple[BitVector]

Mutate the individual.

Each feature is independently mutated according to the given probability.

Parameters:

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

Returns:

The mutant

Return type:

tuple[BitVector]

Private methods

BitVector._setup() None

Init the features of this solution randomly.

Dunder methods

Intended to compare (lexicographically) two bit vectors according to their fitness.

BitVector.__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

BitVector.__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

BitVector.__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

BitVector.__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

BitVector.__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

BitVector.__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

BitVector.__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

BitVector.__str__() str

Solution as a string.

Return type:

str