culebra.solution.parameter_optimization.Individual class

class Individual(species: Species, fitness_cls: Type[Fitness], values: Sequence[int | float] | None = None)

Construct a default solution.

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

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

  • values (Sequence of int or float values, optional) – Initial values

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.solution.parameter_optimization.Species'>

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

Individual.eta = 5

Default value for eta, which controls the probability distribution used in SBX and polynomial mutation.

Class methods

classmethod Individual.load_pickle(filename: str) Base

Load a pickled object from a file.

Parameters:

filename (str) – The file name.

Raises:

Properties

property Individual.species: Species

Get the solution’s species.

Returns:

The species

Return type:

Species

property Individual.fitness: Fitness

Get and set the solution’s fitness.

Getter:

Return the current fitness

Setter:

Set a new Fitness

Type:

Fitness

property Individual.named_values_cls: Type[NamedTuple]

Return the named tuple class to hold the parameter values.

property Individual.values: NamedTuple[int | float, ...]

Get and set the parameter values evolved by the solution.

Getter:

Return the parameter values

Setter:

Set the new parameter values.

Type:

named_values_cls

Raises:

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

Methods

Individual.save_pickle(filename: str) None

Pickle this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
Individual.crossover(other: Individual) Tuple[Individual, Individual]

Cross this individual with another one.

SBX is used.

Parameters:

other (Individual) – The other individual

Returns:

The two offspring

Return type:

tuple

Individual.mutate(indpb: float) Tuple[Individual]

Mutate the individual.

Polynimial mutation is used.

Parameters:

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

Returns:

The mutant

Return type:

tuple

Individual.get(name: str) int | float

Get the value of the parameter with the given name.

Parameters:

name (str) – Name of the parameter

Private methods

Individual._setup() None

Init the values of this solution randomly.

Dunder methods

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.

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

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

Return the solution as a string.