porcupy.utils.enhanced_visualization

Functions

plot_defense_mechanisms(defense_history[, title, ...])

Plot the activation frequency of each defense mechanism over iterations.

plot_population_cycles(pop_size_history, cycles, max_iter)

Plot the population size history with cycle boundaries highlighted.

plot_diversity_history(diversity_history[, title, ...])

Plot the diversity history of the population.

plot_2d_porcupines(positions, func, bounds[, ...])

Plot porcupines in 2D search space with quill-like directional indicators.

animate_porcupines_2d(position_history, func, bounds)

Create an enhanced animation of porcupines moving in 2D search space with additional visual feedback.

plot_3d_porcupines(positions, fitness, func, bounds[, ...])

Plot porcupines in 3D search space.

calculate_diversity(→ float)

Calculate the diversity of a population based on average pairwise distance.

track_defense_mechanisms(→ List[str])

Determine which defense mechanism was likely used for each porcupine.

Module Contents

porcupy.utils.enhanced_visualization.plot_defense_mechanisms(defense_history: Dict[str, List[int]] | List[List[str]], title: str = 'Defense Mechanism Activation', figsize: Tuple[int, int] = (12, 6), colors: Dict[str, str] = None, save_path: str | None = None)[source]

Plot the activation frequency of each defense mechanism over iterations.

Parameters:
  • defense_history (dict or list) –

    Either: 1. Dictionary with keys ‘sight’, ‘sound’, ‘odor’, ‘physical’ and values

    as lists of counts for each iteration, or

    1. List of lists containing defense mechanisms used by each porcupine at each iteration.

  • title (str, optional) – Title of the plot (default: “Defense Mechanism Activation”).

  • figsize (tuple, optional) – Figure size as (width, height) in inches (default: (12, 6)).

  • colors (dict, optional) – Dictionary mapping defense mechanisms to colors. Default: {‘sight’: ‘blue’, ‘sound’: ‘green’, ‘odor’: ‘orange’, ‘physical’: ‘red’}.

  • save_path (str, optional) – Path to save the figure. If None, the figure is not saved (default: None).

Returns:

The created figure.

Return type:

matplotlib.figure.Figure

porcupy.utils.enhanced_visualization.plot_population_cycles(pop_size_history: List[int], cycles: int, max_iter: int, title: str = 'Population Size Cycles', figsize: Tuple[int, int] = (12, 6), save_path: str | None = None)[source]

Plot the population size history with cycle boundaries highlighted.

Parameters:
  • pop_size_history (list) – List of population sizes at each iteration.

  • cycles (int) – Number of cycles used in the optimization.

  • max_iter (int) – Maximum number of iterations.

  • title (str, optional) – Title of the plot (default: “Population Size Cycles”).

  • 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:

matplotlib.figure.Figure

porcupy.utils.enhanced_visualization.plot_diversity_history(diversity_history: List[float], title: str = 'Population Diversity History', figsize: Tuple[int, int] = (10, 6), save_path: str | None = None)[source]

Plot the diversity history of the population.

Parameters:
  • diversity_history (list) – List of diversity measures at each iteration.

  • title (str, optional) – Title of the plot (default: “Population Diversity History”).

  • figsize (tuple, optional) – Figure size as (width, height) in inches (default: (10, 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:

matplotlib.figure.Figure

porcupy.utils.enhanced_visualization.plot_2d_porcupines(positions: numpy.ndarray, func: Callable, bounds: Tuple[numpy.ndarray, numpy.ndarray], best_pos: numpy.ndarray | None = None, defense_types: List[str] | None = None, title: str = 'Porcupine Positions', figsize: Tuple[int, int] = (10, 8), cmap: str = 'viridis', contour_levels: int = 20, quill_length: float = 0.5, save_path: str | None = None)[source]

Plot porcupines in 2D search space with quill-like directional indicators.

Parameters:
  • positions (ndarray) – Current positions of the porcupines, shape (pop_size, 2).

  • func (callable) – The objective function to visualize.

  • bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.

  • best_pos (ndarray, optional) – Global best position, shape (2,).

  • defense_types (list, optional) – List of defense mechanisms used by each porcupine. Options: ‘sight’, ‘sound’, ‘odor’, ‘physical’.

  • title (str, optional) – Title of the plot (default: “Porcupine Positions”).

  • figsize (tuple, optional) – Figure size as (width, height) in inches (default: (10, 8)).

  • cmap (str, optional) – Colormap for the contour plot (default: ‘viridis’).

  • contour_levels (int, optional) – Number of contour levels (default: 20).

  • quill_length (float, optional) – Length of the directional quills (default: 0.5).

  • save_path (str, optional) – Path to save the figure. If None, the figure is not saved (default: None).

Returns:

The created figure.

Return type:

matplotlib.figure.Figure

porcupy.utils.enhanced_visualization.animate_porcupines_2d(position_history: List[numpy.ndarray], func: Callable, bounds: Tuple[numpy.ndarray, numpy.ndarray], defense_history: List[List[str]] | None = None, best_pos_history: List[numpy.ndarray] | None = None, best_cost_history: List[float] | None = None, interval: int = 200, figsize: Tuple[int, int] = (14, 10), cmap: str = 'viridis', contour_levels: int = 20, quill_length: float = 0.5, save_path: str | None = None, dpi: int = 100, show_trail: bool = True, max_trail_length: int = 20, show_convergence: bool = True)[source]

Create an enhanced animation of porcupines moving in 2D search space with additional visual feedback.

Parameters:
  • position_history (list) – List of position arrays at each iteration, each with shape (pop_size, 2).

  • func (callable) – The objective function to visualize.

  • bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.

  • defense_history (list, optional) – List of lists containing defense mechanisms used by each porcupine at each iteration.

  • best_pos_history (list, optional) – List of best positions at each iteration, each with shape (2,).

  • best_cost_history (list, optional) – List of best costs at each iteration.

  • interval (int, optional) – Interval between frames in milliseconds (default: 200).

  • figsize (tuple, optional) – Figure size as (width, height) in inches (default: (14, 10)).

  • cmap (str, optional) – Colormap for the contour plot (default: ‘viridis’).

  • contour_levels (int, optional) – Number of contour levels (default: 20).

  • quill_length (float, optional) – Length of the directional quills (default: 0.5).

  • 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).

  • show_trail (bool, optional) – Whether to show the trail of best positions (default: True).

  • max_trail_length (int, optional) – Maximum number of positions to show in the trail (default: 20).

  • show_convergence (bool, optional) – Whether to show the convergence plot (default: True).

Returns:

The created animation.

Return type:

matplotlib.animation.FuncAnimation

porcupy.utils.enhanced_visualization.plot_3d_porcupines(positions: numpy.ndarray, fitness: numpy.ndarray, func: Callable, bounds: Tuple[numpy.ndarray, numpy.ndarray], best_pos: numpy.ndarray | None = None, defense_types: List[str] | None = None, title: str = '3D Porcupine Positions', figsize: Tuple[int, int] = (12, 10), cmap: str = 'viridis', alpha: float = 0.7, save_path: str | None = None)[source]

Plot porcupines in 3D search space.

Parameters:
  • positions (ndarray) – Current positions of the porcupines, shape (pop_size, 2).

  • fitness (ndarray) – Fitness values of the porcupines, shape (pop_size,).

  • func (callable) – The objective function to visualize.

  • bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.

  • best_pos (ndarray, optional) – Global best position, shape (2,).

  • defense_types (list, optional) – List of defense mechanisms used by each porcupine. Options: ‘sight’, ‘sound’, ‘odor’, ‘physical’.

  • title (str, optional) – Title of the plot (default: “3D Porcupine Positions”).

  • figsize (tuple, optional) – Figure size as (width, height) in inches (default: (12, 10)).

  • cmap (str, optional) – Colormap for the surface plot (default: ‘viridis’).

  • alpha (float, optional) – Transparency of the surface (default: 0.7).

  • save_path (str, optional) – Path to save the figure. If None, the figure is not saved (default: None).

Returns:

The created figure.

Return type:

matplotlib.figure.Figure

porcupy.utils.enhanced_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:

float

porcupy.utils.enhanced_visualization.track_defense_mechanisms(positions: numpy.ndarray, prev_positions: numpy.ndarray, best_pos: numpy.ndarray, tf: float = 0.8) List[str][source]

Determine which defense mechanism was likely used for each porcupine.

Parameters:
  • positions (ndarray) – Current positions of the porcupines, shape (pop_size, dimensions).

  • prev_positions (ndarray) – Previous positions of the porcupines, shape (pop_size, dimensions).

  • best_pos (ndarray) – Global best position, shape (dimensions,).

  • tf (float, optional) – Tradeoff threshold between third and fourth mechanisms (default: 0.8).

Returns:

List of defense mechanisms used by each porcupine.

Return type:

list