Skip to main content
Version: 0.5.x

BaseOptimizer

Abstract base class for optimization algorithms.

Module: aisp.base
Import: from aisp.base import BaseOptimizer


Overview

This class defines the core interface for optimization strategies. It keeps track of cost history, evaluated solutions, and the best solution found during the optimization process. Subclasses must implement optimize and affinity_function.

Use cases:

  • Abstract base class for extending optimization algorithm classes.

Attributes

NameTypeDefaultDescription
cost_historyList[float][]History of best costs found at each iteration.
solution_historyList[]History of the best solution found at each iteration.
best_solutionAnyNoneThe best solution found.
best_costOptional[float]NoneCost of the best solution found.
mode{"min", "max"}'min'Defines whether the algorithm minimizes or maximizes the cost function.

Abstract Methods

optimize

@abstractmethod
def optimize(
self,
max_iters: int = 50,
n_iter_no_change: int = 10,
verbose: bool = True
) -> Any:
...

Execute the optimization process.
This abstract method must be implemented by the subclass, defining how the optimization strategy explores the search space.

Parameters

NameTypeDefaultDescription
max_itersint50Maximum number of iterations
n_iter_no_changeint10the maximum number of iterations without updating the best
verboseboolTrueFlag to enable or disable detailed output during training.

Returns

TypeDescription
SelfReturns the instance of the class that implements this method.

affinity_function

@abstractmethod
def affinity_function(self, solution: Any) -> float:
...

Evaluate the affinity of a candidate solution.

This method must be implemented according to the specific optimization problem, defining how the solution will be evaluated. The returned value should represent the quality of the evaluated solution.

Parameters

NameTypeDefaultDescription
solutionAny-Candidate solution to be evaluated.

Returns

TypeDescription
floatCost value associated with the given solution.

Public Methods

get_report

def get_report(self) -> str:
...

Generate a formatted summary report of the optimization process.
The report includes the best solution, its associated cost, and the evolution of cost values per iteration.

Returns

TypeDescription
strA formatted string containing the optimization summary.

register

def register(self, alias: str, function: Callable[..., Any]) -> None:
...

Register a function dynamically in the optimizer instance.

Parameters

NameTypeDefaultDescription
aliasstr-Name used to access the function as an attribute.
functionCallable[..., Any]-Callable to be registered.

Raises

ExceptionDescription
TypeErrorIf the provided function is not callable.
AttributeErrorIf alias is protected and cannot be modified, or does not exist in the class.

reset

def reset(self):
...

Reset the object's internal state, clearing history and resetting values.