src.environment.optimizer.symbol_optimizer¶
Module Contents¶
Classes¶
Introduction¶SurrRLDE is a novel MetaBBO framework which combines surrogate learning process and reinforcement learning-aided Differential Evolution (DE) algorithm. |
|
Functions¶
Introduction¶Configures the parameters for the symbolic optimizer by setting various attributes on the provided configuration object. |
|
Introduction¶Initializes the population of a student population ( |
|
Introduction¶Calculates the normalized maximum minimum Euclidean distance (“gap”) between each student position and its nearest teacher position in a normalized search space. |
|
Calculates the Euclidean distance between two arrays. |
|
API¶
- class src.environment.optimizer.symbol_optimizer.Myreplace[source]¶
Bases:
objectInitialization
- replace(match)[source]¶
Introduction¶
Replaces matched patterns in a string with a modified pattern based on the number of replacements performed.
Args:¶
match (re.Match): The match object corresponding to the current regex match.
Built-in Attribute:¶
self.count (int): Tracks the number of replacements performed.
self.pattern (str): The pattern string to use for replacement.
Returns:¶
str: The replacement string. If this is the first match, returns the original matched string; otherwise, returns the pattern with a numeric suffix.
Raises:¶
AttributeError: If
self.countorself.patternare not defined in the instance.
- process_string(string, pattern)[source]¶
Introduction¶
Processes the input string by replacing all occurrences that match the given pattern using a custom replacement method.
Args:¶
string (str): The input string to be processed.
pattern (str or Pattern): The regular expression pattern to search for in the string.
Built-in Attribute:¶
self.pattern: Stores the current pattern being used for replacement.
self.count: Tracks the number of replacements made.Default is 0.
Returns:¶
str: The processed string with all pattern matches replaced.
Raises:¶
re.error: If the provided pattern is not a valid regular expression.
- src.environment.optimizer.symbol_optimizer.symbol_config(config)[source]¶
Introduction¶
Configures the parameters for the symbolic optimizer by setting various attributes on the provided configuration object.
Args:¶
config (object): A configuration object whose attributes will be set to define the optimizer’s behavior.
The attributes needed for the Symbol include:
init_pop (str): Method for initializing the population (default: ‘random’).
teacher (str): The teacher algorithm to use (default: ‘MadDE’).
population_size (int): Number of individuals in the population (default: 100).
boarder_method (str): Method for handling boundary conditions (default: ‘clipping’).
skip_step (int): Number of steps to skip during training (default: 5).
test_skip_step (int): Number of steps to skip during testing (default: 5).
max_c (float): Maximum value for coefficient c (default: 1.0).
min_c (float): Minimum value for coefficient c (default: -1.0).
c_interval (float): Interval for coefficient c (default: 0.4).
max_layer (int): Maximum number of layers allowed (default: 6).
value_dim (int): Dimensionality of the value (default: 1).
hidden_dim (int): Dimensionality of the hidden layer (default: 16).
num_layers (int): Number of layers in the model (default: 1).
lr (float): Learning rate for the optimizer (default: 1e-3).
lr_critic (float): Learning rate for the critic (default: 1e-3).
Built-in Attributes:¶
init_pop (str): Method for initializing the population (default: ‘random’).
teacher (str): The teacher algorithm to use (default: ‘MadDE’).
population_size (int): Number of individuals in the population (default: 100).
boarder_method (str): Method for handling boundary conditions (default: ‘clipping’).
skip_step (int): Number of steps to skip during training (default: 5).
test_skip_step (int): Number of steps to skip during testing (default: 5).
max_c (float): Maximum value for coefficient c (default: 1.0).
min_c (float): Minimum value for coefficient c (default: -1.0).
c_interval (float): Interval for coefficient c (default: 0.4).
max_layer (int): Maximum number of layers allowed (default: 6).
value_dim (int): Dimensionality of the value (default: 1).
hidden_dim (int): Dimensionality of the hidden layer (default: 16).
num_layers (int): Number of layers in the model (default: 1).
lr (float): Learning rate for the optimizer (default: 1e-3).
lr_critic (float): Learning rate for the critic (default: 1e-3).
Returns:¶
None
- class src.environment.optimizer.symbol_optimizer.SYMBOL_Optimizer(config)[source]¶
Bases:
src.environment.optimizer.learnable_optimizer.Learnable_OptimizerIntroduction¶
SurrRLDE is a novel MetaBBO framework which combines surrogate learning process and reinforcement learning-aided Differential Evolution (DE) algorithm.
Original paper¶
“Surrogate Learning in Meta-Black-Box Optimization: A Preliminary Study.” arXiv preprint arXiv:2503.18060 (2025).
Official Implementation¶
Initialization
Introduction¶
Initializes the SymbolOptimizer with the provided configuration, setting up tokenizer, replacement strategy, and various optimization parameters.
Args:¶
config (object): Configuration object.
The attributes needed for the Symbol include:
init_pop (str): Method for initializing the population (default: ‘random’).
teacher (str): The teacher algorithm to use (default: ‘MadDE’).
population_size (int): Number of individuals in the population (default: 100).
boarder_method (str): Method for handling boundary conditions (default: ‘clipping’).
skip_step (int): Number of steps to skip during training (default: 5).
test_skip_step (int): Number of steps to skip during testing (default: 5).
Built-in Attribute:¶
self.tokenizer (MyTokenizer): Tokenizer instance for processing symbols.
self.__config (object): Stores the configuration object.
self.NP (int): Number of particles or population size, set to 100.
self.no_improve (int): Counter for iterations without improvement.
self.per_no_improve (np.ndarray): Array tracking no-improvement status per particle.
self.evaling (bool): Flag indicating if evaluation is in progress.
self.max_fes (int): Maximum number of function evaluations.
self.boarder_method (str): Method for handling boundaries, default is ‘periodic’.
self.replace (Myreplace): Replacement strategy instance.
self.log_interval (int): Interval for logging progress.
self.teacher_optimizer (object or None): Optional teacher optimizer for advanced strategies.
self.is_train (bool): Indicates if the optimizer is in training mode.
Raises:¶
None
- __str__()[source]¶
Returns a string representation of the SYMBOL_Optimizer object.
Returns:¶
str: The string "SYMBOL_Optimizer" representing the object.
- init_population(problem)[source]¶
Introduction¶
Initializes the population for the optimization process, optionally using a teacher optimizer for population initialization if in training mode.
Args:¶
problem (Problem): An instance of the optimization problem, containing bounds and other problem-specific information.
Returns:¶
Any: The observed state after population initialization, as returned by
self.observe().
Raises:¶
None directly, but may raise exceptions from called methods such as
eval,copy.deepcopy, or population initialization routines.
Notes:¶
If
self.is_trainis True, the population is initialized using a teacher optimizer and a custom initialization method.If
self.is_trainis False, the population is initialized normally and reset.Logs and meta-data are initialized if configured.
- update(action, problem)[source]¶
Introduction¶
Updates the optimizer’s population based on the provided action and problem, applying the specified update expression, handling boundary conditions, and calculating rewards. This method is central to the optimization process, performing one or more update steps and logging progress.
Args:¶
action (dict): A dictionary containing the update expression (
'expr') and the number of steps to skip ('skip_step').problem: The optimization problem instance, which may provide information such as the optimum value.
Returns:¶
observation: The current observation of the optimizer’s state.
reward (float): The reward calculated for this update step.
is_end (bool): Whether the optimization process has reached its end condition.
info (dict): Additional information (currently an empty dictionary).
Raises:¶
AssertionError: If the number of ‘randx’ replacements does not match the expected count.
AssertionError: If the shapes of the update variables do not match.
AssertionError: If an unsupported boundary method is specified.
- cal_reward(tea_pop, max_step)[source]¶
Introduction¶
Calculates the reward value based on the imitation distance and the improvement in global best cost for the current population.
Args:¶
tea_pop (Population): The target or teacher population used for imitation comparison.
max_step (int): The maximum number of steps or iterations allowed in the optimization process.
Returns:¶
float: The computed reward, which is the sum of the imitation reward and the base reward.
Raises:¶
ZeroDivisionError: If
self.population.init_costis zero, as division by zero is not allowed.
- src.environment.optimizer.symbol_optimizer.get_init_pop(tea_pop, stu_pop, method, rng)[source]¶
Introduction¶
Initializes the population of a student population (
stu_pop) based on a teacher population (tea_pop) using a specified initialization method.Args:¶
tea_pop: An object representing the teacher population, expected to have attributes
c_cost,current_position, andpop_size.stu_pop: An object representing the student population, expected to have attributes
pop_sizeand aresetmethod.method (str): The initialization method to use. Supported values are
'best','harf','random', and'uniform'.rng: A random number generator object with a
randintmethod.
Returns:¶
None: The function modifies
stu_popin place by resetting its population.
Raises:¶
ValueError: If the specified
methodis not supported.
- src.environment.optimizer.symbol_optimizer.cal_gap_nearest(stu_pop, tea_pop)[source]¶
Introduction¶
Calculates the normalized maximum minimum Euclidean distance (“gap”) between each student position and its nearest teacher position in a normalized search space.
Args:¶
stu_pop: An object representing the student population, expected to have attributes
current_position(numpy.ndarray of shape [n_students, dim]) andmax_x(float or array-like for normalization).tea_pop: An object representing the teacher population, expected to have attribute
current_position(numpy.ndarray of shape [n_teachers, dim]).
Returns:¶
float: The normalized gap, defined as the maximum of the minimum distances from each student to the nearest teacher, divided by the maximum possible distance in the normalized space.
Raises:¶
AttributeError: If
stu_poportea_popdo not have the required attributes.ValueError: If the shapes of
current_positionarrays are incompatible.
- src.environment.optimizer.symbol_optimizer.dist(x, y)[source]¶
Calculates the Euclidean distance between two arrays.
Args:¶
x (np.ndarray): The first input array.
y (np.ndarray): The second input array.
Returns:¶
np.ndarray or float: The Euclidean distance(s) between
xandy.
Raises:¶
ValueError: If
xandyhave incompatible shapes for broadcasting.
- class src.environment.optimizer.symbol_optimizer.Population(dim, pop_size, min_x, max_x, max_fes, problem, rng)[source]¶
Bases:
objectInitialization
- class src.environment.optimizer.symbol_optimizer.MadDE_Population(dim, pop_size, min_x, max_x, max_fes, problem, rng)[source]¶
Bases:
src.environment.optimizer.symbol_optimizer.PopulationInitialization
- class src.environment.optimizer.symbol_optimizer.MyTokenizer[source]¶
Bases:
src.environment.optimizer.symbol_optimizer.TokenizerInitialization
- src.environment.optimizer.symbol_optimizer.get_mask(pre_seq, tokenizer, position, max_layer)[source]¶
- src.environment.optimizer.symbol_optimizer.get_along_continuous_mul(tokenizer, seq, begin, visited)[source]¶
- src.environment.optimizer.symbol_optimizer.get_along_continuous_plus_with_minus(tokenizer, seq, begin, visited)[source]¶
- src.environment.optimizer.symbol_optimizer.get_along_continuous_plus(tokenizer, seq, begin, visited)[source]¶
- src.environment.optimizer.symbol_optimizer.along_continuous_plus(tokenizer, seq, neg_ancestor)[source]¶
- src.environment.optimizer.symbol_optimizer.find_prefix_of_token_ancestor(tokenizer, seq, position, token)[source]¶
- src.environment.optimizer.symbol_optimizer.get_next_position(seq, choice, position, tokenizer)[source]¶
- src.environment.optimizer.symbol_optimizer.prefix_to_infix(expr, constants, tokenizer: src.environment.optimizer.symbol_optimizer.Tokenizer)[source]¶