porcupy.utils.visualization_manager

Visualization manager for the Crested Porcupine Optimizer (CPO).

This module provides a unified interface for all visualization tools in the Porcupy library.

Classes

CPOVisualizer

Unified interface for all CPO visualization tools.

Module Contents

class porcupy.utils.visualization_manager.CPOVisualizer(objective_func: Callable | None = None, bounds: Tuple[numpy.ndarray, numpy.ndarray] | None = None)[source]

Unified interface for all CPO visualization tools.

This class provides access to all visualization capabilities for the Crested Porcupine Optimizer algorithm.

objective_func = None[source]
bounds = None[source]
position_history = [][source]
best_position_history = [][source]
fitness_history = [][source]
pop_size_history = [][source]
defense_history[source]
defense_types_history = [][source]
diversity_history = [][source]
record_iteration(positions: numpy.ndarray, best_position: numpy.ndarray, fitness: numpy.ndarray, pop_size: int, defense_types: List[str] | None = None)[source]

Record data from a single iteration for visualization.

Parameters:
  • positions (numpy.ndarray) – Positions of all porcupines in the current iteration.

  • best_position (numpy.ndarray) – Best position found so far.

  • fitness (numpy.ndarray) – Fitness values of all porcupines in the current iteration.

  • pop_size (int) – Current population size.

  • defense_types (list of str, optional) – Types of defense mechanisms used by each porcupine in the current iteration.

create_dashboard(update_interval: float = 0.5, figsize: Tuple[int, int] = (15, 10)) porcupy.utils.interactive_visualization.OptimizationDashboard[source]

Create an interactive dashboard for monitoring optimization.

Parameters:
  • update_interval (float, optional) – Time interval between dashboard updates in seconds (default: 0.5).

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

Returns:

The created dashboard.

Return type:

OptimizationDashboard

visualize_defense_mechanisms(title: str = 'Defense Mechanism Activation', figsize: Tuple[int, int] = (12, 6), save_path: str | None = None)[source]

Visualize the activation of different defense mechanisms over iterations.

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

  • 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

visualize_population_cycles(cycles: int, max_iter: int, title: str = 'Population Size Cycles', figsize: Tuple[int, int] = (12, 6), save_path: str | None = None)[source]

Visualize the population size changes over cycles.

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

visualize_diversity_history(title: str = 'Population Diversity History', figsize: Tuple[int, int] = (10, 6), save_path: str | None = None)[source]

Visualize the diversity history of the population.

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

visualize_porcupines_2d(iteration: int = -1, 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]

Visualize porcupines in 2D search space at a specific iteration.

Parameters:
  • iteration (int, optional) – Iteration to visualize. Default is -1 (last iteration).

  • 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

animate_optimization(interval: int = 200, figsize: Tuple[int, int] = (10, 8), cmap: str = 'viridis', contour_levels: int = 20, quill_length: float = 0.5, save_path: str | None = None, dpi: int = 100)[source]

Create an animation of the optimization process in 2D.

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

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

matplotlib.animation.FuncAnimation

create_animation(positions_history: List[numpy.ndarray], best_position_history: List[numpy.ndarray], title: str = 'CPO Optimization Process', save_path: str | None = None, fps: int = 5, defense_types_history: List[List[str]] | None = None, figsize: Tuple[int, int] = (12, 10), dpi: int = 100, show_exploration_exploitation: bool = True)[source]

Create an enhanced animation of the optimization process with defense mechanisms.

Parameters:
  • positions_history (List[np.ndarray]) – List of position arrays for each iteration.

  • best_position_history (List[np.ndarray]) – List of best positions for each iteration.

  • title (str, optional) – Title of the animation (default: “CPO Optimization Process”).

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

  • fps (int, optional) – Frames per second for the animation (default: 5).

  • defense_types_history (List[List[str]], optional) – List of defense types used by each porcupine at each iteration.

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

  • dpi (int, optional) – DPI for the saved animation (default: 100).

  • show_exploration_exploitation (bool, optional) – Whether to show exploration-exploitation balance subplot (default: True).

Returns:

The created animation.

Return type:

matplotlib.animation.FuncAnimation

visualize_defense_territories(iteration: int = -1, title: str = 'Defense Territories', figsize: Tuple[int, int] = (10, 8), save_path: str | None = None)[source]

Visualize the defense territories of porcupines.

Parameters:
  • iteration (int, optional) – Iteration to visualize. Default is -1 (last iteration).

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

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

  • 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

visualize_exploration_exploitation(sample_iterations: List[int] | None = None, figsize: Tuple[int, int] = (15, 10), save_path: str | None = None)[source]

Visualize the balance between exploration and exploitation.

Parameters:
  • sample_iterations (list, optional) – List of iteration indices to visualize. If None, evenly spaced iterations are selected.

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

matplotlib.figure.Figure

visualize_diversity_vs_convergence(cycles: int, max_iter: int, title: str = 'Diversity vs Convergence', figsize: Tuple[int, int] = (12, 6), save_path: str | None = None)[source]

Visualize the relationship between population diversity and convergence.

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

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

matplotlib.figure.Figure

visualize_defense_effectiveness(title: str = 'Defense Mechanism Effectiveness', figsize: Tuple[int, int] = (12, 8), save_path: str | None = None)[source]

Visualize the effectiveness of each defense mechanism.

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

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

  • 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

compare_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]

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:

matplotlib.figure.Figure

record_from_optimizer(optimizer)[source]

Record data from a CPO optimizer instance.

Parameters:

optimizer (CPO) – The CPO optimizer instance to record data from.

create_parameter_tuning_dashboard(parameter_name: str, parameter_range: List[float], result_metric: str = 'Best Cost', figsize: Tuple[int, int] = (12, 8)) porcupy.utils.interactive_visualization.ParameterTuningDashboard[source]

Create a dashboard for parameter tuning and sensitivity analysis.

Parameters:
  • parameter_name (str) – Name of the parameter being tuned.

  • parameter_range (list) – List of parameter values to test.

  • result_metric (str, optional) – Name of the result metric (default: “Best Cost”).

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

Returns:

The created dashboard.

Return type:

ParameterTuningDashboard