porcupy.utils.visualization
Functions
|
Plot the convergence history of the optimization process. |
|
Plot the population size history of the optimization process. |
|
Plot a 2D search space with positions of the porcupines. |
|
Create an animation of the optimization process in 2D. |
|
Plot the convergence histories of multiple optimization runs. |
|
Plot the sensitivity of results to a parameter. |
Module Contents
- porcupy.utils.visualization.plot_convergence(cost_history: List[float], title: str = 'Convergence Curve', xlabel: str = 'Iterations', ylabel: str = 'Cost', figsize: Tuple[int, int] = (10, 6), log_scale: bool = False, save_path: str | None = None)[source]
Plot the convergence history of the optimization process.
- Parameters:
cost_history (list) – List of cost values at each iteration.
title (str, optional) – Title of the plot (default: “Convergence Curve”).
xlabel (str, optional) – Label for the x-axis (default: “Iterations”).
ylabel (str, optional) – Label for the y-axis (default: “Cost”).
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (10, 6)).
log_scale (bool, optional) – Whether to use logarithmic scale for the y-axis (default: False).
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.visualization.plot_population_size(pop_size_history: List[int], title: str = 'Population Size History', xlabel: str = 'Iterations', ylabel: str = 'Population Size', figsize: Tuple[int, int] = (10, 6), save_path: str | None = None)[source]
Plot the population size history of the optimization process.
- Parameters:
pop_size_history (list) – List of population sizes at each iteration.
title (str, optional) – Title of the plot (default: “Population Size History”).
xlabel (str, optional) – Label for the x-axis (default: “Iterations”).
ylabel (str, optional) – Label for the y-axis (default: “Population Size”).
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:
- porcupy.utils.visualization.plot_2d_search_space(func: Callable, bounds: Tuple[numpy.ndarray, numpy.ndarray], resolution: int = 100, positions: numpy.ndarray | None = None, best_pos: numpy.ndarray | None = None, title: str = '2D Search Space', figsize: Tuple[int, int] = (10, 8), cmap: str = 'viridis', contour_levels: int = 20, save_path: str | None = None)[source]
Plot a 2D search space with positions of the porcupines.
- Parameters:
func (callable) – The objective function to visualize.
bounds (tuple) – A tuple (lb, ub) containing the lower and upper bounds.
resolution (int, optional) – Resolution of the grid for visualization (default: 100).
positions (ndarray, optional) – Current positions of the porcupines, shape (pop_size, 2).
best_pos (ndarray, optional) – Global best position, shape (2,).
title (str, optional) – Title of the plot (default: “2D Search Space”).
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).
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.visualization.animate_optimization_2d(position_history: List[numpy.ndarray], func: Callable, bounds: Tuple[numpy.ndarray, numpy.ndarray], best_pos_history: List[numpy.ndarray] | None = None, interval: int = 200, figsize: Tuple[int, int] = (10, 8), cmap: str = 'viridis', contour_levels: int = 20, save_path: str | None = None, dpi: int = 100)[source]
Create an animation of the optimization process in 2D.
- 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.
best_pos_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: (10, 8)).
cmap (str, optional) – Colormap for the contour plot (default: ‘viridis’).
contour_levels (int, optional) – Number of contour levels (default: 20).
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.visualization.plot_multiple_runs(cost_histories: List[List[float]], labels: List[str] = None, title: str = 'Comparison of Multiple Runs', xlabel: str = 'Iterations', ylabel: str = 'Cost', figsize: Tuple[int, int] = (10, 6), log_scale: bool = False, save_path: str | None = None)[source]
Plot the convergence histories of multiple optimization runs.
- Parameters:
cost_histories (list) – List of cost history lists, each from a different run.
labels (list, optional) – Labels for each run. If None, runs are labeled as “Run 1”, “Run 2”, etc.
title (str, optional) – Title of the plot (default: “Comparison of Multiple Runs”).
xlabel (str, optional) – Label for the x-axis (default: “Iterations”).
ylabel (str, optional) – Label for the y-axis (default: “Cost”).
figsize (tuple, optional) – Figure size as (width, height) in inches (default: (10, 6)).
log_scale (bool, optional) – Whether to use logarithmic scale for the y-axis (default: False).
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.visualization.plot_parameter_sensitivity(parameter_values: List[float], results: List[float], parameter_name: str, result_name: str = 'Best Cost', title: str = None, figsize: Tuple[int, int] = (10, 6), save_path: str | None = None)[source]
Plot the sensitivity of results to a parameter.
- Parameters:
parameter_values (list) – List of parameter values tested.
results (list) – List of corresponding results (e.g., best costs).
parameter_name (str) – Name of the parameter.
result_name (str, optional) – Name of the result metric (default: “Best Cost”).
title (str, optional) – Title of the plot. If None, a default title is generated.
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: