src.environment.optimizer.symbol_optimizer

Module Contents

Classes

Myreplace

SYMBOL_Optimizer

Introduction

SurrRLDE is a novel MetaBBO framework which combines surrogate learning process and reinforcement learning-aided Differential Evolution (DE) algorithm.

MadDE

Population

MadDE_Population

Tokenizer

MyTokenizer

Functions

symbol_config

Introduction

Configures the parameters for the symbolic optimizer by setting various attributes on the provided configuration object.

get_init_pop

Introduction

Initializes the population of a student population (stu_pop) based on a teacher population (tea_pop) using a specified initialization method.

cal_gap_nearest

Introduction

Calculates the normalized maximum minimum Euclidean distance (“gap”) between each student position and its nearest teacher position in a normalized search space.

dist

Calculates the Euclidean distance between two arrays.

get_mask

which_layer

left_or_right

have_continous_const

continus_mul_c

get_along_continuous_mul

test_pre

get_along_continuous_plus_with_minus

get_along_continuous_plus

along_continuous_plus

find_prefix_of_token_ancestor

get_prefix

get_prefix_with_consts

get_next_position

get_str_prefix

prefix_to_infix

expr_to_func

calculate_mean_distance

API

class src.environment.optimizer.symbol_optimizer.Myreplace[source]

Bases: object

Initialization

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.count or self.pattern are 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_Optimizer

Introduction

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

SurrRLDE

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_train is True, the population is initialized using a teacher optimizer and a custom initialization method.

  • If self.is_train is False, the population is initialized normally and reset.

  • Logs and meta-data are initialized if configured.

eval()[source]
train()[source]
observe()[source]
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_cost is 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, and pop_size.

  • stu_pop: An object representing the student population, expected to have attributes pop_size and a reset method.

  • method (str): The initialization method to use. Supported values are 'best', 'harf', 'random', and 'uniform'.

  • rng: A random number generator object with a randint method.

Returns:

  • None: The function modifies stu_pop in place by resetting its population.

Raises:

  • ValueError: If the specified method is 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]) and max_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_pop or tea_pop do not have the required attributes.

  • ValueError: If the shapes of current_position arrays 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 x and y.

Raises:

  • ValueError: If x and y have incompatible shapes for broadcasting.

class src.environment.optimizer.symbol_optimizer.MadDE(config, rng)[source]

Initialization

__ctb_w_arc(group, best, archive, Fs)[source]
__ctr_w_arc(group, archive, Fs)[source]
__weighted_rtb(group, best, Fs, Fas)[source]
__binomial(x, v, Crs)[source]
__sort()[source]
__update_archive(old_id)[source]
__mean_wL(df, s)[source]
__choose_F_Cr()[source]
__update_M_F_Cr(SF, SCr, df)[source]
init_population(problem)[source]
update(action)[source]
class src.environment.optimizer.symbol_optimizer.Population(dim, pop_size, min_x, max_x, max_fes, problem, rng)[source]

Bases: object

Initialization

get_costs(position)[source]
reset(init_pop=None, init_y=None, need_his=True)[source]
update(next_position, filter_survive=False)[source]
update_cmaes(next_position, next_y)[source]
feature_encoding()[source]
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.Population

Initialization

reset()[source]
update(next_position, new_cost, filter_survive=False)[source]
update2(next_position, new_cost)[source]
reset_order(index)[source]
reset_popsize(NP)[source]
class src.environment.optimizer.symbol_optimizer.Tokenizer[source]

Initialization

SPECIAL_SYMBOLS[source]

None

SPECIAL_FLOAT_SYMBOLS[source]

None

SPECIAL_OPERATORS[source]

None

SPECIAL_INTEGERS[source]

None

abstractmethod encode(expr)[source]
abstractmethod decode(expr)[source]
abstractmethod is_unary(token)[source]
abstractmethod is_binary(token)[source]
abstractmethod is_leaf(token)[source]
get_constant_ids()[source]
class src.environment.optimizer.symbol_optimizer.MyTokenizer[source]

Bases: src.environment.optimizer.symbol_optimizer.Tokenizer

Initialization

decode(expr)[source]
encode(expr)[source]
is_consts(id)[source]
is_binary(token)[source]
is_unary(token)[source]
is_leaf(token)[source]
is_var(token)[source]
src.environment.optimizer.symbol_optimizer.get_mask(pre_seq, tokenizer, position, max_layer)[source]
src.environment.optimizer.symbol_optimizer.which_layer(position)[source]
src.environment.optimizer.symbol_optimizer.left_or_right(position, root)[source]
src.environment.optimizer.symbol_optimizer.have_continous_const(seq, position, tokenizer)[source]
src.environment.optimizer.symbol_optimizer.continus_mul_c(seq, position, tokenizer)[source]
src.environment.optimizer.symbol_optimizer.get_along_continuous_mul(tokenizer, seq, begin, visited)[source]
src.environment.optimizer.symbol_optimizer.test_pre(target_vocab, pre_vocab, tokenizer)[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_prefix(seq, sub_root)[source]
src.environment.optimizer.symbol_optimizer.get_prefix_with_consts(seq, consts, sub_root)[source]
src.environment.optimizer.symbol_optimizer.get_next_position(seq, choice, position, tokenizer)[source]
src.environment.optimizer.symbol_optimizer.get_str_prefix(seq, const_vals, tokenizer)[source]
src.environment.optimizer.symbol_optimizer.prefix_to_infix(expr, constants, tokenizer: src.environment.optimizer.symbol_optimizer.Tokenizer)[source]
src.environment.optimizer.symbol_optimizer.expr_to_func(sympy_expr, variables: List[str])[source]
src.environment.optimizer.symbol_optimizer.calculate_mean_distance(population)[source]