Population package
This package contains the building blocks that create the particle and the swarm objects.
Submodules
star_pso.population.jat_particle module
- class star_pso.population.jat_particle.JatParticle(container: list[DataBlock], value: float = -inf, best_value: float = -inf)[source]
Bases:
objectDescription:
Implements a dataclass for the ‘Jack of all trades’ particle. This dataclass maintains a container (list of data blocks) and the rest of the information is held in its data blocks.
- property best_position: list
Accessor of the particle’s best position.
- Returns:
a list with each data block best position.
- property best_value: float
Accessor of the best function value.
- Returns:
(float) best function value.
- property container: list[DataBlock]
Accessor of the container list of the particle.
- Returns:
the list (of data blocks) of the particle.
- property position: list
Accessor of the particle’s position.
- Returns:
a list with each data block position.
- reset_position() None[source]
Iterate through all the data blocks in the container and reset their positions.
- Returns:
None.
- property size: int
Returns the size (or length) of the particle.
Since the size of the container doesn’t change dynamically we can cache this value for faster retrieval.
- Returns:
(int) the length of the particle.
- property value: float
Accessor of the current function value.
- Returns:
(float) function value.
star_pso.population.particle module
- class star_pso.population.particle.Particle(initial_position: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes])[source]
Bases:
object- Description:
Models the particle in the swarm of the PSO.
- property best_position: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]
Returns the best (so far) position of the particle object.
- Returns:
(ArrayLike) position vector.
- property best_value: float
Accessor of the best function value recorded.
- Returns:
(float) best function value.
- property position: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]
Accessor of the positions in the particle.
- Returns:
the ArrayLike (vector) of the positions.
- property size: int
Returns the size (or length) of the particle.
- Returns:
(int) the length of the particle.
- property value: float
Accessor of the current function value.
- Returns:
(float) function value.
star_pso.population.swarm module
- class star_pso.population.swarm.Swarm(population: list[Particle | JatParticle], copy: bool = False)[source]
Bases:
objectDescription:
Implements a dataclass for the Swarm entity. This class is responsible for holding and organizing the individual solutions (i.e. particles), of the optimization problem during the optimization process.
- best_n(n: int = 1) list[Particle | JatParticle][source]
Auxiliary method that returns the best ‘n’ particles with the highest objective function value.
- Parameters:
n – the number of the best chromosomes.
- Returns:
Return a list with the ‘n’ top particles.
- best_particle() Particle | JatParticle[source]
Auxiliary method that returns the particle with the highest function value. Safeguard with ignoring NaNs.
- Returns:
Return the particle with the highest value.
- best_positions_as_array() ndarray[Any, dtype[_ScalarType_co]][source]
Get the particle best positions of all the swarm.
- Returns:
A numpy array with all the best positions.
- function_values() ndarray[Any, dtype[_ScalarType_co]][source]
Get the objectives function values of all the swarm.
- Returns:
A numpy array (vector) with all the values.
- property global_best_index: int
Accessor of the global best index.
- Returns:
the index (int) of the best.
- property has_categorical: bool
Accessor (getter) of the ‘has_categorical’ flag.
- Returns:
true if the data block is CATEGORICAL.
- property population: list[Particle | JatParticle]
Accessor of the population list of the swarm.
- Returns:
the list (of particles) of the swarm.
- position_at(index: int) _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes][source]
Get the position vector of an individual particle of the swarm.
- Parameters:
index – Position of the individual in the population.
- Returns:
The position vector (array).
- positions_as_array() ndarray[Any, dtype[_ScalarType_co]][source]
Get the particle positions of all the swarm.
- Returns:
A numpy array with all the positions.
- positions_as_list() list[source]
Get the particle positions of all the swarm.
- Returns:
A list with all the positions.
- set_positions(new_positions: ndarray[Any, dtype[_ScalarType_co]]) None[source]
Sets the positions of the particles in the swarm.
- Parameters:
new_positions – (NDArray) the new positions of the particles.
- Returns:
None.
- property size: int
Returns the size (length) of the population. Note that the size of the population is not expected to change during the optimization.
- Returns:
(int) the length of the population.