"""
Base class representing a callable objective function.
"""
from typing import Any, Dict
from ..data_classes import FunctionMetadata, Point
[docs]
class ObjectiveFunction:
"""
Base class representing a callable objective function.
"""
[docs]
def __init__(
self,
name: str,
dim: int,
hyperparameters: Dict[str, Any] | None = None,
) -> None:
"""
Class constructor.
Args:
name: Name of the objective function.
dim: Dimensionality of the function.
hyperparameters: Dictionary with hyperparameters of the function.
"""
if not hyperparameters:
hyperparameters = {}
self.metadata = FunctionMetadata(name, dim, hyperparameters)
self.num_calls = 0
[docs]
def __call__(self, point: Point) -> Point:
"""
Evaluate a single point with the objective function.
Args:
point: Point to evaluate.
Raises:
ValueError: If dimensionality of x doesn't match self.dim
Returns:
Evaluated point.
"""
if not len(point.x) == self.metadata.dim:
raise ValueError(
f"The dimensionality of the provided point is not matching the dimensionality"
f"of the function. Expected {self.metadata.dim}, got {len(point.x)}"
)
self.num_calls += 1