porcupy.utils.defense_visualization
Defense mechanism visualization module for the Crested Porcupine Optimizer (CPO).
This module provides specialized visualization tools for the unique defense mechanisms of the CPO algorithm, including sight, sound, odor, and physical defense visualizations.
Functions
|
Visualize the defense territories of porcupines based on their defense mechanisms. |
|
Visualize the specific defense mechanisms of porcupines with directional indicators. |
|
Create an animation of porcupines using different defense mechanisms over time. |
|
Plot the effectiveness of each defense mechanism in relation to fitness improvement. |
|
Visualize porcupines with quills pointing in the direction of their movement. |
Module Contents
- porcupy.utils.defense_visualization.visualize_defense_territories(positions: numpy.ndarray, defense_types: List[str], bounds: Tuple[numpy.ndarray, numpy.ndarray], territory_sizes: numpy.ndarray | None = None, title: str = 'Defense Territories', figsize: Tuple[int, int] = (10, 8), save_path: str | None = None)[source]
Visualize the defense territories of porcupines based on their defense mechanisms.
- Parameters:
positions (ndarray) – Positions of the porcupines, shape (pop_size, 2).
defense_types (list) – List of defense mechanisms used by each porcupine.
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
territory_sizes (ndarray, optional) – Sizes of the territories for each porcupine, shape (pop_size,). If None, territories are sized based on defense mechanism.
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:
- porcupy.utils.defense_visualization.visualize_defense_mechanisms(positions: numpy.ndarray, prev_positions: numpy.ndarray, defense_types: List[str], bounds: Tuple[numpy.ndarray, numpy.ndarray], best_position: numpy.ndarray | None = None, title: str = 'Defense Mechanisms Visualization', figsize: Tuple[int, int] = (12, 10), save_path: str | None = None)[source]
Visualize the specific defense mechanisms of porcupines with directional indicators.
- Parameters:
positions (ndarray) – Current positions of the porcupines, shape (pop_size, 2).
prev_positions (ndarray) – Previous positions of the porcupines, shape (pop_size, 2).
defense_types (list) – List of defense mechanisms used by each porcupine.
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
best_position (ndarray, optional) – Global best position, shape (2,).
title (str, optional) – Title of the plot (default: “Defense Mechanisms Visualization”).
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (12, 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.defense_visualization.animate_defense_mechanisms(position_history: List[numpy.ndarray], defense_history: List[List[str]], bounds: Tuple[numpy.ndarray, numpy.ndarray], best_position_history: List[numpy.ndarray] | None = None, interval: int = 200, figsize: Tuple[int, int] = (12, 10), save_path: str | None = None, dpi: int = 100)[source]
Create an animation of porcupines using different defense mechanisms over time.
- Parameters:
position_history (list) – List of position arrays at each iteration, each with shape (pop_size, 2).
defense_history (list) – List of lists containing defense mechanisms used by each porcupine at each iteration.
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
best_position_history (list, optional) – List of best positions at each iteration, each with shape (2,).
interval (int, optional) – Interval between frames in milliseconds (default: 200).
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (12, 10)).
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.defense_visualization.plot_defense_effectiveness(defense_history: Dict[str, List[int]], fitness_history: List[float], title: str = 'Defense Mechanism Effectiveness', figsize: Tuple[int, int] = (12, 8), save_path: str | None = None)[source]
Plot the effectiveness of each defense mechanism in relation to fitness improvement.
- Parameters:
defense_history (dict) – Dictionary with keys as defense mechanisms and values as lists of activation counts.
fitness_history (list) – List of best fitness values at each iteration.
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:
- porcupy.utils.defense_visualization.visualize_quill_directions(positions: numpy.ndarray, velocities: numpy.ndarray, bounds: Tuple[numpy.ndarray, numpy.ndarray], title: str = 'Porcupine Quill Directions', figsize: Tuple[int, int] = (10, 8), save_path: str | None = None)[source]
Visualize porcupines with quills pointing in the direction of their movement.
- Parameters:
positions (ndarray) – Positions of the porcupines, shape (pop_size, 2).
velocities (ndarray) – Velocities of the porcupines, shape (pop_size, 2).
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
title (str, optional) – Title of the plot (default: “Porcupine Quill Directions”).
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: