src.environment.optimizer.glhf_optimizer¶
Module Contents¶
Classes¶
Introduction¶GLHF: General Learned Evolutionary Algorithm Via Hyper Functions |
API¶
- class src.environment.optimizer.glhf_optimizer.GLHF_Optimizer(config)[source]¶
Bases:
src.environment.optimizer.learnable_optimizer.Learnable_OptimizerIntroduction¶
GLHF: General Learned Evolutionary Algorithm Via Hyper Functions
Original paper¶
“GLHF: General Learned Evolutionary Algorithm Via Hyper Functions.” arXiv preprint arXiv:2405.03728 (2024).
Official Implementation¶
Initialization
Introduction¶
Initializes the optimizer with the provided configuration and sets up key attributes for the optimization process.
Args:¶
config (object): Config object containing parameters for the optimizer.
The Attributes needed for the GLHF_Optimizer are the following:
maxFEs (int): Maximum number of function evaluations.
log_interval (int): Interval for logging progress.
full_meta_data (bool): Flag for whether to store full meta data.
device (str): Device to use for computations (e.g., “cpu”, “cuda”).
n_logpoint (int): Number of log points for logging.
Built-in Attributes:¶
config (object): Configuration object containing algorithm parameters.
NP (int): Population size, set to 100 by default.
MaxFEs (int): Maximum number of function evaluations.
fes (int): Counter for the number of function evaluations.
cost (list): List to store the best cost values during optimization.
log_index (int): Index for logging progress.
log_interval (int): Interval for logging progress.
Raises:¶
None
- __str__()[source]¶
Introduction¶
Returns a string representation of the GLHF_Optimizer object.
Returns:¶
str: The name of the optimizer, “GLHF_Optimizer”.
- get_costs(position, problem)[source]¶
Introduction¶
Calculates the cost of a given position for a specified optimization problem. If the problem has a known optimum, the cost is computed as the difference between the evaluated position and the optimum; otherwise, it returns the evaluated value directly.
Args:¶
position (Any): The candidate solution or position to be evaluated.
problem (object): The optimization problem object, which has an
evalmethod and anoptimumattribute.
Returns:¶
float: The computed cost for the given position.
Raises:¶
AttributeError: If the
problemobject does not have the requiredevalmethod oroptimumattribute.
- init_population(problem)[source]¶
Introduction¶
Initializes the population for the optimizer based on the problem’s dimensionality and bounds, sets up random number generators according to the device configuration, evaluates the initial costs, and prepares metadata for tracking optimization progress.
Args:¶
problem (object): The optimization problem object, which has attributes
dim(int),ub(upper bound, tensor or array), andlb(lower bound, tensor or array).
Returns:¶
torch.Tensor: A tensor where the first column is the cost (unsqueezed to shape [N, 1]) and the remaining columns are the population data, concatenated along dimension 1.as returned by
self.get_state().
Notes:¶
Updates internal attributes such as
self.population,self.c_cost,self.gbest_val,self.init_gbest,self.cost, and optionally metadata attributes ifself.config.full_meta_datais True.Increments the function evaluation counter (
self.fes) by the population size (self.NP).
- get_state()[source]¶
Introduction¶
Returns the current state of the optimizer by concatenating the cost and population tensors.
Returns:¶
torch.Tensor: A tensor where the first column is the cost (unsqueezed to shape [N, 1]) and the remaining columns are the population data, concatenated along dimension 1.
- update(action, problem)[source]¶
Introduction¶
Updates the optimizer’s population and cost values based on the provided action (typically a policy network) and the given problem instance. Calculates the reward, checks for termination, logs progress, and returns the next state and relevant information.
Args:¶
action (Callable): A function or policy network that takes the current population state and returns a new population.
problem (object): The problem instance containing the objective function and (optionally) the optimum value.
Returns:¶
next_state (torch.Tensor): A tensor where the first column is the cost (unsqueezed to shape [N, 1]) and the remaining columns are the population data, concatenated along dimension 1.as returned by
self.get_state().reward (float): The normalized improvement in the global best cost.
is_end (bool): Flag indicating whether the optimization process has reached its end condition.
info (dict): Additional information (currently empty, but can be extended).
Notes:¶
Updates internal logging and meta-data if configured.
Handles both cases where the problem’s optimum is known or unknown.