Distance calculation utilities for multi-objective optimization problems.
The distance module provides various distance metrics and calculation utilities optimized for different use cases in multi-objective optimization:
DistanceMetric: Enumeration of available distance metrics
DistanceCalculator: High-performance distance calculation utility
EuclideanDistance: Standard Euclidean distance implementation
CosineDistance: Cosine distance with reference point translation
Bases: Enum
Enumeration of available distance metrics for optimized distance calculations.
Each metric is optimized for specific use cases: - L2_SQUARED: Fastest, avoids sqrt computation for relative distance comparisons - LINF: Efficient for high-dimensional spaces, emphasizes maximum difference - TCHEBY_WEIGHTED: Flexible weighted distance allowing preference specification
Available distance metrics:
L2_SQUARED: Squared Euclidean distance (fastest, avoids sqrt computation)
LINF: L-infinity (Chebyshev) distance (efficient for high dimensions)
TCHEBY_WEIGHTED: Weighted Chebyshev distance (supports preferences)
Bases: object
High-performance distance calculator supporting multiple metrics.
This utility class provides optimized implementations for different distance metrics commonly used in multi-objective optimization. All methods are static for efficiency and support numpy array operations for vectorized performance.
High-performance static utility class for distance calculations. Supports multiple optimized metrics for different optimization scenarios.
Performance Characteristics:
L2_SQUARED: ~15-20% faster than standard Euclidean distance
LINF: Efficient for high-dimensional objective spaces
TCHEBY_WEIGHTED: Flexible preference-based distance calculation
Usage Examples:
import numpy as np
from jmetal.util.distance import DistanceCalculator, DistanceMetric
point1 = np.array([0.1, 0.5, 0.8])
point2 = np.array([0.3, 0.2, 0.9])
# L2 squared distance (fastest)
dist_l2 = DistanceCalculator.calculate_distance(
point1, point2, DistanceMetric.L2_SQUARED
)
# Chebyshev distance
dist_linf = DistanceCalculator.calculate_distance(
point1, point2, DistanceMetric.LINF
)
# Weighted Chebyshev distance
weights = np.array([0.5, 0.3, 0.2])
dist_weighted = DistanceCalculator.calculate_distance(
point1, point2, DistanceMetric.TCHEBY_WEIGHTED, weights
)
Calculate distance between two points using the specified metric.
point1: First point as numpy array point2: Second point as numpy array metric: Distance metric to use weights: Optional weights for TCHEBY_WEIGHTED metric
float: Distance between points according to specified metric
ValueError: If metric is invalid or weights are required but not provided
Calculate pairwise distance matrix between all points using vectorized operations.
This is significantly faster than calculating distances individually, especially for large numbers of points. Uses optimized NumPy operations for maximum performance.
points: 2D array where each row is a point (n_points, n_dimensions) metric: Distance metric to use weights: Optional weights for TCHEBY_WEIGHTED metric
numpy.ndarray: Symmetric distance matrix (n_points, n_points)
ValueError: If metric is invalid or weights are required but not provided
Calculate minimum distances from each point to the selected points using vectorized operations.
This is optimized for the subset selection process where we need to find the minimum distance from each candidate point to any already selected point.
points: 2D array of all points (n_points, n_dimensions) selected_indices: List of indices of already selected points metric: Distance metric to use weights: Optional weights for TCHEBY_WEIGHTED metric
numpy.ndarray: Array of minimum distances for each point
Bases: Distance
Euclidean distance implementation with enhanced type safety and validation.
Supports both Python lists and numpy arrays as input. Provides comprehensive input validation for robust operation.
Enhanced Euclidean distance implementation with comprehensive input validation and support for both Python lists and numpy arrays.
Calculate the Euclidean distance between two points.
list1: First point as list or numpy array list2: Second point as list or numpy array
float: Euclidean distance between the points
ValueError: If inputs have different dimensions or are empty TypeError: If inputs cannot be converted to numeric arrays
Bases: Distance
Cosine distance implementation with reference point translation.
This class computes the cosine distance between two points after translating them relative to a reference point. The cosine distance is defined as: distance = 1 - cosine_similarity
Where cosine_similarity = (a·b) / (||a|| * ||b||)
The distance ranges from 0 (identical direction) to 2 (opposite directions).
Performance optimizations: - Cached reference point norm for efficiency - Vectorized numpy operations - Robust input validation and error handling
Cosine distance implementation with reference point translation for specialized use cases in multi-objective optimization.
Calculate the cosine distance between two points relative to the reference point.
The computation follows these steps: 1. Translate both points by subtracting the reference point 2. Compute cosine similarity of the translated vectors 3. Return cosine distance = 1 - cosine_similarity
list1: First point as list or numpy array list2: Second point as list or numpy array
0 = vectors point in same direction 1 = vectors are orthogonal 2 = vectors point in opposite directions
ValueError: If inputs have different dimensions than reference point or are empty TypeError: If inputs cannot be converted to numeric arrays
Calculate the cosine similarity between two points relative to the reference point.
This is a convenience method that returns the similarity instead of distance.
list1: First point as list or numpy array list2: Second point as list or numpy array
1 = vectors point in same direction 0 = vectors are orthogonal
-1 = vectors point in opposite directions
../archive - Archive implementations that use distance metrics
Distance-Based Archive Tutorial - Tutorial on distance-based selection