culebra.solution.feature_selection.Ant class

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

Construct a default solution.

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

  • fitness (Fitness) – The solution’s fitness class

  • features (Sequence of int) – Initial features

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

  • TypeError – If fitness_cls is not a valid fitness class

Class attributes

Ant.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 Ant.load_pickle(filename: str) Base

Load a pickled object from a file.

Parameters:

filename (str) – The file name.

Raises:

Properties

property Ant.species: Species

Get the solution’s species.

Returns:

The species

Return type:

Species

property Ant.fitness: Fitness

Get and set the solution’s fitness.

Getter:

Return the current fitness

Setter:

Set a new Fitness

Type:

Fitness

property Ant.path: Sequence[int]

Path traveled by the ant.

Type:

Sequence of int

property Ant.current: int

Return the current node in the path.

Type:

int

property Ant.discarded: Sequence[int]

Nodes discarded by the ant.

Type:

Sequence of int

property Ant.features: Sequence[int]

Get and set the features path traveled by the ant.

Getter:

Return an ordered sequence with the indices of the selected features. Use path to get the path in the order the ant traveled it

Setter:

Set a new path. An array-like object of feature indices is expected

Type:

Sequence of int

Raises:

ValueError – If any feature in the path does not meet the species constraints.

property Ant.num_feats: int

Get the number of features selected by the solution.

Type:

int

property Ant.min_feat: int | None

Minimum feature index selected by the solution.

Type:

int or None if no feature has been selected

property Ant.max_feat: int | None

Maximum feature index selected by the solution.

Type:

int or None if no feature has been selected

Methods

Ant.save_pickle(filename: str) None

Pickle this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
Ant.append(feature: int) None

Append a new feature to the ant’s path.

Raises:
  • TypeError – If feature is not an integer number

  • ValueError – If feature does not meet the species constraints.

  • ValueError – If feature is already in the path or has been previously discarded

Ant.discard(feature: int) None

Discard a feature.

The discarded feature is not appended to the ant’s path.

Raises:
  • TypeError – If feature is not an integer number

  • ValueError – If feature is already in the path or has been previously discarded

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

Ant.delete_fitness() None

Delete the solution’s fitness.

Private methods

Ant._setup() None

Set the default path for ants.

An empty path is set.

Dunder methods

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

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

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

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

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

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

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

Ant.__str__() str

Return the solution as a string.