mealpy.evolutionary_based package

mealpy.evolutionary_based.CRO

class mealpy.evolutionary_based.CRO.BaseCRO(problem, epoch=10000, pop_size=100, po=0.4, Fb=0.9, Fa=0.1, Fd=0.1, Pd=0.1, G=(0.02, 0.2), GCR=0.1, n_trials=3, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Coral Reefs Optimization (CRO)

Links:
  1. http://downloads.hindawi.com/journals/tswj/2014/739768.pdf

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • po (float): [0.2, 0.5], the rate between free/occupied at the beginning

  • Fb (float): [0.6, 0.9], BroadcastSpawner/ExistingCorals rate

  • Fa (float): [0.05, 0.3], fraction of corals duplicates its self and tries to settle in a different part of the reef

  • Fd (float): [0.05, 0.5], fraction of the worse health corals in reef will be applied depredation

  • Pd (float): [0.05, 0.3], Probability of depredation

  • G (list): (gamma_min, gamma_max) -> ([0.01, 0.1], [0.1, 0.5]), factor for mutation process

  • GCR (float): [0.05, 0.2], probability for mutation process

  • n_trials (int): [2, 10], number of attempts for a larvar to set in the reef.

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.CRO import BaseCRO
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> po = 0.4
>>> Fb = 0.9
>>> Fa = 0.1
>>> Fd = 0.1
>>> Pd = 0.1
>>> G = [0.02, 0.2]
>>> GCR = 0.1
>>> n_trials = 5
>>> model = BaseCRO(problem_dict1, epoch, pop_size, po, Fb, Fa, Fd, Pd, G, GCR, n_trials)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Salcedo-Sanz, S., Del Ser, J., Landa-Torres, I., Gil-López, S. and Portilla-Figueras, J.A., 2014. The coral reefs optimization algorithm: a novel metaheuristic for efficiently solving optimization problems. The Scientific World Journal, 2014.

broadcast_spawning_brooding()[source]
evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

class mealpy.evolutionary_based.CRO.OCRO(problem, epoch=10000, pop_size=100, po=0.4, Fb=0.9, Fa=0.1, Fd=0.1, Pd=0.1, G=(0.02, 0.2), GCR=0.1, n_trials=3, restart_count=55, **kwargs)[source]

Bases: mealpy.evolutionary_based.CRO.BaseCRO

The original version of: Opposition-based Coral Reefs Optimization (OCRO)

Links:
  1. https://dx.doi.org/10.2991/ijcis.d.190930.003

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • po (float): [0.2, 0.5], the rate between free/occupied at the beginning

  • Fb (float): [0.6, 0.9], BroadcastSpawner/ExistingCorals rate

  • Fa (float): [0.05, 0.3], fraction of corals duplicates its self and tries to settle in a different part of the reef

  • Fd (float): [0.05, 0.5], fraction of the worse health corals in reef will be applied depredation

  • Pd (float): [0.05, 0.3], Probability of depredation

  • G (list): (gamma_min, gamma_max) -> ([0.01, 0.1], [0.1, 0.5]), factor for mutation process

  • GCR (float): [0.05, 0.2], probability for mutation process

  • n_trials (int): [2, 10], number of attempts for a larvar to set in the reef

  • restart_count (int): [10, 100], reset the whole population after global best solution is not improved after restart_count times

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.CRO import OCRO
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> po = 0.4
>>> Fb = 0.9
>>> Fa = 0.1
>>> Fd = 0.1
>>> Pd = 0.1
>>> G = [0.02, 0.2]
>>> GCR = 0.1
>>> n_trials = 5
>>> restart_count = 50
>>> model = OCRO(problem_dict1, epoch, pop_size, po, Fb, Fa, Fd, Pd, G, GCR, n_trials, restart_count)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Nguyen, T., Nguyen, T., Nguyen, B.M. and Nguyen, G., 2019. Efficient time-series forecasting using neural network and opposition-based coral reefs optimization. International Journal of Computational Intelligence Systems, 12(2), p.1144.

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

mealpy.evolutionary_based.DE

class mealpy.evolutionary_based.DE.BaseDE(problem, epoch=10000, pop_size=100, wf=0.8, cr=0.9, strategy=0, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Differential Evolution (DE)

Links:
  1. https://doi.org/10.1016/j.swevo.2018.10.006

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • wf (float): [0.5, 0.95], weighting factor, default = 0.8

  • cr (float): [0.5, 0.95], crossover rate, default = 0.9

  • strategy (int): [0, 5], there are lots of variant version of DE algorithm,
    • 0: DE/current-to-rand/1/bin

    • 1: DE/best/1/bin

    • 2: DE/best/2/bin

    • 3: DE/rand/2/bin

    • 4: DE/current-to-best/1/bin

    • 5: DE/current-to-rand/1/bin

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.DE import BaseDE
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> wf = 0.01
>>> cr = 2
>>> strategy = 0
>>> model = BaseDE(problem_dict1, epoch, pop_size, wf, cr, strategy)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Mohamed, A.W., Hadi, A.A. and Jambi, K.M., 2019. Novel mutation strategy for enhancing SHADE and LSHADE algorithms for global numerical optimization. Swarm and Evolutionary Computation, 50, p.100455.

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

class mealpy.evolutionary_based.DE.JADE(problem, epoch=10000, pop_size=100, miu_f=0.5, miu_cr=0.5, pt=0.1, ap=0.1, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The variant version of: Differential Evolution (JADE)

Links:
  1. https://doi.org/10.1109/TEVC.2009.2014613

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • miu_f (float): [0.4, 0.6], initial adaptive f, default = 0.5

  • miu_cr (float): [0.4, 0.6], initial adaptive cr, default = 0.5

  • pt (float): [0.05, 0.2], The percent of top best agents (p in the paper), default = 0.1

  • ap (float): [0.05, 0.2], The Adaptation Parameter control value of f and cr (c in the paper), default=0.1

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.DE import JADE
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> miu_f = 0.5
>>> miu_cr = 0.5
>>> pt = 0.1
>>> ap = 0.1
>>> model = JADE(problem_dict1, epoch, pop_size, miu_f, miu_cr, pt, ap)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Zhang, J. and Sanderson, A.C., 2009. JADE: adaptive differential evolution with optional external archive. IEEE Transactions on evolutionary computation, 13(5), pp.945-958.

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

lehmer_mean(list_objects)[source]
class mealpy.evolutionary_based.DE.L_SHADE(problem, epoch=750, pop_size=100, miu_f=0.5, miu_cr=0.5, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Linear Population Size Reduction Success-History Adaptation Differential Evolution (LSHADE)

Links:
  1. https://metahack.org/CEC2014-Tanabe-Fukunaga.pdf

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • miu_f (float): [0.4, 0.6], initial weighting factor, default = 0.5

  • miu_cr (float): [0.4, 0.6], initial cross-over probability, default = 0.5

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.DE import L_SHADE
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> miu_f = 0.5
>>> miu_cr = 0.5
>>> model = L_SHADE(problem_dict1, epoch, pop_size, miu_f, miu_cr)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Tanabe, R. and Fukunaga, A.S., 2014, July. Improving the search performance of SHADE using linear population size reduction. In 2014 IEEE congress on evolutionary computation (CEC) (pp. 1658-1665). IEEE.

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

weighted_lehmer_mean(list_objects, list_weights)[source]
class mealpy.evolutionary_based.DE.SADE(problem, epoch=10000, pop_size=100, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Self-Adaptive Differential Evolution (SADE)

Links:
  1. https://doi.org/10.1109/CEC.2005.1554904

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.DE import SADE
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> model = SADE(problem_dict1, epoch, pop_size)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Qin, A.K. and Suganthan, P.N., 2005, September. Self-adaptive differential evolution algorithm for numerical optimization. In 2005 IEEE congress on evolutionary computation (Vol. 2, pp. 1785-1791). IEEE.

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

class mealpy.evolutionary_based.DE.SAP_DE(problem, epoch=750, pop_size=100, wf=0.8, cr=0.9, branch='ABS', **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Differential Evolution with Self-Adaptive Populations (SAP_DE)

Links:
  1. https://doi.org/10.1007/s00500-005-0537-1

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • wf (float): [0.6, 0.95], weighting factor, default = 0.8

  • cr (float): [0.6, 0.95], crossover rate, default = 0.9

  • branch (str): [“ABS” or “REL”], gaussian (absolute) or uniform (relative) method

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.DE import SAP_DE
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> miu_f = 0.5
>>> miu_cr = 0.5
>>> model = SAP_DE(problem_dict1, epoch, pop_size, miu_f, miu_cr)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Teo, J., 2006. Exploring dynamic self-adaptive populations in differential evolution. Soft Computing, 10(8), pp.673-686.

ID_CR = 2
ID_MR = 3
ID_PS = 4
create_solution()[source]
To get the position, fitness wrapper, target and obj list
  • A[self.ID_POS] –> Return: position

  • A[self.ID_TAR] –> Return: [target, [obj1, obj2, …]]

  • A[self.ID_TAR][self.ID_FIT] –> Return: target

  • A[self.ID_TAR][self.ID_OBJ] –> Return: [obj1, obj2, …]

Returns

wrapper of solution with format [position, [target, [obj1, obj2, …]], crossover_rate, mutation_rate, pop_size]

Return type

list

edit_to_range(var=None, lower=0, upper=1, func_value=None)[source]
evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

class mealpy.evolutionary_based.DE.SHADE(problem, epoch=750, pop_size=100, miu_f=0.5, miu_cr=0.5, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The variant version of: Success-History Adaptation Differential Evolution (SHADE)

Links:
  1. https://doi.org/10.1109/CEC.2013.6557555

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • miu_f (float): [0.4, 0.6], initial weighting factor, default = 0.5

  • miu_cr (float): [0.4, 0.6], initial cross-over probability, default = 0.5

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.DE import SHADE
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> miu_f = 0.5
>>> miu_cr = 0.5
>>> model = SHADE(problem_dict1, epoch, pop_size, miu_f, miu_cr)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Tanabe, R. and Fukunaga, A., 2013, June. Success-history based parameter adaptation for differential evolution. In 2013 IEEE congress on evolutionary computation (pp. 71-78). IEEE.

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

weighted_lehmer_mean(list_objects, list_weights)[source]

mealpy.evolutionary_based.EP

class mealpy.evolutionary_based.EP.BaseEP(problem, epoch=10000, pop_size=100, bout_size=0.05, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Evolutionary Programming (EP)

Links:
  1. http://www.cleveralgorithms.com/nature-inspired/evolution/evolutionary_programming.html

  2. https://github.com/clever-algorithms/CleverAlgorithms

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • bout_size (float/int): Number of tried with tournament selection (5% of pop_size)
    • if float number –> percentage of child agents, [0.05, 0.2]

    • int –> number of child agents, [3, 20]

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.EP import BaseEP
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> bout_size = 0.05
>>> model = BaseEP(problem_dict1, epoch, pop_size, bout_size)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Yao, X., Liu, Y. and Lin, G., 1999. Evolutionary programming made faster. IEEE Transactions on Evolutionary computation, 3(2), pp.82-102.

ID_POS = 0
ID_STR = 2
ID_TAR = 1
ID_WIN = 3
create_solution()[source]
To get the position, fitness wrapper, target and obj list
  • A[self.ID_POS] –> Return: position

  • A[self.ID_TAR] –> Return: [target, [obj1, obj2, …]]

  • A[self.ID_TAR][self.ID_FIT] –> Return: target

  • A[self.ID_TAR][self.ID_OBJ] –> Return: [obj1, obj2, …]

Returns

wrapper of solution with format [position, [target, [obj1, obj2, …]], strategy, times_win]

Return type

list

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

class mealpy.evolutionary_based.EP.LevyEP(problem, epoch=10000, pop_size=100, bout_size=0.05, **kwargs)[source]

Bases: mealpy.evolutionary_based.EP.BaseEP

My Levy-flight version of: Evolutionary Programming (LevyEP)

Notes

I try to apply Levy-flight to EP and change flow and add some equations.

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • bout_size (float/int): Number of tried with tournament selection (5% of pop_size)
    • if float number –> percentage of child agents, [0.05, 0.2]

    • int –> number of child agents, [3, 20]

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.EP import LevyEP
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> bout_size = 0.05
>>> model = LevyEP(problem_dict1, epoch, pop_size, bout_size)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")
evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

mealpy.evolutionary_based.ES

class mealpy.evolutionary_based.ES.BaseES(problem, epoch=10000, pop_size=100, n_child=0.75, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Evolution Strategies (ES)

Links:
  1. http://www.cleveralgorithms.com/nature-inspired/evolution/evolution_strategies.html

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • n_child (float/int): Number of child evolving in the next generation
    • if float number –> percentage of child agents, [0.5, 1.0]

    • int –> number of child agents, [20, pop_size]

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.ES import BaseES
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> n_child = 0.75
>>> model = BaseES(problem_dict1, epoch, pop_size, n_child)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Beyer, H.G. and Schwefel, H.P., 2002. Evolution strategies–a comprehensive introduction. Natural computing, 1(1), pp.3-52.

ID_POS = 0
ID_STR = 2
ID_TAR = 1
create_solution()[source]
To get the position, fitness wrapper, target and obj list
  • A[self.ID_POS] –> Return: position

  • A[self.ID_TAR] –> Return: [target, [obj1, obj2, …]]

  • A[self.ID_TAR][self.ID_FIT] –> Return: target

  • A[self.ID_TAR][self.ID_OBJ] –> Return: [obj1, obj2, …]

Returns

wrapper of solution with format [position, [target, [obj1, obj2, …]], strategy]

Return type

list

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

class mealpy.evolutionary_based.ES.LevyES(problem, epoch=10000, pop_size=100, n_child=0.75, **kwargs)[source]

Bases: mealpy.evolutionary_based.ES.BaseES

My Levy-flight version of: Evolution Strategies (ES)

Links:
  1. http://www.cleveralgorithms.com/nature-inspired/evolution/evolution_strategies.html

Notes

I implement Levy-flight and change the flow of original version.

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • n_child (float/int): Number of child evolving in the next generation
    • if float number –> percentage of child agents, [0.5, 1.0]

    • int –> number of child agents, [20, pop_size]

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.ES import BaseES
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> n_child = 0.75
>>> model = BaseES(problem_dict1, epoch, pop_size, n_child)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Beyer, H.G. and Schwefel, H.P., 2002. Evolution strategies–a comprehensive introduction. Natural computing, 1(1), pp.3-52.

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

mealpy.evolutionary_based.FPA

class mealpy.evolutionary_based.FPA.BaseFPA(problem, epoch=10000, pop_size=100, p_s=0.8, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Flower Pollination Algorithm (FPA)

Links:
  1. https://doi.org/10.1007/978-3-642-32894-7_27

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • p_s (float): [0.5, 0.95], switch probability, default = 0.8

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.FPA import BaseFPA
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> p_s = 0.8
>>> model = BaseFPA(problem_dict1, epoch, pop_size, p_s)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Yang, X.S., 2012, September. Flower pollination algorithm for global optimization. In International conference on unconventional computing and natural computation (pp. 240-249). Springer, Berlin, Heidelberg.

amend_position(position=None)[source]

If solution out of bound at dimension x, then it will re-arrange to random location in the range of domain

Parameters

position – vector position (location) of the solution.

Returns

Amended position

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

mealpy.evolutionary_based.GA

class mealpy.evolutionary_based.GA.BaseGA(problem, epoch=10000, pop_size=100, pc=0.95, pm=0.025, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Genetic Algorithm (GA)

Links:
  1. https://blog.sicara.com/getting-started-genetic-algorithms-python-tutorial-81ffa1dd72f9

  2. https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_quick_guide.htm

  3. https://www.analyticsvidhya.com/blog/2017/07/introduction-to-genetic-algorithm/

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • pc (float): [0.7, 0.95], cross-over probability, default = 0.95

  • pm (float): [0.01, 0.2], mutation probability, default = 0.025

  • selection (str): Optional, can be [“roulette”, “tournament”, “random”], default = “tournament”

  • k_way (float): Optional, set it when use “tournament” selection, default = 0.2

  • crossover (str): Optional, can be [“one_point”, “multi_points”, “uniform”, “arithmetic”], default = “uniform”

  • mutation_multipoints (bool): Optional, True or False, effect on mutation process

  • mutation (str): Optional, can be [“flip”, “swap”] for multipoints and can be [“flip”, “swap”, “scramble”, “inversion”] for one-point

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.GA import BaseGA
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> pc = 0.9
>>> pm = 0.05
>>> model1 = BaseGA(problem_dict1, epoch, pop_size, pc, pm)
>>> best_position, best_fitness = model1.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")
>>>
>>> model2 = BaseGA(problem_dict1, epoch, pop_size, pc, pm, selection="tournament", k_way=0.4, crossover="multi_points")
>>>
>>> model3 = BaseGA(problem_dict1, epoch, pop_size, pc, pm, crossover="one_point", mutation="scramble")
>>>
>>> model4 = BaseGA(problem_dict1, epoch, pop_size, pc, pm, crossover="arithmetic", mutation_multipoints=True, mutation="swap")
>>>
>>> model5 = BaseGA(problem_dict1, epoch, pop_size, pc, pm, selection="roulette", crossover="multi_points")
>>>
>>> model6 = BaseGA(problem_dict1, epoch, pop_size, pc, pm, selection="random", mutation="inversion")
>>>
>>> model7 = BaseGA(problem_dict1, epoch, pop_size, pc, pm, crossover="arithmetic", mutation="flip")

References

[1] Whitley, D., 1994. A genetic algorithm tutorial. Statistics and computing, 4(2), pp.65-85.

crossover_process(dad, mom)[source]

Notes

Parameters
  • dad (np.array) – The position of dad

  • mom (np.array) – The position of mom

Returns

The position of child 1 and child 2

Return type

list

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration

mutation_process(child)[source]

Notes

  • https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_mutation.htm

  • There are 2 strategies that effects by the mutation probability: Mutated on single point or the whole vector.
    • Multiple points (whole vector) has 2 strategies selected via parameter: mutation
      • flip –> should set the pm small such as: [0.01 -> 0.2]

      • swap –> (default in this case) should set the pm small such as: [0.01 -> 0.2]

    • Single point has 4 strategies:
      • flip –> should set the pm large such as: [0.5 -> 0.9]

      • swap –> same as flip: pm in range [0.5 -> 0.9]

      • scramble –> should set the pm small enough such as: [0.4 -> 0.6]

      • inversion –> like scramble [0.4 -> 0.6]

Parameters

child (np.array) – The position of the child

Returns

The mutated vector of the child

Return type

np.array

selection_process(list_fitness)[source]

Notes

Parameters

list_fitness (np.array) – list of fitness values.

Returns

The position of dad and mom

Return type

list

survivor_process(pop, pop_child)[source]

The current survivor process is select the worst solution out of k-way solutions (tournament selection) and compare with child solutions. The better solution will be kept for the next generation.

Parameters
  • pop – The old population

  • pop_child – The new population

Returns

The new population

mealpy.evolutionary_based.MA

class mealpy.evolutionary_based.MA.BaseMA(problem, epoch=10000, pop_size=100, pc=0.85, pm=0.15, p_local=0.5, max_local_gens=20, bits_per_param=16, **kwargs)[source]

Bases: mealpy.optimizer.Optimizer

The original version of: Memetic Algorithm (MA)

Links:
  1. https://www.cleveralgorithms.com/nature-inspired/physical/memetic_algorithm.html

  2. https://github.com/clever-algorithms/CleverAlgorithms

Hyper-parameters should fine tuned in approximate range to get faster convergen toward the global optimum:
  • pc (float): [0.7, 0.95], cross-over probability, default = 0.85

  • pm (float): [0.05, 0.3], mutation probability, default = 0.15

  • p_local (float): [0.3, 0.7], Probability of local search for each agent, default=0.5

  • max_local_gens (int): [5, 25], number of local search agent will be created during local search mechanism, default=20

  • bits_per_param (int): [8, 16, 32], number of bits to decode a real number to 0-1 bitstring, default=16

Examples

>>> import numpy as np
>>> from mealpy.evolutionary_based.MA import BaseMA
>>>
>>> def fitness_function(solution):
>>>     return np.sum(solution**2)
>>>
>>> problem_dict1 = {
>>>     "fit_func": fitness_function,
>>>     "lb": [-10, -15, -4, -2, -8],
>>>     "ub": [10, 15, 12, 8, 20],
>>>     "minmax": "min",
>>>     "verbose": True,
>>> }
>>>
>>> epoch = 1000
>>> pop_size = 50
>>> pc = 0.85
>>> pm = 0.15
>>> p_local = 0.5
>>> max_local_gens = 20
>>> bits_per_param = 16
>>> model = BaseMA(problem_dict1, epoch, pop_size, pc, pm, p_local, max_local_gens, bits_per_param)
>>> best_position, best_fitness = model.solve()
>>> print(f"Solution: {best_position}, Fitness: {best_fitness}")

References

[1] Moscato, P., 1989. On evolution, search, optimization, genetic algorithms and martial arts: Towards memetic algorithms. Caltech concurrent computation program, C3P Report, 826, p.1989.

ID_BIT = 2
create_child(idx, pop_copy)[source]
create_solution()[source]
To get the position, fitness wrapper, target and obj list
  • A[self.ID_POS] –> Return: position

  • A[self.ID_TAR] –> Return: [target, [obj1, obj2, …]]

  • A[self.ID_TAR][self.ID_FIT] –> Return: target

  • A[self.ID_TAR][self.ID_OBJ] –> Return: [obj1, obj2, …]

Returns

wrapper of solution with format [position, [target, [obj1, obj2, …]], bitstring]

Return type

list

evolve(epoch)[source]

The main operations (equations) of algorithm. Inherit from Optimizer class

Parameters

epoch (int) – The current iteration