mealpy.utils package¶
mealpy.utils.history¶
- class mealpy.utils.history.History[source]¶
Bases:
objectA History class is responsible for saving each iteration’s output.
Notes
- Access to variables in this class:
list_global_best: List of global best SOLUTION found so far in all previous generations
list_current_best: List of current best SOLUTION in each previous generations
list_epoch_time: List of runtime for each generation
list_global_best_fit: List of global best FITNESS found so far in all previous generations
list_current_best_fit: List of current best FITNESS in each previous generations
list_diversity: List of DIVERSITY of swarm in all generations
list_exploitation: List of EXPLOITATION percentages for all generations
list_exploration: List of EXPLORATION percentages for all generations
list_population: List of POPULATION in each generations
Warning, the last variable ‘list_population’ can cause the error related to ‘memory’ when using pickle to save model. Better to delete that variable or assign to empty list [] to reduce the ‘memory’.
- There are 8 methods to draw available in this class:
save_global_best_fitness_chart()
save_local_best_fitness_chart()
save_global_objectives_chart()
save_local_objectives_chart()
save_exploration_exploitation_chart()
save_diversity_chart()
save_runtime_chart()
save_trajectory_chart()
Examples
>>> import numpy as np >>> from mealpy.swarm_based.PSO import BasePSO >>> >>> def fitness_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "fit_func": fitness_function, >>> "lb": [-10, -15, -4, -2, -8], >>> "ub": [10, 15, 12, 8, 20], >>> "minmax": "min", >>> "verbose": True, >>> } >>> model = BasePSO(problem_dict, epoch=1000, pop_size=50) >>> >>> model.history.save_global_objectives_chart(filename="hello/goc") >>> model.history.save_local_objectives_chart(filename="hello/loc") >>> model.history.save_global_best_fitness_chart(filename="hello/gbfc") >>> model.history.save_local_best_fitness_chart(filename="hello/lbfc") >>> model.history.save_runtime_chart(filename="hello/rtc") >>> model.history.save_exploration_exploitation_chart(filename="hello/eec") >>> model.history.save_diversity_chart(filename="hello/dc") >>> model.history.save_trajectory_chart(list_agent_idx=[3, 5], list_dimensions=[3], filename="hello/tc") >>> >>> ## Get list of population after all generations >>> print(model.history.list_population)
- save_diversity_chart(title='Diversity Measurement Chart', algorithm_name='GA', filename='diversity-chart', verbose=True)[source]¶
- save_exploration_exploitation_chart(title='Exploration vs Exploitation Percentages', list_colors=('blue', 'orange'), filename='exploration-exploitation-chart', verbose=True)[source]¶
- save_global_best_fitness_chart(title='Global Best Fitness', color='b', x_label='#Iteration', y_label='Function Value', filename='global-best-fitness-chart', verbose=True)[source]¶
- save_global_objectives_chart(title='Global Objectives Chart', x_label='#Iteration', y_label='Function Value', filename='global-objectives-chart', verbose=True)[source]¶
- save_local_best_fitness_chart(title='Local Best Fitness', color='b', x_label='#Iteration', y_label='Function Value', filename='local-best-fitness-chart', verbose=True)[source]¶
- save_local_objectives_chart(title='Local Objectives Chart', x_label='#Iteration', y_label='Objective Function Value', filename='local-objectives-chart', verbose=True)[source]¶
mealpy.utils.problem¶
- class mealpy.utils.problem.Problem(**kwargs)[source]¶
Bases:
objectDefine the mathematical form of optimization problem
Notes
fit_func (callable): your fitness function
lb (list, int, float): lower bound, should be list of values
ub (list, int, float): upper bound, should be list of values
minmax (str): “min” or “max” problem (Optional, default = “min”)
verbose (bool): print out the training process or not (Optional, default = True)
n_dims (int): number of dimensions / problem size (Optional)
obj_weight: list weights for all your objectives (Optional, default = [1, 1, …1])
problem (dict): dictionary of the problem (contains at least the parameter 1, 2, 3) (Optional)
amend_position(callable): Depend on your problem, may need to design an amend_position function (Optional for continuous domain, Required for discrete domain)
Examples
>>> ## 1st way: >>> import numpy as np >>> from mealpy.swarm_based.PSO import BasePSO >>> >>> def fitness_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "fit_func": fitness_function, >>> "lb": [-10, -15, -4, -2, -8], >>> "ub": [10, 15, 12, 8, 20], >>> "minmax": "min", >>> "verbose": True, >>> } >>> model1 = BasePSO(problem_dict, epoch=1000, pop_size=50) >>> >>> ## 2nd way: >>> from mealpy.utils.problem import Problem >>> >>> problem_obj2 = Problem(fit_func=fitness_function, lb=[-10, -15, -4, -2, -8], ub=[10, 15, 12, 8, 20], minmax="min", verbose=True) >>> model3 = BasePSO(problem_obj2, epoch=1000, pop_size=50)
- DEFAULT_LB = -1¶
- DEFAULT_UB = 1¶
- ID_MAX_PROB = -1¶
- ID_MIN_PROB = 0¶
- amend_position(position=None)[source]¶
Depend on what kind of problem are we trying to solve, there will be an different amend_position function to rebound the position of agent into the valid range.
- Parameters
position – vector position (location) of the solution.
- Returns
Amended position (make the position is in bound)
mealpy.utils.termination¶
- class mealpy.utils.termination.Termination(**kwargs)[source]¶
Bases:
objectDefine the Stopping Condition (Termination) for the Optimizer
Notes
By default, the stopping condition is maximum generations (epochs/iterations) in Optimizer class.
By using this class, the default termination will be overridden
- In general, there are 4 termination cases: FE, MG, ES, TB
FE: Number of Function Evaluation
MG: Maximum Generations / Epochs - This is default in all algorithms
ES: Early Stopping - Same idea in training neural network (If the global best solution not better an epsilon after K epochs then stop the program)
TB: Time Bound - You just want your algorithm run in K seconds. Especially when comparing different algorithms.
- Parameters for Termination class
mode (str): FE, MG, ES or TB
quantity (int): value for termination type
problem (dict): dictionary of the termination (contains at least the parameter ‘mode’ and ‘quantity’) (Optional)
Examples
>>> ## 1st way to define and use termination object >>> import numpy as np >>> from mealpy.swarm_based.PSO import BasePSO >>> >>> def fitness_function(solution): >>> return np.sum(solution**2) >>> >>> problem_dict = { >>> "fit_func": fitness_function, >>> "lb": [-10, -15, -4, -2, -8], >>> "ub": [10, 15, 12, 8, 20], >>> } >>> >>> term_dict = { >>> "mode": "FE", >>> "quantity": 100000 # 100000 number of function evaluation >>> } >>> model1 = BasePSO(problem_dict, epoch=1000, pop_size=50, termination=term_dict) >>> >>> ## 2nd and 3rd ways: >>> from mealpy.utils.termination import Termination >>> >>> term_obj2 = Termination(termination = term_dict) >>> model2 = BasePSO(problem_dict, epoch=1000, pop_size=50, termination=term_obj2) >>> >>> term_obj3 = Termination(termination = term_dict) >>> model3 = BasePSO(problem_dict, epoch=1000, pop_size=50, termination=term_obj3)
- DEFAULT_MAX_ES = 20¶
- DEFAULT_MAX_FE = 100000¶
- DEFAULT_MAX_MG = 1000¶
- DEFAULT_MAX_TB = 20¶