topologicpy.GA module

class topologicpy.GA.GA(num_genes: ~typing.Optional[int] = None, gene_space: ~typing.Optional[~typing.Any] = None, fitness_function: ~typing.Optional[~typing.Callable[[~typing.Any, ~numpy.ndarray, int], ~typing.Union[int, float, ~typing.Sequence[~typing.Union[int, float]]]]] = None, *, sol_per_pop: int = 50, num_generations: int = 100, num_parents_mating: ~typing.Optional[int] = None, parent_selection_type: str = 'sss', keep_parents: int = 1, crossover_type: ~typing.Optional[str] = 'single_point', mutation_type: ~typing.Optional[str] = 'random', mutation_num_genes: ~typing.Optional[int] = None, mutation_percent_genes: ~typing.Union[int, str] = 'default', gene_type: ~typing.Optional[~typing.Union[type, ~typing.Sequence[type]]] = <class 'float'>, init_range_low: ~typing.Union[int, float] = -1.0, init_range_high: ~typing.Union[int, float] = 1.0, allow_duplicate_genes: bool = True, random_seed: ~typing.Optional[int] = None, stop_criteria: ~typing.Optional[~typing.Union[str, ~typing.List[str]]] = None, on_generation: ~typing.Optional[~typing.Callable[[~typing.Any], ~typing.Any]] = None, pygad_kwargs: ~typing.Optional[~typing.Dict[str, ~typing.Any]] = None, silent: bool = False)

Bases: object

A TopologicPy-style wrapper around PyGAD with ergonomic hyperparameters, result accessors, and Plotly Pareto visualizations.

Key ideas: - You provide a fitness function (maximization). PyGAD calls it as:

fitness(ga_instance, solution, solution_idx) -> float OR sequence of floats

(single-objective or multi-objective). :contentReference[oaicite:1]{index=1}

  • For multi-objective runs, set parent_selection_type=”nsga2” (or another MO selector).

  • This wrapper caches fitness, solutions, and convenient Pareto-front access.

Attributes
BestFitness
BestIndex
BestSolution
GA

Direct access to the underlying pygad.GA instance (after Build()).

Population

Population of the last generation (if available).

PopulationFitness

Fitness array for the last generation (if available).

Ran

Methods

Build()

Build the internal pygad.GA instance.

EnableCheckpointing(directory[, interval, ...])

Enable periodic checkpointing.

Load(filepath)

Load a saved PyGAD instance and attach it to this wrapper.

Params()

Return a shallow copy of current hyperparameters.

ParetoFront()

Returns (pareto_solutions, pareto_fitness) from the last generation.

ParetoFrontIndices([fitness])

Return indices of non-dominated solutions in the provided fitness array (or the cached last-generation fitness).

Results()

Returns a compact results dictionary designed to be easy to print/log/store.

ResumeFromLatestCheckpoint()

Load the latest checkpoint (if available) into this GA wrapper.

Run([target_generations])

Execute the genetic algorithm and cache commonly-used results.

Save(filepath)

Save the PyGAD instance (requires Build() or Run() beforehand).

SetParams(**kwargs)

Set/override hyperparameters before calling Run().

LatestCheckpoint

PlotParetoFront

property BestFitness: Optional[Union[int, float, Sequence[Union[int, float]]]]
property BestIndex: Optional[int]
property BestSolution: Optional[ndarray]
Build() GA

Build the internal pygad.GA instance. You normally don’t need to call this; Run() will call it if needed.

EnableCheckpointing(directory: str, interval: int = 5, keep_last: int = 5, prefix: str = 'ga_checkpoint', save_final: bool = True, atomic: bool = True, silent: bool = False) GA

Enable periodic checkpointing.

Parameters
directorystr

Directory where checkpoints will be written.

intervalint, optional

Save every N generations. Default is 5.

keep_lastint, optional

Keep only the most recent K checkpoints. Default is 5.

prefixstr, optional

File prefix. Default is “ga_checkpoint”.

save_finalbool, optional

If True, save a final checkpoint at the end of Run(). Default is True.

atomicbool, optional

If True, write to a temp file then move into place. Default is True.

silentbool, optional

If True, suppress checkpoint log messages. Default is False.

property GA: Any

Direct access to the underlying pygad.GA instance (after Build()).

LatestCheckpoint() str | None
Load(filepath: str) GA

Load a saved PyGAD instance and attach it to this wrapper.

Params() Dict[str, Any]

Return a shallow copy of current hyperparameters.

ParetoFront() Tuple[ndarray, ndarray]

Returns (pareto_solutions, pareto_fitness) from the last generation.

ParetoFrontIndices(fitness: Optional[ndarray] = None) ndarray

Return indices of non-dominated solutions in the provided fitness array (or the cached last-generation fitness). Assumes maximization objectives (PyGAD convention). :contentReference[oaicite:3]{index=3}

PlotParetoFront(*, title: str = 'Pareto Front', objective_names: list[str] = None, show_all_points: bool = True, show_pareto_points: bool = True, connect_pareto: bool = False, pareto_color: str = '#000000', population_color: str = '#B0B0B0', pareto_size: int = 8, population_size: int = 5, background_color: str = 'white', font_family: str = 'Arial', font_size: int = 14, width: int = 800, height: int = 600, show_grid: bool = True, grid_color: str = '#E6E6E6', grid_width: float = 1.0, show_legend: bool = True, max_points: int = None)
property Population: Optional[ndarray]

Population of the last generation (if available).

property PopulationFitness: Optional[ndarray]

Fitness array for the last generation (if available).

property Ran: bool
Results() Dict[str, Any]

Returns a compact results dictionary designed to be easy to print/log/store.

ResumeFromLatestCheckpoint() bool

Load the latest checkpoint (if available) into this GA wrapper.

Returns
bool

True if a checkpoint was found and loaded, False otherwise.

Run(target_generations: int = None) GARunSummary

Execute the genetic algorithm and cache commonly-used results.

Parameters
target_generationsint, optional

If provided, ensures the run continues until this TOTAL number of generations (useful when resuming from checkpoints). If current generations_completed is already >= target_generations, no evolution is run.

Save(filepath: str) None

Save the PyGAD instance (requires Build() or Run() beforehand).

SetParams(**kwargs) GA

Set/override hyperparameters before calling Run(). Example: ga.SetParams(num_generations=200, mutation_percent_genes=10)

class topologicpy.GA.GARunSummary(generations_completed: 'int', best_solution: 'Optional[np.ndarray]', best_fitness: 'Optional[Fitness]', best_index: 'Optional[int]')

Bases: object

best_fitness: Optional[Union[int, float, Sequence[Union[int, float]]]]
best_index: Optional[int]
best_solution: Optional[ndarray]
generations_completed: int