All Optimizers

Subpackages

mealpy.optimizer

class mealpy.optimizer.Optimizer(**kwargs)[source]

Bases: object

The base class of all algorithms. All methods in this class will be inherited

Notes

  • The function solve() is the most important method, trained the model

  • The parallel (multithreading or multiprocessing) is used in method: generate_population(), update_target_for_population()

  • The general format of:
    • population = [agent_1, agent_2, …, agent_N]

    • agent = [solution, target]

    • target = [fitness value, objective_list]

    • objective_list = [obj_1, obj_2, …, obj_M]

AVAILABLE_MODES = ['process', 'thread', 'swarm']
EPSILON = 1e-09
PARALLEL_MODES = ['process', 'thread']
SUPPORTED_ARRAYS = [<class 'list'>, <class 'tuple'>, <class 'numpy.ndarray'>]
SUPPORTED_MODES = ['process', 'thread', 'swarm', 'single']
after_initialization() None[source]
amend_solution(solution: numpy.ndarray) numpy.ndarray[source]

This function is based on optimizer’s strategy. In each optimizer, this function can be overridden

Parameters

solution – The position

Returns

The valid solution based on optimizer’s strategy

before_initialization(starting_solutions: Optional[Union[List, Tuple, numpy.ndarray]] = None) None[source]
Parameters

starting_solutions – The starting solutions (not recommended)

before_main_loop()[source]
check_mode_and_workers(mode, n_workers)[source]
check_problem(problem, seed) None[source]
check_termination(mode='start', termination=None, epoch=None)[source]
static compare_fitness(fitness_x: Union[float, int], fitness_y: Union[float, int], minmax: str = 'min') bool[source]
static compare_target(target_x: mealpy.utils.target.Target, target_y: mealpy.utils.target.Target, minmax: str = 'min') bool[source]
correct_solution(solution: numpy.ndarray) numpy.ndarray[source]

This function is based on optimizer’s strategy and problem-specific condition DO NOT override this function

Parameters

solution – The position

Returns

The correct solution that can be used to calculate target

crossover_arithmetic(dad_pos=None, mom_pos=None)[source]
Parameters
  • dad_pos – position of dad

  • mom_pos – position of mom

Returns

position of 1st and 2nd child

Return type

list

static duplicate_pop(pop: List[mealpy.utils.agent.Agent]) List[mealpy.utils.agent.Agent][source]
evolve(epoch: int) None[source]
generate_agent(solution: Optional[numpy.ndarray] = None) mealpy.utils.agent.Agent[source]

Generate new agent with full information

Parameters

solution (np.ndarray) – The solution

generate_empty_agent(solution: Optional[numpy.ndarray] = None) mealpy.utils.agent.Agent[source]

Generate new agent with solution

Parameters

solution (np.ndarray) – The solution

generate_group_population(pop: List[mealpy.utils.agent.Agent], n_groups: int, m_agents: int) List[source]

Generate a list of group population from pop

Parameters
  • pop – The current population

  • n_groups – The n groups

  • m_agents – The m agents in each group

Returns

A list of group population

generate_opposition_solution(agent: Optional[mealpy.utils.agent.Agent] = None, g_best: Optional[mealpy.utils.agent.Agent] = None) numpy.ndarray[source]
Parameters
  • agent – The current agent

  • g_best – the global best agent

Returns

The opposite solution

generate_population(pop_size: Optional[int] = None) List[mealpy.utils.agent.Agent][source]
Parameters

pop_size (int) – number of solutions

Returns

population or list of solutions/agents

Return type

list

get_attributes() Dict[source]

Get all attributes in optimizer.

static get_best_agent(pop: List[mealpy.utils.agent.Agent], minmax: str = 'min') mealpy.utils.agent.Agent[source]
Parameters
  • pop – The population of agents

  • minmax – The type of problem

Returns

The best agent

static get_better_agent(agent_x: mealpy.utils.agent.Agent, agent_y: mealpy.utils.agent.Agent, minmax: str = 'min', reverse: bool = False) mealpy.utils.agent.Agent[source]
Parameters
  • agent_x – First agent

  • agent_y – Second agent

  • minmax – The problem type

  • reverse – Reverse the minmax

Returns

The better agent based on fitness

static get_index_best(pop: List[mealpy.utils.agent.Agent], minmax: str = 'min') int[source]
get_index_kway_tournament_selection(pop: Optional[List] = None, k_way: float = 0.2, output: int = 2, reverse: bool = False) List[source]
Parameters
  • pop – The population

  • k_way (float/int) – The percent or number of solutions are randomized pick

  • output (int) – The number of outputs

  • reverse (bool) – set True when finding the worst fitness

Returns

List of the selected indexes

Return type

list

get_index_roulette_wheel_selection(list_fitness: numpy.array)[source]

This method can handle min/max problem, and negative or positive fitness value.

Parameters

list_fitness (nd.array) – 1-D numpy array

Returns

Index of selected solution

Return type

int

get_levy_flight_step(beta: float = 1.0, multiplier: float = 0.001, size: Optional[Union[List, Tuple, numpy.ndarray]] = None, case: int = 0) Union[float, List, numpy.ndarray][source]

Get the Levy-flight step size

Parameters
  • beta (float) –

    Should be in range [0, 2].

    • 0-1: small range –> exploit

    • 1-2: large range –> explore

  • multiplier (float) – default = 0.001

  • size (tuple, list) – size of levy-flight steps, for example: (3, 2), 5, (4, )

  • case (int) –

    Should be one of these value [0, 1, -1].

    • 0: return multiplier * s * self.generator.uniform()

    • 1: return multiplier * s * self.generator.normal(0, 1)

    • -1: return multiplier * s

Returns

The step size of Levy-flight trajectory

Return type

float, list, np.ndarray

get_name() str[source]

Get name of the optimizer

get_parameters() Dict[source]

Get parameters of optimizer.

static get_sorted_and_trimmed_population(pop: Optional[List[mealpy.utils.agent.Agent]] = None, pop_size: Optional[int] = None, minmax: str = 'min') List[mealpy.utils.agent.Agent][source]
Parameters
  • pop – The population

  • pop_size – The number of selected agents

  • minmax – The problem type

Returns

The sorted and trimmed population with pop_size size

static get_sorted_population(pop: List[mealpy.utils.agent.Agent], minmax: str = 'min', return_index: bool = False) List[mealpy.utils.agent.Agent][source]

Get sorted population based on type (minmax) of problem

Parameters
  • pop – The population

  • minmax – The type of the problem

  • return_index – Return the sorted index or not

Returns

Sorted population (1st agent is the best, last agent is the worst Sorted index (Optional)

static get_special_agents(pop: Optional[List[mealpy.utils.agent.Agent]] = None, n_best: int = 3, n_worst: int = 3, minmax: str = 'min') Tuple[List[mealpy.utils.agent.Agent], Optional[List[mealpy.utils.agent.Agent]], Optional[List[mealpy.utils.agent.Agent]]][source]

Get special agents include sorted population, n1 best agents, n2 worst agents

Parameters
  • pop – The population

  • n_best – Top n1 best agents, default n1=3, good level reduction

  • n_worst – Top n2 worst agents, default n2=3, worst level reduction

  • minmax – The problem type

Returns

The sorted_population, n1 best agents and n2 worst agents

static get_special_fitness(pop: Optional[List[mealpy.utils.agent.Agent]] = None, minmax: str = 'min') Tuple[Union[float, numpy.ndarray], float, float][source]

Get special target include the total fitness, the best fitness, and the worst fitness

Parameters
  • pop – The population

  • minmax – The problem type

Returns

The total fitness, the best fitness, and the worst fitness

get_target(solution: numpy.ndarray, counted: bool = True) mealpy.utils.target.Target[source]

Get target value

Parameters
  • solution – The real-value solution

  • counted – Indicating the number of function evaluations is increasing or not

Returns

The target value

static get_worst_agent(pop: List[mealpy.utils.agent.Agent], minmax: str = 'min') mealpy.utils.agent.Agent[source]
Parameters
  • pop – The population of agents

  • minmax – The type of problem

Returns

The worst agent

static greedy_selection_population(pop_old: Optional[List[mealpy.utils.agent.Agent]] = None, pop_new: Optional[List[mealpy.utils.agent.Agent]] = None, minmax: str = 'min') List[mealpy.utils.agent.Agent][source]
Parameters
  • pop_old – The current population

  • pop_new – The next population

  • minmax – The problem type

Returns

The new population with better solutions

improved_ms(pop=None, g_best=None)[source]
initialization() None[source]
initialize_variables()[source]
set_parameters(parameters: Union[List, Tuple, Dict]) None[source]

Set the parameters for current optimizer.

if paras is a list of parameter’s name, then it will set the default value in optimizer as current parameters if paras is a dict of parameter’s name and value, then it will override the current parameters

Parameters

parameters – The parameters

solve(problem: Optional[Union[Dict, mealpy.utils.problem.Problem]] = None, mode: str = 'single', n_workers: Optional[int] = None, termination: Optional[Union[Dict, mealpy.utils.termination.Termination]] = None, starting_solutions: Optional[Union[List, Tuple, numpy.ndarray]] = None, seed: Optional[int] = None) mealpy.utils.agent.Agent[source]
Parameters
  • problem – an instance of Problem class or a dictionary

  • mode

    Parallel: ‘process’, ‘thread’; Sequential: ‘swarm’, ‘single’.

    • ’process’: The parallel mode with multiple cores run the tasks

    • ’thread’: The parallel mode with multiple threads run the tasks

    • ’swarm’: The sequential mode that no effect on updating phase of other agents

    • ’single’: The sequential mode that effect on updating phase of other agents, this is default mode

  • n_workers – The number of workers (cores or threads) to do the tasks (effect only on parallel mode)

  • termination – The termination dictionary or an instance of Termination class

  • starting_solutions – List or 2D matrix (numpy array) of starting positions with length equal pop_size parameter

  • seed – seed for random number generation needed to be explicitly set to int value

Returns

g_best, the best found agent, that hold the best solution and the best target. Access by: .g_best.solution, .g_best.target

Return type

g_best

track_optimize_process() None[source]

Save some historical data after training process finished

track_optimize_step(pop: Optional[List[mealpy.utils.agent.Agent]] = None, epoch: Optional[int] = None, runtime: Optional[float] = None) None[source]

Save some historical data and print out the detailed information of training process in each epoch

Parameters
  • pop – the current population

  • epoch – current iteration

  • runtime – the runtime for current iteration

update_global_best_agent(pop: List[mealpy.utils.agent.Agent], save: bool = True) Union[List, Tuple][source]

Update global best and current best solutions in history object. Also update global worst and current worst solutions in history object.

Parameters
  • pop (list) – The population of pop_size individuals

  • save (bool) – True if you want to add new current/global best to history, False if you just want to update current/global best

Returns

Sorted population and the global best solution

Return type

list

update_target_for_population(pop: Optional[List[mealpy.utils.agent.Agent]] = None) List[mealpy.utils.agent.Agent][source]

Update target for the input population

Parameters

pop – the population of agents

Returns

population with updated target value

Return type

list

mealpy.tuner

class mealpy.tuner.ParameterGrid(param_grid)[source]

Bases: object

Please check out this class from the scikit-learn library.

It represents a grid of parameters with a discrete number of values for each parameter. This class is useful for iterating over parameter value combinations using the Python built-in function iter, and the generated parameter combinations’ order is deterministic.

Parameters

param_grid (dict of str to sequence, or sequence of such) –

The parameter grid to explore, as a dictionary mapping estimator parameters to sequences of allowed values.

An empty dict signifies default parameters.

A sequence of dicts signifies a sequence of grids to search, and is useful to avoid exploring parameter combinations that make no sense or have no effect. See the examples below.

Examples

>>> from mealpy.tuner import ParameterGrid
>>> param_grid = {'a': [1, 2], 'b': [True, False]}
>>> list(ParameterGrid(param_grid)) == ([{'a': 1, 'b': True}, {'a': 1, 'b': False}, {'a': 2, 'b': True}, {'a': 2, 'b': False}])
True
>>> grid = [{'kernel': ['linear']}, {'kernel': ['rbf'], 'gamma': [1, 10]}]
>>> list(ParameterGrid(grid)) == [{'kernel': 'linear'}, {'kernel': 'rbf', 'gamma': 1}, {'kernel': 'rbf', 'gamma': 10}]
True
>>> ParameterGrid(grid)[1] == {'kernel': 'rbf', 'gamma': 1}
True
class mealpy.tuner.Tuner(algorithm: Optional[Union[str, mealpy.optimizer.Optimizer]] = None, param_grid: Optional[Union[Dict, List]] = None, **kwargs: object)[source]

Bases: object

Tuner utility class.

This is a feature that enables the tuning of hyper-parameters for an algorithm. It also supports exporting results in various formats, such as Pandas DataFrame, JSON, and CSV. This feature provides a better option compared to using GridSearchCV or ParameterGrid from the scikit-learn library to tune hyper-parameters

The important functions to note are ‘execute()’ and resolve()”

Parameters
  • algorithm (Optimizer) – the algorithm/optimizer to tune

  • param_grid (dict, list) – dict or list of dictionaries

  • n_trials (int) – number of repetitions

  • mode (str) – set the mode to run (sequential, thread, process), default=”sequential”

  • n_workers (int) – effected only when mode is “thread” or “process”.

Examples

>>> from opfunu.cec_based.cec2017 import F52017
>>> from mealpy import FloatVar, BBO, Tuner
>>>
>>> f1 = F52017(30, f_bias=0)
>>>
>>> p1 = {
>>>     "bounds": FloatVar(lb=f1.lb, ub=f1.ub),
>>>     "obj_func": f1.evaluate,
>>>     "minmax": "min",
>>>     "name": "F5",
>>>     "log_to": "console",
>>> }
>>>
>>> paras_bbo_grid = {
>>>     "epoch": [10, 20, 30],
>>>     "pop_size": [30, 50, 100],
>>>     "n_elites": [2, 3, 4, 5],
>>>     "p_m": [0.01, 0.02, 0.05]
>>> }
>>> term = {
>>>     "max_epoch": 200,
>>>     "max_time": 20,
>>>     "max_fe": 10000
>>> }
>>> if __name__ == "__main__":
>>>     model = BBO.OriginalBBO()
>>>     tuner = Tuner(model, paras_bbo_grid)
>>>     tuner.execute(problem=p1, termination=term, n_trials=5, n_jobs=4, mode="thread", n_workers=6, verbose=True)
>>>
>>>     print(tuner.best_row)
>>>     print(tuner.best_score)
>>>     print(tuner.best_params)
>>>     print(type(tuner.best_params))
>>>
>>>     print(tuner.best_algorithm)
>>>     tuner.export_results(save_path="history/results", save_as="csv")
>>>     tuner.export_figures()
>>>
>>>     g_best = tuner.resolve(mode="thread", n_workers=4, termination=term)
>>>     print(g_best.solution, g_best.target.fitness)
>>>     print(tuner.algorithm.problem.get_name())
>>>     print(tuner.best_algorithm.get_name())
property best_algorithm
property best_params
property best_row
property best_score
execute(problem: Optional[Union[Dict, mealpy.utils.problem.Problem]] = None, termination: Optional[Union[Dict, mealpy.utils.termination.Termination]] = None, n_trials: int = 2, n_jobs: Optional[int] = None, mode: str = 'single', n_workers: int = 2, verbose: bool = True) None[source]

Execute Tuner utility

Parameters
  • problem (dict, Problem) – An instance of Problem class or problem dictionary

  • termination (None, dict, Termination) – An instance of Termination class or termination dictionary

  • n_trials (int) – Number of trials on the Problem

  • n_jobs (int, None) – Speed up this task (run multiple trials at the same time) by using multiple processes. (<=1 or None: sequential, >=2: parallel)

  • mode (str) – Apply on current Problem (“single”, “swarm”, “thread”, “process”), default=”single”.

  • n_workers (int) – Apply on current Problem, number of processes if mode is “thread” or “process’

  • verbose (bool) – Switch for verbose logging (default: False)

Raises

TypeError – Raises TypeError if problem type is not dictionary or an instance Problem class

export_figures(save_path=None, file_name='tuning_epoch_fit.csv', color=None, x_label=None, y_label=None, exts=('.png', '.pdf'), verbose=False)[source]

Export results to various file type

Parameters
  • save_path (str) – The path to the folder, default None

  • file_name (str) – The file name (with file type, e.g. dataframe, json, csv; default: “tuning_epoch_fit.csv”) that hold results

Raises

TypeError – Raises TypeError if export type is not supported

export_results(save_path=None, file_name='tuning_best_fit.csv')[source]

Export results to various file type

Parameters
  • save_path (str) – The path to the folder, default None

  • file_name (str) – The file name (with file type, e.g. dataframe, json, csv; default: “tuning_best_fit.csv”) that hold results

Raises

TypeError – Raises TypeError if export type is not supported

resolve(mode: str = 'single', starting_solutions: Optional[Union[List, Tuple, numpy.ndarray]] = None, n_workers: Optional[int] = None, termination: Optional[Union[Dict, mealpy.utils.termination.Termination]] = None) mealpy.utils.agent.Agent[source]

Resolving the problem with the best parameters

Parameters
  • mode

    Parallel: ‘process’, ‘thread’; Sequential: ‘swarm’, ‘single’.

    • ’process’: The parallel mode with multiple cores run the tasks

    • ’thread’: The parallel mode with multiple threads run the tasks

    • ’swarm’: The sequential mode that no effect on updating phase of other agents

    • ’single’: The sequential mode that effect on updating phase of other agents, default

  • starting_solutions – List or 2D matrix (numpy array) of starting positions with length equal pop_size parameter

  • n_workers – The number of workers (cores or threads) to do the tasks (effect only on parallel mode)

  • termination – The termination dictionary or an instance of Termination class

Returns

Agent, the best agent found

Return type

g_best

mealpy.multitask

class mealpy.multitask.Multitask(algorithms: Optional[Union[List, Tuple]] = None, problems: Optional[Union[List, Tuple]] = None, terminations: Optional[Union[List, Tuple]] = None, modes: Optional[Union[List, Tuple]] = None, n_workers: Optional[int] = None, **kwargs: object)[source]

Bases: object

Multitask utility class.

This feature enables the execution of multiple algorithms across multiple problems and trials. Additionally, it allows for exporting results in various formats such as Pandas DataFrame, JSON, and CSV.

Parameters
  • algorithms (list, tuple) – List of algorithms to run

  • problems (list, tuple) – List of problems to run

  • terminations (list, tuple) – List of terminations to apply on algorithm/problem

  • modes (list, tuple) – List of modes to apply on algorithm/problem

  • n_workers (int) – Number of workers (threads or processes) to apply on algorithm/problem. Only effect when mode is thread or process.

Examples

>>> ## Import libraries
>>> from opfunu.cec_based.cec2017 import F52017, F102017, F292017
>>> from mealpy import FloatVar
>>> from mealpy import BBO, DE
>>> from mealpy import Multitask
>>> ## Define your own problems
>>> f1 = F52017(30, f_bias=0)
>>> f2 = F102017(30, f_bias=0)
>>> f3 = F292017(30, f_bias=0)
>>> p1 = {
>>>     "bounds": FloatVar(lb=f1.lb, ub=f1.ub),
>>>     "obj_func": f1.evaluate,
>>>     "minmax": "min",
>>>     "name": "F5",
>>>     "log_to": "console",
>>> }
>>> p2 = {
>>>     "bounds": FloatVar(lb=f2.lb, ub=f2.ub),
>>>     "obj_func": f2.evaluate,
>>>     "minmax": "min",
>>>     "name": "F10",
>>>     "log_to": "console",
>>> }
>>> p3 = {
>>>     "bounds": FloatVar(lb=f3.lb, ub=f3.ub),
>>>     "obj_func": f3.evaluate,
>>>     "minmax": "min",
>>>     "name": "F29",
>>>     "log_to": "console",
>>> }
>>> ## Define optimizers
>>> optimizer1 = BBO.DevBBO(epoch=10000, pop_size=50)
>>> optimizer2 = BBO.OriginalBBO(epoch=10000, pop_size=50)
>>> optimizer3 = DE.OriginalDE(epoch=10000, pop_size=50)
>>> optimizer4 = DE.SAP_DE(epoch=10000, pop_size=50)
>>> ## Define termination if needed
>>> term = {
>>>     "max_fe": 30000
>>> }
>>> ## Define and run Multitask
>>> if __name__ == "__main__":
>>>     multitask = Multitask(algorithms=(optimizer1, optimizer2, optimizer3, optimizer4), problems=(p1, p2, p3), terminations=(term, ), modes=("thread", ), n_workers=4)
>>>     # default modes = "single", default termination = epoch (as defined in problem dictionary)
>>>     multitask.execute(n_trials=5, n_jobs=None, save_path="history", save_as="csv", save_convergence=True, verbose=False)
>>>     # multitask.execute(n_trials=5, save_path="history", save_as="csv", save_convergence=True, verbose=False)
check_input(name=None, values=None, kind=None)[source]
execute(n_trials: int = 2, n_jobs: Optional[int] = None, save_path: str = 'history', save_as: str = 'csv', save_convergence: bool = False, verbose: bool = False) None[source]

Execute multitask utility.

Parameters
  • n_trials (int) – Number of repetitions

  • n_jobs (int, None) – Number of processes will be used to speed up the computation (<=1 or None: sequential, >=2: parallel)

  • save_path (str) – The path to the folder that hold results

  • save_as (str) – Saved file type (e.g. dataframe, json, csv) (default: “csv”)

  • save_convergence (bool) – Save the error (convergence/fitness) during generations (default: False)

  • verbose (bool) – Switch for verbose logging (default: False)

Raises

TypeError – Raises TypeError if export type is not supported

static export_to_csv(result: pandas.core.frame.DataFrame, save_path: str)[source]
static export_to_dataframe(result: pandas.core.frame.DataFrame, save_path: str)[source]
static export_to_json(result: pandas.core.frame.DataFrame, save_path: str)[source]