mealpy.evolutionary_based package¶
mealpy.evolutionary_based.CRO module¶
- class mealpy.evolutionary_based.CRO.OCRO(epoch: int = 10000, pop_size: int = 100, po: float = 0.4, Fb: float = 0.9, Fa: float = 0.1, Fd: float = 0.1, Pd: float = 0.5, GCR: float = 0.1, gamma_min: float = 0.02, gamma_max: float = 0.2, n_trials: int = 3, restart_count: int = 20, **kwargs: object)[source]¶
Bases:
mealpy.evolutionary_based.CRO.OriginalCRO
The original version of: Opposition-based Coral Reefs Optimization (OCRO)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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.1, 0.7], the maximum of probability of depredation
GCR (float): [0.05, 0.2], probability for mutation process
gamma_min (float): [0.01, 0.1] factor for mutation process
gamma_max (float): [0.1, 0.5] factor 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 import FloatVar, CRO >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = CRO.OCRO(epoch=1000, pop_size=50, po = 0.4, Fb = 0.9, Fa = 0.1, Fd = 0.1, Pd = 0.5, GCR = 0.1, gamma_min = 0.02, gamma_max = 0.2, n_trials = 5, restart_count = 50) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
- class mealpy.evolutionary_based.CRO.OriginalCRO(epoch: int = 10000, pop_size: int = 100, po: float = 0.4, Fb: float = 0.9, Fa: float = 0.1, Fd: float = 0.1, Pd: float = 0.5, GCR: float = 0.1, gamma_min: float = 0.02, gamma_max: float = 0.2, n_trials: int = 3, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Coral Reefs Optimization (CRO)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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.1, 0.7], Probability of depredation
GCR (float): [0.05, 0.2], probability for mutation process
gamma_min (float): [0.01, 0.1] factor for mutation process
gamma_max (float): [0.1, 0.5] factor 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 import FloatVar, CRO >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = CRO.OriginalCRO(epoch=1000, pop_size=50, po = 0.4, Fb = 0.9, Fa = 0.1, Fd = 0.1, Pd = 0.5, GCR = 0.1, gamma_min = 0.02, gamma_max = 0.2, n_trials = 5) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
mealpy.evolutionary_based.DE module¶
- class mealpy.evolutionary_based.DE.JADE(epoch: int = 10000, pop_size: int = 100, miu_f: float = 0.5, miu_cr: float = 0.5, pt: float = 0.1, ap: float = 0.1, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Differential Evolution (JADE)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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 import FloatVar, DE >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = DE.JADE(epoch=1000, pop_size=50, miu_f = 0.5, miu_cr = 0.5, pt = 0.1, ap = 0.1) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
- class mealpy.evolutionary_based.DE.OriginalDE(epoch: int = 10000, pop_size: int = 100, wf: float = 0.1, cr: float = 0.9, strategy: int = 0, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Differential Evolution (DE)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
wf (float): [-1., 1.0], weighting factor, default = 0.1
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 import FloatVar, DE >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = DE.OriginalDE(epoch=1000, pop_size=50, wf = 0.7, cr = 0.9, strategy = 0) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
- class mealpy.evolutionary_based.DE.SADE(epoch: int = 10000, pop_size: int = 100, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Self-Adaptive Differential Evolution (SADE)
Examples
>>> import numpy as np >>> from mealpy import FloatVar, DE >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = DE.SADE(epoch=1000, pop_size=50) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
- class mealpy.evolutionary_based.DE.SAP_DE(epoch: int = 1000, pop_size: int = 100, branch: str = 'ABS', **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Differential Evolution with Self-Adaptive Populations (SAP_DE)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
branch (str): [“ABS” or “REL”], gaussian (absolute) or uniform (relative) method
Examples
>>> import numpy as np >>> from mealpy import FloatVar, DE >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = DE.SAP_DE(epoch=1000, pop_size=50, branch = "ABS") >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
References
[1] Teo, J., 2006. Exploring dynamic self-adaptive populations in differential evolution. Soft Computing, 10(8), pp.673-686.
- evolve(epoch)[source]¶
The main operations (equations) of algorithm. Inherit from Optimizer class
- Parameters
epoch (int) – The current iteration
- generate_empty_agent(solution: Optional[numpy.ndarray] = None) mealpy.utils.agent.Agent [source]¶
Generate new agent with solution
- Parameters
solution (np.ndarray) – The solution
mealpy.evolutionary_based.EP module¶
- class mealpy.evolutionary_based.EP.LevyEP(epoch: int = 10000, pop_size: int = 100, bout_size: float = 0.05, **kwargs: object)[source]¶
Bases:
mealpy.evolutionary_based.EP.OriginalEP
The developed Levy-flight version: Evolutionary Programming (LevyEP)
Notes
Levy-flight is applied to EP, flow and some equations is changed.
- Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
bout_size (float): [0.05, 0.2], percentage of child agents implement tournament selection
Examples
>>> import numpy as np >>> from mealpy import FloatVar, EP >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = EP.LevyEP(epoch=1000, pop_size=50, bout_size = 0.05) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
- class mealpy.evolutionary_based.EP.OriginalEP(epoch: int = 10000, pop_size: int = 100, bout_size: float = 0.05, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Evolutionary Programming (EP)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
bout_size (float): [0.05, 0.2], percentage of child agents implement tournament selection
Examples
>>> import numpy as np >>> from mealpy import FloatVar, EP >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = EP.OriginalEP(epoch=1000, pop_size=50, bout_size = 0.05) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
- evolve(epoch)[source]¶
The main operations (equations) of algorithm. Inherit from Optimizer class
- Parameters
epoch (int) – The current iteration
- generate_empty_agent(solution: Optional[numpy.ndarray] = None) mealpy.utils.agent.Agent [source]¶
Generate new agent with solution
- Parameters
solution (np.ndarray) – The solution
mealpy.evolutionary_based.ES module¶
- class mealpy.evolutionary_based.ES.CMA_ES(epoch: int = 10000, pop_size: int = 100, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Covariance Matrix Adaptation Evolution Strategy (CMA-ES)
Examples
>>> import numpy as np >>> from mealpy import FloatVar, ES >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = ES.CMA_ES(epoch=1000, pop_size=50) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
References
[1] Hansen, N., & Ostermeier, A. (2001). Completely derandomized self-adaptation in evolution strategies. Evolutionary computation, 9(2), 159-195.
- evolve(epoch)[source]¶
The main operations (equations) of algorithm. Inherit from Optimizer class
- Parameters
epoch (int) – The current iteration
- generate_empty_agent(solution: Optional[numpy.ndarray] = None) mealpy.utils.agent.Agent [source]¶
Generate new agent with solution
- Parameters
solution (np.ndarray) – The solution
- class mealpy.evolutionary_based.ES.LevyES(epoch: int = 10000, pop_size: int = 100, lamda: float = 0.75, **kwargs: object)[source]¶
Bases:
mealpy.evolutionary_based.ES.OriginalES
The developed Levy-flight version: Evolution Strategies (ES)
Notes
The Levy-flight is applied, the flow and equations is changed
Link: https://www.cleveralgorithms.com/nature-inspired/evolution/evolution_strategies.html
- Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
lamda (float): [0.5, 1.0], Percentage of child agents evolving in the next generation
Examples
>>> import numpy as np >>> from mealpy import FloatVar, ES >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = ES.LevyES(epoch=1000, pop_size=50, lamda = 0.75) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
References
[1] Beyer, H.G. and Schwefel, H.P., 2002. Evolution strategies–a comprehensive introduction. Natural computing, 1(1), pp.3-52.
- class mealpy.evolutionary_based.ES.OriginalES(epoch: int = 10000, pop_size: int = 100, lamda: float = 0.75, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Evolution Strategies (ES)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
lamda (float): [0.5, 1.0], Percentage of child agents evolving in the next generation
Examples
>>> import numpy as np >>> from mealpy import FloatVar, ES >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = ES.OriginalES(epoch=1000, pop_size=50, lamda = 0.75) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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
- generate_empty_agent(solution: Optional[numpy.ndarray] = None) mealpy.utils.agent.Agent [source]¶
Generate new agent with solution
- Parameters
solution (np.ndarray) – The solution
- class mealpy.evolutionary_based.ES.Simple_CMA_ES(epoch: int = 10000, pop_size: int = 100, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The simple version of: Covariance Matrix Adaptation Evolution Strategy (Simple-CMA-ES)
- Links:
Inspired from this version: https://github.com/jenkspt/CMA-ES
Examples
>>> import numpy as np >>> from mealpy import FloatVar, ES >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = ES.Simple_CMA_ES(epoch=1000, pop_size=50) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
References
[1] Hansen, N., & Ostermeier, A. (2001). Completely derandomized self-adaptation in evolution strategies. Evolutionary computation, 9(2), 159-195.
mealpy.evolutionary_based.FPA module¶
- class mealpy.evolutionary_based.FPA.OriginalFPA(epoch: int = 10000, pop_size: int = 100, p_s: float = 0.8, levy_multiplier: float = 0.1, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Flower Pollination Algorithm (FPA)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence toward the global optimum:
p_s (float): [0.5, 0.95], switch probability, default = 0.8
levy_multiplier: [0.0001, 1000], mutiplier factor of Levy-flight trajectory, depends on the problem
Examples
>>> import numpy as np >>> from mealpy import FloatVar, FPA >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "minmax": "min", >>> "obj_func": objective_function >>> } >>> >>> model = FPA.OriginalFPA(epoch=1000, pop_size=50, p_s = 0.8, levy_multiplier = 0.2) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
mealpy.evolutionary_based.GA module¶
- class mealpy.evolutionary_based.GA.BaseGA(epoch: int = 10000, pop_size: int = 100, pc: float = 0.95, pm: float = 0.025, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Genetic Algorithm (GA)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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, default = True
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 import FloatVar, GA >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = GA.BaseGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}") >>> >>> model2 = GA.BaseGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05, selection="tournament", k_way=0.4, crossover="multi_points") >>> >>> model3 = GA.BaseGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05, crossover="one_point", mutation="scramble") >>> >>> model4 = GA.BaseGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05, crossover="arithmetic", mutation_multipoints=True, mutation="swap") >>> >>> model5 = GA.BaseGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05, selection="roulette", crossover="multi_points") >>> >>> model6 = GA.BaseGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05, selection="random", mutation="inversion") >>> >>> model7 = GA.BaseGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05, 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
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_crossover.htm
Default crossover strategy is “uniform”
Other strategy like “arithmetic”, “one_point”, “multi_points” can be selected via parameter: crossover
- 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 –> (default in this case) should set the pm small such as: [0.01 -> 0.2]
swap –> 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_00__(pop_selected)[source]¶
Notes
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_parent_selection.htm
Default selection strategy is Tournament with k% = 0.2.
Other strategy like “roulette” and “random” can be selected via Optional parameter “selection”
- Parameters
pop_selected (np.array) – a population that will be selected
- Returns
The position of dad and mom
- Return type
list
- selection_process_01__(pop_dad, pop_mom)[source]¶
Notes
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_parent_selection.htm
Default selection strategy is Tournament with k% = 0.2.
Other strategy like “roulette” and “random” can be selected via Optional parameter “selection”
- Returns
The position of dad and mom
- Return type
list
- selection_process__(list_fitness)[source]¶
Notes
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_parent_selection.htm
Default selection strategy is Tournament with k% = 0.2.
Other strategy like “roulette” and “random” can be selected via Optional parameter “selection”
- 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
- class mealpy.evolutionary_based.GA.EliteMultiGA(epoch=10000, pop_size=100, pc=0.95, pm=0.8, selection='roulette', crossover='uniform', mutation='swap', k_way=0.2, elite_best=0.1, elite_worst=0.3, strategy=0, **kwargs)[source]¶
Bases:
mealpy.evolutionary_based.GA.MultiGA
The developed elite multipoints-mutation version of: Genetic Algorithm (GA)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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 (str): Optional, can be [“flip”, “swap”] for multipoints
elite_best (float/int): Optional, can be float (percentage of the best in elite group), or int (the number of best elite), default = 0.1
elite_worst (float/int): Opttional, can be float (percentage of the worst in elite group), or int (the number of worst elite), default = 0.3
- strategy (int): Optional, can be 0 or 1. If = 0, the selection is select parents from (elite_worst + non_elite_group).
Else, the selection will select dad from elite_worst and mom from non_elite_group.
pop_size = elite_group (elite_best + elite_worst) + non_elite_group
Examples
>>> import numpy as np >>> from mealpy import FloatVar, GA >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = GA.EliteMultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.05, selection = "roulette", crossover = "uniform", mutation = "swap") >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}")
References
[1] Whitley, D., 1994. A genetic algorithm tutorial. Statistics and computing, 4(2), pp.65-85.
- class mealpy.evolutionary_based.GA.EliteSingleGA(epoch=10000, pop_size=100, pc=0.95, pm=0.8, selection='roulette', crossover='uniform', mutation='swap', k_way=0.2, elite_best=0.1, elite_worst=0.3, strategy=0, **kwargs)[source]¶
Bases:
mealpy.evolutionary_based.GA.SingleGA
The developed elite single-point mutation of: Genetic Algorithm (GA)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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”
crossover (str): Optional, can be [“one_point”, “multi_points”, “uniform”, “arithmetic”], default = “uniform”
mutation (str): Optional, can be [“flip”, “swap”, “scramble”, “inversion”] for one-point
k_way (float): Optional, set it when use “tournament” selection, default = 0.2
elite_best (float/int): Optional, can be float (percentage of the best in elite group), or int (the number of best elite), default = 0.1
elite_worst (float/int): Opttional, can be float (percentage of the worst in elite group), or int (the number of worst elite), default = 0.3
- strategy (int): Optional, can be 0 or 1. If = 0, the selection is select parents from (elite_worst + non_elite_group).
Else, the selection will select dad from elite_worst and mom from non_elite_group.
pop_size = elite_group (elite_best + elite_worst) + non_elite_group
Examples
>>> import numpy as np >>> from mealpy import FloatVar, GA >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = GA.EliteSingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection = "roulette", crossover = "uniform", >>> mutation = "swap", elite_best = 0.1, elite_worst = 0.3, strategy = 0) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}") >>> >>> model2 = GA.EliteSingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="tournament", k_way=0.4, crossover="multi_points") >>> >>> model3 = GA.EliteSingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="one_point", mutation="scramble") >>> >>> model4 = GA.EliteSingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="arithmetic", mutation="swap") >>> >>> model5 = GA.EliteSingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="roulette", crossover="multi_points") >>> >>> model6 = GA.EliteSingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="random", mutation="inversion") >>> >>> model7 = GA.EliteSingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="arithmetic", mutation="flip")
References
[1] Whitley, D., 1994. A genetic algorithm tutorial. Statistics and computing, 4(2), pp.65-85.
- class mealpy.evolutionary_based.GA.MultiGA(epoch: int = 10000, pop_size: int = 100, pc: float = 0.95, pm: float = 0.025, selection: str = 'roulette', crossover: str = 'arithmetic', mutation: str = 'flip', k_way: float = 0.2, **kwargs: object)[source]¶
Bases:
mealpy.evolutionary_based.GA.BaseGA
The developed multipoints-mutation version of: Genetic Algorithm (GA)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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 (str): Optional, can be [“flip”, “swap”] for multipoints
Examples
>>> import numpy as np >>> from mealpy import FloatVar, GA >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = GA.MultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection = "roulette", crossover = "uniform", mutation = "swap", k_way=0.2) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}") >>> >>> model2 = GA.MultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="tournament", k_way=0.4, crossover="multi_points") >>> >>> model3 = GA.MultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="one_point", mutation="flip") >>> >>> model4 = GA.MultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="arithmetic", mutation_multipoints=True, mutation="swap") >>> >>> model5 = GA.MultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="roulette", crossover="multi_points") >>> >>> model6 = GA.MultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="random", mutation="swap") >>> >>> model7 = GA.MultiGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="arithmetic", mutation="flip")
References
[1] Whitley, D., 1994. A genetic algorithm tutorial. Statistics and computing, 4(2), pp.65-85.
- mutation_process__(child)[source]¶
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_mutation.htm
- Mutated on the whole vector is effected by parameter: pm
flip –> (default in this case) should set the pm small such as: [0.01 -> 0.2]
swap –> should set the pm small such as: [0.01 -> 0.2]
- Parameters
child (np.array) – The position of the child
- Returns
The mutated vector of the child
- Return type
np.array
- class mealpy.evolutionary_based.GA.SingleGA(epoch: int = 10000, pop_size: int = 100, pc: float = 0.95, pm: float = 0.8, selection: str = 'roulette', crossover: str = 'uniform', mutation: str = 'swap', k_way: float = 0.2, **kwargs: object)[source]¶
Bases:
mealpy.evolutionary_based.GA.BaseGA
The developed single-point mutation of: Genetic Algorithm (GA)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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”
crossover (str): Optional, can be [“one_point”, “multi_points”, “uniform”, “arithmetic”], default = “uniform”
mutation (str): Optional, can be [“flip”, “swap”, “scramble”, “inversion”] for one-point
k_way (float): Optional, set it when use “tournament” selection, default = 0.2
Examples
>>> import numpy as np >>> from mealpy import FloatVar, GA >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = GA.SingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection = "roulette", crossover = "uniform", mutation = "swap") >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.fitness}") >>> >>> model2 = GA.SingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="tournament", k_way=0.4, crossover="multi_points") >>> >>> model3 = GA.SingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="one_point", mutation="scramble") >>> >>> model4 = GA.SingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="arithmetic", mutation="swap") >>> >>> model5 = GA.SingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="roulette", crossover="multi_points") >>> >>> model6 = GA.SingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, selection="random", mutation="inversion") >>> >>> model7 = GA.SingleGA(epoch=1000, pop_size=50, pc=0.9, pm=0.8, crossover="arithmetic", mutation="flip")
References
[1] Whitley, D., 1994. A genetic algorithm tutorial. Statistics and computing, 4(2), pp.65-85.
- mutation_process__(child)[source]¶
https://www.tutorialspoint.com/genetic_algorithms/genetic_algorithms_mutation.htm
- The mutation process is effected by parameter: pm
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
mealpy.evolutionary_based.MA module¶
- class mealpy.evolutionary_based.MA.OriginalMA(epoch: int = 10000, pop_size: int = 100, pc: float = 0.85, pm: float = 0.15, p_local: float = 0.5, max_local_gens: int = 10, bits_per_param: int = 4, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Memetic Algorithm (MA)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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=10
bits_per_param (int): [2, 4, 8, 16], number of bits to decode a real number to 0-1 bitstring, default=4
Examples
>>> import numpy as np >>> from mealpy import FloatVar, MA >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = MA.OriginalMA(epoch=1000, pop_size=50, pc = 0.85, pm = 0.15, p_local = 0.5, max_local_gens = 10, bits_per_param = 4) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
- decode__(bitstring: Optional[str] = None) numpy.ndarray [source]¶
Decode the random bitstring into real number
- Parameters
bitstring (str) – “11000000100101000101010” - bits_per_param = 16, 32 bit for 2 variable. eg. x1 and x2
- Returns
list of real number (vector)
- Return type
list
- evolve(epoch)[source]¶
The main operations (equations) of algorithm. Inherit from Optimizer class
- Parameters
epoch (int) – The current iteration
- generate_empty_agent(solution: Optional[numpy.ndarray] = None) mealpy.utils.agent.Agent [source]¶
Generate new agent with solution
- Parameters
solution (np.ndarray) – The solution
mealpy.evolutionary_based.SHADE module¶
- class mealpy.evolutionary_based.SHADE.L_SHADE(epoch: int = 750, pop_size: int = 100, miu_f: float = 0.5, miu_cr: float = 0.5, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Linear Population Size Reduction Success-History Adaptation Differential Evolution (LSHADE)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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 import FloatVar, SHADE >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = SHADE.L_SHADE(epoch=1000, pop_size=50, miu_f = 0.5, miu_cr = 0.5) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.
- class mealpy.evolutionary_based.SHADE.OriginalSHADE(epoch: int = 750, pop_size: int = 100, miu_f: float = 0.5, miu_cr: float = 0.5, **kwargs: object)[source]¶
Bases:
mealpy.optimizer.Optimizer
The original version of: Success-History Adaptation Differential Evolution (OriginalSHADE)
- Links:
- Hyper-parameters should fine-tune in approximate range to get faster convergence 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 import FloatVar, SHADE >>> >>> def objective_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "bounds": FloatVar(n_vars=30, lb=(-10.,) * 30, ub=(10.,) * 30, name="delta"), >>> "obj_func": objective_function, >>> "minmax": "min", >>> } >>> >>> model = SHADE.OriginalSHADE(epoch=1000, pop_size=50, miu_f = 0.5, miu_cr = 0.5) >>> g_best = model.solve(problem_dict) >>> print(f"Solution: {g_best.solution}, Fitness: {g_best.target.fitness}") >>> print(f"Solution: {model.g_best.solution}, Fitness: {model.g_best.target.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.