culebra.solution.tsp.Ant class

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

Bases: Solution, Ant

Construct a default solution.

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

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

  • path (Sequence[int]) – Initial path

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.tsp.Species'>

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

Class methods

classmethod Ant.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property Ant.current: int

Current node in the path.

Return type:

int

property Ant.discarded: ndarray[int]

Nodes discarded by the ant.

Return an empty array since all nodes must be visited for the TSP problem.

Return type:

ndarray[int]

property Ant.fitness: Fitness

Solution’s fitness.

Return type:

Fitness

Setter:

Set a new Fitness

Parameters:

value (Fitness) – The new fitness

property Ant.path: ndarray[int]

Path.

Return type:

ndarray[int]

Setter:

Set a new path

Parameters:

value (Sequence[int]) – The new path

Raises:

ValueError – If value does not meet the species constraints.

property Ant.species: Species

Solution’s species.

Return type:

Species

Methods

Ant.append(node: int) None

Append a new node to the ant’s path.

Parameters:

node (int) – The node

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

  • ValueError – If node does not meet the species constraints

  • ValueError – If node is already in the path.

Ant.delete_fitness() None

Delete the solution’s fitness.

Ant.discard(node: int) None

Discard a node.

This method raises an exception since nodes can not be discarded for the TSP problem.

Parameters:

node (int) – The node

Raises:

RuntimeError – If called

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.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:

Private methods

Ant._setup() None

Set the default path for ants.

An empty path is set.

Dunder methods

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

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

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.__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.__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.__str__() str

Solution as a string.

A symmetric tsp problem is assumed. Thus, all rotations of paths [0, 1, …, n] and [n, …, 1, 0] are considered the same path.

The path is rolled to start with the node with smallest index.

Return type:

str