culebra.solution.parameter_optimization.Solution class

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

Bases: Solution

Construct a default solution.

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

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

  • values (Sequence[int | float]) – Initial values, optional

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

  • TypeError – If fitness_cls is not a valid fitness class

Class attributes

Solution.species_cls = <class 'culebra.solution.parameter_optimization.Species'>

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

Class methods

classmethod Solution.load(filename: str) Base

Load a serialized object from a file.

Parameters:

filename (str) – The file name.

Returns:

The loaded object

Raises:

Properties

property Solution.fitness: Fitness

Solution’s fitness.

Return type:

Fitness

Setter:

Set a new Fitness

Parameters:

value (Fitness) – The new fitness

property Solution.named_values_cls: type[NamedTuple]

Named tuple class to hold the parameter values.

Return type:

type[NamedTuple]

property Solution.species: Species

Solution’s species.

Return type:

Species

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

Parameter values evolved by the solution.

Return type:

named_values_cls

Setter:

Set new parameter values.

Parameters:

values (Sequence[int | float]) – The new parameters values

Raises:

ValueError – If the values do not meet the species constraints

Methods

Solution.dump(filename: str) None

Serialize this object and save it to a file.

Parameters:

filename (str) – The file name.

Raises:
Solution.get(name: str) int | float

Get the value of the parameter with the given name.

Parameters:

name (str) – Name of the parameter

Returns:

The value of the parameter

Return type:

int | float

Private methods

Solution._setup() None

Init the values of this solution randomly.

Dunder methods

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

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

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

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

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

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

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

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

Solution.__str__() str

Solution as a string.

Return type:

str