porcupy.utils.population_visualization
Population dynamics visualization module for the Crested Porcupine Optimizer (CPO).
This module provides specialized visualization tools for the population dynamics of the CPO algorithm, including cyclic population reduction and diversity visualization.
Functions
|
Plot and compare different population reduction strategies. |
|
Create a grid of plots showing population diversity at different iterations. |
|
Create an animation showing population dynamics throughout cycles. |
|
Plot the balance between exploration and exploitation at different iterations. |
|
Plot the relationship between population diversity and convergence. |
|
Calculate the diversity of a population based on average pairwise distance. |
Module Contents
- porcupy.utils.population_visualization.plot_population_reduction_strategies(max_iter: int, pop_size: int, cycles: int, strategies: List[str] = ['linear', 'cosine', 'exponential'], figsize: Tuple[int, int] = (12, 6), save_path: str | None = None)[source]
Plot and compare different population reduction strategies.
- Parameters:
max_iter (int) – Maximum number of iterations.
pop_size (int) – Initial population size.
cycles (int) – Number of cycles.
strategies (list, optional) – List of reduction strategies to compare (default: [‘linear’, ‘cosine’, ‘exponential’]).
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (12, 6)).
save_path (str, optional) – Path to save the figure. If None, the figure is not saved (default: None).
- Returns:
The created figure.
- Return type:
- porcupy.utils.population_visualization.plot_population_diversity_map(positions_history: List[numpy.ndarray], bounds: Tuple[numpy.ndarray, numpy.ndarray], sample_iterations: List[int], figsize: Tuple[int, int] = (15, 10), cmap: str = 'viridis', save_path: str | None = None)[source]
Create a grid of plots showing population diversity at different iterations.
- Parameters:
positions_history (list) – List of position arrays at each iteration, each with shape (pop_size, 2).
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
sample_iterations (list) – List of iteration indices to visualize.
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (15, 10)).
cmap (str, optional) – Colormap for the density plot (default: ‘viridis’).
save_path (str, optional) – Path to save the figure. If None, the figure is not saved (default: None).
- Returns:
The created figure.
- Return type:
- porcupy.utils.population_visualization.animate_population_cycle(positions_history: List[numpy.ndarray], pop_size_history: List[int], bounds: Tuple[numpy.ndarray, numpy.ndarray], max_iter: int, cycles: int, interval: int = 200, figsize: Tuple[int, int] = (12, 8), cmap: str = 'viridis', save_path: str | None = None, dpi: int = 100)[source]
Create an animation showing population dynamics throughout cycles.
- Parameters:
positions_history (list) – List of position arrays at each iteration, each with shape (pop_size, 2).
pop_size_history (list) – List of population sizes at each iteration.
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
max_iter (int) – Maximum number of iterations.
cycles (int) – Number of cycles.
interval (int, optional) – Interval between frames in milliseconds (default: 200).
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (12, 8)).
cmap (str, optional) – Colormap for the density plot (default: ‘viridis’).
save_path (str, optional) – Path to save the animation. If None, the animation is not saved (default: None).
dpi (int, optional) – DPI for the saved animation (default: 100).
- Returns:
The created animation.
- Return type:
- porcupy.utils.population_visualization.plot_exploration_exploitation_balance(positions_history: List[numpy.ndarray], best_positions_history: List[numpy.ndarray], bounds: Tuple[numpy.ndarray, numpy.ndarray], sample_iterations: List[int], figsize: Tuple[int, int] = (15, 10), save_path: str | None = None)[source]
Plot the balance between exploration and exploitation at different iterations.
- Parameters:
positions_history (list) – List of position arrays at each iteration, each with shape (pop_size, 2).
best_positions_history (list) – List of best position arrays at each iteration, each with shape (2,).
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
sample_iterations (list) – List of iteration indices to visualize.
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (15, 10)).
save_path (str, optional) – Path to save the figure. If None, the figure is not saved (default: None).
- Returns:
The created figure.
- Return type:
- porcupy.utils.population_visualization.plot_diversity_vs_convergence(diversity_history: List[float], fitness_history: List[float], cycles: int, max_iter: int, title: str = 'Diversity vs Convergence', figsize: Tuple[int, int] = (12, 6), save_path: str | None = None)[source]
Plot the relationship between population diversity and convergence.
- Parameters:
diversity_history (list) – List of diversity measures at each iteration.
fitness_history (list) – List of best fitness values at each iteration.
cycles (int) – Number of cycles.
max_iter (int) – Maximum number of iterations.
title (str, optional) – Title of the plot (default: “Diversity vs Convergence”).
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (12, 6)).
save_path (str, optional) – Path to save the figure. If None, the figure is not saved (default: None).
- Returns:
The created figure.
- Return type:
- porcupy.utils.population_visualization.calculate_diversity(positions: numpy.ndarray) float[source]
Calculate the diversity of a population based on average pairwise distance.
- Parameters:
positions (ndarray) – Positions of the porcupines, shape (pop_size, dimensions).
- Returns:
Diversity measure.
- Return type: