Subpackages

mealpy.optimizer module

class mealpy.optimizer.Optimizer(problem, kwargs)[source]

Bases: object

The 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
after_evolve(epoch)[source]
before_evolve(epoch)[source]
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

crossover_arthmetic_recombination(dad_pos=None, mom_pos=None)[source]
evolve(epoch)[source]
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

improved_ms(pop=None, g_best=None)[source]
initialization()[source]
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

termination_start()[source]
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