Subpackages¶
- mealpy.bio_based package
- mealpy.evolutionary_based package
- mealpy.human_based package
- mealpy.math_based package
- mealpy.music_based package
- mealpy.physics_based package
- mealpy.probabilistic_based package
- mealpy.swarm_based package
- mealpy.swarm_based.ABC
- mealpy.swarm_based.ACOR
- mealpy.swarm_based.ALO
- mealpy.swarm_based.AO
- mealpy.swarm_based.BA
- mealpy.swarm_based.BES
- mealpy.swarm_based.BFO
- mealpy.swarm_based.BSA
- mealpy.swarm_based.BeesA
- mealpy.swarm_based.COA
- mealpy.swarm_based.CSA
- mealpy.swarm_based.CSO
- mealpy.swarm_based.DO
- mealpy.swarm_based.EHO
- mealpy.swarm_based.FA
- mealpy.swarm_based.FFA
- mealpy.swarm_based.FOA
- mealpy.swarm_based.GOA
- mealpy.swarm_based.GWO
- mealpy.swarm_based.HGS
- mealpy.swarm_based.HHO
- mealpy.swarm_based.JA
- mealpy.swarm_based.MFO
- mealpy.swarm_based.MRFO
- mealpy.swarm_based.MSA
- mealpy.swarm_based.NMRA
- mealpy.swarm_based.PFA
- mealpy.swarm_based.PSO
- mealpy.swarm_based.SFO
- mealpy.swarm_based.SHO
- mealpy.swarm_based.SLO
- mealpy.swarm_based.SRSR
- mealpy.swarm_based.SSA
- mealpy.swarm_based.SSO
- mealpy.swarm_based.SSpiderA
- mealpy.swarm_based.SSpiderO
- mealpy.swarm_based.WOA
- mealpy.system_based package
- mealpy.utils package
mealpy.optimizer module¶
- class mealpy.optimizer.Optimizer(problem, kwargs)[source]¶
Bases:
objectThe base class of all algorithms. All methods in this class will be inherited
Notes
The solve() is the most important method, trained the model
The parallel (multithreading or multiprocessing) is used in method: create_population(), update_fitness_population()
- The general format of:
population = [agent_1, agent_2, …, agent_N]
agent = global_best = solution = [position, target]
target = [fitness value, objective_list]
objective_list = [obj_1, obj_2, …, obj_M]
- Access to the:
position of solution/agent: solution[0] or solution[self.ID_POS] or model.solution[model.ID_POS]
fitness: solution[1][0] or solution[self.ID_TAR][self.ID_FIT] or model.solution[model.ID_TAR][model.ID_FIT]
objective values: solution[1][1] or solution[self.ID_TAR][self.ID_OBJ] or model.solution[model.ID_TAR][model.ID_OBJ]
- EPSILON = 1e-09¶
- ID_FIT = 0¶
- ID_OBJ = 1¶
- ID_POS = 0¶
- ID_TAR = 1¶
- compare_agent(agent_a: list, agent_b: list)[source]¶
- Parameters
agent_a (list) – Solution a
agent_b (list) – Solution b
- Returns
Return True if solution a better than solution b and otherwise
- Return type
boolean
- create_opposition_position(agent=None, g_best=None)[source]¶
- Parameters
agent – The current solution (agent)
g_best – the global best solution
- Returns
The opposite solution
- create_population(pop_size=None)[source]¶
- Parameters
pop_size (int) – number of solutions
- Returns
population or list of solutions/agents
- Return type
list
- create_solution()[source]¶
- To get the position, target wrapper [fitness and obj list]
A[self.ID_POS] –> Return: position
A[self.ID_TAR] –> Return: [fitness, [obj1, obj2, …]]
A[self.ID_TAR][self.ID_FIT] –> Return: fitness
A[self.ID_TAR][self.ID_OBJ] –> Return: [obj1, obj2, …]
- Returns
wrapper of solution with format [position, [fitness, [obj1, obj2, …]]]
- Return type
list
- get_better_solution(agent1: list, agent2: list)[source]¶
- Parameters
agent1 (list) – A solution
agent2 (list) – Another solution
- Returns
The better solution between them
- get_fitness_position(position=None)[source]¶
- Parameters
position (nd.array) – 1-D numpy array
- Returns
[target, [obj1, obj2, …]]
- get_fitness_solution(solution=None)[source]¶
- Parameters
solution (list) – A solution with format [position, [target, [obj1, obj2, …]]]
- Returns
[target, [obj1, obj2, …]]
- get_global_best_global_worst_solution(pop=None)[source]¶
- Parameters
pop (list) – The population
- Returns
The global best and the global worst solution
- Return type
list
- get_global_best_solution(pop: list)[source]¶
Sort population and return the sorted population and the best solution
- Parameters
pop (list) – The population of pop_size individuals
- Returns
Sorted population and global best solution
- get_index_kway_tournament_selection(pop=None, k_way=0.2, output=2, reverse=False)[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=1.0, multiplier=0.001, case=0)[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
case (int) –
Should be one of these value [0, 1, -1].
0: return multiplier * s * np.random.uniform()
1: return multiplier * s * np.random.normal(0, 1)
-1: return multiplier * s
- Returns
The step size of Levy-flight trajectory
- Return type
int
- get_sorted_strim_population(pop=None, pop_size=None, reverse=False)[source]¶
- Parameters
pop (list) – The population
pop_size (int) – The number of population
- Returns
The sorted population with pop_size size
- get_special_fitness(pop=None)[source]¶
- Parameters
pop (list) – The population
- Returns
Total fitness, best fitness, worst fitness
- Return type
list
- get_special_solutions(pop=None, best=3, worst=3)[source]¶
- Parameters
pop (list) – The population
best (int) – Top k1 best solutions, default k1=3, it can be None
worst (int) – Top k2 worst solutions, default k2=3, it can be None
- Returns
sorted_population, k1 best solutions and k2 worst solutions
- Return type
list
- greedy_selection_population(pop_old=None, pop_new=None)[source]¶
- Parameters
pop_old (list) – The current population
pop_new (list) – The next population
- Returns
The new population with better solutions
- levy_flight(epoch=None, position=None, g_best_position=None, step=0.001, case=0)[source]¶
Get the Levy-flight position of current agent
- Parameters
epoch (int) – The current epoch/iteration
position – The position of current agent
g_best_position – The position of the global best solution
step (float) – The step size in Levy-flight, default = 0.001
case (int) – Should be one of these value [0, 1, 2]
- Returns
The Levy-flight position of current agent
- print_epoch(epoch, runtime)[source]¶
Print out the detailed information of training process
- Parameters
epoch (int) – current iteration
runtime (float) – the runtime for current iteration
- save_optimization_process()[source]¶
- Save important data for later use such as:
list_global_best_fit
list_current_best_fit
list_diversity
list_exploitation
list_exploration
- solve(mode='sequential')[source]¶
- Parameters
mode (str) –
‘sequential’, ‘thread’, ‘process’.
’sequential’: recommended for simple and small task (< 10 seconds for calculating objective)
’thread’: recommended for IO bound task, or small computing task (< 2 minutes for calculating objective)
’process’: recommended for hard and big task (> 2 minutes for calculating objective)
- Returns
[position, fitness value]
- Return type
list
- update_fitness_population(pop=None)[source]¶
- Parameters
pop (list) – the population
- Returns
population with updated fitness value
- Return type
list
- update_global_best_solution(pop=None, save=True)[source]¶
Update the global best solution saved in variable named: self.history_list_g_best
- Parameters
pop (list) – The population of pop_size individuals
save (bool) – True if you want to add new current global best and False if you just want update the current one.
- Returns
Sorted population and the global best solution
- Return type
list